usbh_hid.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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], MIN(ret - 8, 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_get_protocol(struct usbh_hid *hid_class, uint8_t *protocol)
  105. {
  106. struct usb_setup_packet *setup;
  107. int ret;
  108. if (!hid_class || !hid_class->hport) {
  109. return -USB_ERR_INVAL;
  110. }
  111. setup = hid_class->hport->setup;
  112. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  113. setup->bRequest = HID_REQUEST_GET_PROTOCOL;
  114. setup->wValue = 0;
  115. setup->wIndex = hid_class->intf;
  116. setup->wLength = 1;
  117. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf[hid_class->minor]);
  118. if (ret < 8) {
  119. return ret;
  120. }
  121. memcpy(protocol, g_hid_buf[hid_class->minor], MIN(ret - 8, 1));
  122. return ret;
  123. }
  124. int usbh_hid_set_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. if (!hid_class || !hid_class->hport) {
  128. return -USB_ERR_INVAL;
  129. }
  130. setup = hid_class->hport->setup;
  131. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  132. setup->bRequest = HID_REQUEST_SET_REPORT;
  133. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  134. setup->wIndex = 0;
  135. setup->wLength = buflen;
  136. return usbh_control_transfer(hid_class->hport, setup, buffer);
  137. }
  138. int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
  139. {
  140. struct usb_setup_packet *setup;
  141. int ret;
  142. if (!hid_class || !hid_class->hport) {
  143. return -USB_ERR_INVAL;
  144. }
  145. setup = hid_class->hport->setup;
  146. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  147. setup->bRequest = HID_REQUEST_GET_REPORT;
  148. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  149. setup->wIndex = 0;
  150. setup->wLength = buflen;
  151. ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf[hid_class->minor]);
  152. if (ret < 8) {
  153. return ret;
  154. }
  155. memcpy(buffer, g_hid_buf[hid_class->minor], MIN(ret - 8, buflen));
  156. return ret;
  157. }
  158. int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
  159. {
  160. struct usb_endpoint_descriptor *ep_desc;
  161. int ret;
  162. uint8_t cur_iface = 0xff;
  163. uint8_t *p;
  164. bool found = false;
  165. struct usbh_hid *hid_class = usbh_hid_class_alloc();
  166. if (hid_class == NULL) {
  167. USB_LOG_ERR("Fail to alloc hid_class\r\n");
  168. return -USB_ERR_NOMEM;
  169. }
  170. hid_class->hport = hport;
  171. hid_class->intf = intf;
  172. hport->config.intf[intf].priv = hid_class;
  173. p = hport->raw_config_desc;
  174. while (p[DESC_bLength]) {
  175. switch (p[DESC_bDescriptorType]) {
  176. case USB_DESCRIPTOR_TYPE_INTERFACE:
  177. cur_iface = p[INTF_DESC_bInterfaceNumber];
  178. if (cur_iface == intf) {
  179. hid_class->protocol = p[7];
  180. struct usb_hid_descriptor *desc = (struct usb_hid_descriptor *)(p + 9);
  181. if (desc->bDescriptorType != HID_DESCRIPTOR_TYPE_HID) {
  182. USB_LOG_ERR("HID descriptor not found\r\n");
  183. return -USB_ERR_INVAL;
  184. }
  185. if (desc->subdesc[0].bDescriptorType != HID_DESCRIPTOR_TYPE_HID_REPORT) {
  186. USB_LOG_ERR("HID report descriptor not found\r\n");
  187. return -USB_ERR_INVAL;
  188. }
  189. hid_class->report_size = desc->subdesc[0].wDescriptorLength;
  190. found = true;
  191. goto found;
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. /* skip to next descriptor */
  198. p += p[DESC_bLength];
  199. }
  200. if (found == false) {
  201. USB_LOG_ERR("HID interface not found\r\n");
  202. return -USB_ERR_INVAL;
  203. }
  204. found:
  205. // /* 0x0 = boot protocol, 0x1 = report protocol */
  206. // ret = usbh_hid_set_protocol(hid_class, 0x1);
  207. // if (ret < 0) {
  208. // return ret;
  209. // }
  210. ret = usbh_hid_set_idle(hid_class, 0, 0);
  211. if (ret < 0) {
  212. USB_LOG_WRN("Do not support set idle\r\n");
  213. }
  214. /* We read report desc but do nothing (because of too much memory usage for parsing report desc, parsed by users) */
  215. 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));
  216. if (ret < 0) {
  217. return ret;
  218. }
  219. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  220. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  221. if (ep_desc->bEndpointAddress & 0x80) {
  222. USBH_EP_INIT(hid_class->intin, ep_desc);
  223. } else {
  224. USBH_EP_INIT(hid_class->intout, ep_desc);
  225. }
  226. }
  227. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hid_class->minor);
  228. USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname);
  229. usbh_hid_run(hid_class);
  230. return ret;
  231. }
  232. int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
  233. {
  234. int ret = 0;
  235. struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
  236. if (hid_class) {
  237. if (hid_class->intin) {
  238. usbh_kill_urb(&hid_class->intin_urb);
  239. }
  240. if (hid_class->intout) {
  241. usbh_kill_urb(&hid_class->intout_urb);
  242. }
  243. if (hport->config.intf[intf].devname[0] != '\0') {
  244. usb_osal_thread_schedule_other();
  245. USB_LOG_INFO("Unregister HID Class:%s\r\n", hport->config.intf[intf].devname);
  246. usbh_hid_stop(hid_class);
  247. }
  248. usbh_hid_class_free(hid_class);
  249. }
  250. return ret;
  251. }
  252. __WEAK void usbh_hid_run(struct usbh_hid *hid_class)
  253. {
  254. (void)hid_class;
  255. }
  256. __WEAK void usbh_hid_stop(struct usbh_hid *hid_class)
  257. {
  258. (void)hid_class;
  259. }
  260. const struct usbh_class_driver hid_class_driver = {
  261. .driver_name = "hid",
  262. .connect = usbh_hid_connect,
  263. .disconnect = usbh_hid_disconnect
  264. };
  265. CLASS_INFO_DEFINE const struct usbh_class_info hid_custom_class_info = {
  266. .match_flags = USB_CLASS_MATCH_INTF_CLASS,
  267. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  268. .bInterfaceSubClass = 0x00,
  269. .bInterfaceProtocol = 0x00,
  270. .id_table = NULL,
  271. .class_driver = &hid_class_driver
  272. };