struct bus_type中為devices和drivers準備了兩個鏈表,而代表device的結構體struct device中又有兩個成員,struct bus_type *bus和struct device_driver *driver,同樣,代表driver的結構體struct device_driver同樣有兩個成員,struct bus_type *bus和struct list_head devices,struct device和struct device_driver的定義和struct bus_type一樣,在include/linux/device.h中.憑一種男人的直覺,可以知曉,struct device中的bus記錄的是這個裝置連在哪條匯流排上,driver記錄的是這個裝置用的是哪個驅動,反過來,struct device_driver中的bus代表的也是這個驅動屬於哪條匯流排,devices記錄的是這個驅動支援的那些裝置,沒錯,是devices(複數),而不是device(單數),因為一個驅動程式可以支援一個或多個裝置,反過來一個裝置則只會綁定給一個驅動程式.
於是我們想知道,關於bus,關於device,關於driver,他們是如何建立聯絡的呢?換言之,這三個資料結構中的指標是如何被賦值的?絕對不可能發生的事情是,一旦為一條匯流排申請了一個struct bus_type的資料結構之後,它就知道它的devices鏈表和drivers鏈表會包含哪些東西,這些咚咚一定不會是先天就有的,只能是後天填進來的.而具體到usb系統,完成這個工作的就是usb core.usb core的代碼會進行整個usb系統的初始化,比如申請struct bus_type usb_bus_type,然後會掃描usb匯流排,看線上串連了哪些usb裝置,或者說root hub上連了哪些usb裝置,比如說連了一個usb鍵盤,那麼就為它準備一個struct device,根據它的實際情況,為這個struct device賦值,並插入devices鏈表中來.又比如root hub上連了一個普通的hub,那麼除了要為這個hub本身準備一個struct device以外,還得繼續掃描看這個hub上是否又連了別的裝置,有的話繼續重複之前的事情,這樣一直進行下去,直到完成整個掃描,最終就把usb_bus_type中的devices鏈表給建立了起來.
那麼drivers鏈表呢?這個就不用bus方面主動了,而該由每一個driver本身去bus上面登記,或者說掛牌.具體到usb系統,每一個usb裝置的驅動程式都會有一個struct usb_driver結構體,其代碼如下,來自include/linux/usb.h
485 /* -------------------------------------------------------------------------- */
486
487 /**
488 * struct usb_driver - identifies USB driver to usbcore
489 * @owner: Pointer to the module owner of this driver; initialize
490 * it using THIS_MODULE.
491 * @name: The driver name should be unique among USB drivers,
492 * and should normally be the same as the module name.
493 * @probe: Called to see if the driver is willing to manage a particular
494 * interface on a device. If it is, probe returns zero and uses
495 * dev_set_drvdata() to associate driver-specific data with the
496 * interface. It may also use usb_set_interface() to specify the
497 * appropriate altsetting. If unwilling to manage the interface,
498 * return a negative errno value.
499 * @disconnect: Called when the interface is no longer accessible, usually
500 * because its device has been (or is being) disconnected or the
501 * driver module is being unloaded.
502 * @ioctl: Used for drivers that want to talk to userspace through
503 * the "usbfs" filesystem. This lets devices provide ways to
504 * expose information to user space regardless of where they
505 * do (or don't) show up otherwise in the filesystem.
506 * @suspend: Called when the device is going to be suspended by the system.
507 * @resume: Called when the device is being resumed by the system.
508 * @id_table: USB drivers use ID table to support hotplugging.
509 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set
510 * or your driver's probe function will never get called.
511 * @driver: the driver model core driver structure.
512 *
513 * USB drivers must provide a name, probe() and disconnect() methods,
514 * and an id_table. Other driver fields are optional.
515 *
516 * The id_table is used in hotplugging. It holds a set of descriptors,
517 * and specialized data may be associated with each entry. That table
518 * is used by both user and kernel mode hotplugging support.
519 *
520 * The probe() and disconnect() methods are called in a context where
521 * they can sleep, but they should avoid abusing the privilege. Most
522 * work to connect to a device should be done when the device is opened,
523 * and undone at the last close. The disconnect code needs to address
524 * concurrency issues with respect to open() and close() methods, as
525 * well as forcing all pending I/O requests to complete (by unlinking
526 * them as necessary, and blocking until the unlinks complete).
527 */
528 struct usb_driver {
529 struct module *owner;
530
531 const char *name;
532
533 int (*probe) (struct usb_interface *intf,
534 const struct usb_device_id *id);
535
536 void (*disconnect) (struct usb_interface *intf);
537
538 int (*ioctl) (struct usb_interface *intf, unsigned int code, void *buf);
539
540 int (*suspend) (struct usb_interface *intf, u32 state);
541 int (*resume) (struct usb_interface *intf);
542
543 const struct usb_device_id *id_table;
544
545 struct device_driver driver;
546 };
547 #define to_usb_driver(d) container_of(d, struct usb_driver, driver)
看似很長一段,實際上也就是注釋為主.而此刻我們只需注意到其中的struct device_driver driver這個成員,usb core為每一個裝置驅動準備了一個函數,讓它把自己的這個struct device_driver driver插入到usb_bus_type中的drivers鏈表中去.而這個函數正是我們此前看到的usb_register.而與之對應的usb_deregister所從事的正是與之相反的工作,把這個結構體從drivers鏈表中刪除.可以說,usb core的確是用心良苦,為每一個usb裝置驅動做足了功課,正因為如此,作為一個實際的usb裝置驅動,它在初始化階段所要做的事情就很少,很簡單了,直接調用usb_register即可.事實上,沒有人是理所當然應該為你做什麼的,但usb core這麼做了.所以每一個寫usb裝置驅動的人應該銘記,usb device driver絕不是一個人在工作,在他身後,是usb core所提供的默默無聞又不可或缺的支援.