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

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