usbh_hid.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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;
  39. int ret;
  40. if (!hid_class || !hid_class->hport) {
  41. return -USB_ERR_INVAL;
  42. }
  43. setup = hid_class->hport->setup;
  44. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  45. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  46. setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
  47. setup->wIndex = hid_class->intf;
  48. setup->wLength = 128;
  49. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf);
  50. if (ret < 0) {
  51. return ret;
  52. }
  53. memcpy(buffer, g_hid_buf, ret - 8);
  54. return ret;
  55. }
  56. int usbh_hid_set_idle(struct usbh_hid *hid_class, uint8_t report_id, uint8_t duration)
  57. {
  58. struct usb_setup_packet *setup;
  59. if (!hid_class || !hid_class->hport) {
  60. return -USB_ERR_INVAL;
  61. }
  62. setup = hid_class->hport->setup;
  63. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  64. setup->bRequest = HID_REQUEST_SET_IDLE;
  65. setup->wValue = (duration << 8) | report_id;
  66. setup->wIndex = hid_class->intf;
  67. setup->wLength = 0;
  68. return usbh_control_transfer(hid_class->hport, setup, NULL);
  69. }
  70. int usbh_hid_get_idle(struct usbh_hid *hid_class, uint8_t *buffer)
  71. {
  72. struct usb_setup_packet *setup;
  73. int ret;
  74. if (!hid_class || !hid_class->hport) {
  75. return -USB_ERR_INVAL;
  76. }
  77. setup = hid_class->hport->setup;
  78. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  79. setup->bRequest = HID_REQUEST_GET_IDLE;
  80. setup->wValue = 0;
  81. setup->wIndex = hid_class->intf;
  82. setup->wLength = 1;
  83. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf);
  84. if (ret < 0) {
  85. return ret;
  86. }
  87. memcpy(buffer, g_hid_buf, 1);
  88. return ret;
  89. }
  90. int usbh_hid_set_protocol(struct usbh_hid *hid_class, uint8_t protocol)
  91. {
  92. struct usb_setup_packet *setup;
  93. if (!hid_class || !hid_class->hport) {
  94. return -USB_ERR_INVAL;
  95. }
  96. setup = hid_class->hport->setup;
  97. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  98. setup->bRequest = HID_REQUEST_SET_PROTOCOL;
  99. setup->wValue = protocol;
  100. setup->wIndex = 0;
  101. setup->wLength = 0;
  102. return usbh_control_transfer(hid_class->hport, setup, NULL);
  103. }
  104. int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
  105. {
  106. struct usb_setup_packet *setup;
  107. if (!hid_class || !hid_class->hport) {
  108. return -USB_ERR_INVAL;
  109. }
  110. setup = hid_class->hport->setup;
  111. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  112. setup->bRequest = HID_REQUEST_SET_REPORT;
  113. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  114. setup->wIndex = 0;
  115. setup->wLength = buflen;
  116. return usbh_control_transfer(hid_class->hport, setup, buffer);
  117. }
  118. int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
  119. {
  120. struct usb_setup_packet *setup;
  121. if (!hid_class || !hid_class->hport) {
  122. return -USB_ERR_INVAL;
  123. }
  124. setup = hid_class->hport->setup;
  125. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  126. setup->bRequest = HID_REQUEST_GET_REPORT;
  127. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  128. setup->wIndex = 0;
  129. setup->wLength = buflen;
  130. return usbh_control_transfer(hid_class->hport, setup, buffer);
  131. }
  132. int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
  133. {
  134. struct usb_endpoint_descriptor *ep_desc;
  135. int ret;
  136. struct usbh_hid *hid_class = usbh_hid_class_alloc();
  137. if (hid_class == NULL) {
  138. USB_LOG_ERR("Fail to alloc hid_class\r\n");
  139. return -USB_ERR_NOMEM;
  140. }
  141. hid_class->hport = hport;
  142. hid_class->intf = intf;
  143. hport->config.intf[intf].priv = hid_class;
  144. // /* 0x0 = boot protocol, 0x1 = report protocol */
  145. // ret = usbh_hid_set_protocol(hid_class, 0x1);
  146. // if (ret < 0) {
  147. // return ret;
  148. // }
  149. ret = usbh_hid_set_idle(hid_class, 0, 0);
  150. if (ret < 0) {
  151. USB_LOG_WRN("Do not support set idle\r\n");
  152. }
  153. ret = usbh_hid_get_report_descriptor(hid_class, hid_class->report_desc);
  154. if (ret < 0) {
  155. return ret;
  156. }
  157. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  158. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  159. if (ep_desc->bEndpointAddress & 0x80) {
  160. USBH_EP_INIT(hid_class->intin, ep_desc);
  161. } else {
  162. USBH_EP_INIT(hid_class->intout, ep_desc);
  163. }
  164. }
  165. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hid_class->minor);
  166. USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname);
  167. usbh_hid_run(hid_class);
  168. return ret;
  169. }
  170. int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
  171. {
  172. int ret = 0;
  173. struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
  174. if (hid_class) {
  175. if (hid_class->intin) {
  176. usbh_kill_urb(&hid_class->intin_urb);
  177. }
  178. if (hid_class->intout) {
  179. usbh_kill_urb(&hid_class->intout_urb);
  180. }
  181. if (hport->config.intf[intf].devname[0] != '\0') {
  182. USB_LOG_INFO("Unregister HID Class:%s\r\n", hport->config.intf[intf].devname);
  183. usbh_hid_stop(hid_class);
  184. }
  185. usbh_hid_class_free(hid_class);
  186. }
  187. return ret;
  188. }
  189. __WEAK void usbh_hid_run(struct usbh_hid *hid_class)
  190. {
  191. }
  192. __WEAK void usbh_hid_stop(struct usbh_hid *hid_class)
  193. {
  194. }
  195. const struct usbh_class_driver hid_class_driver = {
  196. .driver_name = "hid",
  197. .connect = usbh_hid_connect,
  198. .disconnect = usbh_hid_disconnect
  199. };
  200. CLASS_INFO_DEFINE const struct usbh_class_info hid_custom_class_info = {
  201. .match_flags = USB_CLASS_MATCH_INTF_CLASS,
  202. .class = USB_DEVICE_CLASS_HID,
  203. .subclass = 0x00,
  204. .protocol = 0x00,
  205. .id_table = NULL,
  206. .class_driver = &hid_class_driver
  207. };