the do / gr-peach-opencv-project

Fork of gr-peach-opencv-project by the do

Committer:
thedo
Date:
Tue Jul 04 06:23:13 2017 +0000
Revision:
170:54ff26da7eb6
Parent:
166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.

Who changed what in which revision?

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