usbh_hid.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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(256, 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. int devno;
  24. for (devno = 0; devno < CONFIG_USBHOST_MAX_HID_CLASS; devno++) {
  25. if ((g_devinuse & (1 << devno)) == 0) {
  26. g_devinuse |= (1 << 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. int devno = hid_class->minor;
  37. if (devno >= 0 && devno < 32) {
  38. g_devinuse &= ~(1 << devno);
  39. }
  40. memset(hid_class, 0, sizeof(struct usbh_hid));
  41. }
  42. static int usbh_hid_get_report_descriptor(struct usbh_hid *hid_class, uint8_t *buffer)
  43. {
  44. struct usb_setup_packet *setup;
  45. int ret;
  46. if (!hid_class || !hid_class->hport) {
  47. return -USB_ERR_INVAL;
  48. }
  49. setup = hid_class->hport->setup;
  50. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  51. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  52. setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
  53. setup->wIndex = hid_class->intf;
  54. setup->wLength = hid_class->report_size;
  55. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf[hid_class->minor]);
  56. if (ret < 0) {
  57. return ret;
  58. }
  59. memcpy(buffer, g_hid_buf[hid_class->minor], ret - 8);
  60. return ret;
  61. }
  62. int usbh_hid_set_idle(struct usbh_hid *hid_class, uint8_t report_id, uint8_t duration)
  63. {
  64. struct usb_setup_packet *setup;
  65. if (!hid_class || !hid_class->hport) {
  66. return -USB_ERR_INVAL;
  67. }
  68. setup = hid_class->hport->setup;
  69. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  70. setup->bRequest = HID_REQUEST_SET_IDLE;
  71. setup->wValue = (duration << 8) | report_id;
  72. setup->wIndex = hid_class->intf;
  73. setup->wLength = 0;
  74. return usbh_control_transfer(hid_class->hport, setup, NULL);
  75. }
  76. int usbh_hid_get_idle(struct usbh_hid *hid_class, uint8_t *buffer)
  77. {
  78. struct usb_setup_packet *setup;
  79. int ret;
  80. if (!hid_class || !hid_class->hport) {
  81. return -USB_ERR_INVAL;
  82. }
  83. setup = hid_class->hport->setup;
  84. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  85. setup->bRequest = HID_REQUEST_GET_IDLE;
  86. setup->wValue = 0;
  87. setup->wIndex = hid_class->intf;
  88. setup->wLength = 1;
  89. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf[hid_class->minor]);
  90. if (ret < 0) {
  91. return ret;
  92. }
  93. memcpy(buffer, g_hid_buf[hid_class->minor], ret - 8);
  94. return ret;
  95. }
  96. int usbh_hid_set_protocol(struct usbh_hid *hid_class, uint8_t protocol)
  97. {
  98. struct usb_setup_packet *setup;
  99. if (!hid_class || !hid_class->hport) {
  100. return -USB_ERR_INVAL;
  101. }
  102. setup = hid_class->hport->setup;
  103. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  104. setup->bRequest = HID_REQUEST_SET_PROTOCOL;
  105. setup->wValue = protocol;
  106. setup->wIndex = 0;
  107. setup->wLength = 0;
  108. return usbh_control_transfer(hid_class->hport, setup, NULL);
  109. }
  110. int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
  111. {
  112. struct usb_setup_packet *setup;
  113. if (!hid_class || !hid_class->hport) {
  114. return -USB_ERR_INVAL;
  115. }
  116. setup = hid_class->hport->setup;
  117. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  118. setup->bRequest = HID_REQUEST_SET_REPORT;
  119. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  120. setup->wIndex = 0;
  121. setup->wLength = buflen;
  122. return usbh_control_transfer(hid_class->hport, setup, buffer);
  123. }
  124. int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
  125. {
  126. struct usb_setup_packet *setup;
  127. int ret;
  128. if (!hid_class || !hid_class->hport) {
  129. return -USB_ERR_INVAL;
  130. }
  131. setup = hid_class->hport->setup;
  132. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  133. setup->bRequest = HID_REQUEST_GET_REPORT;
  134. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  135. setup->wIndex = 0;
  136. setup->wLength = buflen;
  137. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf[hid_class->minor]);
  138. if (ret < 0) {
  139. return ret;
  140. }
  141. memcpy(buffer, g_hid_buf[hid_class->minor], ret - 8);
  142. return ret;
  143. }
  144. int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
  145. {
  146. struct usb_endpoint_descriptor *ep_desc;
  147. int ret;
  148. uint8_t cur_iface = 0xff;
  149. uint8_t *p;
  150. bool found = false;
  151. struct usbh_hid *hid_class = usbh_hid_class_alloc();
  152. if (hid_class == NULL) {
  153. USB_LOG_ERR("Fail to alloc hid_class\r\n");
  154. return -USB_ERR_NOMEM;
  155. }
  156. hid_class->hport = hport;
  157. hid_class->intf = intf;
  158. hport->config.intf[intf].priv = hid_class;
  159. p = hport->raw_config_desc;
  160. while (p[DESC_bLength]) {
  161. switch (p[DESC_bDescriptorType]) {
  162. case USB_DESCRIPTOR_TYPE_INTERFACE:
  163. cur_iface = p[INTF_DESC_bInterfaceNumber];
  164. if (cur_iface == intf) {
  165. hid_class->protocol = p[7];
  166. struct usb_hid_descriptor *desc = (struct usb_hid_descriptor *)(p + 9);
  167. if (desc->bDescriptorType != HID_DESCRIPTOR_TYPE_HID) {
  168. USB_LOG_ERR("HID descriptor not found\r\n");
  169. return -USB_ERR_INVAL;
  170. }
  171. if (desc->subdesc[0].bDescriptorType != HID_DESCRIPTOR_TYPE_HID_REPORT) {
  172. USB_LOG_ERR("HID report descriptor not found\r\n");
  173. return -USB_ERR_INVAL;
  174. }
  175. hid_class->report_size = desc->subdesc[0].wDescriptorLength;
  176. if (hid_class->report_size > sizeof(g_hid_buf[hid_class->minor])) {
  177. USB_LOG_ERR("HID report descriptor too large\r\n");
  178. return -USB_ERR_INVAL;
  179. }
  180. found = true;
  181. goto found;
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. /* skip to next descriptor */
  188. p += p[DESC_bLength];
  189. }
  190. if (found == false) {
  191. USB_LOG_ERR("HID interface not found\r\n");
  192. return -USB_ERR_INVAL;
  193. }
  194. found:
  195. // /* 0x0 = boot protocol, 0x1 = report protocol */
  196. // ret = usbh_hid_set_protocol(hid_class, 0x1);
  197. // if (ret < 0) {
  198. // return ret;
  199. // }
  200. ret = usbh_hid_set_idle(hid_class, 0, 0);
  201. if (ret < 0) {
  202. USB_LOG_WRN("Do not support set idle\r\n");
  203. }
  204. ret = usbh_hid_get_report_descriptor(hid_class, hid_class->report_desc);
  205. if (ret < 0) {
  206. return ret;
  207. }
  208. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  209. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  210. if (ep_desc->bEndpointAddress & 0x80) {
  211. USBH_EP_INIT(hid_class->intin, ep_desc);
  212. } else {
  213. USBH_EP_INIT(hid_class->intout, ep_desc);
  214. }
  215. }
  216. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hid_class->minor);
  217. USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname);
  218. usbh_hid_run(hid_class);
  219. return ret;
  220. }
  221. int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
  222. {
  223. int ret = 0;
  224. struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
  225. if (hid_class) {
  226. if (hid_class->intin) {
  227. usbh_kill_urb(&hid_class->intin_urb);
  228. }
  229. if (hid_class->intout) {
  230. usbh_kill_urb(&hid_class->intout_urb);
  231. }
  232. if (hport->config.intf[intf].devname[0] != '\0') {
  233. USB_LOG_INFO("Unregister HID Class:%s\r\n", hport->config.intf[intf].devname);
  234. usbh_hid_stop(hid_class);
  235. }
  236. usbh_hid_class_free(hid_class);
  237. }
  238. return ret;
  239. }
  240. __WEAK void usbh_hid_run(struct usbh_hid *hid_class)
  241. {
  242. (void)hid_class;
  243. }
  244. __WEAK void usbh_hid_stop(struct usbh_hid *hid_class)
  245. {
  246. (void)hid_class;
  247. }
  248. const struct usbh_class_driver hid_class_driver = {
  249. .driver_name = "hid",
  250. .connect = usbh_hid_connect,
  251. .disconnect = usbh_hid_disconnect
  252. };
  253. CLASS_INFO_DEFINE const struct usbh_class_info hid_custom_class_info = {
  254. .match_flags = USB_CLASS_MATCH_INTF_CLASS,
  255. .class = USB_DEVICE_CLASS_HID,
  256. .subclass = 0x00,
  257. .protocol = 0x00,
  258. .id_table = NULL,
  259. .class_driver = &hid_class_driver
  260. };