基础知识
1、USB 包的种类
USB数据基本单位:usb包的种类,总体上分为四类:令牌包、数据包、握手包、特殊包。
2、USB 事务
USB数据逻辑单位:USB包组织成事务(transaction)进行传输

3、USB 的四种传输模式
- 批量传输 bluk

- 等时传输
- 中断传输
- 控制传输
usbmon 报文分析
usbmon 只抓取事务(transaction)中的数据包,不会抓取令牌包和握手包。
Here is the list of words, from left to right:
c
ffffffc5d99f4e00 744861615 S Ci:1:001:0 s a3 00 0000 0001 0004 4 <1
我们对上面报文进行拆分分析如下:
1、URB Tag(标签)
该字段表示驱动中定义的 struct urb 结构体变量在内核空间的地址:
c
ffffffc5d99f4e001
内核解释:
2、Timestamp(时间戳)
该字段表示的是时间戳,单位是微秒。1 微秒 = 10^(-6) 秒。
c
7448616151
内核解释:
文件:mon_text.c
c
static inline unsigned int mon_get_timestamp(void){struct timeval tval;unsigned int stamp;do_gettimeofday(&tval);
stamp = tval.tv_sec & 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */
stamp = stamp * 1000000 + tval.tv_usec;return stamp;}1
2
3
2
3
通过函数可知:时间戳单位为微秒、取值范围为 0 到 4096s
3、Event Type.(事件类型)
事件类型:
c
S1
This type refers to the format of the event, not URB type. Available types are:
S-submission,向usb controller提交URBC-callback,URB提交完成后的回调E-submission error,向usb controller提交URB发生错误
4、 “Address” word (formerly a “pipe”).
这个字段包含 4 部分,各个部分之间使用分号隔开,这 4 部分分别是 URB 类型及传输方向、usb 总线号、usb 设备地址、端点号。
c
Ci:1:001:01
- 1.
URB type and direction
c
== == =============================
Ci Co Control(控制传输) input and output
Zi Zo Isochronous(等时传输) input and output
Ii Io Interrupt(中断传输) input and output
Bi Bo Bulk(批量传输) input and output
== == =============================1
2
3
4
5
6
2
3
4
5
6
- 2.
Bus numberHost端一般有多个usb controller,每个usb controller都有一条对应的usb总线 - 3.
Device address该字段表示usb设备的地址 - 4.
Endpoint number设备的哪个端点
5、URB Status word.
这个字段有两种表示形式:
5.1、s + 一串数字
5.2、一串以分号间隔的数字(或单个数字)构成的。
- 批量传输:只包含
URB status这个字段 例如:
c
ffffffc60688f000 869878860 S Bo:1:002:4 -115 1 = 0d // 这里的-115(EINPROGRESS)仅仅表示URB被提交给usb控制器去处理了
ffffffc60688f000 869878978 C Bo:1:002:4 0 1 > // 成功向模块发送了T1
2
2
表格 还在加载中,请等待加载完成后再尝试复制
- 中断传输
- 等时传输
- 控制传输
6、Setup packet(控制传输建立阶段的数据包)
如果是控制传输,这个字段表示控制传输建立阶段主机发给设备的数据包。
7、Data Length(数据包的长度)

8、Data tag.
The usbmon may not always capture data, even if length is nonzero. The data words are present only if this tag is ‘=’.
- ”
=” 后面紧跟数据流 - “
>” 表示这是一次 Output 数据传输 - “
<” 表示这是一次 Input 数据传输
关键报文解析:

特殊报文定义: 目录:kernel\include\linux\usb\hcd.h
c
/* class requests from the USB 2.0 hub spec, table 11-15 *//* GetBusState and SetHubDescriptor are optional, omitted */#define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)#define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)#define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)#define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)#define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)#define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)#define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)1
目录:kernel\include\uapi\linux\usb\ch11.h
c
/*
* wPortStatus bit field
* See USB 2.0 spec Table 11-21
*/#define USB_PORT_STAT_CONNECTION 0x0001#define USB_PORT_STAT_ENABLE 0x0002#define USB_PORT_STAT_SUSPEND 0x0004#define USB_PORT_STAT_OVERCURRENT 0x0008#define USB_PORT_STAT_RESET 0x0010#define USB_PORT_STAT_L1 0x0020/* bits 6 to 7 are reserved */#define USB_PORT_STAT_POWER 0x0100#define USB_PORT_STAT_LOW_SPEED 0x0200#define USB_PORT_STAT_HIGH_SPEED 0x0400#define USB_PORT_STAT_TEST 0x0800#define USB_PORT_STAT_INDICATOR 0x1000/* bits 13 to 15 are reserved */1
2
3
4
2
3
4