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.

пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 uint8_t enumbuf[256] __attribute__ ((aligned(16)));
  26. static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  27. uint32_t wValue, uint32_t wIndex, uint32_t wLength);
  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. // Create a new device and begin the enumeration process
  32. //
  33. Device_t * USBHost::new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)
  34. {
  35. Device_t *dev;
  36. Serial.print("new_Device: ");
  37. switch (speed) {
  38. case 0: Serial.print("12"); break;
  39. case 1: Serial.print("1.5"); break;
  40. case 2: Serial.print("480"); break;
  41. default: Serial.print("??");
  42. }
  43. Serial.println(" Mbit/sec");
  44. dev = allocate_Device();
  45. if (!dev) return NULL;
  46. memset(dev, 0, sizeof(Device_t));
  47. dev->speed = speed;
  48. dev->address = 0;
  49. dev->hub_address = hub_addr;
  50. dev->hub_port = hub_port;
  51. dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8);
  52. if (!dev->control_pipe) {
  53. free_Device(dev);
  54. return NULL;
  55. }
  56. dev->control_pipe->callback_function = &enumeration;
  57. dev->control_pipe->direction = 1; // 1=IN
  58. mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR
  59. new_Transfer(dev->control_pipe, enumbuf, 8);
  60. return dev;
  61. }
  62. void USBHost::enumeration(const Transfer_t *transfer)
  63. {
  64. uint32_t len;
  65. Serial.print(" CALLBACK: ");
  66. print_hexbytes(transfer->buffer, transfer->length);
  67. //print(transfer);
  68. Device_t *dev = transfer->pipe->device;
  69. while (1) {
  70. // Within this large switch/case, "break" means we've done
  71. // some work, but more remains to be done in a different
  72. // state. Generally break is used after parsing received
  73. // data, but what happens next could be different states.
  74. // When completed, return is used. Generally, return happens
  75. // only after a new control transfer is queued, or when
  76. // enumeration is complete and no more communication is needed.
  77. switch (dev->enum_state) {
  78. case 0: // read 8 bytes of device desc, set max packet, and send set address
  79. pipe_set_maxlen(dev->control_pipe, enumbuf[7]);
  80. mk_setup(dev->setup, 0, 5, assign_addr(), 0, 0); // 5=SET_ADDRESS
  81. new_Transfer(dev->control_pipe, NULL, 0);
  82. dev->enum_state = 1;
  83. return;
  84. case 1: // request all 18 bytes of device descriptor
  85. pipe_set_addr(dev->control_pipe, dev->setup.wValue);
  86. mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 18); // 6=GET_DESCRIPTOR
  87. new_Transfer(dev->control_pipe, enumbuf, 18);
  88. dev->enum_state = 2;
  89. return;
  90. case 2: // parse 18 device desc bytes
  91. dev->bDeviceClass = enumbuf[4];
  92. dev->bDeviceSubClass = enumbuf[5];
  93. dev->bDeviceProtocol = enumbuf[6];
  94. dev->idVendor = enumbuf[8] | (enumbuf[9] << 8);
  95. dev->idProduct = enumbuf[10] | (enumbuf[11] << 8);
  96. enumbuf[0] = enumbuf[14];
  97. enumbuf[1] = enumbuf[15];
  98. enumbuf[2] = enumbuf[16];
  99. if ((enumbuf[0] | enumbuf[1] | enumbuf[2]) > 0) {
  100. dev->enum_state = 3;
  101. } else {
  102. dev->enum_state = 11;
  103. }
  104. break;
  105. case 3: // request Language ID
  106. len = sizeof(enumbuf) - 4;
  107. mk_setup(dev->setup, 0x80, 6, 0x0300, 0, len); // 6=GET_DESCRIPTOR
  108. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  109. dev->enum_state = 4;
  110. return;
  111. case 4: // parse Language ID
  112. if (enumbuf[4] < 4 || enumbuf[5] != 3) {
  113. dev->enum_state = 11;
  114. } else {
  115. dev->LanguageID = enumbuf[6] | (enumbuf[7] << 8);
  116. if (enumbuf[0]) dev->enum_state = 5;
  117. else if (enumbuf[1]) dev->enum_state = 7;
  118. else if (enumbuf[2]) dev->enum_state = 9;
  119. else dev->enum_state = 11;
  120. }
  121. break;
  122. case 5: // request Manufacturer string
  123. len = sizeof(enumbuf) - 4;
  124. mk_setup(dev->setup, 0x80, 6, 0x0300 | enumbuf[0], dev->LanguageID, len);
  125. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  126. dev->enum_state = 6;
  127. return;
  128. case 6: // parse Manufacturer string
  129. // TODO: receive the string...
  130. if (enumbuf[1]) dev->enum_state = 7;
  131. else if (enumbuf[2]) dev->enum_state = 9;
  132. else dev->enum_state = 11;
  133. break;
  134. case 7: // request Product string
  135. len = sizeof(enumbuf) - 4;
  136. mk_setup(dev->setup, 0x80, 6, 0x0300 | enumbuf[1], dev->LanguageID, len);
  137. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  138. dev->enum_state = 8;
  139. return;
  140. case 8: // parse Product string
  141. // TODO: receive the string...
  142. if (enumbuf[2]) dev->enum_state = 9;
  143. else dev->enum_state = 11;
  144. break;
  145. case 9: // request Serial Number string
  146. len = sizeof(enumbuf) - 4;
  147. mk_setup(dev->setup, 0x80, 6, 0x0300 | enumbuf[2], dev->LanguageID, len);
  148. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  149. dev->enum_state = 10;
  150. return;
  151. case 10: // parse Serial Number string
  152. // TODO: receive the string...
  153. dev->enum_state = 11;
  154. break;
  155. case 11: // request first 9 bytes of config desc
  156. mk_setup(dev->setup, 0x80, 6, 0x0200, 0, 9); // 6=GET_DESCRIPTOR
  157. new_Transfer(dev->control_pipe, enumbuf, 9);
  158. dev->enum_state = 12;
  159. return;
  160. case 12: // read 9 bytes, request all of config desc
  161. len = enumbuf[2] | (enumbuf[3] << 8);
  162. Serial.print("Config data length = ");
  163. Serial.println(len);
  164. if (len > sizeof(enumbuf)) {
  165. // TODO: how to handle device with too much config data
  166. }
  167. mk_setup(dev->setup, 0x80, 6, 0x0200, 0, len); // 6=GET_DESCRIPTOR
  168. new_Transfer(dev->control_pipe, enumbuf, len);
  169. dev->enum_state = 13;
  170. return;
  171. case 13: // read all config desc, send set config
  172. Serial.print("bNumInterfaces = ");
  173. Serial.println(enumbuf[4]);
  174. Serial.print("bConfigurationValue = ");
  175. Serial.println(enumbuf[5]);
  176. // TODO: actually do something with interface descriptor?
  177. mk_setup(dev->setup, 0, 9, enumbuf[5], 0, 0); // 9=SET_CONFIGURATION
  178. new_Transfer(dev->control_pipe, NULL, 0);
  179. dev->enum_state = 14;
  180. return;
  181. case 14: // device is now configured
  182. // TODO: initialize drivers??
  183. dev->enum_state = 15;
  184. return;
  185. case 15: // control transfers for other stuff??
  186. default:
  187. return;
  188. }
  189. }
  190. }
  191. static uint32_t assign_addr(void)
  192. {
  193. return 29; // TODO: when multiple devices, assign a unique address
  194. }
  195. static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  196. uint32_t wValue, uint32_t wIndex, uint32_t wLength)
  197. {
  198. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  199. s.word2 = wIndex | (wLength << 16);
  200. }
  201. static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
  202. {
  203. Serial.print("pipe_set_maxlen ");
  204. Serial.println(maxlen);
  205. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
  206. }
  207. static void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
  208. {
  209. Serial.print("pipe_set_addr ");
  210. Serial.println(addr);
  211. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
  212. }
  213. //static uint32_t pipe_get_addr(Pipe_t *pipe)
  214. //{
  215. // return pipe->qh.capabilities[0] & 0xFFFFFF80;
  216. //}