Linux下platform驱动简介

一. 简介

上一篇文章简单学习了Linux内核的 platform总线。文章地址如下:

Linux下platform总线-CSDN博客

在 SOC 中有些外设是没有总线这个概念的,但是又要使用总 线、驱动和设备模型该怎么办呢?
为了解决此问题, Linux 提出了 platform 这个虚拟总线,相应 的就有 platform_driver 和 platform_device 。

本文来简单学习一下 Linux内核中的 platform驱动。

二. Linux下platform驱动简介

1. platform_driver 结 构 体

platform_driver 结 构 体 表 示 platform 驱动,此结构体定义在文件 include/linux/platform_device.h 中,内容如下:

复制代码
struct platform_driver {
	int (*probe)(struct platform_device *);
	int (*remove)(struct platform_device *);
	void (*shutdown)(struct platform_device *);
	int (*suspend)(struct platform_device *, pm_message_t state);
	int (*resume)(struct platform_device *);
	struct device_driver driver;
	const struct platform_device_id *id_table;
	bool prevent_deferred_probe;
};

第 2 行,probe 函数,当驱动与设备匹配成功以后, probe 函数就会执行,非常重要的函数!!
一般驱动的提供者会编写,如果自己要编写一个全新的驱动,那么 probe 就需要自行实现。
第 7 行, driver 成员,为 device_driver 结构体变量, Linux 内核里面大量使用到了面向对象 的思维, device_driver 相当于基类,提供了最基础的驱动框架。 plaform_driver 继承了这个基类, 然后在此基础上又添加了一些特有的成员变量。

第 8 行, id_table 表,也就是我们上一篇文章(platform总线)讲解 platform 总线匹配驱动和设备的时候采用的 第三种方法。
id_table 是个表 ( 也就是数组 ) ,每个元素的类型为 platform_device_id , platform_device_id 结构体内容如下:

复制代码
struct platform_device_id {
	char name[PLATFORM_NAME_SIZE];
	kernel_ulong_t driver_data;
};

2. device_driver 结构体

device_driver 结构体定义在 include/linux/device.h , device_driver 结构体内容如下:

复制代码
struct device_driver {
	const char		*name;
	struct bus_type		*bus;

	struct module		*owner;
	const char		*mod_name;	/* used for built-in modules */

	bool suppress_bind_attrs;	/* disables bind/unbind via sysfs */

	const struct of_device_id	*of_match_table;
	const struct acpi_device_id	*acpi_match_table;

	int (*probe) (struct device *dev);
	int (*remove) (struct device *dev);
	void (*shutdown) (struct device *dev);
	int (*suspend) (struct device *dev, pm_message_t state);
	int (*resume) (struct device *dev);
	const struct attribute_group **groups;

	const struct dev_pm_ops *pm;

	struct driver_private *p;
};

上面 device_driver **结构体中,**第 10 行, of_match_table 就是采用设备树时,驱动使用的匹配表,同样是数组,每个匹 配项都为 of_device_id 结构体类型,此结构体定义在文件 include/linux/mod_devicetable.h 中,内 容如下:

复制代码
struct platform_device_id {
	char name[PLATFORM_NAME_SIZE];
	kernel_ulong_t driver_data;
};

struct of_device_id 结构体中,第 4 行的 compatible **非常重要,**因为对于设备树而言,就是通过设备节点的 compatible 属性值和 of_match_table 中每个项目的 compatible 成员变量进行比较,如果有相等,就表示设备和此驱动匹配成功。

关于Linux内核源码中的 platform驱动代码,简单学习到这里。

接下来学习一下,在编写 platform 驱动时需要做的工作。

相关推荐
西阳未落10 分钟前
Linux(14)——库的制作与原理
linux
444A4E34 分钟前
深入Linux进程优先级:Nice值与O(1)调度器原理
linux·操作系统
Jooolin35 分钟前
【编程史】Git是如何诞生的?这可并非计划之中...
linux·git·ai编程
云边有个稻草人38 分钟前
【Linux系统】第八节—进程概念(上)—冯诺依曼体系结构+操作系统+进程及进程状态+僵尸进程—详解!
linux·进程·冯诺依曼体系结构·pcb·僵尸进程·进程的状态·task_ struct
xian0gang1 小时前
rk3588 区分两个相同的usb相机
linux
这儿有一堆花1 小时前
安全访问家中 Linux 服务器的远程方案 —— 专为单用户场景设计
linux·服务器·安全
RussellFans1 小时前
Linux 文本三剑客(grep, awk, sed)
linux·运维·服务器
Chuncheng's blog2 小时前
CentOS 7如何编译安装升级gcc至7.5版本?
linux·运维·c++·centos
听风吹等浪起2 小时前
CentOS在vmware局域网内搭建DHCP服务器【踩坑记录】
linux·服务器·centos
明月看潮生2 小时前
青少年编程与数学 01-011 系统软件简介 04 Linux操作系统
linux·青少年编程·操作系统·系统软件·编程与数学