I. Application scenarios
USB hid is a relatively large type. Hid devices are human-computer interaction devices. It is used to control computer operations, such as USB mouse, USB keyboard, USB game joystick, USB touchpad, USB track ball, dialing device, VCR remote control, and so on. In addition, An advantage of using the HID device is that the operating system comes with a hid driver, and you do not need to develop a very troublesome driver, as long as you directly call the API to complete communication. Therefore, many simple USB devices like to enumerate them into hid devices so that they can be directly used without installing drivers.
Ii. Communication channels available for USB hid
All hid devices communicate with the host through a USB control pipe (default pipe, that is, endpoint 0) and interrupt the pipe.
Table 1 available HID device endpoints defined in USB hid Specification
MPs queue requirements
Control (endpoint 0) must transmit the USB descriptor, request code, and message data for query.
The interrupted input must transmit the input data from the device to the host.
Interrupt output (optional) transmits output data from the host to the device.
The control pipeline is mainly used in the following three aspects:
Receive/respond to USB Host Control requests and related class data
Transmit data when querying a usb host (for example, responding to a get_report request)
Receive data from a USB Host
The interrupted pipeline is mainly used in the following two aspects:
The USB Host receives asynchronous data transmission from the USB device.
The USB Host sends real-time data to the USB device.
The interrupted output data transmission from the USB host to the USB device is optional. When the interrupted output data transmission is not supported, the USB Host transmits the data to the USB device through the control pipe.
3. descriptors related to USB hid Devices
Except for five standard USB Descriptors (device descriptor, configuration descriptor, interface descriptor, endpoint descriptor, and string descriptor), see another article in Lily electronics studio: USB development basics-USB commands (requests) and USB descriptors) include three HID device class specific descriptors: HID descriptor, report descriptor, and entity descriptor.
In addition to the interpretation of the HID device consisting of three specific descriptors, the five standard descriptors are related to the HID device:
The value of bdeviceclass, bdevicesubclass, and bdeviceprotocol in the device descriptor must be zero.
The binterfaceclass value in the interface descriptor must be 0x03, And the binterfacesubclass value must be 0 or 1. If it is 1, The HID device identifier is a boot device, generally, it makes sense for a PC. It means that the BIOS can identify and use your HID device when it is started, and only standard mouse or keyboard devices can become boot devices. If the value is 0, your HID device can be identified and used only after the operating system is started.) The value of binterfaceprotocol is shown in the following table:
Table 2. Definitions of binterfaceprotocol in the hid interface Descriptor
Meaning of binterfaceprotocol in decimal format
0 none
1 keyboard
2 mouse
3 ~ 255 Reserved
The following describes the specific descriptors of these three hid devices.
1. Hid Descriptor
The hid descriptor is associated with the interface descriptor. Therefore, if a device has only one interface descriptor, each device has only one hid descriptor regardless of its endpoints. The HID device descriptor mainly describes the version number of the hid specification, the additional descriptor used by the hid communication, and the length of the report descriptor. Table 2 shows the structure of the hid descriptor:
Table 3 structure of USB hid class Descriptor
Description of offset domain size value
0 blength 1 number the length of this descriptor (in bytes)
1 bdescriptortype 1 constant descriptor type (here 0x21 is the hid class descriptor)
2 bcdhid 2 digital hid standard version (BCD code), which is encoded in 4 hexadecimal BCD formats. For example, the BCD code of version 1.0 is 0x0100, the BCD code of Version 1.1 is 0x0110.
4 bcountrycode 1 national identification code for digital hardware (BCD Code) (see table 3)
5 bnumdescritors 1 Number of affiliated descriptors supported
6 bdescriptortype 1 the type of the constant hid correlation descriptor. The values are shown in table 5.
7 wdescriptorlength 2 total length of the digital report Descriptor
9 bdescriptortype 1 constant is used to identify a constant of the descriptor type, used by a device with more than one Descriptor
10 wdescriptorlength 2: the total length of a digital descriptor, used by a device with more than one Descriptor
Table 4. National ID codes for HID hardware
Country and region identification code (decimal) country and region
00 does not support 18 Netherlands/Dutch
01 Arabic 19 Norwegian
02 Belgian 20 Persian (Farsi)
03 Canadian-bilingual 21 Poland
04 Canadian-French 22 Portuguese
05 Czech Republic 23 Russia
06 Danish 24 Slovakia
07 Finnish 25 Spanish
08 French 26 Swedish
09 German 27 Swiss/French
10 Greek 28 Swiss/German
11 Hebrew 29 Switzerland
12 Hungary 30 Taiwan
13 International (ISO) 31 Turkish-Q
14 Italian 32 UK
15 Japan (katakana) 33 us
16 Korean 34 Yugoslavia
17 Latin American 35 Turkish-F
36 ~ 255 Reserved
Table 5. Definitions of HID descriptor types
Descriptor type value hid-related descriptor type
0x21 hid Descriptor
0x22 report Descriptor
0x23 object Descriptor
2. Report Descriptor
The report descriptors of HID devices are complex and difficult to understand.
The syntax of the report descriptor is different from that of the USB standard descriptor. It is arranged as an item (items) without a certain length. The report descriptor of HID does not simply describe a value that corresponds to a fixed meaning. It can combine many situations, in addition, the hid driver on the PC needs to provide a parser interpreter to re-interpret the device situation described, and then combine to generate a unique data stream format of the hid hardware device, so I think it is more appropriate to understand it as a "report descriptor scripting language. We use the dedicated scripting language "report descriptor" to allow users to define what data their hid devices have and what the meaning of each bit of the data is.
For more information about the report descriptor, see the USB hid protocol. The USB Association provides a hid descriptor editing tool called the hid descriptor tool, which allows us to easily generate our report descriptor.
3. Object Descriptor
Object descriptors are used to describe the behavior characteristics of devices. The object descriptor is an optional descriptor. The HID device can choose whether to include the object descriptor based on its device characteristics. Table 5 shows the structure of the hid entity descriptor.
Table 6. Structure of the hid object Descriptor
Description of offset Domain Size
0 bdesignator 1 is used to specify which part of the Impact Project of the Ontology (see table 6)
1 bflags 1-Bit Flag
Bit 0 ~ 4: effort
5-digit ~ 7: qualifier (meaning See table 7)
Table 7 bdesignator Value Meaning table
Bdesignator Value Meaning
0x00 no 0x15 finger
0x01 hands 0x16 Heads
0x02 eyeball 0x17 shoulder
0x03 eyebrow 0x18 waist bone
0x04 eyelid 0x19 waist
0x05 ears 0x1a thigh
0x06 nose 0x1b knee
0x07 mouth 0x1c calf
0x08 top lip 0x1d foot
0x09 lower lip 0x1e foot
0x0a jaw 0x1f heel
0x0b neck 0x20 thumb
0x0c upper arm 0x21 thumb up
0x0d elbow 0x22 second finger
0x0e forearm 0x23 third finger
0x0f wrist 0x24 fourth finger
0x10 palm 0x25 small thumb
0x11 thumb 0x26 eyebrow
0x12 index finger 0x27 face
0x13: 0x28 ~ 0xff Reserved
0x14 ring finger
Table 8. Meaning of qualifier values
Qualifier Value Meaning qualifier Value Meaning
0x00 none 0x04 one of them
0x01 right 0x05 middle
0x02 left 0x06 Reserved
0x03 both 0x07 retain
Iv. USB hid commands (requests)
There are 6 specific commands (requests) for the HID device class, including get_report, get_idle, get_protocol, set_report, set_idle, and set_protocol.
The format of HID request (command) data packets is shown in Table 8:
Table 9. Hid request (command) package format
Description of offset Domain Size
0 bmrequesttype 1
The request features of HID devices are as follows:
Bits 7:
0 = from USB host to USB device
1 = from USB device to USB Host
6-digit ~ 5:
01 = request type: device type request
4-digit ~ 0:
0001 = the request object is an interface)
Therefore, only 10100001 and 00100001 valid for HID device requests
1 brequest 1 hid requests (see table 9)
2 wvalue 2 high bytes indicate the type of the descriptor (see table 5). When the low byte is not 0, it is used to select the object descriptor.
4 Windex 2-byte value, which has different meanings based on different brequest
6 wlength 2 the length of the request's Data Segment
Table 10. Hid requests
Value hid request descriptor Annotation
0x01 get_report
0x02 get_idle
0x03 get_protocol is only applicable to HID devices that support startup)
0x09 set_report
0x0a set_idle
0x0b set_protocol is only applicable to HID devices that support startup)
When a USB host requests the configuration descriptor of a hid device, the device first returns the following descriptors: configuration descriptor, interface descriptor, hid descriptor, and Endpoint descriptor. The hid descriptor contains the type and length of its affiliated description (such as the report descriptor), and then the host requests the relevant descriptor based on the information of the hid descriptor.