usbd_hid.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
  41. return -1;
  42. }
  43. return 0;
  44. }
  45. struct usbd_interface *usbd_hid_init_intf(uint8_t busid, struct usbd_interface *intf, const uint8_t *desc, uint32_t desc_len)
  46. {
  47. (void)busid;
  48. intf->class_interface_handler = hid_class_interface_request_handler;
  49. intf->class_endpoint_handler = NULL;
  50. intf->vendor_handler = NULL;
  51. intf->notify_handler = NULL;
  52. intf->hid_report_descriptor = desc;
  53. intf->hid_report_descriptor_len = desc_len;
  54. return intf;
  55. }
  56. /*
  57. * Appendix G: HID Request Support Requirements
  58. *
  59. * The following table enumerates the requests that need to be supported by various types of HID class devices.
  60. * Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  61. * ------------------------------------------------------------------------------------------
  62. * Boot Mouse Required Optional Optional Optional Required Required
  63. * Non-Boot Mouse Required Optional Optional Optional Optional Optional
  64. * Boot Keyboard Required Optional Required Required Required Required
  65. * Non-Boot Keybrd Required Optional Required Required Optional Optional
  66. * Other Device Required Optional Optional Optional Optional Optional
  67. */
  68. __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)
  69. {
  70. (void)busid;
  71. (void)intf;
  72. (void)report_id;
  73. (void)report_type;
  74. (*data[0]) = 0;
  75. *len = 1;
  76. }
  77. __WEAK uint8_t usbd_hid_get_idle(uint8_t busid, uint8_t intf, uint8_t report_id)
  78. {
  79. (void)busid;
  80. (void)intf;
  81. (void)report_id;
  82. return 0;
  83. }
  84. __WEAK uint8_t usbd_hid_get_protocol(uint8_t busid, uint8_t intf)
  85. {
  86. (void)busid;
  87. (void)intf;
  88. return 0;
  89. }
  90. __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)
  91. {
  92. (void)busid;
  93. (void)intf;
  94. (void)report_id;
  95. (void)report_type;
  96. (void)report;
  97. (void)report_len;
  98. }
  99. __WEAK void usbd_hid_set_idle(uint8_t busid, uint8_t intf, uint8_t report_id, uint8_t duration)
  100. {
  101. (void)busid;
  102. (void)intf;
  103. (void)report_id;
  104. (void)duration;
  105. }
  106. __WEAK void usbd_hid_set_protocol(uint8_t busid, uint8_t intf, uint8_t protocol)
  107. {
  108. (void)busid;
  109. (void)intf;
  110. (void)protocol;
  111. }