韦东山嵌入式linux系列-驱动进化之路:总线设备驱动模型

1 驱动编写的 3 种方法

以 LED 驱动为例

1.1 传统写法

使用哪个引脚,怎么操作引脚,都写死在代码中。
最简单,不考虑扩展性,可以快速实现功能。
修改引脚时,需要重新编译。

应用程序调用open等函数最简单的方法是驱动层也提供对应的drv_open,应用程序调用read,驱动层也提供对应的drv_read等等。需要写出驱动层的函数,为了便于管理,将这些函数放到file_operations结构体中,即第一步定义对应的file_operations结构体,并实现对应的open等程序(第一步);实现完成之后,将file_operations结构体通过register_chrdev注册到内核(第二步);然后通过入口函数调用注册函数(chrdev),安装驱动程序的时候,内核会调用入口函数,完成file_operations结构体的注册(第三步);有入口函数就有出口函数(第四步)。对于字符设备驱动程序而言,file_operations结构体是核心,每一个驱动程序都对应file_operations结构体,内核中有众多file_operations结构体,怎么才能找到对应的结构体呢?

应用程序要访问驱动程序,需要打开一个设备结点,里面有主设备号,根据设备结点的主设备号在内核中找到对应的file_operations结构体。注册结构体时足以提供主设备号,可以让内核分配;最后就是完善信息,创建类,创建设备。

之后有了面向对象/分层/分离

1.2 总线设备驱动模型

引入 platform_device/platform_driver,将“资源”与“驱动”分离开来。
代码稍微复杂,但是易于扩展。
冗余代码太多,修改引脚时设备端的代码需要重新编译。
更换引脚时 , 图中的led_drv.c基本不用改,但是需要修改led_dev.c。

谁来管理device?谁来管理driver?,引入总线Bus的概念

左边是和具体硬件打交道,右边对应是抽象。通过总线bus管理,两两匹配,右边的代码基本不用修改,左边的代码针对不同的硬件提供不同的device。会有大量的,导致内核庞大臃肿。

1.3 设备树

通过配置文件dts──设备树来定义“资源”(内核之外),会被编译成dtb文件(内核之外),再传给内核,内核解析dtb文件,构造出一系列的platform device。
代码稍微复杂,但是易于扩展。
无冗余代码,修改引脚时只需要修改 dts 文件并编译得到 dtb 文件,把它传给内核。
无需重新编译内核/驱动。

2 在 Linux 中实现“分离”: Bus/Dev/Drv 模型

实例

有一个platform device,里面有resource,resource指向一个数组,里面定义了设备的资源(中断),flag指示了是一个中断,由start决定是哪个中断,平台设备指定资源;平台platform driver有probe函数,这个函数用来做驱动相关的事情(做任何事情);platform device和platform driver通过name建立联系。

找到platform_device.h(/include/linux)

有注册和注销函数

搜索,随便找一个是全局变量的

有一个platform device,里面有resource,resource指向一个数组,里面定义了设备的资源(中断),平台设备指定资源;平台platform driver有probe函数,这个函数用来做驱动相关的事情;platform device和platform driver通过name建立联系。这里的resource需要遵守一定的规则

搜索platform driver-serial8250

platform device和platform driver是怎么挂钩的

搜索-serial8250_isa_driver

也是注册进内核

内核中有虚拟的总线platform_bus_type,有两个链表,左边是设备链表,右边是driver链表。当注册平台设备platform device时,平台设备就会放到左边的链表;当注册平台驱动platform driver时,平台驱动就会放入到右边的链表。放入之后就会和对方的成员比较,匹配成功就调用对应driver中的probe函数。

是否匹配成功

怎么匹配呢

(1)如果platform_device中定义了char* driver_override这一项,根据这个名字去platform_driver中查找struct device_driver driver中的name属性 和driver_override相等时,表示找到了,如果platform_device中没有定义了char* driver_override这一项,就开始第二项比较;

(2)如果platform_driver中的id_table不为空,比较两者name(较多使用);如果还不行,就开始比较第三项

(3)比较platform_device的name 和 platform_driver中查找struct device_driver driver中的name属性。(较多使用)

在source insight中随便找个地方输入platform_match,ctrl

