https://www.cnblogs.com/shangdawei/archive/2012/11/01/2749619.html

1、事务

在介绍USB传输类型之前,请允许我先简答介绍一下USB事务。

事务一般由令牌包、数据包(可选)、握手包组成。

  • 令牌包:用来启动一个事务,总是由主机发送。
  • 数据包:可以从主机到设备,也可以由设备到主机,方向由令牌包决定。
  • 握手包:通常情况,数据的接收者发送握手包(ACK或者NAK)。

USB协议规定了四种传输类型:控制传输、批量传输、同步传输、中断传输。下面简答介绍一下这四种传输类型及其注意事项。

2、控制传输

2.1. 控制传输分为三个过程:建立过程、数据过程(可选)、状态过程。

建立过程:

a. 主机发送令牌包:SETUP

b. 主机发送数据包:DATA0

c. 设备返回握手包:ACK或不应答,注意:设备不能返回NAK或STALL,即设备必须接收建立事务的数据。

数据过程(可选):

一个数据过程可以包含多个数据事务,但所有数据事务必须是同一方向的。若数据方向发生了改变,则认为进入状态过程。数据过程的第一个数据包必须为DATA1,然后每次正确传输一个数据包后就在DATA0和DATA1之间交替。

状态过程:

状态过程只使用DATA1包,并且传输方向与数据过程相反。

2.2. 控制传输的应用场合:主要应用于设备的枚举过程和对数据准确性要求较高的场合。

3、同步传输

  • 同步传输的应用场合:主要用在数据量大,对实时性要求较高的场合。如视频设备、音频设备等。

4、批量传输

  • 批量传输使用批量传输事务,一次批量传输事务分为三个阶段:令牌包阶段、数据我阶段、握手包阶段。
  • 批量传输分为批量读和批量写,批量读使用批量输入事务,批量写使用批量输出事务。注意:不论输入还是输出都是以主机为参考的。
  • 批量传输的应用场合:主要用在数据量大、对数据实时性要求不高的场合,例如USB打印机、扫描仪、大容量存储设备等等。

批量输出流程:

1) 主机发送OUT令牌包。

说明:令牌包中包含设备地址、端点号。

2) 设备处理令牌包。

  • 令牌包正确:设备等待接收数据包。
  • 令牌包错误:设备既不接收、处理数据包,也不返回握手包,让主机等待超时。

注:令牌包的正确与否包括设备地址和端点号是否匹配。

3) 主机发送数据包,然后切换到接收模式,等待接收设备返回的握手包。

说明:数据包中包含一个DATA(具体是什么类型的DATA,取决于数据切换位)。

4) 设备处理数据包。

  • 数据包正确,并有足够的空间保存数据:设备返回ACK握手包或NYET握手包(只有告诉模式才有NYET握手包,它表示本次数据接收成功,但是没有能力接收下一次传输)。
  • 数据包正确,但是没有足够的空间保存数据:设备返回NAK握手包。主机收到NAK,延时一段时间后,再重新进行批量输出事务。
  • 数据包正确,但端点处于挂起状态:设备返回一个STALL握手包。
  • 数据包错误:设备不返回任何握手包,让主机等待超时。

批量输入流程:

1) 主机发送IN令牌包,然后切换到接收数据状态,等待设备返回数据。

说明:令牌包中包含设备地址、端点号。

2) 设备处理令牌包。

  • 令牌包正确,且有数据需要返回:设备将一个数据包放到总线上。
  • 令牌包正确,但没有数据需要返回:设备返回NAK握手包。主机收到NAK,延时一段时间后,再重新进行批量输入事务。
  • 令牌包正确,但端点处于挂起状态:设备返回一个STALL握手包。
  • 令牌包错误:设备不做任何响应,让主机等待超时。

3) 主机接收并处理数据包。

  • 数据包正确:主机发送一个ACK握手包。
  • 数据包错误:主机不做任何响应,让设备等待超时。

注:USB协议规定,不允许主机使用NAK握手包来拒绝接收数据包。

