Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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