You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
于是我们想知道,关于bus,关于device,关于driver,他们是如何建立联系的呢?换言之,这三个<br />
数据结构中的指针是如何被赋值的?绝对不可能发生的事情是,一旦为一条总线申请了一个<br />
struct bus_type 的数据结构之后,它就知道它的devices 链表和drivers 链表会包含哪些东<br />
西,<br />
这些咚咚一定不会是先天就有的,只能是后天填进来的.而具体到usb 系统,完成这个工作的就是<br />
usb core.usb core 的代码会进行整个usb 系统的初始化,比如申请struct bus_type<br />
usb_bus_type,然后会扫描usb 总线,看线上连接了哪些usb 设备,或者说root hub 上连了哪<br />
些usb 设备,比如说连了一个usb 键盘,那么就为它准备一个struct device,根据它的实际情况,<br />
为这个struct device 赋值,并插入devices 链表中来.又比如root hub 上连了一个普通的<br />
hub,<br />
那么除了要为这个hub 本身准备一个struct device 以外,还得继续扫描看这个hub 上是否又<br />
连了别的设备,有的话继续重复之前的事情,这样一直进行下去,直到完成整个扫描,最终就把<br />
usb_bus_type 中的devices 链表给建立了起来.<br />
那么drivers 链表呢?这个就不用bus 方面主动了,而该由每一个driver 本身去bus 上面登记,<br />
或者说挂牌.具体到usb 系统,每一个usb 设备的驱动程序都会有一个struct usb_driver 结构<br />
体,其代码如下,来自include/linux/usb.h<br />
485 /* -------------------------------------------------------------------------- */<br />
486<br />
487 /**<br />
488 * struct usb_driver - identifies USB driver to usbcore<br />
489 * @owner: Pointer to the module owner <strong>of</strong> this driver; initialize<br />
490 * it using THIS_MODULE.<br />
491 * @name: The driver name should be unique among USB drivers,<br />
492 * and should normally be the same as the module name.<br />
493 * @probe: Called to see if the driver is willing to manage a particular<br />
494 * interface on a device. If it is, probe returns zero and uses<br />
495 * dev_set_drvdata() to associate driver-specific data with the<br />
496 * interface. It may also use usb_set_interface() to specify the<br />
497 * appropriate altsetting. If unwilling to manage the interface,<br />
498 * return a negative errno value.<br />
499 * @disconnect: Called when the interface is no longer accessible, usually<br />
500 * because its device has been (or is being) disconnected or the<br />
501 * driver module is being unloaded.<br />
14<br />
502 * @ioctl: Used for drivers that want to talk to userspace through<br />
503 * the "usbfs" filesystem. This lets devices provide ways to<br />
504 * expose information to user space regardless <strong>of</strong> where they<br />
505 * do (or don't) show up otherwise in the filesystem.<br />
506 * @suspend: Called when the device is going to be suspended by the<br />
system.<br />
507 * @resume: Called when the device is being resumed by the system.<br />
508 * @id_table: USB drivers use ID table to support hotplugging.<br />
509 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set<br />
510 * or your driver's probe function will never get called.