PING事务:

  • PING事务只有令牌包和握手包。
  • PING事务是USB2.0高速模式特有的,全速模式和低速模式没有。
  • PING事务作用:主机探测设备是否有空间接收数据。

PING流程:

1. 主机发送PING令牌包。

2. 设备返回状态。

  • 设备有空间接收数据:设备返回ACK握手包。
  • 设备没有空间接收数据:设备返回NAK握手包。
  • 设备端点挂起:设备返回STALL握手包。

5、中断传输

  • 中断传输是一种保证查询频率的传输。
  • 中断端点在端点描述符中报告它的查询间隔,主机会保证在不小于这个时间间隔的范围内安排一次传输。
  • 中断传输可以用来不断检测某个状态,当条件满足后,再使用批量传输来传送大量的数据。
  • 中断传输和批量传输的结构基本上一样(除了对端点查询策略)。
  • 中断传输的应用场合:主要用在数据量小,但对时间要求严格的场合。如人机接口设备(HID)中的鼠标、键盘、轨迹球等。

6、端点

  • 一个具体的端点只能工作在一种传输模式下。通常我们把工作在什么模式下的端点,叫做什么端点。如控制端点、批量端点、同步端点、中断端点。
  • 端点0是设备的默认控制端点,在设备上电后就存在并可以使用,在Set Config之前所有的传输都是通过端点0传输的。
  • 端点描述符中规定了端点所能支持的最大包长。
    • 控制传输:高速模式的最大包长固定为64个字节;全速模式可在8、16、32、64字节中选择;低速模式的最大包长固定为8个字节。
    • 批量传输:高速模式固定为512个字节;全速模式最大包长可在8、16、32、64字节中选择;低速模式不支持批量传输。
    • 同步传输:高速模式的最大包长上限为1024个字节;全速模式的最大包长上限为1023个字节;低速模式不支持同步传输。
    • 中断传输:告诉模式的最大包长上限为1024个字节;全速模式最大包长上限为64个字节;低速模式最大最大包长上限为8个字节。

-



USB枚举,USB Emulation,从字面意思看,就是去列举USB,而列举啥呢,其实就是USB的初始化。

简单来说,USB的枚举,对应的就是USB的Host和Device之间的对话,即Host根据Device所报告上来的参数,得知USB的device是啥类型的,具有啥功能,然后初始化相关参数。

接下来,就USB Device就可以正常工作了。

所以,可以简单的理解为,USB枚举,就是USB设备的初始化(init)。

USB枚举过程

  • 1 获取USB设备的设备描述符 : 使用默认地址0
  • 2 成功后复位设备
  • 3 对设备进行地址设置  : 使设备从地址状态进入寻址状态
  • 4 再次获取设备描述符 : 使用新的地址
  • 5 获取设备配置符
  • 6 根据设备配置符中的配置符总长度,获取其他配置符(接口,端点等)
  • 7 设置配置 : 使设备从地址状态进入配置状态

Enumeration Steps

The USB 2.0 specification defines six device states.

During enumeration, a device moves through the Powered, Default, Address, and Configured states.

(The other states are Attached and Suspend.)

In each state, the device has defined capabilities and behavior.

Typical USB 2.0 Sequence

The steps below are a typical sequence of events that occurs during enumeration of a USB 2.0 device under Windows.

Device firmware shouldn’t assume that enumeration requests and events will occur in a particular order.

To function successfully, a device must detect and respond to any control request or other bus event at any time.

1. The system has a new device.

A user attaches a device to a USB port, or the system powers up with a device attached. 
The port may be on the root hub at the host or on a hub that connects downstream from the host. 
The hub provides power to the port, and the device is in the Powered state. 
The device can draw up to 100 mA from the bus.

2. The hub detects the device.

