2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 04:20:29 2018 +0000
Revision:
44:ef52682c423e
Parent:
43:1675750cca08
2018.07.26;

Who changed what in which revision?

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