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.

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