一,简介
本文通过USB monitor工具抓取USB FS HID设备枚举过程的协议,来分析USB HID设备在电脑的枚举过程,供参考。
二,枚举过程协议分析
2.1 复位并获取设备描述符
主机发送:
80 06 00 01 00 00 40 00
//获取设备描述符
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x01, // wValue[8:15] Desc Type: (Device)
0x00, 0x00, // wIndex Language ID: 0x00
0x40, 0x00, // wLength = 64
// 8 bytes
// best guess: USB Standard Request
从机返回:
12 01 00 02 00 00 00 40 83 04 50 57 00 02 01 02 03 01
//返回HID设备描述符
0x12, // bLength
0x01, // bDescriptorType (Device)
0x00, 0x02, // bcdUSB 2.00
0x00, // bDeviceClass (Use class information in the Interface Descriptors)
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x40, // bMaxPacketSize0 64
0x83, 0x04, // idVendor 0x0483
0x50, 0x57, // idProduct 0x5750
0x00, 0x02, // bcdDevice 4.00
0x01, // iManufacturer (String Index)
0x02, // iProduct (String Index)
0x03, // iSerialNumber (String Index)
0x01, // bNumConfigurations 1
// 18 bytes
// best guess: USB Standard Descriptor
2.2 设置USB设备地址
主机发送:
00 05 04 00 00 00 00 00
//设置设备地址为0x04
丛机返回:
从机返回空数据ACK代表收到地址,后续通信设备地址变成0x04
2.3 设置USB从机地址之后,再次获取设备描述符
主机发送:
80 06 00 01 00 00 12 00
//再次获取设备地址
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x01, // wValue[8:15] Desc Type: (Device)
0x00, 0x00, // wIndex Language ID: 0x00
0x12, 0x00, // wLength = 18
// 8 bytes
// best guess: USB Standard Request
从机返回:
12 01 00 02 00 00 00 40 83 04 50 57 00 02 01 02 03 01
//返回设备描述符
0x12, // bLength
0x01, // bDescriptorType (Device)
0x00, 0x02, // bcdUSB 2.00
0x00, // bDeviceClass (Use class information in the Interface Descriptors)
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x40, // bMaxPacketSize0 64
0x83, 0x04, // idVendor 0x0483
0x50, 0x57, // idProduct 0x5750
0x00, 0x02, // bcdDevice 4.00
0x01, // iManufacturer (String Index)
0x02, // iProduct (String Index)
0x03, // iSerialNumber (String Index)
0x01, // bNumConfigurations 1
// 18 bytes
// best guess: USB Standard Descriptor
2.4 获取配置描述符
主机发送:
80 06 00 02 00 00 FF 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x02, // wValue[8:15] Desc Type: (Configuration)
0x00, 0x00, // wIndex Language ID: 0x00
0xFF, 0x00, // wLength = 255
// 8 bytes
// best guess: USB Standard Request
从机返回:
09 02 29 00 01 01 00 C0 32 09 04 00 00 02 03 00 00 00 09 21 11 01 00 01 22 02 00 07 05 81 03 02 00 05 07 05 01 03 02 00 05
//
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x29, 0x00, // wTotalLength 41
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0xC0, // bmAttributes Self Powered
0x32, // bMaxPower 100mA
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x00, // bInterfaceNumber 0
0x00, // bAlternateSetting
0x02, // bNumEndpoints 2
0x03, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0x00, // iInterface (String Index)
0x09, // bLength
0x21, // bDescriptorType (HID)
0x11, 0x01, // bcdHID 1.11
0x00, // bCountryCode
0x01, // bNumDescriptors
0x22, // bDescriptorType[0] (HID)
0x02, 0x00, // wDescriptorLength[0] 2
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x81, // bEndpointAddress (IN/D2H)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x01, // bEndpointAddress (OUT/H2D)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
// 41 bytes
// best guess: USB Standard Descriptor
2.5 获取字符串描述符
主机发送:
80 06 03 03 09 04 FF 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x03, // wValue[0:7] Desc Index: 3
0x03, // wValue[8:15] Desc Type: (String)
0x09, 0x04, // wIndex Language ID: 0x0409
0xFF, 0x00, // wLength = 255
// 8 bytes
// best guess: USB Standard Request
从机返回:
1A 03 33 00 35 00 36 00 31 00 33 00 34 00 35 00 30 00 33 00 35 00 33 00 34 00
//字符串:..3.5.6.1.3.4.5.0.3.5.3.4.
2.6 继续获取字符串描述符
主机发送:
80 06 00 03 00 00 FF 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x03, // wValue[8:15] Desc Type: (String)
0x00, 0x00, // wIndex Language ID: 0x00
0xFF, 0x00, // wLength = 255
// 8 bytes
// best guess: USB Standard Request
从机返回:
04 03 09 04
//下条命令获取字符串描述符的时候,设置参数wIndex Language ID: 0x0409
2.7 再次获取字符串描述符
主机发送:
80 06 02 03 09 04 FF 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x02, // wValue[0:7] Desc Index: 2
0x03, // wValue[8:15] Desc Type: (String)
0x09, 0x04, // wIndex Language ID: 0x0409
0xFF, 0x00, // wLength = 255
// 8 bytes
// best guess: USB Standard Request
从机返回:
3A 03 53 00 54 00 4D 00 33 00 32 00 20 00 43 00 75 00 73 00 74 00 6F 00 6D 00 20 00 48 00 75 00 6D 00 61 00 6E 00 20 00 69 00 6E 00 74 00 65 00 72 00 66 00 61 00 63 00 65 00
//返回字符串:.S.T.M.3.2. .C.u.s.t.o.m. .H.u.m.a.n. .i.n.t.e.r.f.a.c.e.
2.8 获取Device Qualifier描述符
主机发送:
80 06 00 06 00 00 0A 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x06, // wValue[8:15] Desc Type: (Device Qualifier)
0x00, 0x00, // wIndex Language ID: 0x00
0x0A, 0x00, // wLength = 10
// 8 bytes
// best guess: USB Standard Request
从机返回:
从机返回0数据,STALL状态。
2.9 再次获取设备描述符
主机发送:
80 06 00 01 00 00 12 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x01, // wValue[8:15] Desc Type: (Device)
0x00, 0x00, // wIndex Language ID: 0x00
0x12, 0x00, // wLength = 18
// 8 bytes
// best guess: USB Standard Request
从机返回:
12 01 00 02 00 00 00 40 83 04 50 57 00 02 01 02 03 01
//
0x12, // bLength
0x01, // bDescriptorType (Device)
0x00, 0x02, // bcdUSB 2.00
0x00, // bDeviceClass (Use class information in the Interface Descriptors)
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x40, // bMaxPacketSize0 64
0x83, 0x04, // idVendor 0x0483
0x50, 0x57, // idProduct 0x5750
0x00, 0x02, // bcdDevice 4.00
0x01, // iManufacturer (String Index)
0x02, // iProduct (String Index)
0x03, // iSerialNumber (String Index)
0x01, // bNumConfigurations 1
// 18 bytes
// best guess: USB Standard Descriptor
2.10 获取Configuration Descriptor
主机发送:
80 06 00 02 00 00 09 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x02, // wValue[8:15] Desc Type: (Configuration)
0x00, 0x00, // wIndex Language ID: 0x00
0x09, 0x00, // wLength = 9
// 8 bytes
// best guess: USB Standard Request
从机返回:
09 02 29 00 01 01 00 C0 32
//返回配置描述符的长度为41个字节
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x29, 0x00, // wTotalLength 41
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0xC0, // bmAttributes Self Powered
0x32, // bMaxPower 100mA
// 9 bytes
// best guess: USB Standard Descriptor
2.11 获取具体HID配置描述符信息
主机发送:
80 06 00 02 00 00 29 00
//获取长度为41个字节的配置描述符
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x02, // wValue[8:15] Desc Type: (Configuration)
0x00, 0x00, // wIndex Language ID: 0x00
0x29, 0x00, // wLength = 41
// 8 bytes
// best guess: USB Standard Request
从机返回:
09 02 29 00 01 01 00 C0 32 09 04 00 00 02 03 00 00 00 09 21 11 01 00 01 22 02 00 07 05 81 03 02 00 05 07 05 01 03 02 00 05
//返回具体描述符信息
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x29, 0x00, // wTotalLength 41
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0xC0, // bmAttributes Self Powered
0x32, // bMaxPower 100mA
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x00, // bInterfaceNumber 0
0x00, // bAlternateSetting
0x02, // bNumEndpoints 2
0x03, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0x00, // iInterface (String Index)
0x09, // bLength
0x21, // bDescriptorType (HID)
0x11, 0x01, // bcdHID 1.11
0x00, // bCountryCode
0x01, // bNumDescriptors
0x22, // bDescriptorType[0] (HID)
0x02, 0x00, // wDescriptorLength[0] 2
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x81, // bEndpointAddress (IN/D2H)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x01, // bEndpointAddress (OUT/H2D)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
// 41 bytes
// best guess: USB Standard Descriptor
2.12 获取设备描述符
主机发送:
80 06 00 01 00 00 12 00
//获取设备描述符
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x01, // wValue[8:15] Desc Type: (Device)
0x00, 0x00, // wIndex Language ID: 0x00
0x12, 0x00, // wLength = 18
// 8 bytes
// best guess: USB Standard Request
从机返回:
12 01 00 02 00 00 00 40 83 04 50 57 00 02 01 02 03 01
//返回具体设备描述符信息
0x12, // bLength
0x01, // bDescriptorType (Device)
0x00, 0x02, // bcdUSB 2.00
0x00, // bDeviceClass (Use class information in the Interface Descriptors)
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x40, // bMaxPacketSize0 64
0x83, 0x04, // idVendor 0x0483
0x50, 0x57, // idProduct 0x5750
0x00, 0x02, // bcdDevice 4.00
0x01, // iManufacturer (String Index)
0x02, // iProduct (String Index)
0x03, // iSerialNumber (String Index)
0x01, // bNumConfigurations 1
// 18 bytes
// best guess: USB Standard Descriptor
2.13 获取Configuration Descriptor
主机发送:
80 06 00 02 00 00 09 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x02, // wValue[8:15] Desc Type: (Configuration)
0x00, 0x00, // wIndex Language ID: 0x00
0x09, 0x00, // wLength = 9
// 8 bytes
// best guess: USB Standard Request
从机返回:
09 02 29 00 01 01 00 C0 32
//返回配置描述符信息,长度为41个字节
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x29, 0x00, // wTotalLength 41
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0xC0, // bmAttributes Self Powered
0x32, // bMaxPower 100mA
// 9 bytes
// best guess: USB Standard Descriptor
2.14 获取Configuration Descriptor
主机发送:
80 06 00 02 00 00 29 00
//获取Configuration Descriptor
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x02, // wValue[8:15] Desc Type: (Configuration)
0x00, 0x00, // wIndex Language ID: 0x00
0x29, 0x00, // wLength = 41
// 8 bytes
// best guess: USB Standard Request
从机返回:
09 02 29 00 01 01 00 C0 32 09 04 00 00 02 03 00 00 00 09 21 11 01 00 01 22 02 00 07 05 81 03 02 00 05 07 05 01 03 02 00 05
//返回HID的配置描述符信息
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x29, 0x00, // wTotalLength 41
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0xC0, // bmAttributes Self Powered
0x32, // bMaxPower 100mA
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x00, // bInterfaceNumber 0
0x00, // bAlternateSetting
0x02, // bNumEndpoints 2
0x03, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0x00, // iInterface (String Index)
0x09, // bLength
0x21, // bDescriptorType (HID)
0x11, 0x01, // bcdHID 1.11
0x00, // bCountryCode
0x01, // bNumDescriptors
0x22, // bDescriptorType[0] (HID)
0x02, 0x00, // wDescriptorLength[0] 2
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x81, // bEndpointAddress (IN/D2H)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x01, // bEndpointAddress (OUT/H2D)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
// 41 bytes
// best guess: USB Standard Descriptor
2.15 获取Device Descriptor
主机发送:
80 06 00 01 00 00 12 00
//Get Device Descriptor
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x01, // wValue[8:15] Desc Type: (Device)
0x00, 0x00, // wIndex Language ID: 0x00
0x12, 0x00, // wLength = 18
// 8 bytes
// best guess: USB Standard Request
从机返回:
12 01 00 02 00 00 00 40 83 04 50 57 00 02 01 02 03 01
//返回设备描述符信息
0x12, // bLength
0x01, // bDescriptorType (Device)
0x00, 0x02, // bcdUSB 2.00
0x00, // bDeviceClass (Use class information in the Interface Descriptors)
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x40, // bMaxPacketSize0 64
0x83, 0x04, // idVendor 0x0483
0x50, 0x57, // idProduct 0x5750
0x00, 0x02, // bcdDevice 4.00
0x01, // iManufacturer (String Index)
0x02, // iProduct (String Index)
0x03, // iSerialNumber (String Index)
0x01, // bNumConfigurations 1
// 18 bytes
// best guess: USB Standard Descriptor
2.16 获取Configuration Descriptor
主机发送:
80 06 00 02 00 00 09 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x02, // wValue[8:15] Desc Type: (Configuration)
0x00, 0x00, // wIndex Language ID: 0x00
0x09, 0x00, // wLength = 9
// 8 bytes
// best guess: USB Standard Request
从机返回:
09 02 29 00 01 01 00 C0 32
//
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x29, 0x00, // wTotalLength 41
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0xC0, // bmAttributes Self Powered
0x32, // bMaxPower 100mA
// 9 bytes
// best guess: USB Standard Descriptor
2.17 获取具体配置描述符信息
主机发送:
80 06 00 02 00 00 29 00
//
0x80, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Device
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x02, // wValue[8:15] Desc Type: (Configuration)
0x00, 0x00, // wIndex Language ID: 0x00
0x29, 0x00, // wLength = 41
// 8 bytes
// best guess: USB Standard Request
从机返回:
09 02 29 00 01 01 00 C0 32 09 04 00 00 02 03 00 00 00 09 21 11 01 00 01 22 02 00 07 05 81 03 02 00 05 07 05 01 03 02 00 05
//
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x29, 0x00, // wTotalLength 41
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0xC0, // bmAttributes Self Powered
0x32, // bMaxPower 100mA
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x00, // bInterfaceNumber 0
0x00, // bAlternateSetting
0x02, // bNumEndpoints 2
0x03, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0x00, // iInterface (String Index)
0x09, // bLength
0x21, // bDescriptorType (HID)
0x11, 0x01, // bcdHID 1.11
0x00, // bCountryCode
0x01, // bNumDescriptors
0x22, // bDescriptorType[0] (HID)
0x02, 0x00, // wDescriptorLength[0] 2
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x81, // bEndpointAddress (IN/D2H)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x01, // bEndpointAddress (OUT/H2D)
0x03, // bmAttributes (Interrupt)
0x02, 0x00, // wMaxPacketSize 2
0x05, // bInterval 5 (unit depends on device speed)
// 41 bytes
// best guess: USB Standard Descriptor
2.18 设置配置set config
主机发送:
00 09 01 00 00 00 00 00
//
0x00, // bmRequestType: Dir: H2D, Type: Standard, Recipient: Device
0x09, // bRequest (Set Config)
0x01, 0x00, // wValue Config Num: 1
0x00, 0x00, // wIndex = 0x00
0x00, 0x00, // wLength = 0
// 8 bytes
// best guess: USB Standard Request
从机返回:
从机只返回ack,不返回具体的数据。
2.19
主机发送:
21 0A 00 00 00 00 00 00
//
0x21, // bmRequestType: Dir: H2D, Type: Class, Recipient: Interface
0x0A, // bRequest
0x00, 0x00, // wValue[0:15] = 0x00
0x00, 0x00, // wIndex = 0x00
0x00, 0x00, // wLength = 0
// 8 bytes
// best guess: USB Standard Request
从机返回:
从机只返回ack,不返回具体的数据。
2.20
主机发送:
81 06 00 22 00 00 42 00
//
0x81, // bmRequestType: Dir: D2H, Type: Standard, Recipient: Interface
0x06, // bRequest (Get Descriptor)
0x00, // wValue[0:7] Desc Index: 0
0x22, // wValue[8:15] Desc Type: (HID Report)
0x00, 0x00, // wIndex Language ID: 0x00
0x42, 0x00, // wLength = 66
// 8 bytes
// best guess: USB Standard Request
从机返回:
00 C0
//
0x00, // Unknown (bTag: 0x00, bType: 0x00)
0xC0, // End Collection
// 2 bytes
// best guess: USB HID Report Descriptor
三,总结
本文主要介绍HID设备枚举的过程协议的解析,供参考。