The hub monitors the voltages on the signal lines (D+ and D-) at each of its ports. 
The hub has a pull-down resistor of 14.25k–24.8kW on each line. 
A device has a pull-up resistor of 900–1575W on D+ for a full-speed device or D- for a low-speed device. 
High-speed-capable devices attach at full speed. 
On attaching to a port, the device’s pull-up brings its line high, enabling the hub to detect that a device is attached. 
On detecting a device, the hub continues to provide power but doesn’t yet transmit USB traffic to the device. 
Chapter 15 has more on how hubs detect devices.

3. The host learns of the new device.

Each hub uses its interrupt endpoint to report events at the hub. 
The report indicates only whether the hub or a port (and if so, which port) has experienced an event. 
On learning of an event, the host sends the hub a Get Port Status request to find out more. 
Get Port Status and the other hub-class requests described are standard requests that all hubs support. 
The information returned tells the host when a device is newly attached.

4. The hub detects whether a device is low or full speed.

Just before resetting the device, the hub determines whether the device is low or full speed 
by examining the voltages on the two signal lines. 
The hub detects the device’s speed by determining which line has a higher voltage when idle. 
The hub sends the information to the host in response to the next Get Port Status request. 
A USB 1.x hub may instead detect the device’s speed just after a bus reset. 
USB 2.0 requires speed detection before the reset so the hub knows 
whether to check for a high-speed-capable device during reset as described below.

5. The hub resets the device.

When a host learns of a new device, the host sends the hub a Set Port Feature request that asks the hub to reset the port. 
The hub places the device’s USB data lines in the Reset condition for at least 10 ms. 
Reset is a special condition where both D+ and D- are logic low. 
(Normally, the lines have opposite logic states.) 
The hub sends the reset only to the new device. 
Other hubs and devices on the bus don’t see the reset.

6. The host learns if a full-speed device supports high speed.

Detecting whether a device supports high speed uses two special signal states. 
In the Chirp J state, only the D+ line is driven and in the Chirp K state, only the D- line is driven.
During the reset, a device that supports high speed sends a Chirp K. 
A high-speed-capable hub detects the Chirp K and responds with a series of alternating Chirp K and Chirp J. 
On detecting the pattern KJKJKJ, the device removes its full-speed pull-up 
and performs all further communications at high speed. 
If the hub doesn’t respond to the device’s Chirp K, the device knows it must continue to communicate at full speed. 
All high-speed devices must be capable of responding to control requests at full speed.

7. The hub establishes a signal path between the device and the bus.

The host verifies that the device has exited the reset state by sending a Get Port Status request. 
A bit in the returned data indicates whether the device is still in the reset state. 
If necessary, the host repeats the request until the device has exited the reset state.
When the hub removes the reset, the device is in the Default state. 
The device’s USB registers are in their reset states, and the device is ready to respond to control transfers at endpoint zero. 
The device communicates with the host using the default address of 00h.

8. The host sends a Get Descriptor request to learn the maximum packet size of the default pipe.

The host sends the request to device address 00h, endpoint zero. 
Because the host enumerates only one device at a time, 
only one device will respond to communications addressed to device address 00h even if several devices attach at once.
The eighth byte of the device descriptor contains the maximum packet size supported by endpoint zero. 
A Windows host requests 64 bytes but after receiving just one packet (whether or not it has 64 bytes), 
the host begins the Status stage of the transfer. 
On completing the Status stage, Windows requests the hub to reset the device as in step 5 above. 
The USB 2.0 specification doesn’t require a reset here. 
The reset is a precaution that ensures that the device will be in a known state when the reset ends.

9. The host assigns an address.

When the reset is complete, the host controller assigns a unique address to the device by sending a Set Address request. 
The device completes the Status stage of the request using the default address and then implements the new address. 
The device is now in the Address state. 
All communications from this point on use the new address. 
The address is valid until the device is detached, a hub resets the port, or the system reboots. 
On the next enumeration, the host may assign a different address to the device.

10. The host learns about the device’s abilities.

