这里是usb初始化时,将usb设备驱动注册到总线上,从usb设备的层次结构(usb device --- configuration --- interface --- point)上来说,这里的driver所对应的为设备描述符(usb device)的驱动. 当然这里的注册过程和usb interface的注册过程类似,只是用的注册函数是 usb_register_driver 而不是usb_register_device_driver。
/* drivers/usb/core/usb.c */ static int __init usb_init(void) { retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE); } /* drivers/usb/core/driver.c */ int usb_register_device_driver(struct usb_device_driver *new_udriver, struct module *owner) { new_udriver->drvwrap.driver.bus = &usb_bus_type; retval = driver_register(&new_udriver->drvwrap.driver); } /* drivers/base/driver.c */ int driver_register(struct device_driver *drv) { ret = bus_add_driver(drv); } /* drivers/base/bus.c */ int bus_add_driver(struct device_driver *drv) { bus = bus_get(drv->bus); priv = kzalloc(sizeof(*priv), GFP_KERNEL); priv->driver = drv; drv->p = priv; klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); }
下面我们看一看usb_generic_driver里的函数,我们仅看下probe的这条线
/* drivers/usb/core/generic.c */ static int generic_probe(struct usb_device *udev) { c = usb_choose_configuration(udev); if (c >= 0) { err = usb_set_configuration(udev, c); } } /* drivers/usb/core/message.c */ int usb_set_configuration(struct usb_device *dev, int configuration) { for (i = 0; i < nintf; ++i) { ret = device_add(&intf->dev); create_intf_ep_devs(intf); } } /* drivers/base/core.c */ int device_add(struct device *dev) { bus_probe_device(dev); } /* drivers/base/bus.c */ void bus_probe_device(struct device *dev) { if (bus && bus->p->drivers_autoprobe) { ret = device_attach(dev); } } /* drivers/base/dd.c */ int device_attach(struct device *dev) { if (dev->driver) { ret = device_bind_driver(dev); } else { ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach); } } int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, void *data, int (*fn)(struct device_driver *, void *)) { /* 遍历所有驱动 */ klist_iter_init_node(&bus->p->klist_drivers, &i, start ? &start->p->knode_bus : NULL); while ((drv = next_driver(&i)) && !error) error = fn(drv, data); } static int __device_attach(struct device_driver *drv, void *data) { /* 这里进行匹配ID,如果有支持的驱动,执行probe函数 */ if (!driver_match_device(drv, dev)) retrun 0; return driver_probe_device(drv, dev); } int driver_probe_device(struct device_driver *drv, struct device *dev) { ret = really_probe(dev, drv); } static int really_probe(struct device *dev, struct device_driver *drv) { if (dev->bus->probe) { ret = dev->bus->probe(dev); } else if (drv->probe) { ret = drv->probe(dev); } }