USB笔记 设备描述符(Device Descriptor)
设备描述符(Device Descriptor)说明了USB设备的通用信息,包含应用到全部设备和所有设备配置的信息。USB设备只有一个设备描述符。设备描述符是在设备连接时主机读取的第一个描述字。此描述字所含的信息,被主机用来取得设备的额外内容。主机通过发送Get Descriptor 请求,并使设置事务中wValue 字段的高字节等于01h,来取得一个设备描述字。设备描述字提供了关于设备、设备的配置以及任何设备所归属的类的信息。
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 |
bLength以字节为单位的描述符大小。
bDescriptorType设备描述符类型,为DEVICE (0x01)。
bcdUSB是设备所符合的USB规范的版本号,该值采用BCD(用二进制编码的十进制数)格式。如果要将这个数值转化为十进制数的话,,前面的1字节(8位)代表整数部分,接下来的4位代表十分位,最后4位代表百分位。USB1.1是0x0110(不是0x0101);USB 2.0是0x0200;USB 2.1是0x0210;USB 3.0是0x0300;USB 3.1是0x0310。
bcdUSB 等于0x0210或者更高的设备,必须支持BOS描述符。Wireless USB V1.0的设备或设备导线适配器应当把bcdUSB 设置为0x0250。
bDeviceClass为那些在设备层次定义功能的设备规定了它们所属的类。从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可规定一个类的子类。子类可增加对由类中的一组功能所共享的额外特性和功能的支持。如果bDeviceClass是0x00,则bDeviceSubClass也必须为0x00。如果bDeviceClass在0x01到0xFE范围内,则bDeviceSubClass等于0x00或等于为设备的类所定义的代码。标准类中,制造商定义的子类使用0xFF 。
bDeviceProtocol可为所选的类和子类规定一个协议。例如,USB2.0集线器就使用此字段来表明是否当前集线器支持高速模式,如果支持,则进一步表明集线器是支持一个还是多个事务转换器(transaction translators)。如果bDeviceClass 在0x01到0xFE的范围内,协议就等于0x00或由设备类所定义的代码。
bMaxPacketSize0规定了端点0的最大包大小。主机会在对设备描述符进行请求之后跟着的那个请求中使用这一信息。对于USB2.0,最大包大小等于此字段的值,且对于低速必须是8;对于全速可为8、16、32或64;对于高速为64。对于超高速状态,最大信息包尺寸等于2^bMaxPacketSize0,且bMaxPacketSize0必须等于9以规定一个为512的最大包大小。
idVendor 是厂商ID,它由USB-IF分配给其成员和其他缴纳了管理费的用户。主机可能会含有INF文件,其中就含有此值;且如果有,Windows 操作系统可使用此值来为设备选择驱动程序。除了内部使用(由用户负责防止冲突)的设备外,每个设备描述符必须在这个字段上拥有一个有效的厂商ID。
idProduct是表明厂商设备的产品ID。厂商ID的拥有者指定了产品ID。设备描述符和主机上设备的NF文件都可能含有此值,如果有,Windows 操作系统可能会使用这个值来帮助为设备选择驱动程序。对于厂商ID来说,每个产品ID都是唯一的,因此多个制造商可使用同一产品ID却不会引起冲突。
bcdDevice是BCD格式的设备版本号。厂商指定这个值。主机可能会使用这个值来为设备选择驱动程序。
iManufacturer是一个指向描述制造商的字符串的索引。如果没有制造商描述符,此值即为0。
iProduct 是指向一个描述产品的字符串的索引。如果没有字符串描述符,此值为0。
iSeriaINumber是指向含有设备***的字符串的索引。如果没有***,此值为0。若用户在总线上拥有多个相同的设备,且即使在重启之后主机仍需要将它们区分开,这种情况下***十分有用。***还可使主机能够确定设备是与之前所使用的相同,还是一个新安装的有相同制造商ID和产品ID的设备。带有同样制造商ID、产品ID和设备版本号的设备,就不能再共享***了。使用批量专属协议的大容量存储设备必须含有***。
bNumConfigurations是当前运行速度下设备所支持的配置数目。
示例
下面举例说明一个获取设备描述符的过程。
获取一个键盘的设备描述符,该键盘的设备描述符如下所示:
使用USB分析仪抓包可以看到,设备第一个获取的描述符就是设备描述符:
设备描述符的获取过程如下:
再展开看到详细的过程:
接下来详细看整个获取过程的各个字段含义:
一开始的setup包过程:
setup data:
IN包过程:
OUT包过程:
完成整个设备描述符Device Descriptor的获取。
[参考资料]
Universal Serial Bus Specification Revision 2.0
Universal Serial Bus 3.0 Specification
Universal Serial Bus 3.1 Specification
USB Complete, 5th Edition
USB开发大全
USB2.0与OTG规范及开发指南