You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 satır
15KB

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