usbh_hid.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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(32, 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((uint32_t)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((uint32_t)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. if (!hid_class || !hid_class->hport) {
  142. return -USB_ERR_INVAL;
  143. }
  144. setup = hid_class->hport->setup;
  145. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  146. setup->bRequest = HID_REQUEST_GET_REPORT;
  147. setup->wValue = (uint16_t)(((uint32_t)report_type << 8U) | (uint32_t)report_id);
  148. setup->wIndex = 0;
  149. setup->wLength = buflen;
  150. return usbh_control_transfer(hid_class->hport, setup, buffer);
  151. }
  152. int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
  153. {
  154. struct usb_endpoint_descriptor *ep_desc;
  155. uint8_t cur_iface = 0xff;
  156. uint8_t *p;
  157. bool found = false;
  158. struct usbh_hid *hid_class = usbh_hid_class_alloc();
  159. if (hid_class == NULL) {
  160. USB_LOG_ERR("Fail to alloc hid_class\r\n");
  161. return -USB_ERR_NOMEM;
  162. }
  163. hid_class->hport = hport;
  164. hid_class->intf = intf;
  165. hport->config.intf[intf].priv = hid_class;
  166. p = hport->raw_config_desc;
  167. while (p[DESC_bLength]) {
  168. switch (p[DESC_bDescriptorType]) {
  169. case USB_DESCRIPTOR_TYPE_INTERFACE:
  170. cur_iface = p[INTF_DESC_bInterfaceNumber];
  171. if (cur_iface == intf) {
  172. hid_class->protocol = p[7];
  173. struct usb_hid_descriptor *desc = (struct usb_hid_descriptor *)(p + 9);
  174. if (desc->bDescriptorType != HID_DESCRIPTOR_TYPE_HID) {
  175. USB_LOG_ERR("HID descriptor not found\r\n");
  176. return -USB_ERR_INVAL;
  177. }
  178. if (desc->subdesc[0].bDescriptorType != HID_DESCRIPTOR_TYPE_HID_REPORT) {
  179. USB_LOG_ERR("HID report descriptor not found\r\n");
  180. return -USB_ERR_INVAL;
  181. }
  182. hid_class->report_size = desc->subdesc[0].wDescriptorLength;
  183. found = true;
  184. goto found;
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. /* skip to next descriptor */
  191. p += p[DESC_bLength];
  192. }
  193. if (found == false) {
  194. USB_LOG_ERR("HID interface not found\r\n");
  195. return -USB_ERR_INVAL;
  196. }
  197. found:
  198. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  199. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  200. if (ep_desc->bEndpointAddress & 0x80) {
  201. USBH_EP_INIT(hid_class->intin, ep_desc);
  202. } else {
  203. USBH_EP_INIT(hid_class->intout, ep_desc);
  204. }
  205. }
  206. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hid_class->minor);
  207. USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname);
  208. usbh_hid_run(hid_class);
  209. return 0;
  210. }
  211. int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
  212. {
  213. int ret = 0;
  214. struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
  215. if (hid_class) {
  216. if (hid_class->intin) {
  217. usbh_kill_urb(&hid_class->intin_urb);
  218. }
  219. if (hid_class->intout) {
  220. usbh_kill_urb(&hid_class->intout_urb);
  221. }
  222. if (hport->config.intf[intf].devname[0] != '\0') {
  223. usb_osal_thread_schedule_other();
  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. static uint32_t hid_get_itemval(const uint8_t *data, unsigned int idx, unsigned int size)
  232. {
  233. uint32_t value = 0;
  234. for (unsigned int i = 1; i <= size; i++)
  235. value |= data[idx + i] << (8 * (i - 1));
  236. return value;
  237. }
  238. int usbh_hid_parse_report_descriptor(const uint8_t *report_data, uint32_t report_size, struct usbh_hid_report_info *report_info)
  239. {
  240. struct usbh_hid_report_item_attribute current_item_attr = { 0 };
  241. struct usbh_hid_report_item *current_item = NULL;
  242. uint32_t itemtag, itemtype, itemsize, itemval;
  243. uint16_t temp_usage;
  244. uint32_t total_report_size[3] = { 0 }; /* input, output, feature */
  245. uint32_t i = 0;
  246. memset(report_info, 0, sizeof(struct usbh_hid_report_info));
  247. memset(&current_item_attr, 0, sizeof(struct usbh_hid_report_item_attribute));
  248. current_item_attr.usage_min = 0xffff;
  249. current_item_attr.usage_max = 0;
  250. while (i < report_size) {
  251. itemtag = report_data[i] & HID_TAG_MASK;
  252. itemtype = report_data[i] & HID_TYPE_MASK;
  253. itemsize = report_data[i] & HID_SIZE_MASK;
  254. if (itemsize == 3) /* HID spec: 6.2.2.2 - Short Items */
  255. itemsize = 4;
  256. itemval = hid_get_itemval(report_data, i, itemsize);
  257. USB_LOG_DBG("itemtype 0x%02x, itemtag 0x%02x, itemsize %d, itemval 0x%08x\r\n",
  258. itemtype, itemtag, itemsize, itemval);
  259. switch (itemtype) {
  260. case HID_ITEMTYPE_MAIN:
  261. switch (itemtag) {
  262. case HID_MAINITEM_TAG_INPUT:
  263. case HID_MAINITEM_TAG_OUTPUT:
  264. case HID_MAINITEM_TAG_FEATURE:
  265. if (report_info->report_item_count == CONFIG_USB_HID_MAX_REPORT_ITEMS) {
  266. goto err;
  267. }
  268. current_item = &report_info->report_items[report_info->report_item_count];
  269. current_item->report_flags = itemval;
  270. if (itemtag == HID_MAINITEM_TAG_INPUT) {
  271. current_item->report_type = HID_REPORT_INPUT;
  272. } else if (itemtag == HID_MAINITEM_TAG_OUTPUT) {
  273. current_item->report_type = HID_REPORT_OUTPUT;
  274. } else {
  275. current_item->report_type = HID_REPORT_FEATURE;
  276. }
  277. current_item->report_bit_offset = total_report_size[current_item->report_type - 1];
  278. total_report_size[current_item->report_type - 1] += current_item_attr.report_size * current_item_attr.report_count;
  279. memcpy(&current_item->attribute, &current_item_attr, sizeof(struct usbh_hid_report_item_attribute));
  280. report_info->report_item_count++;
  281. // reset for next item
  282. current_item_attr.usage_min = 0xffff;
  283. current_item_attr.usage_max = 0;
  284. break;
  285. case HID_MAINITEM_TAG_COLLECTION:
  286. // reset for next item
  287. current_item_attr.usage_min = 0xffff;
  288. current_item_attr.usage_max = 0;
  289. break;
  290. case HID_MAINITEM_TAG_ENDCOLLECTION:
  291. break;
  292. default:
  293. goto err;
  294. }
  295. break;
  296. case HID_ITEMTYPE_GLOBAL:
  297. switch (itemtag) {
  298. case HID_GLOBALITEM_TAG_USAGE_PAGE:
  299. current_item_attr.usage_page = (uint16_t)itemval;
  300. break;
  301. case HID_GLOBALITEM_TAG_LOGICAL_MIN:
  302. current_item_attr.logical_min = itemval;
  303. break;
  304. case HID_GLOBALITEM_TAG_LOGICAL_MAX:
  305. current_item_attr.logical_max = itemval;
  306. break;
  307. case HID_GLOBALITEM_TAG_PHYSICAL_MIN:
  308. current_item_attr.physical_min = itemval;
  309. break;
  310. case HID_GLOBALITEM_TAG_PHYSICAL_MAX:
  311. current_item_attr.physical_max = itemval;
  312. break;
  313. case HID_GLOBALITEM_TAG_UNIT_EXP:
  314. current_item_attr.unit_exponent = itemval;
  315. break;
  316. case HID_GLOBALITEM_TAG_UNIT:
  317. current_item_attr.unit = itemval;
  318. break;
  319. case HID_GLOBALITEM_TAG_REPORT_SIZE:
  320. current_item_attr.report_size = itemval;
  321. break;
  322. case HID_GLOBALITEM_TAG_REPORT_COUNT:
  323. current_item_attr.report_count = itemval;
  324. break;
  325. case HID_GLOBALITEM_TAG_REPORT_ID:
  326. current_item_attr.report_id = itemval;
  327. break;
  328. default:
  329. goto err;
  330. }
  331. break;
  332. case HID_ITEMTYPE_LOCAL:
  333. switch (itemtag) {
  334. case HID_LOCALITEM_TAG_USAGE:
  335. if (itemsize == 4) {
  336. temp_usage = (uint16_t)(itemval >> 16);
  337. } else {
  338. temp_usage = (uint16_t)itemval;
  339. }
  340. current_item_attr.usage_min = MIN(current_item_attr.usage_min, temp_usage);
  341. current_item_attr.usage_max = MAX(current_item_attr.usage_max, temp_usage);
  342. break;
  343. case HID_LOCALITEM_TAG_USAGE_MIN:
  344. current_item_attr.usage_min = (uint16_t)itemval;
  345. break;
  346. case HID_LOCALITEM_TAG_USAGE_MAX:
  347. current_item_attr.usage_max = (uint16_t)itemval;
  348. break;
  349. default:
  350. goto err;
  351. }
  352. break;
  353. default:
  354. goto err;
  355. }
  356. i += (1 + itemsize);
  357. }
  358. return 0;
  359. err:
  360. return -1;
  361. }
  362. static void usbh_hid_item_info_print(struct usbh_hid_report_item *item)
  363. {
  364. USB_LOG_RAW("Item Type: %s\r\n", (unsigned int)item->report_type == HID_REPORT_INPUT ? "Input" :
  365. (unsigned int)item->report_type == HID_REPORT_OUTPUT ? "Output" :
  366. "Feature");
  367. USB_LOG_RAW("Usage Page: 0x%04x\r\n", (unsigned int)item->attribute.usage_page);
  368. USB_LOG_RAW("Report ID: 0x%04x\r\n", (unsigned int)item->attribute.report_id);
  369. USB_LOG_RAW("Report Size: %ubit\r\n", (unsigned int)item->attribute.report_size);
  370. USB_LOG_RAW("Report Count: %u\r\n", (unsigned int)item->attribute.report_count);
  371. USB_LOG_RAW("Usages(0x%04x ~ 0x%04x)\r\n", (unsigned int)item->attribute.usage_min, (unsigned int)item->attribute.usage_max);
  372. USB_LOG_RAW("Logical Min: %d\r\n", item->attribute.logical_min);
  373. USB_LOG_RAW("Logical Max: %d\r\n", item->attribute.logical_max);
  374. USB_LOG_RAW("Properties: 0x%04x\r\n", (unsigned int)item->report_flags);
  375. USB_LOG_RAW("Bit Offset: 0x%04x\r\n", (unsigned int)item->report_bit_offset);
  376. USB_LOG_RAW("\r\n");
  377. }
  378. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_report_buf[2048];
  379. int lshid(int argc, char **argv)
  380. {
  381. struct usbh_hid *hid_class;
  382. int ret;
  383. struct usbh_hid_report_info report_info = { 0 };
  384. if (argc < 2) {
  385. USB_LOG_ERR("please input correct command: lshid path\r\n");
  386. return -1;
  387. }
  388. hid_class = usbh_find_class_instance(argv[1]);
  389. if (!hid_class) {
  390. USB_LOG_ERR("cannot find hid device\r\n");
  391. return -1;
  392. }
  393. if (hid_class->report_size > sizeof(g_hid_report_buf)) {
  394. USB_LOG_ERR("hid report buffer is too small\r\n");
  395. return -1;
  396. }
  397. ret = usbh_hid_get_report_descriptor(hid_class, g_hid_report_buf, hid_class->report_size);
  398. if (ret < 0) {
  399. USB_LOG_ERR("get hid report descriptor failed, errcode: %d\r\n", ret);
  400. return -1;
  401. }
  402. ret = usbh_hid_parse_report_descriptor(g_hid_report_buf, hid_class->report_size, &report_info);
  403. if (ret < 0) {
  404. USB_LOG_ERR("parse hid report descriptor failed\r\n");
  405. return -1;
  406. }
  407. USB_LOG_INFO("HID report item count: %u\r\n", report_info.report_item_count);
  408. for (uint32_t i = 0; i < report_info.report_item_count; i++) {
  409. usbh_hid_item_info_print(&report_info.report_items[i]);
  410. }
  411. return 0;
  412. }
  413. __WEAK void usbh_hid_run(struct usbh_hid *hid_class)
  414. {
  415. (void)hid_class;
  416. }
  417. __WEAK void usbh_hid_stop(struct usbh_hid *hid_class)
  418. {
  419. (void)hid_class;
  420. }
  421. const struct usbh_class_driver hid_class_driver = {
  422. .driver_name = "hid",
  423. .connect = usbh_hid_connect,
  424. .disconnect = usbh_hid_disconnect
  425. };
  426. CLASS_INFO_DEFINE const struct usbh_class_info hid_custom_class_info = {
  427. .match_flags = USB_CLASS_MATCH_INTF_CLASS,
  428. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  429. .bInterfaceSubClass = 0x00,
  430. .bInterfaceProtocol = 0x00,
  431. .id_table = NULL,
  432. .class_driver = &hid_class_driver
  433. };