Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

438 lines
14KB

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