USB descriptor [reprinted]

Source: Internet
Author: User

USBDescriptor

The USB descriptor information is stored in the USB device. During the enumeration process, the USB Host sends a getdescriptor request to the USB device. After receiving the request, the USB descriptor information is returned to the USB host. The data returned by the USB host is analyzed to determine which USB device is used and a data link channel is established. What kind of data is the USB descriptor? The USB protocol has a detailed description.

 

General USB descriptors include device descriptors, configuration descriptors, interface descriptors, and Endpoint descriptors. Different USB devices also include other types of descriptors, such, USB mouse and keyboard also include hid Descriptor and report descriptor, and may include string descriptor. the descriptor type in USB protocol is defined as follows:

 

All descriptor information is obtained by sending the getdescriptor request, but the USB device does not know which descriptor you want to obtain, therefore, you also need to specify the descriptor type and length in the getdescriptor request so that the USB device can correctly return the descriptor information.

 

 

1 device descriptor

The device descriptor struct in Linux is defined as follows:

 

216/* usb_dt_device: device descriptor */
217 struct usb_device_descriptor {
218 _ u8 blength;
219 _ u8 bdescriptortype;
220
221 _ le16 bcdusb;
222 _ u8 bdeviceclass;
223 _ u8 bdevicesubclass;
224 _ u8 bdeviceprotocol;
225 _ u8 bmaxpacketsize0;
226 _ le16 idvendor;
227 _ le16 idproduct;
228 _ le16 bcddevice;
229 _ u8 imanufacturer;
230 _ u8 iproduct;
231 _ u8 iserialnumber;
232 _ u8 bnumconfigurations;
233} _ attribute _ (packed ));
234
235 # define usb_dt_device_size 18

 

Blength: the length of the descriptor. The device descriptor is 18 bytes in length.

Bdescriptortype: the descriptor type. The device descriptor type is 0x01.

Bcdusb: The Protocol version number that the USB device complies with. For example, the 2.0 protocol is 0x0200.

Bdeviceclass: USB device class code, assigned by the USB-IF. If this field is 0x00, it indicates it is specified by the interface Descriptor (it is possible that the USB device is a composite device, interfaces of USB devices are independent of each other and belong to different device classes respectively ). If it is 0x01 ~ 0xfe represents the device class defined for the USB-IF, for example, 0x03 is a HID device and 0x09 is a hub device. If it is 0xff, the device type is customized by the vendor.

Bdevicesubclass: USB subclass code, assigned by the USB-IF. If bdeviceclass is 0x00, this field must also be 0x00. For other cases, see USB definition of USB device class.

Bdeviceprotocol: protocol code assigned by the USB-IF. This field must also be 0x00 if bdeviceclass and bdevicesubclass are defined as 0x00.

Bmaxpacketsize0: Maximum packet length of endpoint 0, which must be 8, 16, 32, or 64.

Idvendor: vendor ID, which is assigned by the USB-IF and needs to be applied to the USB-IF organization.

Idproduct: The product ID, which is specified by the manufacturer.

Bcddevice: Device serial number, which is set by the vendor.

Imanufacturer: used to describe the vendor's string descriptor index.

Iproduct: String descriptor index used to describe the product.

Iserialnumber: String descriptor index used to describe the product serial number. Note that all string descriptors are optional. If there is no string descriptor, specify these indexes as 0x00.

Bnumconfigurations: configure the number of descriptors.

For example, my USB mouse device descriptor information is as follows:

 

It can be seen that the USB device complies with the USB protocol. The USB device type is not specified in the device description. The maximum data packet length of endpoint 0 is 8 bytes, that is to say, the full device descriptor cannot be obtained at one time. It takes three times to obtain the complete device descriptor. Let's look at the offset of bmaxpacketsize0 in the device descriptor, Which is exactly 8th bytes. That is to say, once transmitted, we can know how long the endpoint 0 can transmit at a time. Based on this value, we can determine whether to continue transmission, to obtain complete descriptor information. The idvendor of the USB device is 0x80ee, idproduct is 0x0021, and there is a configuration and so on.

 

 

