usbh_hid.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. struct hid_report *usbh_hid_report_parse(const uint8_t *data, uint32_t report_len, uint32_t max_usages)
  239. {
  240. uint32_t i = 0;
  241. uint32_t itemtag, itemtype, itemsize;
  242. uint32_t itemval;
  243. struct hid_report_field field;
  244. uint32_t usage_page = 0, usage = 0, usage_min = 0, usage_max = 0, flags = 0;
  245. uint32_t *usages;
  246. struct hid_report *hid_report;
  247. hid_report = usb_osal_malloc(sizeof(struct hid_report));
  248. if (!hid_report) {
  249. USB_LOG_ERR("hid report malloc failed\r\n");
  250. return NULL;
  251. }
  252. usages = usb_osal_malloc(sizeof(uint32_t) * max_usages);
  253. if (!usages) {
  254. USB_LOG_ERR("hid usages malloc failed\r\n");
  255. goto err;
  256. }
  257. memset(hid_report, 0, sizeof(struct hid_report));
  258. memset(&field, 0, sizeof(struct hid_report_field));
  259. while (i < report_len) {
  260. itemtag = data[i] & HID_TAG_MASK;
  261. itemtype = data[i] & HID_TYPE_MASK;
  262. itemsize = data[i] & HID_SIZE_MASK;
  263. if (itemsize == 3) /* HID spec: 6.2.2.2 - Short Items */
  264. itemsize = 4;
  265. itemval = hid_get_itemval(data, i, itemsize);
  266. USB_LOG_DBG("itemtype 0x%02x, itemtag 0x%02x, itemsize %d, itemval 0x%08x\r\n",
  267. itemtype, itemtag, itemsize, itemval);
  268. switch (itemtype) {
  269. case HID_ITEMTYPE_MAIN:
  270. switch (itemtag) {
  271. case HID_MAINITEM_TAG_INPUT:
  272. if ((flags & HID_REPORT_FLAG_REQUIRED_MASK) != HID_REPORT_FLAG_REQUIRED_MASK)
  273. goto err;
  274. if (hid_report->input_count >= CONFIG_USBHOST_HID_MAX_INPUT) {
  275. USB_LOG_ERR("hid input fields exceed max limit\r\n");
  276. goto err;
  277. }
  278. field.flags = flags;
  279. field.properties = itemval;
  280. field.usage_page = usage_page;
  281. memcpy(&hid_report->input_fields[hid_report->input_count], &field, sizeof(struct hid_report_field));
  282. if (field.usage_count > 0) {
  283. hid_report->input_fields[hid_report->input_count].usages = usb_osal_malloc(sizeof(uint32_t) * field.usage_count);
  284. if (!hid_report->input_fields[hid_report->input_count].usages) {
  285. USB_LOG_ERR("hid input usages malloc failed\r\n");
  286. goto err;
  287. }
  288. memcpy(hid_report->input_fields[hid_report->input_count].usages, usages, sizeof(uint32_t) * field.usage_count);
  289. }
  290. hid_report->input_count++;
  291. /* only keep the global items */
  292. flags &= HID_REPORT_FLAG_GLOBAL_MASK;
  293. memset(&field, 0, sizeof(struct hid_report_field));
  294. break;
  295. case HID_MAINITEM_TAG_OUTPUT:
  296. if ((flags & HID_REPORT_FLAG_REQUIRED_MASK) != HID_REPORT_FLAG_REQUIRED_MASK)
  297. goto err;
  298. if (hid_report->output_count >= CONFIG_USBHOST_HID_MAX_OUTPUT) {
  299. USB_LOG_ERR("hid output fields exceed max limit\r\n");
  300. goto err;
  301. }
  302. field.flags = flags;
  303. field.properties = itemval;
  304. field.usage_page = usage_page;
  305. memcpy(&hid_report->output_fields[hid_report->output_count], &field, sizeof(struct hid_report_field));
  306. if (field.usage_count > 0) {
  307. hid_report->output_fields[hid_report->output_count].usages = usb_osal_malloc(sizeof(uint32_t) * field.usage_count);
  308. if (!hid_report->output_fields[hid_report->output_count].usages) {
  309. USB_LOG_ERR("hid output usages malloc failed\r\n");
  310. goto err;
  311. }
  312. memcpy(hid_report->output_fields[hid_report->output_count].usages, usages, sizeof(uint32_t) * field.usage_count);
  313. }
  314. hid_report->output_count++;
  315. /* only keep the global items */
  316. flags &= HID_REPORT_FLAG_GLOBAL_MASK;
  317. memset(&field, 0, sizeof(struct hid_report_field));
  318. break;
  319. case HID_MAINITEM_TAG_COLLECTION:
  320. memset(&field, 0, sizeof(struct hid_report_field));
  321. break;
  322. case HID_MAINITEM_TAG_FEATURE:
  323. if (hid_report->feature_count >= CONFIG_USBHOST_HID_MAX_FEATURE) {
  324. USB_LOG_ERR("hid feature fields exceed max limit\r\n");
  325. goto err;
  326. }
  327. field.flags = flags;
  328. field.properties = itemval;
  329. field.usage_page = usage_page;
  330. memcpy(&hid_report->feature_fields[hid_report->feature_count], &field, sizeof(struct hid_report_field));
  331. if (field.usage_count > 0) {
  332. hid_report->feature_fields[hid_report->feature_count].usages = usb_osal_malloc(sizeof(uint32_t) * field.usage_count);
  333. if (!hid_report->feature_fields[hid_report->feature_count].usages) {
  334. USB_LOG_ERR("hid feature usages malloc failed\r\n");
  335. goto err;
  336. }
  337. memcpy(hid_report->feature_fields[hid_report->feature_count].usages, usages, sizeof(uint32_t) * field.usage_count);
  338. }
  339. hid_report->feature_count++;
  340. memset(&field, 0, sizeof(struct hid_report_field));
  341. break;
  342. case HID_MAINITEM_TAG_ENDCOLLECTION:
  343. break;
  344. default:
  345. goto err;
  346. }
  347. break;
  348. case HID_ITEMTYPE_GLOBAL:
  349. switch (itemtag) {
  350. case HID_GLOBALITEM_TAG_USAGE_PAGE:
  351. usage_page = itemval;
  352. if (usage_page > UINT16_MAX)
  353. goto err;
  354. flags |= HID_REPORT_FLAG_USAGE_PAGE;
  355. break;
  356. case HID_GLOBALITEM_TAG_LOGICAL_MIN:
  357. field.logical_min = (int32_t)itemval;
  358. flags |= HID_REPORT_FLAG_LOGICAL_MIN;
  359. break;
  360. case HID_GLOBALITEM_TAG_LOGICAL_MAX:
  361. field.logical_max = (int32_t)itemval;
  362. flags |= HID_REPORT_FLAG_LOGICAL_MAX;
  363. break;
  364. case HID_GLOBALITEM_TAG_REPORT_SIZE:
  365. field.report_size = itemval;
  366. flags |= HID_REPORT_FLAG_REPORT_SIZE;
  367. break;
  368. case HID_GLOBALITEM_TAG_REPORT_COUNT:
  369. field.report_count = itemval;
  370. flags |= HID_REPORT_FLAG_REPORT_COUNT;
  371. break;
  372. case HID_GLOBALITEM_TAG_REPORT_ID:
  373. hid_report->uses_report_id = true;
  374. field.report_id = itemval;
  375. flags |= HID_REPORT_FLAG_REPORT_ID;
  376. break;
  377. default:
  378. goto err;
  379. }
  380. break;
  381. case HID_ITEMTYPE_LOCAL:
  382. switch (itemtag) {
  383. case HID_LOCALITEM_TAG_USAGE:
  384. usage = itemval;
  385. /* Extended usage (size 4) combines both usage page and id */
  386. if (itemsize != 4) {
  387. if (!(flags & HID_REPORT_FLAG_USAGE_PAGE))
  388. goto err;
  389. usage |= usage_page << 16;
  390. }
  391. usages[field.usage_count++] = usage;
  392. break;
  393. case HID_LOCALITEM_TAG_USAGE_MIN:
  394. usage_min = itemval;
  395. if (itemsize == 4) {
  396. /* Usage max must be extended as well */
  397. flags |= HID_REPORT_FLAG_EXTENDED_USAGE;
  398. } else {
  399. if (!(flags & HID_REPORT_FLAG_USAGE_PAGE))
  400. goto err;
  401. usage_min |= usage_page << 16;
  402. }
  403. field.usage_min = usage_min;
  404. flags |= HID_REPORT_FLAG_USAGE_MIN;
  405. break;
  406. case HID_LOCALITEM_TAG_USAGE_MAX:
  407. if (!(flags & HID_REPORT_FLAG_USAGE_MIN))
  408. goto err;
  409. usage_max = itemval;
  410. if (flags & HID_REPORT_FLAG_EXTENDED_USAGE) {
  411. /* Fail if max is not extended usage (HID spec 6.2.2.8) */
  412. if (itemsize != 4)
  413. goto err;
  414. } else if (itemsize == 4) {
  415. /* Fail because min wasn't extended, but max is */
  416. goto err;
  417. } else {
  418. if (!(flags & HID_REPORT_FLAG_USAGE_PAGE))
  419. goto err;
  420. usage_max |= usage_page << 16;
  421. }
  422. /* Usage min and max must be on the same page */
  423. if (USAGE_PAGE(usage_min) != USAGE_PAGE(usage_max)) {
  424. goto err;
  425. }
  426. if (usage_min > usage_max) {
  427. goto err;
  428. }
  429. for (uint32_t j = usage_min; j <= usage_max; j++) {
  430. usages[field.usage_count++] = j;
  431. }
  432. field.usage_max = usage_max;
  433. flags |= HID_REPORT_FLAG_USAGE_MAX;
  434. flags &= ~(HID_REPORT_FLAG_USAGE_MIN | HID_REPORT_FLAG_EXTENDED_USAGE);
  435. break;
  436. default:
  437. goto err;
  438. }
  439. break;
  440. default:
  441. goto err;
  442. }
  443. i += (1 + itemsize);
  444. }
  445. usb_osal_free(usages);
  446. return hid_report;
  447. err:
  448. if (hid_report) {
  449. usb_osal_free(hid_report);
  450. for (uint32_t j = 0; j < hid_report->input_count; j++)
  451. usb_osal_free(hid_report->input_fields[j].usages);
  452. for (uint32_t j = 0; j < hid_report->output_count; j++)
  453. usb_osal_free(hid_report->output_fields[j].usages);
  454. for (uint32_t j = 0; j < hid_report->feature_count; j++)
  455. usb_osal_free(hid_report->feature_fields[j].usages);
  456. }
  457. if (usages)
  458. usb_osal_free(usages);
  459. return NULL;
  460. }
  461. void usbh_hid_report_free(struct hid_report *hid_report)
  462. {
  463. if (hid_report) {
  464. for (uint32_t j = 0; j < hid_report->input_count; j++)
  465. usb_osal_free(hid_report->input_fields[j].usages);
  466. for (uint32_t j = 0; j < hid_report->output_count; j++)
  467. usb_osal_free(hid_report->output_fields[j].usages);
  468. for (uint32_t j = 0; j < hid_report->feature_count; j++)
  469. usb_osal_free(hid_report->feature_fields[j].usages);
  470. usb_osal_free(hid_report);
  471. }
  472. }
  473. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_report_buf[2048];
  474. static const char *hid_property_string(uint32_t value)
  475. {
  476. uint32_t off = 0;
  477. static char buffer[160];
  478. memset(buffer, 0, sizeof(buffer));
  479. if (value & HID_MAINITEM_CONSTANT)
  480. off += snprintf(buffer + off, sizeof(buffer) - off, "Constant, ");
  481. else
  482. off += snprintf(buffer + off, sizeof(buffer) - off, "Data, ");
  483. if (value & HID_MAINITEM_VARIABLE)
  484. off += snprintf(buffer + off, sizeof(buffer) - off, "Variable, ");
  485. else
  486. off += snprintf(buffer + off, sizeof(buffer) - off, "Array, ");
  487. if (value & HID_MAINITEM_RELATIVE)
  488. off += snprintf(buffer + off, sizeof(buffer) - off, "Relative, ");
  489. else
  490. off += snprintf(buffer + off, sizeof(buffer) - off, "Absolute, ");
  491. if (value & HID_MAINITEM_WRAP)
  492. off += snprintf(buffer + off, sizeof(buffer) - off, "Wrap, ");
  493. else
  494. off += snprintf(buffer + off, sizeof(buffer) - off, "NoWrap, ");
  495. if (value & HID_MAINITEM_NONLINEAR)
  496. off += snprintf(buffer + off, sizeof(buffer) - off, "NonLinear, ");
  497. else
  498. off += snprintf(buffer + off, sizeof(buffer) - off, "Linear, ");
  499. if (value & HID_MAINITEM_NOPREFERRED)
  500. off += snprintf(buffer + off, sizeof(buffer) - off, "NoPreferred, ");
  501. else
  502. off += snprintf(buffer + off, sizeof(buffer) - off, "Preferred, ");
  503. if (value & HID_MAINITEM_NULLSTATE)
  504. off += snprintf(buffer + off, sizeof(buffer) - off, "NullState, ");
  505. else
  506. off += snprintf(buffer + off, sizeof(buffer) - off, "NoNullState, ");
  507. if (value & HID_MAINITEM_VOLATILE)
  508. off += snprintf(buffer + off, sizeof(buffer) - off, "Volatile, ");
  509. else
  510. off += snprintf(buffer + off, sizeof(buffer) - off, "NonVolatile, ");
  511. if (value & HID_MAINITEM_BUFFEREDBYTES)
  512. off += snprintf(buffer + off, sizeof(buffer) - off, "BufferedBytes");
  513. else
  514. off += snprintf(buffer + off, sizeof(buffer) - off, "BitField");
  515. return buffer;
  516. }
  517. static void usbh_hid_field_info_print(uint32_t idx, struct hid_report_field *field)
  518. {
  519. USB_LOG_RAW(" Field %u:\r\n", idx);
  520. USB_LOG_RAW(" Usage Page: 0x%04x\r\n", (unsigned int)field->usage_page);
  521. USB_LOG_RAW(" Report ID: %u\r\n", (unsigned int)field->report_id);
  522. USB_LOG_RAW(" Report Size: %ubit\r\n", (unsigned int)field->report_size);
  523. USB_LOG_RAW(" Report Count: %u\r\n", (unsigned int)field->report_count);
  524. USB_LOG_RAW(" Logical Min: %d\r\n", field->logical_min);
  525. USB_LOG_RAW(" Logical Max: %d\r\n", field->logical_max);
  526. USB_LOG_RAW(" Usage Count: %u\r\n", (unsigned int)field->usage_count);
  527. if (field->usage_count > 0) {
  528. if (field->usage_count == 1) {
  529. USB_LOG_RAW(" Usage: 0x%04x\r\n", USAGE_ID(field->usages[0]));
  530. } else {
  531. USB_LOG_RAW(" Usages(0x%04x ~ 0x%04x)\r\n", USAGE_ID(field->usage_min), USAGE_ID(field->usage_max));
  532. }
  533. }
  534. USB_LOG_RAW(" Flags: 0x%04x\r\n", (unsigned int)field->flags);
  535. USB_LOG_RAW(" Properties: 0x%04x(%s)\r\n", (unsigned int)field->properties, hid_property_string(field->properties));
  536. }
  537. int lshid(int argc, char **argv)
  538. {
  539. struct usbh_hid *hid_class;
  540. struct hid_report *hid_report;
  541. int ret;
  542. if (argc < 2) {
  543. USB_LOG_ERR("please input correct command: lshid path\r\n");
  544. return -1;
  545. }
  546. hid_class = usbh_find_class_instance(argv[1]);
  547. if (!hid_class) {
  548. USB_LOG_ERR("cannot find hid device\r\n");
  549. return -1;
  550. }
  551. if (hid_class->report_size > sizeof(g_hid_report_buf)) {
  552. USB_LOG_ERR("hid report buffer is too small\r\n");
  553. return -1;
  554. }
  555. ret = usbh_hid_get_report_descriptor(hid_class, g_hid_report_buf, hid_class->report_size);
  556. if (ret < 0) {
  557. USB_LOG_ERR("get hid report descriptor failed, errcode: %d\r\n", ret);
  558. return -1;
  559. }
  560. hid_report = usbh_hid_report_parse(g_hid_report_buf, hid_class->report_size, 1024);
  561. if (hid_report) {
  562. USB_LOG_RAW("HID report parsed successfully\r\n");
  563. USB_LOG_RAW("Input fields: %u\r\n", (unsigned int)hid_report->input_count);
  564. for (uint32_t i = 0; i < hid_report->input_count; i++) {
  565. struct hid_report_field *field = &hid_report->input_fields[i];
  566. usbh_hid_field_info_print(i, field);
  567. }
  568. USB_LOG_RAW("Output fields: %u\r\n", (unsigned int)hid_report->output_count);
  569. for (uint32_t i = 0; i < hid_report->output_count; i++) {
  570. struct hid_report_field *field = &hid_report->output_fields[i];
  571. usbh_hid_field_info_print(i, field);
  572. }
  573. USB_LOG_RAW("Feature fields: %u\r\n", (unsigned int)hid_report->feature_count);
  574. for (uint32_t i = 0; i < hid_report->feature_count; i++) {
  575. struct hid_report_field *field = &hid_report->feature_fields[i];
  576. usbh_hid_field_info_print(i, field);
  577. }
  578. usbh_hid_report_free(hid_report);
  579. } else {
  580. USB_LOG_ERR("HID report parsed failed\r\n");
  581. }
  582. return 0;
  583. }
  584. __WEAK void usbh_hid_run(struct usbh_hid *hid_class)
  585. {
  586. (void)hid_class;
  587. }
  588. __WEAK void usbh_hid_stop(struct usbh_hid *hid_class)
  589. {
  590. (void)hid_class;
  591. }
  592. const struct usbh_class_driver hid_class_driver = {
  593. .driver_name = "hid",
  594. .connect = usbh_hid_connect,
  595. .disconnect = usbh_hid_disconnect
  596. };
  597. CLASS_INFO_DEFINE const struct usbh_class_info hid_custom_class_info = {
  598. .match_flags = USB_CLASS_MATCH_INTF_CLASS,
  599. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  600. .bInterfaceSubClass = 0x00,
  601. .bInterfaceProtocol = 0x00,
  602. .id_table = NULL,
  603. .class_driver = &hid_class_driver
  604. };