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.

138 line
4.4KB

  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. // TODO: need a way to do control transfers with our own setup data.
  70. mk_setup(dev->setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
  71. new_Control_Transfer(dev, &dev->setup, hub_desc);
  72. return true;
  73. }
  74. void USBHub::poweron(uint32_t port)
  75. {
  76. // TODO: need a way to do control transfers with our own setup data.
  77. mk_setup(device->setup, 0x23, 3, 8, port, 0);
  78. new_Control_Transfer(device, &device->setup, NULL);
  79. }
  80. bool USBHub::control(const Transfer_t *transfer)
  81. {
  82. Serial.println("USBHub control callback");
  83. print_hexbytes(transfer->buffer, transfer->length);
  84. if (state == 0) {
  85. // read hub descriptor to learn hub's capabilities
  86. if (transfer->buffer != hub_desc) return false;
  87. // Hub Descriptor, USB 2.0, 11.23.2.1 page 417
  88. if (hub_desc[0] == 9 && hub_desc[1] == 0x29) {
  89. numports = hub_desc[2];
  90. characteristics = hub_desc[3];
  91. powertime = hub_desc[5];
  92. // TODO: do we need to use the DeviceRemovable
  93. // bits to mke synthetic device connect events?
  94. Serial.print("Hub has ");
  95. Serial.print(numports);
  96. Serial.println(" ports");
  97. state = 1;
  98. poweron(1);
  99. }
  100. } else if (state < numports) {
  101. // turn on power to all ports
  102. poweron(++state);
  103. } else if (state == numports) {
  104. Serial.println("power turned on to all ports");
  105. // TODO: create interrupt pipe for status change notifications
  106. changepipe = new_Pipe(device, 3, endpoint, 1, 1);
  107. state = 255;
  108. } else if (state == 255) {
  109. // parse a status response
  110. }
  111. return true;
  112. }
  113. /*
  114. config descriptor from a Multi-TT hub
  115. 09 02 29 00 01 01 00 E0 32
  116. 09 04 00 00 01 09 00 01 00
  117. 07 05 81 03 01 00 0C
  118. 09 04 00 01 01 09 00 02 00
  119. 07 05 81 03 01 00 0C
  120. */