目的:对USB作深入学习,在此留下笔记。欢迎讨论。
[Linux 3.2] [driver/usb/core/usb.c]
USB作为Linux系统的一个子系统,其初始化函数被subsys_initcall修饰。
其初始化代码如下:
- /*
- * Init
- */
- static int __init usb_init(void)
- {
- int retval;
- if (nousb) {
- pr_info("%s: USB support disabled\n", usbcore_name);
- return 0;
- }
- retval = usb_debugfs_init();
- if (retval)
- goto out;
- retval = bus_register(&usb_bus_type);
- if (retval)
- goto bus_register_failed;
- retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
- if (retval)
- goto bus_notifier_failed;
- retval = usb_major_init();
- if (retval)
- goto major_init_failed;
- retval = usb_register(&usbfs_driver);
- if (retval)
- goto driver_register_failed;
- retval = usb_devio_init();
- if (retval)
- goto usb_devio_init_failed;
- retval = usbfs_init();
- if (retval)
- goto fs_init_failed;
- retval = usb_hub_init();
- if (retval)
- goto hub_init_failed;
- retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
- if (!retval)
- goto out;
- usb_hub_cleanup();
- hub_init_failed:
- usbfs_cleanup();
- fs_init_failed:
- usb_devio_cleanup();
- usb_devio_init_failed:
- usb_deregister(&usbfs_driver);
- driver_register_failed:
- usb_major_cleanup();
- major_init_failed:
- bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
- bus_notifier_failed:
- bus_unregister(&usb_bus_type);
- bus_register_failed:
- usb_debugfs_cleanup();
- out:
- return retval;
- }
/*
* Init
*/
static int __init usb_init(void)
{
int retval;
if (nousb) {
pr_info("%s: USB support disabled\n", usbcore_name);
return 0;
}
retval = usb_debugfs_init();
if (retval)
goto out;
retval = bus_register(&usb_bus_type);
if (retval)
goto bus_register_failed;
retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
if (retval)
goto bus_notifier_failed;
retval = usb_major_init();
if (retval)
goto major_init_failed;
retval = usb_register(&usbfs_driver);
if (retval)
goto driver_register_failed;
retval = usb_devio_init();
if (retval)
goto usb_devio_init_failed;
retval = usbfs_init();
if (retval)
goto fs_init_failed;
retval = usb_hub_init();
if (retval)
goto hub_init_failed;
retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
if (!retval)
goto out;
usb_hub_cleanup();
hub_init_failed:
usbfs_cleanup();
fs_init_failed:
usb_devio_cleanup();
usb_devio_init_failed:
usb_deregister(&usbfs_driver);
driver_register_failed:
usb_major_cleanup();
major_init_failed:
bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
bus_notifier_failed:
bus_unregister(&usb_bus_type);
bus_register_failed:
usb_debugfs_cleanup();
out:
return retval;
}
如果在boot参数(对u-boot是在bootargs里面加:nousb)里面有nousb,输出: usbcore: USB support disabled,然后退出。
如果在boot参数()里面没有nousb,则进行USB的初始化工作。
初始化的主要内容如下:
1. usb debug 文件系统初始化: usb_debugfs_init()。
2. 注册USB总线 :bus_register(&usb_bus_type)。
3. 给USB总线注册一个通知链 : bus_register_notifier(&usb_bus_type, &usb_bus_nb)。
4. usb_major_init
5. 注册usbfs驱动: usb_register(&usbfs_driver)
usbfs_driver在<drivers/usb/core/devio.c>中定义
6. usb_devio_init
7. USB文件系统注册:usbfs_init()。
注:如果没有CONFIG_USB_DEVICEFS,此函数为inline空函数。如果定义CONFIG_USB_DEVICEFS,则在<drivers/usb/core/inode.c>实现。
8. 对USB HUB进行初始化:usb_hub_init()
usb_hub_init()在<driver/usb/core/hub.c>中实现。
9. 注册一个generic的USB驱动:usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
下面文章会依次对每个部分进行详细描述。