linux内核部件分析(九)——设备驱动模型之device_第1页
linux内核部件分析(九)——设备驱动模型之device_第2页
linux内核部件分析(九)——设备驱动模型之device_第3页
linux内核部件分析(九)——设备驱动模型之device_第4页
linux内核部件分析(九)——设备驱动模型之device_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、e 与 bus 间的联系,并添加 bus 为 device 定义的属性,然后会调用bus_probe_device() 。bus_probe_device()会试图为已挂在总线上的该设备寻找对应 的驱动。我们的故事就从这里开始。cpp view plaincopyprint?/* * bus_probe_device - probe drivers for a new device * dev: device to probe * -Automatically probe for a driver if the bus allows it. */ void bus_probe_device(s

3、truct device *dev) struct bus_type*bus = dev->bus;int ret;if (bus&& bus->p->drivers_autoprobe)ret = device_attach(dev);WARN_ON(ret < 0); bus_probe_device()为总线上的设备寻找驱动。它先是检查 bus->p->drivers_autoprobe ,看是否允许自动探测。允 许了才会调用 device_attach() 进行实际的寻找

4、工作。 说到 bus->p->drivers_autoprobe 这个变量,它是在 bus_type_private 中的,在调用 bus_register() 前都初始化不了, 在 bus_register() 中自动定为 1。所以,除非是用户空间通过 drivers_autoprobe 属性文件主动禁止, bus 总是允许自动探测 的,所有的 bus 都是如此。cpp view plaincopyprint?/* device_attach - try to attachdevice to a driver. * dev: device. * * Walk th

5、e list of drivers that the bus has and call * driver_probe_device() for each pair. If a compatible * pair is found, break out and return. * * Returns 1 if the device was bound to a driver; * 0 if no matching driver was found; * -ENODEV if the device is not registered. * * When called for a USB inter

6、face, dev->parent->sem must be held. */ int device_attach(struct device *dev) int ret = 0;down(&dev->sem);if (dev->driver) ret = device_bind_driver(dev); if (ret = 0)ret = 1;else dev->driver = NULL; ret = 0; else pm_runtime_get_noresume(dev); ret = bus_for_

7、each_drv(dev->bus, NULL, dev, _device_attach);pm_runtime_put_sync(dev); up(&dev->sem); return ret; device_attach()在实际绑定之前,会用dev->sem进行加锁。 不错, dev->sem 几乎就是为了在设备与驱动绑定或者解 除绑定时加锁用的。还没有看到它在其它地方被调用。如果在调用 device_attach()前就已经有了 dev->driver(),就 调用 device_bind_drive

8、r() 进行绑定,不然还要调用 bus_for_each_drv()进行依次匹配。至于 pm_runtime_get_noresume 之类的函数,属于电源管理部分, 我们现在先忽略。cpp view plaincopyprint?static void driver_bound(struct device *dev) if(klist_node_attached(&dev->p->knode_driver)printk(KERN_WARNING "%s: device %s alreadyboundn",_func_,kobje

9、ct_name(&dev->kobj);return;pr_debug("driver: '%s': %s: bound to device '%s'n", dev_name(dev),_func_,dev->driver->name);if (dev->bus)blocking_notifier_call_chain(&dev->bus->p->bus_notifier,BUS_NOTIFY_BOUND_DRIV

10、ER, dev); klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices); staticint driver_sysfs_add(struct device *dev) int ret;ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,kobject_nam

11、e(&dev->kobj);if (ret = 0) ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,"driver"); if (ret) sysfs_remove_link(&dev->driver->p->kobj, kobject_name(&dev->kobj); return ret; sta

12、tic void driver_sysfs_remove(struct device *dev)struct device_driver *drv = dev->driver;if(drv) sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj);sysfs_remove_link(&dev->kobj, "driver"); /* * device_bind_driver - bind a dri

13、ver to one device. * dev: device. * * Allow manual attachment of a driver to a device. * Caller must have already set dev->driver. * Note that this does not modify the bus reference count * nor take the bus's rwsem. Please verify those are accounted * for before calling this. (It is ok to

14、 call with no other effort * from a driver's probe() method.) * * This function must be called with dev->sem held. */ int device_bind_driver(struct device *dev) int ret; ret = driver_sysfs_add(dev); if (!ret) driver_bound(dev); return ret; device_bind_driver() 将 device 与 driver 绑定。它调用了两个内

