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.

209 lines
6.2KB

  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)
  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. mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
  70. queue_Control_Transfer(dev, &setup, hub_desc, this);
  71. return true;
  72. }
  73. void USBHub::poweron(uint32_t port)
  74. {
  75. mk_setup(setup, 0x23, 3, 8, port, 0);
  76. queue_Control_Transfer(device, &setup, NULL, this);
  77. }
  78. void USBHub::getstatus(uint32_t port)
  79. {
  80. if (port == 0) {
  81. mk_setup(setup, 0xA0, 0, 0, port, 4); // get hub status
  82. } else {
  83. mk_setup(setup, 0xA3, 0, 0, port, 4); // get port status
  84. }
  85. queue_Control_Transfer(device, &setup, &status, this);
  86. }
  87. void USBHub::clearstatus(uint32_t port)
  88. {
  89. if (port == 0) {
  90. mk_setup(setup, 0x20, 1, 0x10, port, 0); // clear hub status
  91. } else {
  92. mk_setup(setup, 0x23, 1, 0x10, port, 0); // clear port status
  93. }
  94. queue_Control_Transfer(device, &setup, NULL, this);
  95. }
  96. bool USBHub::control(const Transfer_t *transfer)
  97. {
  98. Serial.println("USBHub control callback");
  99. print_hexbytes(transfer->buffer, transfer->length);
  100. if (state == 0) {
  101. // read hub descriptor to learn hub's capabilities
  102. if (transfer->buffer != hub_desc) return false;
  103. // Hub Descriptor, USB 2.0, 11.23.2.1 page 417
  104. if (hub_desc[0] == 9 && hub_desc[1] == 0x29) {
  105. numports = hub_desc[2];
  106. characteristics = hub_desc[3];
  107. powertime = hub_desc[5];
  108. // TODO: do we need to use the DeviceRemovable
  109. // bits to mke synthetic device connect events?
  110. Serial.print("Hub has ");
  111. Serial.print(numports);
  112. Serial.println(" ports");
  113. state = 1;
  114. poweron(1);
  115. }
  116. } else if (state < numports) {
  117. // turn on power to all ports
  118. poweron(++state);
  119. } else if (state == numports) {
  120. Serial.println("power turned on to all ports");
  121. Serial.print("device addr = ");
  122. Serial.println(device->address);
  123. changepipe = new_Pipe(device, 3, endpoint, 1, 1, 512);
  124. Serial.print("pipe cap1 = ");
  125. Serial.println(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. Serial.println("New Hub Status");
  134. clearstatus(0);
  135. return true;
  136. case 0x000000A3: // get port status
  137. Serial.print("New Port Status, port=");
  138. Serial.println(setup.wIndex);
  139. clearstatus(setup.wIndex);
  140. return true;
  141. case 0x00100120: // clear hub status
  142. Serial.println("Hub Status Cleared");
  143. changebits &= ~1;
  144. break;
  145. case 0x00100123: // clear port status
  146. Serial.print("Port Status Cleared, port=");
  147. Serial.println(setup.wIndex);
  148. changebits &= ~(1 << setup.wIndex);
  149. break;
  150. }
  151. update_status();
  152. }
  153. return true;
  154. }
  155. void USBHub::callback(const Transfer_t *transfer)
  156. {
  157. Serial.println("HUB Callback (static)");
  158. if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
  159. }
  160. void USBHub::status_change(const Transfer_t *transfer)
  161. {
  162. Serial.println("HUB Callback (member)");
  163. Serial.print("status = ");
  164. Serial.println(changebits, HEX);
  165. // TODO: do something with the status change info
  166. update_status();
  167. queue_Data_Transfer(changepipe, &changebits, 1, this);
  168. }
  169. void USBHub::update_status()
  170. {
  171. uint32_t i, mask;
  172. for (i=0, mask=1; i <= numports; i++, mask <<= 1) {
  173. if (changebits & mask) {
  174. getstatus(i);
  175. return;
  176. }
  177. }
  178. }
  179. /*
  180. config descriptor from a Multi-TT hub
  181. 09 02 29 00 01 01 00 E0 32
  182. 09 04 00 00 01 09 00 01 00
  183. 07 05 81 03 01 00 0C
  184. 09 04 00 01 01 09 00 02 00
  185. 07 05 81 03 01 00 0C
  186. */