USB调试技巧:USB抓包软件usbmon报文解析
1.1、USB 包的种类
USB数据基本单位:usb包的种类,总体上分为四类:令牌包、数据包、握手包、特殊包。
1.2、USB事务
USB数据逻辑单位:USB包组织成事务(transaction)进行传输

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

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

1.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
/* 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)目录:kernel\include\uapi\linux\usb\ch11.h
/*
* 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 */2
3
4