Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

281 rinda
8.8KB

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