15、 部函数。其中 drivers_sysfs_add()负责创建 sysfs 中 driver 和 device 指 向对方的软链接。还有一个与它相对的函数 drivers_sysfs_remove() 。driver_bound() 则实际将 device 加入驱动的设备链表。 因为在调用 device_bind_driver() 之前就已经设置过 dev->driver 了,所以这样就将 device 和 driver 绑定了。 只是这样好像还缺少了什么,不错,之前看到 driver 时曾定 义了 drv->probe 函数, bus->probe

16、也有类似的功能, 这 里只是绑定,却没有调用 probe 函数。让我们回过头来,继续看如果device_attach()中没有定义dev->driver 会怎么样,是用 bus_for_each_drv()对 bus 的驱动链表进行遍历,遍历函数使用_device_attach。cpp view plaincopyprint?static int _device_attach(struct device_driver *drv, void *data) struct device *dev = data; if (!driver_match_device(drv, dev) re

17、turn 0; return driver_probe_device(drv, dev); 不要小看了 _device_attach(),就是在 _device_attach()中既完 成了匹配工作,又完成了绑定工作。 bus_for_each_drv() 在遍 历中,如果遍历函数返回值不为 0,则遍历结束。所以在_device_attach()找到并绑定了适合的驱动,就会返回1停止 遍历,否则继续遍历剩余的驱动。先来看匹配工作,这是在 driver_match_device() 中完成的。 cpp view plaincopyprint?static inline int driver_ma

18、tch_device(struct device_driver *drv, struct device *dev) return drv->bus->match ?drv->bus->match(dev, drv) : 1; 原来driver_match_device() 实际是调用 drv->bus->match() 来 完成设备和驱动的匹配的。其实这也是理所当然。因为总线 不同,总线规范设备、厂商、类设备等定义的规格都不同, 也只有 bus 亲自主持匹配工作。再具体的就只能等分析具体 总线的时候了。cpp

19、view plaincopyprint?int driver_probe_device(struct device_driver *drv, struct device *dev) int ret = 0;drv->name);if (!device_is_registered(dev) return -ENODEV; pr_debug("bus: '%s': %s: matched device %s with driver %sn", drv->bus->name, _func_, dev_name(dev),p

20、m_runtime_get_noresume(dev);pm_runtime_barrier(dev);ret = really_probe(dev, drv);return ret; 如果pm_runtime_put_sync(dev);driver_match_device() 匹配成功了, _device_attach() 就会继续 调用 driver_probe_devices() 完成绑定。但 driver_probe_devices() 又是调用 really_probe() 完成的。cpp view plaincopyprint?static atomic_t probe_cou

21、nt = ATOMIC_INIT(0); staticDECLARE_WAIT_QUEUE_HEAD(probe_waitqueue); static int really_probe(struct device *dev, struct device_driver *drv) int ret = 0;atomic_inc(&probe_count); pr_debug("bus: '%s': %s: probing driver %s with device %sn",drv->bus->name, _func_

22、, drv->name, dev_name(dev);WARN_ON(!list_empty(&dev->devres_head); dev->driver = drv;if (driver_sysfs_add(dev)printk(KERN_ERR "%s: driver_sysfs_add(%s)failedn",_func_, dev_name(dev);goto probe_failed;if (dev->bus->probe)ifret = dev->bus-&am

23、p;gt;probe(dev);(ret)goto probe_failed; else if(drv->probe) ret = drv->probe(dev);if (ret)goto probe_failed;driver_bound(dev);ret = 1;pr_debug("bus:goto probe_failed;if (dev->bus->probe)goto probe_failed;if (dev->bus->probe)'%s': %s: bound devic

24、e %s to driver %sn",drv->bus->name, _func_, dev_name(dev),drv->name);goto done; probe_failed:devres_release_all(dev);driver_sysfs_remove(dev);dev->driver = NULL;if (ret != -ENODEV&& ret != -ENXIO) /* driver matchedbut the probe failed */printk(KERN_WA

25、RNINGgoto probe_failed;if (dev->bus->probe)goto probe_failed;if (dev->bus->probe)/*"%s: probe of %s failed with error %dn", drv->name, dev_name(dev), ret);Ignore errors returned by ->probe so that the next driver cantry* its luck.*/ret = 0; done:at

26、omic_dec(&probe_count); wake_up(&probe_waitqueue); return ret; really_probe() 完成的绑定工作和 device_bind_driver() 差不多, 只是它还会调用 bus->probe 或者 drv->probe 中定义的probe 函数。至于在 really_probe() 中使用 probe_count 保护,最后调用 wake_up(&probe_waitqueue) ,都是为了进行同步。 cpp view plaincopyprint

27、?/* * driver_probe_done * Determine if the probe sequence is finished or not. * * Should somehow figure out how to use a semaphore, not an atomic variable. */ int driver_probe_done(void) pr_debug("%s: probe_count = %dn", _func_,return -EBUSY;atomic_read(&probe_count); if (atomic_re

28、ad(&probe_count)return 0; /* * wait_for_device_probe * Wait fordevice probing to be completed. */voidwait_for_device_probe(void) /* wait for the knowndevices to complete their probing */wait_event(probe_waitqueue, atomic_read(&probe_count) = 0); async_synchronize_full(); driver_probe

29、_done() 检查当前是否有设备正在绑定驱动。 wait_for_device_probe() 会阻塞到所有的设备绑定完驱动。关 于 bus_probe_device() 的过程就分析到这里,下面来看下 bus_add_driver() 又是怎样做的。之前我们已经知道 driver_register() 把绝大部分操作都移到了 bus_add_driver()中来。其中只有一点和设备与驱动的绑定相关,就是对 driver_attach() 的调用。cpp view plaincopyprint?int driver_attach(struct device_driver *drv) retu

30、rn bus_for_each_dev(drv->bus, NULL,drv, _driver_attach); driver_attach() 一如 device_attach, 只是这里是对总线的设备链表进行遍历,使用的遍历函数是_driver_attach() 。cpp view plaincopyprint?static int _driver_attach(struct device *dev, void *data) struct device_driver *drv = data; /* Lock device and try to bind to it. Wedr

31、op the error* here and always return 0, because weneed to keep trying* to bind to devices and some driverswill return an error * simply if it didn't support the device. * driver_probe_device() will spit a warning if thereif (dev->parent) /* is an error. */ if (!driver_match_device(drv, de

32、v) return 0;Needed for USB */ down(&dev->parent->sem); down(&dev->sem); if (!dev->driver) driver_probe_device(drv, dev); up(&dev->sem); if (dev->parent)up(&dev->parent->sem); return 0; 在_driver_attach()中,driver_m

33、atch_device()就不说了,它是调 到 bus->match 去的。 然后依然是加锁,调用 driver_probe_device() 函数。这就与 _device_attach()的路径一致了。不要以为就这样结束了,现 在我们只是看到了把 device 和 driver 绑定到一起的方法,却 没有看到解除绑定的方法。 既然绑定的方法是在设备和驱动注册的时候调用的,那解除 绑定自然是在设备或驱动注销的时候。还是先来看设备的, device_unregister()->device_del() 会调 用bus_remove_device()将设备从总线上删除。

34、bus_remove_device()是与 bus_add_device()相对的,但也不仅 如此,它还调用了 device_release_driver()来解除与 driver的 绑定。cpp view plaincopyprint?/* * device_release_driver - manually detach device from driver.* dev: device. * *Manually detach device from driver.* When called for a USBinterface, dev->parent->sem

35、 must be held. */ void device_release_driver(struct device *dev) /*If anyone calls device_release_driver() recursively from* within their ->remove callback for the same device, they* will deadlock right here.*/down(&dev->sem);_device_release_driver(dev);up(&dev->

36、sem); /* *_device_release_driver() must be called with dev->sem held. * When called for a USB interface, dev->parent->sem must be held as well. */ static void _device_release_driver(struct device *dev) struct device_driver *drv; drv = dev->driver; if (drv) pm_runtime_get_

37、noresume(dev); pm_runtime_barrier(dev);driver_sysfs_remove(dev); if (dev->bus) blocking_notifier_call_chain(&dev->bus->p->bus _notifier,BUS_NOTIFY_UNBIND_DRIVER,dev);if (dev->bus &&else ifdrv->remove(dev); dev->driver = NULL;de

38、v->bus->remove) dev->bus->remove(dev); (drv->remove) devres_release_all(dev);klist_remove(&dev->p->knode_driver);if (dev->bus) blocking_notifier_call_chain(&dev->bus->p->bus _notifier,BUS_NOTIFY_UNBOUND_DRIV

39、ER,dev);pm_runtime_put_sync(dev);device_release_driver()还是负责加加锁,实际的工作由device_release_driver()来完成。除了 sysfs 和结构中解除绑定的操作,还调用了bus->remove 或者 driver->remove 。虽然 device 注销时 与 driver 解除绑定很简单,但 driver 注销要与 device 解除绑 定就要复杂一些,因为它要与设备链表上所有的设备解除绑 定。在 driver_unregister()->bus_remove_driver(

40、) 中,调用了driver_detach()函数。cpp view plaincopyprint?/* * driver_detach - detach driverstructfrom all devices it controls. * drv: driver. */ voiddriver_detach(struct device_driver *drv) device_private *dev_prv;struct device *dev;for(;)ifspin_lock(&drv->p->klist_devices.k_lock);(lis

41、t_empty(&drv->p->klist_devices.k_list)break;spin_unlock(&drv->p->klist_devices.k_lock);dev_prv = list_entry(drv->p->klist_devices.k_list.prev, struct device_private,knode_driver.n_node);dev = dev_prv->device;get_device(dev);spin_unlock

42、(&drv->p->klist_devices.k_lock);if (dev->parent) /* Needed for USB */ down(&dev->parent->sem); down(&dev->sem); if (dev->driver = drv)_device_release_driver(dev);up(&dev->sem); if (dev->parent) up(&amp

43、;dev->parent->sem);put_device(dev); 可以看到, driver_detach() 基本 操作就是与设备链表上的设备解除绑定。等了这么久,终于 有个有点意思的地方。一看这个 drv 的设备链表遍历,首先 明明是 klist ,却没使用标准的循环函数,奇怪,然后发现竟 然没有将设备卸下链表的地方,更奇怪。其实再一想就明白 了。你看到 list_entry() 中,是从设备链表末尾取设备解除绑 定的,这是驱动生怕前面的设备解除绑定了,后面的就不工 作了。 也正是因为 klist 遍历是逆向的, 所以无法使用标准函 数。至于将设备卸下链表的

44、地方,是在_device_release_driver() 中。或许会奇怪这里为什么会有get_device()和put_device()的操作。这是为了防止设备一取下链表,就会释放最后一个引用 计数,导致直接注销。那时候的情况,一定是在占用了 dev->sem 的同时去等待 dev->sem ,通俗来说就是死锁。 通过 driver_attach() 和 driver_detach() 的训练,我们已经习惯 在为设备加锁时, 顺便为其父设备加锁。 虽然在 device_attach() 和 device_release_driver() 中只是对设备本身加锁。或许

45、是害 怕在驱动与设备解除绑定的过程中,父设备突然也要解除绑 定,导致不一致状态。为至于为什么设备方主动要求时不需 要对父设备加锁,或许是设备的主动申请更靠谱,不会在子 设备绑定或释放的同时, 父设备也申请释放。 总之,在 linux 看来,设备恐怕比驱动还要靠谱一些,从 driver 和 bus 的引 用计数,从这里的加锁情况,都可以看出一二。 cpp view plaincopyprint?void *dev_get_drvdata(const struct device *dev) if (dev && dev->p) return dev->p->driver_data; return NULL; void dev_set_drvdata(struct device *dev, void *data) int error; if (!dev) return; if (!dev->p) error = device_private_init(dev); if (error) return; dev->p->driver_data = data; 最后的

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论