nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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