2018.07.26
Dependencies: FATFileSystem3 mbed-rtos
Fork of USBHost by
USBHostHub/USBHostHub.cpp@4:b320d68e98e7, 2013-03-12 (annotated)
- Committer:
- samux
- Date:
- Tue Mar 12 17:23:37 2013 +0000
- Revision:
- 4:b320d68e98e7
- Parent:
- 0:a554658735bf
- Child:
- 8:93da8ea2708b
bug fixes - support composite device
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:a554658735bf | 1 | /* Copyright (c) 2010-2012 mbed.org, MIT License |
mbed_official | 0:a554658735bf | 2 | * |
mbed_official | 0:a554658735bf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 0:a554658735bf | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed_official | 0:a554658735bf | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed_official | 0:a554658735bf | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 0:a554658735bf | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 0:a554658735bf | 8 | * |
mbed_official | 0:a554658735bf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 0:a554658735bf | 10 | * substantial portions of the Software. |
mbed_official | 0:a554658735bf | 11 | * |
mbed_official | 0:a554658735bf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 0:a554658735bf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 0:a554658735bf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 0:a554658735bf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:a554658735bf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 0:a554658735bf | 17 | */ |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #include "USBHostHub.h" |
mbed_official | 0:a554658735bf | 20 | |
mbed_official | 0:a554658735bf | 21 | #if MAX_HUB_NB |
mbed_official | 0:a554658735bf | 22 | |
mbed_official | 0:a554658735bf | 23 | #include "USBHost.h" |
mbed_official | 0:a554658735bf | 24 | #include "dbg.h" |
mbed_official | 0:a554658735bf | 25 | |
mbed_official | 0:a554658735bf | 26 | #define GET_STATUS 0x00 |
mbed_official | 0:a554658735bf | 27 | #define CLEAR_FEATURE 0x01 |
mbed_official | 0:a554658735bf | 28 | #define GET_STATE 0x02 |
mbed_official | 0:a554658735bf | 29 | #define SET_FEATURE 0x03 |
mbed_official | 0:a554658735bf | 30 | #define GET_DESCRIPTOR 0x06 |
mbed_official | 0:a554658735bf | 31 | |
mbed_official | 0:a554658735bf | 32 | #define PORT_CONNECTION_FEATURE (0x00) |
mbed_official | 0:a554658735bf | 33 | #define PORT_ENABLE_FEATURE (0x01) |
mbed_official | 0:a554658735bf | 34 | #define PORT_RESET_FEATURE (0x04) |
mbed_official | 0:a554658735bf | 35 | #define PORT_POWER_FEATURE (0x08) |
mbed_official | 0:a554658735bf | 36 | |
mbed_official | 0:a554658735bf | 37 | #define C_PORT_CONNECTION_FEATURE (16) |
mbed_official | 0:a554658735bf | 38 | #define C_PORT_ENABLE_FEATURE (17) |
mbed_official | 0:a554658735bf | 39 | #define C_PORT_RESET_FEATURE (20) |
mbed_official | 0:a554658735bf | 40 | |
mbed_official | 0:a554658735bf | 41 | #define PORT_CONNECTION (1 << 0) |
mbed_official | 0:a554658735bf | 42 | #define PORT_ENABLE (1 << 1) |
mbed_official | 0:a554658735bf | 43 | #define PORT_SUSPEND (1 << 2) |
mbed_official | 0:a554658735bf | 44 | #define PORT_OVER_CURRENT (1 << 3) |
mbed_official | 0:a554658735bf | 45 | #define PORT_RESET (1 << 4) |
mbed_official | 0:a554658735bf | 46 | #define PORT_POWER (1 << 8) |
mbed_official | 0:a554658735bf | 47 | #define PORT_LOW_SPEED (1 << 9) |
mbed_official | 0:a554658735bf | 48 | |
mbed_official | 0:a554658735bf | 49 | #define C_PORT_CONNECTION (1 << 16) |
mbed_official | 0:a554658735bf | 50 | #define C_PORT_ENABLE (1 << 17) |
mbed_official | 0:a554658735bf | 51 | #define C_PORT_SUSPEND (1 << 18) |
mbed_official | 0:a554658735bf | 52 | #define C_PORT_OVER_CURRENT (1 << 19) |
mbed_official | 0:a554658735bf | 53 | #define C_PORT_RESET (1 << 20) |
mbed_official | 0:a554658735bf | 54 | |
mbed_official | 0:a554658735bf | 55 | USBHostHub::USBHostHub() { |
mbed_official | 0:a554658735bf | 56 | host = NULL; |
mbed_official | 0:a554658735bf | 57 | init(); |
mbed_official | 0:a554658735bf | 58 | } |
mbed_official | 0:a554658735bf | 59 | |
mbed_official | 0:a554658735bf | 60 | void USBHostHub::init() { |
mbed_official | 0:a554658735bf | 61 | dev_connected = false; |
mbed_official | 0:a554658735bf | 62 | dev = NULL; |
mbed_official | 0:a554658735bf | 63 | int_in = NULL; |
mbed_official | 0:a554658735bf | 64 | dev_connected = false; |
mbed_official | 0:a554658735bf | 65 | hub_intf = -1; |
mbed_official | 0:a554658735bf | 66 | hub_device_found = false; |
mbed_official | 0:a554658735bf | 67 | nb_port = 0; |
mbed_official | 0:a554658735bf | 68 | hub_characteristics = 0; |
mbed_official | 0:a554658735bf | 69 | |
mbed_official | 0:a554658735bf | 70 | for (int i = 0; i < MAX_HUB_PORT; i++) { |
mbed_official | 0:a554658735bf | 71 | device_children[i] = NULL; |
mbed_official | 0:a554658735bf | 72 | } |
mbed_official | 0:a554658735bf | 73 | } |
mbed_official | 0:a554658735bf | 74 | |
mbed_official | 0:a554658735bf | 75 | void USBHostHub::setHost(USBHost * host_) { |
mbed_official | 0:a554658735bf | 76 | host = host_; |
mbed_official | 0:a554658735bf | 77 | } |
mbed_official | 0:a554658735bf | 78 | |
mbed_official | 0:a554658735bf | 79 | bool USBHostHub::connected() |
mbed_official | 0:a554658735bf | 80 | { |
mbed_official | 0:a554658735bf | 81 | return dev_connected; |
mbed_official | 0:a554658735bf | 82 | } |
mbed_official | 0:a554658735bf | 83 | |
mbed_official | 0:a554658735bf | 84 | bool USBHostHub::connect(USBDeviceConnected * dev) |
mbed_official | 0:a554658735bf | 85 | { |
mbed_official | 0:a554658735bf | 86 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 87 | return true; |
mbed_official | 0:a554658735bf | 88 | } |
mbed_official | 0:a554658735bf | 89 | |
mbed_official | 0:a554658735bf | 90 | if(host->enumerate(dev, this)) { |
mbed_official | 0:a554658735bf | 91 | init(); |
mbed_official | 0:a554658735bf | 92 | return false; |
mbed_official | 0:a554658735bf | 93 | } |
mbed_official | 0:a554658735bf | 94 | |
mbed_official | 0:a554658735bf | 95 | if (hub_device_found) { |
mbed_official | 0:a554658735bf | 96 | this->dev = dev; |
mbed_official | 0:a554658735bf | 97 | |
mbed_official | 0:a554658735bf | 98 | int_in = dev->getEndpoint(hub_intf, INTERRUPT_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 99 | |
mbed_official | 0:a554658735bf | 100 | if (!int_in) { |
mbed_official | 0:a554658735bf | 101 | init(); |
mbed_official | 0:a554658735bf | 102 | return false; |
mbed_official | 0:a554658735bf | 103 | } |
mbed_official | 0:a554658735bf | 104 | |
samux | 4:b320d68e98e7 | 105 | USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, hub_intf); |
samux | 4:b320d68e98e7 | 106 | dev->setName("Hub", hub_intf); |
mbed_official | 0:a554658735bf | 107 | host->registerDriver(dev, hub_intf, this, &USBHostHub::disconnect); |
mbed_official | 0:a554658735bf | 108 | |
mbed_official | 0:a554658735bf | 109 | int_in->attach(this, &USBHostHub::rxHandler); |
mbed_official | 0:a554658735bf | 110 | |
mbed_official | 0:a554658735bf | 111 | // get HUB descriptor |
mbed_official | 0:a554658735bf | 112 | host->controlRead( dev, |
mbed_official | 0:a554658735bf | 113 | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
mbed_official | 0:a554658735bf | 114 | GET_DESCRIPTOR, |
mbed_official | 0:a554658735bf | 115 | 0x29 << 8, 0, buf, sizeof(HubDescriptor)); |
mbed_official | 0:a554658735bf | 116 | nb_port = buf[2]; |
mbed_official | 0:a554658735bf | 117 | hub_characteristics = buf[3]; |
mbed_official | 0:a554658735bf | 118 | |
mbed_official | 0:a554658735bf | 119 | USB_DBG("Hub has %d port", nb_port); |
mbed_official | 0:a554658735bf | 120 | |
mbed_official | 0:a554658735bf | 121 | for (uint8_t j = 1; j <= nb_port; j++) { |
mbed_official | 0:a554658735bf | 122 | setPortFeature(PORT_POWER_FEATURE, j); |
mbed_official | 0:a554658735bf | 123 | } |
mbed_official | 0:a554658735bf | 124 | wait_ms(buf[5]*2); |
mbed_official | 0:a554658735bf | 125 | |
mbed_official | 0:a554658735bf | 126 | host->interruptRead(dev, int_in, buf, 1, false); |
mbed_official | 0:a554658735bf | 127 | dev_connected = true; |
mbed_official | 0:a554658735bf | 128 | return true; |
mbed_official | 0:a554658735bf | 129 | } |
mbed_official | 0:a554658735bf | 130 | |
mbed_official | 0:a554658735bf | 131 | return false; |
mbed_official | 0:a554658735bf | 132 | } |
mbed_official | 0:a554658735bf | 133 | |
mbed_official | 0:a554658735bf | 134 | void USBHostHub::disconnect() { |
mbed_official | 0:a554658735bf | 135 | init(); |
mbed_official | 0:a554658735bf | 136 | } |
mbed_official | 0:a554658735bf | 137 | |
mbed_official | 0:a554658735bf | 138 | /*virtual*/ void USBHostHub::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 139 | { |
mbed_official | 0:a554658735bf | 140 | // we don't check VID/PID for MSD driver |
mbed_official | 0:a554658735bf | 141 | } |
mbed_official | 0:a554658735bf | 142 | |
mbed_official | 0:a554658735bf | 143 | /*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 |
mbed_official | 0:a554658735bf | 144 | { |
mbed_official | 0:a554658735bf | 145 | if ((hub_intf == -1) && |
mbed_official | 0:a554658735bf | 146 | (intf_class == HUB_CLASS) && |
mbed_official | 0:a554658735bf | 147 | (intf_subclass == 0) && |
mbed_official | 0:a554658735bf | 148 | (intf_protocol == 0)) { |
mbed_official | 0:a554658735bf | 149 | hub_intf = intf_nb; |
mbed_official | 0:a554658735bf | 150 | return true; |
mbed_official | 0:a554658735bf | 151 | } |
mbed_official | 0:a554658735bf | 152 | return false; |
mbed_official | 0:a554658735bf | 153 | } |
mbed_official | 0:a554658735bf | 154 | |
mbed_official | 0:a554658735bf | 155 | /*virtual*/ bool USBHostHub::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
mbed_official | 0:a554658735bf | 156 | { |
mbed_official | 0:a554658735bf | 157 | if (intf_nb == hub_intf) { |
mbed_official | 0:a554658735bf | 158 | if ((type == INTERRUPT_ENDPOINT) && (dir == IN)) { |
mbed_official | 0:a554658735bf | 159 | hub_device_found = true; |
mbed_official | 0:a554658735bf | 160 | return true; |
mbed_official | 0:a554658735bf | 161 | } |
mbed_official | 0:a554658735bf | 162 | } |
mbed_official | 0:a554658735bf | 163 | return false; |
mbed_official | 0:a554658735bf | 164 | } |
mbed_official | 0:a554658735bf | 165 | |
mbed_official | 0:a554658735bf | 166 | void USBHostHub::deviceConnected(USBDeviceConnected * dev) { |
mbed_official | 0:a554658735bf | 167 | device_children[dev->getPort() - 1] = dev; |
mbed_official | 0:a554658735bf | 168 | } |
mbed_official | 0:a554658735bf | 169 | |
mbed_official | 0:a554658735bf | 170 | void USBHostHub::deviceDisconnected(USBDeviceConnected * dev) { |
mbed_official | 0:a554658735bf | 171 | device_children[dev->getPort() - 1] = NULL; |
mbed_official | 0:a554658735bf | 172 | } |
mbed_official | 0:a554658735bf | 173 | |
mbed_official | 0:a554658735bf | 174 | void USBHostHub::hubDisconnected() { |
mbed_official | 0:a554658735bf | 175 | for (uint8_t i = 0; i < MAX_HUB_PORT; i++) { |
mbed_official | 0:a554658735bf | 176 | if (device_children[i] != NULL) { |
mbed_official | 0:a554658735bf | 177 | host->freeDevice(device_children[i]); |
mbed_official | 0:a554658735bf | 178 | } |
mbed_official | 0:a554658735bf | 179 | } |
mbed_official | 0:a554658735bf | 180 | } |
mbed_official | 0:a554658735bf | 181 | |
mbed_official | 0:a554658735bf | 182 | void USBHostHub::rxHandler() { |
mbed_official | 0:a554658735bf | 183 | uint32_t status; |
mbed_official | 0:a554658735bf | 184 | if (int_in) { |
mbed_official | 0:a554658735bf | 185 | if (int_in->getState() == USB_TYPE_IDLE) { |
mbed_official | 0:a554658735bf | 186 | for (int port = 1; port <= nb_port; port++) { |
mbed_official | 0:a554658735bf | 187 | status = getPortStatus(port); |
mbed_official | 0:a554658735bf | 188 | USB_DBG("[hub handler hub: %d] status port %d [hub: %p]: 0x%X", dev->getHub(), port, dev, status); |
mbed_official | 0:a554658735bf | 189 | |
mbed_official | 0:a554658735bf | 190 | // if connection status has changed |
mbed_official | 0:a554658735bf | 191 | if (status & C_PORT_CONNECTION) { |
mbed_official | 0:a554658735bf | 192 | if (status & PORT_CONNECTION) { |
mbed_official | 0:a554658735bf | 193 | USB_DBG("[hub handler hub: %d - port: %d] new device connected", dev->getHub(), port); |
mbed_official | 0:a554658735bf | 194 | host->deviceConnected(dev->getHub() + 1, port, status & PORT_LOW_SPEED, this); |
mbed_official | 0:a554658735bf | 195 | } else { |
mbed_official | 0:a554658735bf | 196 | USB_DBG("[hub handler hub: %d - port: %d] device disconnected", dev->getHub(), port); |
mbed_official | 0:a554658735bf | 197 | host->deviceDisconnected(dev->getHub() + 1, port, this, NULL); |
mbed_official | 0:a554658735bf | 198 | } |
mbed_official | 0:a554658735bf | 199 | |
mbed_official | 0:a554658735bf | 200 | clearPortFeature(C_PORT_CONNECTION_FEATURE, port); |
mbed_official | 0:a554658735bf | 201 | } |
mbed_official | 0:a554658735bf | 202 | |
mbed_official | 0:a554658735bf | 203 | if (status & C_PORT_RESET) { |
mbed_official | 0:a554658735bf | 204 | clearPortFeature(C_PORT_RESET_FEATURE, port); |
mbed_official | 0:a554658735bf | 205 | } |
mbed_official | 0:a554658735bf | 206 | |
mbed_official | 0:a554658735bf | 207 | if (status & C_PORT_ENABLE) { |
mbed_official | 0:a554658735bf | 208 | clearPortFeature(C_PORT_ENABLE_FEATURE, port); |
mbed_official | 0:a554658735bf | 209 | } |
mbed_official | 0:a554658735bf | 210 | |
mbed_official | 0:a554658735bf | 211 | if ((status & PORT_OVER_CURRENT)) { |
mbed_official | 0:a554658735bf | 212 | USB_ERR("OVER CURRENT DETECTED\r\n"); |
mbed_official | 0:a554658735bf | 213 | clearPortFeature(PORT_OVER_CURRENT, port); |
mbed_official | 0:a554658735bf | 214 | host->deviceDisconnected(dev->getHub() + 1, port, this, NULL); |
mbed_official | 0:a554658735bf | 215 | } |
mbed_official | 0:a554658735bf | 216 | } |
mbed_official | 0:a554658735bf | 217 | } |
mbed_official | 0:a554658735bf | 218 | host->interruptRead(dev, int_in, buf, 1, false); |
mbed_official | 0:a554658735bf | 219 | } |
mbed_official | 0:a554658735bf | 220 | } |
mbed_official | 0:a554658735bf | 221 | |
mbed_official | 0:a554658735bf | 222 | void USBHostHub::portReset(uint8_t port) { |
mbed_official | 0:a554658735bf | 223 | // reset port |
mbed_official | 0:a554658735bf | 224 | uint32_t status; |
mbed_official | 0:a554658735bf | 225 | USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this) |
mbed_official | 0:a554658735bf | 226 | setPortFeature(PORT_RESET_FEATURE, port); |
mbed_official | 0:a554658735bf | 227 | while(1) { |
mbed_official | 0:a554658735bf | 228 | status = getPortStatus(port); |
mbed_official | 0:a554658735bf | 229 | if (status & (PORT_ENABLE | PORT_RESET)) |
mbed_official | 0:a554658735bf | 230 | break; |
mbed_official | 0:a554658735bf | 231 | if (status & PORT_OVER_CURRENT) { |
mbed_official | 0:a554658735bf | 232 | USB_ERR("OVER CURRENT DETECTED\r\n"); |
mbed_official | 0:a554658735bf | 233 | clearPortFeature(PORT_OVER_CURRENT, port); |
mbed_official | 0:a554658735bf | 234 | host->deviceDisconnected(dev->getHub() + 1, port, this, NULL); |
mbed_official | 0:a554658735bf | 235 | break; |
mbed_official | 0:a554658735bf | 236 | } |
mbed_official | 0:a554658735bf | 237 | Thread::wait(10); |
mbed_official | 0:a554658735bf | 238 | } |
mbed_official | 0:a554658735bf | 239 | } |
mbed_official | 0:a554658735bf | 240 | |
mbed_official | 0:a554658735bf | 241 | void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) { |
mbed_official | 0:a554658735bf | 242 | host->controlWrite( dev, |
mbed_official | 0:a554658735bf | 243 | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, |
mbed_official | 0:a554658735bf | 244 | SET_FEATURE, |
mbed_official | 0:a554658735bf | 245 | feature, |
mbed_official | 0:a554658735bf | 246 | port, |
mbed_official | 0:a554658735bf | 247 | NULL, |
mbed_official | 0:a554658735bf | 248 | 0); |
mbed_official | 0:a554658735bf | 249 | } |
mbed_official | 0:a554658735bf | 250 | |
mbed_official | 0:a554658735bf | 251 | void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) { |
mbed_official | 0:a554658735bf | 252 | host->controlWrite( dev, |
mbed_official | 0:a554658735bf | 253 | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, |
mbed_official | 0:a554658735bf | 254 | CLEAR_FEATURE, |
mbed_official | 0:a554658735bf | 255 | feature, |
mbed_official | 0:a554658735bf | 256 | port, |
mbed_official | 0:a554658735bf | 257 | NULL, |
mbed_official | 0:a554658735bf | 258 | 0); |
mbed_official | 0:a554658735bf | 259 | } |
mbed_official | 0:a554658735bf | 260 | |
mbed_official | 0:a554658735bf | 261 | uint32_t USBHostHub::getPortStatus(uint8_t port) { |
mbed_official | 0:a554658735bf | 262 | uint32_t st; |
mbed_official | 0:a554658735bf | 263 | host->controlRead( dev, |
mbed_official | 0:a554658735bf | 264 | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, |
mbed_official | 0:a554658735bf | 265 | GET_STATUS, |
mbed_official | 0:a554658735bf | 266 | 0, |
mbed_official | 0:a554658735bf | 267 | port, |
mbed_official | 0:a554658735bf | 268 | (uint8_t *)&st, |
mbed_official | 0:a554658735bf | 269 | 4); |
mbed_official | 0:a554658735bf | 270 | return st; |
mbed_official | 0:a554658735bf | 271 | } |
mbed_official | 0:a554658735bf | 272 | |
mbed_official | 0:a554658735bf | 273 | #endif |