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.

enumeration.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. dev->iStrings[0] = 0; // Set indexes into string buffer to say not there...
  184. dev->iStrings[1] = 0;
  185. dev->iStrings[2] = 0;
  186. dev->string_buf[0] = 0; // have trailing NULL..
  187. if (enumbuf[4] < 4 || enumbuf[5] != 3) {
  188. dev->enum_state = 11;
  189. } else {
  190. dev->LanguageID = enumbuf[6] | (enumbuf[7] << 8);
  191. if (enumbuf[0]) dev->enum_state = 5;
  192. else if (enumbuf[1]) dev->enum_state = 7;
  193. else if (enumbuf[2]) dev->enum_state = 9;
  194. else dev->enum_state = 11;
  195. }
  196. break;
  197. case 5: // request Manufacturer string
  198. len = sizeof(enumbuf) - 4;
  199. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[0], dev->LanguageID, len);
  200. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  201. dev->enum_state = 6;
  202. return;
  203. case 6: // parse Manufacturer string
  204. convertStringDescriptorToASCIIString(0, dev, transfer);
  205. // TODO: receive the string...
  206. if (enumbuf[1]) dev->enum_state = 7;
  207. else if (enumbuf[2]) dev->enum_state = 9;
  208. else dev->enum_state = 11;
  209. break;
  210. case 7: // request Product string
  211. len = sizeof(enumbuf) - 4;
  212. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[1], dev->LanguageID, len);
  213. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  214. dev->enum_state = 8;
  215. return;
  216. case 8: // parse Product string
  217. convertStringDescriptorToASCIIString(1, dev, transfer);
  218. if (enumbuf[2]) dev->enum_state = 9;
  219. else dev->enum_state = 11;
  220. break;
  221. case 9: // request Serial Number string
  222. len = sizeof(enumbuf) - 4;
  223. mk_setup(enumsetup, 0x80, 6, 0x0300 | enumbuf[2], dev->LanguageID, len);
  224. queue_Control_Transfer(dev, &enumsetup, enumbuf + 4, NULL);
  225. dev->enum_state = 10;
  226. return;
  227. case 10: // parse Serial Number string
  228. convertStringDescriptorToASCIIString(2, dev, transfer);
  229. dev->enum_state = 11;
  230. break;
  231. case 11: // request first 9 bytes of config desc
  232. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, 9); // 6=GET_DESCRIPTOR
  233. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  234. dev->enum_state = 12;
  235. return;
  236. case 12: // read 9 bytes, request all of config desc
  237. enumlen = enumbuf[2] | (enumbuf[3] << 8);
  238. println("Config data length = ", enumlen);
  239. if (enumlen > sizeof(enumbuf)) {
  240. // TODO: how to handle device with too much config data
  241. }
  242. mk_setup(enumsetup, 0x80, 6, 0x0200, 0, enumlen); // 6=GET_DESCRIPTOR
  243. queue_Control_Transfer(dev, &enumsetup, enumbuf, NULL);
  244. dev->enum_state = 13;
  245. return;
  246. case 13: // read all config desc, send set config
  247. println("bNumInterfaces = ", enumbuf[4]);
  248. println("bConfigurationValue = ", enumbuf[5]);
  249. dev->bmAttributes = enumbuf[7];
  250. dev->bMaxPower = enumbuf[8];
  251. // TODO: actually do something with interface descriptor?
  252. mk_setup(enumsetup, 0, 9, enumbuf[5], 0, 0); // 9=SET_CONFIGURATION
  253. queue_Control_Transfer(dev, &enumsetup, NULL, NULL);
  254. dev->enum_state = 14;
  255. return;
  256. case 14: // device is now configured
  257. claim_drivers(dev);
  258. dev->enum_state = 15;
  259. // unlock exclusive access to enumeration process. If any
  260. // more devices are waiting, the hub driver is responsible
  261. // for resetting their ports and starting their enumeration
  262. // when the port enables.
  263. USBHost::enumeration_busy = false;
  264. return;
  265. case 15: // control transfers for other stuff?
  266. // TODO: handle other standard control: set/clear feature, etc
  267. default:
  268. return;
  269. }
  270. }
  271. }
  272. void USBHost::convertStringDescriptorToASCIIString(uint8_t string_index, Device_t *dev, const Transfer_t *transfer) {
  273. uint8_t *buffer = (uint8_t*)transfer->buffer;
  274. uint8_t buf_index = string_index? dev->iStrings[string_index]+1 : 0;
  275. // Try to verify - The first byte should be length and the 2nd byte should be 0x3
  276. if (!buffer || (buffer[1] != 0x3)) {
  277. return; // No string so can simply return
  278. }
  279. dev->iStrings[string_index] = buf_index; // remember our starting positio
  280. uint8_t count_bytes_returned = buffer[0];
  281. if ((buf_index + count_bytes_returned/2) >= DEVICE_STRUCT_STRING_BUF_SIZE)
  282. count_bytes_returned = (DEVICE_STRUCT_STRING_BUF_SIZE - buf_index) * 2;
  283. // Now copy into our storage buffer.
  284. for (uint8_t i = 2; (i < count_bytes_returned) && (buf_index < (DEVICE_STRUCT_STRING_BUF_SIZE -1)); i += 2) {
  285. dev->string_buf[buf_index++] = buffer[i];
  286. }
  287. dev->string_buf[buf_index] = 0; // null terminate.
  288. // Update other indexes to point to null character
  289. while (++string_index < 3) {
  290. dev->iStrings[string_index] = buf_index; // point to trailing NULL character
  291. }
  292. }
  293. void USBHost::claim_drivers(Device_t *dev)
  294. {
  295. USBDriver *driver, *prev=NULL;
  296. // first check if any driver wishes to claim the entire device
  297. for (driver=available_drivers; driver != NULL; driver = driver->next) {
  298. if (driver->device != NULL) continue;
  299. if (driver->claim(dev, 0, enumbuf + 9, enumlen - 9)) {
  300. if (prev) {
  301. prev->next = driver->next;
  302. } else {
  303. available_drivers = driver->next;
  304. }
  305. driver->device = dev;
  306. driver->next = NULL;
  307. dev->drivers = driver;
  308. return;
  309. }
  310. prev = driver;
  311. }
  312. // parse interfaces from config descriptor
  313. const uint8_t *p = enumbuf + 9;
  314. const uint8_t *end = enumbuf + enumlen;
  315. while (p < end) {
  316. uint8_t desclen = *p;
  317. uint8_t desctype = *(p+1);
  318. print("Descriptor ");
  319. print(desctype);
  320. print(" = ");
  321. if (desctype == 4) println("INTERFACE");
  322. else if (desctype == 5) println("ENDPOINT");
  323. else if (desctype == 6) println("DEV_QUALIFIER");
  324. else if (desctype == 7) println("OTHER_SPEED");
  325. else if (desctype == 11) println("IAD");
  326. else if (desctype == 33) println("HID");
  327. else println(" ???");
  328. if (desctype == 11 && desclen == 8) {
  329. // TODO: parse IAD, ask drivers for claim
  330. // TODO: how to skip over all interfaces IAD represented
  331. }
  332. if (desctype == 4 && desclen == 9) {
  333. // found an interface, ask available drivers if they want it
  334. prev = NULL;
  335. for (driver=available_drivers; driver != NULL; driver = driver->next) {
  336. if (driver->device != NULL) continue;
  337. // TODO: should parse ahead and give claim()
  338. // an accurate length. (end - p) is the rest
  339. // of ALL descriptors, likely more interfaces
  340. // this driver has no business parsing
  341. if (driver->claim(dev, 1, p, end - p)) {
  342. // this driver claims iface
  343. // remove it from available_drivers list
  344. if (prev) {
  345. prev->next = driver->next;
  346. } else {
  347. available_drivers = driver->next;
  348. }
  349. // add to list of drivers using this device
  350. driver->next = dev->drivers;
  351. dev->drivers = driver;
  352. driver->device = dev;
  353. // not done, may be more interface for more drivers
  354. }
  355. prev = driver;
  356. }
  357. }
  358. p += desclen;
  359. }
  360. }
  361. static bool address_in_use(uint32_t addr)
  362. {
  363. for (Device_t *p = devlist; p; p = p->next) {
  364. if (p->address == addr) return true;
  365. }
  366. return false;
  367. }
  368. uint32_t USBHost::assign_address(void)
  369. {
  370. static uint8_t last_assigned_address=0;
  371. uint32_t addr = last_assigned_address;
  372. while (1) {
  373. if (++addr > 127) addr = 1;
  374. if (!address_in_use(addr)) {
  375. last_assigned_address = addr;
  376. return addr;
  377. }
  378. }
  379. }
  380. static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
  381. {
  382. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
  383. }
  384. static void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
  385. {
  386. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
  387. }
  388. void USBHost::disconnect_Device(Device_t *dev)
  389. {
  390. if (!dev) return;
  391. println("disconnect_Device:");
  392. // Disconnect all drivers using this device. If this device is
  393. // a hub, the hub driver is responsible for recursively calling
  394. // this function to disconnect its downstream devices.
  395. print_driverlist("available_drivers", available_drivers);
  396. print_driverlist("dev->drivers", dev->drivers);
  397. for (USBDriver *p = dev->drivers; p; ) {
  398. println("disconnect driver ", (uint32_t)p, HEX);
  399. p->disconnect();
  400. p->device = NULL;
  401. USBDriver *next = p->next;
  402. p->next = available_drivers;
  403. available_drivers = p;
  404. p = next;
  405. }
  406. print_driverlist("available_drivers", available_drivers);
  407. // delete all the pipes
  408. for (Pipe_t *p = dev->data_pipes; p; ) {
  409. Pipe_t *next = p->next;
  410. delete_Pipe(p);
  411. p = next;
  412. }
  413. delete_Pipe(dev->control_pipe);
  414. // remove device from devlist and free its Device_t
  415. Device_t *prev_dev = NULL;
  416. for (Device_t *p = devlist; p; p = p->next) {
  417. if (p == dev) {
  418. if (prev_dev == NULL) {
  419. devlist = p->next;
  420. } else {
  421. prev_dev->next = p->next;
  422. }
  423. println("removed Device_t from devlist");
  424. free_Device(p);
  425. break;
  426. }
  427. prev_dev = p;
  428. }
  429. }