Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

hid.cpp 18KB

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