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.

287 lines
8.0KB

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