Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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