Adaptation of the official mbed USBHost repository to work with the LPC4088 Display Module

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_USBHost by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Wed Dec 03 13:32:37 2014 +0000
Revision:
28:a55c5a98d5e9
Parent:
27:aa2fd412f1d3
Child:
31:9a462d032742
Disabled Serial, MIDI and 3G classes as they are unverified. Fixed the KeyBoard, Hub and Mouse classes.

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