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