Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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