No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

500 líneas
15KB

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