USB笔记 设备描述符(Device Descriptor)
设备描述符(Device Descriptor)说明了USB设备的通用信息,包含应用到全部设备和所有设备配置的信息。USB设备只有一个设备描述符(Device Descriptor)。设备描述符是在设备连接时主机读取的第一个描述符。设备描述符所含的信息,被主机用来取得设备的额外内容。设备描述符(Device Descriptor)提供了关于设备、设备的配置以及任何设备所归属的类的信息。
Device Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number | 以字节为单位的描述符大小 |
1 | bDescriptorType | 1 | Constant | 设备描述符类型 |
2 | bcdUSB | 2 | BCD | USB规范版本号 |
4 | bDeviceClass | 1 | Class | 类码 |
5 | bDeviceSubClass | 1 | SubClass | 子类码 |
6 | bDeviceProtocol | 1 | Protocol | 协议码 |
7 | bMaxPacketSize0 | 1 | Number | 端点0的最大包大小 |
8 | idVendor | 2 | ID | 厂商ID |
10 | idProduct | 2 | ID | 产品ID |
12 | bcdDevice | 2 | BCD | 设备版本号 |
14 | iManufacturer | 1 | Index | 制造商字符串描述符索引 |
15 | iProduct | 1 | Index | 产品的字符串描述符索引 |
16 | iSerialNumber | 1 | Index | 设备序列号的字符串描述符索引 |
17 | bNumConfigurations | 1 | Number | 可能的配置数目 |
bLength以字节为单位的描述符大小(0x12)。
bDescriptorType设备描述符类型,为DEVICE (0x01)。
bcdUSB以BCD码表示的USB规范发布版本编号。这个字段确定了设备及其描述符所遵循的USB规范发布版本。
该字段的值采用BCD(用二进制编码的十进制数)格式。bcdUSB字段的值为0xJJMN (JJ–主版本号(major version number),M–次版本号(minor version number),N–子次版本号(sub-minor version number)),例如,版本2.1.3用值0213H表示,版本3.0用值0300H表示。 如果要将这个数值转化为十进制数的话,前面的1字节(8位)代表整数部分,接下来的4位代表十分位,最后4位代表百分位。
USB 1.1: 0x0110
USB 2.0: 0x0200
USB 2.1: 0x0210
USB 2.5: 0x0250
USB 3.0: 0x0300
USB 3.1: 0x0310
USB 3.2: 0x0320
支持BOS descriptor的设备的bcdUSB值必须是0201H或更大的值。
符合Wireless USB specification的设备的bcdUSB值必须是0250H。
bDeviceClass类码(Class code)。这个字段为那些在设备层次定义功能的设备规定了它们所属的类。从0x01到0xFE的值是留给那些被USB规范定义的类。厂商定义的类使用0xFF。大多数设备在接口描述符中规定了它们的类。对于这些设备,若其功能不使用接口关联描述符(interface association descriptor),则设备描述符中的bDeviceClass 将等于0x00;若其功能使用接口关联描述符(interface association descriptor),bDeviceClass 就等于0xEF。
bDeviceClass | Description |
0x00 | 接口描述符(interface descriptor)规定了类和功能不使用接口联合描述符(interface association descriptor) |
0x02 | 通信设备(可以改为在接口层次声明) |
0x09 | 集线器Hub bDeviceSubclass = 0x00 bDeviceProtocol = 0x00: Full speed bDeviceProtocol = 0x01: High speed with single Transaction Translator bDeviceProtocol = 0x02: High speed with multiple Transaction Translators bDeviceProtocol = 0x03: SuperSpeed/SuperSpeedPlus |
0x0F | 个人健康设备Personal healthcare device(首选在接口层次声明) |
0xDC | 诊断设备Diagnostic device(可以改为在接口层次声明) bDeviceSubclass = 0x01 bDeviceProtocol = 0x01: USB2 Compliance Device |
0xE0 | 无线控制器Wireless Controller (只限蓝牙(Bluetooth)。 所有其他协议必须在接口层次声明。) bDeviceSubclass = 0x01 bDeviceProtocol = 0x01: Bluetooth programming interface (也应该在接口层次声明) bDeviceSubclass = 0x04: Bluetooth AMP controller (也应该在接口层次声明) |
0xEF | 各种各样的(Miscellaneous) bDeviceSubclass = 0x01 bDeviceProtocol = 0x01: active sync bDeviceProtocol = 0x02: Palm sync bDeviceSubclass = 0x02 bDeviceProtocol = 0x01: interface association descriptor bDeviceProtocol = 0x01: wire adapter multifunction peripheral (Wireless USB) |
0xFF | 厂商专属Vendor-specific (可以改为在接口层次声明) |
bDeviceSubClass 子类码(Subclass code)。这个字段可规定一个类的子类。子类可增加对由类中的一组功能所共享的额外特性和功能的支持。如果bDeviceClass是0x00,则bDeviceSubClass也必须为0x00。如果bDeviceClass在0x01到0xFE范围内,则bDeviceSubClass等于0x00或等于为设备的类所定义的代码。标准类(standard classes)中,制造商定义的子类使用0xFF 。
bDeviceProtocol 协议码(Protocol code)。这个字段可为所选的类和子类规定一个协议。例如,USB2.0集线器就使用此字段来表明是否当前集线器支持高速模式,如果支持,则进一步表明集线器是支持一个还是多个事务转换器(transaction translators)。如果bDeviceClass 在0x01到0xFE的范围内,协议就等于0x00或由设备类所定义的代码。
bMaxPacketSize0 端点0的最大包大小。主机会在对设备描述符(Device Descriptor)进行请求之后跟着的那个请求中使用这一信息。
对于USB2.0/USB1.x,最大包大小等于此字段的值。
对于低速(low speed)必须是8。
对于全速(full speed)只能是8、16、32或64。
对于高速(high speed)只能是64。对于增强型超高速状态(Enhanced SuperSpeed),bMaxPacketSize0 的值是作为的指数,端点0的最大包大小等于。bMaxPacketSize0必须等于9,端点0的最大包大小要为512。
idVendor 厂商ID。这个字段的值由USB-IF分配给其成员和其他缴纳了管理费的用户。主机可能会含有INF文件,其中就含有此值;且如果有,Windows 操作系统可使用此值来为设备选择驱动程序。除了内部使用(由用户负责防止冲突)的设备外,每个设备描述符必须在这个字段上拥有一个有效的厂商ID。
idProduct 产品ID。这个字段是表明厂商设备的产品ID。厂商ID的拥有者指定了产品ID。设备描述符和主机上设备的NF文件都可能含有此值,如果有,Windows 操作系统可能会使用这个值来帮助为设备选择驱动程序。对于厂商ID来说,每个产品ID都是唯一的,因此多个制造商可使用同一产品ID却不会引起冲突。
bcdDevice BCD格式表示的设备版本号。厂商指定这个值。主机可能会使用这个值来为设备选择驱动程序。
iManufacturer指向描述制造商的字符串描述符的索引。如果没有制造商描述符,此值即为0。
iProduct 指向一个描述产品的字符串描述符的索引。如果没有字符串描述符,此值为0。
iSeriaINumber指向含有设备序列号的字符串描述符的索引。如果没有序列号,此值为0。若用户在总线上拥有多个相同的设备,且即使在重启之后主机仍需要将它们区分开,这种情况下序列号十分有用。序列号还可使主机能够确定设备是与之前所使用的相同,还是一个新安装的有相同制造商ID和产品ID的设备。带有同样制造商ID、产品ID和设备版本号的设备,就不能再共享序列号了。使用批量专属协议的大容量存储设备必须含有序列号。
bNumConfigurations是当前运行速度下设备所支持的配置数目。其他运行速度下的配置数目不包含在其中。
获取设备描述符
主机通过发送Get Descriptor
请求,来取得一个设备描述符(Device Descriptor)。
Get Descriptor | Value |
bmRequestType | 0x80 |
bmRequestType.Recipient | 0b00000(Device) |
bmRequestType.Type | 0b00(Standard) |
bmRequestType.Direction | 0b1(Device-to-Host) |
bRequest | 0x06(Get Descriptor) |
wValue | 0x0100(Device#0) |
wIndex | 0x0000 |
wLength | 0x??? |
下面举例说明一个获取设备描述符的过程。
获取一个键盘的设备描述符,该键盘的设备描述符如下所示:
使用USB分析仪抓包可以看到,设备第一个获取的描述符就是设备描述符:
设备描述符的获取过程如下:
再展开看到详细的过程:
接下来详细看整个获取过程的各个字段含义:
一开始的setup包过程:
setup data:
IN包过程:
OUT包过程:
完成整个设备描述符Device Descriptor的获取。
描述符示例
这里举例一个带有该描述符的示例设备,这是一个键盘Keyboard的完整描述符信息。
Device Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x12 | |
1 | bDescriptorType | 1 | 0x01 | DEVICE |
2 | bcdUSB | 2 | 0x0110 | 1.10 |
4 | bDeviceClass | 1 | 0x00 | |
5 | bDeviceSubClass | 1 | 0x00 | |
6 | bDeviceProtocol | 1 | 0x00 | |
7 | bMaxPacketSize0 | 1 | 0x08 | |
8 | idVendor | 2 | 0x046d | |
10 | idProduct | 2 | 0xc31c | |
12 | bcdDevice | 2 | 0x6400 | 64.00 |
14 | iManufacturer | 1 | 0x01 | |
15 | iProduct | 1 | 0x02 | |
16 | iSerialNumber | 1 | 0x00 | |
17 | bNumConfigurations | 1 | 0x01 |
Configuration Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x09 | |
1 | bDescriptorType | 1 | 0x02 | CONFIGURATION |
2 | wTotalLength | 2 | 0x003b | |
4 | bNumInterfaces | 1 | 0x02 | |
5 | bConfigurationValue | 1 | 0x01 | |
6 | iConfiguration | 1 | 0x03 | |
7 | bmAttributes | 1 | 0xa0 | |
8 | bMaxPower | 1 | 0x2d |
Interface Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x09 | |
1 | bDescriptorType | 1 | 0x04 | INTERFACE |
2 | bInterfaceNumber | 1 | 0x00 | |
3 | bAlternateSetting | 1 | 0x00 | |
4 | bNumEndpoints | 1 | 0x01 | |
5 | bInterfaceClass | 1 | 0x03 | Human Interface Device |
6 | bInterfaceSubClass | 1 | 0x01 | Boot Interface |
7 | bInterfaceProtocol | 1 | 0x01 | Keyboard |
8 | iInterface | 1 | 0x02 |
HID Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x09 | |
1 | bDescriptorType | 1 | 0x21 | HID |
2 | bcdHID | 2 | 0x0110 | 1.10 |
4 | bCountryCode | 1 | 0x00 | |
5 | bNumDescriptors | 1 | 0x01 | |
6 | bDescriptorType | 1 | 0x22 | REPORT |
7 | wDescriptorLength | 2 | 0x0041 |
Endpoint Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x07 | |
1 | bDescriptorType | 1 | 0x05 | ENDPOINT |
2 | bEndpointAddress | 1 | 0x81 | 1 IN |
3 | bmAttributes | 1 | 0x03 | Interrupt |
4 | wMaxPacketSize | 2 | 0x0008 | |
6 | bInterval | 1 | 0x0a |
Interface Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x09 | |
1 | bDescriptorType | 1 | 0x04 | INTERFACE |
2 | bInterfaceNumber | 1 | 0x01 | |
3 | bAlternateSetting | 1 | 0x00 | |
4 | bNumEndpoints | 1 | 0x01 | |
5 | bInterfaceClass | 1 | 0x03 | Human Interface Device |
6 | bInterfaceSubClass | 1 | 0x00 | |
7 | bInterfaceProtocol | 1 | 0x00 | |
8 | iInterface | 1 | 0x02 |
HID Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x09 | |
1 | bDescriptorType | 1 | 0x21 | HID |
2 | bcdHID | 2 | 0x0110 | 1.10 |
4 | bCountryCode | 1 | 0x00 | |
5 | bNumDescriptors | 1 | 0x01 | |
6 | bDescriptorType | 1 | 0x22 | REPORT |
7 | wDescriptorLength | 2 | 0x009f |
Endpoint Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | 0x07 | |
1 | bDescriptorType | 1 | 0x05 | ENDPOINT |
2 | bEndpointAddress | 1 | 0x82 | 2 IN |
3 | bmAttributes | 1 | 0x03 | Interrupt |
4 | wMaxPacketSize | 2 | 0x0004 | |
6 | bInterval | 1 | 0xff |
附录
USB1.x/USB 2.0 Device Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number | Size of this descriptor in bytes |
1 | bDescriptorType | 1 | Constant | DEVICE Descriptor Type |
2 | bcdUSB | 2 | BCD | USB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). This field identifies the release of the USB Specification with which the device and its descriptors are compliant. |
4 | bDeviceClass | 1 | Class | Class code (assigned by the USB-IF). If this field is reset to zero, each interface within a configuration specifies its own class information and the various interfaces operate independently. If this field is set to a value between 1 and FEH, the device supports different class specifications on different interfaces and the interfaces may not operate independently. This value identifies the class definition used for the aggregate interfaces. If this field is set to FFH, the device class is vendor-specific. |
5 | bDeviceSubClass | 1 | SubClass | Subclass code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass field. If the bDeviceClass field is reset to zero, this field must also be reset to zero. If the bDeviceClass field is not set to FFH, all values are reserved for assignment by the USB-IF. |
6 | bDeviceProtocol | 1 | Protocol | Protocol code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass and the bDeviceSubClass fields. If a device supports class-specific protocols on a device basis as opposed to an interface basis, this code identifies the protocols that the device uses as defined by the specification of the device class. If this field is reset to zero, the device does not use class-specific protocols on a device basis. However, it may use class-specific protocols on an interface basis. If this field is set to FFH, the device uses a vendor-specific protocol on a device basis. |
7 | bMaxPacketSize0 | 1 | Number | Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid) |
8 | idVendor | 2 | ID | Vendor ID (assigned by the USB-IF) |
10 | idProduct | 2 | ID | Product ID (assigned by the manufacturer) |
12 | bcdDevice | 2 | BCD | Device release number in binary-coded decimal |
14 | iManufacturer | 1 | Index | Index of string descriptor describing manufacturer |
15 | iProduct | 1 | Index | Index of string descriptor describing product |
16 | iSerialNumber | 1 | Index | Index of string descriptor describing the device’s serial number |
17 | bNumConfigurations | 1 | Number | Number of possible configurations |
USB 3.x Device Descriptor
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number | Size of this descriptor in bytes |
1 | bDescriptorType | 1 | Constant | DEVICE Descriptor Type |
2 | bcdUSB | 2 | BCD | USB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). This field identifies the release of the USB Specification with which the device and its descriptors are compliant. |
4 | bDeviceClass | 1 | Class | Class code (assigned by the USB-IF). If this field is reset to zero, each interface within a configuration specifies its own class information and the various interfaces operate independently. If this field is set to a value between 1 and FEH, the device supports different class specifications on different interfaces and the interfaces may not operate independently. This value identifies the class definition used for the aggregate interfaces. If this field is set to FFH, the device class is vendor-specific. |
5 | bDeviceSubClass | 1 | SubClass | Subclass code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass field. If the bDeviceClass field is reset to zero, this field must also be reset to zero. If the bDeviceClass field is not set to FFH, all values are reserved for assignment by the USB-IF. |
6 | bDeviceProtocol | 1 | Protocol | Protocol code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass and the bDeviceSubClass fields. If a device supports class-specific protocols on a device basis as opposed to an interface basis, this code identifies the protocols that the device uses as defined by the specification of the device class. If this field is reset to zero, the device does not use class-specific protocols on a device basis. However, it may use class-specific protocols on an interface basis. If this field is set to FFH, the device uses a vendor-specific protocol on a device basis. |
7 | bMaxPacketSize0 | 1 | Number | Maximum packet size for endpoint zero. The bMaxPacketSize0 value is used as the exponent for a value; e.g., a bMaxPacketSize0 of 4 means a Max Packet size of 16 ( → 16). 09H is the only valid value in this field when operating at Gen X speed. |
8 | idVendor | 2 | ID | Vendor ID (assigned by the USB-IF) |
10 | idProduct | 2 | ID | Product ID (assigned by the manufacturer) |
12 | bcdDevice | 2 | BCD | Device release number in binary-coded decimal |
14 | iManufacturer | 1 | Index | Index of string descriptor describing manufacturer |
15 | iProduct | 1 | Index | Index of string descriptor describing product |
16 | iSerialNumber | 1 | Index | Index of string descriptor describing the device’s serial number |
17 | bNumConfigurations | 1 | Number | Number of possible configurations |
[参考资料]
Universal Serial Bus Specification Revision 1.0
Universal Serial Bus Specification Revision 1.1
Universal Serial Bus Specification Revision 2.0
Universal Serial Bus 3.0 Specification
Universal Serial Bus 3.1 Specification
Universal Serial Bus 3.2 Specification
Wireless Universal Serial Bus Specification Revision 1.0
Wireless Universal Serial Bus Specification Revision 1.1
USB 2.0 ECN: Link Power Management (LPM)
USB Complete, 5th Edition
USB开发大全
USB2.0与OTG规范及开发指南