Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

282 lines
8.9KB

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