Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

enumeration.cpp 9.0KB

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