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