The host sends a Get Descriptor request to the new address to read the device descriptor. 
This time the host retrieves the entire descriptor. 
The descriptor contains the maximum packet size for endpoint zero, 
the number of configurations the device supports, and other basic information about the device.
The host continues to learn about the device by requesting the one or more configuration descriptors specified in the device descriptor.
A request for a configuration descriptor is actually a request for the configuration descriptor 
followed by all of its subordinate descriptors up to the number of bytes requested. 
A Windows host begins by requesting just the configuration descriptor’s nine bytes. 
Included in these bytes is the total length of the configuration descriptor and its subordinate descriptors.

Windows then requests the configuration descriptor again, this time requesting the number of bytes in the retrieved total length. 
The device responds by sending the configuration descriptor followed by all of the configuration’s subordinate descriptors, 
including interface descriptor(s), with each interface descriptor followed by any endpoint descriptors for the interface. 
Some configurations also have class- or vendor-specific descriptors. This chapter has more on what the descriptors contain.

11. The host assigns and loads a device driver (except for composite devices).

After learning about a device from its descriptors, the host looks for the best match in a driver to manage communications with the device. 
Windows hosts use INF files to identify the best match. 
The INF file may be a system file for a USB class or a vendor-provided file that contains the device’s Vendor ID and Product ID. 
Chapter 9 has more about selecting a driver.

For devices that have been enumerated previously, Windows may use stored information instead of searching the INF files. 
After the operating system assigns and loads the driver, the driver may request the device to resend descriptors or send other class-specific descriptors.

An exception to this sequence is composite devices, which can have different drivers assigned to multiple interfaces in a configuration. 
The host can assign these drivers only after enabling the interfaces, so the host must first configure the device as described below.

12. The host’s device driver selects a configuration.

After learning about a device from the descriptors, the device driver requests a configuration 
by sending a Set Configuration request with the desired configuration number. 
Many devices support only one configuration. 
If a device supports multiple configurations, the driver can decide 
which configuration to request based on information the driver has about how the device will be used, 
or the driver can ask the user what to do or just select the first configuration. 
(Many drivers only select the first configuration.) 
On receiving the request, the device implements the requested configuration. 
The device is now in the Configured state and the device’s interface(s) are enabled.

For composite devices, the host can now assign drivers. 
As with other devices, the host uses the information retrieved from the device 
to find a driver for each active interface in the configuration.

The device is then ready for use.

Hubs are also USB devices, and the host enumerates a newly attached hub in the same way as other devices. 
If the hub has devices attached, the host enumerates these after the hub informs the host of their presence.

Attached state.

If the hub isn’t providing power to a device’s VBUS line, the device is in the Attached state. 
The absence of power may occur if the hub has detected an over-current condition or 
if the host requests the hub to remove power from the port. 
With no power on VBUS, the host and device can’t communicate, 
so from their perspective, the situation is the same as when the device isn’t attached.

Suspend State.

A device enters the Suspend state after detecting no bus activity, including SOF markers, for at least 3 ms. 
In the Suspend state, the device should limit its use of bus power. 
Both configured and unconfigured devices must support this state. 
Chapter 16 has more about the Suspend state.

SuperSpeed Differences

Enumerating SuperSpeed devices has some differences compared to USB 2.0:

  • On detecting a downstream SuperSpeed termination at a port, a hub initializes and trains the port’s link. Enumeration then proceeds at SuperSpeed with no need for further speed detecting.
  • The host isn’t required to reset the port after learning of a new device.
  • The bus-current limits are 150 mA before configuration and 900 mA after configuration.
  • The host sends a Set Isochronous Delay request to inform the device of the bus delay for isochronous packets.
  • The host sends a Set SEL request to inform the device of the system exit latency (the amount of time required to transition out of a low-power state).
  • Protocols for entering and exiting the Suspend state differ.
  • For hubs, the host sends a Set Hub Depth request to set the hub-depth value.