2. Configuration Descriptor

A USB device has only one USB device descriptor and can have multiple configuration descriptors. The configuration descriptor is defined as follows:

 

265/* usb_dt_config: configuration descriptor information.
266 *
267 * usb_dt_other_speed_config is the same descriptor, doesn't that
268 * descriptor type is different. highspeed-capable devices can look
269 * different depending on what speed They're currently running. Only
270 * devices with a usb_dt_device_qualifier have any other_speed_config
271 * descriptors.
272 */
273 struct usb_config_descriptor {
274 _ u8 blength;
275 _ u8 bdescriptortype;
276
277 _ le16 wtotallength;
278 _ u8 bnuminterfaces;
279 _ u8 bconfigurationvalue;
280 _ u8 iconfiguration;
281 _ u8 bmattributes;
282 _ u8 bmaxpower;
283} _ attribute _ (packed ));
284
285 # define usb_dt_config_size 9

 

Blength: configure the descriptor length, which is 9 bytes in length.

Bdescriptortype: descriptor type. The configuration descriptor type is 0x02.

Wtotallength: configure the total size of the descriptor information, including the interface Descriptor and Endpoint descriptor.

Bnuminterfaces: Number of USB interfaces.

Bconfigurationvalue: the configuration index value specified when the setconfiguration and getconfiguration requests are used.

Iconfiguration: Describes the configured string descriptor index.

Bmattributes: Power Supply configuration. The bit is defined as follows:

D7 reserved, must be set to 1

D6 self-powered Mode

D5 remote Wakeup

D4 ~ D0 Reserved

Bmaxpower: maximum power consumption, in the unit of 2mA. For example, 0x32 is 50*2 = 100mA.

My USB mouse configuration descriptor information is as follows:

 

It can be seen that the total configuration information of the device is 0x22 = 34 bytes, there is an interface, the maximum power consumption is 100mA and other information.

 

 

3. Interface Descriptor

One configuration can have one or more interfaces. One interface has zero or multiple endpoints. The interface Descriptor and the endpoint descriptor cannot be returned directly through the getdescriptor request. They must be returned together with the configuration descriptor, the interface descriptor in Linux is defined as follows:

 

309/* usb_dt_interface: interface descriptor */
310 struct usb_interface_descriptor {
311 _ u8 blength;
312 _ u8 bdescriptortype;
313
314 _ u8 binterfacenumber;
315 _ u8 balternatesetting;
316 _ u8 bnumendpoints;
317 _ u8 binterfaceclass;
318 _ u8 binterfacesubclass;
319 _ u8 binterfaceprotocol;
320 _ u8 iinterface;
321} _ attribute _ (packed ));
322
323 # define usb_dt_interface_size 9

 

Blength: the descriptor length. The interface descriptor length is 9 bytes.

Bdescriptortype: Type of the descriptor. the type of the interface descriptor is 0x04.

Binterfacenumber: indicates the interface number, which is allocated from 0. This field is used to differentiate different interfaces when a configuration has multiple interfaces.

Balternatesetting:

Bnumendpoints: Number of endpoints, excluding endpoint 0.

Binterfaceclass

Binterfacesubclass

Binterfaceprotocol: similar to bdeviceclass, bdevicesubclass, and bdeviceprotocol in the device descriptor.

Iinterface: String descriptor index that describes this interface.

The interface descriptor instance is as follows:

 

The first nine bytes are the configuration descriptor, from which we can see that the number of endpoints is 1, binterfaceclass is 0x03, that is, the HID device.

 

 

4. endpoint Descriptor

Note that endpoint 0 does not have an endpoint descriptor. The structure of the endpoint descriptor in Linux is defined as follows:

 

