usbh_hid.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /* general descriptor field offsets */
  13. #define DESC_bLength 0 /** Length offset */
  14. #define DESC_bDescriptorType 1 /** Descriptor type offset */
  15. /* interface descriptor field offsets */
  16. #define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
  17. #define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
  18. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_buf[CONFIG_USBHOST_MAX_HID_CLASS][USB_ALIGN_UP(64, CONFIG_USB_ALIGN_SIZE)];
  19. static struct usbh_hid g_hid_class[CONFIG_USBHOST_MAX_HID_CLASS];
  20. static uint32_t g_devinuse = 0;
  21. static struct usbh_hid *usbh_hid_class_alloc(void)
  22. {
  23. uint8_t devno;
  24. for (devno = 0; devno < CONFIG_USBHOST_MAX_HID_CLASS; devno++) {
  25. if ((g_devinuse & (1U << devno)) == 0) {
  26. g_devinuse |= (1U << devno);
  27. memset(&g_hid_class[devno], 0, sizeof(struct usbh_hid));
  28. g_hid_class[devno].minor = devno;
  29. return &g_hid_class[devno];
  30. }
  31. }
  32. return NULL;
  33. }
  34. static void usbh_hid_class_free(struct usbh_hid *hid_class)
  35. {
  36. uint8_t devno = hid_class->minor;
  37. if (devno < 32) {
  38. g_devinuse &= ~(1U << devno);
  39. }
  40. memset(hid_class, 0, sizeof(struct usbh_hid));
  41. }
  42. int usbh_hid_get_report_descriptor(struct usbh_hid *hid_class, uint8_t *buffer, uint32_t buflen)
  43. {
  44. struct usb_setup_packet *setup;
  45. if (!hid_class || !hid_class->hport) {
  46. return -USB_ERR_INVAL;
  47. }
  48. setup = hid_class->hport->setup;
  49. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  50. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  51. setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
  52. setup->wIndex = hid_class->intf;
  53. setup->wLength = buflen;
  54. return usbh_control_transfer(hid_class->hport, setup, buffer);
  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[hid_class->minor]);
  84. if (ret < 8) {
  85. return ret;
  86. }
  87. memcpy(buffer, g_hid_buf[hid_class->minor], ret - 8);
  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. int ret;
  122. if (!hid_class || !hid_class->hport) {
  123. return -USB_ERR_INVAL;
  124. }
  125. setup = hid_class->hport->setup;
  126. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  127. setup->bRequest = HID_REQUEST_GET_REPORT;
  128. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  129. setup->wIndex = 0;
  130. setup->wLength = buflen;
  131. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf[hid_class->minor]);
  132. if (ret < 8) {
  133. return ret;
  134. }
  135. memcpy(buffer, g_hid_buf[hid_class->minor], ret - 8);
  136. return ret;
  137. }
  138. int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
  139. {
  140. struct usb_endpoint_descriptor *ep_desc;
  141. int ret;
  142. uint8_t cur_iface = 0xff;
  143. uint8_t *p;
  144. bool found = false;
  145. struct usbh_hid *hid_class = usbh_hid_class_alloc();
  146. if (hid_class == NULL) {
  147. USB_LOG_ERR("Fail to alloc hid_class\r\n");
  148. return -USB_ERR_NOMEM;
  149. }
  150. hid_class->hport = hport;
  151. hid_class->intf = intf;
  152. hport->config.intf[intf].priv = hid_class;
  153. p = hport->raw_config_desc;
  154. while (p[DESC_bLength]) {
  155. switch (p[DESC_bDescriptorType]) {
  156. case USB_DESCRIPTOR_TYPE_INTERFACE:
  157. cur_iface = p[INTF_DESC_bInterfaceNumber];
  158. if (cur_iface == intf) {
  159. hid_class->protocol = p[7];
  160. struct usb_hid_descriptor *desc = (struct usb_hid_descriptor *)(p + 9);
  161. if (desc->bDescriptorType != HID_DESCRIPTOR_TYPE_HID) {
  162. USB_LOG_ERR("HID descriptor not found\r\n");
  163. return -USB_ERR_INVAL;
  164. }
  165. if (desc->subdesc[0].bDescriptorType != HID_DESCRIPTOR_TYPE_HID_REPORT) {
  166. USB_LOG_ERR("HID report descriptor not found\r\n");
  167. return -USB_ERR_INVAL;
  168. }
  169. hid_class->report_size = desc->subdesc[0].wDescriptorLength;
  170. found = true;
  171. goto found;
  172. }
  173. break;
  174. default:
  175. break;
  176. }
  177. /* skip to next descriptor */
  178. p += p[DESC_bLength];
  179. }
  180. if (found == false) {
  181. USB_LOG_ERR("HID interface not found\r\n");
  182. return -USB_ERR_INVAL;
  183. }
  184. found:
  185. // /* 0x0 = boot protocol, 0x1 = report protocol */
  186. // ret = usbh_hid_set_protocol(hid_class, 0x1);
  187. // if (ret < 0) {
  188. // return ret;
  189. // }
  190. ret = usbh_hid_set_idle(hid_class, 0, 0);
  191. if (ret < 0) {
  192. USB_LOG_WRN("Do not support set idle\r\n");
  193. }
  194. /* We read report desc but do nothing (because of too much memory usage for parsing report desc, parsed by users) */
  195. ret = usbh_hid_get_report_descriptor(hid_class, g_hid_buf[hid_class->minor], MIN(sizeof(g_hid_buf[hid_class->minor]), hid_class->report_size));
  196. if (ret < 0) {
  197. return ret;
  198. }
  199. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  200. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  201. if (ep_desc->bEndpointAddress & 0x80) {
  202. USBH_EP_INIT(hid_class->intin, ep_desc);
  203. } else {
  204. USBH_EP_INIT(hid_class->intout, ep_desc);
  205. }
  206. }
  207. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hid_class->minor);
  208. USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname);
  209. usbh_hid_run(hid_class);
  210. return ret;
  211. }
  212. int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
  213. {
  214. int ret = 0;
  215. struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
  216. if (hid_class) {
  217. if (hid_class->intin) {
  218. usbh_kill_urb(&hid_class->intin_urb);
  219. }
  220. if (hid_class->intout) {
  221. usbh_kill_urb(&hid_class->intout_urb);
  222. }
  223. if (hport->config.intf[intf].devname[0] != '\0') {
  224. USB_LOG_INFO("Unregister HID Class:%s\r\n", hport->config.intf[intf].devname);
  225. usbh_hid_stop(hid_class);
  226. }
  227. usbh_hid_class_free(hid_class);
  228. }
  229. return ret;
  230. }
  231. __WEAK void usbh_hid_run(struct usbh_hid *hid_class)
  232. {
  233. (void)hid_class;
  234. }
  235. __WEAK void usbh_hid_stop(struct usbh_hid *hid_class)
  236. {
  237. (void)hid_class;
  238. }
  239. const struct usbh_class_driver hid_class_driver = {
  240. .driver_name = "hid",
  241. .connect = usbh_hid_connect,
  242. .disconnect = usbh_hid_disconnect
  243. };
  244. CLASS_INFO_DEFINE const struct usbh_class_info hid_custom_class_info = {
  245. .match_flags = USB_CLASS_MATCH_INTF_CLASS,
  246. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  247. .bInterfaceSubClass = 0x00,
  248. .bInterfaceProtocol = 0x00,
  249. .id_table = NULL,
  250. .class_driver = &hid_class_driver
  251. };