1、集线器检测新设备
主机集线器监视着每个端口的信号电压,当有新设备接入时便可觉察。(集线器端口的两根信号线的每一根都有15kΩ的下拉电阻,而每一个设备在D+都有一个1.5kΩ的上拉电阻。当用USB线将PC和设备接通后,设备的上拉电阻使信号线的电位升高,因此被主机集线器检测到。)

2、主机知道了新设备连接
每个集线器用中断传输来报告在集线器上的事件。当主机知道了这个事件,它给集线器发送一个Get_Status请求来了解更多的消息。返回的消息告诉主机一个设备是什么时候连接的。

3、集线器重新设置这个新设备
当主机知道有一个新的设备时,主机给集线器发送一个Set_Feature请求,请求集线器来重新设置端口。集线器使得设备的USB数据线处于重启(RESET)状态至少10ms。

4、集线器在设备和主机之间建立一个信号通路
主机发送一个Get_Status请求来验证设备是否激起重启状态。返回的数据有一位表示设备仍然处于重启状态。当集线器释放了重启状态,设备就处于默认状态了,即设备已经准备好通过Endpoint 0 的默认流程响应控制传输。即设备现在使用默认地址0x0与主机通信。

5、集线器检测设备速度
集线器通过测定那根信号线(D+或D-)在空闲时有更高的电压来检测设备是低速设备还是全速设备。(全速和高速设备D+有上拉电阻,低速设备D-有上拉电阻)。

6、获取最大数据包长度
向address0发送USB协议规定的Get_Device_Descriptor命令,以取得却缺省控制管道所支持的最大数据包长度,并在有限的时间内等待USB设备的响应,主机一次只能列举一个USB设备,所以同一时刻只能有一个USB设备使用缺省地址0。

7、主机分配一个新的地址给设备
主机通过发送一个Set_Address请求来分配一个唯一的地址给设备。设备读取这个请求,返回一个确认,并保存新的地址。从此开始所有通信都使用这个新地址。

8、主机向新地址重新发送Get_Device_Descriptor命令,此次读取其设备描述符的全部字段,以了解该设备的总体信息,如VID,PID。

9、主机向设备循环发送Get_Device_Configuration命令,要求USB设备回答,以读取全部配置信息。

10、主机发送Get_Device_String命令,获得字符集描述(unicode),比如产商、产品描述、型号等等。

11、此时主机将会弹出窗口,展示发现新设备的信息,产商、产品描述、型号等。

12、根据Device_Descriptor和Device_Configuration应答,PC判断是否能够提供USB的Driver,一般能提供几大类的设备,如游戏操作杆、存储、打印机、扫描仪等,操作就在后台运行。但是有的不可以,所以在此时将会弹出对话框,索要USB的Driver。

13、加载了USB设备驱动以后,主机发送Set_Configuration(x)命令请求为该设备选择一个合适的配置(x代表非0的配置值)。如果配置成功,USB设备进入“配置”状态,并可以和客户软件进行数据传输。此时,常规的USB完成了其必须进行的配置和连接工作。查看注册表,能够发现相应的项目已经添加完毕,至此设备应当可以开始使用。不过,USB协议还提供了一些用户可选的协议,设备如果不应答,也不会出错,但是会影响到系统的功能。

