Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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