Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

390 lines
12KB

  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 uint16_t enumlen;
  29. static Device_t *devlist=NULL;
  30. static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen);
  31. static void pipe_set_addr(Pipe_t *pipe, uint32_t addr);
  32. void USBHost::driver_ready_for_device(USBDriver *driver)
  33. {
  34. driver->device = NULL;
  35. driver->next = NULL;
  36. if (available_drivers == NULL) {
  37. available_drivers = driver;
  38. } else {
  39. // append to end of list
  40. USBDriver *last = available_drivers;
  41. while (last->next) last = last->next;
  42. last->next = driver;
  43. }
  44. }
  45. // Create a new device and begin the enumeration process
  46. //
  47. Device_t * USBHost::new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)
  48. {
  49. Device_t *dev;
  50. print("new_Device: ");
  51. switch (speed) {
  52. case 0: print("12"); break;
  53. case 1: print("1.5"); break;
  54. case 2: print("480"); break;
  55. default: print("??");
  56. }
  57. println(" Mbit/sec");
  58. dev = allocate_Device();
  59. if (!dev) return NULL;
  60. memset(dev, 0, sizeof(Device_t));
  61. dev->speed = speed;
  62. dev->address = 0;
  63. dev->hub_address = hub_addr;
  64. dev->hub_port = hub_port;
  65. dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8);
  66. if (!dev->control_pipe) {
  67. free_Device(dev);
  68. return NULL;
  69. }
  70. dev->control_pipe->callback_function = &enumeration;
  71. dev->control_pipe->direction = 1; // 1=IN
  72. // TODO: exclusive access to enumeration process
  73. // any new devices detected while enumerating would
  74. // go onto a waiting list
  75. mk_setup(enumsetup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR
  76. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  77. if (devlist == NULL) {
  78. devlist = dev;
  79. } else {
  80. Device_t *p;
  81. for (p = devlist; p->next; p = p->next) ; // walk devlist
  82. p->next = dev;
  83. }
  84. return dev;
  85. }
  86. void USBHost::enumeration(const Transfer_t *transfer)
  87. {
  88. Device_t *dev;
  89. uint32_t len;
  90. // If a driver created this control transfer, allow it to process the result
  91. if (transfer->driver) {
  92. transfer->driver->control(transfer);
  93. return;
  94. }
  95. println("enumeration:");
  96. //print_hexbytes(transfer->buffer, transfer->length);
  97. //print(transfer);
  98. dev = transfer->pipe->device;
  99. while (1) {
  100. // Within this large switch/case, "break" means we've done
  101. // some work, but more remains to be done in a different
  102. // state. Generally break is used after parsing received
  103. // data, but what happens next could be different states.
  104. // When completed, return is used. Generally, return happens
  105. // only after a new control transfer is queued, or when
  106. // enumeration is complete and no more communication is needed.
  107. switch (dev->enum_state) {
  108. case 0: // read 8 bytes of device desc, set max packet, and send set address
  109. pipe_set_maxlen(dev->control_pipe, enumbuf[7]);
  110. mk_setup(enumsetup, 0, 5, assign_address(), 0, 0); // 5=SET_ADDRESS
  111. queue_Control_Transfer(dev, &enumsetup, NULL, NULL);
  112. dev->enum_state = 1;
  113. return;
  114. case 1: // request all 18 bytes of device descriptor
  115. dev->address = enumsetup.wValue;
  116. pipe_set_addr(dev->control_pipe, enumsetup.wValue);
  117. mk_setup(enumsetup, 0x80, 6, 0x0100, 0, 18); // 6=GET_DESCRIPTOR
  118. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  119. dev->enum_state = 2;
  120. return;
  121. case 2: // parse 18 device desc bytes
  122. dev->bDeviceClass = enumbuf[4];
  123. dev->bDeviceSubClass = enumbuf[5];
  124. dev->bDeviceProtocol = enumbuf[6];
  125. dev->idVendor = enumbuf[8] | (enumbuf[9] << 8);
  126. dev->idProduct = enumbuf[10] | (enumbuf[11] << 8);
  127. enumbuf[0] = enumbuf[14];
  128. enumbuf[1] = enumbuf[15];
  129. enumbuf[2] = enumbuf[16];
  130. if ((enumbuf[0] | enumbuf[1] | enumbuf[2]) > 0) {
  131. dev->enum_state = 3;
  132. } else {
  133. dev->enum_state = 11;
  134. }
  135. break;
  136. case 3: // request Language ID
  137. len = sizeof(enumbuf) - 4;
  138. mk_setup(enumsetup, 0x80, 6, 0x0300, 0, len); // 6=GET_DESCRIPTOR
  139. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  140. dev->enum_state = 4;
  141. return;
  142. case 4: // parse Language ID
  143. if (enumbuf[4] < 4 || enumbuf[5] != 3) {
  144. dev->enum_state = 11;
  145. } else {
  146. dev->LanguageID = enumbuf[6] | (enumbuf[7] << 8);
  147. if (enumbuf[0]) dev->enum_state = 5;
  148. else if (enumbuf[1]) dev->enum_state = 7;
  149. else if (enumbuf[2]) dev->enum_state = 9;
  150. else dev->enum_state = 11;
  151. }
  152. break;
  153. case 5: // request Manufacturer string
  154. len = sizeof(enumbuf) - 4;
  155. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[0], dev->LanguageID, len);
  156. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  157. dev->enum_state = 6;
  158. return;
  159. case 6: // parse Manufacturer string
  160. // TODO: receive the string...
  161. if (enumbuf[1]) dev->enum_state = 7;
  162. else if (enumbuf[2]) dev->enum_state = 9;
  163. else dev->enum_state = 11;
  164. break;
  165. case 7: // request Product string
  166. len = sizeof(enumbuf) - 4;
  167. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[1], dev->LanguageID, len);
  168. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  169. dev->enum_state = 8;
  170. return;
  171. case 8: // parse Product string
  172. // TODO: receive the string...
  173. if (enumbuf[2]) dev->enum_state = 9;
  174. else dev->enum_state = 11;
  175. break;
  176. case 9: // request Serial Number string
  177. len = sizeof(enumbuf) - 4;
  178. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[2], dev->LanguageID, len);
  179. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  180. dev->enum_state = 10;
  181. return;
  182. case 10: // parse Serial Number string
  183. // TODO: receive the string...
  184. dev->enum_state = 11;
  185. break;
  186. case 11: // request first 9 bytes of config desc
  187. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, 9); // 6=GET_DESCRIPTOR
  188. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  189. dev->enum_state = 12;
  190. return;
  191. case 12: // read 9 bytes, request all of config desc
  192. enumlen = enumbuf[2] | (enumbuf[3] << 8);
  193. println("Config data length = ", enumlen);
  194. if (enumlen > sizeof(enumbuf)) {
  195. // TODO: how to handle device with too much config data
  196. }
  197. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, enumlen); // 6=GET_DESCRIPTOR
  198. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  199. dev->enum_state = 13;
  200. return;
  201. case 13: // read all config desc, send set config
  202. println("bNumInterfaces = ", enumbuf[4]);
  203. println("bConfigurationValue = ", enumbuf[5]);
  204. dev->bmAttributes = enumbuf[7];
  205. dev->bMaxPower = enumbuf[8];
  206. // TODO: actually do something with interface descriptor?
  207. mk_setup(enumsetup, 0, 9, enumbuf[5], 0, 0); // 9=SET_CONFIGURATION
  208. queue_Control_Transfer(dev, &enumsetup, NULL, NULL);
  209. dev->enum_state = 14;
  210. return;
  211. case 14: // device is now configured
  212. claim_drivers(dev);
  213. dev->enum_state = 15;
  214. // TODO: unlock exclusive access to enumeration process
  215. // if any detected devices are waiting, start the first
  216. return;
  217. case 15: // control transfers for other stuff?
  218. // TODO: handle other standard control: set/clear feature, etc
  219. default:
  220. return;
  221. }
  222. }
  223. }
  224. void USBHost::claim_drivers(Device_t *dev)
  225. {
  226. USBDriver *driver, *prev=NULL;
  227. // first check if any driver wishes to claim the entire device
  228. for (driver=available_drivers; driver != NULL; driver = driver->next) {
  229. if (driver->claim(dev, 0, enumbuf + 9, enumlen - 9)) {
  230. if (prev) {
  231. prev->next = driver->next;
  232. } else {
  233. available_drivers = driver->next;
  234. }
  235. driver->device = dev;
  236. driver->next = NULL;
  237. dev->drivers = driver;
  238. return;
  239. }
  240. prev = driver;
  241. }
  242. // parse interfaces from config descriptor
  243. const uint8_t *p = enumbuf + 9;
  244. const uint8_t *end = enumbuf + enumlen;
  245. while (p < end) {
  246. uint8_t desclen = *p;
  247. uint8_t desctype = *(p+1);
  248. print("Descriptor ");
  249. print(desctype);
  250. print(" = ");
  251. if (desctype == 4) println("INTERFACE");
  252. else if (desctype == 5) println("ENDPOINT");
  253. else if (desctype == 6) println("DEV_QUALIFIER");
  254. else if (desctype == 7) println("OTHER_SPEED");
  255. else if (desctype == 11) println("IAD");
  256. else if (desctype == 33) println("HID");
  257. else println(" ???");
  258. if (desctype == 11 && desclen == 8) {
  259. // TODO: parse IAD, ask drivers for claim
  260. // TODO: how to skip over all interfaces IAD represented
  261. }
  262. if (desctype == 4 && desclen == 9) {
  263. // found an interface, ask available drivers if they want it
  264. prev = NULL;
  265. for (driver=available_drivers; driver != NULL; driver = driver->next) {
  266. // TODO: should parse ahead and give claim()
  267. // an accurate length. (end - p) is the rest
  268. // of ALL descriptors, likely more interfaces
  269. // this driver has no business parsing
  270. if (driver->claim(dev, 1, p, end - p)) {
  271. // this driver claims iface
  272. // remove it from available_drivers list
  273. if (prev) {
  274. prev->next = driver->next;
  275. } else {
  276. available_drivers = driver->next;
  277. }
  278. // add to list of drivers using this device
  279. if (dev->drivers) {
  280. dev->drivers->next = driver;
  281. }
  282. dev->drivers = driver;
  283. driver->next = NULL;
  284. driver->device = dev;
  285. // not done, may be more interface for more drivers
  286. }
  287. prev = driver;
  288. }
  289. }
  290. p += desclen;
  291. }
  292. }
  293. static bool address_in_use(uint32_t addr)
  294. {
  295. for (Device_t *p = devlist; p; p = p->next) {
  296. if (p->address == addr) return true;
  297. }
  298. return false;
  299. }
  300. uint32_t USBHost::assign_address(void)
  301. {
  302. static uint8_t last_assigned_address=0;
  303. uint32_t addr = last_assigned_address;
  304. while (1) {
  305. if (++addr > 127) addr = 1;
  306. if (!address_in_use(addr)) {
  307. last_assigned_address = addr;
  308. return addr;
  309. }
  310. }
  311. }
  312. static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
  313. {
  314. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
  315. }
  316. static void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
  317. {
  318. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
  319. }
  320. void USBHost::disconnect_Device(Device_t *dev)
  321. {
  322. if (!dev) return;
  323. println("disconnect_Device:");
  324. // Disconnect all drivers using this device. If this device is
  325. // a hub, the hub driver is responsible for recursively calling
  326. // this function to disconnect its downstream devices.
  327. print_driverlist("available_drivers", available_drivers);
  328. print_driverlist("dev->drivers", dev->drivers);
  329. for (USBDriver *p = dev->drivers; p; ) {
  330. println("disconnect driver ", (uint32_t)p, HEX);
  331. p->disconnect();
  332. USBDriver *next = p->next;
  333. p->next = available_drivers;
  334. available_drivers = p;
  335. p = next;
  336. }
  337. print_driverlist("available_drivers", available_drivers);
  338. // delete all the pipes
  339. for (Pipe_t *p = dev->data_pipes; p; ) {
  340. Pipe_t *next = p->next;
  341. delete_Pipe(p);
  342. p = next;
  343. }
  344. delete_Pipe(dev->control_pipe);
  345. // remove device from devlist and free its Device_t
  346. Device_t *prev_dev = NULL;
  347. for (Device_t *p = devlist; p; p = p->next) {
  348. if (p == dev) {
  349. if (prev_dev == NULL) {
  350. devlist = p->next;
  351. } else {
  352. prev_dev->next = p->next;
  353. }
  354. println("removed Device_t from devlist");
  355. free_Device(p);
  356. break;
  357. }
  358. prev_dev = p;
  359. }
  360. }