usbh_hid.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_hid.h"
  8. #undef USB_DBG_TAG
  9. #define USB_DBG_TAG "usbh_hid"
  10. #include "usb_log.h"
  11. #define DEV_FORMAT "/dev/input%d"
  12. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_buf[128];
  13. static struct usbh_hid g_hid_class[CONFIG_USBHOST_MAX_HID_CLASS];
  14. static uint32_t g_devinuse = 0;
  15. static struct usbh_hid *usbh_hid_class_alloc(void)
  16. {
  17. int devno;
  18. for (devno = 0; devno < CONFIG_USBHOST_MAX_HID_CLASS; devno++) {
  19. if ((g_devinuse & (1 << devno)) == 0) {
  20. g_devinuse |= (1 << devno);
  21. memset(&g_hid_class[devno], 0, sizeof(struct usbh_hid));
  22. g_hid_class[devno].minor = devno;
  23. return &g_hid_class[devno];
  24. }
  25. }
  26. return NULL;
  27. }
  28. static void usbh_hid_class_free(struct usbh_hid *hid_class)
  29. {
  30. int devno = hid_class->minor;
  31. if (devno >= 0 && devno < 32) {
  32. g_devinuse &= ~(1 << devno);
  33. }
  34. memset(hid_class, 0, sizeof(struct usbh_hid));
  35. }
  36. static int usbh_hid_get_report_descriptor(struct usbh_hid *hid_class, uint8_t *buffer)
  37. {
  38. struct usb_setup_packet *setup = hid_class->hport->setup;
  39. int ret;
  40. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  41. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  42. setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
  43. setup->wIndex = hid_class->intf;
  44. setup->wLength = 128;
  45. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf);
  46. if (ret < 0) {
  47. return ret;
  48. }
  49. memcpy(buffer, g_hid_buf, ret - 8);
  50. return ret;
  51. }
  52. int usbh_hid_set_idle(struct usbh_hid *hid_class, uint8_t report_id, uint8_t duration)
  53. {
  54. struct usb_setup_packet *setup = hid_class->hport->setup;
  55. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  56. setup->bRequest = HID_REQUEST_SET_IDLE;
  57. setup->wValue = (duration << 8) | report_id;
  58. setup->wIndex = hid_class->intf;
  59. setup->wLength = 0;
  60. return usbh_control_transfer(hid_class->hport, setup, NULL);
  61. }
  62. int usbh_hid_get_idle(struct usbh_hid *hid_class, uint8_t *buffer)
  63. {
  64. struct usb_setup_packet *setup = hid_class->hport->setup;
  65. int ret;
  66. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  67. setup->bRequest = HID_REQUEST_GET_IDLE;
  68. setup->wValue = 0;
  69. setup->wIndex = hid_class->intf;
  70. setup->wLength = 1;
  71. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf);
  72. if (ret < 0) {
  73. return ret;
  74. }
  75. memcpy(buffer, g_hid_buf, 1);
  76. return ret;
  77. }
  78. int usbh_hid_set_protocol(struct usbh_hid *hid_class, uint8_t protocol)
  79. {
  80. struct usb_setup_packet *setup = hid_class->hport->setup;
  81. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  82. setup->bRequest = HID_REQUEST_SET_PROTOCOL;
  83. setup->wValue = protocol;
  84. setup->wIndex = 0;
  85. setup->wLength = 0;
  86. return usbh_control_transfer(hid_class->hport, setup, NULL);
  87. }
  88. int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
  89. {
  90. struct usb_setup_packet *setup = hid_class->hport->setup;
  91. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  92. setup->bRequest = HID_REQUEST_SET_REPORT;
  93. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  94. setup->wIndex = 0;
  95. setup->wLength = buflen;
  96. return usbh_control_transfer(hid_class->hport, setup, buffer);
  97. }
  98. int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
  99. {
  100. struct usb_setup_packet *setup = hid_class->hport->setup;
  101. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  102. setup->bRequest = HID_REQUEST_GET_REPORT;
  103. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  104. setup->wIndex = 0;
  105. setup->wLength = buflen;
  106. return usbh_control_transfer(hid_class->hport, setup, buffer);
  107. }
  108. int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
  109. {
  110. struct usb_endpoint_descriptor *ep_desc;
  111. int ret;
  112. struct usbh_hid *hid_class = usbh_hid_class_alloc();
  113. if (hid_class == NULL) {
  114. USB_LOG_ERR("Fail to alloc hid_class\r\n");
  115. return -USB_ERR_NOMEM;
  116. }
  117. hid_class->hport = hport;
  118. hid_class->intf = intf;
  119. hport->config.intf[intf].priv = hid_class;
  120. // /* 0x0 = boot protocol, 0x1 = report protocol */
  121. // ret = usbh_hid_set_protocol(hid_class, 0x1);
  122. // if (ret < 0) {
  123. // return ret;
  124. // }
  125. ret = usbh_hid_set_idle(hid_class, 0, 0);
  126. if (ret < 0) {
  127. USB_LOG_WRN("Do not support set idle\r\n");
  128. }
  129. ret = usbh_hid_get_report_descriptor(hid_class, hid_class->report_desc);
  130. if (ret < 0) {
  131. return ret;
  132. }
  133. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  134. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  135. if (ep_desc->bEndpointAddress & 0x80) {
  136. USBH_EP_INIT(hid_class->intin, ep_desc);
  137. } else {
  138. USBH_EP_INIT(hid_class->intout, ep_desc);
  139. }
  140. }
  141. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hid_class->minor);
  142. USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname);
  143. usbh_hid_run(hid_class);
  144. return ret;
  145. }
  146. int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
  147. {
  148. int ret = 0;
  149. struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
  150. if (hid_class) {
  151. if (hid_class->intin) {
  152. usbh_kill_urb(&hid_class->intin_urb);
  153. }
  154. if (hid_class->intout) {
  155. usbh_kill_urb(&hid_class->intout_urb);
  156. }
  157. if (hport->config.intf[intf].devname[0] != '\0') {
  158. USB_LOG_INFO("Unregister HID Class:%s\r\n", hport->config.intf[intf].devname);
  159. usbh_hid_stop(hid_class);
  160. }
  161. usbh_hid_class_free(hid_class);
  162. }
  163. return ret;
  164. }
  165. __WEAK void usbh_hid_run(struct usbh_hid *hid_class)
  166. {
  167. }
  168. __WEAK void usbh_hid_stop(struct usbh_hid *hid_class)
  169. {
  170. }
  171. const struct usbh_class_driver hid_class_driver = {
  172. .driver_name = "hid",
  173. .connect = usbh_hid_connect,
  174. .disconnect = usbh_hid_disconnect
  175. };
  176. CLASS_INFO_DEFINE const struct usbh_class_info hid_custom_class_info = {
  177. .match_flags = USB_CLASS_MATCH_INTF_CLASS,
  178. .class = USB_DEVICE_CLASS_HID,
  179. .subclass = 0x00,
  180. .protocol = 0x00,
  181. .id_table = NULL,
  182. .class_driver = &hid_class_driver
  183. };