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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. /*Serial.print("HID: ");
  192. uint8_t *pb = (uint8_t*)transfer->buffer;
  193. for (uint8_t i = 0; i < transfer->length; i++) {
  194. Serial.print(pb[i], HEX);
  195. Serial.print(" ");
  196. }
  197. Serial.println(); */
  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) {
  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. uint8_t *p = tx1;
  240. if ((txstate & 1) == 0) {
  241. txstate |= 1;
  242. } else {
  243. txstate |= 2;
  244. p = tx2;
  245. }
  246. // copy the users data into our out going buffer
  247. memcpy(p, buffer, out_size);
  248. println("USBHIDParser Send packet");
  249. print_hexbytes(buffer, out_size);
  250. queue_Data_Transfer(out_pipe, p, out_size, this);
  251. println(" Queue_data transfer returned");
  252. return true;
  253. }
  254. // This no-inputs parse is meant to be used when we first get the
  255. // HID report descriptor. It finds all the top level collections
  256. // and allows drivers to claim them. This is always where we
  257. // learn whether the reports will or will not use a Report ID byte.
  258. void USBHIDParser::parse()
  259. {
  260. const uint8_t *p = descriptor;
  261. const uint8_t *end = p + descsize;
  262. uint16_t usage_page = 0;
  263. uint16_t usage = 0;
  264. uint8_t collection_level = 0;
  265. uint8_t topusage_count = 0;
  266. use_report_id = false;
  267. while (p < end) {
  268. uint8_t tag = *p;
  269. if (tag == 0xFE) { // Long Item
  270. p += *p + 3;
  271. continue;
  272. }
  273. uint32_t val;
  274. switch (tag & 0x03) { // Short Item data
  275. case 0: val = 0;
  276. p++;
  277. break;
  278. case 1: val = p[1];
  279. p += 2;
  280. break;
  281. case 2: val = p[1] | (p[2] << 8);
  282. p += 3;
  283. break;
  284. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  285. p += 5;
  286. break;
  287. }
  288. if (p > end) break;
  289. switch (tag & 0xFC) {
  290. case 0x84: // Report ID (global)
  291. use_report_id = true;
  292. break;
  293. case 0x04: // Usage Page (global)
  294. usage_page = val;
  295. break;
  296. case 0x08: // Usage (local)
  297. usage = val;
  298. break;
  299. case 0xA0: // Collection
  300. if (collection_level == 0 && topusage_count < TOPUSAGE_LIST_LEN) {
  301. uint32_t topusage = ((uint32_t)usage_page << 16) | usage;
  302. println("Found top level collection ", topusage, HEX);
  303. //topusage_list[topusage_count] = topusage;
  304. topusage_drivers[topusage_count] = find_driver(topusage);
  305. topusage_count++;
  306. }
  307. collection_level++;
  308. usage = 0;
  309. break;
  310. case 0xC0: // End Collection
  311. if (collection_level > 0) {
  312. collection_level--;
  313. }
  314. case 0x80: // Input
  315. case 0x90: // Output
  316. case 0xB0: // Feature
  317. usage = 0;
  318. break;
  319. }
  320. }
  321. while (topusage_count < TOPUSAGE_LIST_LEN) {
  322. //topusage_list[topusage_count] = 0;
  323. topusage_drivers[topusage_count] = NULL;
  324. topusage_count++;
  325. }
  326. }
  327. // This is a list of all the drivers inherited from the USBHIDInput class.
  328. // Unlike the list of USBDriver (managed in enumeration.cpp), drivers stay
  329. // on this list even when they have claimed a top level collection.
  330. USBHIDInput * USBHIDParser::available_hid_drivers_list = NULL;
  331. void USBHIDParser::driver_ready_for_hid_collection(USBHIDInput *driver)
  332. {
  333. driver->next = NULL;
  334. if (available_hid_drivers_list == NULL) {
  335. available_hid_drivers_list = driver;
  336. } else {
  337. USBHIDInput *last = available_hid_drivers_list;
  338. while (last->next) last = last->next;
  339. last->next = driver;
  340. }
  341. }
  342. // When a new top level collection is found, this function asks drivers
  343. // if they wish to claim it. The driver taking ownership of the
  344. // collection is returned, or NULL if no driver wants it.
  345. USBHIDInput * USBHIDParser::find_driver(uint32_t topusage)
  346. {
  347. println("find_driver");
  348. USBHIDInput *driver = available_hid_drivers_list;
  349. hidclaim_t claim_type;
  350. while (driver) {
  351. println(" driver ", (uint32_t)driver, HEX);
  352. if ((claim_type = driver->claim_collection(this, device, topusage)) != CLAIM_NO) {
  353. if (claim_type == CLAIM_INTERFACE) hid_driver_claimed_control_ = true;
  354. return driver;
  355. }
  356. driver = driver->next;
  357. }
  358. return NULL;
  359. }
  360. // Extract 1 to 32 bits from the data array, starting at bitindex.
  361. static uint32_t bitfield(const uint8_t *data, uint32_t bitindex, uint32_t numbits)
  362. {
  363. uint32_t output = 0;
  364. uint32_t bitcount = 0;
  365. data += (bitindex >> 3);
  366. uint32_t offset = bitindex & 7;
  367. if (offset) {
  368. output = (*data++) >> offset;
  369. bitcount = 8 - offset;
  370. }
  371. while (bitcount < numbits) {
  372. output |= (uint32_t)(*data++) << bitcount;
  373. bitcount += 8;
  374. }
  375. if (bitcount > numbits && numbits < 32) {
  376. output &= ((1 << numbits) - 1);
  377. }
  378. return output;
  379. }
  380. // convert a number with the specified number of bits from unsigned to signed,
  381. // so the result is a proper 32 bit signed integer.
  382. static int32_t signext(uint32_t num, uint32_t bitcount)
  383. {
  384. if (bitcount < 32 && bitcount > 0 && (num & (1 << (bitcount-1)))) {
  385. num |= ~((1 << bitcount) - 1);
  386. }
  387. return (int32_t)num;
  388. }
  389. // convert a tag's value to a signed integer.
  390. static int32_t signedval(uint32_t num, uint8_t tag)
  391. {
  392. tag &= 3;
  393. if (tag == 1) return (int8_t)num;
  394. if (tag == 2) return (int16_t)num;
  395. return (int32_t)num;
  396. }
  397. // parse the report descriptor and use it to feed the fields of the report
  398. // to the drivers which have claimed its top level collections
  399. void USBHIDParser::parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len)
  400. {
  401. const uint8_t *p = descriptor;
  402. const uint8_t *end = p + descsize;
  403. USBHIDInput *driver = NULL;
  404. uint32_t topusage = 0;
  405. uint8_t topusage_index = 0;
  406. uint8_t collection_level = 0;
  407. uint16_t usage[USAGE_LIST_LEN] = {0, 0};
  408. uint8_t usage_count = 0;
  409. uint8_t report_id = 0;
  410. uint16_t report_size = 0;
  411. uint16_t report_count = 0;
  412. uint16_t usage_page = 0;
  413. int32_t logical_min = 0;
  414. int32_t logical_max = 0;
  415. uint32_t bitindex = 0;
  416. while (p < end) {
  417. uint8_t tag = *p;
  418. if (tag == 0xFE) { // Long Item (unsupported)
  419. p += p[1] + 3;
  420. continue;
  421. }
  422. uint32_t val;
  423. switch (tag & 0x03) { // Short Item data
  424. case 0: val = 0;
  425. p++;
  426. break;
  427. case 1: val = p[1];
  428. p += 2;
  429. break;
  430. case 2: val = p[1] | (p[2] << 8);
  431. p += 3;
  432. break;
  433. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  434. p += 5;
  435. break;
  436. }
  437. if (p > end) break;
  438. bool reset_local = false;
  439. switch (tag & 0xFC) {
  440. case 0x04: // Usage Page (global)
  441. usage_page = val;
  442. break;
  443. case 0x14: // Logical Minimum (global)
  444. logical_min = signedval(val, tag);
  445. break;
  446. case 0x24: // Logical Maximum (global)
  447. logical_max = signedval(val, tag);
  448. break;
  449. case 0x74: // Report Size (global)
  450. report_size = val;
  451. break;
  452. case 0x94: // Report Count (global)
  453. report_count = val;
  454. break;
  455. case 0x84: // Report ID (global)
  456. report_id = val;
  457. break;
  458. case 0x08: // Usage (local)
  459. if (usage_count < USAGE_LIST_LEN) {
  460. // Usages: 0 is reserved 0x1-0x1f is sort of reserved for top level things like
  461. // 0x1 - Pointer - A collection... So lets try ignoring these
  462. if (val > 0x1f) {
  463. usage[usage_count++] = val;
  464. }
  465. }
  466. break;
  467. case 0x18: // Usage Minimum (local)
  468. usage[0] = val;
  469. usage_count = 255;
  470. break;
  471. case 0x28: // Usage Maximum (local)
  472. usage[1] = val;
  473. usage_count = 255;
  474. break;
  475. case 0xA0: // Collection
  476. if (collection_level == 0) {
  477. topusage = ((uint32_t)usage_page << 16) | usage[0];
  478. driver = NULL;
  479. if (topusage_index < TOPUSAGE_LIST_LEN) {
  480. driver = topusage_drivers[topusage_index++];
  481. }
  482. }
  483. // discard collection info if not top level, hopefully that's ok?
  484. collection_level++;
  485. reset_local = true;
  486. break;
  487. case 0xC0: // End Collection
  488. if (collection_level > 0) {
  489. collection_level--;
  490. if (collection_level == 0 && driver != NULL) {
  491. driver->hid_input_end();
  492. driver = NULL;
  493. }
  494. }
  495. reset_local = true;
  496. break;
  497. case 0x80: // Input
  498. if (use_report_id && (report_id != (type_and_report_id & 0xFF))) {
  499. // completely ignore and do not advance bitindex
  500. // for descriptors of other report IDs
  501. reset_local = true;
  502. break;
  503. }
  504. if ((val & 1) || (driver == NULL)) {
  505. // skip past constant fields or when no driver is listening
  506. bitindex += report_count * report_size;
  507. } else {
  508. println("begin, usage=", topusage, HEX);
  509. println(" type= ", val, HEX);
  510. println(" min= ", logical_min);
  511. println(" max= ", logical_max);
  512. println(" reportcount=", report_count);
  513. println(" usage count=", usage_count);
  514. driver->hid_input_begin(topusage, val, logical_min, logical_max);
  515. println("Input, total bits=", report_count * report_size);
  516. if ((val & 2)) {
  517. // ordinary variable format
  518. uint32_t uindex = 0;
  519. bool uminmax = false;
  520. if (usage_count > USAGE_LIST_LEN || usage_count == 0) {
  521. // usage numbers by min/max, not from list
  522. uindex = usage[0];
  523. uminmax = true;
  524. }
  525. for (uint32_t i=0; i < report_count; i++) {
  526. uint32_t u;
  527. if (uminmax) {
  528. u = uindex;
  529. if (uindex < usage[1]) uindex++;
  530. } else {
  531. u = usage[uindex++];
  532. if (uindex >= USAGE_LIST_LEN-1) {
  533. uindex = USAGE_LIST_LEN-1;
  534. }
  535. }
  536. u |= (uint32_t)usage_page << 16;
  537. print(" usage = ", u, HEX);
  538. uint32_t n = bitfield(data, bitindex, report_size);
  539. if (logical_min >= 0) {
  540. println(" data = ", n);
  541. driver->hid_input_data(u, n);
  542. } else {
  543. int32_t sn = signext(n, report_size);
  544. println(" sdata = ", sn);
  545. driver->hid_input_data(u, sn);
  546. }
  547. bitindex += report_size;
  548. }
  549. } else {
  550. // array format, each item is a usage number
  551. for (uint32_t i=0; i < report_count; i++) {
  552. uint32_t u = bitfield(data, bitindex, report_size);
  553. int n = u;
  554. if (n >= logical_min && n <= logical_max) {
  555. u |= (uint32_t)usage_page << 16;
  556. print(" usage = ", u, HEX);
  557. println(" data = 1");
  558. driver->hid_input_data(u, 1);
  559. } else {
  560. print (" usage =", u, HEX);
  561. print(" out of range: ", logical_min, HEX);
  562. println(" ", logical_max, HEX);
  563. }
  564. bitindex += report_size;
  565. }
  566. }
  567. }
  568. reset_local = true;
  569. break;
  570. case 0x90: // Output
  571. // TODO.....
  572. reset_local = true;
  573. break;
  574. case 0xB0: // Feature
  575. // TODO.....
  576. reset_local = true;
  577. break;
  578. case 0x34: // Physical Minimum (global)
  579. case 0x44: // Physical Maximum (global)
  580. case 0x54: // Unit Exponent (global)
  581. case 0x64: // Unit (global)
  582. break; // Ignore these commonly used tags. Hopefully not needed?
  583. case 0xA4: // Push (yikes! Hope nobody really uses this?!)
  584. case 0xB4: // Pop (yikes! Hope nobody really uses this?!)
  585. case 0x38: // Designator Index (local)
  586. case 0x48: // Designator Minimum (local)
  587. case 0x58: // Designator Maximum (local)
  588. case 0x78: // String Index (local)
  589. case 0x88: // String Minimum (local)
  590. case 0x98: // String Maximum (local)
  591. case 0xA8: // Delimiter (local)
  592. default:
  593. println("Ruh Roh, unsupported tag, not a good thing Scoob ", tag, HEX);
  594. break;
  595. }
  596. if (reset_local) {
  597. usage_count = 0;
  598. usage[0] = 0;
  599. usage[1] = 0;
  600. }
  601. }
  602. }