http://blog.csdn.net/myarrow/article/details/7020728
usbfs为咱们提供了在用户空间直接访问usb硬件设备的接口,但是世界上没有免费的午餐,它需要内核的大力支持, usbfs_driver 就是用来完成这个光荣任务的。
/dev: 该目录包含了Linux系统中使用的所有外部设备,它实际上是访问这些外部设备的端口,访问这些外部设备与访问一个文件或一个目录没有区别。这包括终端设备(tty*)、软盘(fd*)、硬盘(hd*)、RAM(ram*)和CD-ROM(cd*)(用户通常通过设备文件直接访问这些设备)。
/proc:包含有关系统资源的信息。
/sys:一个类似于/proc的文件系统,在Linux2.6内核中最新出现的,包含的文件用于获得硬件状态并反映内核看到的系统设备树。它使用了/proc中的很多帮助。
USB从出生到死亡,其中会经历些什么,他对外界要求多吗? 对其中充满好奇心!
Usb的设备是非常复杂的,它由许多不同的逻辑单元组成,这些逻辑单元之间的关系可以简单地描述如下:
l 设备(usb_device)通常具有一个或者更多的配置(usb_host_config)
l 配置通常具有一个或者更多的接口(usb_interface)
l 接口通常具有一个或者更多的设置(usb_host_interface )
l 设置没有或者具有一个以上的端点(usb_host_endpoint)
1. USB设备的出生
当你将usb 设备连接在hub 的某个端口上,hub 检测到有设备连接了进来,于是精神头儿就上来了,就觉得有必要为设备做点什么。它会为设备分配一个struct usb_device 结构的对象并初始化,并调用设备模型提供的接口将设备添加到usb 总线的设备列表里,然后usb 总线会遍历驱动列表里的每个驱动,调用自己的match 函数看它们和你的设备或接口是否匹配。
hub 检测到自己的某个端口有设备连接了进来后,它会调用core 里的usb_alloc_dev 函数为struct usb_device 结构的对象申请内存,这个函数在usb.c 文件里定义:
struct usb_device * usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
usb_alloc_dev 函数就相当于usb 设备的构造函数,参数parent 是设备连接的那个hub,bus 是设备连接的那条总线,port1 就是设备连接在hub 上的那个端口。
Linux usb_device usb_bus usb_driver的三角关系-USB Device
一、USB Device
![](http://hi.csdn.net/attachment/201112/2/0_13228177003PZQ.gif)
1. struct device:
The Basic Device Structure, generic device interface(所有设备的抽象)
-struct bus_type *bus; /* type of bus device is on */
-struct device_driver *driver; /* which driver has allocated this device */
2. struct usb_device:
kernel's representation of a USB device (它包含struct device,USB设备)
-struct device dev; /* generic device interface */
-struct usb_device_descriptor descriptor; /* USB device descriptor */
- struct usb_bus *bus; /* bus we're part of */
-struct usb_host_endpoint ep0; /* endpoint 0 data (default control pipe) */
-struct usb_host_config *config; /* all of the device's configs */
-struct usb_host_config *actconfig; /* the active configuration */
-struct usb_host_endpoint *ep_in[16]; /* array of IN endpoints */
-struct usb_host_endpoint *ep_out[16]; /* array of OUT endpoints */
3. struct usb_host_config
representation of a device's configuration
/* array of pointers to usb_interface structures, one for each interface in the configuration. These pointers are valid only while the the configuration is active.*/
-struct usb_interface *interface[USB_MAXINTERFACES];
/* array of pointers to usb_interface_cache structures, one for each interface in the configuration. These structures exist for the entirelife of the device. Interface information availableeven when this isnot activeconfiguration */
-struct usb_interface_cache *intf_cache[USB_MAXINTERFACES];
4. struct usb_interface
what usb device drivers talk to
/* array of alternate settings for this interface, stored in no particular order. one for each alternate setting that may be selected. Each one includes a set of endpoint configurations. */
-struct usb_host_interface *altsetting;
-struct usb_host_interface *cur_altsetting; /* the currently
5. struct usb_host_interface
host-side wrapper for one interface setting's parsed descriptors
-struct usb_interface_descriptor desc;
/* array of desc.bNumEndpoint endpoints associated with this interface setting. these will be in no particular order.*/
-struct usb_host_endpoint *endpoint;
6. struct usb_host_endpoint
host-side endpoint descriptor and queue
/* descriptor for this endpoint, e.g.: wMaxPacketSize */
-struct usb_endpoint_descriptor desc;
/*urbs queued to this endpoint; maintained by usbcore*/
-struct list_head urb_list;
/* for use by HCD; typically holds hardware dma queue head (QH) */
-void *hcpriv;
/* ep_device for sysfs info*/
-struct ep_device *ep_dev;
7. usb_alloc_dev
usb device constructor (usbcore-internal)
struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
@parent: hub to which device is connected; null to allocate a root hub
@bus: bus used to access the device
@port1: one-based index of port; ignored for root hubs