/** * bus_add_driver - Add a driver to the bus. * @drv: driver. */ int bus_add_driver(struct device_driver *drv) { struct bus_type *bus; struct driver_private *priv; int error = 0; bus = bus_get(drv->bus); if (!bus) return -EINVAL; pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); priv = kzalloc(sizeof(*priv), GFP_KERNEL); if (!priv) { error = -ENOMEM; goto out_put_bus; } klist_init(&priv->klist_devices, NULL, NULL); priv->driver = drv; drv->p = priv; priv->kobj.kset = bus->p->drivers_kset; error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, "%s", drv->name); if (error) goto out_unregister; if (drv->bus->p->drivers_autoprobe) { error = driver_attach(drv); if (error) goto out_unregister; } klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); module_add_driver(drv->owner, drv); error = driver_create_file(drv, &driver_attr_uevent); if (error) { printk(KERN_ERR "%s: uevent attr (%s) failed\n", __func__, drv->name); } error = driver_add_attrs(bus, drv); if (error) { /* How the hell do we get out of this pickle? Give up */ printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n", __func__, drv->name); } if (!drv->suppress_bind_attrs) { error = add_bind_files(drv); if (error) { /* Ditto */ printk(KERN_ERR "%s: add_bind_files(%s) failed\n", __func__, drv->name); } } kobject_uevent(&priv->kobj, KOBJ_ADD); return 0; out_unregister: kobject_put(&priv->kobj); kfree(drv->p); drv->p = NULL; out_put_bus: bus_put(bus); return error; }
其功能是向bus中添加一个driver。
1. bus_get() ==> bus的计数加1;
2. kzalloc,分配driver_private内存空间。
3. 初始化此driver的klist_devices链表。
4. kobject_init_and_add() ==> 在/sys/bus/usb/drivers/下面创建usbfs文件夹。
5. 如果总线支持drivers_autoprobe,调用driver_attach。(USB 总线支持)
6. driver_create_file ==> 在/sys/bus/usb/drivers/usbfs下面创建uevent属性文件。
7. driver_add_attrs() ==> 将总线的属性也加到/sys/bus/usb/drivers/usbfs
8. add_bind_files() ==> 在/sys/bus/usb/drivers/usbfs创建bind和unbind属性文件。
9. kobject_uevent() ==> 发送一个KOBJ_ADD的事件。
在/sys/bus/usb/drivers/usbfs下面的文件: