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.

7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. // True when any hub port is in the reset or reset recovery phase.
  26. // Only one USB device may be reset at a time, because it will
  27. // begin responding to address zero.
  28. volatile bool USBHub::reset_busy = false;
  29. USBHub::USBHub() : debouncetimer(this), resettimer(this)
  30. {
  31. // TODO: free Device_t, Pipe_t & Transfer_t we will need
  32. driver_ready_for_device(this);
  33. }
  34. bool USBHub::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  35. {
  36. // only claim entire device, never at interface level
  37. if (type != 0) return false;
  38. println("USBHub memory usage = ", sizeof(USBHub));
  39. println("USBHub claim_device this=", (uint32_t)this, HEX);
  40. resettimer.pointer = (void *)"Hello, I'm resettimer";
  41. debouncetimer.pointer = (void *)"Debounce Timer";
  42. // check for HUB type
  43. if (dev->bDeviceClass != 9 || dev->bDeviceSubClass != 0) return false;
  44. // protocol must be 0=FS, 1=HS Single-TT, or 2=HS Multi-TT
  45. if (dev->bDeviceProtocol > 2) return false;
  46. // check for endpoint descriptor
  47. if (descriptors[9] != 7 || descriptors[10] != 5) return false;
  48. // endpoint must be IN direction
  49. if ((descriptors[11] & 0xF0) != 0x80) return false;
  50. // endpoint type must be interrupt
  51. if (descriptors[12] != 3) return false;
  52. // get the endpoint number, must not be zero
  53. endpoint = descriptors[11] & 0x0F;
  54. if (endpoint == 0) return false;
  55. // get the maximum packet size
  56. uint32_t maxsize = descriptors[13] | (descriptors[14] << 8);
  57. if (maxsize == 0) return false;
  58. if (maxsize > 1) return false; // do hub chips with > 7 ports exist?
  59. interval = descriptors[15];
  60. println(" polling interval = ", interval);
  61. println(descriptors[9]);
  62. println(descriptors[10]);
  63. println(descriptors[11], HEX);
  64. println(maxsize);
  65. // bDeviceProtocol = 0 is full speed
  66. // bDeviceProtocol = 1 is high speed single TT
  67. // bDeviceProtocol = 2 is high speed multiple TT
  68. println("bDeviceClass = ", dev->bDeviceClass);
  69. println("bDeviceSubClass = ", dev->bDeviceSubClass);
  70. println("bDeviceProtocol = ", dev->bDeviceProtocol);
  71. numports = 0; // unknown until hub descriptor is read
  72. changepipe = NULL;
  73. changebits = 0;
  74. sending_control_transfer = 0;
  75. port_doing_reset = 0;
  76. memset(portstate, 0, sizeof(portstate));
  77. memset(devicelist, 0, sizeof(devicelist));
  78. mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
  79. queue_Control_Transfer(dev, &setup, hub_desc, this);
  80. return true;
  81. }
  82. bool USBHub::can_send_control_now()
  83. {
  84. if (sending_control_transfer) return false;
  85. sending_control_transfer = 1;
  86. return true;
  87. }
  88. void USBHub::send_poweron(uint32_t port)
  89. {
  90. if (port == 0 || port > numports) return;
  91. if (can_send_control_now()) {
  92. mk_setup(setup, 0x23, 3, 8, port, 0);
  93. queue_Control_Transfer(device, &setup, NULL, this);
  94. send_pending_poweron &= ~(1 << port);
  95. } else {
  96. send_pending_poweron |= (1 << port);
  97. }
  98. }
  99. void USBHub::send_getstatus(uint32_t port)
  100. {
  101. if (port > numports) return;
  102. if (can_send_control_now()) {
  103. println("getstatus, port = ", port);
  104. mk_setup(setup, ((port > 0) ? 0xA3 : 0xA0), 0, 0, port, 4);
  105. queue_Control_Transfer(device, &setup, &statusbits, this);
  106. send_pending_getstatus &= ~(1 << port);
  107. } else {
  108. println("deferred getstatus, port = ", port);
  109. send_pending_getstatus |= (1 << port);
  110. }
  111. }
  112. void USBHub::send_clearstatus_connect(uint32_t port)
  113. {
  114. if (port == 0 || port > numports) return;
  115. if (can_send_control_now()) {
  116. mk_setup(setup, 0x23, 1, 16, port, 0); // 16=C_PORT_CONNECTION
  117. queue_Control_Transfer(device, &setup, NULL, this);
  118. send_pending_clearstatus_connect &= ~(1 << port);
  119. } else {
  120. send_pending_clearstatus_connect |= (1 << port);
  121. }
  122. }
  123. void USBHub::send_clearstatus_enable(uint32_t port)
  124. {
  125. if (port == 0 || port > numports) return;
  126. if (can_send_control_now()) {
  127. mk_setup(setup, 0x23, 1, 17, port, 0); // 17=C_PORT_ENABLE
  128. queue_Control_Transfer(device, &setup, NULL, this);
  129. send_pending_clearstatus_enable &= ~(1 << port);
  130. } else {
  131. send_pending_clearstatus_enable |= (1 << port);
  132. }
  133. }
  134. void USBHub::send_clearstatus_suspend(uint32_t port)
  135. {
  136. if (port == 0 || port > numports) return;
  137. if (can_send_control_now()) {
  138. mk_setup(setup, 0x23, 1, 18, port, 0); // 18=C_PORT_SUSPEND
  139. queue_Control_Transfer(device, &setup, NULL, this);
  140. send_pending_clearstatus_suspend &= ~(1 << port);
  141. } else {
  142. send_pending_clearstatus_suspend |= (1 << port);
  143. }
  144. }
  145. void USBHub::send_clearstatus_overcurrent(uint32_t port)
  146. {
  147. if (port == 0 || port > numports) return;
  148. if (can_send_control_now()) {
  149. mk_setup(setup, 0x23, 1, 19, port, 0); // 19=C_PORT_OVER_CURRENT
  150. queue_Control_Transfer(device, &setup, NULL, this);
  151. send_pending_clearstatus_overcurrent &= ~(1 << port);
  152. } else {
  153. send_pending_clearstatus_overcurrent |= (1 << port);
  154. }
  155. }
  156. void USBHub::send_clearstatus_reset(uint32_t port)
  157. {
  158. if (port == 0 || port > numports) return;
  159. if (can_send_control_now()) {
  160. mk_setup(setup, 0x23, 1, 20, port, 0); // 20=C_PORT_RESET
  161. queue_Control_Transfer(device, &setup, NULL, this);
  162. send_pending_clearstatus_reset &= ~(1 << port);
  163. } else {
  164. send_pending_clearstatus_reset |= (1 << port);
  165. }
  166. }
  167. void USBHub::send_setreset(uint32_t port)
  168. {
  169. if (port == 0 || port > numports) return;
  170. println("send_setreset");
  171. if (can_send_control_now()) {
  172. mk_setup(setup, 0x23, 3, 4, port, 0); // set feature PORT_RESET
  173. queue_Control_Transfer(device, &setup, NULL, this);
  174. send_pending_setreset &= ~(1 << port);
  175. } else {
  176. send_pending_setreset |= (1 << port);
  177. }
  178. }
  179. static uint32_t lowestbit(uint32_t bitmask)
  180. {
  181. return 31 - __builtin_clz(bitmask);
  182. }
  183. void USBHub::control(const Transfer_t *transfer)
  184. {
  185. println("USBHub control callback");
  186. print_hexbytes(transfer->buffer, transfer->length);
  187. uint32_t port = transfer->setup.wIndex;
  188. uint32_t mesg = transfer->setup.word1;
  189. switch (mesg) {
  190. case 0x290006A0: // read hub descriptor
  191. numports = hub_desc[2];
  192. characteristics = hub_desc[3];
  193. powertime = hub_desc[5];
  194. // TODO: do we need to use the DeviceRemovable
  195. // bits to make synthetic device connect events?
  196. println("Hub ports = ", numports);
  197. for (uint32_t i=1; i <= numports; i++) {
  198. send_poweron(i);
  199. }
  200. break;
  201. case 0x00080323: // power turned on
  202. if (port == numports && changepipe == NULL) {
  203. println("power turned on to all ports");
  204. println("device addr = ", device->address);
  205. changepipe = new_Pipe(device, 3, endpoint, 1, 1, interval);
  206. println("pipe cap1 = ", changepipe->qh.capabilities[0], HEX);
  207. changepipe->callback_function = callback;
  208. queue_Data_Transfer(changepipe, &changebits, 1, this);
  209. }
  210. break;
  211. case 0x000000A0: // get hub status
  212. println("New Hub Status");
  213. break;
  214. case 0x000000A3: // get port status
  215. println("New Port Status");
  216. if (transfer->length == 4) {
  217. uint32_t status = *(uint32_t *)(transfer->buffer);
  218. if (status != statusbits) println("ERROR: status not same");
  219. new_port_status(port, status);
  220. }
  221. //if (changebits & (1 << port)) {
  222. //changebits &= ~(1 << port);
  223. //send_clearstatus(port);
  224. //}
  225. break;
  226. case 0x00100120: // clear hub status
  227. println("Hub Status Cleared");
  228. break;
  229. case 0x00100123: // clear port status
  230. println("Port Status Cleared, port=", port);
  231. break;
  232. default:
  233. println("unhandled setup, message = ", mesg, HEX);
  234. }
  235. // After we've completed processing for this control
  236. // transfer, check if any more need to be sent. These
  237. // allow only a single control transfer to occur at once
  238. // which isn't fast, but requires only 3 Transfer_t and
  239. // allows reusing the setup and other buffers
  240. sending_control_transfer = 0;
  241. if (send_pending_poweron) {
  242. send_poweron(lowestbit(send_pending_poweron));
  243. } else if (send_pending_clearstatus_connect) {
  244. send_clearstatus_connect(lowestbit(send_pending_clearstatus_connect));
  245. } else if (send_pending_clearstatus_enable) {
  246. send_clearstatus_enable(lowestbit(send_pending_clearstatus_enable));
  247. } else if (send_pending_clearstatus_suspend) {
  248. send_clearstatus_suspend(lowestbit(send_pending_clearstatus_suspend));
  249. } else if (send_pending_clearstatus_overcurrent) {
  250. send_clearstatus_overcurrent(lowestbit(send_pending_clearstatus_overcurrent));
  251. } else if (send_pending_clearstatus_reset) {
  252. send_clearstatus_reset(lowestbit(send_pending_clearstatus_reset));
  253. } else if (send_pending_getstatus) {
  254. send_getstatus(lowestbit(send_pending_getstatus));
  255. } else if (send_pending_setreset) {
  256. send_setreset(lowestbit(send_pending_setreset));
  257. }
  258. }
  259. void USBHub::callback(const Transfer_t *transfer)
  260. {
  261. //println("HUB Callback (static)");
  262. if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
  263. }
  264. void USBHub::status_change(const Transfer_t *transfer)
  265. {
  266. println("HUB Callback (member)");
  267. println("status = ", changebits, HEX);
  268. for (uint32_t i=0; i <= numports; i++) {
  269. if (changebits & (1 << i)) {
  270. send_getstatus(i);
  271. }
  272. }
  273. queue_Data_Transfer(changepipe, &changebits, 1, this);
  274. }
  275. void USBHub::new_port_status(uint32_t port, uint32_t status)
  276. {
  277. if (port == 0 || port > numports) return;
  278. #if 1
  279. print(" status=");
  280. print(status, HEX);
  281. println(" port=", port);
  282. println(" state=", portstate[port-1]);
  283. // status bits, USB 2.0: 11.24.2.7.1 page 427
  284. if (status & 0x0001) println(" Device is present: ");
  285. if (status & 0x0002) {
  286. print(" Enabled, speed = ");
  287. if (status & 0x0200) {
  288. print("1.5");
  289. } else {
  290. if (status & 0x0400) {
  291. print("480");
  292. } else {
  293. print("12");
  294. }
  295. }
  296. println(" Mbit/sec");
  297. }
  298. if (status & 0x0004) println(" Suspended");
  299. if (status & 0x0008) println(" Over-current");
  300. if (status & 0x0010) println(" Reset");
  301. if (status & 0x0100) println(" Has Power");
  302. if (status & 0x0800) println(" Test Mode");
  303. if (status & 0x1000) println(" Software Controls LEDs");
  304. #endif
  305. uint8_t &state = portstate[port-1];
  306. switch (state) {
  307. case PORT_OFF:
  308. case PORT_DISCONNECT:
  309. if (status & 0x0001) { // connected
  310. state = PORT_DEBOUNCE1;
  311. start_debounce_timer(port);
  312. send_clearstatus_connect(port);
  313. }
  314. break;
  315. case PORT_DEBOUNCE1:
  316. case PORT_DEBOUNCE2:
  317. case PORT_DEBOUNCE3:
  318. case PORT_DEBOUNCE4:
  319. case PORT_DEBOUNCE5:
  320. if (status & 0x0001) {
  321. if (++state > PORT_DEBOUNCE5) {
  322. if (USBHub::reset_busy || USBHost::enumeration_busy) {
  323. // wait in debounce state if another port is
  324. // resetting or a device is busy enumerating
  325. state = PORT_DEBOUNCE5;
  326. break;
  327. }
  328. USBHub::reset_busy = true;
  329. stop_debounce_timer(port);
  330. state = PORT_RESET;
  331. println("sending reset");
  332. send_setreset(port);
  333. port_doing_reset = port;
  334. }
  335. } else {
  336. stop_debounce_timer(port);
  337. state = PORT_DISCONNECT;
  338. }
  339. break;
  340. case PORT_RESET:
  341. if (status & 0x0002) {
  342. // port is now enabled
  343. send_clearstatus_reset(port);
  344. state = PORT_RECOVERY;
  345. uint8_t speed=0;
  346. if (status & 0x0200) speed = 1;
  347. else if (status & 0x0400) speed = 2;
  348. port_doing_reset_speed = speed;
  349. resettimer.start(25000);
  350. } else if (!(status & 0x0001)) {
  351. send_clearstatus_connect(port);
  352. USBHub::reset_busy = false;
  353. state = PORT_DISCONNECT;
  354. }
  355. break;
  356. case PORT_RECOVERY:
  357. if (!(status & 0x0001)) {
  358. send_clearstatus_connect(port);
  359. USBHub::reset_busy = false;
  360. state = PORT_DISCONNECT;
  361. }
  362. break;
  363. case PORT_ACTIVE:
  364. if (!(status & 0x0001)) {
  365. disconnect_Device(devicelist[port-1]);
  366. devicelist[port-1] = NULL;
  367. send_clearstatus_connect(port);
  368. state = PORT_DISCONNECT;
  369. }
  370. break;
  371. }
  372. }
  373. void USBHub::timer_event(USBDriverTimer *timer)
  374. {
  375. uint32_t us = micros() - timer->started_micros;
  376. print("timer event (");
  377. print(us);
  378. print(" us): ");
  379. print((char *)timer->pointer);
  380. print(", this = ");
  381. print((uint32_t)this, HEX);
  382. println(", timer = ", (uint32_t)timer, HEX);
  383. if (timer == &debouncetimer) {
  384. uint32_t in_use = debounce_in_use;
  385. println("ports in use bitmask = ", in_use, HEX);
  386. if (in_use) {
  387. for (uint32_t i=1; i <= numports; i++) {
  388. if (in_use & (1 << i)) send_getstatus(i);
  389. }
  390. debouncetimer.start(20000);
  391. }
  392. } else if (timer == &resettimer) {
  393. uint8_t port = port_doing_reset;
  394. println("port_doing_reset = ", port);
  395. if (port_doing_reset) {
  396. uint8_t &state = portstate[port-1];
  397. if (state == PORT_RECOVERY) {
  398. port_doing_reset = 0;
  399. println("PORT_RECOVERY");
  400. // begin enumeration process
  401. uint8_t speed = port_doing_reset_speed;
  402. devicelist[port-1] = new_Device(speed, device->address, port);
  403. // TODO: if return is NULL, what to do? Panic?
  404. // Can we disable the port? Will this device
  405. // play havoc if it sits unconfigured responding
  406. // to address zero? Does that even matter? Maybe
  407. // we have far worse issues when memory isn't
  408. // available?!
  409. USBHub::reset_busy = false;
  410. state = PORT_ACTIVE;
  411. }
  412. }
  413. }
  414. // TODO: testing only!!!
  415. //static uint32_t count=0;
  416. //if (++count > 36) while (1) ; // stop here
  417. }
  418. void USBHub::start_debounce_timer(uint32_t port)
  419. {
  420. if (debounce_in_use == 0) debouncetimer.start(20000);
  421. debounce_in_use |= (1 << port);
  422. }
  423. void USBHub::stop_debounce_timer(uint32_t port)
  424. {
  425. debounce_in_use &= ~(1 << port);
  426. }
  427. void USBHub::disconnect()
  428. {
  429. // TODO: free resources
  430. }
  431. /*
  432. config descriptor from a Multi-TT hub
  433. 09 02 29 00 01 01 00 E0 32
  434. 09 04 00 00 01 09 00 01 00
  435. 07 05 81 03 01 00 0C
  436. 09 04 00 01 01 09 00 02 00
  437. 07 05 81 03 01 00 0C
  438. */