usbh_hid.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USBH_HID_H
  7. #define USBH_HID_H
  8. #include "usb_hid.h"
  9. /* local items */
  10. #define HID_REPORT_FLAG_USAGE_MIN (1 << 0)
  11. #define HID_REPORT_FLAG_USAGE_MAX (1 << 1)
  12. /* global items */
  13. #define HID_REPORT_FLAG_REPORT_ID (1 << 2)
  14. #define HID_REPORT_FLAG_REPORT_COUNT (1 << 3)
  15. #define HID_REPORT_FLAG_REPORT_SIZE (1 << 4)
  16. #define HID_REPORT_FLAG_LOGICAL_MIN (1 << 5)
  17. #define HID_REPORT_FLAG_LOGICAL_MAX (1 << 6)
  18. #define HID_REPORT_FLAG_USAGE_PAGE (1 << 7)
  19. /* main items */
  20. #define HID_REPORT_FLAG_INPUT (1 << 8)
  21. #define HID_REPORT_FLAG_OUTPUT (1 << 9)
  22. #define HID_REPORT_FLAG_FEATURE (1 << 10)
  23. #define HID_REPORT_FLAG_EXTENDED_USAGE (1 << 11)
  24. /* masks */
  25. #define HID_REPORT_FLAG_GLOBAL_MASK (HID_REPORT_FLAG_REPORT_ID | \
  26. HID_REPORT_FLAG_REPORT_COUNT | \
  27. HID_REPORT_FLAG_REPORT_SIZE | \
  28. HID_REPORT_FLAG_LOGICAL_MIN | \
  29. HID_REPORT_FLAG_LOGICAL_MAX | \
  30. HID_REPORT_FLAG_USAGE_PAGE)
  31. #define HID_REPORT_FLAG_REQUIRED_MASK (HID_REPORT_FLAG_REPORT_COUNT | \
  32. HID_REPORT_FLAG_REPORT_SIZE | \
  33. HID_REPORT_FLAG_LOGICAL_MIN | \
  34. HID_REPORT_FLAG_LOGICAL_MAX)
  35. #define USAGE_ID(usage) (usage & 0x0000FFFF)
  36. #define USAGE_PAGE(usage) ((usage & 0xFFFF0000) >> 16)
  37. #ifndef CONFIG_USBHOST_HID_MAX_INPUT
  38. #define CONFIG_USBHOST_HID_MAX_INPUT 16
  39. #endif
  40. #ifndef CONFIG_USBHOST_HID_MAX_OUTPUT
  41. #define CONFIG_USBHOST_HID_MAX_OUTPUT 16
  42. #endif
  43. #ifndef CONFIG_USBHOST_HID_MAX_FEATURE
  44. #define CONFIG_USBHOST_HID_MAX_FEATURE 16
  45. #endif
  46. struct hid_report_field {
  47. uint32_t *usages; /* usage page + usage */
  48. uint32_t usage_count;
  49. uint32_t usage_page;
  50. uint32_t report_id; /* optional */
  51. uint32_t report_count;
  52. uint32_t report_size;
  53. int32_t logical_min;
  54. int32_t logical_max;
  55. uint32_t properties;
  56. uint32_t usage_min;
  57. uint32_t usage_max;
  58. uint32_t flags;
  59. };
  60. struct hid_report {
  61. bool uses_report_id;
  62. uint32_t input_count;
  63. struct hid_report_field input_fields[CONFIG_USBHOST_HID_MAX_INPUT];
  64. uint32_t output_count;
  65. struct hid_report_field output_fields[CONFIG_USBHOST_HID_MAX_OUTPUT];
  66. uint32_t feature_count;
  67. struct hid_report_field feature_fields[CONFIG_USBHOST_HID_MAX_FEATURE];
  68. };
  69. struct usbh_hid {
  70. struct usbh_hubport *hport;
  71. struct usb_endpoint_descriptor *intin; /* INTR IN endpoint */
  72. struct usb_endpoint_descriptor *intout; /* INTR OUT endpoint */
  73. struct usbh_urb intin_urb; /* INTR IN urb */
  74. struct usbh_urb intout_urb; /* INTR OUT urb */
  75. uint16_t report_size;
  76. uint8_t protocol;
  77. uint8_t intf; /* interface number */
  78. uint8_t minor;
  79. void *user_data;
  80. };
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. int usbh_hid_get_report_descriptor(struct usbh_hid *hid_class, uint8_t *buffer, uint32_t buflen);
  85. int usbh_hid_set_idle(struct usbh_hid *hid_class, uint8_t report_id, uint8_t duration);
  86. int usbh_hid_get_idle(struct usbh_hid *hid_class, uint8_t *buffer);
  87. int usbh_hid_set_protocol(struct usbh_hid *hid_class, uint8_t protocol);
  88. int usbh_hid_get_protocol(struct usbh_hid *hid_class, uint8_t *protocol);
  89. int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen);
  90. int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen);
  91. struct hid_report *usbh_hid_report_parse(const uint8_t *data, uint32_t report_len, uint32_t max_usages);
  92. void usbh_hid_report_free(struct hid_report *hid_report);
  93. void usbh_hid_run(struct usbh_hid *hid_class);
  94. void usbh_hid_stop(struct usbh_hid *hid_class);
  95. int lshid(int argc, char **argv);
  96. #ifdef __cplusplus
  97. }
  98. #endif
  99. #endif /* USBH_HID_H */