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.

593 line
18KB

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