USB笔记 配置描述符(Configuration Descriptor)
配置描述符(Configuration Descriptor)说明了一个特定配置的相关信息。取得设备描述符(Device Descriptor)后,主机就可以继续去获取设备的配置、接口和端点描述符。当主机请求配置描述符(Configuration Descriptor)时,返回的是所有相关的接口和端点描述符。
一个USB设备有一个或多个配置描述符(Configuration Descriptor)。配置描述符描述了配置所提供的接口数量。每个接口可以独立操作。每种配置有一个或多个接口,而且每个接口有零个或多个端点。在一个配置中,接口不会共享一个端点,除非端点被相同接口的备用设置使用。没有这一限制、属于不同配置的接口可以共享端点。
配置描述符(Configuration Descriptor)规定了设备的特征和能力。一般单个配置已经足够了,但在驱动程序的支持下,带有多应用或多电源选择的设备可支持多重配置。且每次只有一个配置被激活。每个配置需要一个配置描述符,其中含有关于设备电源使用及所支持接口数的信息。每个配置描述符都有附属描述符(subordinate descriptor),包括一个或多个接口描述符(Interface Descriptor)以及可选的端点描述符(Endpoint Descriptor)。
Configuration Descriptor
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | 以字节为单位的描述符大小 |
1 | bDescriptorType | 1 | Constant | 配置描述符类型 |
2 | wTotalLength | 2 | Number | 配置返回的数据总长度 |
4 | bNumInterfaces | 1 | Number | 配置支持的接口数量 |
5 | bConfigurationValue | 1 | Number | Get Configuration 和Set Configuration请求的配置值 |
6 | iConfiguration | 1 | Index | 字符串描述符索引 |
7 | bmAttributes | 1 | Bitmap | 配置特性 |
8 | bMaxPower | 1 | mA | 设备从总线获取的最大功耗 |
bLength以字节为单位的描述符大小(0x09)。
bDescriptorType配置描述符类型,为CONFIGURATION (0x02)。
wTotalLength配置返回的数据总长度。包括该配置返回的所有描述符(配置、接口、端点、和专用的类型或者专用的厂商描述符)的总长度。
bNumInterfaces这个配置支持的接口数量,最小值为0x01。
bConfigurationValue确认Get Configuration 和Set Configuration请求的配置,且必须为0x01或者更高值。取值为0的Set Configuration请求会使设备进入未配置状态(Not Configured state)。
iConfiguration描述这个配置的字符串描述符索引。若没有字符串描述符,这个字段的值为0。
bmAttributes配置特性。
Bit 7:USB1.0协议中表示总线供电(Bus Powered),设置Bit 7 = 1表示由总线供电(Bus Powered)。其他协议该位保留(Reserved),必须设置为1。
Bit 6:自供电(Self-powered)。如果Bits 6 = 1,设备是自供电(Self-powered)的。
Bit 5 :远程唤醒(Remote Wakeup)。如果Bit 5 = 1,设备支持远程唤醒。
Bits 4…0:未使用,保留,必须为0。
bMaxPower当设备完全运行时,特定配置的USB设备从总线取得的最大功耗。
对于usb2.0,bMaxPower 以2mA为单位。如果设备要求200ma,则bMaxPower = 100 (0x64)。设备可请求的最大总线电流500mA。
对于增强型超高速(Enhanced SuperSpeed)设备,bMaxPower以8mA为单位。如果设备要求200ma,则bMaxPower = 25(0x19)。设备可请求的最大总线电流900mA。
当设备和主机支持USB Power Delivery Rev. 2.0,主机可以从PD Class Specific Descriptors检索设备的电源需求。
获取配置描述符
主机通过发送Get 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 | 0x0200(Configuration #0) |
wIndex | 0x0000 |
wLength | 0x???(wTotalLength) |
下面举例说明一个获取配置描述符Configuration Descriptor的过程。使用USB分析仪抓包,软件Total Phase Data Center。
配置描述符Configuration Descriptor如下所示:
使用USB分析仪抓包可以看到,获取配置描述符Configuration Descriptor:
配置描述符Configuration Descriptor的获取过程如下:
再展开看到详细的过程:
接下来详细看整个获取过程的各个字段含义:
一开始的setup包过程:
setup data:
IN包过程
OUT包过程:
完成整个配置描述符Configuration 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.1/USB 2.0 Configuration Descriptor
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | Size of this descriptor in bytes |
1 | bDescriptorType | 1 | Constant | CONFIGURATION Descriptor Type |
2 | wTotalLength | 2 | Number | Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface,endpoint, and class- or vendor-specific)returned for this configuration. |
4 | bNumInterfaces | 1 | Number | Number of interfaces supported by this configuration |
5 | bConfigurationValue | 1 | Number | Value to use as an argument to the SetConfiguration() request to select this configuration |
6 | iConfiguration | 1 | Index | Index of string descriptor describing this configuration |
7 | bmAttributes | 1 | Bitmap | Configuration characteristics D7: Reserved (set to one) D6: Self-powered D5: Remote Wakeup D4…0: Reserved (reset to zero) D7 is reserved and must be set to one for historical reasons. A device configuration that uses power from the bus and a local source reports a non-zero value in bMaxPower to indicate the amount of bus power required and sets D6. The actual power source at runtime may be determined using the GetStatus(DEVICE) request (see Section 9.4.5). If a device configuration supports remote wakeup, D5 is set to one. |
8 | bMaxPower | 1 | mA | Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA). Note: A device configuration reports whether the configuration is bus-powered or self-powered. Device status reports whether the device is currently self-powered. If a device is disconnected from its external power source, it updates device status to indicate that it is no longer self-powered. A device may not increase its power draw from the bus, when it loses its external power source, beyond the amount reported by its configuration. If a device can continue to operate when disconnected from its external power source, it continues to do so. If the device cannot continue to operate, it fails operations it can no longer support. The USB System Software may determine the cause of the failure by checking the status and noting the loss of the device’s power source. |
USB 3.x Configuration Descriptor
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | Size of this descriptor in bytes |
1 | bDescriptorType | 1 | Constant | CONFIGURATION Descriptor Type |
2 | wTotalLength | 2 | Number | Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface,endpoint, and class- or vendor-specific)returned for this configuration. |
4 | bNumInterfaces | 1 | Number | Number of interfaces supported by this configuration |
5 | bConfigurationValue | 1 | Number | Value to use as an argument to the SetConfiguration() request to select this configuration |
6 | iConfiguration | 1 | Index | Index of string descriptor describing this configuration |
7 | bmAttributes | 1 | Bitmap | Configuration characteristics D7: Reserved (set to one) D6: Self-powered D5: Remote Wakeup D4…0: Reserved (reset to zero) D7 is reserved and must be set to one for historical reasons. A device configuration that uses power from the bus and a local source reports a non-zero value in bMaxPower to indicate the amount of bus power required and sets D6. The actual power source at runtime may be determined using the GetStatus(DEVICE) request (see Section 9.4.5). If a device configuration supports remote wakeup, D5 is set to one. |
8 | bMaxPower | 1 | mA | Maximum power consumption of the device from the bus in this specific configuration when the device is fully operational. Expressed in 2-mA units when the device is operating in high-speed mode and in 8-mA units when operating at Gen X speed.(i.e., 50 = 100 mA when operating at high-speed and 50 = 400 mA when operating at Gen X speed). Note: A device configuration reports whether the configuration is bus-powered or self-powered. Device status reports whether the device is currently self-powered. If a device is disconnected from its external power source, it updates device status to indicate that it is no longer self-powered. A device may not increase its power draw from the bus, when it loses its external power source, beyond the amount reported by its configuration. If a device can continue to operate when disconnected from its external power source, it continues to do so. If the device cannot continue to operate, it shall return to the Powered state. |
USB1.0 Configuration Descriptor
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | Size of this descriptor in bytes |
1 | bDescriptorType | 1 | Constant | CONFIGURATION |
2 | wTotalLength | 2 | Number | Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface, endpoint, and class or vendor specific) returned for this configuration. |
4 | bNumInterfaces | 1 | Number | Number of interfaces supported by this configuration |
5 | bConfigurationValue | 1 | Number | Value to use as an argument to Set Configuration to select this configuration |
6 | iConfiguration | 1 | Index | Index of string descriptor describing this configuration |
7 | bmAttributes | 1 | Bitmap | Configuration characteristics D7 Bus Powered D6 Self Powered D5 Remote Wakeup D4…0 Reserved (reset to 0) A device configuration that uses power from the bus and a local source sets both D7 and D6. The actual power source at runtime may be determined using the Get Status device request. If a device configuration supports remote wakeup, D5 is set to 1. |
8 | bMaxPower | 1 | mA | Maximum power consumption of USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA). Note: A device configuration reports whether the configuration is bus-powered or self-powered. Device status reports whether the device is currently self-powered. If a device is disconnected from its external power source, it updates device status to indicate that it is no longer self-powered. A device may not increase its power draw from the bus, when it loses its external power source, beyond the amount reported by its configuration. If a device can continue to operate when disconnected from its external power source, it continues to do so. If the device cannot continue to operate, it fails operations it can no longer support. Host software may determine the cause of the failure by checking the status and noting the loss of the device’s power source. |
[参考资料]
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
Universal Serial Bus Power Delivery Specification Revision 2.0, V1.2
USB Complete, 5th Edition
USB开发大全
USB2.0与OTG规范及开发指南
https://blog.csdn.net/u012028275/article/details/109280001