Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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