Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

675 lines
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 && device->idProduct == 0x0268) {
  156. println("send special PS3 feature command");
  157. mk_setup(setup, 0x21, 9, 0x03F4, 0, 4); // ps3 tell to send report 1?
  158. static uint8_t ps3_feature_F4_report[] = {0x42, 0x0c, 0x00, 0x00};
  159. queue_Control_Transfer(device, &setup, ps3_feature_F4_report, this);
  160. }
  161. }
  162. }
  163. void USBHIDParser::in_callback(const Transfer_t *transfer)
  164. {
  165. if (transfer->driver) {
  166. ((USBHIDParser*)(transfer->driver))->in_data(transfer);
  167. }
  168. }
  169. void USBHIDParser::out_callback(const Transfer_t *transfer)
  170. {
  171. //println("USBHIDParser:: out_callback (static)");
  172. if (transfer->driver) {
  173. ((USBHIDParser*)(transfer->driver))->out_data(transfer);
  174. }
  175. }
  176. // When the device goes away, we need to call disconnect_collection()
  177. // for all drivers which claimed a top level collection
  178. void USBHIDParser::disconnect()
  179. {
  180. for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) {
  181. USBHIDInput *driver = topusage_drivers[i];
  182. if (driver) {
  183. driver->disconnect_collection(device);
  184. topusage_drivers[i] = NULL;
  185. }
  186. }
  187. }
  188. // Called when the HID device sends a report
  189. void USBHIDParser::in_data(const Transfer_t *transfer)
  190. {
  191. /*USBHDBGSerial.printf("HID: ");
  192. uint8_t *pb = (uint8_t*)transfer->buffer;
  193. for (uint8_t i = 0; i < transfer->length; i++) {
  194. USBHDBGSerial.printf("%x ",pb[i]);
  195. }
  196. USBHDBGSerial.printf("\n"); */
  197. print("HID: ");
  198. print(use_report_id);
  199. print(" - ");
  200. print_hexbytes(transfer->buffer, transfer->length);
  201. const uint8_t *buf = (const uint8_t *)transfer->buffer;
  202. uint32_t len = transfer->length;
  203. // See if the first top report wishes to bypass the
  204. // parse...
  205. if (!(topusage_drivers[0] && topusage_drivers[0]->hid_process_in_data(transfer))) {
  206. if (use_report_id == false) {
  207. parse(0x0100, buf, len);
  208. } else {
  209. if (len > 1) {
  210. parse(0x0100 | buf[0], buf + 1, len - 1);
  211. }
  212. }
  213. }
  214. queue_Data_Transfer(in_pipe, report, in_size, this);
  215. }
  216. void USBHIDParser::out_data(const Transfer_t *transfer)
  217. {
  218. println("USBHIDParser:out_data called (instance)");
  219. // A packet completed. lets mark it as done and call back
  220. // to top reports handler. We unmark our checkmark to
  221. // handle case where they may want to queue up another one.
  222. if (transfer->buffer == tx1) txstate &= ~1;
  223. if (transfer->buffer == tx2) txstate &= ~2;
  224. if (topusage_drivers[0]) {
  225. topusage_drivers[0]->hid_process_out_data(transfer);
  226. }
  227. }
  228. bool USBHIDParser::sendPacket(const uint8_t *buffer, int cb) {
  229. if (!out_size || !out_pipe) return false;
  230. if (!tx1) {
  231. // Was not init before, for now lets put it at end of descriptor
  232. // TODO: should verify that either don't exceed overlap descsize
  233. // Or that we have taken over this device
  234. tx1 = &descriptor[sizeof(descriptor) - out_size];
  235. tx2 = tx1 - out_size;
  236. }
  237. if ((txstate & 3) == 3) return false; // both transmit buffers are full
  238. if (cb == -1)
  239. cb = out_size;
  240. uint8_t *p = tx1;
  241. if ((txstate & 1) == 0) {
  242. txstate |= 1;
  243. } else {
  244. if (!tx2)
  245. return false; // only one buffer
  246. txstate |= 2;
  247. p = tx2;
  248. }
  249. // copy the users data into our out going buffer
  250. memcpy(p, buffer, cb);
  251. println("USBHIDParser Send packet");
  252. print_hexbytes(buffer, cb);
  253. queue_Data_Transfer(out_pipe, p, cb, this);
  254. println(" Queue_data transfer returned");
  255. return true;
  256. }
  257. void USBHIDParser::setTXBuffers(uint8_t *buffer1, uint8_t *buffer2, uint8_t cb)
  258. {
  259. tx1 = buffer1;
  260. tx2 = buffer2;
  261. }
  262. bool USBHIDParser::sendControlPacket(uint32_t bmRequestType, uint32_t bRequest,
  263. uint32_t wValue, uint32_t wIndex, uint32_t wLength, void *buf)
  264. {
  265. // Use setup structure to build packet
  266. mk_setup(setup, bmRequestType, bRequest, wValue, wIndex, wLength); // ps3 tell to send report 1?
  267. return queue_Control_Transfer(device, &setup, buf, this);
  268. }
  269. // This no-inputs parse is meant to be used when we first get the
  270. // HID report descriptor. It finds all the top level collections
  271. // and allows drivers to claim them. This is always where we
  272. // learn whether the reports will or will not use a Report ID byte.
  273. void USBHIDParser::parse()
  274. {
  275. const uint8_t *p = descriptor;
  276. const uint8_t *end = p + descsize;
  277. uint16_t usage_page = 0;
  278. uint16_t usage = 0;
  279. uint8_t collection_level = 0;
  280. uint8_t topusage_count = 0;
  281. use_report_id = false;
  282. while (p < end) {
  283. uint8_t tag = *p;
  284. if (tag == 0xFE) { // Long Item
  285. p += *p + 3;
  286. continue;
  287. }
  288. uint32_t val;
  289. switch (tag & 0x03) { // Short Item data
  290. case 0: val = 0;
  291. p++;
  292. break;
  293. case 1: val = p[1];
  294. p += 2;
  295. break;
  296. case 2: val = p[1] | (p[2] << 8);
  297. p += 3;
  298. break;
  299. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  300. p += 5;
  301. break;
  302. }
  303. if (p > end) break;
  304. switch (tag & 0xFC) {
  305. case 0x84: // Report ID (global)
  306. use_report_id = true;
  307. break;
  308. case 0x04: // Usage Page (global)
  309. usage_page = val;
  310. break;
  311. case 0x08: // Usage (local)
  312. usage = val;
  313. break;
  314. case 0xA0: // Collection
  315. if (collection_level == 0 && topusage_count < TOPUSAGE_LIST_LEN) {
  316. uint32_t topusage = ((uint32_t)usage_page << 16) | usage;
  317. println("Found top level collection ", topusage, HEX);
  318. //topusage_list[topusage_count] = topusage;
  319. topusage_drivers[topusage_count] = find_driver(topusage);
  320. topusage_count++;
  321. }
  322. collection_level++;
  323. usage = 0;
  324. break;
  325. case 0xC0: // End Collection
  326. if (collection_level > 0) {
  327. collection_level--;
  328. }
  329. case 0x80: // Input
  330. case 0x90: // Output
  331. case 0xB0: // Feature
  332. usage = 0;
  333. break;
  334. }
  335. }
  336. while (topusage_count < TOPUSAGE_LIST_LEN) {
  337. //topusage_list[topusage_count] = 0;
  338. topusage_drivers[topusage_count] = NULL;
  339. topusage_count++;
  340. }
  341. }
  342. // This is a list of all the drivers inherited from the USBHIDInput class.
  343. // Unlike the list of USBDriver (managed in enumeration.cpp), drivers stay
  344. // on this list even when they have claimed a top level collection.
  345. USBHIDInput * USBHIDParser::available_hid_drivers_list = NULL;
  346. void USBHIDParser::driver_ready_for_hid_collection(USBHIDInput *driver)
  347. {
  348. driver->next = NULL;
  349. if (available_hid_drivers_list == NULL) {
  350. available_hid_drivers_list = driver;
  351. } else {
  352. USBHIDInput *last = available_hid_drivers_list;
  353. while (last->next) last = last->next;
  354. last->next = driver;
  355. }
  356. }
  357. // When a new top level collection is found, this function asks drivers
  358. // if they wish to claim it. The driver taking ownership of the
  359. // collection is returned, or NULL if no driver wants it.
  360. USBHIDInput * USBHIDParser::find_driver(uint32_t topusage)
  361. {
  362. println("find_driver");
  363. USBHIDInput *driver = available_hid_drivers_list;
  364. hidclaim_t claim_type;
  365. while (driver) {
  366. println(" driver ", (uint32_t)driver, HEX);
  367. if ((claim_type = driver->claim_collection(this, device, topusage)) != CLAIM_NO) {
  368. if (claim_type == CLAIM_INTERFACE) hid_driver_claimed_control_ = true;
  369. return driver;
  370. }
  371. driver = driver->next;
  372. }
  373. return NULL;
  374. }
  375. // Extract 1 to 32 bits from the data array, starting at bitindex.
  376. static uint32_t bitfield(const uint8_t *data, uint32_t bitindex, uint32_t numbits)
  377. {
  378. uint32_t output = 0;
  379. uint32_t bitcount = 0;
  380. data += (bitindex >> 3);
  381. uint32_t offset = bitindex & 7;
  382. if (offset) {
  383. output = (*data++) >> offset;
  384. bitcount = 8 - offset;
  385. }
  386. while (bitcount < numbits) {
  387. output |= (uint32_t)(*data++) << bitcount;
  388. bitcount += 8;
  389. }
  390. if (bitcount > numbits && numbits < 32) {
  391. output &= ((1 << numbits) - 1);
  392. }
  393. return output;
  394. }
  395. // convert a number with the specified number of bits from unsigned to signed,
  396. // so the result is a proper 32 bit signed integer.
  397. static int32_t signext(uint32_t num, uint32_t bitcount)
  398. {
  399. if (bitcount < 32 && bitcount > 0 && (num & (1 << (bitcount-1)))) {
  400. num |= ~((1 << bitcount) - 1);
  401. }
  402. return (int32_t)num;
  403. }
  404. // convert a tag's value to a signed integer.
  405. static int32_t signedval(uint32_t num, uint8_t tag)
  406. {
  407. tag &= 3;
  408. if (tag == 1) return (int8_t)num;
  409. if (tag == 2) return (int16_t)num;
  410. return (int32_t)num;
  411. }
  412. // parse the report descriptor and use it to feed the fields of the report
  413. // to the drivers which have claimed its top level collections
  414. void USBHIDParser::parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len)
  415. {
  416. const uint8_t *p = descriptor;
  417. const uint8_t *end = p + descsize;
  418. USBHIDInput *driver = NULL;
  419. uint32_t topusage = 0;
  420. uint8_t topusage_index = 0;
  421. uint8_t collection_level = 0;
  422. uint16_t usage[USAGE_LIST_LEN] = {0, 0};
  423. uint8_t usage_count = 0;
  424. uint8_t report_id = 0;
  425. uint16_t report_size = 0;
  426. uint16_t report_count = 0;
  427. uint16_t usage_page = 0;
  428. uint32_t last_usage = 0;
  429. int32_t logical_min = 0;
  430. int32_t logical_max = 0;
  431. uint32_t bitindex = 0;
  432. while (p < end) {
  433. uint8_t tag = *p;
  434. if (tag == 0xFE) { // Long Item (unsupported)
  435. p += p[1] + 3;
  436. continue;
  437. }
  438. uint32_t val;
  439. switch (tag & 0x03) { // Short Item data
  440. case 0: val = 0;
  441. p++;
  442. break;
  443. case 1: val = p[1];
  444. p += 2;
  445. break;
  446. case 2: val = p[1] | (p[2] << 8);
  447. p += 3;
  448. break;
  449. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  450. p += 5;
  451. break;
  452. }
  453. if (p > end) break;
  454. bool reset_local = false;
  455. switch (tag & 0xFC) {
  456. case 0x04: // Usage Page (global)
  457. usage_page = val;
  458. break;
  459. case 0x14: // Logical Minimum (global)
  460. logical_min = signedval(val, tag);
  461. break;
  462. case 0x24: // Logical Maximum (global)
  463. logical_max = signedval(val, tag);
  464. break;
  465. case 0x74: // Report Size (global)
  466. report_size = val;
  467. break;
  468. case 0x94: // Report Count (global)
  469. report_count = val;
  470. break;
  471. case 0x84: // Report ID (global)
  472. report_id = val;
  473. break;
  474. case 0x08: // Usage (local)
  475. if (usage_count < USAGE_LIST_LEN) {
  476. // Usages: 0 is reserved 0x1-0x1f is sort of reserved for top level things like
  477. // 0x1 - Pointer - A collection... So lets try ignoring these
  478. if (val > 0x1f) {
  479. usage[usage_count++] = val;
  480. }
  481. }
  482. break;
  483. case 0x18: // Usage Minimum (local)
  484. usage[0] = val;
  485. usage_count = 255;
  486. break;
  487. case 0x28: // Usage Maximum (local)
  488. usage[1] = val;
  489. usage_count = 255;
  490. break;
  491. case 0xA0: // Collection
  492. if (collection_level == 0) {
  493. topusage = ((uint32_t)usage_page << 16) | usage[0];
  494. driver = NULL;
  495. if (topusage_index < TOPUSAGE_LIST_LEN) {
  496. driver = topusage_drivers[topusage_index++];
  497. }
  498. }
  499. // discard collection info if not top level, hopefully that's ok?
  500. collection_level++;
  501. reset_local = true;
  502. break;
  503. case 0xC0: // End Collection
  504. if (collection_level > 0) {
  505. collection_level--;
  506. if (collection_level == 0 && driver != NULL) {
  507. driver->hid_input_end();
  508. driver = NULL;
  509. }
  510. }
  511. reset_local = true;
  512. break;
  513. case 0x80: // Input
  514. if (use_report_id && (report_id != (type_and_report_id & 0xFF))) {
  515. // completely ignore and do not advance bitindex
  516. // for descriptors of other report IDs
  517. reset_local = true;
  518. break;
  519. }
  520. if ((val & 1) || (driver == NULL)) {
  521. // skip past constant fields or when no driver is listening
  522. bitindex += report_count * report_size;
  523. } else {
  524. println("begin, usage=", topusage, HEX);
  525. println(" type= ", val, HEX);
  526. println(" min= ", logical_min);
  527. println(" max= ", logical_max);
  528. println(" reportcount=", report_count);
  529. println(" usage count=", usage_count);
  530. driver->hid_input_begin(topusage, val, logical_min, logical_max);
  531. println("Input, total bits=", report_count * report_size);
  532. if ((val & 2)) {
  533. // ordinary variable format
  534. uint32_t uindex = 0;
  535. uint32_t uindex_max = 0xffff; // assume no MAX
  536. bool uminmax = false;
  537. if (usage_count > USAGE_LIST_LEN) {
  538. // usage numbers by min/max, not from list
  539. uindex = usage[0];
  540. uindex_max = usage[1];
  541. uminmax = true;
  542. } else if ((report_count > 1) && (usage_count <= 1)) {
  543. // Special cases: Either only one or no usages specified and there are more than one
  544. // report counts .
  545. if (usage_count == 1) {
  546. uindex = usage[0];
  547. } else {
  548. // BUGBUG:: Not sure good place to start? maybe round up from last usage to next higher group up of 0x100?
  549. uindex = (last_usage & 0xff00) + 0x100;
  550. }
  551. uminmax = true;
  552. }
  553. //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,
  554. // report_count, usage_count, uminmax, usage[0], usage[1]);
  555. for (uint32_t i=0; i < report_count; i++) {
  556. uint32_t u;
  557. if (uminmax) {
  558. u = uindex;
  559. if (uindex < uindex_max) uindex++;
  560. } else {
  561. u = usage[uindex++];
  562. if (uindex >= USAGE_LIST_LEN-1) {
  563. uindex = USAGE_LIST_LEN-1;
  564. }
  565. }
  566. last_usage = u; // remember the last one we used...
  567. u |= (uint32_t)usage_page << 16;
  568. print(" usage = ", u, HEX);
  569. uint32_t n = bitfield(data, bitindex, report_size);
  570. if (logical_min >= 0) {
  571. println(" data = ", n);
  572. driver->hid_input_data(u, n);
  573. } else {
  574. int32_t sn = signext(n, report_size);
  575. println(" sdata = ", sn);
  576. driver->hid_input_data(u, sn);
  577. }
  578. bitindex += report_size;
  579. }
  580. } else {
  581. // array format, each item is a usage number
  582. for (uint32_t i=0; i < report_count; i++) {
  583. uint32_t u = bitfield(data, bitindex, report_size);
  584. int n = u;
  585. if (n >= logical_min && n <= logical_max) {
  586. u |= (uint32_t)usage_page << 16;
  587. print(" usage = ", u, HEX);
  588. println(" data = 1");
  589. driver->hid_input_data(u, 1);
  590. } else {
  591. print (" usage =", u, HEX);
  592. print(" out of range: ", logical_min, HEX);
  593. println(" ", logical_max, HEX);
  594. }
  595. bitindex += report_size;
  596. }
  597. }
  598. }
  599. reset_local = true;
  600. break;
  601. case 0x90: // Output
  602. // TODO.....
  603. reset_local = true;
  604. break;
  605. case 0xB0: // Feature
  606. // TODO.....
  607. reset_local = true;
  608. break;
  609. case 0x34: // Physical Minimum (global)
  610. case 0x44: // Physical Maximum (global)
  611. case 0x54: // Unit Exponent (global)
  612. case 0x64: // Unit (global)
  613. break; // Ignore these commonly used tags. Hopefully not needed?
  614. case 0xA4: // Push (yikes! Hope nobody really uses this?!)
  615. case 0xB4: // Pop (yikes! Hope nobody really uses this?!)
  616. case 0x38: // Designator Index (local)
  617. case 0x48: // Designator Minimum (local)
  618. case 0x58: // Designator Maximum (local)
  619. case 0x78: // String Index (local)
  620. case 0x88: // String Minimum (local)
  621. case 0x98: // String Maximum (local)
  622. case 0xA8: // Delimiter (local)
  623. default:
  624. println("Ruh Roh, unsupported tag, not a good thing Scoob ", tag, HEX);
  625. break;
  626. }
  627. if (reset_local) {
  628. usage_count = 0;
  629. usage[0] = 0;
  630. usage[1] = 0;
  631. }
  632. }
  633. }