hid.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2021 Nils Ollrogge
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include <stdint.h>
22 
23 #include "usb/hid/hid_usage.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
32 #define USB_TYPE_DESCRIPTOR_HID 0x21
33 
37 #define USB_HID_VERSION_BCD 0x0110
38 
43 #define USB_HID_SUBCLASS_NONE 0x0
44 #define USB_HID_SUBCLASS_BOOT 0x1
51 #define USB_HID_PROTOCOL_NONE 0x0
52 #define USB_HID_PROTOCOL_KEYBOARD 0x1
53 #define USB_HID_PROTOCOL_MOUSE 0x2
60 #define USB_HID_DESCR_HID 0x21
61 #define USB_HID_DESCR_REPORT 0x22
62 #define USB_HID_DESCR_PHYSICAL 0x23
68 #define USB_HID_COUNTRY_CODE_NOTSUPPORTED 0x00
69 
74 #define USB_HID_REQUEST_GET_REPORT 0x01
75 #define USB_HID_REQUEST_GET_IDLE 0x02
76 #define USB_HID_REQUEST_GET_PROTOCOL 0x03
77 #define USB_HID_REQUEST_SET_REPORT 0x09
78 #define USB_HID_REQUEST_SET_IDLE 0x0a
79 #define USB_HID_REQUEST_SET_PROTOCOL 0x0b
85 #define USBUS_HID_EP_IN_REQUIRED_NUMOF 1
86 
90 #define USBUS_HID_EP_OUT_REQUIRED_NUMOF 1
91 
97 typedef struct __attribute__((packed)){
98  uint8_t length;
100  uint8_t desc_type;
103  uint16_t bcd_hid;
105  uint8_t country_code;
107  uint8_t num_descrs;
110  uint8_t report_type;
111  uint16_t report_length;
113 
125 #define USB_HID_ITEM_TYPE_MAIN 0x0
126 
130 #define USB_HID_ITEM_TYPE_GLOBAL 0x1
131 
135 #define USB_HID_ITEM_TYPE_LOCAL 0x2
136 
140 #define USB_HID_ITEM_TAG_INPUT 0x8
141 
145 #define USB_HID_ITEM_TAG_OUTPUT 0x9
146 
150 #define USB_HID_ITEM_TAG_COLLECTION 0xA
151 
155 #define USB_HID_ITEM_TAG_FEATURE 0xB
156 
160 #define USB_HID_ITEM_TAG_COLLECTION_END 0xC
161 
165 #define USB_HID_ITEM_TAG_USAGE_PAGE 0x0
169 #define USB_HID_ITEM_TAG_LOGICAL_MIN 0x1
170 
174 #define USB_HID_ITEM_TAG_LOGICAL_MAX 0x2
175 
179 #define USB_HID_ITEM_TAG_PHYSICAL_MIN 0x3
180 
184 #define USB_HID_ITEM_TAG_PHYSICAL_MAX 0x4
185 
189 #define USB_HID_ITEM_TAG_UNIT_EXPONENT 0x5
190 
194 #define USB_HID_ITEM_TAG_UNIT 0x6
195 
199 #define USB_HID_ITEM_TAG_REPORT_SIZE 0x7
200 
204 #define USB_HID_ITEM_TAG_REPORT_ID 0x8
205 
209 #define USB_HID_ITEM_TAG_REPORT_COUNT 0x9
210 
214 #define USB_HID_ITEM_TAG_USAGE 0x0
215 
219 #define USB_HID_ITEM_TAG_USAGE_MIN 0x1
220 
224 #define USB_HID_ITEM_TAG_USAGE_MAX 0x2
225 
229 #define USB_HID_COLLECTION_PHYSICAL 0x00
230 
234 #define USB_HID_COLLECTION_APPLICATION 0x01
235 
239 #define USB_HID_COLLECTION_LOGICAL 0x02
240 
244 #define USB_HID_COLLECTION_REPORT 0x03
245 
249 #define USB_HID_COLLECTION_NAMED_ARRAY 0x04
250 
254 #define USB_HID_COLLECTION_USAGE_SWITCH 0x05
255 
259 #define USB_HID_COLLECTION_USAGE_MOD 0x06
260 
268 #define USB_HID_ITEM(tag, type, size) \
269  (((tag & 0xF) << 4) | ((type & 0x3) << 2) | (size & 0x3))
270 
278 #define USB_HID_INPUT(input) \
279  USB_HID_ITEM(USB_HID_ITEM_TAG_INPUT, USB_HID_ITEM_TYPE_MAIN, 1), \
280  input
281 
289 #define USB_HID_OUTPUT(output) \
290  USB_HID_ITEM(USB_HID_ITEM_TAG_OUTPUT, USB_HID_ITEM_TYPE_MAIN, 1), \
291  output
292 
300 #define USB_HID_FEATURE(feature) \
301  USB_HID_ITEM(USB_HID_ITEM_TAG_FEATURE, USB_HID_ITEM_TYPE_MAIN, 1), \
302  feature
303 
311 #define USB_HID_COLLECTION(collection) \
312  USB_HID_ITEM(USB_HID_ITEM_TAG_COLLECTION, USB_HID_ITEM_TYPE_MAIN, 1), \
313  collection
314 
318 #define USB_HID_END_COLLECTION \
319  USB_HID_ITEM(USB_HID_ITEM_TAG_COLLECTION_END, USB_HID_ITEM_TYPE_MAIN, 0)
320 
328 #define USB_HID_USAGE_PAGE(page) \
329  USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_PAGE, USB_HID_ITEM_TYPE_GLOBAL, 1), \
330  page
331 
339 #define USB_HID_USAGE_PAGE16(page) \
340  USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_PAGE, USB_HID_ITEM_TYPE_GLOBAL, 2), \
341  page & 0xff, (page >> 8)
342 
350 #define USB_HID_LOGICAL_MIN8(minimum) \
351  USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MIN, USB_HID_ITEM_TYPE_GLOBAL, 1), \
352  minimum
353 
361 #define USB_HID_LOGICAL_MIN16(minimum) \
362  USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MIN, USB_HID_ITEM_TYPE_GLOBAL, 2), \
363  minimum & 0xff, (minimum >> 8)
364 
372 #define USB_HID_LOGICAL_MIN32(minimum) \
373  USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MIN, USB_HID_ITEM_TYPE_GLOBAL, 3), \
374  minimum & 0xff, \
375  (minimum >> 8) & 0xff, \
376  (minimum >> 16) & 0xff, \
377  (minimum >> 24) & 0xff
378 
386 #define USB_HID_LOGICAL_MAX8(maximum) \
387  USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MAX, USB_HID_ITEM_TYPE_GLOBAL, 1), \
388  maximum
389 
397 #define USB_HID_LOGICAL_MAX16(maximum) \
398  USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MAX, USB_HID_ITEM_TYPE_GLOBAL, 2), \
399  (maximum & 0xff), (maximum >> 8)
400 
408 #define USB_HID_LOGICAL_MAX32(maximum) \
409  USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MAX, USB_HID_ITEM_TYPE_GLOBAL, 3), \
410  maximum & 0xff, \
411  (maximum >> 8) & 0xff, \
412  (maximum >> 16) & 0xff, \
413  (maximum >> 24) & 0xff
414 
422 #define USB_HID_REPORT_SIZE(size) \
423  USB_HID_ITEM(USB_HID_ITEM_TAG_REPORT_SIZE, USB_HID_ITEM_TYPE_GLOBAL, 1), \
424  size
425 
433 #define USB_HID_REPORT_ID(id) \
434  USB_HID_ITEM(USB_HID_ITEM_TAG_REPORT_ID, USB_HID_ITEM_TYPE_GLOBAL, 1), \
435  id
436 
444 #define USB_HID_REPORT_COUNT(count) \
445  USB_HID_ITEM(USB_HID_ITEM_TAG_REPORT_COUNT, USB_HID_ITEM_TYPE_GLOBAL, 1), \
446  count
447 
455 #define USB_HID_USAGE(index) \
456  USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE, USB_HID_ITEM_TYPE_LOCAL, 1), \
457  index
458 
466 #define USB_HID_USAGE_MIN8(start) \
467  USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MIN, USB_HID_ITEM_TYPE_LOCAL, 1), \
468  start
469 
477 #define USB_HID_USAGE_MIN16(start) \
478  USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MIN, USB_HID_ITEM_TYPE_LOCAL, 2), \
479  start & 0xff, (start >> 8) & 0xff
480 
488 #define USB_HID_USAGE_MAX8(end) \
489  USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MAX, USB_HID_ITEM_TYPE_LOCAL, 1), \
490  end
491 
499 #define USB_HID_USAGE_MAX16(end) \
500  USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MAX, USB_HID_ITEM_TYPE_LOCAL, 2), \
501  end & 0xff, (end >> 8) & 0xff
502 
505 #ifdef __cplusplus
506 }
507 #endif
508 
Definition for USB HID usage tables.
USB HID descriptor struct.
Definition: hid.h:97
uint8_t report_type
Type of HID class report.
Definition: hid.h:110
uint8_t country_code
Numeric expression identifying country code of the localized hardware.
Definition: hid.h:105
uint16_t report_length
the total size of the Report descriptor.
Definition: hid.h:111
uint8_t desc_type
Constant name specifying type of HID descriptor.
Definition: hid.h:100
uint8_t length
Numeric expression that is the total size of the HID descriptor.
Definition: hid.h:98
uint16_t bcd_hid
Numeric expression identifying the HID Class Specification release.
Definition: hid.h:103
uint8_t num_descrs
Numeric expression specifying the number of class descriptors.
Definition: hid.h:107