In USB, USB Host identifies a device through various descriptors, including device descriptors, configuration descriptors, interface descriptors, endpoint descriptors, string descriptors, and report descriptors. The USB report descriptor is a descriptor in the HID device. It is a complicated descriptor.
USB hid devices transmit data through reports, including input reports and output reports. The input report is sent by a USB device to the host. For example, the USB mouse moves the mouse and clicks the mouse to the computer, and the keyboard returns the key data to the computer; the output report is sent by the host to a USB device, for example, a digital keyboard lock lamp or an uppercase letter lock lamp on the keyboard. A report is a data packet that contains the data to be transmitted. The input report is input by the interrupt input endpoint, but the output report is a little different. When there is no interrupt output point, you can send it by controlling the output endpoint 0. When there is an interrupt output point, it is sent by the interrupt output endpoint.
The report descriptor describes a report and what data is used. The USB host can analyze the meaning of the data in the report. It controls the input endpoint 0 and the host uses the report descriptor command to obtain the report descriptor. Pay attention to this request.
Sent to the interface, not to the device. A report descriptor can describe multiple reports. Different reports are identified by report IDs. The report IDs are at the beginning of the report, that is, the first byte. If no report ID is specified in the report descriptor, there is no ID field in the report, and the start is data. For more details, see the USB hid protocol, which can be downloaded from http: // www.usb.org.
The following uses the USB mouse and USB keyboard generated by the hid descriptor tool to describe the report Descriptor and report.
Code char keyboardreportdescriptor [63] = {
// Indicates that the usage page is a universal desktop device.
0x05, 0x01, // usage_page (generic desktop)
// Indicates that the keyboard is used.
0x09, 0x06, // usage (keyboard)
// Indicates the application set, which must end with end_collection. For details, see end_collection.
0xa1, 0x01, // collection (Application)
// Indicates that the usage page is a button
0x05, 0x07, // usage_page (keyboard)
// Minimum usage value. The value is the left ctrl key.
0x19, 0xe0, // usage_minimum (Keyboard leftcontrol)
// Maximum usage value. The right GUI key is used here, that is, the window key.
0x29, 0xe7, // usage_maximum (keyboard right GUI)
// The logical minimum value is 0.
0x15, 0x00, // logical_minimum (0)
// The logical maximum value is 1.
0x25, 0x01, // logical_maximum (1)
// The report size (that is, the width of this field) is 1 bit. Therefore, the minimum logical value is 0 and the maximum logical value is 1.
0x75, 0x01, // report_size (1)
// The number of reports is 8, that is, there are a total of 8 bits
0x95, 0x08, // report_count (8)
// Input, variable, value, and absolute value. General report absolute values such as keyboards,
// When the mouse moves, the relative value of the report indicates how much the mouse moves.
0x81, 0x02, // input (data, VAR, ABS)
// The above items describe a field used for input, which is a total of 8 bits, each bit represents a key
// From the left ctrl key to the right GUI key. The eight bits exactly constitute a byte, which is located in the first byte of the report.
// Its leading bit, that is, bit-0 corresponds to the left ctrl key. If the returned data is 1, the left ctrl key is pressed,
// Otherwise, the left ctrl key is not pressed. The highest bit, that is, bit-7, indicates the press condition of the right GUI key. Several digits in the middle,
// It must be determined based on the purpose page table (hid usage tables) specified in the hid protocol. It is usually used to indicate
// Special keys, such as Ctrl, shift, and del
// The number of such data segments is 1
0x95, 0x01, // report_count (1)
// The length of each segment is 8 bits
0x75, 0x08, // report_size (8)
// Input, constant, value, and absolute value
0x81, 0x03, // input (cnst, VAR, ABS)
// The above eight bits are constants and the device must return 0
// The number of such data segments is 5
0x95, 0x05, // report_count (5)
// The size of each segment is 1 bit
0x75, 0x01, // report_size (1)
// The usage is led, which is used to control the LEDs on the keyboard. Therefore, the following shows that it is used for output.
0x05, 0x08, // usage_page (LEDs)
// The minimum usage value is num lock, that is, the number key lock lamp.
0x19, 0x01, // usage_minimum (num lock)
// The maximum usage value is Kana. I don't know what the lamp is. ^_^
0x29, 0x05, // usage_maximum (Kana)
// As mentioned above, this field is used for output to control the LED. Variable, value, and absolute value.
// 1 indicates that the light is on, and 0 indicates that the light is off
0x91, 0x02, // output (data, VAR, ABS)
// The number of such data segments is 1
0x95, 0x01, // report_count (1)
// The size of each segment is 3 bits
0x75, 0x03, // report_size (3)
// Used for output, constant, value, absolute
0x91, 0x03, // output (cnst, VAR, ABS)
// Because it needs to be aligned by byte, only 5 bits are used for the led control,
// Therefore, three unnecessary bits need to be appended and set as constants.
// The number of reports is 6
0x95, 0x06, // report_count (6)
// The size of each segment is 8 bits
0x75, 0x08, // report_size (8)
// The logical minimum value is 0.
0x15, 0x00, // logical_minimum (0)
// The maximum logical value is 255.
0x25, 0xff, // logical_maximum (255)
// Use the button
0x05, 0x07, // usage_page (keyboard)
// The minimum value is 0.
0x19, 0x00, // usage_minimum (Reserved (no event indicated ))
// The maximum value is 0x65.
0x29, 0x65, // usage_maximum (Keyboard Application)
// Input, variable, array, and absolute value
0x81, 0x00, // input (data, ary, ABS)
// The preceding six 8-bit arrays are defined. Each 8-Bit Array (that is, one byte) is used to represent a key.
// There are 6 buttons to press. If no buttons are pressed, all return 0. If too many keys are pressed, the keyboard scans the system.
// If keys cannot be distinguished, 0x01 is returned, that is, 6 0x01 keys are returned. If a key is pressed, the first of the six bytes
// Each byte is the corresponding key value (for specific values, see hid usage tables). If two keys are pressed, the values are 1st and 2.
// Bytes are the corresponding key values, and so on.
// Close the set to keep up with the corresponding
0xc0 // end_collection
};
Through the above analysis, we know that there is only one report in this report, so there is no report ID, so the returned data is actually used. 8-byte input and 1-byte output. The first byte is used to indicate special keys, and the second byte is retained. The next six bytes are normal keys. If only the left ctrl key is pressed, the return value is 01 00 00 00 00 00 00 00 00 (hexadecimal). If only the number key 1 is pressed, the return value is 00 00 59 00 00 00 00 00, if the number keys 1 and 2 are simultaneously pressed, 00 00 59 5A 00 00 00 is returned. If you press the Left Shift key, 02 00 59 5A 00 00 00 00 is returned, then, if the first key is released, 02 00 5A 00 00 00 00 00 is returned. If all the keys are released, 00 00 00 00 00 00 is returned. These data (that is, reports) are returned by the interrupt endpoint. When you press the Num Lock key, the PC sends an output report. From the report descriptor, we know that the num lock LEDs correspond to the leading bit of the output report. When the keypad is opened, output xxxxxxx1 (binary, X is determined by other led States );
When the keypad is disabled, xxxxxxx0 is output (same as before ). You can control the number key to lock the led by removing the leading digit.
The following report descriptor is a USB mouse report descriptor, which is easier than the keyboard. It describes four bytes. The first byte indicates the button, and the second byte indicates the X axis (that is, moving the mouse left and right, 0 indicates not moving, positive value indicates moving to the right, and negative value indicates moving to the left ), the third byte indicates the Y axis (that is, the mouse moves up and down, 0 indicates not moving, positive value indicates moving down, negative value indicates moving up), and the fourth byte indicates the mouse wheel (positive value indicates rolling up, the negative value is scroll down ).
Code char mousereportdescriptor [52] = {
// Universal desktop device
0x05, 0x01, // usage_page (generic desktop)
// Mouse
0x09, 0x02, // usage (Mouse)
// Set
0xa1, 0x01, // collection (Application)
// Pointer Device
0x09, 0x01, // usage (pointer)
// Set
0xa1, 0x00, // collection (physical)
// Press the button
0x05, 0x09, // usage_page (button)
// Use the minimum value of 1
0x19, 0x01, // usage_minimum (Button 1)
// Use the maximum value of 3. 1 indicates the left button, 2 indicates the right button, and 3 indicates the middle key.
0x29, 0x03, // usage_maximum (button 3)
// The logical minimum value is 0.
0x15, 0x00, // logical_minimum (0)
// Logical maximum value 1
0x25, 0x01, // logical_maximum (1)
// 3
0x95, 0x03, // report_count (3)
// The size is 1 bit.
0x75, 0x01, // report_size (1)
// Input, variable, value, and absolute value
// The above three bits indicate the three buttons of the mouse respectively, and the lowest Bit (bit-0) is the left button
// Right-click bit-1, and right-click bit-2. When you press the button, the corresponding bit value is 1, and when you release the button, the corresponding value is 0.
0x81, 0x02, // input (data, VAR, ABS)
// Fill in 5 bits to complement one byte
0x95, 0x01, // report_count (1)
0x75, 0x05, // report_size (5)
0x81, 0x03, // input (cnst, VAR, ABS)
// The usage page is generic Desktop
0x05, 0x01, // usage_page (generic desktop)
// Use X
0x09, 0x30, // usage (X)
// Use y
0x09, 0x31, // usage (y)
// Use Wheel
0x09, 0x38, // usage (wheel)
// The logical minimum value is-127.
0x15, 0x81, // logical_minimum (-127)
// The maximum logical value is + 127
0x25, 0x7f, // logical_maximum (127)
// The size is 8 bits
0x75, 0x08, // report_size (8)
// The number is three, that is, x, y, and scroll wheel.
0x95, 0x03, // report_count (3)
// Input, variable, value, Relative Value
0x81, 0x06, // input (data, VAR, rel)
// Close the set
0xc0, // end_collection
0xc0 // end_collection
};
Through the preceding report analysis, we know that the report returns 4 bytes without the report ID. If you press the left mouse button, the return value is 01 00 00 00 (hexadecimal value). If you right-click the value, the return value is 02 00 00 00. If you press the key, the return value is 04 00 00 00, if the three keys are pressed at the same time, the return value is 07 00 00. If you move the cursor to the right, the second byte returns a positive value. The larger the value, the faster the movement. And so on.
Here we will only give a brief introduction to the report descriptor. For more information, see USB hid protocol and hid usage tables.
[From: http://www.soeol.com/news/content-10245.aspx]
The USB report descriptor can be generated using the hid descriptor tool, which can be downloaded at [http: // www.usb.org.