USB 驱动之 usb_register 函数解析

  • 前段时间在kernel 添加了 USB to LAN 模块 AX88772B 的驱动。

  • 根据相关添加解析一下 usb_register_driver 函数

  • drivers/net/usb/asix.c

    1677 static int __init asix_init(void)
    1678 {
    679     return usb_register(&asix_driver);    //---> usb注册
    1680 }
    1681 module_init(asix_init);
  • include/linux/usb.h

     // 今天就是要解析这个函数
     947 extern int usb_register_driver(struct usb_driver *, struct module *,
     948                    const char *);
     949 
     950 /* use a define to avoid include chaining to get THIS_MODULE & friends */
     951 #define usb_register(driver) \
     952     usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
  • include/linux/usb.h

     861 struct usb_driver {
     862     const char *name;
     863 
     864     int (*probe) (struct usb_interface *intf,
     865               const struct usb_device_id *id);
     866 
     867     void (*disconnect) (struct usb_interface *intf);
     868 
     869     int (*unlocked_ioctl) (struct usb_interface *intf, unsigned int code,
     870             void *buf);
     871 
     872     int (*suspend) (struct usb_interface *intf, pm_message_t message);
     873     int (*resume) (struct usb_interface *intf);
     874     int (*reset_resume)(struct usb_interface *intf);
     875 
     876     int (*pre_reset)(struct usb_interface *intf);
     877     int (*post_reset)(struct usb_interface *intf);
     878 
     879     const struct usb_device_id *id_table;
     880 
     881     struct usb_dynids dynids;
     882     struct usbdrv_wrap drvwrap;
     883     unsigned int no_dynamic_id:1;
     884     unsigned int supports_autosuspend:1;
     885     unsigned int soft_unbind:1;
     886 };
     887 #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver)
  • drivers/usb/core/driver.c usb_register_driver

     848 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
     849             const char *mod_name)
     850 {
     851     int retval = 0;
     852 
     853     if (usb_disabled())
     854         return -ENODEV;
     855 
     856     new_driver->drvwrap.for_devices = 0;
     857     new_driver->drvwrap.driver.name = (char *) new_driver->name;
     858     new_driver->drvwrap.driver.bus = &usb_bus_type;
     859     new_driver->drvwrap.driver.probe = usb_probe_interface;
     860     new_driver->drvwrap.driver.remove = usb_unbind_interface;
     861     new_driver->drvwrap.driver.owner = owner;
     862     new_driver->drvwrap.driver.mod_name = mod_name;
     863     spin_lock_init(&new_driver->dynids.lock);
     864     INIT_LIST_HEAD(&new_driver->dynids.list);
     865 
     866     retval = driver_register(&new_driver->drvwrap.driver);
     867     if (retval)
     868         goto out;
     869 
     870     usbfs_update_special();
     871 
     872     retval = usb_create_newid_file(new_driver);
     873     if (retval)
     874         goto out_newid;
     875 
     876     retval = usb_create_removeid_file(new_driver);
     877     if (retval)
     878         goto out_removeid;
     879 
     880     pr_info("%s: registered new interface driver %s\n",
     881             usbcore_name, new_driver->name);
     882 
     883 out:
     884     return retval;
     885 
     886 out_removeid:
     887     usb_remove_newid_file(new_driver);
     888 out_newid:
     889     driver_unregister(&new_driver->drvwrap.driver);
     890 
     891     printk(KERN_ERR "%s: error %d registering interface "
     892             "   driver %s\n",
     893             usbcore_name, retval, new_driver->name);
     894     goto out;
     895 }
     896 EXPORT_SYMBOL_GPL(usb_register_driver);
posted @ 2017-09-12 11:10  陈富林  阅读(3919)  评论(0编辑  收藏  举报