linux内核部件分析(九)——设备驱动模型之device-driver

linux内核部件分析(九)——设备驱动模型之device-driver

ID:42604127

大小:33.31 KB

页数:15页

时间:2019-09-18

linux内核部件分析(九)——设备驱动模型之device-driver_第1页
linux内核部件分析(九)——设备驱动模型之device-driver_第2页
linux内核部件分析(九)——设备驱动模型之device-driver_第3页
linux内核部件分析(九)——设备驱动模型之device-driver_第4页
linux内核部件分析(九)——设备驱动模型之device-driver_第5页
资源描述:

《linux内核部件分析(九)——设备驱动模型之device-driver》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、前面我们分析了device、driver、bus三种类型,主要是三者的注册与注销,在sysfs中的目录与属性文件创建等内容。本节就来详细分析下,在设备注册到总线上时,总线是如何为其寻找对应的驱动的;在驱动注册到总线上时,总线又是如何为其寻找对应的设备的。   本节的实现代码集中在drivers/base/bus.c和drivers/base/dd.c中。先来回忆下,在device_register()->device_add()中,先是调用bus_add_device()添加device与bus间

2、的联系,并添加bus为device定义的属性,然后会调用bus_probe_device()。bus_probe_device()会试图为已挂在总线上的该设备寻找对应的驱动。我们的故事就从这里开始。[cpp] viewplaincopyprint?1./** 2. * bus_probe_device - probe drivers for a new device 3. * @dev: device to probe 4. * 5. * - Automatically probe for a d

3、river if the bus allows it. 6. */  7.void bus_probe_device(struct device *dev)  8.{  9.    struct bus_type *bus = dev->bus;  10.    int ret;  11.  12.    if (bus && bus->p->drivers_autoprobe) {  13.        ret = device_attach(dev);  14.        WARN_ON

4、(ret < 0);  15.    }  16.}  bus_probe_device()为总线上的设备寻找驱动。它先是检查bus->p->drivers_autoprobe,看是否允许自动探测。允许了才会调用device_attach()进行实际的寻找工作。说到bus->p->drivers_autoprobe这个变量,它是在bus_type_private中的,在调用bus_register()前都初始化不了,在bus_register()中自动定为1。所以,除非是用户空间通过drivers

5、_autoprobe属性文件主动禁止,bus总是允许自动探测的,所有的bus都是如此。[cpp] viewplaincopyprint?1./** 2. * device_attach - try to attach device to a driver. 3. * @dev: device. 1. * 2. * Walk the list of drivers that the bus has and call 3. * driver_probe_device() for each pair. 

6、If a compatible 4. * pair is found, break out and return. 5. * 6. * Returns 1 if the device was bound to a driver; 7. * 0 if no matching driver was found; 8. * -ENODEV if the device is not registered. 9. * 10. * When called for a USB interface, @dev->

7、parent->sem must be held. 11. */  12.int device_attach(struct device *dev)  13.{  14.    int ret = 0;  15.  16.    down(&dev->sem);  17.    if (dev->driver) {  18.        ret = device_bind_driver(dev);  19.        if (ret == 0)  20.            ret = 1

8、;  21.        else {  22.            dev->driver = NULL;  23.            ret = 0;  24.        }  25.    } else {  26.        pm_runtime_get_noresume(dev);  27.        ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);  28.        pm

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。