hid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #include "includes.h"
  2. #include "asm/includes.h"
  3. #include "app_config.h"
  4. #include "system/timer.h"
  5. #include "device/ioctl_cmds.h"
  6. #include "device_drive.h"
  7. #if TCFG_HID_HOST_ENABLE
  8. #include "usb/host/usb_host.h"
  9. #include "usb_ctrl_transfer.h"
  10. #include "usb_bulk_transfer.h"
  11. #include "hid.h"
  12. #include "usb_config.h"
  13. #include "usb_hid_keys.h"
  14. #define MAIN_ITEM 0
  15. #define GLOBAL_ITEM 1
  16. #define LOCAL_ITEM 2
  17. #define LOG_TAG_CONST USB
  18. #define LOG_TAG "[HID]"
  19. #define LOG_ERROR_ENABLE
  20. #define LOG_DEBUG_ENABLE
  21. #define LOG_INFO_ENABLE
  22. /* #define LOG_DUMP_ENABLE */
  23. #define LOG_CLI_ENABLE
  24. #include "debug.h"
  25. struct hid_device_t hid_device[USB_MAX_HW_NUM][MAX_HOST_INTERFACE];
  26. static int set_power(struct usb_host_device *host_dev, u32 value)
  27. {
  28. const usb_dev usb_id = host_device2id(host_dev);
  29. memset(hid_device[usb_id], 0, sizeof(hid_device[usb_id]));
  30. return DEV_ERR_NONE;
  31. }
  32. static int get_power(struct usb_host_device *host_dev, u32 value)
  33. {
  34. return DEV_ERR_NONE;
  35. }
  36. static const struct interface_ctrl hid_ctrl = {
  37. .interface_class = USB_CLASS_HID,
  38. .set_power = set_power,
  39. .get_power = get_power,
  40. .ioctl = NULL,
  41. };
  42. static const struct usb_interface_info _usb_if[USB_MAX_HW_NUM][MAX_HOST_INTERFACE] = {
  43. {
  44. {
  45. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  46. .dev.hid = &hid_device[0][0],
  47. },
  48. {
  49. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  50. .dev.hid = &hid_device[0][1],
  51. },
  52. {
  53. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  54. .dev.hid = &hid_device[0][2],
  55. },
  56. {
  57. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  58. .dev.hid = &hid_device[0][3],
  59. },
  60. },
  61. #if USB_MAX_HW_NUM > 1
  62. {
  63. {
  64. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  65. .dev.hid = &hid_device[1][0],
  66. },
  67. {
  68. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  69. .dev.hid = &hid_device[1][1],
  70. },
  71. {
  72. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  73. .dev.hid = &hid_device[1][2],
  74. },
  75. {
  76. .ctrl = (struct interface_ctrl *) &hid_ctrl,
  77. .dev.hid = &hid_device[1][3],
  78. },
  79. },
  80. #endif
  81. };
  82. static u8 interval[USB_MAX_HW_NUM][16];
  83. int usb_hid_parser(struct usb_host_device *host_dev, u8 interface_num, const u8 *pBuf)
  84. {
  85. struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)pBuf;
  86. pBuf += sizeof(struct usb_interface_descriptor);
  87. int len = 0;
  88. const usb_dev usb_id = host_device2id(host_dev);
  89. struct usb_endpoint_descriptor *endpoint;
  90. pBuf += 9;//hid desc;
  91. const struct usb_interface_info *usb_if = &_usb_if[usb_id][interface_num];
  92. memset(usb_if->dev.p, 0, sizeof(struct hid_device_t));
  93. host_dev->interface_info[interface_num] = usb_if;
  94. usb_if->dev.hid->parent = host_dev;
  95. log_info("hid eps %d %d %x %x", interface->bNumEndpoints, interface_num, usb_if, usb_if->dev.p);
  96. log_info("parent %x hid @ interface %d usb_if %x hid %x",
  97. host_dev, interface_num, usb_if, usb_if->dev.hid);
  98. if ((interface->bInterfaceProtocol == 0x02) ||
  99. (interface->bInterfaceProtocol == 0x01)) { //mouse & keyboard
  100. usb_if->dev.hid->bNumEndpoints = interface->bNumEndpoints;
  101. usb_if->dev.hid->report_list[0].usage = interface->bInterfaceProtocol;
  102. for (int i = 0 ; i < interface->bNumEndpoints; i++) {
  103. endpoint = (struct usb_endpoint_descriptor *)pBuf;
  104. if (USB_DIR_IN & endpoint->bEndpointAddress) {
  105. const u8 ep = endpoint->bEndpointAddress & 0x0f;
  106. usb_if->dev.hid->ep_pair[i] = ep;
  107. interval[usb_id][ep] = endpoint->bInterval;
  108. log_info("interfacenum = %d,endpoint = %x interval = %x",
  109. interface->bInterfaceNumber, endpoint->bEndpointAddress, endpoint->bInterval);
  110. }
  111. pBuf += endpoint->bLength;
  112. }
  113. } else {
  114. log_info("vendor");
  115. host_dev->interface_info[interface_num] = NULL; //???
  116. for (int i = 0 ; i < interface->bNumEndpoints; i++) {
  117. endpoint = (struct usb_endpoint_descriptor *)pBuf;
  118. if (USB_DIR_IN & endpoint->bEndpointAddress) {
  119. /* interface_endpoint[interface->bInterfaceNumber] = endpoint->bEndpointAddress & 0x0f; */
  120. log_info("interfacenum = %d,endpoint = %x interval = %x",
  121. interface->bInterfaceNumber, endpoint->bEndpointAddress, endpoint->bInterval);
  122. }
  123. pBuf += endpoint->bLength;
  124. }
  125. return sizeof(struct usb_interface_descriptor);
  126. }
  127. return pBuf - (u8 *)interface;
  128. }
  129. static u32 _hid_report_parse(struct hid_device_t *hid, const u8 *report, u32 len)
  130. {
  131. hid->report_count = 0;
  132. struct report_info_t null_rpt;
  133. struct report_info_t *const rpt = &null_rpt;
  134. memset(rpt, 0, sizeof(*rpt));
  135. unsigned char ops;
  136. int index = 0;
  137. u8 report_size = 0;
  138. u8 report_count = 0;
  139. u8 cur_ops_is_report_size_count = 0;
  140. u8 old_ops_is_report_size_count = 0;
  141. s8 cur_section_bit = 0;
  142. u8 input_bit_index = 0;
  143. u8 total_bits = 0;
  144. u8 output_bit_index = 0;
  145. u8 cur_usage = 0;
  146. u32 undef_type = 0;
  147. u8 undef_usage = 0;
  148. u8 collection_deep = 0;
  149. while (index < len) {
  150. ops = (char)report[index++];
  151. char bSize = ops & 0x03;
  152. bSize = bSize == 3 ? 4 : bSize; // size is 4 when bSize is 3
  153. char bType = (ops >> 2) & 0x03;
  154. char bTag = (ops >> 4) & 0x0F;
  155. cur_ops_is_report_size_count = 0;
  156. char bSizeActual = 0;
  157. int itemVal = 0;
  158. for (int j = 0; j < bSize; j++) {
  159. if (index + j < len) {
  160. itemVal += report[index + j] << (8 * j);
  161. bSizeActual++;
  162. }
  163. }
  164. if (undef_type) {
  165. undef_type ++;
  166. if (bTag == 0x0A) {
  167. undef_usage ++;
  168. } else if (bTag == 0x0C) {
  169. undef_usage --;
  170. }
  171. if (undef_usage == 0 && undef_type > 2) {
  172. undef_type = 0;
  173. }
  174. index += bSize;
  175. continue;
  176. }
  177. if (undef_type) {
  178. index += bSize;
  179. continue;
  180. }
  181. if (itemVal == 0xffb5) {
  182. undef_type = 1;
  183. index += bSize;
  184. continue;
  185. } else {
  186. undef_type = 0;
  187. }
  188. if (bType == MAIN_ITEM) {
  189. if (old_ops_is_report_size_count) {
  190. cur_section_bit += report_size * report_count;
  191. }
  192. if (bTag == 0x08) {
  193. /* log_info("input %X", itemVal); */
  194. /* log_info("\tusage %x", cur_usage); */
  195. /* log_info("\t\tcur_section_bit %d", cur_section_bit); */
  196. if (rpt->usage == 0x02) { //mouse
  197. if (cur_usage == 1) {
  198. if (rpt->btn_start_bit == 0) {
  199. rpt->btn_start_bit = total_bits ;
  200. }
  201. rpt->btn_width += cur_section_bit ;
  202. /* log_info("btn_width %d-%d", rpt->btn_start_bit, rpt->btn_width); */
  203. } else if ((cur_usage == 0x30) || (cur_usage == 0x31)) {
  204. if (rpt->xy_start_bit == 0) {
  205. rpt->xy_start_bit = total_bits;
  206. }
  207. rpt->xy_width = cur_section_bit;
  208. /* log_info("xy_width %d-%d", rpt->xy_start_bit, rpt->xy_width); */
  209. } else if (cur_usage == 0x38) {
  210. if (rpt->wheel_start_bit == 0) {
  211. rpt->wheel_start_bit = total_bits;
  212. }
  213. if (rpt->xy_width || cur_section_bit < 24) {
  214. rpt->wheel_width = cur_section_bit;
  215. /* log_info("wheel_width %d-%d", rpt->wheel_start_bit, rpt->wheel_width); */
  216. } else {
  217. rpt->wheel_width = rpt->xy_width = cur_section_bit / 3;
  218. rpt->xy_start_bit = total_bits;
  219. rpt->wheel_start_bit = rpt->xy_start_bit + rpt->xy_width * 2;
  220. /* log_info("wheel_width %d-%d", rpt->wheel_start_bit, rpt->wheel_width); */
  221. /* log_info("xy_width %d-%d", rpt->xy_start_bit, rpt->xy_width); */
  222. }
  223. } else if (cur_usage == 0xb8) {
  224. rpt->wheel_width = rpt->xy_width = cur_section_bit / 4;
  225. rpt->xy_start_bit = total_bits;
  226. rpt->wheel_start_bit = rpt->xy_start_bit + rpt->xy_width * 2;
  227. }
  228. }
  229. total_bits += cur_section_bit;
  230. /* input_bit[input_bit_index++] = cur_section_bit; */
  231. cur_section_bit = -1;
  232. } else if (bTag == 0x09) {
  233. /* log_info("OUTPUT %X", itemVal); */
  234. /* log_info("\tusage %x", cur_usage); */
  235. /* log_info("\t\tcur_section_bit %d", cur_section_bit); */
  236. /* output_bit[output_bit_index++] = cur_section_bit; */
  237. cur_section_bit = -1;
  238. } else if (bTag == 0x0B) {
  239. /* log_info("Feature %X", itemVal); */
  240. /* log_info("\tusage %x", cur_usage); */
  241. /* log_info("\t\tcur_section_bit %d", cur_section_bit); */
  242. /* output_bit[output_bit_index++] = cur_section_bit; */
  243. cur_section_bit = -1;
  244. } else if (bTag == 0x0A) {
  245. collection_deep ++ ;
  246. log_info("Collection %d %x", collection_deep, rpt->usage);
  247. } else if (bTag == 0x0C) {
  248. collection_deep --;
  249. log_info("End Collection %d %x", collection_deep, rpt->usage);
  250. if (collection_deep == 0) {
  251. if (rpt->usage == 0x02 ||
  252. rpt->usage == 0x06 ||
  253. rpt->usage == 0x07) {
  254. memcpy(&hid->report_list[hid->report_count], rpt, sizeof(*rpt));
  255. memset(rpt, 0, sizeof(*rpt));
  256. hid->report_count ++;
  257. }
  258. }
  259. if (index < len) {
  260. continue;
  261. }
  262. } else {
  263. log_info("MAIN_ITEM Unknown btag :%x", bTag);
  264. return 1;
  265. }
  266. } else if (bType == GLOBAL_ITEM) {
  267. /* log_info("GLOBAL_ITEM"); */
  268. if (bTag == 0x00) {
  269. /* log_info("Usage Page %x", itemVal); */
  270. if (rpt->usage == 0x06) {
  271. if (itemVal == 0x07) {
  272. rpt->usage = 0x07;
  273. log_info("re set type %x", 0x07);
  274. }
  275. }
  276. if (itemVal == 0x02) {
  277. rpt->usage = 0x02;
  278. log_info("re set type %x", 0x02);
  279. }
  280. } else if (bTag == 0x01) {
  281. //log_info("Logical Minimum %x", itemVal);
  282. } else if (bTag == 0x02) {
  283. //log_info("Logical Maximum %x", itemVal);
  284. } else if (bTag == 0x03) {
  285. /* log_info("Physical Minimum %x", itemVal); */
  286. } else if (bTag == 0x04) {
  287. /* log_info("Physical Maximum %x", itemVal); */
  288. } else if (bTag == 0x05) {
  289. /* log_info("Unit Exponent %x", itemVal); */
  290. } else if (bTag == 0x06) {
  291. /* log_info("Unit %x", itemVal); */
  292. } else if (bTag == 0x07) {
  293. /* log_info("Report Size %x", itemVal); */
  294. report_size = itemVal;
  295. cur_ops_is_report_size_count = 1;
  296. } else if (bTag == 0x08) {
  297. log_info("Report ID %x", itemVal, rpt->usage);
  298. rpt->report_id = itemVal;
  299. } else if (bTag == 0x09) {
  300. /* log_info("Report Count %x", itemVal); */
  301. report_count = itemVal;
  302. cur_ops_is_report_size_count = 1;
  303. } else if (bTag == 0x0A) {
  304. /* log_info("Push %x", bSizeActual); */
  305. } else if (bTag == 0x0B) {
  306. /* log_info("Pop %x", bSizeActual); */
  307. } else {
  308. log_info("GLOBAL_ITEM Unknown btag :%x", bTag);
  309. return 2;
  310. }
  311. } else if (bType == LOCAL_ITEM) {
  312. /* log_info("LOCAL_ITEM"); */
  313. if (bTag == 0x00) {
  314. if (rpt->usage == 0) {
  315. rpt->usage = itemVal;
  316. log_info("set type %x", rpt->usage);
  317. }
  318. if (itemVal == 0x30) { //X
  319. } else if (itemVal == 0x31) { //y
  320. } else if (itemVal == 0x38) { //wheel
  321. } else {
  322. }
  323. /* log_info("\t change usage %x -> %x", cur_usage, itemVal); */
  324. cur_usage = itemVal;
  325. if (!collection_deep) {
  326. if (itemVal == 0x06 || itemVal == 0x07 || itemVal == 0x02) {
  327. //仅限键盘和鼠标
  328. rpt->usage = itemVal;
  329. log_info("set typee %x", rpt->usage);
  330. }
  331. }
  332. /* type = itemVal; */
  333. } else if (bTag == 0x01) {
  334. // log_info("Usage Minimum %x", itemVal);
  335. } else if (bTag == 0x02) {
  336. // log_info("Usage Maximum %x", itemVal);
  337. } else if (bTag == 0x03) {
  338. /* log_info("Designator Index %x", itemVal); */
  339. } else if (bTag == 0x04) {
  340. /* log_info("Designator Minimum %x", itemVal); */
  341. } else if (bTag == 0x05) {
  342. /* log_info("Designator Maximum %x", itemVal); */
  343. } else if (bTag == 0x07) {
  344. /* log_info("String Index %x", itemVal); */
  345. } else if (bTag == 0x08) {
  346. /* log_info("String Minimum %x", itemVal); */
  347. } else if (bTag == 0x09) {
  348. /* log_info("String Maximum %x", itemVal); */
  349. } else if (bTag == 0x0A) {
  350. /* log_info("Delimiter %x", itemVal); */
  351. } else {
  352. log_info("LOCAL_ITEM Unknown btag :%x", bTag);
  353. return 3;
  354. }
  355. } else {
  356. log_info("OTHER Unknown btag :%x", bTag);
  357. return 4;
  358. }
  359. if (!cur_ops_is_report_size_count && old_ops_is_report_size_count) {
  360. if (cur_section_bit != -1) {
  361. cur_section_bit += report_size * report_count;
  362. } else {
  363. cur_section_bit = 0;
  364. }
  365. }
  366. if (cur_section_bit == -1) {
  367. cur_section_bit = 0;
  368. }
  369. old_ops_is_report_size_count = cur_ops_is_report_size_count;
  370. index += bSize;
  371. }
  372. return 0;
  373. }
  374. static u32 hid_report_parse(struct hid_device_t *hid, const u8 *report, u32 len)
  375. {
  376. u8 type = _hid_report_parse(hid, report, len);
  377. for (int i = 0; i < hid->report_count; i++) {
  378. struct report_info_t *rpt = &hid->report_list[i];
  379. if (rpt->usage == 0x02) {
  380. if (rpt->report_id == 0) {
  381. rpt->report_id = 0xff;
  382. }
  383. rpt->btn_start_bit /= 8;
  384. rpt->btn_width /= 8;
  385. rpt->xy_start_bit /= 8;
  386. rpt->xy_width /= 8;
  387. rpt->wheel_start_bit /= 8;
  388. rpt->wheel_width /= 8;
  389. log_info("mouse report_id %d",
  390. rpt->report_id);
  391. log_info("btn_width %d-%d",
  392. rpt->btn_start_bit, rpt->btn_width);
  393. log_info("xy_width %d-%d",
  394. rpt->xy_start_bit, rpt->xy_width);
  395. log_info("wheel_width %d-%d",
  396. rpt->wheel_start_bit, rpt->wheel_width);
  397. if (rpt->btn_width != 2) {
  398. rpt->btn_width = 1;
  399. }
  400. log_info("btn_width %d-%d",
  401. rpt->btn_start_bit, rpt->btn_width);
  402. } else if (rpt->usage == 6 || rpt->usage == 7) {
  403. if (rpt->report_id == 0) {
  404. rpt->report_id = 0xff;
  405. }
  406. log_info("keyboard report_id %d", rpt->report_id);
  407. } else {
  408. log_info("unknown usage %d", rpt->usage);
  409. }
  410. }
  411. return 0;
  412. }
  413. void mouse_route(const struct mouse_data_t *p);
  414. /* __attribute__((weak)) void mouse_route(const struct mouse_data_t *p) */
  415. /* { */
  416. /* log_info("btn: %x x-y %d %d wheel %d ac_pan %d", */
  417. /* p->btn, p->x, p->y, p->wheel, p->ac_pan); */
  418. /* } */
  419. static void hid_convert_mouse(const struct report_info_t *mouse, const u8 *buffer)
  420. {
  421. struct mouse_data_t mouse_data;
  422. memset(&mouse_data, 0, sizeof(mouse_data));
  423. if (mouse->report_id != 0xff) {
  424. if (mouse->report_id != buffer[0]) {
  425. log_error("report_id = %x buffer[0] = %x", mouse->report_id, buffer[0]);
  426. return;
  427. }
  428. buffer++;
  429. }
  430. const u8 *ptr;
  431. ptr = &buffer[mouse->btn_start_bit];
  432. if (mouse->btn_width == 2) {
  433. mouse_data.btn = ptr[0] | (ptr[1] << 8);
  434. } else {
  435. mouse_data.btn = ptr[0] ;
  436. }
  437. s16 tmp;
  438. ptr = &buffer[mouse->xy_start_bit];
  439. if (mouse->xy_width == 1 || mouse->xy_width == 2) {
  440. mouse_data.x = (char)ptr[0];
  441. mouse_data.y = (char)ptr[1];
  442. } else if (mouse->xy_width == 4) {
  443. mouse_data.x = ptr[0] | (ptr[1] << 8);
  444. ptr += 2;
  445. mouse_data.y = ptr[0] | (ptr[1] << 8);
  446. } else if (mouse->xy_width == 3) {
  447. tmp = (ptr[1] & 0xf) << 12 | ptr[0] << 4;
  448. tmp = tmp >> 4;
  449. mouse_data.x = tmp;
  450. tmp = (ptr[2] << 8) | ((ptr[1] >> 4) << 4);
  451. tmp = tmp >> 4;
  452. mouse_data.y = tmp;
  453. } else {
  454. log_error("error mouse xy_width %d", mouse->xy_width);
  455. }
  456. ptr = &buffer[mouse->wheel_start_bit];
  457. if (mouse->wheel_width == 1) {
  458. mouse_data.wheel = (char)ptr[0];
  459. } else {
  460. mouse_data.wheel = ptr[0] | (ptr[1] << 8);
  461. }
  462. mouse_route(&mouse_data);
  463. }
  464. __attribute__((weak)) void keyboard_route(const u8 *p)
  465. {
  466. log_info("keyboard_buffer:");
  467. printf_buf(p, 12);
  468. }
  469. void hid_convert_krbd(const struct report_info_t *kbd, u8 *buffer)
  470. {
  471. #if 0
  472. u8 keyboard_buffer[12];
  473. memset(keyboard_buffer, 0, sizeof(keyboard_buffer));
  474. if (kbd->report_id != 0xff) {
  475. if (kbd->report_id != buffer[0]) {
  476. log_error("report_id = %x buffer[0] = %x", kbd->report_id, buffer[0]);
  477. return;
  478. }
  479. buffer++;
  480. }
  481. u8 idx = 0;
  482. u8 index = 0;
  483. int i = 0;
  484. for (i = 0; i < 8; i++) {
  485. if (buffer[0] & BIT(i)) {
  486. keyboard_buffer[idx++] = 0xe0 + i;
  487. }
  488. }
  489. if (buffer[1] == 0) {
  490. buffer += 2;
  491. }
  492. index = idx;
  493. for (; idx < 12; idx++) {
  494. if (*buffer) {
  495. keyboard_buffer[index] = *buffer;
  496. index++;
  497. }
  498. buffer ++;
  499. }
  500. keyboard_route(keyboard_buffer);
  501. #else
  502. if (kbd->report_id != 0xff) {
  503. if (kbd->report_id != buffer[0]) {
  504. log_error("report_id = %x buffer[0] = %x", kbd->report_id, buffer[0]);
  505. return;
  506. }
  507. buffer++;
  508. }
  509. u8 keyboard_buffer[8];
  510. memset(keyboard_buffer, 0, sizeof(keyboard_buffer));
  511. keyboard_buffer[0] = *buffer;
  512. buffer ++;
  513. if (*buffer == 0) {
  514. buffer ++;
  515. }
  516. keyboard_buffer[1] = 0;
  517. /* memcpy(&keyboard_buffer[2], buffer, 6); */
  518. u8 pos = 2;
  519. for (int i = pos; i < 8; i++) {
  520. if (*buffer) {
  521. keyboard_buffer[pos] = *buffer;
  522. pos++;
  523. }
  524. buffer++;
  525. }
  526. keyboard_route(keyboard_buffer);
  527. #endif
  528. }
  529. static void hid_route(const struct hid_device_t *hid, const u8 *buffer)
  530. {
  531. for (int i = 0; i < hid->report_count; i++) {
  532. if (hid->report_list[i].usage == 0x02) {
  533. hid_convert_mouse(&hid->report_list[i], buffer);
  534. } else if ((hid->report_list[i].usage == 0x06) ||
  535. (hid->report_list[i].usage == 0x07)) {
  536. hid_convert_krbd(&hid->report_list[i], buffer);
  537. } else {
  538. r_printf("usage %x", hid->report_list[i].usage);
  539. }
  540. }
  541. }
  542. static void hid_isr(struct usb_interface_info *usb_if, u32 ep)
  543. {
  544. u8 buffer[64] = {0};
  545. struct usb_host_device *host_dev = usb_if->dev.hid->parent;
  546. usb_dev usb_id = host_device2id(host_dev);
  547. u32 target_ep = usb_if->dev.hid->ep_pair[ep];
  548. u32 rx_len = usb_h_ep_read_async(usb_id, ep, target_ep, buffer, 64, USB_ENDPOINT_XFER_INT, 0);
  549. if (rx_len) {
  550. hid_route(usb_if->dev.hid, buffer);
  551. }
  552. /* printf_buf(buffer, rx_len); */
  553. usb_h_ep_read_async(usb_id, ep, target_ep, buffer, 8, USB_ENDPOINT_XFER_INT, 1);
  554. }
  555. void hid_process(u32 id)
  556. {
  557. struct usb_host_device *host_dev = host_id2device(id);
  558. u8 report[256 + 2];
  559. u8 ep_pair[4];
  560. for (u8 i = 0; i < MAX_HOST_INTERFACE; i++) {
  561. struct usb_interface_info *usb_if = host_dev->interface_info[i];
  562. log_info("parent %x hid @ interface %d usb_if %x hid %x",
  563. host_dev, i, usb_if, usb_if ? usb_if->dev.hid : 0);
  564. if (usb_if &&
  565. (usb_if->ctrl->interface_class == USB_CLASS_HID)) {
  566. hid_set_idle(host_dev, i);
  567. memset(report, 0, sizeof(report));
  568. hid_get_report(host_dev, report, i, 0xff);
  569. printf_buf(report, 256);
  570. hid_report_parse(usb_if->dev.hid, report, 256);
  571. memcpy(ep_pair, usb_if->dev.hid->ep_pair, 4);
  572. if (usb_if->dev.hid->report_count == 0) {
  573. continue;
  574. }
  575. for (int i = 0; i < usb_if->dev.hid->bNumEndpoints; i++) {
  576. u32 host_ep = usb_get_ep_num(id, USB_DIR_IN, USB_ENDPOINT_XFER_INT);
  577. ASSERT(host_ep != -1, "ep not enough");
  578. u32 target_ep = ep_pair[i];
  579. usb_if->dev.hid->ep_pair[host_ep] = target_ep;
  580. log_info("D2H ep: %x --> %x interval %d",
  581. target_ep, host_ep, interval[id][target_ep]);
  582. usb_h_set_ep_isr(host_dev, host_ep | USB_DIR_IN, hid_isr, usb_if);
  583. u8 *ep_buffer = usb_h_get_ep_buffer(id, host_ep | USB_DIR_OUT);
  584. usb_h_ep_config(id, host_ep | USB_DIR_IN, USB_ENDPOINT_XFER_INT, 1,
  585. interval[id][target_ep], ep_buffer, 64);
  586. int r = usb_h_ep_read_async(id, host_ep, target_ep, NULL, 0, USB_ENDPOINT_XFER_INT, 1);
  587. }
  588. } else {
  589. if (usb_if) {
  590. log_error("error hid class %x", usb_if->ctrl->interface_class);
  591. }
  592. }
  593. }
  594. }
  595. #endif