USB枚举过程 (一)相关推荐

  1. Linux-USB学习 -- USB枚举过程

    USB架构中, hub负责检测设备的连接和断开,利用其中断IN端点(Interrupt IN Endpoint)来向主机(Host)报告.在系统启动时,主机轮询它的根hub(Root Hub)的状态看 ...

  2. USB具体通讯过程(含枚举过程)

    /*********************************************************************** USB入门系列: 1.USB行业标准介绍 2.USB具 ...

  3. 基于s3c6410 otg controller的gadget driver及usb枚举分 析

    一.简介      一个完整的USB系统由两部分构成,即usb主机(usb host)和usb设备(usb device).usb主机通常是指我们的pc机.具有host controller的嵌入式设 ...

  4. ctl命令 usb_USB入门系列之六 —— USB设备的枚举过程

    USB主机在检测到USB设备插入后,就要对设备进行枚举了.为什么要枚举呢?枚举就是从设备读取一些信息,知道设备是什么样的设备,如何进行通信,这样主机就可以根据这些信息来加载合适的驱动程序.调试USB设 ...

  5. USB 之枚举过程概述

    来源:公众号[鱼鹰谈单片机] 作者:鱼鹰Osprey ID   :emOsprey 上篇笔记我们大概了解了一下 USB 设备插入主机后经历了什么阶段,本篇笔记和接下来的笔记将详细介绍整个流程. 每一个 ...

  6. USB UVC实战笔记第1篇—UVC设备枚举过程详细分析

    1 UVC枚举过程分析 UVC,全称为:USB video class 或USB video device class.是Microsoft与另外几家设备厂商联合推出的为USB视频捕获设备定义的协议标 ...

  7. 第五篇 USB设备枚举过程(1)

    上一篇:https://blog.csdn.net/qq_40088639/article/details/109741653 总述 1. 设备枚举的整个过程 USB设备枚举过程,可大致分为下面的几个 ...

  8. usb mass storage设备枚举过程

    8月份从PC开发岗位换到底层嵌入式驱动开发来了.接到的第二个活,是在高通8650平台上实现驻留支持SCSI指令.8650平台是高通推出的EVDO RevB的双核平台,和之前6085相比性能更强,软件结 ...

  9. 5、USB协议学习:USB的枚举过程

    文章目录 枚举顺序 枚举过程 标准请求 bmRequestType bReqest 请求类型 GetDescriptor 设备描述符 设备描述符定义 获取设备描述符 返回设备描述符 配置描述符 配置描 ...

最新文章

  1. Eclipse link方式安装反编译插件
  2. 美团十年,支撑最大规模外卖配送的一站式机器学习平台如何炼成?
  3. Linux C 程序的开发环境
  4. Qt实现截屏并保存(转载)
  5. 傅里叶变换进行缺陷检测detect_indent_fft.hdev(源代码与详细解析)
  6. php curlopt_postfields,PHP的CURLOPT_POSTFIELDS参数使用数组和字符串的区别 - CSDN博客
  7. paramiko执行nohup_记一次使用django+paramiko远程操作时报错无法返回问题
  8. 最新SparkSQL 学习路径. pdf(廖雪峰团队研发)
  9. python 上传excel_简历批量合并Python+VBA小工具
  10. Dubbo面试 - 如何自己设计一个类似 Dubbo 的 RPC 框架?
  11. ORA-27300错误
  12. 【网络安全面试题】—如何注入攻击Java Python PHP等主流语言
  13. MATLAB图形界面设计(上)
  14. 自治系统中单个路由表的构造
  15. python做路径图_python实现生成图片路径和对应标签
  16. STAMP:Short-TermAttention/MemoryPriorityModelfor Session-basedRecommendation
  17. 智能电销机器人语音自动外呼效果好吗?《各版本机器人部署》
  18. 创维电视显示服务器异常,创维网络电视,看在线电影及网络异常的处理!
  19. AOE网络-关键路径
  20. cmdb 指南_无限供应商制造商指南

热门文章

  1. 微信小程序教学第二章(含视频):小程序中级实战教程之预备篇 - 提取util公用方法 |基于最新版1.0开发者工具
  2. mtk-drm property简单理解
  3. excel函数实战(CEILING函数,ROUND函数,FLOOR函数的语法)
  4. nce计算机证书,Nova科技第二届工程师技术认证大会NCE完美收官
  5. godaddy mysql不存在_godaddy win主机安装ecshop提示不支持mysql
  6. 在线音乐网站的设计和搭建
  7. 词云中去重复的词_手把手教你怎么挖掘蓝海词,打造淘宝爆款标题
  8. java基础与案例开发详解 pdf下载_Java基础与案例开发详解(张孝祥) pdf_IT教程网...
  9. 单片机中如何通过程序控制震动马达震动
  10. 甘肃郎木寺:寻梦者的天堂