Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

enumeration.cpp 16KB

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