選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

397 行
12KB

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