Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

enumeration.cpp 9.0KB

7 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. while (1) {
  86. // Within this large switch/case, "break" means we've done
  87. // some work, but more remains to be done in a different
  88. // state. Generally break is used after parsing received
  89. // data, but what happens next could be different states.
  90. // When completed, return is used. Generally, return happens
  91. // only after a new control transfer is queued, or when
  92. // enumeration is complete and no more communication is needed.
  93. switch (dev->enum_state) {
  94. case 0: // read 8 bytes of device desc, set max packet, and send set address
  95. pipe_set_maxlen(dev->control_pipe, enumbuf[7]);
  96. mk_setup(enumsetup, 0, 5, assign_addr(), 0, 0); // 5=SET_ADDRESS
  97. queue_Control_Transfer(dev, &enumsetup, NULL, NULL);
  98. dev->enum_state = 1;
  99. return;
  100. case 1: // request all 18 bytes of device descriptor
  101. pipe_set_addr(dev->control_pipe, enumsetup.wValue);
  102. mk_setup(enumsetup, 0x80, 6, 0x0100, 0, 18); // 6=GET_DESCRIPTOR
  103. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  104. dev->enum_state = 2;
  105. return;
  106. case 2: // parse 18 device desc bytes
  107. dev->bDeviceClass = enumbuf[4];
  108. dev->bDeviceSubClass = enumbuf[5];
  109. dev->bDeviceProtocol = enumbuf[6];
  110. dev->idVendor = enumbuf[8] | (enumbuf[9] << 8);
  111. dev->idProduct = enumbuf[10] | (enumbuf[11] << 8);
  112. enumbuf[0] = enumbuf[14];
  113. enumbuf[1] = enumbuf[15];
  114. enumbuf[2] = enumbuf[16];
  115. if ((enumbuf[0] | enumbuf[1] | enumbuf[2]) > 0) {
  116. dev->enum_state = 3;
  117. } else {
  118. dev->enum_state = 11;
  119. }
  120. break;
  121. case 3: // request Language ID
  122. len = sizeof(enumbuf) - 4;
  123. mk_setup(enumsetup, 0x80, 6, 0x0300, 0, len); // 6=GET_DESCRIPTOR
  124. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  125. dev->enum_state = 4;
  126. return;
  127. case 4: // parse Language ID
  128. if (enumbuf[4] < 4 || enumbuf[5] != 3) {
  129. dev->enum_state = 11;
  130. } else {
  131. dev->LanguageID = enumbuf[6] | (enumbuf[7] << 8);
  132. if (enumbuf[0]) dev->enum_state = 5;
  133. else if (enumbuf[1]) dev->enum_state = 7;
  134. else if (enumbuf[2]) dev->enum_state = 9;
  135. else dev->enum_state = 11;
  136. }
  137. break;
  138. case 5: // request Manufacturer string
  139. len = sizeof(enumbuf) - 4;
  140. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[0], dev->LanguageID, len);
  141. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  142. dev->enum_state = 6;
  143. return;
  144. case 6: // parse Manufacturer string
  145. // TODO: receive the string...
  146. if (enumbuf[1]) dev->enum_state = 7;
  147. else if (enumbuf[2]) dev->enum_state = 9;
  148. else dev->enum_state = 11;
  149. break;
  150. case 7: // request Product string
  151. len = sizeof(enumbuf) - 4;
  152. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[1], dev->LanguageID, len);
  153. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  154. dev->enum_state = 8;
  155. return;
  156. case 8: // parse Product string
  157. // TODO: receive the string...
  158. if (enumbuf[2]) dev->enum_state = 9;
  159. else dev->enum_state = 11;
  160. break;
  161. case 9: // request Serial Number string
  162. len = sizeof(enumbuf) - 4;
  163. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[2], dev->LanguageID, len);
  164. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  165. dev->enum_state = 10;
  166. return;
  167. case 10: // parse Serial Number string
  168. // TODO: receive the string...
  169. dev->enum_state = 11;
  170. break;
  171. case 11: // request first 9 bytes of config desc
  172. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, 9); // 6=GET_DESCRIPTOR
  173. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  174. dev->enum_state = 12;
  175. return;
  176. case 12: // read 9 bytes, request all of config desc
  177. len = enumbuf[2] | (enumbuf[3] << 8);
  178. Serial.print("Config data length = ");
  179. Serial.println(len);
  180. if (len > sizeof(enumbuf)) {
  181. // TODO: how to handle device with too much config data
  182. }
  183. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, len); // 6=GET_DESCRIPTOR
  184. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  185. dev->enum_state = 13;
  186. return;
  187. case 13: // read all config desc, send set config
  188. Serial.print("bNumInterfaces = ");
  189. Serial.println(enumbuf[4]);
  190. Serial.print("bConfigurationValue = ");
  191. Serial.println(enumbuf[5]);
  192. dev->bmAttributes = enumbuf[7];
  193. dev->bMaxPower = enumbuf[8];
  194. // TODO: actually do something with interface descriptor?
  195. mk_setup(enumsetup, 0, 9, enumbuf[5], 0, 0); // 9=SET_CONFIGURATION
  196. queue_Control_Transfer(dev, &enumsetup, NULL, NULL);
  197. dev->enum_state = 14;
  198. return;
  199. case 14: // device is now configured
  200. claim_drivers(dev);
  201. dev->enum_state = 15;
  202. // TODO: unlock exclusive access to enumeration process
  203. // if any detected devices are waiting, start the first
  204. return;
  205. case 15: // control transfers for other stuff?
  206. // TODO: handle other standard control: set/clear feature, etc
  207. for (USBDriver *d = dev->drivers; d != NULL; d = d->next) {
  208. if (d->control(transfer)) {
  209. // this driver processed the control transfer reply
  210. return;
  211. }
  212. }
  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. //}