PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

479 lines
16KB

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