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.

270 lines
7.7KB

  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()
  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. Serial.print("USBHub claim_device this=");
  35. Serial.println((uint32_t)this, HEX);
  36. // check for HUB type
  37. if (dev->bDeviceClass != 9 || dev->bDeviceSubClass != 0) return false;
  38. // protocol must be 0=FS, 1=HS Single-TT, or 2=HS Multi-TT
  39. if (dev->bDeviceProtocol > 2) return false;
  40. // check for endpoint descriptor
  41. if (descriptors[9] != 7 || descriptors[10] != 5) return false;
  42. // endpoint must be IN direction
  43. if ((descriptors[11] & 0xF0) != 0x80) return false;
  44. // endpoint type must be interrupt
  45. if (descriptors[12] != 3) return false;
  46. // get the endpoint number, must not be zero
  47. endpoint = descriptors[11] & 0x0F;
  48. if (endpoint == 0) return false;
  49. // get the maximum packet size
  50. uint32_t maxsize = descriptors[13] | (descriptors[14] << 8);
  51. if (maxsize == 0) return false;
  52. if (maxsize > 1) return false; // do hub chips with > 7 ports exist?
  53. Serial.println(descriptors[9]);
  54. Serial.println(descriptors[10]);
  55. Serial.println(descriptors[11], HEX);
  56. Serial.println(maxsize);
  57. // bDeviceProtocol = 0 is full speed
  58. // bDeviceProtocol = 1 is high speed single TT
  59. // bDeviceProtocol = 2 is high speed multiple TT
  60. Serial.print("bDeviceClass = ");
  61. Serial.println(dev->bDeviceClass);
  62. Serial.print("bDeviceSubClass = ");
  63. Serial.println(dev->bDeviceSubClass);
  64. Serial.print("bDeviceProtocol = ");
  65. Serial.println(dev->bDeviceProtocol);
  66. changepipe = NULL;
  67. changebits = 0;
  68. state = 0;
  69. memset(portstatus, 0, sizeof(portstatus));
  70. memset(portstate, 0, sizeof(portstate));
  71. mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
  72. queue_Control_Transfer(dev, &setup, hub_desc, this);
  73. return true;
  74. }
  75. void USBHub::poweron(uint32_t port)
  76. {
  77. mk_setup(setup, 0x23, 3, 8, port, 0);
  78. queue_Control_Transfer(device, &setup, NULL, this);
  79. }
  80. void USBHub::getstatus(uint32_t port)
  81. {
  82. if (port == 0) {
  83. mk_setup(setup, 0xA0, 0, 0, port, 4); // get hub status
  84. } else {
  85. mk_setup(setup, 0xA3, 0, 0, port, 4); // get port status
  86. }
  87. queue_Control_Transfer(device, &setup, &statusbits, this);
  88. }
  89. void USBHub::clearstatus(uint32_t port)
  90. {
  91. if (port == 0) {
  92. mk_setup(setup, 0x20, 1, 0x10, port, 0); // clear hub status
  93. } else {
  94. mk_setup(setup, 0x23, 1, 0x10, port, 0); // clear port status
  95. }
  96. queue_Control_Transfer(device, &setup, NULL, this);
  97. }
  98. void USBHub::reset(uint32_t port)
  99. {
  100. mk_setup(setup, 0x23, 3, 4, port, 0); // set feature PORT_RESET
  101. queue_Control_Transfer(device, &setup, NULL, this);
  102. }
  103. void USBHub::control(const Transfer_t *transfer)
  104. {
  105. Serial.println("USBHub control callback");
  106. print_hexbytes(transfer->buffer, transfer->length);
  107. if (state == 0) {
  108. // read hub descriptor to learn hub's capabilities
  109. // Hub Descriptor, USB 2.0, 11.23.2.1 page 417
  110. if (hub_desc[0] == 9 && hub_desc[1] == 0x29) {
  111. numports = hub_desc[2];
  112. characteristics = hub_desc[3];
  113. powertime = hub_desc[5];
  114. // TODO: do we need to use the DeviceRemovable
  115. // bits to mke synthetic device connect events?
  116. Serial.print("Hub has ");
  117. Serial.print(numports);
  118. Serial.println(" ports");
  119. state = 1;
  120. poweron(1);
  121. }
  122. } else if (state < numports) {
  123. // turn on power to all ports
  124. poweron(++state);
  125. } else if (state == numports) {
  126. Serial.println("power turned on to all ports");
  127. Serial.print("device addr = ");
  128. Serial.println(device->address);
  129. changepipe = new_Pipe(device, 3, endpoint, 1, 1, 512);
  130. Serial.print("pipe cap1 = ");
  131. Serial.println(changepipe->qh.capabilities[0], HEX);
  132. changepipe->callback_function = callback;
  133. queue_Data_Transfer(changepipe, &changebits, 1, this);
  134. state = 255;
  135. } else if (state == 255) {
  136. // up and running...
  137. switch (setup.word1) {
  138. case 0x000000A0: // get hub status
  139. Serial.println("New Hub Status");
  140. clearstatus(0);
  141. return;
  142. case 0x000000A3: // get port status
  143. new_port_status(setup.wIndex, statusbits);
  144. clearstatus(setup.wIndex);
  145. return;
  146. case 0x00100120: // clear hub status
  147. Serial.println("Hub Status Cleared");
  148. changebits &= ~1;
  149. break;
  150. case 0x00100123: // clear port status
  151. Serial.print("Port Status Cleared, port=");
  152. Serial.println(setup.wIndex);
  153. changebits &= ~(1 << setup.wIndex);
  154. break;
  155. }
  156. update_status();
  157. }
  158. }
  159. void USBHub::callback(const Transfer_t *transfer)
  160. {
  161. Serial.println("HUB Callback (static)");
  162. if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
  163. }
  164. void USBHub::status_change(const Transfer_t *transfer)
  165. {
  166. Serial.println("HUB Callback (member)");
  167. Serial.print("status = ");
  168. Serial.println(changebits, HEX);
  169. // TODO: do something with the status change info
  170. update_status();
  171. queue_Data_Transfer(changepipe, &changebits, 1, this);
  172. }
  173. void USBHub::update_status()
  174. {
  175. uint32_t i, mask;
  176. for (i=0, mask=1; i <= numports; i++, mask <<= 1) {
  177. if (changebits & mask) {
  178. getstatus(i);
  179. return;
  180. }
  181. }
  182. }
  183. void USBHub::new_port_status(uint32_t port, uint32_t status)
  184. {
  185. if (port < 1 || port > 7) return;
  186. uint32_t priorstatus = portstatus[port - 1];
  187. portstatus[port] = status;
  188. Serial.print("New Port Status, port=");
  189. Serial.print(port);
  190. Serial.print(", status=");
  191. Serial.println(status, HEX);
  192. // status bits, USB 2.0: 11.24.2.7.1 page 427
  193. if (status & 0x0001) Serial.println(" Device is present: ");
  194. if (status & 0x0002) {
  195. Serial.println(" Enabled, speed = ");
  196. if (status & 0x0200) {
  197. Serial.print("1.5");
  198. } else {
  199. if (status & 0x0400) {
  200. Serial.print("480");
  201. } else {
  202. Serial.print("12");
  203. }
  204. }
  205. Serial.println(" Mbit/sec");
  206. }
  207. if (status & 0x0004) Serial.println(" Suspended");
  208. if (status & 0x0008) Serial.println(" Over-current");
  209. if (status & 0x0010) Serial.println(" Reset");
  210. if (status & 0x0100) Serial.println(" Has Power");
  211. if (status & 0x0800) Serial.println(" Test Mode");
  212. if (status & 0x1000) Serial.println(" Software Controls LEDs");
  213. if ((status & 0x0001) && !(priorstatus & 0x0001)) {
  214. Serial.println(" connect");
  215. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  216. delay(100); // TODO: horribly bad... need timing events
  217. reset(port);
  218. // TODO... reset timer?
  219. } else if (!(status & 0x0001) && (priorstatus & 0x0001)) {
  220. Serial.println(" disconnect");
  221. }
  222. }
  223. void USBHub::disconnect()
  224. {
  225. // TODO: free resources
  226. }
  227. /*
  228. config descriptor from a Multi-TT hub
  229. 09 02 29 00 01 01 00 E0 32
  230. 09 04 00 00 01 09 00 01 00
  231. 07 05 81 03 01 00 0C
  232. 09 04 00 01 01 09 00 02 00
  233. 07 05 81 03 01 00 0C
  234. */