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.

461 lines
14KB

  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. send_pending_getstatus |= (1 << port);
  109. }
  110. }
  111. void USBHub::send_clearstatus_connect(uint32_t port)
  112. {
  113. if (port == 0 || port > numports) return;
  114. if (can_send_control_now()) {
  115. mk_setup(setup, 0x23, 1, 16, port, 0); // 16=C_PORT_CONNECTION
  116. queue_Control_Transfer(device, &setup, NULL, this);
  117. send_pending_clearstatus_connect &= ~(1 << port);
  118. } else {
  119. send_pending_clearstatus_connect |= (1 << port);
  120. }
  121. }
  122. void USBHub::send_clearstatus_enable(uint32_t port)
  123. {
  124. if (port == 0 || port > numports) return;
  125. if (can_send_control_now()) {
  126. mk_setup(setup, 0x23, 1, 17, port, 0); // 17=C_PORT_ENABLE
  127. queue_Control_Transfer(device, &setup, NULL, this);
  128. send_pending_clearstatus_enable &= ~(1 << port);
  129. } else {
  130. send_pending_clearstatus_enable |= (1 << port);
  131. }
  132. }
  133. void USBHub::send_clearstatus_suspend(uint32_t port)
  134. {
  135. if (port == 0 || port > numports) return;
  136. if (can_send_control_now()) {
  137. mk_setup(setup, 0x23, 1, 18, port, 0); // 18=C_PORT_SUSPEND
  138. queue_Control_Transfer(device, &setup, NULL, this);
  139. send_pending_clearstatus_suspend &= ~(1 << port);
  140. } else {
  141. send_pending_clearstatus_suspend |= (1 << port);
  142. }
  143. }
  144. void USBHub::send_clearstatus_overcurrent(uint32_t port)
  145. {
  146. if (port == 0 || port > numports) return;
  147. if (can_send_control_now()) {
  148. mk_setup(setup, 0x23, 1, 19, port, 0); // 19=C_PORT_OVER_CURRENT
  149. queue_Control_Transfer(device, &setup, NULL, this);
  150. send_pending_clearstatus_overcurrent &= ~(1 << port);
  151. } else {
  152. send_pending_clearstatus_overcurrent |= (1 << port);
  153. }
  154. }
  155. void USBHub::send_clearstatus_reset(uint32_t port)
  156. {
  157. if (port == 0 || port > numports) return;
  158. if (can_send_control_now()) {
  159. mk_setup(setup, 0x23, 1, 20, port, 0); // 20=C_PORT_RESET
  160. queue_Control_Transfer(device, &setup, NULL, this);
  161. send_pending_clearstatus_reset &= ~(1 << port);
  162. } else {
  163. send_pending_clearstatus_reset |= (1 << port);
  164. }
  165. }
  166. void USBHub::send_setreset(uint32_t port)
  167. {
  168. if (port == 0 || port > numports) return;
  169. println("send_setreset");
  170. if (can_send_control_now()) {
  171. mk_setup(setup, 0x23, 3, 4, port, 0); // set feature PORT_RESET
  172. queue_Control_Transfer(device, &setup, NULL, this);
  173. send_pending_setreset &= ~(1 << port);
  174. } else {
  175. send_pending_setreset |= (1 << port);
  176. }
  177. }
  178. static uint32_t lowestbit(uint32_t bitmask)
  179. {
  180. return 31 - __builtin_clz(bitmask);
  181. }
  182. void USBHub::control(const Transfer_t *transfer)
  183. {
  184. println("USBHub control callback");
  185. print_hexbytes(transfer->buffer, transfer->length);
  186. uint32_t port = transfer->setup.wIndex;
  187. uint32_t mesg = transfer->setup.word1;
  188. switch (mesg) {
  189. case 0x290006A0: // read hub descriptor
  190. numports = hub_desc[2];
  191. characteristics = hub_desc[3];
  192. powertime = hub_desc[5];
  193. // TODO: do we need to use the DeviceRemovable
  194. // bits to make synthetic device connect events?
  195. println("Hub ports = ", numports);
  196. for (uint32_t i=1; i <= numports; i++) {
  197. send_poweron(i);
  198. }
  199. break;
  200. case 0x00080323: // power turned on
  201. if (port == numports && changepipe == NULL) {
  202. println("power turned on to all ports");
  203. println("device addr = ", device->address);
  204. changepipe = new_Pipe(device, 3, endpoint, 1, 1, interval);
  205. println("pipe cap1 = ", changepipe->qh.capabilities[0], HEX);
  206. changepipe->callback_function = callback;
  207. queue_Data_Transfer(changepipe, &changebits, 1, this);
  208. }
  209. break;
  210. case 0x000000A0: // get hub status
  211. println("New Hub Status");
  212. break;
  213. case 0x000000A3: // get port status
  214. println("New Port Status");
  215. if (transfer->length == 4) {
  216. uint32_t status = *(uint32_t *)(transfer->buffer);
  217. if (status != statusbits) println("ERROR: status not same");
  218. new_port_status(port, status);
  219. }
  220. //if (changebits & (1 << port)) {
  221. //changebits &= ~(1 << port);
  222. //send_clearstatus(port);
  223. //}
  224. break;
  225. case 0x00100120: // clear hub status
  226. println("Hub Status Cleared");
  227. break;
  228. case 0x00100123: // clear port status
  229. println("Port Status Cleared, port=", port);
  230. break;
  231. default:
  232. println("unhandled setup, message = ", mesg, HEX);
  233. }
  234. // After we've completed processing for this control
  235. // transfer, check if any more need to be sent. These
  236. // allow only a single control transfer to occur at once
  237. // which isn't fast, but requires only 3 Transfer_t and
  238. // allows reusing the setup and other buffers
  239. sending_control_transfer = 0;
  240. if (send_pending_poweron) {
  241. send_poweron(lowestbit(send_pending_poweron));
  242. } else if (send_pending_clearstatus_connect) {
  243. send_clearstatus_connect(lowestbit(send_pending_clearstatus_connect));
  244. } else if (send_pending_clearstatus_enable) {
  245. send_clearstatus_enable(lowestbit(send_pending_clearstatus_enable));
  246. } else if (send_pending_clearstatus_suspend) {
  247. send_clearstatus_suspend(lowestbit(send_pending_clearstatus_suspend));
  248. } else if (send_pending_clearstatus_overcurrent) {
  249. send_clearstatus_overcurrent(lowestbit(send_pending_clearstatus_overcurrent));
  250. } else if (send_pending_clearstatus_reset) {
  251. send_clearstatus_reset(lowestbit(send_pending_clearstatus_reset));
  252. } else if (send_pending_getstatus) {
  253. send_getstatus(lowestbit(send_pending_getstatus));
  254. } else if (send_pending_setreset) {
  255. send_setreset(lowestbit(send_pending_setreset));
  256. }
  257. }
  258. void USBHub::callback(const Transfer_t *transfer)
  259. {
  260. //println("HUB Callback (static)");
  261. if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
  262. }
  263. void USBHub::status_change(const Transfer_t *transfer)
  264. {
  265. println("HUB Callback (member)");
  266. println("status = ", changebits, HEX);
  267. for (uint32_t i=0; i <= numports; i++) {
  268. if (changebits & (1 << i)) {
  269. send_getstatus(i);
  270. }
  271. }
  272. queue_Data_Transfer(changepipe, &changebits, 1, this);
  273. }
  274. void USBHub::new_port_status(uint32_t port, uint32_t status)
  275. {
  276. if (port == 0 || port > numports) return;
  277. #if 1
  278. print(" status=");
  279. print(status, HEX);
  280. println(" port=", port);
  281. println(" state=", portstate[port-1]);
  282. // status bits, USB 2.0: 11.24.2.7.1 page 427
  283. if (status & 0x0001) println(" Device is present: ");
  284. if (status & 0x0002) {
  285. print(" Enabled, speed = ");
  286. if (status & 0x0200) {
  287. print("1.5");
  288. } else {
  289. if (status & 0x0400) {
  290. print("480");
  291. } else {
  292. print("12");
  293. }
  294. }
  295. println(" Mbit/sec");
  296. }
  297. if (status & 0x0004) println(" Suspended");
  298. if (status & 0x0008) println(" Over-current");
  299. if (status & 0x0010) println(" Reset");
  300. if (status & 0x0100) println(" Has Power");
  301. if (status & 0x0800) println(" Test Mode");
  302. if (status & 0x1000) println(" Software Controls LEDs");
  303. #endif
  304. uint8_t &state = portstate[port-1];
  305. switch (state) {
  306. case PORT_OFF:
  307. case PORT_DISCONNECT:
  308. if (status & 0x0001) { // connected
  309. state = PORT_DEBOUNCE1;
  310. start_debounce_timer(port);
  311. send_clearstatus_connect(port);
  312. }
  313. break;
  314. case PORT_DEBOUNCE1:
  315. case PORT_DEBOUNCE2:
  316. case PORT_DEBOUNCE3:
  317. case PORT_DEBOUNCE4:
  318. case PORT_DEBOUNCE5:
  319. if (status & 0x0001) {
  320. if (++state > PORT_DEBOUNCE5) {
  321. if (USBHub::reset_busy || USBHost::enumeration_busy) {
  322. // wait in debounce state if another port is
  323. // resetting or a device is busy enumerating
  324. state = PORT_DEBOUNCE5;
  325. break;
  326. }
  327. USBHub::reset_busy = true;
  328. stop_debounce_timer(port);
  329. state = PORT_RESET;
  330. println("sending reset");
  331. send_setreset(port);
  332. port_doing_reset = port;
  333. }
  334. } else {
  335. state = PORT_DISCONNECT;
  336. }
  337. break;
  338. case PORT_RESET:
  339. if (status & 0x0002) {
  340. // port is now enabled
  341. send_clearstatus_reset(port);
  342. state = PORT_RECOVERY;
  343. uint8_t speed=0;
  344. if (status & 0x0200) speed = 1;
  345. else if (status & 0x0400) speed = 2;
  346. port_doing_reset_speed = speed;
  347. resettimer.start(25000);
  348. }
  349. break;
  350. case PORT_RECOVERY:
  351. break;
  352. case PORT_ACTIVE:
  353. break;
  354. }
  355. }
  356. void USBHub::timer_event(USBDriverTimer *timer)
  357. {
  358. uint32_t us = micros() - timer->started_micros;
  359. print("timer event (");
  360. print(us);
  361. print(" us): ");
  362. print((char *)timer->pointer);
  363. print(", this = ");
  364. print((uint32_t)this, HEX);
  365. println(", timer = ", (uint32_t)timer, HEX);
  366. if (timer == &debouncetimer) {
  367. uint32_t in_use = debounce_in_use;
  368. if (in_use) {
  369. for (uint32_t i=1; i < numports; i++) {
  370. if (in_use & (1 << i)) send_getstatus(i);
  371. }
  372. debouncetimer.start(20000);
  373. }
  374. } else if (timer == &resettimer) {
  375. uint8_t port = port_doing_reset;
  376. println("port_doing_reset = ", port);
  377. if (port_doing_reset) {
  378. uint8_t &state = portstate[port-1];
  379. if (state == PORT_RECOVERY) {
  380. port_doing_reset = 0;
  381. println("PORT_RECOVERY");
  382. // begin enumeration process
  383. uint8_t speed = port_doing_reset_speed;
  384. devicelist[port-1] = new_Device(speed, device->address, port);
  385. // TODO: if return is NULL, what to do? Panic?
  386. // Can we disable the port? Will this device
  387. // play havoc if it sits unconfigured responding
  388. // to address zero? Does that even matter? Maybe
  389. // we have far worse issues when memory isn't
  390. // available?!
  391. USBHub::reset_busy = false;
  392. state = PORT_ACTIVE;
  393. }
  394. }
  395. }
  396. // TODO: testing only!!!
  397. static uint32_t count=0;
  398. if (++count > 36) while (1) ; // stop here
  399. }
  400. void USBHub::start_debounce_timer(uint32_t port)
  401. {
  402. if (debounce_in_use == 0) debouncetimer.start(20000);
  403. debounce_in_use |= (1 << port);
  404. }
  405. void USBHub::stop_debounce_timer(uint32_t port)
  406. {
  407. debounce_in_use &= ~(1 << port);
  408. }
  409. void USBHub::disconnect()
  410. {
  411. // TODO: free resources
  412. }
  413. /*
  414. config descriptor from a Multi-TT hub
  415. 09 02 29 00 01 01 00 E0 32
  416. 09 04 00 00 01 09 00 01 00
  417. 07 05 81 03 01 00 0C
  418. 09 04 00 01 01 09 00 02 00
  419. 07 05 81 03 01 00 0C
  420. */