In USB, the USB Host identifies the device by using various descriptors, including the device descriptor,
Configuration descriptor, interface descriptor, endpoint descriptor, string descriptor, report descriptor, and so on.
The USB report descriptor is a descriptor in the HID device, which is a comparison
A complex 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, move the USB mouse and click the mouse.
The information is returned to the computer, and the keyboard returns the key data to the computer. The output report is sent by the host.
For USB devices, such as the Digital keypad lock lamp and uppercase letter lock lamp. Report is
A data packet contains the data to be transmitted. Input reports are input through interrupt endpoints
The output report is a little different. When the output point is not interrupted, you can control the output
When the output end point is interrupted, it is sent by the output end point.
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
The host uses the report descriptor command to obtain the report descriptor. Pay attention to this request.
Is sent to the interface, not to the device. One report descriptor can describe multiple reports.
The report is identified by the report ID, which is the first byte in the report. In the report Descriptor
If no ID is specified for a report, there is no ID field in the report, and the start is data. For more details, see
USB hid protocol, which can be downloaded from http: // www.usb.org.
You can use the hid descriptor tool to generate a USB report descriptor.
Download to http: // www.usb.org. I uploaded a copy for your convenience.
Use the USB mouse and USB keyboard generated by the hid descriptor tool below the http://www.ednchina.com/Upload/Blog/2007/4/2/af7c3443-ad61-4465-adc7-a74d28bbc322.zipuploadfile-/2007-4/42672233.zip to declare a 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 minimum 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,
Therefore, the returned data is actually used. 8-byte input and 1-byte output. The entered
The first byte is used to indicate special keys, and the second byte is retained. The next six bytes are normal keys. If
If only the left ctrl key is pressed, the return value is 01 00 00 00 00 00 00 (hexadecimal). If
If only number key 1 is pressed, 00 00 59 00 00 00 00 00 is returned.
If keys 1 and 2 are pressed at the same time, 00 00 59 5A 00 00 00 is returned. If
Press the Left Shift key, then 02 00 59 5A 00 00 00,
Then release the 1 key, and then return 02 00 5A 00 00 00 00,
If all the buttons are released, 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 will send
Output report. From the report descriptor, we know that the LED of Num Lock corresponds to the leading bit of the output report,
When the keypad is opened, xxxxxxx1 is output (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 cursor left and right,
0 indicates not moving, positive value indicates moving to the right, negative value indicates moving to the left), and the third byte indicates the Y axis (that is, the mouse
Move up or down. 0 indicates not moving. positive value indicates moving down, negative value indicates moving up), fourth byte
Indicates the scroll wheel of the mouse (positive value indicates scroll up, and negative value indicates 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 minimum 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,
01 00 00 00 (hexadecimal value) is returned. If you right-click the value, 02 00 00 00 is returned. If you press the key,
The return value is 04 00 00. If the three keys are simultaneously pressed, 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 moving speed. 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,
You can download it from http: // www.usb.org.
According to the actual design of the USB keyboard and USB mouse:
USB keyboard:
User1/2198/archives/2007/36484 .html
Http://www.ednchina.com/blog/computer00/20134/message.aspx
USB mouse:
User1/2198/archives/2007/36520 .html
Http://www.ednchina.com/blog/computer00/20135/message.aspx