You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

677 line
21KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. // This HID driver claims a USB interface and parses its incoming
  26. // data (reports). It doesn't actually use the data, but it allows
  27. // drivers which inherit the USBHIDInput base class to claim the
  28. // top level collections within the reports. Those drivers get
  29. // callbacks with the arriving data full decoded to data/usage
  30. // pairs.
  31. #define print USBHost::print_
  32. #define println USBHost::println_
  33. void USBHIDParser::init()
  34. {
  35. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  36. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  37. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  38. driver_ready_for_device(this);
  39. }
  40. bool USBHIDParser::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  41. {
  42. println("HIDParser claim this=", (uint32_t)this, HEX);
  43. // only claim at interface level
  44. if (type != 1) return false;
  45. if (len < 9+9+7) return false;
  46. // interface descriptor
  47. uint32_t numendpoint = descriptors[4];
  48. if (numendpoint < 1 || numendpoint > 2) return false;
  49. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  50. println(" bInterfaceClass = ", descriptors[5]);
  51. println(" bInterfaceSubClass = ", descriptors[6]);
  52. println(" bInterfaceProtocol = ", descriptors[7]);
  53. // do not claim boot protocol keyboards
  54. if (descriptors[6] == 1 && descriptors[7] == 1) return false;
  55. print("HID Parser Claim: ");
  56. print_hexbytes(descriptors, len);
  57. // hid interface descriptor
  58. uint32_t hidlen = descriptors[9];
  59. if (hidlen < 9) return false;
  60. if (descriptors[10] != 33) return false; // descriptor type, 33=HID
  61. if (descriptors[14] < 1) return false; // must be at least 1 extra descriptor
  62. if (hidlen != (uint32_t)(6 + descriptors[14] * 3)) return false; // must be correct size
  63. if (9 + hidlen > len) return false;
  64. uint32_t i=0;
  65. while (1) {
  66. if (descriptors[15 + i * 3] == 34) { // found HID report descriptor
  67. descsize = descriptors[16 + i * 3] | (descriptors[17 + i * 3] << 8);
  68. println("report descriptor size = ", descsize);
  69. break;
  70. }
  71. i++;
  72. if (i >= descriptors[14]) return false;
  73. }
  74. if (descsize > sizeof(descriptor)) return false; // can't fit the report descriptor
  75. // endpoint descriptor(s)
  76. uint32_t offset = 9 + hidlen;
  77. if (len < offset + numendpoint * 7) return false; // not enough data
  78. if (numendpoint == 1) {
  79. println("Single endpoint HID:");
  80. if (descriptors[offset] != 7) return false;
  81. if (descriptors[offset+1] != 5) return false; // endpoint descriptor
  82. if (descriptors[offset+3] != 3) return false; // must be interrupt type
  83. uint32_t endpoint = descriptors[offset+2];
  84. uint32_t size = descriptors[offset+4] | (descriptors[offset+5] << 8);
  85. uint32_t interval = descriptors[offset+6];
  86. println(" endpoint = ", endpoint, HEX);
  87. println(" size = ", size);
  88. println(" interval = ", interval);
  89. if ((endpoint & 0x0F) == 0) return false;
  90. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  91. in_pipe = new_Pipe(dev, 3, endpoint & 0x0F, 1, size, interval);
  92. out_pipe = NULL;
  93. in_size = size;
  94. } else {
  95. println("Two endpoint HID:");
  96. if (descriptors[offset] != 7) return false;
  97. if (descriptors[offset+1] != 5) return false; // endpoint descriptor
  98. if (descriptors[offset+3] != 3) return false; // must be interrupt type
  99. uint32_t endpoint1 = descriptors[offset+2];
  100. uint32_t size1 = descriptors[offset+4] | (descriptors[offset+5] << 8);
  101. uint32_t interval1 = descriptors[offset+6];
  102. println(" endpoint = ", endpoint1, HEX);
  103. println(" size = ", size1);
  104. println(" interval = ", interval1);
  105. if ((endpoint1 & 0x0F) == 0) return false;
  106. if (descriptors[offset+7] != 7) return false;
  107. if (descriptors[offset+8] != 5) return false; // endpoint descriptor
  108. if (descriptors[offset+10] != 3) return false; // must be interrupt type
  109. uint32_t endpoint2 = descriptors[offset+9];
  110. uint32_t size2 = descriptors[offset+11] | (descriptors[offset+12] << 8);
  111. uint32_t interval2 = descriptors[offset+13];
  112. println(" endpoint = ", endpoint2, HEX);
  113. println(" size = ", size2);
  114. println(" interval = ", interval2);
  115. if ((endpoint2 & 0x0F) == 0) return false;
  116. if (((endpoint1 & 0xF0) == 0x80) && ((endpoint2 & 0xF0) == 0)) {
  117. // first endpoint is IN, second endpoint is OUT
  118. in_pipe = new_Pipe(dev, 3, endpoint1 & 0x0F, 1, size1, interval1);
  119. out_pipe = new_Pipe(dev, 3, endpoint2, 0, size2, interval2);
  120. in_size = size1;
  121. out_size = size2;
  122. } else if (((endpoint1 & 0xF0) == 0) && ((endpoint2 & 0xF0) == 0x80)) {
  123. // first endpoint is OUT, second endpoint is IN
  124. in_pipe = new_Pipe(dev, 3, endpoint2 & 0x0F, 1, size2, interval2);
  125. out_pipe = new_Pipe(dev, 3, endpoint1, 0, size1, interval1);
  126. in_size = size2;
  127. out_size = size1;
  128. } else {
  129. return false;
  130. }
  131. out_pipe->callback_function = out_callback;
  132. }
  133. in_pipe->callback_function = in_callback;
  134. for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) {
  135. //topusage_list[i] = 0;
  136. topusage_drivers[i] = NULL;
  137. }
  138. // request the HID report descriptor
  139. mk_setup(setup, 0x81, 6, 0x2200, descriptors[2], descsize); // get report desc
  140. queue_Control_Transfer(dev, &setup, descriptor, this);
  141. return true;
  142. }
  143. void USBHIDParser::control(const Transfer_t *transfer)
  144. {
  145. println("control callback (hid)");
  146. print_hexbytes(transfer->buffer, transfer->length);
  147. // To decode hex dump to human readable HID report summary:
  148. // http://eleccelerator.com/usbdescreqparser/
  149. uint32_t mesg = transfer->setup.word1;
  150. println(" mesg = ", mesg, HEX);
  151. if (mesg == 0x22000681 && transfer->length == descsize) { // HID report descriptor
  152. println(" got report descriptor");
  153. parse();
  154. queue_Data_Transfer(in_pipe, report, in_size, this);
  155. if (device->idVendor == 0x054C &&
  156. ((device->idProduct == 0x0268) || (device->idProduct == 0x042F)/* || (device->idProduct == 0x03D5)*/)) {
  157. println("send special PS3 feature command");
  158. mk_setup(setup, 0x21, 9, 0x03F4, 0, 4); // ps3 tell to send report 1?
  159. static uint8_t ps3_feature_F4_report[] = {0x42, 0x0c, 0x00, 0x00};
  160. queue_Control_Transfer(device, &setup, ps3_feature_F4_report, this);
  161. }
  162. }
  163. }
  164. void USBHIDParser::in_callback(const Transfer_t *transfer)
  165. {
  166. if (transfer->driver) {
  167. ((USBHIDParser*)(transfer->driver))->in_data(transfer);
  168. }
  169. }
  170. void USBHIDParser::out_callback(const Transfer_t *transfer)
  171. {
  172. //println("USBHIDParser:: out_callback (static)");
  173. if (transfer->driver) {
  174. ((USBHIDParser*)(transfer->driver))->out_data(transfer);
  175. }
  176. }
  177. // When the device goes away, we need to call disconnect_collection()
  178. // for all drivers which claimed a top level collection
  179. void USBHIDParser::disconnect()
  180. {
  181. for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) {
  182. USBHIDInput *driver = topusage_drivers[i];
  183. if (driver) {
  184. driver->disconnect_collection(device);
  185. topusage_drivers[i] = NULL;
  186. }
  187. }
  188. }
  189. // Called when the HID device sends a report
  190. void USBHIDParser::in_data(const Transfer_t *transfer)
  191. {
  192. /*USBHDBGSerial.printf("HID: ");
  193. uint8_t *pb = (uint8_t*)transfer->buffer;
  194. for (uint8_t i = 0; i < transfer->length; i++) {
  195. USBHDBGSerial.printf("%x ",pb[i]);
  196. }
  197. USBHDBGSerial.printf("\n"); */
  198. print("HID: ");
  199. print(use_report_id);
  200. print(" - ");
  201. print_hexbytes(transfer->buffer, transfer->length);
  202. const uint8_t *buf = (const uint8_t *)transfer->buffer;
  203. uint32_t len = transfer->length;
  204. // See if the first top report wishes to bypass the
  205. // parse...
  206. if (!(topusage_drivers[0] && topusage_drivers[0]->hid_process_in_data(transfer))) {
  207. if (use_report_id == false) {
  208. parse(0x0100, buf, len);
  209. } else {
  210. if (len > 1) {
  211. parse(0x0100 | buf[0], buf + 1, len - 1);
  212. }
  213. }
  214. }
  215. queue_Data_Transfer(in_pipe, report, in_size, this);
  216. }
  217. void USBHIDParser::out_data(const Transfer_t *transfer)
  218. {
  219. println("USBHIDParser:out_data called (instance)");
  220. // A packet completed. lets mark it as done and call back
  221. // to top reports handler. We unmark our checkmark to
  222. // handle case where they may want to queue up another one.
  223. if (transfer->buffer == tx1) txstate &= ~1;
  224. if (transfer->buffer == tx2) txstate &= ~2;
  225. if (topusage_drivers[0]) {
  226. topusage_drivers[0]->hid_process_out_data(transfer);
  227. }
  228. }
  229. bool USBHIDParser::sendPacket(const uint8_t *buffer, int cb) {
  230. if (!out_size || !out_pipe) return false;
  231. if (!tx1) {
  232. // Was not init before, for now lets put it at end of descriptor
  233. // TODO: should verify that either don't exceed overlap descsize
  234. // Or that we have taken over this device
  235. tx1 = &descriptor[sizeof(descriptor) - out_size];
  236. tx2 = tx1 - out_size;
  237. }
  238. if ((txstate & 3) == 3) return false; // both transmit buffers are full
  239. if (cb == -1)
  240. cb = out_size;
  241. uint8_t *p = tx1;
  242. if ((txstate & 1) == 0) {
  243. txstate |= 1;
  244. } else {
  245. if (!tx2)
  246. return false; // only one buffer
  247. txstate |= 2;
  248. p = tx2;
  249. }
  250. // copy the users data into our out going buffer
  251. memcpy(p, buffer, cb);
  252. println("USBHIDParser Send packet");
  253. print_hexbytes(buffer, cb);
  254. queue_Data_Transfer(out_pipe, p, cb, this);
  255. println(" Queue_data transfer returned");
  256. return true;
  257. }
  258. void USBHIDParser::setTXBuffers(uint8_t *buffer1, uint8_t *buffer2, uint8_t cb)
  259. {
  260. tx1 = buffer1;
  261. tx2 = buffer2;
  262. }
  263. bool USBHIDParser::sendControlPacket(uint32_t bmRequestType, uint32_t bRequest,
  264. uint32_t wValue, uint32_t wIndex, uint32_t wLength, void *buf)
  265. {
  266. // Use setup structure to build packet
  267. mk_setup(setup, bmRequestType, bRequest, wValue, wIndex, wLength); // ps3 tell to send report 1?
  268. return queue_Control_Transfer(device, &setup, buf, this);
  269. }
  270. // This no-inputs parse is meant to be used when we first get the
  271. // HID report descriptor. It finds all the top level collections
  272. // and allows drivers to claim them. This is always where we
  273. // learn whether the reports will or will not use a Report ID byte.
  274. void USBHIDParser::parse()
  275. {
  276. const uint8_t *p = descriptor;
  277. const uint8_t *end = p + descsize;
  278. uint16_t usage_page = 0;
  279. uint16_t usage = 0;
  280. uint8_t collection_level = 0;
  281. uint8_t topusage_count = 0;
  282. use_report_id = false;
  283. while (p < end) {
  284. uint8_t tag = *p;
  285. if (tag == 0xFE) { // Long Item
  286. p += *p + 3;
  287. continue;
  288. }
  289. uint32_t val;
  290. switch (tag & 0x03) { // Short Item data
  291. case 0: val = 0;
  292. p++;
  293. break;
  294. case 1: val = p[1];
  295. p += 2;
  296. break;
  297. case 2: val = p[1] | (p[2] << 8);
  298. p += 3;
  299. break;
  300. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  301. p += 5;
  302. break;
  303. }
  304. if (p > end) break;
  305. switch (tag & 0xFC) {
  306. case 0x84: // Report ID (global)
  307. use_report_id = true;
  308. break;
  309. case 0x04: // Usage Page (global)
  310. usage_page = val;
  311. break;
  312. case 0x08: // Usage (local)
  313. usage = val;
  314. break;
  315. case 0xA0: // Collection
  316. if (collection_level == 0 && topusage_count < TOPUSAGE_LIST_LEN) {
  317. uint32_t topusage = ((uint32_t)usage_page << 16) | usage;
  318. println("Found top level collection ", topusage, HEX);
  319. //topusage_list[topusage_count] = topusage;
  320. topusage_drivers[topusage_count] = find_driver(topusage);
  321. topusage_count++;
  322. }
  323. collection_level++;
  324. usage = 0;
  325. break;
  326. case 0xC0: // End Collection
  327. if (collection_level > 0) {
  328. collection_level--;
  329. }
  330. case 0x80: // Input
  331. case 0x90: // Output
  332. case 0xB0: // Feature
  333. usage = 0;
  334. break;
  335. }
  336. }
  337. while (topusage_count < TOPUSAGE_LIST_LEN) {
  338. //topusage_list[topusage_count] = 0;
  339. topusage_drivers[topusage_count] = NULL;
  340. topusage_count++;
  341. }
  342. }
  343. // This is a list of all the drivers inherited from the USBHIDInput class.
  344. // Unlike the list of USBDriver (managed in enumeration.cpp), drivers stay
  345. // on this list even when they have claimed a top level collection.
  346. USBHIDInput * USBHIDParser::available_hid_drivers_list = NULL;
  347. void USBHIDParser::driver_ready_for_hid_collection(USBHIDInput *driver)
  348. {
  349. driver->next = NULL;
  350. if (available_hid_drivers_list == NULL) {
  351. available_hid_drivers_list = driver;
  352. } else {
  353. USBHIDInput *last = available_hid_drivers_list;
  354. while (last->next) last = last->next;
  355. last->next = driver;
  356. }
  357. }
  358. // When a new top level collection is found, this function asks drivers
  359. // if they wish to claim it. The driver taking ownership of the
  360. // collection is returned, or NULL if no driver wants it.
  361. USBHIDInput * USBHIDParser::find_driver(uint32_t topusage)
  362. {
  363. println("find_driver");
  364. USBHIDInput *driver = available_hid_drivers_list;
  365. hidclaim_t claim_type;
  366. while (driver) {
  367. println(" driver ", (uint32_t)driver, HEX);
  368. if ((claim_type = driver->claim_collection(this, device, topusage)) != CLAIM_NO) {
  369. if (claim_type == CLAIM_INTERFACE) hid_driver_claimed_control_ = true;
  370. return driver;
  371. }
  372. driver = driver->next;
  373. }
  374. println("No Driver claimed topusage: ", topusage, HEX);
  375. return NULL;
  376. }
  377. // Extract 1 to 32 bits from the data array, starting at bitindex.
  378. static uint32_t bitfield(const uint8_t *data, uint32_t bitindex, uint32_t numbits)
  379. {
  380. uint32_t output = 0;
  381. uint32_t bitcount = 0;
  382. data += (bitindex >> 3);
  383. uint32_t offset = bitindex & 7;
  384. if (offset) {
  385. output = (*data++) >> offset;
  386. bitcount = 8 - offset;
  387. }
  388. while (bitcount < numbits) {
  389. output |= (uint32_t)(*data++) << bitcount;
  390. bitcount += 8;
  391. }
  392. if (bitcount > numbits && numbits < 32) {
  393. output &= ((1 << numbits) - 1);
  394. }
  395. return output;
  396. }
  397. // convert a number with the specified number of bits from unsigned to signed,
  398. // so the result is a proper 32 bit signed integer.
  399. static int32_t signext(uint32_t num, uint32_t bitcount)
  400. {
  401. if (bitcount < 32 && bitcount > 0 && (num & (1 << (bitcount-1)))) {
  402. num |= ~((1 << bitcount) - 1);
  403. }
  404. return (int32_t)num;
  405. }
  406. // convert a tag's value to a signed integer.
  407. static int32_t signedval(uint32_t num, uint8_t tag)
  408. {
  409. tag &= 3;
  410. if (tag == 1) return (int8_t)num;
  411. if (tag == 2) return (int16_t)num;
  412. return (int32_t)num;
  413. }
  414. // parse the report descriptor and use it to feed the fields of the report
  415. // to the drivers which have claimed its top level collections
  416. void USBHIDParser::parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len)
  417. {
  418. const uint8_t *p = descriptor;
  419. const uint8_t *end = p + descsize;
  420. USBHIDInput *driver = NULL;
  421. uint32_t topusage = 0;
  422. uint8_t topusage_index = 0;
  423. uint8_t collection_level = 0;
  424. uint16_t usage[USAGE_LIST_LEN] = {0, 0};
  425. uint8_t usage_count = 0;
  426. uint8_t report_id = 0;
  427. uint16_t report_size = 0;
  428. uint16_t report_count = 0;
  429. uint16_t usage_page = 0;
  430. uint32_t last_usage = 0;
  431. int32_t logical_min = 0;
  432. int32_t logical_max = 0;
  433. uint32_t bitindex = 0;
  434. while (p < end) {
  435. uint8_t tag = *p;
  436. if (tag == 0xFE) { // Long Item (unsupported)
  437. p += p[1] + 3;
  438. continue;
  439. }
  440. uint32_t val;
  441. switch (tag & 0x03) { // Short Item data
  442. case 0: val = 0;
  443. p++;
  444. break;
  445. case 1: val = p[1];
  446. p += 2;
  447. break;
  448. case 2: val = p[1] | (p[2] << 8);
  449. p += 3;
  450. break;
  451. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  452. p += 5;
  453. break;
  454. }
  455. if (p > end) break;
  456. bool reset_local = false;
  457. switch (tag & 0xFC) {
  458. case 0x04: // Usage Page (global)
  459. usage_page = val;
  460. break;
  461. case 0x14: // Logical Minimum (global)
  462. logical_min = signedval(val, tag);
  463. break;
  464. case 0x24: // Logical Maximum (global)
  465. logical_max = signedval(val, tag);
  466. break;
  467. case 0x74: // Report Size (global)
  468. report_size = val;
  469. break;
  470. case 0x94: // Report Count (global)
  471. report_count = val;
  472. break;
  473. case 0x84: // Report ID (global)
  474. report_id = val;
  475. break;
  476. case 0x08: // Usage (local)
  477. if (usage_count < USAGE_LIST_LEN) {
  478. // Usages: 0 is reserved 0x1-0x1f is sort of reserved for top level things like
  479. // 0x1 - Pointer - A collection... So lets try ignoring these
  480. if (val > 0x1f) {
  481. usage[usage_count++] = val;
  482. }
  483. }
  484. break;
  485. case 0x18: // Usage Minimum (local)
  486. usage[0] = val;
  487. usage_count = 255;
  488. break;
  489. case 0x28: // Usage Maximum (local)
  490. usage[1] = val;
  491. usage_count = 255;
  492. break;
  493. case 0xA0: // Collection
  494. if (collection_level == 0) {
  495. topusage = ((uint32_t)usage_page << 16) | usage[0];
  496. driver = NULL;
  497. if (topusage_index < TOPUSAGE_LIST_LEN) {
  498. driver = topusage_drivers[topusage_index++];
  499. }
  500. }
  501. // discard collection info if not top level, hopefully that's ok?
  502. collection_level++;
  503. reset_local = true;
  504. break;
  505. case 0xC0: // End Collection
  506. if (collection_level > 0) {
  507. collection_level--;
  508. if (collection_level == 0 && driver != NULL) {
  509. driver->hid_input_end();
  510. driver = NULL;
  511. }
  512. }
  513. reset_local = true;
  514. break;
  515. case 0x80: // Input
  516. if (use_report_id && (report_id != (type_and_report_id & 0xFF))) {
  517. // completely ignore and do not advance bitindex
  518. // for descriptors of other report IDs
  519. reset_local = true;
  520. break;
  521. }
  522. if ((val & 1) || (driver == NULL)) {
  523. // skip past constant fields or when no driver is listening
  524. bitindex += report_count * report_size;
  525. } else {
  526. println("begin, usage=", topusage, HEX);
  527. println(" type= ", val, HEX);
  528. println(" min= ", logical_min);
  529. println(" max= ", logical_max);
  530. println(" reportcount=", report_count);
  531. println(" usage count=", usage_count);
  532. driver->hid_input_begin(topusage, val, logical_min, logical_max);
  533. println("Input, total bits=", report_count * report_size);
  534. if ((val & 2)) {
  535. // ordinary variable format
  536. uint32_t uindex = 0;
  537. uint32_t uindex_max = 0xffff; // assume no MAX
  538. bool uminmax = false;
  539. if (usage_count > USAGE_LIST_LEN) {
  540. // usage numbers by min/max, not from list
  541. uindex = usage[0];
  542. uindex_max = usage[1];
  543. uminmax = true;
  544. } else if ((report_count > 1) && (usage_count <= 1)) {
  545. // Special cases: Either only one or no usages specified and there are more than one
  546. // report counts .
  547. if (usage_count == 1) {
  548. uindex = usage[0];
  549. } else {
  550. // BUGBUG:: Not sure good place to start? maybe round up from last usage to next higher group up of 0x100?
  551. uindex = (last_usage & 0xff00) + 0x100;
  552. }
  553. uminmax = true;
  554. }
  555. //USBHDBGSerial.printf("TU:%x US:%x %x %d %d: C:%d, %d, MM:%d, %x %x\n", topusage, usage_page, val, logical_min, logical_max,
  556. // report_count, usage_count, uminmax, usage[0], usage[1]);
  557. for (uint32_t i=0; i < report_count; i++) {
  558. uint32_t u;
  559. if (uminmax) {
  560. u = uindex;
  561. if (uindex < uindex_max) uindex++;
  562. } else {
  563. u = usage[uindex++];
  564. if (uindex >= USAGE_LIST_LEN-1) {
  565. uindex = USAGE_LIST_LEN-1;
  566. }
  567. }
  568. last_usage = u; // remember the last one we used...
  569. u |= (uint32_t)usage_page << 16;
  570. print(" usage = ", u, HEX);
  571. uint32_t n = bitfield(data, bitindex, report_size);
  572. if (logical_min >= 0) {
  573. println(" data = ", n);
  574. driver->hid_input_data(u, n);
  575. } else {
  576. int32_t sn = signext(n, report_size);
  577. println(" sdata = ", sn);
  578. driver->hid_input_data(u, sn);
  579. }
  580. bitindex += report_size;
  581. }
  582. } else {
  583. // array format, each item is a usage number
  584. for (uint32_t i=0; i < report_count; i++) {
  585. uint32_t u = bitfield(data, bitindex, report_size);
  586. int n = u;
  587. if (n >= logical_min && n <= logical_max) {
  588. u |= (uint32_t)usage_page << 16;
  589. print(" usage = ", u, HEX);
  590. println(" data = 1");
  591. driver->hid_input_data(u, 1);
  592. } else {
  593. print (" usage =", u, HEX);
  594. print(" out of range: ", logical_min, HEX);
  595. println(" ", logical_max, HEX);
  596. }
  597. bitindex += report_size;
  598. }
  599. }
  600. }
  601. reset_local = true;
  602. break;
  603. case 0x90: // Output
  604. // TODO.....
  605. reset_local = true;
  606. break;
  607. case 0xB0: // Feature
  608. // TODO.....
  609. reset_local = true;
  610. break;
  611. case 0x34: // Physical Minimum (global)
  612. case 0x44: // Physical Maximum (global)
  613. case 0x54: // Unit Exponent (global)
  614. case 0x64: // Unit (global)
  615. break; // Ignore these commonly used tags. Hopefully not needed?
  616. case 0xA4: // Push (yikes! Hope nobody really uses this?!)
  617. case 0xB4: // Pop (yikes! Hope nobody really uses this?!)
  618. case 0x38: // Designator Index (local)
  619. case 0x48: // Designator Minimum (local)
  620. case 0x58: // Designator Maximum (local)
  621. case 0x78: // String Index (local)
  622. case 0x88: // String Minimum (local)
  623. case 0x98: // String Maximum (local)
  624. case 0xA8: // Delimiter (local)
  625. default:
  626. println("Ruh Roh, unsupported tag, not a good thing Scoob ", tag, HEX);
  627. break;
  628. }
  629. if (reset_local) {
  630. usage_count = 0;
  631. usage[0] = 0;
  632. usage[1] = 0;
  633. }
  634. }
  635. }