Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

262 lines
7.3KB

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