mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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