![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test public
Dependencies: HttpServer_snapshot_mbed-os
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 #if defined(TARGET_RZ_A2XX) 00056 buf = (uint8_t *)AllocNonCacheMem(sizeof(HubDescriptor)); 00057 p_st = (uint32_t *)AllocNonCacheMem(sizeof(HubDescriptor)); 00058 #endif 00059 init(); 00060 } 00061 00062 #if defined(TARGET_RZ_A2XX) 00063 USBHostHub::~USBHostHub() { 00064 FreeNonCacheMem(buf); 00065 FreeNonCacheMem(p_st); 00066 } 00067 #endif 00068 00069 void USBHostHub::init() { 00070 dev_connected = false; 00071 dev = NULL; 00072 int_in = NULL; 00073 dev_connected = false; 00074 hub_intf = -1; 00075 hub_device_found = false; 00076 nb_port = 0; 00077 hub_characteristics = 0; 00078 00079 for (int i = 0; i < MAX_HUB_PORT; i++) { 00080 device_children[i] = NULL; 00081 } 00082 } 00083 00084 void USBHostHub::setHost(USBHost * host_) { 00085 host = host_; 00086 } 00087 00088 bool USBHostHub::connected() 00089 { 00090 return dev_connected; 00091 } 00092 00093 bool USBHostHub::connect(USBDeviceConnected * dev) 00094 { 00095 if (dev_connected) { 00096 return true; 00097 } 00098 00099 if(host->enumerate(dev, this)) { 00100 init(); 00101 return false; 00102 } 00103 00104 if (hub_device_found) { 00105 this->dev = dev; 00106 00107 int_in = dev->getEndpoint(hub_intf, INTERRUPT_ENDPOINT, IN); 00108 00109 if (!int_in) { 00110 init(); 00111 return false; 00112 } 00113 00114 USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, hub_intf); 00115 dev->setName("Hub", hub_intf); 00116 host->registerDriver(dev, hub_intf, this, &USBHostHub::disconnect); 00117 00118 int_in->attach(this, &USBHostHub::rxHandler); 00119 00120 // get HUB descriptor 00121 host->controlRead( dev, 00122 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, 00123 GET_DESCRIPTOR, 00124 0x29 << 8, 0, buf, sizeof(HubDescriptor)); 00125 nb_port = buf[2]; 00126 hub_characteristics = buf[3]; 00127 00128 USB_DBG("Hub has %d port", nb_port); 00129 00130 for (uint8_t j = 1; j <= nb_port; j++) { 00131 setPortFeature(PORT_POWER_FEATURE, j); 00132 } 00133 wait_ms(buf[5]*2); 00134 00135 host->interruptRead(dev, int_in, buf, 1, false); 00136 dev_connected = true; 00137 return true; 00138 } 00139 00140 return false; 00141 } 00142 00143 void USBHostHub::disconnect() { 00144 init(); 00145 } 00146 00147 /*virtual*/ void USBHostHub::setVidPid(uint16_t vid, uint16_t pid) 00148 { 00149 // we don't check VID/PID for MSD driver 00150 } 00151 00152 /*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 00153 { 00154 if ((hub_intf == -1) && 00155 (intf_class == HUB_CLASS) && 00156 (intf_subclass == 0) && 00157 (intf_protocol == 0)) { 00158 hub_intf = intf_nb; 00159 return true; 00160 } 00161 return false; 00162 } 00163 00164 /*virtual*/ bool USBHostHub::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00165 { 00166 if (intf_nb == hub_intf) { 00167 if ((type == INTERRUPT_ENDPOINT) && (dir == IN)) { 00168 hub_device_found = true; 00169 return true; 00170 } 00171 } 00172 return false; 00173 } 00174 00175 void USBHostHub::deviceConnected(USBDeviceConnected * dev) { 00176 device_children[dev->getPort() - 1] = dev; 00177 } 00178 00179 void USBHostHub::deviceDisconnected(USBDeviceConnected * dev) { 00180 device_children[dev->getPort() - 1] = NULL; 00181 } 00182 00183 void USBHostHub::hubDisconnected() { 00184 for (uint8_t i = 0; i < MAX_HUB_PORT; i++) { 00185 if (device_children[i] != NULL) { 00186 host->freeDevice(device_children[i]); 00187 } 00188 } 00189 } 00190 00191 void USBHostHub::rxHandler() { 00192 uint32_t status; 00193 if (int_in) { 00194 if (int_in->getState() == USB_TYPE_IDLE) { 00195 for (int port = 1; port <= nb_port; port++) { 00196 status = getPortStatus(port); 00197 USB_DBG("[hub handler hub: %d] status port %d [hub: %p]: 0x%X", dev->getHub(), port, dev, status); 00198 00199 // if connection status has changed 00200 if (status & C_PORT_CONNECTION) { 00201 if (status & PORT_CONNECTION) { 00202 USB_DBG("[hub handler hub: %d - port: %d] new device connected", dev->getHub(), port); 00203 host->deviceConnected(dev->getHub() + 1, port, status & PORT_LOW_SPEED, this); 00204 } else { 00205 USB_DBG("[hub handler hub: %d - port: %d] device disconnected", dev->getHub(), port); 00206 host->deviceDisconnected(dev->getHub() + 1, port, this, 0); 00207 } 00208 00209 clearPortFeature(C_PORT_CONNECTION_FEATURE, port); 00210 } 00211 00212 if (status & C_PORT_RESET) { 00213 clearPortFeature(C_PORT_RESET_FEATURE, port); 00214 } 00215 00216 if (status & C_PORT_ENABLE) { 00217 clearPortFeature(C_PORT_ENABLE_FEATURE, port); 00218 } 00219 00220 if ((status & PORT_OVER_CURRENT)) { 00221 USB_ERR("OVER CURRENT DETECTED\r\n"); 00222 clearPortFeature(PORT_OVER_CURRENT, port); 00223 host->deviceDisconnected(dev->getHub() + 1, port, this, 0); 00224 } 00225 } 00226 } 00227 host->interruptRead(dev, int_in, buf, 1, false); 00228 } 00229 } 00230 00231 void USBHostHub::portReset(uint8_t port) { 00232 // reset port 00233 uint32_t status; 00234 USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this) 00235 setPortFeature(PORT_RESET_FEATURE, port); 00236 #if defined(TARGET_RZ_A1H) 00237 ThisThread::sleep_for(50); // Reset release waiting for Hi-Speed check. 00238 #endif 00239 while(1) { 00240 status = getPortStatus(port); 00241 if (status & (PORT_ENABLE | PORT_RESET)) 00242 break; 00243 if (status & PORT_OVER_CURRENT) { 00244 USB_ERR("OVER CURRENT DETECTED\r\n"); 00245 clearPortFeature(PORT_OVER_CURRENT, port); 00246 host->deviceDisconnected(dev->getHub() + 1, port, this, 0); 00247 break; 00248 } 00249 ThisThread::sleep_for(10); 00250 } 00251 } 00252 00253 void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) { 00254 host->controlWrite( dev, 00255 USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, 00256 SET_FEATURE, 00257 feature, 00258 port, 00259 NULL, 00260 0); 00261 } 00262 00263 void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) { 00264 host->controlWrite( dev, 00265 USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, 00266 CLEAR_FEATURE, 00267 feature, 00268 port, 00269 NULL, 00270 0); 00271 } 00272 00273 uint32_t USBHostHub::getPortStatus(uint8_t port) { 00274 #if defined(TARGET_RZ_A2XX) 00275 host->controlRead( dev, 00276 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, 00277 GET_STATUS, 00278 0, 00279 port, 00280 (uint8_t *)p_st, 00281 4); 00282 return *p_st; 00283 #else 00284 uint32_t st; 00285 host->controlRead( dev, 00286 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, 00287 GET_STATUS, 00288 0, 00289 port, 00290 (uint8_t *)&st, 00291 4); 00292 return st; 00293 #endif 00294 } 00295 00296 #endif
Generated on Wed Jul 13 2022 05:33:37 by
![doxygen](doxygen.png)