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.

329 lines
10KB

  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.h"
  25. static USBDriver *available_drivers = NULL;
  26. static uint8_t enumbuf[256] __attribute__ ((aligned(16)));
  27. static setup_t enumsetup __attribute__ ((aligned(16)));
  28. static uint16_t enumlen;
  29. static uint32_t assign_addr(void);
  30. static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen);
  31. static void pipe_set_addr(Pipe_t *pipe, uint32_t addr);
  32. void USBHost::driver_ready_for_device(USBDriver *driver)
  33. {
  34. driver->device = NULL;
  35. driver->next = NULL;
  36. if (available_drivers == NULL) {
  37. available_drivers = driver;
  38. } else {
  39. // append to end of list
  40. USBDriver *last = available_drivers;
  41. while (last->next) last = last->next;
  42. last->next = driver;
  43. }
  44. }
  45. // Create a new device and begin the enumeration process
  46. //
  47. Device_t * USBHost::new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)
  48. {
  49. Device_t *dev;
  50. Serial.print("new_Device: ");
  51. switch (speed) {
  52. case 0: Serial.print("12"); break;
  53. case 1: Serial.print("1.5"); break;
  54. case 2: Serial.print("480"); break;
  55. default: Serial.print("??");
  56. }
  57. Serial.println(" Mbit/sec");
  58. dev = allocate_Device();
  59. if (!dev) return NULL;
  60. memset(dev, 0, sizeof(Device_t));
  61. dev->speed = speed;
  62. dev->address = 0;
  63. dev->hub_address = hub_addr;
  64. dev->hub_port = hub_port;
  65. dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8);
  66. if (!dev->control_pipe) {
  67. free_Device(dev);
  68. return NULL;
  69. }
  70. dev->control_pipe->callback_function = &enumeration;
  71. dev->control_pipe->direction = 1; // 1=IN
  72. // TODO: exclusive access to enumeration process
  73. // any new devices detected while enumerating would
  74. // go onto a waiting list
  75. mk_setup(enumsetup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR
  76. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  77. return dev;
  78. }
  79. void USBHost::enumeration(const Transfer_t *transfer)
  80. {
  81. Device_t *dev;
  82. uint32_t len;
  83. // If a driver created this control transfer, allow it to process the result
  84. if (transfer->driver) {
  85. transfer->driver->control(transfer);
  86. return;
  87. }
  88. Serial.println("enumeration:");
  89. //print_hexbytes(transfer->buffer, transfer->length);
  90. //print(transfer);
  91. dev = transfer->pipe->device;
  92. while (1) {
  93. // Within this large switch/case, "break" means we've done
  94. // some work, but more remains to be done in a different
  95. // state. Generally break is used after parsing received
  96. // data, but what happens next could be different states.
  97. // When completed, return is used. Generally, return happens
  98. // only after a new control transfer is queued, or when
  99. // enumeration is complete and no more communication is needed.
  100. switch (dev->enum_state) {
  101. case 0: // read 8 bytes of device desc, set max packet, and send set address
  102. pipe_set_maxlen(dev->control_pipe, enumbuf[7]);
  103. mk_setup(enumsetup, 0, 5, assign_addr(), 0, 0); // 5=SET_ADDRESS
  104. queue_Control_Transfer(dev, &enumsetup, NULL, NULL);
  105. dev->enum_state = 1;
  106. return;
  107. case 1: // request all 18 bytes of device descriptor
  108. dev->address = enumsetup.wValue;
  109. pipe_set_addr(dev->control_pipe, enumsetup.wValue);
  110. mk_setup(enumsetup, 0x80, 6, 0x0100, 0, 18); // 6=GET_DESCRIPTOR
  111. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  112. dev->enum_state = 2;
  113. return;
  114. case 2: // parse 18 device desc bytes
  115. dev->bDeviceClass = enumbuf[4];
  116. dev->bDeviceSubClass = enumbuf[5];
  117. dev->bDeviceProtocol = enumbuf[6];
  118. dev->idVendor = enumbuf[8] | (enumbuf[9] << 8);
  119. dev->idProduct = enumbuf[10] | (enumbuf[11] << 8);
  120. enumbuf[0] = enumbuf[14];
  121. enumbuf[1] = enumbuf[15];
  122. enumbuf[2] = enumbuf[16];
  123. if ((enumbuf[0] | enumbuf[1] | enumbuf[2]) > 0) {
  124. dev->enum_state = 3;
  125. } else {
  126. dev->enum_state = 11;
  127. }
  128. break;
  129. case 3: // request Language ID
  130. len = sizeof(enumbuf) - 4;
  131. mk_setup(enumsetup, 0x80, 6, 0x0300, 0, len); // 6=GET_DESCRIPTOR
  132. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  133. dev->enum_state = 4;
  134. return;
  135. case 4: // parse Language ID
  136. if (enumbuf[4] < 4 || enumbuf[5] != 3) {
  137. dev->enum_state = 11;
  138. } else {
  139. dev->LanguageID = enumbuf[6] | (enumbuf[7] << 8);
  140. if (enumbuf[0]) dev->enum_state = 5;
  141. else if (enumbuf[1]) dev->enum_state = 7;
  142. else if (enumbuf[2]) dev->enum_state = 9;
  143. else dev->enum_state = 11;
  144. }
  145. break;
  146. case 5: // request Manufacturer string
  147. len = sizeof(enumbuf) - 4;
  148. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[0], dev->LanguageID, len);
  149. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  150. dev->enum_state = 6;
  151. return;
  152. case 6: // parse Manufacturer string
  153. // TODO: receive the string...
  154. if (enumbuf[1]) dev->enum_state = 7;
  155. else if (enumbuf[2]) dev->enum_state = 9;
  156. else dev->enum_state = 11;
  157. break;
  158. case 7: // request Product string
  159. len = sizeof(enumbuf) - 4;
  160. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[1], dev->LanguageID, len);
  161. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  162. dev->enum_state = 8;
  163. return;
  164. case 8: // parse Product string
  165. // TODO: receive the string...
  166. if (enumbuf[2]) dev->enum_state = 9;
  167. else dev->enum_state = 11;
  168. break;
  169. case 9: // request Serial Number string
  170. len = sizeof(enumbuf) - 4;
  171. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[2], dev->LanguageID, len);
  172. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  173. dev->enum_state = 10;
  174. return;
  175. case 10: // parse Serial Number string
  176. // TODO: receive the string...
  177. dev->enum_state = 11;
  178. break;
  179. case 11: // request first 9 bytes of config desc
  180. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, 9); // 6=GET_DESCRIPTOR
  181. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  182. dev->enum_state = 12;
  183. return;
  184. case 12: // read 9 bytes, request all of config desc
  185. enumlen = enumbuf[2] | (enumbuf[3] << 8);
  186. Serial.print("Config data length = ");
  187. Serial.println(enumlen);
  188. if (enumlen > sizeof(enumbuf)) {
  189. // TODO: how to handle device with too much config data
  190. }
  191. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, enumlen); // 6=GET_DESCRIPTOR
  192. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  193. dev->enum_state = 13;
  194. return;
  195. case 13: // read all config desc, send set config
  196. Serial.print("bNumInterfaces = ");
  197. Serial.println(enumbuf[4]);
  198. Serial.print("bConfigurationValue = ");
  199. Serial.println(enumbuf[5]);
  200. dev->bmAttributes = enumbuf[7];
  201. dev->bMaxPower = enumbuf[8];
  202. // TODO: actually do something with interface descriptor?
  203. mk_setup(enumsetup, 0, 9, enumbuf[5], 0, 0); // 9=SET_CONFIGURATION
  204. queue_Control_Transfer(dev, &enumsetup, NULL, NULL);
  205. dev->enum_state = 14;
  206. return;
  207. case 14: // device is now configured
  208. claim_drivers(dev);
  209. dev->enum_state = 15;
  210. // TODO: unlock exclusive access to enumeration process
  211. // if any detected devices are waiting, start the first
  212. return;
  213. case 15: // control transfers for other stuff?
  214. // TODO: handle other standard control: set/clear feature, etc
  215. default:
  216. return;
  217. }
  218. }
  219. }
  220. void USBHost::claim_drivers(Device_t *dev)
  221. {
  222. USBDriver *driver, *prev=NULL;
  223. // first check if any driver wishes to claim the entire device
  224. for (driver=available_drivers; driver != NULL; driver = driver->next) {
  225. if (driver->claim(dev, 0, enumbuf + 9, enumlen - 9)) {
  226. if (prev) {
  227. prev->next = driver->next;
  228. } else {
  229. available_drivers = driver->next;
  230. }
  231. driver->device = dev;
  232. driver->next = NULL;
  233. dev->drivers = driver;
  234. return;
  235. }
  236. prev = driver;
  237. }
  238. // parse interfaces from config descriptor
  239. const uint8_t *p = enumbuf + 9;
  240. const uint8_t *end = enumbuf + enumlen;
  241. while (p < end) {
  242. uint8_t desclen = *p;
  243. uint8_t desctype = *(p+1);
  244. Serial.print("Descriptor ");
  245. Serial.print(desctype);
  246. Serial.print(" = ");
  247. if (desctype == 4) Serial.println("INTERFACE");
  248. else if (desctype == 5) Serial.println("ENDPOINT");
  249. else if (desctype == 6) Serial.println("DEV_QUALIFIER");
  250. else if (desctype == 7) Serial.println("OTHER_SPEED");
  251. else if (desctype == 11) Serial.println("IAD");
  252. else if (desctype == 33) Serial.println("HID");
  253. else Serial.println(" ???");
  254. if (desctype == 11 && desclen == 8) {
  255. // TODO: parse IAD, ask drivers for claim
  256. // TODO: how to skip over all interfaces IAD represented
  257. }
  258. if (desctype == 4 && desclen == 9) {
  259. // found an interface, ask available drivers if they want it
  260. prev = NULL;
  261. for (driver=available_drivers; driver != NULL; driver = driver->next) {
  262. if (driver->claim(dev, 1, p, end - p)) {
  263. // this driver claims iface
  264. // remove it from available_drivers list
  265. if (prev) {
  266. prev->next = driver->next;
  267. } else {
  268. available_drivers = driver->next;
  269. }
  270. // add to list of drivers using this device
  271. if (dev->drivers) {
  272. dev->drivers->next = driver;
  273. }
  274. dev->drivers = driver;
  275. driver->next = NULL;
  276. driver->device = dev;
  277. // not done, may be more interface for more drivers
  278. }
  279. prev = driver;
  280. }
  281. }
  282. p += desclen;
  283. }
  284. }
  285. static uint32_t assign_addr(void)
  286. {
  287. return 29; // TODO: when multiple devices, assign a unique address
  288. }
  289. static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
  290. {
  291. Serial.print("pipe_set_maxlen ");
  292. Serial.println(maxlen);
  293. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
  294. }
  295. static void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
  296. {
  297. Serial.print("pipe_set_addr ");
  298. Serial.println(addr);
  299. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
  300. }
  301. //static uint32_t pipe_get_addr(Pipe_t *pipe)
  302. //{
  303. // return pipe->qh.capabilities[0] & 0xFFFFFF80;
  304. //}