Linux USB subsystem --- USB File System initialize


目的:对USB作深入学习,在此留下笔记。欢迎讨论。

[Linux 3.2] [driver/usb/core/inode.c]

函数:usbfs_init()
USB文件系统的初始化取决于是否CONFIG_USB_DEVICEFS. (make menuconfig ---> Device Drivers ---> USB support --->  USB device filesystem (DEPRECATED))

如果没有配置CONFIG_USB_DEVICEFS, 则usbfs_init()为inline函数,直接return 0;

如果配置CONFIG_USB_DEVICEFS, 则代码如下:

  1. static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)  
  2. {  
  3.     switch (action) {  
  4.     case USB_DEVICE_ADD:  
  5.         usbfs_add_device(dev);  
  6.         break;  
  7.     case USB_DEVICE_REMOVE:  
  8.         usbfs_remove_device(dev);  
  9.         break;  
  10.     case USB_BUS_ADD:  
  11.         usbfs_add_bus(dev);  
  12.         break;  
  13.     case USB_BUS_REMOVE:  
  14.         usbfs_remove_bus(dev);  
  15.     }  
  16.   
  17.     usbfs_update_special();  
  18.     usbfs_conn_disc_event();  
  19.     return NOTIFY_OK;  
  20. }  
  21.   
  22. static struct notifier_block usbfs_nb = {  
  23.     .notifier_call =    usbfs_notify,  
  24. };  
  25.   
  26. /* --------------------------------------------------------------------- */  
  27.   
  28. static struct proc_dir_entry *usbdir = NULL;  
  29.   
  30. int __init usbfs_init(void)  
  31. {  
  32.     int retval;  
  33.   
  34.     retval = register_filesystem(&usb_fs_type);  
  35.     if (retval)  
  36.         return retval;  
  37.   
  38.     usb_register_notify(&usbfs_nb);  
  39.   
  40.     /* create mount point for usbfs */  
  41.     usbdir = proc_mkdir("bus/usb", NULL);  
  42.   
  43.     return 0;  
  44. }  

其主要作用:注册usb文件系统,注册一个usbfs_nb通知链,最后是在proc文件系统下面创建bus/usb目录。

进入/proc/bus/usb目录

# ls

001      002      devices

此devices的内容完全与usb debug文件系统里的devices文件一样。

001,代表usb总线1

002,代表usb总线2

注意:具体内容可以参见<Documentations/usb/proc_usb_info.txt>

问题:这些内容是如何产生的呢?

回答:见后面分析。

相关内容