usbd_hid.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_hid.h"
  8. static int hid_class_interface_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  9. {
  10. USB_LOG_DBG("HID Class request: "
  11. "bRequest 0x%02x\r\n",
  12. setup->bRequest);
  13. uint8_t intf_num = LO_BYTE(setup->wIndex);
  14. switch (setup->bRequest) {
  15. case HID_REQUEST_GET_REPORT:
  16. /* report id ,report type */
  17. usbd_hid_get_report(busid, intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), data, len);
  18. break;
  19. case HID_REQUEST_GET_IDLE:
  20. (*data)[0] = usbd_hid_get_idle(busid, intf_num, LO_BYTE(setup->wValue));
  21. *len = 1;
  22. break;
  23. case HID_REQUEST_GET_PROTOCOL:
  24. (*data)[0] = usbd_hid_get_protocol(busid, intf_num);
  25. *len = 1;
  26. break;
  27. case HID_REQUEST_SET_REPORT:
  28. /* report id ,report type, report, report len */
  29. usbd_hid_set_report(busid, intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), *data, *len);
  30. break;
  31. case HID_REQUEST_SET_IDLE:
  32. /* report id, duration */
  33. usbd_hid_set_idle(busid, intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue));
  34. break;
  35. case HID_REQUEST_SET_PROTOCOL:
  36. /* protocol */
  37. usbd_hid_set_protocol(busid, intf_num, LO_BYTE(setup->wValue));
  38. break;
  39. default:
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. struct usbd_interface *usbd_hid_init_intf(uint8_t busid, struct usbd_interface *intf, const uint8_t *desc, uint32_t desc_len)
  45. {
  46. (void)busid;
  47. intf->class_interface_handler = hid_class_interface_request_handler;
  48. intf->class_endpoint_handler = NULL;
  49. intf->vendor_handler = NULL;
  50. intf->notify_handler = NULL;
  51. intf->hid_report_descriptor = desc;
  52. intf->hid_report_descriptor_len = desc_len;
  53. return intf;
  54. }
  55. /*
  56. * Appendix G: HID Request Support Requirements
  57. *
  58. * The following table enumerates the requests that need to be supported by various types of HID class devices.
  59. * Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  60. * ------------------------------------------------------------------------------------------
  61. * Boot Mouse Required Optional Optional Optional Required Required
  62. * Non-Boot Mouse Required Optional Optional Optional Optional Optional
  63. * Boot Keyboard Required Optional Required Required Required Required
  64. * Non-Boot Keybrd Required Optional Required Required Optional Optional
  65. * Other Device Required Optional Optional Optional Optional Optional
  66. */
  67. __WEAK void usbd_hid_get_report(uint8_t busid, uint8_t intf, uint8_t report_id, uint8_t report_type, uint8_t **data, uint32_t *len)
  68. {
  69. (void)busid;
  70. (void)intf;
  71. (void)report_id;
  72. (void)report_type;
  73. (*data[0]) = 0;
  74. *len = 1;
  75. }
  76. __WEAK uint8_t usbd_hid_get_idle(uint8_t busid, uint8_t intf, uint8_t report_id)
  77. {
  78. (void)busid;
  79. (void)intf;
  80. (void)report_id;
  81. return 0;
  82. }
  83. __WEAK uint8_t usbd_hid_get_protocol(uint8_t busid, uint8_t intf)
  84. {
  85. (void)busid;
  86. (void)intf;
  87. return 0;
  88. }
  89. __WEAK void usbd_hid_set_report(uint8_t busid, uint8_t intf, uint8_t report_id, uint8_t report_type, uint8_t *report, uint32_t report_len)
  90. {
  91. (void)busid;
  92. (void)intf;
  93. (void)report_id;
  94. (void)report_type;
  95. (void)report;
  96. (void)report_len;
  97. }
  98. __WEAK void usbd_hid_set_idle(uint8_t busid, uint8_t intf, uint8_t report_id, uint8_t duration)
  99. {
  100. (void)busid;
  101. (void)intf;
  102. (void)report_id;
  103. (void)duration;
  104. }
  105. __WEAK void usbd_hid_set_protocol(uint8_t busid, uint8_t intf, uint8_t protocol)
  106. {
  107. (void)busid;
  108. (void)intf;
  109. (void)protocol;
  110. }