USB 描述符①
- 设备描述符(Device Descriptors)
- 配置描述符(Configuration Descriptors)
- 接口描述符(Interface Descriptors)
- 端点描述符(Endpoint Descriptors)
- 字符串描述符(String Descriptors)
USB 描述符的构成
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number |
Size of Descriptor in Bytes |
1 | bDescriptionType | 1 | Constant |
DescriptorType |
2 | ... | n |
Start of parameters for descriptor |
/* All standard descriptors have these 2 fields at the beginning */ struct usb_descriptor_header { __u8 bLength; __u8 bDescriptorType; } __attribute__ ((packed));
设备描述符
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number |
Size of the Descriptor in Bytes (18 bytes) |
1 | bDescriptorType | 1 | Constant |
Device Descriptor (0x01) |
2 | bcdUSB | 2 | BCD |
USB Specification Number which device complies too. |
4 | bDeviceClass | 1 | Class |
Class Code (Assigned by USB Org) If equal to Zero, each interface specifies it’s own class code If equal to 0xFF, the class code is vendor specified. Otherwise field is valid Class Code. |
5 | bDeviceSubClass | 1 | SubClass |
Subclass Code (Assigned by USB Org) |
6 | bDeviceProtocol | 1 | Protocol |
Protocol Code (Assigned by USB Org) |
7 | bMaxPacketSize | 1 | Number |
Maximum Packet Size for Zero Endpoint. Valid Sizes are 8, 16, 32, 64 |
8 | idVendor | 2 | ID |
Vendor ID (Assigned by USB Org) |
10 | idProduct | 2 | ID |
Product ID (Assigned by Manufacturer) |
12 | bcdDevice | 2 | BCD |
Device Release Number |
14 | iManufacturer | 1 | Index |
Index of Manufacturer String Descriptor |
15 | iProduct | 1 | Index |
Index of Product String Descriptor |
16 | iSerialNumber | 1 | Index |
Index of Serial Number String Descriptor |
17 | bNumConfigurations | 1 | Integer |
Number of Possible Configurations |
- bcdUSB 域:指明设备支持的 USB 的最高版本。该值采用二进制编码的十进制格式,格式为 0xJJMN,其中 JJ 是主要版本号(major),M 是次要版本号(minor),N是次子版本号(sub minor)。举例来说,USB 2.0 写作 0x0200,USB 1.1 写作 0x0110,USB 1.0 写作 0x0100
- bDeviceClass/bDeviceSubClass/bDeviceProtocol 域:它们被操作系统使用,用于为你的设备加载对应的驱动程序。通常,仅在设备级别设置 bDeviceClass,大多数类规范(class specification)选择在接口级别(interface level)标识自身,因此将 bDeviceClass 设置为 0x00,这样一台设备即可支持多个类别(注:意味着一个设备需要有多个驱动程序支持)。
- bMaxPacketSize 域:指明端点 0 的最大数据包大小。所有设备必须支持零端 0。
- idVendor/idProduct 域:被操作系统用来为你的设备加载对应的驱动程序。idVendor 由 USB 联盟组织分配。
- bcdDevice 域:与 bcdUSB 具有相同的格式,用于提供设备版本号。此值由开发人员分配。
- bNumConfigurations 域:定义设备以其当前速度支持的可能的配置数。
/* USB_DT_DEVICE: Device descriptor */ struct usb_device_descriptor { __u8 bLength; __u8 bDescriptorType; __le16 bcdUSB; __u8 bDeviceClass; __u8 bDeviceSubClass; __u8 bDeviceProtocol; __u8 bMaxPacketSize0; __le16 idVendor; __le16 idProduct; __le16 bcdDevice; __u8 iManufacturer; __u8 iProduct; __u8 iSerialNumber; __u8 bNumConfigurations; } __attribute__ ((packed)); #define USB_DT_DEVICE_SIZE 18
配置描述符
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number |
Size of Descriptor in Bytes |
1 | bDescriptorType | 1 | Constant |
Configuration Descriptor (0x02) |
2 | wTotalLength | 2 | Number |
Total length in bytes of data returned |
4 | bNumInterfaces | 1 | Number |
Number of Interfaces |
5 | bConfigurationValue | 1 | Number |
Value to use as an argument to select this configuration |
6 | iConfiguration | 1 | Index |
Index of String Descriptor describing this configuration |
7 | bmAttributes | 1 | Bitmap |
D7 Reserved, set to 1. (USB 1.0 Bus Powered) D6 Self Powered D5 Remote Wakeup D4..0 Reserved, set to 0. |
8 | bMaxPower | 1 | mA |
Maximum Power Consumption in 2mA units |
- wTotalLength 域:读取配置描述符时,它将返回整个配置层次结构,其中包括所有相关的接口和端点描述符。wTotalLength 代表层次结构中的字节数。示例如下:
- bNumInterfaces 域:指定此配置的接口数。
- bConfigurationValue 域:SetConfiguration 请求通过该域值选中此配置。
- iConfiguration 域:iConfiguration 是字符串描述符的索引,该字符串描述符以人类可读的形式描述了配置。
- bmAttributes 域:指定此配置的电源参数。如果设备是自供电的,则会设置 D6。USB 1.0 中使用位 D7 表示总线供电的设备,但是现在由 bMaxPower 完成。如果设备使用总线上的任何电源,无论是总线供电的设备还是自供电设备,都必须在 bMaxPower 中报告其功耗。设备还可以支持远程唤醒,远程唤醒允许设备在主机处于挂起状态时唤醒主机。
- bMaxPower 域:定义设备将从总线消耗的最大功率,以 2mA 为单位,因此可以指定最大约 500mA。该规范允许高功率总线供电的设备从 Vbus 汲取不超过 500mA 的电流。如果设备失去了外部电源,则其消耗的电量不得超过 bMaxPower 中指定的。如果没有外部电源,它将无法执行任何操作。
/* USB_DT_CONFIG: Configuration descriptor information. * * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the * descriptor type is different. Highspeed-capable devices can look * different depending on what speed they're currently running. Only * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG * descriptors. */ struct usb_config_descriptor { __u8 bLength; __u8 bDescriptorType; __le16 wTotalLength; __u8 bNumInterfaces; __u8 bConfigurationValue; __u8 iConfiguration; __u8 bmAttributes; __u8 bMaxPower; } __attribute__ ((packed)); #define USB_DT_CONFIG_SIZE 9
接口描述符
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number |
Size of Descriptor in Bytes (9 Bytes) |
1 | bDescriptorType | 1 | Constant |
Interface Descriptor (0x04) |
2 | bInterfaceNumber | 1 | Number |
Number of Interface |
3 | bAlternateSetting | 1 | Number |
Value used to select alternative setting |
4 | bNumEndpoints | 1 | Number |
Number of Endpoints used for this interface |
5 | bInterfaceClass | 1 | Class |
Class Code (Assigned by USB Org) |
6 | bInterfaceSubClass | 1 | SubClass |
Subclass Code (Assigned by USB Org) |
7 | bInterfaceProtocol | 1 | Protocol |
Protocol Code (Assigned by USB Org) |
8 | iInterface | 1 | Index |
Index of String Descriptor Describing this interface |
- bInterfaceNumber 域:表示接口描述符的索引(index)。 它应该从零开始,并为每个新接口描述符增加一次。
- bAlternativeSetting 域:用来指定备用接口(alternative interfaces),备用接口可由 SetInterface 请求选中。
- bNumEndpoints 域:表示接口使用的端点数量,该值不包括端点 0,并用于指示要遵循的端点描述符的数量。
- bInterfaceClass/bInterfaceSubClass/bInterfaceProtocol 域:用于指定支持的设备类别(例如 HID 设备,通信设备,大容量存储设备等),这使许多设备可以使用类驱动程序,从而无需为设备编写特定的驱动程序。
- iInterface 域:描述该接口的字符串描述符的索引。
/* USB_DT_INTERFACE: Interface descriptor */ struct usb_interface_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bInterfaceNumber; __u8 bAlternateSetting; __u8 bNumEndpoints; __u8 bInterfaceClass; __u8 bInterfaceSubClass; __u8 bInterfaceProtocol; __u8 iInterface; } __attribute__ ((packed)); #define USB_DT_INTERFACE_SIZE 9
端点描述符
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number |
Size of Descriptor in Bytes (7 bytes) |
1 | bDescriptorType | 1 | Constant |
Endpoint Descriptor (0x05) |
2 | bEndpointAddress | 1 | Endpoint | Endpoint Address Bits 0..3b Endpoint Number. Bits 4..6b Reserved. Set to Zero Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) |
3 | bmAttributes | 1 | Bitmap | Bits 0..1 Transfer Type
Bits 2..7 are reserved. If Isochronous endpoint, Bits 3..2 = Synchronisation Type (Iso Mode)
Bits 5..4 = Usage Type (Iso Mode)
|
4 | wMaxPacketSize | 2 | Number |
Maximum Packet Size this endpoint is capable of sending or receiving |
6 | bInterval | 1 | Number |
Interval for polling endpoint data transfers. Value in frame counts. Ignored for Bulk & Control Endpoints. Isochronous must equal 1 and field may range from 1 to 255 for interrupt endpoints. |
- bEndpointAddress 域:描述端点的基本信息,如端点地址,端点数据方向。
- bmAttributes 域:指定传输类型,可以是控制,中断,同步或批量传输。如果指定了是同步传输端点,则可以指定其他熟悉,如 Synchronisation 类型,Usage 类型。
- wMaxPacketSize 域:指定此端点的最大有效负载大小。
- bInterval 域:用于指定某些传输的轮询间隔,单位以帧表示(The units are expressed in frames),因此对于低速/全速设备而言等于 1ms,对于高速设备而言等于 125us。
/* USB_DT_ENDPOINT: Endpoint descriptor */ struct usb_endpoint_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bEndpointAddress; __u8 bmAttributes; __le16 wMaxPacketSize; __u8 bInterval; /* NOTE: these two are _only_ in audio endpoints. */ /* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */ __u8 bRefresh; __u8 bSynchAddress; } __attribute__ ((packed)); #define USB_DT_ENDPOINT_SIZE 7 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
字符串描述符
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number |
Size of Descriptor in Bytes |
1 | bDescriptorType | 1 | Constant |
String Descriptor (0x03) |
2 | wLANGID[0] | 2 | number |
Supported Language Code Zero |
4 | wLANGID[1] | 2 | number |
Supported Language Code One |
n | wLANGID[x] | 2 | number |
Supported Language Code x |
Offset | Field | Size | Value | Description |
0 | bLength | 1 | Number |
Size of Descriptor in Bytes |
1 | bDescriptorType | 1 | Constant |
String Descriptor (0x03) |
2 | bString | n | Unicode |
Unicode Encoded String |