Loading...
Searching...
No Matches
hid.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Nils Ollrogge
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License v2.1. See the file LICENSE in the top level directory for
6 * more details.
7 */
8
22#ifndef USB_HID_H
23#define USB_HID_H
24
25#include <stdint.h>
26
27#include "usb/hid/hid_usage.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
36#define USB_TYPE_DESCRIPTOR_HID 0x21
37
41#define USB_HID_VERSION_BCD 0x0110
42
47#define USB_HID_SUBCLASS_NONE 0x0
48#define USB_HID_SUBCLASS_BOOT 0x1
55#define USB_HID_PROTOCOL_NONE 0x0
56#define USB_HID_PROTOCOL_KEYBOARD 0x1
57#define USB_HID_PROTOCOL_MOUSE 0x2
64#define USB_HID_DESCR_HID 0x21
65#define USB_HID_DESCR_REPORT 0x22
66#define USB_HID_DESCR_PHYSICAL 0x23
72#define USB_HID_COUNTRY_CODE_NOTSUPPORTED 0x00
73
78#define USB_HID_REQUEST_GET_REPORT 0x01
79#define USB_HID_REQUEST_GET_IDLE 0x02
80#define USB_HID_REQUEST_GET_PROTOCOL 0x03
81#define USB_HID_REQUEST_SET_REPORT 0x09
82#define USB_HID_REQUEST_SET_IDLE 0x0a
83#define USB_HID_REQUEST_SET_PROTOCOL 0x0b
89#define USBUS_HID_EP_IN_REQUIRED_NUMOF 1
90
94#define USBUS_HID_EP_OUT_REQUIRED_NUMOF 1
95
101typedef struct __attribute__((packed)){
102 uint8_t length;
104 uint8_t desc_type;
107 uint16_t bcd_hid;
109 uint8_t country_code;
111 uint8_t num_descrs;
114 uint8_t report_type;
115 uint16_t report_length;
117
130#define USB_HID_ITEM_TYPE_MAIN 0x0
131
135#define USB_HID_ITEM_TYPE_GLOBAL 0x1
136
140#define USB_HID_ITEM_TYPE_LOCAL 0x2
141
145#define USB_HID_ITEM_TAG_INPUT 0x8
146
150#define USB_HID_ITEM_TAG_OUTPUT 0x9
151
155#define USB_HID_ITEM_TAG_COLLECTION 0xA
156
160#define USB_HID_ITEM_TAG_FEATURE 0xB
161
165#define USB_HID_ITEM_TAG_COLLECTION_END 0xC
166
170#define USB_HID_ITEM_TAG_USAGE_PAGE 0x0
174#define USB_HID_ITEM_TAG_LOGICAL_MIN 0x1
175
179#define USB_HID_ITEM_TAG_LOGICAL_MAX 0x2
180
184#define USB_HID_ITEM_TAG_PHYSICAL_MIN 0x3
185
189#define USB_HID_ITEM_TAG_PHYSICAL_MAX 0x4
190
194#define USB_HID_ITEM_TAG_UNIT_EXPONENT 0x5
195
199#define USB_HID_ITEM_TAG_UNIT 0x6
200
204#define USB_HID_ITEM_TAG_REPORT_SIZE 0x7
205
209#define USB_HID_ITEM_TAG_REPORT_ID 0x8
210
214#define USB_HID_ITEM_TAG_REPORT_COUNT 0x9
215
219#define USB_HID_ITEM_TAG_USAGE 0x0
220
224#define USB_HID_ITEM_TAG_USAGE_MIN 0x1
225
229#define USB_HID_ITEM_TAG_USAGE_MAX 0x2
230
234#define USB_HID_COLLECTION_PHYSICAL 0x00
235
239#define USB_HID_COLLECTION_APPLICATION 0x01
240
244#define USB_HID_COLLECTION_LOGICAL 0x02
245
249#define USB_HID_COLLECTION_REPORT 0x03
250
254#define USB_HID_COLLECTION_NAMED_ARRAY 0x04
255
259#define USB_HID_COLLECTION_USAGE_SWITCH 0x05
260
264#define USB_HID_COLLECTION_USAGE_MOD 0x06
265
273#define USB_HID_ITEM(tag, type, size) \
274 (((tag & 0xF) << 4) | ((type & 0x3) << 2) | (size & 0x3))
275
283#define USB_HID_INPUT(input) \
284 USB_HID_ITEM(USB_HID_ITEM_TAG_INPUT, USB_HID_ITEM_TYPE_MAIN, 1), \
285 input
286
294#define USB_HID_OUTPUT(output) \
295 USB_HID_ITEM(USB_HID_ITEM_TAG_OUTPUT, USB_HID_ITEM_TYPE_MAIN, 1), \
296 output
297
298
306#define USB_HID_FEATURE(feature) \
307 USB_HID_ITEM(USB_HID_ITEM_TAG_FEATURE, USB_HID_ITEM_TYPE_MAIN, 1), \
308 feature
309
317#define USB_HID_COLLECTION(collection) \
318 USB_HID_ITEM(USB_HID_ITEM_TAG_COLLECTION, USB_HID_ITEM_TYPE_MAIN, 1), \
319 collection
320
324#define USB_HID_END_COLLECTION \
325 USB_HID_ITEM(USB_HID_ITEM_TAG_COLLECTION_END, USB_HID_ITEM_TYPE_MAIN, 0)
326
334#define USB_HID_USAGE_PAGE(page) \
335 USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_PAGE, USB_HID_ITEM_TYPE_GLOBAL, 1), \
336 page
337
345#define USB_HID_USAGE_PAGE16(page) \
346 USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_PAGE, USB_HID_ITEM_TYPE_GLOBAL, 2), \
347 page & 0xff, (page >> 8)
348
356#define USB_HID_LOGICAL_MIN8(minimum) \
357 USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MIN, USB_HID_ITEM_TYPE_GLOBAL, 1), \
358 minimum
359
367#define USB_HID_LOGICAL_MIN16(minimum) \
368 USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MIN, USB_HID_ITEM_TYPE_GLOBAL, 2), \
369 minimum & 0xff, (minimum >> 8)
370
371
379#define USB_HID_LOGICAL_MIN32(minimum) \
380 USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MIN, USB_HID_ITEM_TYPE_GLOBAL, 3), \
381 minimum & 0xff, \
382 (minimum >> 8) & 0xff, \
383 (minimum >> 16) & 0xff, \
384 (minimum >> 24) & 0xff
385
393#define USB_HID_LOGICAL_MAX8(maximum) \
394 USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MAX, USB_HID_ITEM_TYPE_GLOBAL, 1), \
395 maximum
396
404#define USB_HID_LOGICAL_MAX16(maximum) \
405 USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MAX, USB_HID_ITEM_TYPE_GLOBAL, 2), \
406 (maximum & 0xff), (maximum >> 8)
407
415#define USB_HID_LOGICAL_MAX32(maximum) \
416 USB_HID_ITEM(USB_HID_ITEM_TAG_LOGICAL_MAX, USB_HID_ITEM_TYPE_GLOBAL, 3), \
417 maximum & 0xff, \
418 (maximum >> 8) & 0xff, \
419 (maximum >> 16) & 0xff, \
420 (maximum >> 24) & 0xff
421
429#define USB_HID_REPORT_SIZE(size) \
430 USB_HID_ITEM(USB_HID_ITEM_TAG_REPORT_SIZE, USB_HID_ITEM_TYPE_GLOBAL, 1), \
431 size
432
440#define USB_HID_REPORT_ID(id) \
441 USB_HID_ITEM(USB_HID_ITEM_TAG_REPORT_ID, USB_HID_ITEM_TYPE_GLOBAL, 1), \
442 id
443
451#define USB_HID_REPORT_COUNT(count) \
452 USB_HID_ITEM(USB_HID_ITEM_TAG_REPORT_COUNT, USB_HID_ITEM_TYPE_GLOBAL, 1), \
453 count
454
462#define USB_HID_USAGE(index) \
463 USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE, USB_HID_ITEM_TYPE_LOCAL, 1), \
464 index
465
473#define USB_HID_USAGE_MIN8(start) \
474 USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MIN, USB_HID_ITEM_TYPE_LOCAL, 1), \
475 start
476
484#define USB_HID_USAGE_MIN16(start) \
485 USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MIN, USB_HID_ITEM_TYPE_LOCAL, 2), \
486 start & 0xff, (start >> 8) & 0xff
487
495#define USB_HID_USAGE_MAX8(end) \
496 USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MAX, USB_HID_ITEM_TYPE_LOCAL, 1), \
497 end
498
506#define USB_HID_USAGE_MAX16(end) \
507 USB_HID_ITEM(USB_HID_ITEM_TAG_USAGE_MAX, USB_HID_ITEM_TYPE_LOCAL, 2), \
508 end & 0xff, (end >> 8) & 0xff
509
512#ifdef __cplusplus
513}
514#endif
515
516#endif /* USB_HID_H */
Definition for USB HID usage tables.
USB HID descriptor struct.
Definition hid.h:101
uint8_t report_type
Type of HID class report.
Definition hid.h:114
uint8_t country_code
Numeric expression identifying country code of the localized hardware.
Definition hid.h:109
uint16_t report_length
the total size of the Report descriptor.
Definition hid.h:115
uint8_t desc_type
Constant name specifying type of HID descriptor.
Definition hid.h:104
uint8_t length
Numeric expression that is the total size of the HID descriptor.
Definition hid.h:102
uint16_t bcd_hid
Numeric expression identifying the HID Class Specification release.
Definition hid.h:107
uint8_t num_descrs
Numeric expression specifying the number of class descriptors.
Definition hid.h:111