327/* usb_dt_endpoint: endpoint descriptor */
328 struct usb_endpoint_descriptor {
329 _ u8 blength;
330 _ u8 bdescriptortype;
331
332 _ u8 bendpointaddress;
333 _ u8 bmattributes;
334 _ le16 wmaxpacketsize;
335 _ u8 binterval;
336
337/* Note: These two are _ only _ in audio endpoints .*/
338/* use usb_dt_endpoint * _ size in blength, not sizeof .*/
339 _ u8 brefresh;
340 _ u8 bsynchaddress;
341} _ attribute _ (packed ));
342
343 # define usb_dt_endpoint_size 7
344 # define usb_dt_endpoint_audio_size 9/* audio extension */

 

Blength: descriptor length. If two values are the end points of the audio device, the length of the end point descriptor is 9 bytes. For other device endpoints, the length of the end point descriptor is 7 bytes.

Bdescriptortype: descriptor type. The endpoint descriptor type is 0x05.

Bendpointaddress: the endpoint address. The detailed definition is as follows:

D7 endpoint direction

0 out endpoint

1 In endpoint

D6 ~ D4 Reserved

D3 ~ D0 endpoint number

Bmattributes: the endpoint type, which is defined as follows:

D5 ~ D4 usage

00 data endpoint

01 feedback endpoint

10 implicit feedback on data endpoints

11 Reserved

D3 ~ D2 synchronization type

00 non-synchronous

01 asynchronous

10 adaptive

11 Synchronization

D1 ~ D0 transmission type

00 Control Transmission

01 Synchronous Transmission

10 pieces of transmission

11 interrupted transmission

If this endpoint is not a synchronization endpoint, D5 ~ D2 is retained and must be set to 0.

Wmaxpacketsize: the maximum data packet length supported by the endpoint. The detailed definition is as follows:

D10 ~ D0 Maximum packet length

D12 ~ D11

The remaining bits are retained and must be set to 0.

Binterval: the access interval of the endpoint data transmission. For interrupt endpoints at full speed/low speed, the value range is 1 ~ 255. for high-speed interrupt endpoints, the value range is 1 ~ 16. For detailed definitions, refer to the USB protocol.

The endpoint descriptor instance is as follows:

 

Because the interface and Endpoint descriptor cannot be obtained separately, I have obtained all the device configuration information. The endpoint descriptor starts from 0x07 and 0x07 is the length of the endpoint descriptor, this endpoint is an in endpoint with the endpoint address 1. This endpoint is an interrupt endpoint. The maximum packet length is 6 bytes and the interval is 10 ms.

 

 

5. Hid Descriptor

It can be seen that in addition to configuration descriptors, interface descriptors, and Endpoint descriptors, there is also a descriptor, which is a hid descriptor, which is dedicated to HID devices. The structure of the hid descriptor in Linux is defined as follows:

 

537 struct hid_class_descriptor {
538 _ u8 bdescriptortype;
539 _ le16 wdescriptorlength;
540} _ attribute _ (packed ));
541
542 struct hid_descriptor {
543 _ u8 blength;
544 _ u8 bdescriptortype;
545 _ le16 bcdhid;
546 _ u8 bcountrycode;
547 _ u8 bnumdescriptors;
548
549 struct hid_class_descriptor DESC [1];
550} _ attribute _ (packed ));

 

Blength: the length of the descriptor.

Bdescriptortype: the type of the descriptor. the type of the hid descriptor is 0x21.

Bcdhid: The hid Protocol version to be followed.

Bcountrycode: country code.

Bnumdescriptors: number of sub-descriptors. Generally, at least one report descriptor is required.

Bdescriptortype: Type of the sub-descriptor, for example, report descriptor.

Wdescriptorlength: the length of the sub-descriptor.

From the above USB mouse configuration descriptor, we can see that the hid Protocol version followed by the mouse is 1.1, there is a hidclass descriptor, the descriptor type is 0x22, that is, the report descriptor, the descriptor length is 0x46.

Address: http://blog.csdn.net/mcgrady_tracy/article/details/8129992

USB descriptor [reprinted]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.