xref: /DragonOS/kernel/src/common/hid.h (revision 38b341b8aa671f75ac26d05059aa2e9a09e653b7)
1 #pragma once
2 #include <common/stddef.h>
3 
4 #define __HID_USAGE_TABLE_SIZE 64 // usage stack的大小
5 #define HID_MAX_REPORT 300        // 最大允许的hid report数目(包括feature、input、output)
6 #define HID_MAX_PATH_SIZE 16      // maximum depth for path
7 
8 // 这部分请参考hid_1_11.pdf Section 6.2.2.4
9 
10 #define HID_ITEM_COLLECTION 0xA0
11 #define HID_ITEM_END_COLLECTION 0xC0
12 #define HID_ITEM_FEATURE 0xB0
13 #define HID_ITEM_INPUT 0x80
14 #define HID_ITEM_OUTPUT 0x90
15 
16 /**
17  * @brief 枚举hid的usage page列表。
18  * 原始数据请见<HID Usage Tables FOR Universal Serial Bus (USB)>。
19  * 该文件可从usb.org下载
20  */
21 enum HID_USAGE_PAGE_TYPES
22 {
23     HID_USAGE_PAGE_GEN_DESKTOP = 0x1,
24     HID_USAGE_PAGE_SIMU_CTRL,               // simulation controls
25     HID_USAGE_PAGE_VR_CTRL,                 // vr controls page
26     HID_USAGE_PAGE_SPORT_CTRL,              // sport controls
27     HID_USAGE_PAGE_GAME_CTRL,               // game controls
28     HID_USAGE_PAGE_GEN_DEVICE_CTRL,         // general device controls
29     HID_USAGE_PAGE_KBD_KPD,                     // keyboard/ keypad page
30     HID_USAGE_PAGE_LED,                     // LED
31     HID_USAGE_PAGE_BUTTON,                  // button page
32     HID_USAGE_PAGE_ORDINAL,                 // ordinal page
33     HID_USAGE_PAGE_TEL_DEVICE,              // telephony device
34     HID_USAGE_PAGE_CONSUMER,                // consumer page
35     HID_USAGE_PAGE_DIGITIZER,               // digitizers page
36     HID_USAGE_PAGE_HAPTICS,                 // haptics page
37     HID_USAGE_PAGE_PHY_INPUT_DEVICE,        // physical input device page
38     HID_USAGE_PAGE_UNICODE = 0x10,          // unicode page
39     HID_USAGE_PAGE_EYE_HEAD_TRACKER = 0x12, // eye and head trackers page
40     HID_USAGE_PAGE_AUX_DISPLAY = 0x14,      // auxiliary display page
41     HID_USAGE_PAGE_SENSORS = 0x20,          // sensors page
42     HID_USAGE_PAGE_MEDICAL = 0x40,          // medical instruments
43     HID_USAGE_PAGE_BRAILLE_DISPLAY,         // barille display
44     HID_USAGE_PAGE_LIGHTNING_ILLU = 0x59,   // lighting and illumination page
45     HID_USAGE_PAGE_MONITOR = 0x80,          // monitor page
46     HID_USAGE_PAGE_MONITOR_ENUMERATED,      // monitor enumerated page
47     HID_USAGE_PAGE_VESA_VIRT_CTRL,          // VESA virtual controls page
48     HID_USAGE_PAGE_POWER = 0x84,            // power page
49     HID_USAGE_PAGE_BATTERY_SYSTEM,          // battery system page
50     HID_USAGE_PAGE_BARCODE_SCANNER = 0x8c,  // barcode scanner page
51     HID_USAGE_PAGE_SCALES,                  // scales page
52     HID_USAGE_PAGE_MAGNET_STRIPE_READER,    // magnetic stript reader page
53     HID_USAGE_PAGE_CAMERA_CONTROL = 0x90,   // camera control page
54     HID_USAGE_PAGE_ARCADE,                  // arcade page
55     HID_USAGE_PAGE_GAMING_DEVICE,           // gaming device page
56     HID_USAGE_PAGE_FIDO_ALLIANCE = 0xf1d0,  // FIDO alliance page
57 };
58 
59 /**
60  * @brief usage type for HID_USAGE_PAGE_GEN_DESKTOP page
61  *
62  */
63 enum USAGE_TYPE_GENDESK
64 {
65     HID_USAGE_GENDESK_UNDEF = 0, // undefined
66     HID_USAGE_GENDESK_POINTER,
67     HID_USAGE_GENDESK_MOUSE,
68     HID_USAGE_GENDESK_KEYBOARD = 0x6,
69     HID_USAGE_GENDESK_POINTER_X = 0x30,
70     HID_USAGE_GENDESK_POINTER_Y,
71     HID_USAGE_GENDESK_WHEEL = 0x38,
72     HID_USAGE_GENDESK_NOTHING = 0xff,
73 };
74 
75 /**
76  * @brief 描述hid path中的一个节点
77  *
78  */
79 struct hid_node_t
80 {
81     int u_page;
82     int usage;
83 };
84 
85 /**
86  * @brief 描述一条hid path
87  *
88  */
89 struct hid_path_t
90 {
91     int size; // 路径中的节点数目
92     struct hid_node_t node[HID_MAX_PATH_SIZE];
93 };
94 
95 /**
96  * @brief Describe a HID Data with its location in report
97  *
98  */
99 struct hid_data_t
100 {
101     int value;              // hid对象的值
102     struct hid_path_t path; // hid path
103 
104     int report_count; // count of reports for this usage type
105     int offset;       // offset of data in report
106     int size;         // size of data in bits
107 
108     uint8_t report_id; // report id(from incoming report)
109     uint8_t type;      // 数据类型:FEATURE / INPUT / OUTPUT
110     uint8_t attribute; // report field attribute. (2 = (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position))
111                        //                           (6 = (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position))
112     int8_t unit_exp;   // unit exponent;
113 
114     uint32_t unit; // HID unit
115 
116     int logical_min; // Logical min
117     int logical_max; // Logical max
118     int phys_min;    // Physical min
119     int phys_max;    // Physical max
120 };
121 
122 /**
123  * @brief hid解析器
124  *
125  */
126 struct hid_parser
127 {
128     const uint8_t *report_desc; // 指向report descriptor的指针
129     int report_desc_size;       // report descriptor的大小(字节)
130     int pos;                    // report_desc中,当前正在处理的位置
131     uint8_t item;               // 暂存当前的item
132     uint32_t value;             // 暂存当前的值
133 
134     struct hid_data_t data; // 存储当前的环境
135 
136     int offset_table[HID_MAX_REPORT][3]; // 存储 hid report的ID、type、offset
137     int report_count;                    // hid report的数量
138     int count;                           // local items的计数
139 
140     uint32_t u_page;
141     struct hid_node_t usage_table[__HID_USAGE_TABLE_SIZE]; // Usage stack
142     int usage_size;                                        // usage的数量
143     int usage_min;
144     int usage_max;
145 
146     int cnt_objects; // report descriptor中的对象数目
147 
148     int cnt_report; // report desc中的report数目
149 };
150 
151 struct hid_usage_types_string
152 {
153     int value;
154     const char *string;
155 };
156 
157 struct hid_usage_pages_string
158 {
159     int value;
160     struct hid_usage_types_string *types;
161     const char *string;
162 };
163 
164 int hid_parse_report(const void *report_data, const int len);
165 
166 bool hid_parse_find_object(const void *hid_report, const int report_size, struct hid_data_t *data);