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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostHub.cpp Source File

USBHostHub.cpp

00001 /* mbed USBHost Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "USBHostHub.h"
00018 
00019 #if MAX_HUB_NB
00020 
00021 #include "USBHost.h"
00022 #include "dbg.h"
00023 
00024 #define GET_STATUS 0x00
00025 #define CLEAR_FEATURE 0x01
00026 #define GET_STATE 0x02
00027 #define SET_FEATURE 0x03
00028 #define GET_DESCRIPTOR 0x06
00029 
00030 #define PORT_CONNECTION_FEATURE     (0x00)
00031 #define PORT_ENABLE_FEATURE         (0x01)
00032 #define PORT_RESET_FEATURE          (0x04)
00033 #define PORT_POWER_FEATURE          (0x08)
00034 
00035 #define C_PORT_CONNECTION_FEATURE     (16)
00036 #define C_PORT_ENABLE_FEATURE         (17)
00037 #define C_PORT_RESET_FEATURE          (20)
00038 
00039 #define PORT_CONNECTION   (1 << 0)
00040 #define PORT_ENABLE       (1 << 1)
00041 #define PORT_SUSPEND      (1 << 2)
00042 #define PORT_OVER_CURRENT (1 << 3)
00043 #define PORT_RESET        (1 << 4)
00044 #define PORT_POWER        (1 << 8)
00045 #define PORT_LOW_SPEED    (1 << 9)
00046 
00047 #define C_PORT_CONNECTION   (1 << 16)
00048 #define C_PORT_ENABLE       (1 << 17)
00049 #define C_PORT_SUSPEND      (1 << 18)
00050 #define C_PORT_OVER_CURRENT (1 << 19)
00051 #define C_PORT_RESET        (1 << 20)
00052 
00053 USBHostHub::USBHostHub() {
00054     host = NULL;
00055     init();
00056 }
00057 
00058 USBHostHub::~USBHostHub()
00059 {
00060     if (buf != NULL) {
00061         host->returnSafeMem(buf);
00062         buf = NULL;
00063     }
00064 }
00065 
00066 void USBHostHub::init() {
00067     dev_connected = false;
00068     dev = NULL;
00069     int_in = NULL;
00070     dev_connected = false;
00071     hub_intf = -1;
00072     hub_device_found = false;
00073     nb_port = 0;
00074     hub_characteristics = 0;
00075 
00076     for (int i = 0; i < MAX_HUB_PORT; i++) {
00077         device_children[i] = NULL;
00078     }
00079     
00080     if (host != NULL) {
00081         buf = host->getSafeMem(sizeof(HubDescriptor));
00082     }
00083 }
00084 
00085 void USBHostHub::setHost(USBHost * host_) {
00086     host = host_;
00087 }
00088 
00089 bool USBHostHub::connected()
00090 {
00091     return dev_connected;
00092 }
00093 
00094 bool USBHostHub::connect(USBDeviceConnected * dev)
00095 {
00096     if (dev_connected) {
00097         return true;
00098     }
00099 
00100     if(host->enumerate(dev, this)) {
00101         init();
00102         return false;
00103     }
00104 
00105     if (hub_device_found) {
00106         this->dev = dev;
00107 
00108         int_in = dev->getEndpoint(hub_intf, INTERRUPT_ENDPOINT, IN);
00109 
00110         if (!int_in) {
00111             init();
00112             return false;
00113         }
00114 
00115         USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, hub_intf);
00116         dev->setName("Hub", hub_intf);
00117         host->registerDriver(dev, hub_intf, this, &USBHostHub::disconnect);
00118 
00119         int_in->attach(this, &USBHostHub::rxHandler);
00120 
00121         // get HUB descriptor
00122         host->controlRead(  dev,
00123                             USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
00124                             GET_DESCRIPTOR,
00125                             0x29 << 8, 0, buf, sizeof(HubDescriptor));
00126         nb_port = buf[2];
00127         hub_characteristics = buf[3];
00128 
00129         USB_DBG("Hub has %d port", nb_port);
00130 
00131         for (uint8_t j = 1; j <= nb_port; j++) {
00132             setPortFeature(PORT_POWER_FEATURE, j);
00133         }
00134         ThisThread::sleep_for(buf[5]*2);
00135 
00136         host->interruptRead(dev, int_in, buf, 1, false);
00137         dev_connected = true;
00138         return true;
00139     }
00140 
00141     return false;
00142 }
00143 
00144 void USBHostHub::disconnect() {
00145     init();
00146 }
00147 
00148 /*virtual*/ void USBHostHub::setVidPid(uint16_t vid, uint16_t pid)
00149 {
00150     // we don't check VID/PID for MSD driver
00151 }
00152 
00153 /*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
00154 {
00155     if ((hub_intf == -1) &&
00156         (intf_class == HUB_CLASS) &&
00157         (intf_subclass == 0) &&
00158         (intf_protocol == 0)) {
00159         hub_intf = intf_nb;
00160         return true;
00161     }
00162     return false;
00163 }
00164 
00165 /*virtual*/ bool USBHostHub::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00166 {
00167     if (intf_nb == hub_intf) {
00168         if ((type == INTERRUPT_ENDPOINT) && (dir == IN)) {
00169             hub_device_found = true;
00170             return true;
00171         }
00172     }
00173     return false;
00174 }
00175 
00176 void USBHostHub::deviceConnected(USBDeviceConnected * dev) {
00177     device_children[dev->getPort() - 1] = dev;
00178 }
00179 
00180 void USBHostHub::deviceDisconnected(USBDeviceConnected * dev) {
00181     device_children[dev->getPort() - 1] = NULL;
00182 }
00183 
00184 void USBHostHub::hubDisconnected() {
00185     for (uint8_t i = 0; i < MAX_HUB_PORT; i++) {
00186         if (device_children[i] != NULL) {
00187             host->freeDevice(device_children[i]);
00188         }
00189     }
00190 }
00191 
00192 void USBHostHub::rxHandler() {
00193     uint32_t status;
00194     if (int_in) {
00195         if (int_in->getState() == USB_TYPE_IDLE) {
00196             for (int port = 1; port <= nb_port; port++) {
00197                 status = getPortStatus(port);
00198                 USB_DBG("[hub handler hub: %d] status port %d [hub: %p]: 0x%X", dev->getHub(), port, dev, status);
00199 
00200                 // if connection status has changed
00201                 if (status & C_PORT_CONNECTION) {
00202                     if (status & PORT_CONNECTION) {
00203                         USB_DBG("[hub handler hub: %d - port: %d] new device connected", dev->getHub(), port);
00204                         host->deviceConnected(dev->getHub() + 1, port, status & PORT_LOW_SPEED, this);
00205                     } else {
00206                         USB_DBG("[hub handler hub: %d - port: %d] device disconnected", dev->getHub(), port);
00207                         host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
00208                     }
00209 
00210                     clearPortFeature(C_PORT_CONNECTION_FEATURE, port);
00211                 }
00212 
00213                 if (status & C_PORT_RESET) {
00214                     clearPortFeature(C_PORT_RESET_FEATURE, port);
00215                 }
00216 
00217                 if (status & C_PORT_ENABLE) {
00218                     clearPortFeature(C_PORT_ENABLE_FEATURE, port);
00219                 }
00220 
00221                 if ((status & PORT_OVER_CURRENT)) {
00222                     USB_ERR("OVER CURRENT DETECTED\r\n");
00223                     clearPortFeature(PORT_OVER_CURRENT, port);
00224                     host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
00225                 }
00226             }
00227         }
00228         host->interruptRead(dev, int_in, buf, 1, false);
00229     }
00230 }
00231 
00232 void USBHostHub::portReset(uint8_t port) {
00233     // reset port
00234     uint32_t status;
00235     USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this);
00236     setPortFeature(PORT_RESET_FEATURE, port);
00237     while(1) {
00238         status = getPortStatus(port);
00239         if (status & (PORT_ENABLE | PORT_RESET))
00240             break;
00241         if (status & PORT_OVER_CURRENT) {
00242             USB_ERR("OVER CURRENT DETECTED\r\n");
00243             clearPortFeature(PORT_OVER_CURRENT, port);
00244             host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
00245             break;
00246         }
00247         ThisThread::sleep_for(10);
00248     }
00249 }
00250 
00251 void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) {
00252     host->controlWrite( dev,
00253                         USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
00254                         SET_FEATURE,
00255                         feature,
00256                         port,
00257                         NULL,
00258                         0);
00259 }
00260 
00261 void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) {
00262     host->controlWrite( dev,
00263                         USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
00264                         CLEAR_FEATURE,
00265                         feature,
00266                         port,
00267                         NULL,
00268                         0);
00269 }
00270 
00271 uint32_t USBHostHub::getPortStatus(uint8_t port) {
00272     uint8_t* safe = host->getSafeMem(4);
00273     uint32_t st;
00274     host->controlRead(  dev,
00275                         USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
00276                         GET_STATUS,
00277                         0,
00278                         port,
00279                         safe,
00280                         4);
00281     st = *((uint32_t*)safe);
00282     host->returnSafeMem(safe);
00283     return st;
00284 }
00285 
00286 #endif