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:
Mon Nov 04 14:30:07 2019 +0000
Revision:
33:819bbf04163b
Parent:
32:f2d129436056
More updates related to mbed OS 5

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