您的位置:首页 > 其它

USB枚举过程的详细分析<转>

2013-12-19 20:47 375 查看
原文地址:USB枚举过程的详细分析<转>作者:闫天瑜

借了同学各S 3C2440的开发板来玩,脑子一发热想看看USB HOST是怎么用的。于是到处找资料,看规范(包括USB协议规范和OHCI规范),写程序,调程序,折腾了好几天,还算有点儿进展,下面就我用S3C2440的usb host对Netac的U盘枚举过程为例详细说明枚举的过程。这是本人初次接触USB技术,理解难免偏差,望大家不吝赐教!

对2440的USB HOST进行初始化完毕(主要包括对符合OHCI规范的寄存器的初始化—总线复位、中断使能、清除中断标志、电源管理、内存指针寄存器的初始化,各种数据结构的初始化等),等待USB设备的插入,当2440检测到有设备插入,就要对设备进行枚举了。那么为什么要对设备进行枚举呢?起始枚举就相当于主机和设备建立连接的过程(接头),Host向Device询问一些东西,Device将自身的设备类型,如何进行通信报告给Host,这样Host就知道怎么着对Device进行操作了。

枚举的过程实际上用到而且只用到了总线的“控制传输(Control Transfer)”的传输方式。这种传输方式通常用于配置/命令/状态等情形,其中的设置操作setup和状态操作status过程的数据包具有USB协议定义的数据结构,因此,控制传输只能通过消息管道进行。

一个完整的控制传输包括三个过程:1.建立连接 2.数据过程(可选) 3.状态过程

建立连接的过程都是有Host发起,它开始于一个Setup令牌包,后面紧跟一个DATA0包。如果是控制输入传输,数据过程则为输入数据,若是控制输出传输,则数据过程是输出数据。

数据过程的可选型是指设置过程需要指定数据长度,如果指定为0,则没有数据过程。状态过程跟在数据过程之后,状态过程恰好和数据过程的数据传输方向相反,因为此阶段主要是用来确认之前两阶段的所有数据都已经正确传输了。

好了,下面就结合我的这个实例来看看枚举的详细过程:

1.控制2440向U盘发送第一个Setup包,内容是80 06 00 01 00 00 08
00
,其中最后的0008表示得到DEVICE_DCESCRIPTOR的前8个字节,因为这个包的主要目的是要获得USB Device中端点0的最大包的大小(第8个字节),所以只需要8个字节就可以了。USB Device返回的设备标识符为12 01 10 01 00 00 00
40
,下面我们需要对0x40记录下来,将其放到Endpoint
Descriptor
数据结构的DWORD0的MPS(bit16~bit32)块中去。

2.接下来2440发送第二个Setup包,内容是00 05 01 00 00 00 00
00
这一个次的作用是为USB设备分配地址(相当于SD卡中的RCA)。如果USB Device接收并接受了此地址设置包,会返回一个长度为0的数据包。Host接收到长度为0的状态包之后就会返回一个ACK给Device,Device再接收到这个ACK之后,就可以启用新地址了。这样Device就得到了一个唯一的设备地址,作为主机通信的唯一表示。

3.发送第三个Setup包,内容是80 06 00 02 00 00 09
00
,这次是为了获取配置描述符集合的大小,此位位于读回数据的第三个字节。U盘返回的数据为09 02 20 00 01 01 00 80
32
,即描述符集合总大小为0x20。

4.发送第四个Setup包,内容是80 06 00 02 00 00 09
00
,和上次不同的仅仅是,这次要读回来的数据是整个配置描述符区域。U盘返回来的数据是09 02 20 00 01 01 00 80 32 09 04 00 00 02
08 06 50 00 07 05 82 02 40
00 00
07 05 02 02 40 00 00


这时候我们就可以知道该设备是什么类型的设备,支持什么样的操作了。

上述这两个过程也有的程序就是直接读取0xff个字符大小,当然同样可以达到读回设备描述符集合的目的。

至此,我们已经得到了所需要的设备信息,之后就可以对设备进行配置了。

5.向设备发送第五个Setup包,数据为00 09 01 00 00 00 00
00
,USB Device返回一个长度为0的数据包,表明数据正确接收。

至此,USB的枚举过程就完成了。

以上是我的理解,不知道有没有什么问题。有什么问题希望大家指出,谢谢!

附:USB1.1规范中对枚举过程的说明

9.1.2 Bus Enumeration

When a USB device is attached to or removed from the USB, the host
uses a process known as bus

enumeration to identify and manage the device state changes
necessary. When a USB device is attached to

a powered port, the following actions are taken:

1. The hub to which the USB device is now attached informs the host
of the event via a reply on its status

change pipe (refer to Section 11.13.3 for more information). At
this point, the USB device is in the

Powered state and the port to which it is attached is
disabled.

2. The host determines the exact nature of the change by querying
the hub.

3. Now that the host knows the port to which the new device has
been attached, the host then waits for at

least 100 ms to allow completion of an insertion process and for
power at the device to become stable.

The host then issues a port enable and reset command to that port.
Refer to Section 7.1.7.1 and Figure

7-19 for sequence of events and timings of connection through
device reset.

4. The hub maintains the reset signal to that port for 10 ms (See
Section 11.5.1.5). When the reset signal

is released, the port has been enabled. The USB device is now in
the Default state and can draw no

more than 100mA from VBUS. All of its registers and state have been
reset and it answers to the

default address.

5. The host assigns a unique address to the USB device, moving the
device to the Address state.

6. Before the USB device receives a unique address, its Default
Control Pipe is still accessible via the

default address. The host reads the device descriptor to determine
what actual maximum data payload

size this USB device’s default pipe can use.

7. The host reads the configuration information from the device by
reading each configuration zero to

n-1, where n is the number of configurations. This process may take
several milliseconds to complete.

8. Based on the configuration information and how the USB device
will be used, the host assigns a

configuration value to the device. The device is now in the
Configured state and all of the endpoints in

this configuration have taken on their described characteristics.
The USB device may now draw the

amount of VBUS power described in its descriptor for the selected
configuration. From the device’s

point of view it is now ready for use.

When the USB device is removed, the hub again sends a notification
to the host. Detaching a device

disables the port to which it had been attached. Upon receiving the
detach notification, the host will update

its local topological information.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: