Keyboard firmwares for Atmel AVR and Cortex-M
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.

USBHostHub.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* mbed USBHost Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "USBHostHub.h"
  17. #if MAX_HUB_NB
  18. #include "USBHost.h"
  19. #include "dbg.h"
  20. #define GET_STATUS 0x00
  21. #define CLEAR_FEATURE 0x01
  22. #define GET_STATE 0x02
  23. #define SET_FEATURE 0x03
  24. #define GET_DESCRIPTOR 0x06
  25. #define PORT_CONNECTION_FEATURE (0x00)
  26. #define PORT_ENABLE_FEATURE (0x01)
  27. #define PORT_RESET_FEATURE (0x04)
  28. #define PORT_POWER_FEATURE (0x08)
  29. #define C_PORT_CONNECTION_FEATURE (16)
  30. #define C_PORT_ENABLE_FEATURE (17)
  31. #define C_PORT_RESET_FEATURE (20)
  32. #define PORT_CONNECTION (1 << 0)
  33. #define PORT_ENABLE (1 << 1)
  34. #define PORT_SUSPEND (1 << 2)
  35. #define PORT_OVER_CURRENT (1 << 3)
  36. #define PORT_RESET (1 << 4)
  37. #define PORT_POWER (1 << 8)
  38. #define PORT_LOW_SPEED (1 << 9)
  39. #define C_PORT_CONNECTION (1 << 16)
  40. #define C_PORT_ENABLE (1 << 17)
  41. #define C_PORT_SUSPEND (1 << 18)
  42. #define C_PORT_OVER_CURRENT (1 << 19)
  43. #define C_PORT_RESET (1 << 20)
  44. USBHostHub::USBHostHub() {
  45. host = NULL;
  46. init();
  47. }
  48. void USBHostHub::init() {
  49. dev_connected = false;
  50. dev = NULL;
  51. int_in = NULL;
  52. dev_connected = false;
  53. hub_intf = -1;
  54. hub_device_found = false;
  55. nb_port = 0;
  56. hub_characteristics = 0;
  57. for (int i = 0; i < MAX_HUB_PORT; i++) {
  58. device_children[i] = NULL;
  59. }
  60. }
  61. void USBHostHub::setHost(USBHost * host_) {
  62. host = host_;
  63. }
  64. bool USBHostHub::connected()
  65. {
  66. return dev_connected;
  67. }
  68. bool USBHostHub::connect(USBDeviceConnected * dev)
  69. {
  70. if (dev_connected) {
  71. return true;
  72. }
  73. if(host->enumerate(dev, this)) {
  74. init();
  75. return false;
  76. }
  77. if (hub_device_found) {
  78. this->dev = dev;
  79. int_in = dev->getEndpoint(hub_intf, INTERRUPT_ENDPOINT, IN);
  80. if (!int_in) {
  81. init();
  82. return false;
  83. }
  84. USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, hub_intf);
  85. dev->setName("Hub", hub_intf);
  86. host->registerDriver(dev, hub_intf, this, &USBHostHub::disconnect);
  87. int_in->attach(this, &USBHostHub::rxHandler);
  88. // get HUB descriptor
  89. host->controlRead( dev,
  90. USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
  91. GET_DESCRIPTOR,
  92. 0x29 << 8, 0, buf, sizeof(HubDescriptor));
  93. nb_port = buf[2];
  94. hub_characteristics = buf[3];
  95. USB_DBG("Hub has %d port", nb_port);
  96. for (uint8_t j = 1; j <= nb_port; j++) {
  97. setPortFeature(PORT_POWER_FEATURE, j);
  98. }
  99. wait_ms(buf[5]*2);
  100. host->interruptRead(dev, int_in, buf, 1, false);
  101. dev_connected = true;
  102. return true;
  103. }
  104. return false;
  105. }
  106. void USBHostHub::disconnect() {
  107. init();
  108. }
  109. /*virtual*/ void USBHostHub::setVidPid(uint16_t vid, uint16_t pid)
  110. {
  111. // we don't check VID/PID for MSD driver
  112. }
  113. /*virtual*/ bool USBHostHub::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
  114. {
  115. if ((hub_intf == -1) &&
  116. (intf_class == HUB_CLASS) &&
  117. (intf_subclass == 0) &&
  118. (intf_protocol == 0)) {
  119. hub_intf = intf_nb;
  120. return true;
  121. }
  122. return false;
  123. }
  124. /*virtual*/ bool USBHostHub::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
  125. {
  126. if (intf_nb == hub_intf) {
  127. if ((type == INTERRUPT_ENDPOINT) && (dir == IN)) {
  128. hub_device_found = true;
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. void USBHostHub::deviceConnected(USBDeviceConnected * dev) {
  135. device_children[dev->getPort() - 1] = dev;
  136. }
  137. void USBHostHub::deviceDisconnected(USBDeviceConnected * dev) {
  138. device_children[dev->getPort() - 1] = NULL;
  139. }
  140. void USBHostHub::hubDisconnected() {
  141. for (uint8_t i = 0; i < MAX_HUB_PORT; i++) {
  142. if (device_children[i] != NULL) {
  143. host->freeDevice(device_children[i]);
  144. }
  145. }
  146. }
  147. void USBHostHub::rxHandler() {
  148. uint32_t status;
  149. if (int_in) {
  150. if (int_in->getState() == USB_TYPE_IDLE) {
  151. for (int port = 1; port <= nb_port; port++) {
  152. status = getPortStatus(port);
  153. USB_DBG("[hub handler hub: %d] status port %d [hub: %p]: 0x%X", dev->getHub(), port, dev, status);
  154. // if connection status has changed
  155. if (status & C_PORT_CONNECTION) {
  156. if (status & PORT_CONNECTION) {
  157. USB_DBG("[hub handler hub: %d - port: %d] new device connected", dev->getHub(), port);
  158. host->deviceConnected(dev->getHub() + 1, port, status & PORT_LOW_SPEED, this);
  159. } else {
  160. USB_DBG("[hub handler hub: %d - port: %d] device disconnected", dev->getHub(), port);
  161. host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
  162. }
  163. clearPortFeature(C_PORT_CONNECTION_FEATURE, port);
  164. }
  165. if (status & C_PORT_RESET) {
  166. clearPortFeature(C_PORT_RESET_FEATURE, port);
  167. }
  168. if (status & C_PORT_ENABLE) {
  169. clearPortFeature(C_PORT_ENABLE_FEATURE, port);
  170. }
  171. if ((status & PORT_OVER_CURRENT)) {
  172. USB_ERR("OVER CURRENT DETECTED\r\n");
  173. clearPortFeature(PORT_OVER_CURRENT, port);
  174. host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
  175. }
  176. }
  177. }
  178. host->interruptRead(dev, int_in, buf, 1, false);
  179. }
  180. }
  181. void USBHostHub::portReset(uint8_t port) {
  182. // reset port
  183. uint32_t status;
  184. USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this)
  185. setPortFeature(PORT_RESET_FEATURE, port);
  186. #if defined(TARGET_RZ_A1H)
  187. Thread::wait(50); // Reset release waiting for Hi-Speed check.
  188. #endif
  189. while(1) {
  190. status = getPortStatus(port);
  191. if (status & (PORT_ENABLE | PORT_RESET))
  192. break;
  193. if (status & PORT_OVER_CURRENT) {
  194. USB_ERR("OVER CURRENT DETECTED\r\n");
  195. clearPortFeature(PORT_OVER_CURRENT, port);
  196. host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
  197. break;
  198. }
  199. Thread::wait(10);
  200. }
  201. }
  202. void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) {
  203. host->controlWrite( dev,
  204. USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
  205. SET_FEATURE,
  206. feature,
  207. port,
  208. NULL,
  209. 0);
  210. }
  211. void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) {
  212. host->controlWrite( dev,
  213. USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
  214. CLEAR_FEATURE,
  215. feature,
  216. port,
  217. NULL,
  218. 0);
  219. }
  220. uint32_t USBHostHub::getPortStatus(uint8_t port) {
  221. uint32_t st;
  222. host->controlRead( dev,
  223. USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
  224. GET_STATUS,
  225. 0,
  226. port,
  227. (uint8_t *)&st,
  228. 4);
  229. return st;
  230. }
  231. #endif