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.

enumeration.cpp 9.0KB

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