User Tools

Site Tools


Sidebar

Go Back

Refresh

You are not allowed to add pages

Direct Link

library:usb:hid

HID 1.11

Device Class Definition for HID 1.11

根据USB官方的HID文档描述,HID描述符作用是标识设备的从属描述符的长度和类型,也就是说USB设备还有从属描述符。

1. Standard Descriptors

参考 6.1 Standard Descriptors

The HID class device class uses the following standard USB descriptors:

  • Device
  • Configuration
  • Interface
  • Endpoint
  • String

2. Class-Specific Descriptors

参考 6.2 Class-Specific Descriptors

Each device class includes one or more class-specific descriptors. These descriptors differ from standard USB descriptors. A HID class device uses the following class-specific descriptors:

  • HID
  • Report
  • Physical

2.1 HID Descriptor

2.1.1 字段定义

参考 6.2.1 HID Descriptor

Description     The HID descriptor identifies the length and type of subordinate descriptors for a 
                device.

Parts           Part             Offset/Size    Description
                                 (Bytes)    
                ———————————————————————————————————————————————————————————————————————————————————
                bLength                 0/1     Numeric expression that is the total size of the 
                                                HID descriptor.
                bDescriptorType         1/1     Constant name specifying type of HID
                                                descriptor.
                bcdHID                  2/2     Numeric expression identifying the HIDClass 
                                                Specification release.
                bCountryCode            4/1     Numeric expression identifying country code of
                                                the localized hardware.
                bNumDescriptors         5/1     Numeric expression specifying the number of
                                                class descriptors (always at least one i.e. Report 
                                                descriptor.)
                bDescriptorType         6/1     Constant name identifying type of class
                                                descriptor. See Section 7.1.2: Set_Descriptor 
                                                Request for a table of class descriptor constants.
                wDescriptorLength       7/2     Numeric expression that is the total size of the
                                                Report descriptor.
                [bDescriptorType]...    9/1     Constant name specifying type of optional
                                                descriptor.
                [wDescriptorLength]...  10/2    Numeric expression that is the total size of the
                                                optional descriptor.

                * If an optional descriptor is specified, a corresponding length entry must also be 
                  specified.
                * Multiple optional descriptors and associated lengths may be specified up to 
                  offset (3*n)+6 and (3*n)+7 respectively.
                * The value bNumDescriptors identifies the number of additional class specific 
                  descriptors present. This number must be at least one (1) as a Report 
                  descriptor will always be present. The remainder of the HID descriptor has the 
                  length and type of each additional class descriptor.
                * The value bCountryCode identifies which country the hardware is localized 
                  for. Most hardware is not localized and thus this value would be zero (0). 
                  However, keyboards may use the field to indicate the language of the key caps. 
                  Devices are not required to place a value other than zero in this field, but some 
                  operating environments may require this information. The following table 
                  specifies the valid country codes.     
偏移量 名称 大小 说明
0 bLength 1 描述符的长度(长度由下级描述符多少决定)
1 bDescriptorType 1 描述符类型(HID描述符为0x21)
2 bcdHID 2 HID协议版本
4 bCountryCode 1 国家代码
5 bNumDescriptors 1 下级描述符的数量
6 bDescriptorTyep 1 下级描述符的类型
7 wDescriptorLength 2 下级描述符的长度
9 bDescriptorType 1 下级描述符的类型(可选)
10 wDescriptorLength 2 下级描述符的长度(可选)
(可选)

2.1.2 字段说明

bLength,长度。和前面的描述符长度不一样的是,HID描述符长度不是固定的,长度多少与描述符中包含的下级描述符个数相关。如果只有一个下级描述符,也就是不包括可选的部分,一共有9字节的长度。如果有多个下级描述符,按照长度往上加就行了。

bDescriptorType,描述符类型,不解释,看代码: #define HID_DESCRIPTOR_TYPE 0x21U #define HID_REPORT_DESC 0x22U

bcdHID,HID类的规范版本。文档封面写着

Device Class Definition 
for Human Interface 
Devices (HID)
Firmware Specification—5/27/01
Version 1.11

所以,版本是1.11,写成0x0111,小端形式就是:0x11,0x01.

