test

Dependencies:   mbed-rtos

Dependents:   Production_version1_0 Production_ver1_0 USBHost_TEST USBHost_Test5 ... more

Fork of USBHost by mbed official

Committer:
Kojto
Date:
Thu Jul 20 10:13:56 2017 +0100
Revision:
37:f1e388e7b752
Parent:
27:4206883f4cb7
Update libraries (ed9d1da)

Who changed what in which revision?

UserRevisionLine numberNew 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
Kojto 37:f1e388e7b752 53 USBHostHub::USBHostHub()
Kojto 37:f1e388e7b752 54 {
mbed_official 0:a554658735bf 55 host = NULL;
mbed_official 0:a554658735bf 56 init();
mbed_official 0:a554658735bf 57 }
mbed_official 0:a554658735bf 58
Kojto 37:f1e388e7b752 59 void USBHostHub::init()
Kojto 37:f1e388e7b752 60 {
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 24:868cbfe611a7 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
Kojto 37:f1e388e7b752 75 void USBHostHub::setHost(USBHost * host_)
Kojto 37:f1e388e7b752 76 {
mbed_official 0:a554658735bf 77 host = host_;
mbed_official 0:a554658735bf 78 }
mbed_official 0:a554658735bf 79
mbed_official 0:a554658735bf 80 bool USBHostHub::connected()
mbed_official 0:a554658735bf 81 {
mbed_official 0:a554658735bf 82 return dev_connected;
mbed_official 0:a554658735bf 83 }
mbed_official 0:a554658735bf 84
mbed_official 0:a554658735bf 85 bool USBHostHub::connect(USBDeviceConnected * dev)
mbed_official 24:868cbfe611a7 86 {
mbed_official 0:a554658735bf 87 if (dev_connected) {
mbed_official 0:a554658735bf 88 return true;
mbed_official 0:a554658735bf 89 }
mbed_official 24:868cbfe611a7 90
mbed_official 0:a554658735bf 91 if(host->enumerate(dev, this)) {
mbed_official 0:a554658735bf 92 init();
mbed_official 0:a554658735bf 93 return false;
mbed_official 0:a554658735bf 94 }
mbed_official 24:868cbfe611a7 95
mbed_official 0:a554658735bf 96 if (hub_device_found) {
mbed_official 0:a554658735bf 97 this->dev = dev;
mbed_official 24:868cbfe611a7 98
mbed_official 0:a554658735bf 99 int_in = dev->getEndpoint(hub_intf, INTERRUPT_ENDPOINT, IN);
mbed_official 24:868cbfe611a7 100
mbed_official 0:a554658735bf 101 if (!int_in) {
mbed_official 0:a554658735bf 102 init();
mbed_official 0:a554658735bf 103 return false;
mbed_official 0:a554658735bf 104 }
mbed_official 24:868cbfe611a7 105
samux 4:b320d68e98e7 106 USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, hub_intf);
samux 4:b320d68e98e7 107 dev->setName("Hub", hub_intf);
mbed_official 0:a554658735bf 108 host->registerDriver(dev, hub_intf, this, &USBHostHub::disconnect);
mbed_official 24:868cbfe611a7 109
mbed_official 0:a554658735bf 110 int_in->attach(this, &USBHostHub::rxHandler);
mbed_official 24:868cbfe611a7 111
mbed_official 0:a554658735bf 112 // get HUB descriptor
mbed_official 24:868cbfe611a7 113 host->controlRead( dev,
mbed_official 0:a554658735bf 114 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
mbed_official 0:a554658735bf 115 GET_DESCRIPTOR,
mbed_official 0:a554658735bf 116 0x29 << 8, 0, buf, sizeof(HubDescriptor));
mbed_official 0:a554658735bf 117 nb_port = buf[2];
mbed_official 0:a554658735bf 118 hub_characteristics = buf[3];
mbed_official 24:868cbfe611a7 119
mbed_official 0:a554658735bf 120 USB_DBG("Hub has %d port", nb_port);
mbed_official 24:868cbfe611a7 121
mbed_official 0:a554658735bf 122 for (uint8_t j = 1; j <= nb_port; j++) {
mbed_official 0:a554658735bf 123 setPortFeature(PORT_POWER_FEATURE, j);
mbed_official 0:a554658735bf 124 }
mbed_official 0:a554658735bf 125 wait_ms(buf[5]*2);
mbed_official 24:868cbfe611a7 126
mbed_official 0:a554658735bf 127 host->interruptRead(dev, int_in, buf, 1, false);
mbed_official 0:a554658735bf 128 dev_connected = true;
mbed_official 0:a554658735bf 129 return true;
mbed_official 0:a554658735bf 130 }
mbed_official 24:868cbfe611a7 131
mbed_official 0:a554658735bf 132 return false;
mbed_official 0:a554658735bf 133 }
mbed_official 0:a554658735bf 134
Kojto 37:f1e388e7b752 135 void USBHostHub::disconnect()
Kojto 37:f1e388e7b752 136 {
mbed_official 0:a554658735bf 137 init();
mbed_official 0:a554658735bf 138 }
mbed_official 0:a554658735bf 139
mbed_official 0:a554658735bf 140 /*virtual*/ void USBHostHub::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 141 {
mbed_official 0:a554658735bf 142 // we don't check VID/PID for MSD driver
mbed_official 0:a554658735bf 143 }
mbed_official 0:a554658735bf 144
mbed_official 0:a554658735bf 145 /*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 146 {
mbed_official 0:a554658735bf 147 if ((hub_intf == -1) &&
Kojto 37:f1e388e7b752 148 (intf_class == HUB_CLASS) &&
Kojto 37:f1e388e7b752 149 (intf_subclass == 0) &&
Kojto 37:f1e388e7b752 150 (intf_protocol == 0)) {
mbed_official 0:a554658735bf 151 hub_intf = intf_nb;
mbed_official 0:a554658735bf 152 return true;
mbed_official 0:a554658735bf 153 }
mbed_official 0:a554658735bf 154 return false;
mbed_official 0:a554658735bf 155 }
mbed_official 0:a554658735bf 156
mbed_official 0:a554658735bf 157 /*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 158 {
mbed_official 0:a554658735bf 159 if (intf_nb == hub_intf) {
mbed_official 0:a554658735bf 160 if ((type == INTERRUPT_ENDPOINT) && (dir == IN)) {
mbed_official 0:a554658735bf 161 hub_device_found = true;
mbed_official 0:a554658735bf 162 return true;
mbed_official 0:a554658735bf 163 }
mbed_official 0:a554658735bf 164 }
mbed_official 0:a554658735bf 165 return false;
mbed_official 0:a554658735bf 166 }
mbed_official 0:a554658735bf 167
Kojto 37:f1e388e7b752 168 void USBHostHub::deviceConnected(USBDeviceConnected * dev)
Kojto 37:f1e388e7b752 169 {
mbed_official 0:a554658735bf 170 device_children[dev->getPort() - 1] = dev;
mbed_official 0:a554658735bf 171 }
mbed_official 0:a554658735bf 172
Kojto 37:f1e388e7b752 173 void USBHostHub::deviceDisconnected(USBDeviceConnected * dev)
Kojto 37:f1e388e7b752 174 {
mbed_official 0:a554658735bf 175 device_children[dev->getPort() - 1] = NULL;
mbed_official 0:a554658735bf 176 }
mbed_official 0:a554658735bf 177
Kojto 37:f1e388e7b752 178 void USBHostHub::hubDisconnected()
Kojto 37:f1e388e7b752 179 {
mbed_official 0:a554658735bf 180 for (uint8_t i = 0; i < MAX_HUB_PORT; i++) {
mbed_official 0:a554658735bf 181 if (device_children[i] != NULL) {
mbed_official 0:a554658735bf 182 host->freeDevice(device_children[i]);
mbed_official 0:a554658735bf 183 }
mbed_official 0:a554658735bf 184 }
mbed_official 0:a554658735bf 185 }
mbed_official 0:a554658735bf 186
Kojto 37:f1e388e7b752 187 void USBHostHub::rxHandler()
Kojto 37:f1e388e7b752 188 {
mbed_official 0:a554658735bf 189 uint32_t status;
mbed_official 0:a554658735bf 190 if (int_in) {
Kojto 37:f1e388e7b752 191 if ((int_in->getLengthTransferred())&&(int_in->getState() == USB_TYPE_IDLE)) {
mbed_official 0:a554658735bf 192 for (int port = 1; port <= nb_port; port++) {
mbed_official 0:a554658735bf 193 status = getPortStatus(port);
mbed_official 0:a554658735bf 194 USB_DBG("[hub handler hub: %d] status port %d [hub: %p]: 0x%X", dev->getHub(), port, dev, status);
mbed_official 24:868cbfe611a7 195
mbed_official 0:a554658735bf 196 // if connection status has changed
mbed_official 0:a554658735bf 197 if (status & C_PORT_CONNECTION) {
mbed_official 0:a554658735bf 198 if (status & PORT_CONNECTION) {
mbed_official 0:a554658735bf 199 USB_DBG("[hub handler hub: %d - port: %d] new device connected", dev->getHub(), port);
mbed_official 0:a554658735bf 200 host->deviceConnected(dev->getHub() + 1, port, status & PORT_LOW_SPEED, this);
mbed_official 0:a554658735bf 201 } else {
mbed_official 0:a554658735bf 202 USB_DBG("[hub handler hub: %d - port: %d] device disconnected", dev->getHub(), port);
mbed_official 13:b58a2204422f 203 host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
mbed_official 0:a554658735bf 204 }
mbed_official 24:868cbfe611a7 205
mbed_official 0:a554658735bf 206 clearPortFeature(C_PORT_CONNECTION_FEATURE, port);
mbed_official 0:a554658735bf 207 }
mbed_official 24:868cbfe611a7 208
mbed_official 0:a554658735bf 209 if (status & C_PORT_RESET) {
mbed_official 0:a554658735bf 210 clearPortFeature(C_PORT_RESET_FEATURE, port);
mbed_official 0:a554658735bf 211 }
mbed_official 24:868cbfe611a7 212
mbed_official 0:a554658735bf 213 if (status & C_PORT_ENABLE) {
mbed_official 0:a554658735bf 214 clearPortFeature(C_PORT_ENABLE_FEATURE, port);
mbed_official 0:a554658735bf 215 }
mbed_official 24:868cbfe611a7 216
mbed_official 0:a554658735bf 217 if ((status & PORT_OVER_CURRENT)) {
mbed_official 0:a554658735bf 218 USB_ERR("OVER CURRENT DETECTED\r\n");
mbed_official 0:a554658735bf 219 clearPortFeature(PORT_OVER_CURRENT, port);
mbed_official 13:b58a2204422f 220 host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
mbed_official 0:a554658735bf 221 }
mbed_official 0:a554658735bf 222 }
mbed_official 0:a554658735bf 223 }
mbed_official 0:a554658735bf 224 host->interruptRead(dev, int_in, buf, 1, false);
mbed_official 0:a554658735bf 225 }
mbed_official 0:a554658735bf 226 }
mbed_official 0:a554658735bf 227
Kojto 37:f1e388e7b752 228 void USBHostHub::portReset(uint8_t port)
Kojto 37:f1e388e7b752 229 {
mbed_official 0:a554658735bf 230 // reset port
mbed_official 0:a554658735bf 231 uint32_t status;
mbed_official 0:a554658735bf 232 USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this)
mbed_official 0:a554658735bf 233 setPortFeature(PORT_RESET_FEATURE, port);
mbed_official 27:4206883f4cb7 234 #if defined(TARGET_RZ_A1H)
mbed_official 27:4206883f4cb7 235 Thread::wait(50); // Reset release waiting for Hi-Speed check.
mbed_official 27:4206883f4cb7 236 #endif
mbed_official 0:a554658735bf 237 while(1) {
mbed_official 0:a554658735bf 238 status = getPortStatus(port);
Kojto 37:f1e388e7b752 239 /* disconnection since reset request */
Kojto 37:f1e388e7b752 240 if (!(status & PORT_CONNECTION)) {
mbed_official 0:a554658735bf 241 break;
Kojto 37:f1e388e7b752 242 }
Kojto 37:f1e388e7b752 243 if (status & (PORT_ENABLE | PORT_RESET)) {
Kojto 37:f1e388e7b752 244 break;
Kojto 37:f1e388e7b752 245 }
mbed_official 0:a554658735bf 246 if (status & PORT_OVER_CURRENT) {
mbed_official 0:a554658735bf 247 USB_ERR("OVER CURRENT DETECTED\r\n");
mbed_official 0:a554658735bf 248 clearPortFeature(PORT_OVER_CURRENT, port);
mbed_official 13:b58a2204422f 249 host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
mbed_official 0:a554658735bf 250 break;
mbed_official 0:a554658735bf 251 }
mbed_official 0:a554658735bf 252 Thread::wait(10);
mbed_official 0:a554658735bf 253 }
mbed_official 0:a554658735bf 254 }
mbed_official 0:a554658735bf 255
Kojto 37:f1e388e7b752 256 void USBHostHub::setPortFeature(uint32_t feature, uint8_t port)
Kojto 37:f1e388e7b752 257 {
mbed_official 0:a554658735bf 258 host->controlWrite( dev,
mbed_official 0:a554658735bf 259 USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
mbed_official 0:a554658735bf 260 SET_FEATURE,
mbed_official 0:a554658735bf 261 feature,
mbed_official 0:a554658735bf 262 port,
mbed_official 0:a554658735bf 263 NULL,
mbed_official 0:a554658735bf 264 0);
mbed_official 0:a554658735bf 265 }
mbed_official 0:a554658735bf 266
Kojto 37:f1e388e7b752 267 void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port)
Kojto 37:f1e388e7b752 268 {
mbed_official 0:a554658735bf 269 host->controlWrite( dev,
mbed_official 0:a554658735bf 270 USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
mbed_official 0:a554658735bf 271 CLEAR_FEATURE,
mbed_official 0:a554658735bf 272 feature,
mbed_official 0:a554658735bf 273 port,
mbed_official 0:a554658735bf 274 NULL,
mbed_official 0:a554658735bf 275 0);
mbed_official 0:a554658735bf 276 }
mbed_official 0:a554658735bf 277
Kojto 37:f1e388e7b752 278 uint32_t USBHostHub::getPortStatus(uint8_t port)
Kojto 37:f1e388e7b752 279 {
mbed_official 0:a554658735bf 280 uint32_t st;
mbed_official 0:a554658735bf 281 host->controlRead( dev,
mbed_official 0:a554658735bf 282 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
mbed_official 0:a554658735bf 283 GET_STATUS,
mbed_official 0:a554658735bf 284 0,
mbed_official 0:a554658735bf 285 port,
mbed_official 0:a554658735bf 286 (uint8_t *)&st,
mbed_official 0:a554658735bf 287 4);
mbed_official 0:a554658735bf 288 return st;
mbed_official 0:a554658735bf 289 }
mbed_official 0:a554658735bf 290
mbed_official 0:a554658735bf 291 #endif