参看源码

 * Platform device IDs are assumed to be encoded like this:
 * "<name><instance>", where <name> is a short description of the type of
 * device, like "pci" or "floppy", and <instance> is the enumerated
 * instance of the device, like '0' or '42'.  Driver IDs are simply
 * "<name>".  So, extract the <name> from the platform_device structure,
 * and compare it against the name of the driver. Return whether they match
 * or not.
 */
static int platform_match(struct device *dev, struct device_driver *drv)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct platform_driver *pdrv = to_platform_driver(drv);

	/* When driver_override is set, only bind to the matching driver */
    // 1先用它
	if (pdev->driver_override)
		return !strcmp(pdev->driver_override, drv->name);

	/* Attempt an OF style match first */
	if (of_driver_match_device(dev, drv))
		return 1;

	/* Then try ACPI style match */
	if (acpi_driver_match_device(dev, drv))
		return 1;

	/* Then try to match against the id table */
    // 2再用它
	if (pdrv->id_table)
		return platform_match_id(pdrv->id_table, pdev) != NULL;

	/* fall-back to driver name match */
    // 3最后用它
	return (strcmp(pdev->name, drv->name) == 0);
}


// 第2步
static const struct platform_device_id *platform_match_id(
			const struct platform_device_id *id,
			struct platform_device *pdev)
{
	while (id->name[0]) {
		if (strcmp(pdev->name, id->name) == 0) {
			pdev->id_entry = id;
			return id;
		}
		id++;
	}
	return NULL;
}

匹配过程是怎么启动的呢

3 匹配规则

3.1 最先比较

platform_device.driver_override 和 platform_driver.driver.name,可以设置 platform_device 的 driver_override,强制选择某个 platform_driver

3.2 然后比较

platform_device. name 和 platform_driver.id_table[i].name

Platform_driver.id_table 是“platform_device_id”指针,表示该 drv 支持若干个 device,它里面列出了各个 device 的{.name, .driver_data},其中的“ name”表示该 drv 支持的设备的名字, driver_data 是些提供给该 device 的私有数据。

3.3 最后比较

platform_device.name 和 platform_driver.driver.name
platform_driver.id_table 可能为空,
这时可以根据 platform_driver.driver.name 来寻找同名的 platform_device。

3.4 函数调用关系
platform_device_register
platform_device_add
    device_add
        bus_add_device // 放入链表
        bus_probe_device // probe 枚举设备,即找到匹配的(dev, drv)
            device_initial_probe
                __device_attach
                    bus_for_each_drv(...,__device_attach_driver,...)
                        __device_attach_driver
                            driver_match_device(drv, dev) // 是否匹配
                            driver_probe_device // 调用 drv 的 probe


platform_driver_register
    __platform_driver_register
        driver_register
            bus_add_driver // 放入链表
                driver_attach(drv)
                    bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
                        __driver_attach
                            driver_match_device(drv, dev) // 是否匹配
                            driver_probe_device // 调用 drv 的 probe

4 常用函数

这些函数可查看内核源码: drivers/base/platform.c,根据函数名即可知道其含义。下面摘取常用的几个函数

4.1 注册/反注册
platform_device_register/ platform_device_unregister
platform_driver_register/ platform_driver_unregister
platform_add_devices // 注册多个 device
4.2 获得资源

返回该 dev 中某类型(type)资源中的第几个(num)

struct resource *platform_get_resource(struct platform_device *dev,
                                       unsigned int type,
                                       unsigned int num)

返回该 dev 所用的第几个(num)中断:

int platform_get_irq(struct platform_device *dev, unsigned int num)

通过名字(name)返回该 dev 的某类型(type)资源:

struct resource *platform_get_resource_byname(struct platform_device *dev,
                                              unsigned int type,
                                              const char *name)

通过名字(name)返回该 dev 的中断号

int platform_get_irq_byname(struct platform_device *dev, const char *name)

5 怎么写程序

5.1 分配/设置/注册 platform_device 结构体

硬件抽象出platform_device,在里面定义所用资源,指定设备名字。驱动层构造platform_driver结构体,注册这个结构体。两个名字相同

5.2 分配/设置/注册 platform_driver 结构体

在其中的probe 函数里,分配/设置/注册 file_operations 结构体 ,并从platform_device 中确实所用硬件资源, 指定platform_driver的名字。

  • 28
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值