bCountryCode,国家代码。看下面的表格:

Code(decimal) Country Code Code(decimal) Country Code
00 Not Supported 18 Netherlands/Dutch
01 Arabic 19 Norwegian
02 Belgian 20 Persian
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
36 255 Reserved

一般选择,00.

bNumDescriptors,下级描述符的数量。可以是报告描述符,也可以说是物理描述符。该值至少为1,表示至少有一个报告描述符。

bDescriptorTyep,下级描述符的类型。前面说了,下级描述符一般是报告描述符,所以这里写报告描述符。那它对应的值是多少?

参考文档:7.1章节:

The following defines valid types of Class descriptors.
Value             Class Descriptor Types
---------------------------------------------------------
0x21              HID
0x22              Report
0x23              Physical descriptor
0x24 - 0x2F       Reserved

0x22,报告描述符

wDescriptorLength,下级描述符的长度。前面说了,下级描述符是报告描述符,所以这里放报告描述符的长度。

2.1.3 实例

Appendix E: Example USB Descriptors for HID Class Devices

				E.4 HID Descriptor (Keyboard)
Part                Offset/Size     Description                                         Sample Value
                    (Bytes)
————————————————————————————————————————————————————————————————————————————————————————————————————
bLength             0/1             Size of this descriptor in bytes.                   0x09
bDescriptorType     1/1             HID descriptor type (assigned by USB).              0x21
bcdHID              2/2             HID Class Specification release number in binary-   0x101
                                    coded decimal—for example, 2.10 is 0x210).
bCountryCode        4/1             Hardware target country.                            0x00
bNumDescriptors     5/1             Number of HID class descriptors to follow.          0x01 
bDescriptorType     6/1             Report descriptor type.                             0x22
wDescriptorLength   7/2             Total length of Report descriptor.                  0x3F
/******************** Descriptor of Joystick Mouse HID ********************/
/* 18 */
0x09,                        /*bLength: HID Descriptor size*/
HID_DESCRIPTOR_TYPE,         /*bDescriptorType: HID*/
0x11,                         /*bcdHID: HID Class Spec release number*/
0x01,
0x00,                        /*bCountryCode: Hardware target country*/
0x01,                        /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22,                        /*bDescriptorType*/
HID_MOUSE_REPORT_DESC_SIZE,  /*wItemLength: Total length of Report descriptor*/
0x00,

2.2 Report Descriptor

The Report descriptor is unlike other descriptors in that it is not simply a table of values. The length and content of a Report descriptor vary depending on the number of data fields required for the device’s report or reports. The Report descriptor is made up of items that provide information about the device. The first part of an item contains three fields: item type, item tag, and item size. Together these fields identify the kind of information the item provides.

参考:USB HID 报告描述符

2.2.1 字段定义

2.2.2 字段说明

2.2.3 例子

const  uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] = 
{ 
    0x05, 0x8c,         // USAGE_PAGE (ST Page)
    0x09, 0x01,         // USAGE (Demo Kit)
    0xa1, 0x01,         // COLLECTION (Application)
    // The Input report 
    0x09,0x03,          // USAGE ID - Vendor defined 
    0x15,0x00,          // LOGICAL_MINIMUM (0) 
    0x26,0x00, 0xFF,    // LOGICAL_MAXIMUM (255) 
    0x75,0x08,          // REPORT_SIZE (8bit) 
    0x95,0x40,          // REPORT_COUNT (64Byte) 
    0x81,0x02,          // INPUT (Data,Var,Abs) 
    // The Output report 
    0x09,0x04,          // USAGE ID - Vendor defined 
    0x15,0x00,          // LOGICAL_MINIMUM (0) 
    0x26,0x00,0xFF,     // LOGICAL_MAXIMUM (255) 
    0x75,0x08,          // REPORT_SIZE (8bit) 
    0x95,0x40,          // REPORT_COUNT (64Byte) 
    0x91,0x02,          // OUTPUT (Data,Var,Abs) 
    0xc0                // END_COLLECTION
}; // CustomHID_ReportDescriptor

3. Reference

library/usb/hid.txt · Last modified: 2022/05/02 00:32 (external edit)