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.

332 lines
9.4KB

  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. // Printing of specific data structures. When this is enabled,
  26. // a tremendous amount of debug printing occurs. It's done all
  27. // from interrupt context, so this should never normally be
  28. // enabled for regular programs that print from the Arduino sketch.
  29. #ifdef USBHOST_PRINT_DEBUG
  30. void USBHost::print_(const Transfer_t *transfer)
  31. {
  32. if (!((uint32_t)transfer & 0xFFFFFFE0)) return;
  33. Serial.print("Transfer @ ");
  34. Serial.println(((uint32_t)transfer & 0xFFFFFFE0), HEX);
  35. Serial.print(" next: ");
  36. Serial.println(transfer->qtd.next, HEX);
  37. Serial.print(" anext: ");
  38. Serial.println(transfer->qtd.alt_next, HEX);
  39. Serial.print(" token: ");
  40. Serial.println(transfer->qtd.token, HEX);
  41. Serial.print(" bufs: ");
  42. for (int i=0; i < 5; i++) {
  43. Serial.print(transfer->qtd.buffer[i], HEX);
  44. if (i < 4) Serial.print(',');
  45. }
  46. Serial.println();
  47. }
  48. void USBHost::print_(const Transfer_t *first, const Transfer_t *last)
  49. {
  50. Serial.print("Transfer Followup List ");
  51. Serial.print((uint32_t)first, HEX);
  52. Serial.print(" to ");
  53. Serial.println((uint32_t)last, HEX);
  54. Serial.println(" forward:");
  55. while (first) {
  56. Serial.print(" ");
  57. Serial.print((uint32_t)first, HEX);
  58. print_token(first->qtd.token);
  59. first = first->next_followup;
  60. }
  61. Serial.println(" backward:");
  62. while (last) {
  63. Serial.print(" ");
  64. Serial.print((uint32_t)last, HEX);
  65. print_token(last->qtd.token);
  66. last = last->prev_followup;
  67. }
  68. }
  69. void USBHost::print_token(uint32_t token)
  70. {
  71. switch ((token >> 8) & 3) {
  72. case 0:
  73. Serial.print(" OUT ");
  74. Serial.println((token >> 16) & 0x7FFF);
  75. break;
  76. case 1:
  77. Serial.print(" IN ");
  78. Serial.println((token >> 16) & 0x7FFF);
  79. break;
  80. case 2:
  81. Serial.println(" SETUP");
  82. break;
  83. default:
  84. Serial.println(" unknown");
  85. }
  86. }
  87. void USBHost::print_(const Pipe_t *pipe)
  88. {
  89. if (!((uint32_t)pipe & 0xFFFFFFE0)) return;
  90. Serial.print("Pipe ");
  91. if (pipe->type == 0) Serial.print("control");
  92. else if (pipe->type == 1) Serial.print("isochronous");
  93. else if (pipe->type == 2) Serial.print("bulk");
  94. else if (pipe->type == 3) Serial.print("interrupt");
  95. Serial.print(pipe->direction ? " IN" : " OUT");
  96. Serial.print(" @ ");
  97. Serial.println((uint32_t)pipe, HEX);
  98. Serial.print(" horiz link: ");
  99. Serial.println(pipe->qh.horizontal_link, HEX);
  100. Serial.print(" capabilities: ");
  101. Serial.print(pipe->qh.capabilities[0], HEX);
  102. Serial.print(',');
  103. Serial.println(pipe->qh.capabilities[1], HEX);
  104. Serial.println(" overlay:");
  105. Serial.print(" cur: ");
  106. Serial.println(pipe->qh.current, HEX);
  107. Serial.print(" next: ");
  108. Serial.println(pipe->qh.next, HEX);
  109. Serial.print(" anext: ");
  110. Serial.println(pipe->qh.alt_next, HEX);
  111. Serial.print(" token: ");
  112. Serial.println(pipe->qh.token, HEX);
  113. Serial.print(" bufs: ");
  114. for (int i=0; i < 5; i++) {
  115. Serial.print(pipe->qh.buffer[i], HEX);
  116. if (i < 4) Serial.print(',');
  117. }
  118. Serial.println();
  119. const Transfer_t *t = (Transfer_t *)pipe->qh.next;
  120. while (((uint32_t)t & 0xFFFFFFE0)) {
  121. print_(t);
  122. t = (Transfer_t *)t->qtd.next;
  123. }
  124. //Serial.print();
  125. }
  126. void USBHost::print_driverlist(const char *name, const USBDriver *driver)
  127. {
  128. Serial.print("USBDriver (");
  129. Serial.print(name);
  130. Serial.print(") list: ");
  131. if (driver == NULL) {
  132. Serial.println("(empty");
  133. return;
  134. }
  135. uint32_t count=0;
  136. for (const USBDriver *p = driver; p; p = p->next) {
  137. Serial.print((uint32_t)p, HEX);
  138. if (p->next) Serial.print(" -> ");
  139. if (++count > 30) {
  140. Serial.println("abort:list too long");
  141. return;
  142. }
  143. }
  144. Serial.println();
  145. }
  146. void USBHost::print_qh_list(const Pipe_t *list)
  147. {
  148. if (!list) {
  149. Serial.println("(empty)");
  150. return;
  151. }
  152. const Pipe_t *node = list;
  153. while (1) {
  154. Serial.print((uint32_t)node, HEX);
  155. node = (const Pipe_t *)(node->qh.horizontal_link & 0xFFFFFFE0);
  156. if (!node) break;
  157. if (node == list) {
  158. Serial.print(" (loops)");
  159. break;
  160. }
  161. Serial.print(" -> ");
  162. }
  163. Serial.println();
  164. }
  165. static void print_class_subclass_protocol(uint8_t c, uint8_t s, uint8_t p)
  166. {
  167. Serial.print(c);
  168. if (c == 3) Serial.print("(HID)");
  169. if (c == 8) Serial.print("(Mass Storage)");
  170. if (c == 9) Serial.print("(Hub)");
  171. Serial.print(" / ");
  172. Serial.print(s);
  173. if (c == 3 && s == 1) Serial.print("(Boot)");
  174. if (c == 8 && s == 6) Serial.print("(SCSI)");
  175. Serial.print(" / ");
  176. Serial.print(p);
  177. if (c == 3 && s == 1 && p == 1) Serial.print("(Keyboard)");
  178. if (c == 3 && s == 1 && p == 2) Serial.print("(Mouse)");
  179. if (c == 8 && s == 6 && p == 0x50) Serial.print("(Bulk Only)");
  180. if (c == 8 && s == 6 && p == 0x62) Serial.print("(UAS)");
  181. if (c == 9 && s == 0 && p == 1) Serial.print("(Single-TT)");
  182. if (c == 9 && s == 0 && p == 2) Serial.print("(Multi-TT)");
  183. Serial.println();
  184. }
  185. void USBHost::print_device_descriptor(const uint8_t *p)
  186. {
  187. Serial.println("Device Descriptor:");
  188. Serial.print(" ");
  189. print_hexbytes(p, p[0]);
  190. if (p[0] != 18) {
  191. Serial.println("error: device must be 18 bytes");
  192. return;
  193. }
  194. if (p[1] != 1) {
  195. Serial.println("error: device must type 1");
  196. return;
  197. }
  198. Serial.printf(" VendorID = %04X, ProductID = %04X, Version = %04X",
  199. p[8] | (p[9] << 8), p[10] | (p[11] << 8), p[12] | (p[13] << 8));
  200. Serial.println();
  201. Serial.print(" Class/Subclass/Protocol = ");
  202. print_class_subclass_protocol(p[4], p[5], p[6]);
  203. Serial.print(" Number of Configurations = ");
  204. Serial.println(p[17]);
  205. }
  206. void USBHost::print_config_descriptor(const uint8_t *p, uint32_t maxlen)
  207. {
  208. // Descriptor Types: (USB 2.0, page 251)
  209. Serial.println("Configuration Descriptor:");
  210. Serial.print(" ");
  211. print_hexbytes(p, p[0]);
  212. if (p[0] != 9) {
  213. Serial.println("error: config must be 9 bytes");
  214. return;
  215. }
  216. if (p[1] != 2) {
  217. Serial.println("error: config must type 2");
  218. return;
  219. }
  220. Serial.print(" NumInterfaces = ");
  221. Serial.println(p[4]);
  222. Serial.print(" ConfigurationValue = ");
  223. Serial.println(p[5]);
  224. uint32_t len = p[2] | (p[3] << 8);
  225. if (len > maxlen) len = maxlen;
  226. len -= p[0];
  227. p += 9;
  228. while (len > 0) {
  229. if (p[0] > len) {
  230. Serial.print(" ");
  231. print_hexbytes(p, len);
  232. Serial.println(" error: length beyond total data size");
  233. break;
  234. }
  235. Serial.print(" ");
  236. print_hexbytes(p, p[0]);
  237. if (p[0] == 9 && p[1] == 4) { // Interface Descriptor
  238. Serial.print(" Interface = ");
  239. Serial.println(p[2]);
  240. Serial.print(" Number of endpoints = ");
  241. Serial.println(p[4]);
  242. Serial.print(" Class/Subclass/Protocol = ");
  243. print_class_subclass_protocol(p[5], p[6], p[7]);
  244. } else if (p[0] >= 7 && p[0] <= 9 && p[1] == 5) { // Endpoint Descriptor
  245. Serial.print(" Endpoint = ");
  246. Serial.print(p[2] & 15);
  247. Serial.println((p[2] & 128) ? " IN" : " OUT");
  248. Serial.print(" Type = ");
  249. switch (p[3] & 3) {
  250. case 0: Serial.println("Control"); break;
  251. case 1: Serial.println("Isochronous"); break;
  252. case 2: Serial.println("Bulk"); break;
  253. case 3: Serial.println("Interrupt"); break;
  254. }
  255. Serial.print(" Max Size = ");
  256. Serial.println(p[4] | (p[5] << 8));
  257. Serial.print(" Polling Interval = ");
  258. Serial.println(p[6]);
  259. } else if (p[0] == 8 && p[1] == 11) { // IAD
  260. Serial.print(" Interface Association = ");
  261. Serial.print(p[2]);
  262. Serial.print(" through ");
  263. Serial.println(p[2] + p[3] - 1);
  264. Serial.print(" Class / Subclass / Protocol = ");
  265. print_class_subclass_protocol(p[4], p[5], p[7]);
  266. } else if (p[0] >= 9 && p[1] == 0x21) { // HID
  267. Serial.print(" HID, ");
  268. Serial.print(p[5]);
  269. Serial.print(" report descriptor");
  270. if (p[5] != 1) Serial.print('s');
  271. Serial.println();
  272. }
  273. len -= p[0];
  274. p += p[0];
  275. }
  276. }
  277. void USBHost::print_string_descriptor(const char *name, const uint8_t *p)
  278. {
  279. uint32_t len = p[0];
  280. if (len < 4) return;
  281. Serial.print(name);
  282. len -= 2;
  283. p += 2;
  284. while (len >= 2) {
  285. uint32_t c = p[0] | (p[1] << 8);
  286. if (c < 0x80) {
  287. Serial.write(c);
  288. } else if (c < 0x800) {
  289. Serial.write((c >> 6) | 0xC0);
  290. Serial.write((c & 0x3F) | 0x80);
  291. } else {
  292. Serial.write((c >> 12) | 0xE0);
  293. Serial.write(((c >> 6) & 0x3F) | 0x80);
  294. Serial.write((c & 0x3F) | 0x80);
  295. }
  296. len -= 2;
  297. p += 2;
  298. }
  299. Serial.println();
  300. //print_hexbytes(p, p[0]);
  301. }
  302. void USBHost::print_hexbytes(const void *ptr, uint32_t len)
  303. {
  304. if (ptr == NULL || len == 0) return;
  305. const uint8_t *p = (const uint8_t *)ptr;
  306. do {
  307. if (*p < 16) Serial.print('0');
  308. Serial.print(*p++, HEX);
  309. Serial.print(' ');
  310. } while (--len);
  311. Serial.println();
  312. }
  313. #endif