一:前言
繼UHCI的驅動之後,我們對USB Control的運作有了一定的了解.在接下來的分析中,我們對USB設備的驅動做一個全面的分析,我們先從HUB的驅動說起.關於HUB,usb2.0 spec上有詳細的定義,基於這部份的代碼位於linux-2.6.25/drivers/usb/core下,也就是說,這部份代碼是位於core下,和具體設備是無關的,因為各廠商的hub都是按照spec的要求來設計的.
二:UHCI驅動中的root hub
記得在分析UHCI驅動的時候,曾詳細分析過root hub的初始化操作.為了分析方便,將代碼片段列出如下:
usb_add_hcd() à usb_alloc_dev():
struct usb_device *usb_alloc_dev(struct usb_device *parent,
struct usb_bus *bus, unsigned port1)
{
……
……
//usb_device,內嵌有struct device結構,對這個結構進行初始化
device_initialize(&dev->dev);
dev->dev.bus = &usb_bus_type;
dev->dev.type = &usb_device_type;
……
……
}
一看到前面對dev的賦值,根據我們對設備模型的理解,一旦這個device進行注冊,就會發生driver和device的匹配過程了.
不過,現在還不是分析這個過程的時候,我們先來看一下,USB子系統中的兩種驅動.
三:USB子系統中的兩種驅動
linux-2.6.25/drivers/usb/core/driver.c中,我們可以找到兩種register driver的方式,分別為usb_register_driver()和usb_register_device_driver().分別來分析一下這兩個接口.
usb_register_device_driver()接口的代碼如下:
int usb_register_device_driver(struct usb_device_driver *new_udriver,
struct module *owner)
{
int retval = 0;
if (usb_disabled())
return -ENODEV;
new_udriver->drvwrap.for_devices = 1;
new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
new_udriver->drvwrap.driver.bus = &usb_bus_type;
new_udriver->drvwrap.driver.probe = usb_probe_device;
new_udriver->drvwrap.driver.remove = usb_unbind_device;
new_udriver->drvwrap.driver.owner = owner;
retval = driver_register(&new_udriver->drvwrap.driver);
if (!retval) {
pr_info("%s: registered new device driver %s\n",
usbcore_name, new_udriver->name);
usbfs_update_special();
} else {
printk(KERN_ERR "%s: error %d registering device "
" driver %s\n",
usbcore_name, retval, new_udriver->name);
}
return retval;
}
首先,通過usb_disabled()來判斷一下usb是否被禁用,如果被禁用,當然就不必執行下面的流程了,直接退出即可.
從上面的代碼,很明顯可以看到, struct usb_device_driver 對struct device_driver進行了一次封裝,我們注意一下這裡的賦值操作:new_udriver->drvwrap.for_devices = 1.等等.這些在後面都是用派上用場的.
usb_register_driver()的代碼如下:
int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
const char *mod_name)
{
int retval = 0;
if (usb_disabled())
return -ENODEV;
new_driver->drvwrap.for_devices = 0;
new_driver->drvwrap.driver.name = (char *) new_driver->name;
new_driver->drvwrap.driver.bus = &usb_bus_type;
new_driver->drvwrap.driver.probe = usb_probe_interface;
new_driver->drvwrap.driver.remove = usb_unbind_interface;
new_driver->drvwrap.driver.owner = owner;
new_driver->drvwrap.driver.mod_name = mod_name;
spin_lock_init(&new_driver->dynids.lock);
INIT_LIST_HEAD(&new_driver->dynids.list);
retval = driver_register(&new_driver->drvwrap.driver);
if (!retval) {
pr_info("%s: registered new interface driver %s\n",
usbcore_name, new_driver->name);
usbfs_update_special();
usb_create_newid_file(new_driver);
} else {
printk(KERN_ERR "%s: error %d registering interface "
" driver %s\n",
usbcore_name, retval, new_driver->name);
}
return retval;
}
很明顯,在這裡接口裡,將new_driver->drvwrap.for_devices設為了0.而且兩個接口的porbe()函數也不一樣.
其實,對於usb_register_driver()可以看作是usb設備中的接口驅動,而usb_register_device_driver()是一個單純的USB設備驅動.