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.

562 line
17KB

  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. void USBHIDParser::init()
  32. {
  33. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  34. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  35. driver_ready_for_device(this);
  36. }
  37. bool USBHIDParser::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  38. {
  39. println("HIDParser claim this=", (uint32_t)this, HEX);
  40. // only claim at interface level
  41. if (type != 1) return false;
  42. if (len < 9+9+7) return false;
  43. // interface descriptor
  44. uint32_t numendpoint = descriptors[4];
  45. if (numendpoint < 1 || numendpoint > 2) return false;
  46. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  47. println(" bInterfaceClass = ", descriptors[5]);
  48. println(" bInterfaceSubClass = ", descriptors[6]);
  49. println(" bInterfaceProtocol = ", descriptors[7]);
  50. // hid interface descriptor
  51. uint32_t hidlen = descriptors[9];
  52. if (hidlen < 9) return false;
  53. if (descriptors[10] != 33) return false; // descriptor type, 33=HID
  54. if (descriptors[14] < 1) return false; // must be at least 1 extra descriptor
  55. if (hidlen != (uint32_t)(6 + descriptors[14] * 3)) return false; // must be correct size
  56. if (9 + hidlen > len) return false;
  57. uint32_t i=0;
  58. while (1) {
  59. if (descriptors[15 + i * 3] == 34) { // found HID report descriptor
  60. descsize = descriptors[16 + i * 3] | (descriptors[17 + i * 3] << 8);
  61. println("report descriptor size = ", descsize);
  62. break;
  63. }
  64. i++;
  65. if (i >= descriptors[14]) return false;
  66. }
  67. if (descsize > sizeof(descriptor)) return false; // can't fit the report descriptor
  68. // endpoint descriptor(s)
  69. uint32_t offset = 9 + hidlen;
  70. if (len < offset + numendpoint * 7) return false; // not enough data
  71. if (numendpoint == 1) {
  72. println("Single endpoint HID:");
  73. if (descriptors[offset] != 7) return false;
  74. if (descriptors[offset+1] != 5) return false; // endpoint descriptor
  75. if (descriptors[offset+3] != 3) return false; // must be interrupt type
  76. uint32_t endpoint = descriptors[offset+2];
  77. uint32_t size = descriptors[offset+4] | (descriptors[offset+5] << 8);
  78. uint32_t interval = descriptors[offset+6];
  79. println(" endpoint = ", endpoint, HEX);
  80. println(" size = ", size);
  81. println(" interval = ", interval);
  82. if ((endpoint & 0x0F) == 0) return false;
  83. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  84. in_pipe = new_Pipe(dev, 3, endpoint & 0x0F, 1, size, interval);
  85. out_pipe = NULL;
  86. in_size = size;
  87. } else {
  88. println("Two endpoint HID:");
  89. if (descriptors[offset] != 7) return false;
  90. if (descriptors[offset+1] != 5) return false; // endpoint descriptor
  91. if (descriptors[offset+3] != 3) return false; // must be interrupt type
  92. uint32_t endpoint1 = descriptors[offset+2];
  93. uint32_t size1 = descriptors[offset+4] | (descriptors[offset+5] << 8);
  94. uint32_t interval1 = descriptors[offset+6];
  95. println(" endpoint = ", endpoint1, HEX);
  96. println(" size = ", size1);
  97. println(" interval = ", interval1);
  98. if ((endpoint1 & 0x0F) == 0) return false;
  99. if (descriptors[offset+7] != 7) return false;
  100. if (descriptors[offset+8] != 5) return false; // endpoint descriptor
  101. if (descriptors[offset+10] != 3) return false; // must be interrupt type
  102. uint32_t endpoint2 = descriptors[offset+9];
  103. uint32_t size2 = descriptors[offset+11] | (descriptors[offset+12] << 8);
  104. uint32_t interval2 = descriptors[offset+13];
  105. println(" endpoint = ", endpoint2, HEX);
  106. println(" size = ", size2);
  107. println(" interval = ", interval2);
  108. if ((endpoint2 & 0x0F) == 0) return false;
  109. if (((endpoint1 & 0xF0) == 0x80) && ((endpoint2 & 0xF0) == 0)) {
  110. // first endpoint is IN, second endpoint is OUT
  111. in_pipe = new_Pipe(dev, 3, endpoint1 & 0x0F, 1, size1, interval1);
  112. //out_pipe = new_Pipe(dev, 3, endpoint2, 0, size2, interval2);
  113. out_pipe = NULL; // TODO; fixme
  114. in_size = size1;
  115. out_size = size2;
  116. } else if (((endpoint1 & 0xF0) == 0) && ((endpoint2 & 0xF0) == 0x80)) {
  117. // first endpoint is OUT, second endpoint is IN
  118. in_pipe = new_Pipe(dev, 3, endpoint2 & 0x0F, 1, size2, interval2);
  119. //out_pipe = new_Pipe(dev, 3, endpoint1, 0, size1, interval1);
  120. out_pipe = NULL; // TODO; fixme
  121. in_size = size2;
  122. out_size = size1;
  123. } else {
  124. return false;
  125. }
  126. out_pipe->callback_function = out_callback;
  127. }
  128. in_pipe->callback_function = in_callback;
  129. for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) {
  130. //topusage_list[i] = 0;
  131. topusage_drivers[i] = NULL;
  132. }
  133. // request the HID report descriptor
  134. mk_setup(setup, 0x81, 6, 0x2200, descriptors[2], descsize); // get report desc
  135. queue_Control_Transfer(dev, &setup, descriptor, this);
  136. return true;
  137. }
  138. void USBHIDParser::control(const Transfer_t *transfer)
  139. {
  140. println("control callback (hid)");
  141. print_hexbytes(transfer->buffer, transfer->length);
  142. // To decode hex dump to human readable HID report summary:
  143. // http://eleccelerator.com/usbdescreqparser/
  144. uint32_t mesg = transfer->setup.word1;
  145. //println(" mesg = ", mesg, HEX);
  146. if (mesg == 0x22000681 && transfer->length == descsize) { // HID report descriptor
  147. println(" got report descriptor");
  148. parse();
  149. queue_Data_Transfer(in_pipe, report, in_size, this);
  150. }
  151. }
  152. void USBHIDParser::in_callback(const Transfer_t *transfer)
  153. {
  154. if (transfer->driver) {
  155. ((USBHIDParser*)(transfer->driver))->in_data(transfer);
  156. }
  157. }
  158. void USBHIDParser::out_callback(const Transfer_t *transfer)
  159. {
  160. if (transfer->driver) {
  161. ((USBHIDParser*)(transfer->driver))->out_data(transfer);
  162. }
  163. }
  164. // When the device goes away, we need to call disconnect_collection()
  165. // for all drivers which claimed a top level collection
  166. void USBHIDParser::disconnect()
  167. {
  168. for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) {
  169. USBHIDInput *driver = topusage_drivers[i];
  170. if (driver) {
  171. driver->disconnect_collection(device);
  172. topusage_drivers[i] = NULL;
  173. }
  174. }
  175. }
  176. // Called when the HID device sends a report
  177. void USBHIDParser::in_data(const Transfer_t *transfer)
  178. {
  179. print("HID: ");
  180. print_hexbytes(transfer->buffer, transfer->length);
  181. const uint8_t *buf = (const uint8_t *)transfer->buffer;
  182. uint32_t len = transfer->length;
  183. if (use_report_id == false) {
  184. parse(0x0100, buf, len);
  185. } else {
  186. if (len > 1) {
  187. parse(0x0100 | buf[0], buf + 1, len - 1);
  188. }
  189. }
  190. queue_Data_Transfer(in_pipe, report, in_size, this);
  191. }
  192. void USBHIDParser::out_data(const Transfer_t *transfer)
  193. {
  194. }
  195. // This no-inputs parse is meant to be used when we first get the
  196. // HID report descriptor. It finds all the top level collections
  197. // and allows drivers to claim them. This is always where we
  198. // learn whether the reports will or will not use a Report ID byte.
  199. void USBHIDParser::parse()
  200. {
  201. const uint8_t *p = descriptor;
  202. const uint8_t *end = p + descsize;
  203. uint16_t usage_page = 0;
  204. uint16_t usage = 0;
  205. uint8_t collection_level = 0;
  206. uint8_t topusage_count = 0;
  207. use_report_id = false;
  208. while (p < end) {
  209. uint8_t tag = *p;
  210. if (tag == 0xFE) { // Long Item
  211. p += *p + 3;
  212. continue;
  213. }
  214. uint32_t val;
  215. switch (tag & 0x03) { // Short Item data
  216. case 0: val = 0;
  217. p++;
  218. break;
  219. case 1: val = p[1];
  220. p += 2;
  221. break;
  222. case 2: val = p[1] | (p[2] << 8);
  223. p += 3;
  224. break;
  225. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  226. p += 5;
  227. break;
  228. }
  229. if (p > end) break;
  230. switch (tag & 0xFC) {
  231. case 0x84: // Report ID (global)
  232. use_report_id = true;
  233. break;
  234. case 0x04: // Usage Page (global)
  235. usage_page = val;
  236. break;
  237. case 0x08: // Usage (local)
  238. usage = val;
  239. break;
  240. case 0xA0: // Collection
  241. if (collection_level == 0 && topusage_count < TOPUSAGE_LIST_LEN) {
  242. uint32_t topusage = ((uint32_t)usage_page << 16) | usage;
  243. println("Found top level collection ", topusage, HEX);
  244. //topusage_list[topusage_count] = topusage;
  245. topusage_drivers[topusage_count] = find_driver(topusage);
  246. topusage_count++;
  247. }
  248. collection_level++;
  249. usage = 0;
  250. break;
  251. case 0xC0: // End Collection
  252. if (collection_level > 0) {
  253. collection_level--;
  254. }
  255. case 0x80: // Input
  256. case 0x90: // Output
  257. case 0xB0: // Feature
  258. usage = 0;
  259. break;
  260. }
  261. }
  262. while (topusage_count < TOPUSAGE_LIST_LEN) {
  263. //topusage_list[topusage_count] = 0;
  264. topusage_drivers[topusage_count] = NULL;
  265. topusage_count++;
  266. }
  267. }
  268. // This is a list of all the drivers inherited from the USBHIDInput class.
  269. // Unlike the list of USBDriver (managed in enumeration.cpp), drivers stay
  270. // on this list even when they have claimed a top level collection.
  271. USBHIDInput * USBHIDParser::available_hid_drivers_list = NULL;
  272. void USBHIDParser::driver_ready_for_hid_collection(USBHIDInput *driver)
  273. {
  274. driver->next = NULL;
  275. if (available_hid_drivers_list == NULL) {
  276. available_hid_drivers_list = driver;
  277. } else {
  278. USBHIDInput *last = available_hid_drivers_list;
  279. while (last->next) last = last->next;
  280. last->next = driver;
  281. }
  282. }
  283. // When a new top level collection is found, this function asks drivers
  284. // if they wish to claim it. The driver taking ownership of the
  285. // collection is returned, or NULL if no driver wants it.
  286. USBHIDInput * USBHIDParser::find_driver(uint32_t topusage)
  287. {
  288. println("find_driver");
  289. USBHIDInput *driver = available_hid_drivers_list;
  290. while (driver) {
  291. println(" driver ", (uint32_t)driver, HEX);
  292. if (driver->claim_collection(device, topusage)) {
  293. return driver;
  294. }
  295. driver = driver->next;
  296. }
  297. return NULL;
  298. }
  299. // Extract 1 to 32 bits from the data array, starting at bitindex.
  300. static uint32_t bitfield(const uint8_t *data, uint32_t bitindex, uint32_t numbits)
  301. {
  302. uint32_t output = 0;
  303. uint32_t bitcount = 0;
  304. data += (bitindex >> 3);
  305. uint32_t offset = bitindex & 7;
  306. if (offset) {
  307. output = (*data++) >> offset;
  308. bitcount = 8 - offset;
  309. }
  310. while (bitcount < numbits) {
  311. output |= (uint32_t)(*data++) << bitcount;
  312. bitcount += 8;
  313. }
  314. if (bitcount > numbits && numbits < 32) {
  315. output &= ((1 << numbits) - 1);
  316. }
  317. return output;
  318. }
  319. // convert a number with the specified number of bits from unsigned to signed,
  320. // so the result is a proper 32 bit signed integer.
  321. static int32_t signext(uint32_t num, uint32_t bitcount)
  322. {
  323. if (bitcount < 32 && bitcount > 0 && (num & (1 << (bitcount-1)))) {
  324. num |= ~((1 << bitcount) - 1);
  325. }
  326. return (int32_t)num;
  327. }
  328. // convert a tag's value to a signed integer.
  329. static int32_t signedval(uint32_t num, uint8_t tag)
  330. {
  331. tag &= 3;
  332. if (tag == 1) return (int8_t)num;
  333. if (tag == 2) return (int16_t)num;
  334. return (int32_t)num;
  335. }
  336. // parse the report descriptor and use it to feed the fields of the report
  337. // to the drivers which have claimed its top level collections
  338. void USBHIDParser::parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len)
  339. {
  340. const uint8_t *p = descriptor;
  341. const uint8_t *end = p + descsize;
  342. USBHIDInput *driver = NULL;
  343. uint32_t topusage = 0;
  344. uint8_t topusage_index = 0;
  345. uint8_t collection_level = 0;
  346. uint16_t usage[USAGE_LIST_LEN] = {0, 0};
  347. uint8_t usage_count = 0;
  348. uint8_t report_id = 0;
  349. uint16_t report_size = 0;
  350. uint16_t report_count = 0;
  351. uint16_t usage_page = 0;
  352. int32_t logical_min = 0;
  353. int32_t logical_max = 0;
  354. uint32_t bitindex = 0;
  355. while (p < end) {
  356. uint8_t tag = *p;
  357. if (tag == 0xFE) { // Long Item (unsupported)
  358. p += p[1] + 3;
  359. continue;
  360. }
  361. uint32_t val;
  362. switch (tag & 0x03) { // Short Item data
  363. case 0: val = 0;
  364. p++;
  365. break;
  366. case 1: val = p[1];
  367. p += 2;
  368. break;
  369. case 2: val = p[1] | (p[2] << 8);
  370. println("val16 = ", val, HEX);
  371. p += 3;
  372. break;
  373. case 3: val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
  374. p += 5;
  375. break;
  376. }
  377. if (p > end) break;
  378. bool reset_local = false;
  379. switch (tag & 0xFC) {
  380. case 0x04: // Usage Page (global)
  381. usage_page = val;
  382. break;
  383. case 0x14: // Logical Minimum (global)
  384. logical_min = signedval(val, tag);
  385. break;
  386. case 0x24: // Logical Maximum (global)
  387. logical_max = signedval(val, tag);
  388. break;
  389. case 0x74: // Report Size (global)
  390. report_size = val;
  391. break;
  392. case 0x94: // Report Count (global)
  393. report_count = val;
  394. break;
  395. case 0x84: // Report ID (global)
  396. report_id = val;
  397. break;
  398. case 0x08: // Usage (local)
  399. if (usage_count < USAGE_LIST_LEN) {
  400. usage[usage_count++] = val;
  401. }
  402. break;
  403. case 0x18: // Usage Minimum (local)
  404. usage[0] = val;
  405. usage_count = 255;
  406. break;
  407. case 0x28: // Usage Maximum (local)
  408. usage[1] = val;
  409. usage_count = 255;
  410. break;
  411. case 0xA0: // Collection
  412. if (collection_level == 0) {
  413. topusage = ((uint32_t)usage_page << 16) | usage[0];
  414. driver = NULL;
  415. if (topusage_index < TOPUSAGE_LIST_LEN) {
  416. driver = topusage_drivers[topusage_index++];
  417. }
  418. }
  419. // discard collection info if not top level, hopefully that's ok?
  420. collection_level++;
  421. reset_local = true;
  422. break;
  423. case 0xC0: // End Collection
  424. if (collection_level > 0) {
  425. collection_level--;
  426. if (collection_level == 0 && driver != NULL) {
  427. driver->hid_input_end();
  428. driver = NULL;
  429. }
  430. }
  431. reset_local = true;
  432. break;
  433. case 0x80: // Input
  434. if (use_report_id && (report_id != (type_and_report_id & 0xFF))) {
  435. // completely ignore and do not advance bitindex
  436. // for descriptors of other report IDs
  437. reset_local = true;
  438. break;
  439. }
  440. if ((val & 1) || (driver == NULL)) {
  441. // skip past constant fields or when no driver is listening
  442. bitindex += report_count * report_size;
  443. } else {
  444. println("begin, usage=", topusage, HEX);
  445. println(" type= ", val, HEX);
  446. println(" min= ", logical_min);
  447. println(" max= ", logical_max);
  448. driver->hid_input_begin(topusage, val, logical_min, logical_max);
  449. println("Input, total bits=", report_count * report_size);
  450. if ((val & 2)) {
  451. // ordinary variable format
  452. uint32_t uindex = 0;
  453. bool uminmax = false;
  454. if (usage_count > USAGE_LIST_LEN || usage_count == 0) {
  455. // usage numbers by min/max, not from list
  456. uindex = usage[0];
  457. uminmax = true;
  458. }
  459. for (uint32_t i=0; i < report_count; i++) {
  460. uint32_t u;
  461. if (uminmax) {
  462. u = uindex;
  463. if (uindex < usage[1]) uindex++;
  464. } else {
  465. u = usage[uindex++];
  466. if (uindex >= USAGE_LIST_LEN-1) {
  467. uindex = USAGE_LIST_LEN-1;
  468. }
  469. }
  470. u |= (uint32_t)usage_page << 16;
  471. print(" usage = ", u, HEX);
  472. uint32_t n = bitfield(data, bitindex, report_size);
  473. if (logical_min >= 0) {
  474. println(" data = ", n);
  475. driver->hid_input_data(u, n);
  476. } else {
  477. int32_t sn = signext(n, report_size);
  478. println(" sdata = ", sn);
  479. driver->hid_input_data(u, sn);
  480. }
  481. bitindex += report_size;
  482. }
  483. } else {
  484. // array format, each item is a usage number
  485. for (uint32_t i=0; i < report_count; i++) {
  486. uint32_t u = bitfield(data, bitindex, report_size);
  487. int n = u;
  488. if (n >= logical_min && n <= logical_max) {
  489. u |= (uint32_t)usage_page << 16;
  490. print(" usage = ", u, HEX);
  491. println(" data = 1");
  492. driver->hid_input_data(u, 1);
  493. }
  494. bitindex += report_size;
  495. }
  496. }
  497. }
  498. reset_local = true;
  499. break;
  500. case 0x90: // Output
  501. // TODO.....
  502. reset_local = true;
  503. break;
  504. case 0xB0: // Feature
  505. // TODO.....
  506. reset_local = true;
  507. break;
  508. case 0xA4: // Push (yikes! Hope nobody really uses this?!)
  509. case 0xB4: // Pop (yikes! Hope nobody really uses this?!)
  510. case 0x34: // Physical Minimum (global)
  511. case 0x44: // Physical Maximum (global)
  512. case 0x54: // Unit Exponent (global)
  513. case 0x64: // Unit (global)
  514. case 0x38: // Designator Index (local)
  515. case 0x48: // Designator Minimum (local)
  516. case 0x58: // Designator Maximum (local)
  517. case 0x78: // String Index (local)
  518. case 0x88: // String Minimum (local)
  519. case 0x98: // String Maximum (local)
  520. case 0xA8: // Delimiter (local)
  521. default:
  522. println("Ruh Roh, unsupported tag, not a good thing Scoob ", tag, HEX);
  523. break;
  524. }
  525. if (reset_local) {
  526. usage_count = 0;
  527. usage[0] = 0;
  528. usage[1] = 0;
  529. }
  530. }
  531. }