Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

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

Committer:
thedo
Date:
Fri Jul 21 01:26:02 2017 +0000
Revision:
166:240bc5a0f42a
gr-peach-opencv-project-sd-card

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thedo 166:240bc5a0f42a 1 /* mbed USBHost Library
thedo 166:240bc5a0f42a 2 * Copyright (c) 2006-2013 ARM Limited
thedo 166:240bc5a0f42a 3 *
thedo 166:240bc5a0f42a 4 * Licensed under the Apache License, Version 2.0 (the "License");
thedo 166:240bc5a0f42a 5 * you may not use this file except in compliance with the License.
thedo 166:240bc5a0f42a 6 * You may obtain a copy of the License at
thedo 166:240bc5a0f42a 7 *
thedo 166:240bc5a0f42a 8 * http://www.apache.org/licenses/LICENSE-2.0
thedo 166:240bc5a0f42a 9 *
thedo 166:240bc5a0f42a 10 * Unless required by applicable law or agreed to in writing, software
thedo 166:240bc5a0f42a 11 * distributed under the License is distributed on an "AS IS" BASIS,
thedo 166:240bc5a0f42a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
thedo 166:240bc5a0f42a 13 * See the License for the specific language governing permissions and
thedo 166:240bc5a0f42a 14 * limitations under the License.
thedo 166:240bc5a0f42a 15 */
thedo 166:240bc5a0f42a 16
thedo 166:240bc5a0f42a 17
thedo 166:240bc5a0f42a 18 #include "USBHost.h"
thedo 166:240bc5a0f42a 19 #include "USBHostHub.h"
thedo 166:240bc5a0f42a 20
thedo 166:240bc5a0f42a 21 USBHost * USBHost::instHost = NULL;
thedo 166:240bc5a0f42a 22
thedo 166:240bc5a0f42a 23 #define DEVICE_CONNECTED_EVENT (1 << 0)
thedo 166:240bc5a0f42a 24 #define DEVICE_DISCONNECTED_EVENT (1 << 1)
thedo 166:240bc5a0f42a 25 #define TD_PROCESSED_EVENT (1 << 2)
thedo 166:240bc5a0f42a 26
thedo 166:240bc5a0f42a 27 #define MAX_TRY_ENUMERATE_HUB 3
thedo 166:240bc5a0f42a 28
thedo 166:240bc5a0f42a 29 #define MIN(a, b) ((a > b) ? b : a)
thedo 166:240bc5a0f42a 30
thedo 166:240bc5a0f42a 31 /**
thedo 166:240bc5a0f42a 32 * How interrupts are processed:
thedo 166:240bc5a0f42a 33 * - new device connected:
thedo 166:240bc5a0f42a 34 * - a message is queued in queue_usb_event with the id DEVICE_CONNECTED_EVENT
thedo 166:240bc5a0f42a 35 * - when the usb_thread receives the event, it:
thedo 166:240bc5a0f42a 36 * - resets the device
thedo 166:240bc5a0f42a 37 * - reads the device descriptor
thedo 166:240bc5a0f42a 38 * - sets the address of the device
thedo 166:240bc5a0f42a 39 * - if it is a hub, enumerates it
thedo 166:240bc5a0f42a 40 * - device disconnected:
thedo 166:240bc5a0f42a 41 * - a message is queued in queue_usb_event with the id DEVICE_DISCONNECTED_EVENT
thedo 166:240bc5a0f42a 42 * - when the usb_thread receives the event, it:
thedo 166:240bc5a0f42a 43 * - free the device and all its children (hub)
thedo 166:240bc5a0f42a 44 * - td processed
thedo 166:240bc5a0f42a 45 * - a message is queued in queue_usb_event with the id TD_PROCESSED_EVENT
thedo 166:240bc5a0f42a 46 * - when the usb_thread receives the event, it:
thedo 166:240bc5a0f42a 47 * - call the callback attached to the endpoint where the td is attached
thedo 166:240bc5a0f42a 48 */
thedo 166:240bc5a0f42a 49 void USBHost::usb_process() {
thedo 166:240bc5a0f42a 50
thedo 166:240bc5a0f42a 51 bool controlListState;
thedo 166:240bc5a0f42a 52 bool bulkListState;
thedo 166:240bc5a0f42a 53 bool interruptListState;
thedo 166:240bc5a0f42a 54 USBEndpoint * ep;
thedo 166:240bc5a0f42a 55 uint8_t i, j, res, timeout_set_addr = 10;
thedo 166:240bc5a0f42a 56 uint8_t buf[8];
thedo 166:240bc5a0f42a 57 bool too_many_hub;
thedo 166:240bc5a0f42a 58 int idx;
thedo 166:240bc5a0f42a 59
thedo 166:240bc5a0f42a 60 #if DEBUG_TRANSFER
thedo 166:240bc5a0f42a 61 uint8_t * buf_transfer;
thedo 166:240bc5a0f42a 62 #endif
thedo 166:240bc5a0f42a 63
thedo 166:240bc5a0f42a 64 #if MAX_HUB_NB
thedo 166:240bc5a0f42a 65 uint8_t k;
thedo 166:240bc5a0f42a 66 #endif
thedo 166:240bc5a0f42a 67
thedo 166:240bc5a0f42a 68 while(1) {
thedo 166:240bc5a0f42a 69 osEvent evt = mail_usb_event.get();
thedo 166:240bc5a0f42a 70
thedo 166:240bc5a0f42a 71 if (evt.status == osEventMail) {
thedo 166:240bc5a0f42a 72
thedo 166:240bc5a0f42a 73 message_t * usb_msg = (message_t*)evt.value.p;
thedo 166:240bc5a0f42a 74
thedo 166:240bc5a0f42a 75 switch (usb_msg->event_id) {
thedo 166:240bc5a0f42a 76
thedo 166:240bc5a0f42a 77 // a new device has been connected
thedo 166:240bc5a0f42a 78 case DEVICE_CONNECTED_EVENT:
thedo 166:240bc5a0f42a 79 too_many_hub = false;
thedo 166:240bc5a0f42a 80 buf[4] = 0;
thedo 166:240bc5a0f42a 81
thedo 166:240bc5a0f42a 82 do
thedo 166:240bc5a0f42a 83 {
thedo 166:240bc5a0f42a 84 Lock lock(this);
thedo 166:240bc5a0f42a 85
thedo 166:240bc5a0f42a 86 for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
thedo 166:240bc5a0f42a 87 if (!deviceInUse[i]) {
thedo 166:240bc5a0f42a 88 USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]);
thedo 166:240bc5a0f42a 89 devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed);
thedo 166:240bc5a0f42a 90 deviceReset[i] = false;
thedo 166:240bc5a0f42a 91 deviceInited[i] = true;
thedo 166:240bc5a0f42a 92 break;
thedo 166:240bc5a0f42a 93 }
thedo 166:240bc5a0f42a 94 }
thedo 166:240bc5a0f42a 95
thedo 166:240bc5a0f42a 96 if (i == MAX_DEVICE_CONNECTED) {
thedo 166:240bc5a0f42a 97 USB_ERR("Too many device connected!!\r\n");
thedo 166:240bc5a0f42a 98 continue;
thedo 166:240bc5a0f42a 99 }
thedo 166:240bc5a0f42a 100
thedo 166:240bc5a0f42a 101 if (!controlEndpointAllocated) {
thedo 166:240bc5a0f42a 102 control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00);
thedo 166:240bc5a0f42a 103 addEndpoint(NULL, 0, (USBEndpoint*)control);
thedo 166:240bc5a0f42a 104 controlEndpointAllocated = true;
thedo 166:240bc5a0f42a 105 }
thedo 166:240bc5a0f42a 106
thedo 166:240bc5a0f42a 107 #if MAX_HUB_NB
thedo 166:240bc5a0f42a 108 if (usb_msg->hub_parent)
thedo 166:240bc5a0f42a 109 devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent));
thedo 166:240bc5a0f42a 110 #endif
thedo 166:240bc5a0f42a 111
thedo 166:240bc5a0f42a 112 for (j = 0; j < timeout_set_addr; j++) {
thedo 166:240bc5a0f42a 113
thedo 166:240bc5a0f42a 114 resetDevice(&devices[i]);
thedo 166:240bc5a0f42a 115
thedo 166:240bc5a0f42a 116 // set size of control endpoint
thedo 166:240bc5a0f42a 117 devices[i].setSizeControlEndpoint(8);
thedo 166:240bc5a0f42a 118
thedo 166:240bc5a0f42a 119 devices[i].activeAddress(false);
thedo 166:240bc5a0f42a 120
thedo 166:240bc5a0f42a 121 // get first 8 bit of device descriptor
thedo 166:240bc5a0f42a 122 // and check if we deal with a hub
thedo 166:240bc5a0f42a 123 USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]);
thedo 166:240bc5a0f42a 124 res = getDeviceDescriptor(&devices[i], buf, 8);
thedo 166:240bc5a0f42a 125
thedo 166:240bc5a0f42a 126 if (res != USB_TYPE_OK) {
thedo 166:240bc5a0f42a 127 USB_ERR("usb_thread could not read dev descr");
thedo 166:240bc5a0f42a 128 continue;
thedo 166:240bc5a0f42a 129 }
thedo 166:240bc5a0f42a 130
thedo 166:240bc5a0f42a 131 // set size of control endpoint
thedo 166:240bc5a0f42a 132 devices[i].setSizeControlEndpoint(buf[7]);
thedo 166:240bc5a0f42a 133
thedo 166:240bc5a0f42a 134 // second step: set an address to the device
thedo 166:240bc5a0f42a 135 res = setAddress(&devices[i], devices[i].getAddress());
thedo 166:240bc5a0f42a 136
thedo 166:240bc5a0f42a 137 if (res != USB_TYPE_OK) {
thedo 166:240bc5a0f42a 138 USB_ERR("SET ADDR FAILED");
thedo 166:240bc5a0f42a 139 continue;
thedo 166:240bc5a0f42a 140 }
thedo 166:240bc5a0f42a 141 devices[i].activeAddress(true);
thedo 166:240bc5a0f42a 142 USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress());
thedo 166:240bc5a0f42a 143
thedo 166:240bc5a0f42a 144 // try to read again the device descriptor to check if the device
thedo 166:240bc5a0f42a 145 // answers to its new address
thedo 166:240bc5a0f42a 146 res = getDeviceDescriptor(&devices[i], buf, 8);
thedo 166:240bc5a0f42a 147
thedo 166:240bc5a0f42a 148 if (res == USB_TYPE_OK) {
thedo 166:240bc5a0f42a 149 break;
thedo 166:240bc5a0f42a 150 }
thedo 166:240bc5a0f42a 151
thedo 166:240bc5a0f42a 152 Thread::wait(100);
thedo 166:240bc5a0f42a 153 }
thedo 166:240bc5a0f42a 154
thedo 166:240bc5a0f42a 155 USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
thedo 166:240bc5a0f42a 156
thedo 166:240bc5a0f42a 157 #if MAX_HUB_NB
thedo 166:240bc5a0f42a 158 if (buf[4] == HUB_CLASS) {
thedo 166:240bc5a0f42a 159 for (k = 0; k < MAX_HUB_NB; k++) {
thedo 166:240bc5a0f42a 160 if (hub_in_use[k] == false) {
thedo 166:240bc5a0f42a 161 for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) {
thedo 166:240bc5a0f42a 162 if (hubs[k].connect(&devices[i])) {
thedo 166:240bc5a0f42a 163 devices[i].hub = &hubs[k];
thedo 166:240bc5a0f42a 164 hub_in_use[k] = true;
thedo 166:240bc5a0f42a 165 break;
thedo 166:240bc5a0f42a 166 }
thedo 166:240bc5a0f42a 167 }
thedo 166:240bc5a0f42a 168 if (hub_in_use[k] == true)
thedo 166:240bc5a0f42a 169 break;
thedo 166:240bc5a0f42a 170 }
thedo 166:240bc5a0f42a 171 }
thedo 166:240bc5a0f42a 172
thedo 166:240bc5a0f42a 173 if (k == MAX_HUB_NB) {
thedo 166:240bc5a0f42a 174 USB_ERR("Too many hubs connected!!\r\n");
thedo 166:240bc5a0f42a 175 too_many_hub = true;
thedo 166:240bc5a0f42a 176 }
thedo 166:240bc5a0f42a 177 }
thedo 166:240bc5a0f42a 178
thedo 166:240bc5a0f42a 179 if (usb_msg->hub_parent)
thedo 166:240bc5a0f42a 180 ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]);
thedo 166:240bc5a0f42a 181 #endif
thedo 166:240bc5a0f42a 182
thedo 166:240bc5a0f42a 183 if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) {
thedo 166:240bc5a0f42a 184 deviceInUse[i] = true;
thedo 166:240bc5a0f42a 185 }
thedo 166:240bc5a0f42a 186
thedo 166:240bc5a0f42a 187 } while(0);
thedo 166:240bc5a0f42a 188
thedo 166:240bc5a0f42a 189 break;
thedo 166:240bc5a0f42a 190
thedo 166:240bc5a0f42a 191 // a device has been disconnected
thedo 166:240bc5a0f42a 192 case DEVICE_DISCONNECTED_EVENT:
thedo 166:240bc5a0f42a 193
thedo 166:240bc5a0f42a 194 do
thedo 166:240bc5a0f42a 195 {
thedo 166:240bc5a0f42a 196 Lock lock(this);
thedo 166:240bc5a0f42a 197
thedo 166:240bc5a0f42a 198 controlListState = disableList(CONTROL_ENDPOINT);
thedo 166:240bc5a0f42a 199 bulkListState = disableList(BULK_ENDPOINT);
thedo 166:240bc5a0f42a 200 interruptListState = disableList(INTERRUPT_ENDPOINT);
thedo 166:240bc5a0f42a 201
thedo 166:240bc5a0f42a 202 idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent));
thedo 166:240bc5a0f42a 203 if (idx != -1) {
thedo 166:240bc5a0f42a 204 freeDevice((USBDeviceConnected*)&devices[idx]);
thedo 166:240bc5a0f42a 205 }
thedo 166:240bc5a0f42a 206
thedo 166:240bc5a0f42a 207 if (controlListState) enableList(CONTROL_ENDPOINT);
thedo 166:240bc5a0f42a 208 if (bulkListState) enableList(BULK_ENDPOINT);
thedo 166:240bc5a0f42a 209 if (interruptListState) enableList(INTERRUPT_ENDPOINT);
thedo 166:240bc5a0f42a 210
thedo 166:240bc5a0f42a 211 } while(0);
thedo 166:240bc5a0f42a 212
thedo 166:240bc5a0f42a 213 break;
thedo 166:240bc5a0f42a 214
thedo 166:240bc5a0f42a 215 // a td has been processed
thedo 166:240bc5a0f42a 216 // call callback on the ed associated to the td
thedo 166:240bc5a0f42a 217 // we are not in ISR -> users can use printf in their callback method
thedo 166:240bc5a0f42a 218 case TD_PROCESSED_EVENT:
thedo 166:240bc5a0f42a 219 ep = (USBEndpoint *) ((HCTD *)usb_msg->td_addr)->ep;
thedo 166:240bc5a0f42a 220 if (usb_msg->td_state == USB_TYPE_IDLE) {
thedo 166:240bc5a0f42a 221 USB_DBG_EVENT("call callback on td %p [ep: %p state: %s - dev: %p - %s]", usb_msg->td_addr, ep, ep->getStateString(), ep->dev, ep->dev->getName(ep->getIntfNb()));
thedo 166:240bc5a0f42a 222
thedo 166:240bc5a0f42a 223 #if DEBUG_TRANSFER
thedo 166:240bc5a0f42a 224 if (ep->getDir() == IN) {
thedo 166:240bc5a0f42a 225 buf_transfer = ep->getBufStart();
thedo 166:240bc5a0f42a 226 printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress());
thedo 166:240bc5a0f42a 227 for (int i = 0; i < ep->getLengthTransferred(); i++)
thedo 166:240bc5a0f42a 228 printf("%02X ", buf_transfer[i]);
thedo 166:240bc5a0f42a 229 printf("\r\n\r\n");
thedo 166:240bc5a0f42a 230 }
thedo 166:240bc5a0f42a 231 #endif
thedo 166:240bc5a0f42a 232 ep->call();
thedo 166:240bc5a0f42a 233 } else {
thedo 166:240bc5a0f42a 234 idx = findDevice(ep->dev);
thedo 166:240bc5a0f42a 235 if (idx != -1) {
thedo 166:240bc5a0f42a 236 if (deviceInUse[idx]) {
thedo 166:240bc5a0f42a 237 USB_WARN("td %p processed but not in idle state: %s [ep: %p - dev: %p - %s]", usb_msg->td_addr, ep->getStateString(), ep, ep->dev, ep->dev->getName(ep->getIntfNb()));
thedo 166:240bc5a0f42a 238 ep->setState(USB_TYPE_IDLE);
thedo 166:240bc5a0f42a 239 }
thedo 166:240bc5a0f42a 240 }
thedo 166:240bc5a0f42a 241 }
thedo 166:240bc5a0f42a 242 break;
thedo 166:240bc5a0f42a 243 }
thedo 166:240bc5a0f42a 244
thedo 166:240bc5a0f42a 245 mail_usb_event.free(usb_msg);
thedo 166:240bc5a0f42a 246 }
thedo 166:240bc5a0f42a 247 }
thedo 166:240bc5a0f42a 248 }
thedo 166:240bc5a0f42a 249
thedo 166:240bc5a0f42a 250 /* static */void USBHost::usb_process_static(void const * arg) {
thedo 166:240bc5a0f42a 251 ((USBHost *)arg)->usb_process();
thedo 166:240bc5a0f42a 252 }
thedo 166:240bc5a0f42a 253
thedo 166:240bc5a0f42a 254 USBHost::USBHost() : usbThread(USBHost::usb_process_static, (void *)this, osPriorityNormal, USB_THREAD_STACK)
thedo 166:240bc5a0f42a 255 {
thedo 166:240bc5a0f42a 256 headControlEndpoint = NULL;
thedo 166:240bc5a0f42a 257 headBulkEndpoint = NULL;
thedo 166:240bc5a0f42a 258 headInterruptEndpoint = NULL;
thedo 166:240bc5a0f42a 259 tailControlEndpoint = NULL;
thedo 166:240bc5a0f42a 260 tailBulkEndpoint = NULL;
thedo 166:240bc5a0f42a 261 tailInterruptEndpoint = NULL;
thedo 166:240bc5a0f42a 262
thedo 166:240bc5a0f42a 263 lenReportDescr = 0;
thedo 166:240bc5a0f42a 264
thedo 166:240bc5a0f42a 265 controlEndpointAllocated = false;
thedo 166:240bc5a0f42a 266
thedo 166:240bc5a0f42a 267 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
thedo 166:240bc5a0f42a 268 deviceInUse[i] = false;
thedo 166:240bc5a0f42a 269 devices[i].setAddress(i + 1);
thedo 166:240bc5a0f42a 270 deviceReset[i] = false;
thedo 166:240bc5a0f42a 271 deviceInited[i] = false;
thedo 166:240bc5a0f42a 272 for (uint8_t j = 0; j < MAX_INTF; j++)
thedo 166:240bc5a0f42a 273 deviceAttachedDriver[i][j] = false;
thedo 166:240bc5a0f42a 274 }
thedo 166:240bc5a0f42a 275
thedo 166:240bc5a0f42a 276 #if MAX_HUB_NB
thedo 166:240bc5a0f42a 277 for (uint8_t i = 0; i < MAX_HUB_NB; i++) {
thedo 166:240bc5a0f42a 278 hubs[i].setHost(this);
thedo 166:240bc5a0f42a 279 hub_in_use[i] = false;
thedo 166:240bc5a0f42a 280 }
thedo 166:240bc5a0f42a 281 #endif
thedo 166:240bc5a0f42a 282 }
thedo 166:240bc5a0f42a 283
thedo 166:240bc5a0f42a 284 USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost)
thedo 166:240bc5a0f42a 285 {
thedo 166:240bc5a0f42a 286 m_pHost->usb_mutex.lock();
thedo 166:240bc5a0f42a 287 }
thedo 166:240bc5a0f42a 288
thedo 166:240bc5a0f42a 289 USBHost::Lock::~Lock()
thedo 166:240bc5a0f42a 290 {
thedo 166:240bc5a0f42a 291 m_pHost->usb_mutex.unlock();
thedo 166:240bc5a0f42a 292 }
thedo 166:240bc5a0f42a 293
thedo 166:240bc5a0f42a 294 void USBHost::transferCompleted(volatile uint32_t addr)
thedo 166:240bc5a0f42a 295 {
thedo 166:240bc5a0f42a 296 uint8_t state;
thedo 166:240bc5a0f42a 297
thedo 166:240bc5a0f42a 298 if(addr == 0)
thedo 166:240bc5a0f42a 299 return;
thedo 166:240bc5a0f42a 300
thedo 166:240bc5a0f42a 301 volatile HCTD* tdList = NULL;
thedo 166:240bc5a0f42a 302
thedo 166:240bc5a0f42a 303 //First we must reverse the list order and dequeue each TD
thedo 166:240bc5a0f42a 304 do {
thedo 166:240bc5a0f42a 305 volatile HCTD* td = (volatile HCTD*)addr;
thedo 166:240bc5a0f42a 306 addr = (uint32_t)td->nextTD; //Dequeue from physical list
thedo 166:240bc5a0f42a 307 td->nextTD = (hcTd*)tdList; //Enqueue into reversed list
thedo 166:240bc5a0f42a 308 tdList = td;
thedo 166:240bc5a0f42a 309 } while(addr);
thedo 166:240bc5a0f42a 310
thedo 166:240bc5a0f42a 311 while(tdList != NULL) {
thedo 166:240bc5a0f42a 312 volatile HCTD* td = tdList;
thedo 166:240bc5a0f42a 313 tdList = (volatile HCTD*)td->nextTD; //Dequeue element now as it could be modified below
thedo 166:240bc5a0f42a 314 if (td->ep != NULL) {
thedo 166:240bc5a0f42a 315 USBEndpoint * ep = (USBEndpoint *)(td->ep);
thedo 166:240bc5a0f42a 316
thedo 166:240bc5a0f42a 317 if (((HCTD *)td)->control >> 28) {
thedo 166:240bc5a0f42a 318 state = ((HCTD *)td)->control >> 28;
thedo 166:240bc5a0f42a 319 } else {
thedo 166:240bc5a0f42a 320 if (td->currBufPtr)
thedo 166:240bc5a0f42a 321 ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart());
thedo 166:240bc5a0f42a 322 state = 16 /*USB_TYPE_IDLE*/;
thedo 166:240bc5a0f42a 323 }
thedo 166:240bc5a0f42a 324
thedo 166:240bc5a0f42a 325 ep->unqueueTransfer(td);
thedo 166:240bc5a0f42a 326
thedo 166:240bc5a0f42a 327 if (ep->getType() != CONTROL_ENDPOINT) {
thedo 166:240bc5a0f42a 328 // callback on the processed td will be called from the usb_thread (not in ISR)
thedo 166:240bc5a0f42a 329 message_t * usb_msg = mail_usb_event.alloc();
thedo 166:240bc5a0f42a 330 usb_msg->event_id = TD_PROCESSED_EVENT;
thedo 166:240bc5a0f42a 331 usb_msg->td_addr = (void *)td;
thedo 166:240bc5a0f42a 332 usb_msg->td_state = state;
thedo 166:240bc5a0f42a 333 mail_usb_event.put(usb_msg);
thedo 166:240bc5a0f42a 334 }
thedo 166:240bc5a0f42a 335 ep->setState(state);
thedo 166:240bc5a0f42a 336 ep->ep_queue.put((uint8_t*)1);
thedo 166:240bc5a0f42a 337 }
thedo 166:240bc5a0f42a 338 }
thedo 166:240bc5a0f42a 339 }
thedo 166:240bc5a0f42a 340
thedo 166:240bc5a0f42a 341 USBHost * USBHost::getHostInst()
thedo 166:240bc5a0f42a 342 {
thedo 166:240bc5a0f42a 343 if (instHost == NULL) {
thedo 166:240bc5a0f42a 344 instHost = new USBHost();
thedo 166:240bc5a0f42a 345 instHost->init();
thedo 166:240bc5a0f42a 346 }
thedo 166:240bc5a0f42a 347 return instHost;
thedo 166:240bc5a0f42a 348 }
thedo 166:240bc5a0f42a 349
thedo 166:240bc5a0f42a 350
thedo 166:240bc5a0f42a 351 /*
thedo 166:240bc5a0f42a 352 * Called when a device has been connected
thedo 166:240bc5a0f42a 353 * Called in ISR!!!! (no printf)
thedo 166:240bc5a0f42a 354 */
thedo 166:240bc5a0f42a 355 /* virtual */ void USBHost::deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent)
thedo 166:240bc5a0f42a 356 {
thedo 166:240bc5a0f42a 357 // be sure that the new device connected is not already connected...
thedo 166:240bc5a0f42a 358 int idx = findDevice(hub, port, hub_parent);
thedo 166:240bc5a0f42a 359 if (idx != -1) {
thedo 166:240bc5a0f42a 360 if (deviceInited[idx])
thedo 166:240bc5a0f42a 361 return;
thedo 166:240bc5a0f42a 362 }
thedo 166:240bc5a0f42a 363
thedo 166:240bc5a0f42a 364 message_t * usb_msg = mail_usb_event.alloc();
thedo 166:240bc5a0f42a 365 usb_msg->event_id = DEVICE_CONNECTED_EVENT;
thedo 166:240bc5a0f42a 366 usb_msg->hub = hub;
thedo 166:240bc5a0f42a 367 usb_msg->port = port;
thedo 166:240bc5a0f42a 368 usb_msg->lowSpeed = lowSpeed;
thedo 166:240bc5a0f42a 369 usb_msg->hub_parent = hub_parent;
thedo 166:240bc5a0f42a 370 mail_usb_event.put(usb_msg);
thedo 166:240bc5a0f42a 371 }
thedo 166:240bc5a0f42a 372
thedo 166:240bc5a0f42a 373 /*
thedo 166:240bc5a0f42a 374 * Called when a device has been disconnected
thedo 166:240bc5a0f42a 375 * Called in ISR!!!! (no printf)
thedo 166:240bc5a0f42a 376 */
thedo 166:240bc5a0f42a 377 /* virtual */ void USBHost::deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr)
thedo 166:240bc5a0f42a 378 {
thedo 166:240bc5a0f42a 379 // be sure that the device disconnected is connected...
thedo 166:240bc5a0f42a 380 int idx = findDevice(hub, port, hub_parent);
thedo 166:240bc5a0f42a 381 if (idx != -1) {
thedo 166:240bc5a0f42a 382 if (!deviceInUse[idx])
thedo 166:240bc5a0f42a 383 return;
thedo 166:240bc5a0f42a 384 } else {
thedo 166:240bc5a0f42a 385 return;
thedo 166:240bc5a0f42a 386 }
thedo 166:240bc5a0f42a 387
thedo 166:240bc5a0f42a 388 message_t * usb_msg = mail_usb_event.alloc();
thedo 166:240bc5a0f42a 389 usb_msg->event_id = DEVICE_DISCONNECTED_EVENT;
thedo 166:240bc5a0f42a 390 usb_msg->hub = hub;
thedo 166:240bc5a0f42a 391 usb_msg->port = port;
thedo 166:240bc5a0f42a 392 usb_msg->hub_parent = hub_parent;
thedo 166:240bc5a0f42a 393 mail_usb_event.put(usb_msg);
thedo 166:240bc5a0f42a 394 }
thedo 166:240bc5a0f42a 395
thedo 166:240bc5a0f42a 396 void USBHost::freeDevice(USBDeviceConnected * dev)
thedo 166:240bc5a0f42a 397 {
thedo 166:240bc5a0f42a 398 USBEndpoint * ep = NULL;
thedo 166:240bc5a0f42a 399 HCED * ed = NULL;
thedo 166:240bc5a0f42a 400
thedo 166:240bc5a0f42a 401 #if MAX_HUB_NB
thedo 166:240bc5a0f42a 402 if (dev->getClass() == HUB_CLASS) {
thedo 166:240bc5a0f42a 403 if (dev->hub == NULL) {
thedo 166:240bc5a0f42a 404 USB_ERR("HUB NULL!!!!!\r\n");
thedo 166:240bc5a0f42a 405 } else {
thedo 166:240bc5a0f42a 406 dev->hub->hubDisconnected();
thedo 166:240bc5a0f42a 407 for (uint8_t i = 0; i < MAX_HUB_NB; i++) {
thedo 166:240bc5a0f42a 408 if (dev->hub == &hubs[i]) {
thedo 166:240bc5a0f42a 409 hub_in_use[i] = false;
thedo 166:240bc5a0f42a 410 break;
thedo 166:240bc5a0f42a 411 }
thedo 166:240bc5a0f42a 412 }
thedo 166:240bc5a0f42a 413 }
thedo 166:240bc5a0f42a 414 }
thedo 166:240bc5a0f42a 415
thedo 166:240bc5a0f42a 416 // notify hub parent that this device has been disconnected
thedo 166:240bc5a0f42a 417 if (dev->getHubParent())
thedo 166:240bc5a0f42a 418 dev->getHubParent()->deviceDisconnected(dev);
thedo 166:240bc5a0f42a 419
thedo 166:240bc5a0f42a 420 #endif
thedo 166:240bc5a0f42a 421
thedo 166:240bc5a0f42a 422 int idx = findDevice(dev);
thedo 166:240bc5a0f42a 423 if (idx != -1) {
thedo 166:240bc5a0f42a 424 deviceInUse[idx] = false;
thedo 166:240bc5a0f42a 425 deviceReset[idx] = false;
thedo 166:240bc5a0f42a 426
thedo 166:240bc5a0f42a 427 for (uint8_t j = 0; j < MAX_INTF; j++) {
thedo 166:240bc5a0f42a 428 deviceAttachedDriver[idx][j] = false;
thedo 166:240bc5a0f42a 429 if (dev->getInterface(j) != NULL) {
thedo 166:240bc5a0f42a 430 USB_DBG("FREE INTF %d on dev: %p, %p, nb_endpot: %d, %s", j, (void *)dev->getInterface(j), dev, dev->getInterface(j)->nb_endpoint, dev->getName(j));
thedo 166:240bc5a0f42a 431 for (int i = 0; i < dev->getInterface(j)->nb_endpoint; i++) {
thedo 166:240bc5a0f42a 432 if ((ep = dev->getEndpoint(j, i)) != NULL) {
thedo 166:240bc5a0f42a 433 ed = (HCED *)ep->getHCED();
thedo 166:240bc5a0f42a 434 ed->control |= (1 << 14); //sKip bit
thedo 166:240bc5a0f42a 435 unqueueEndpoint(ep);
thedo 166:240bc5a0f42a 436
thedo 166:240bc5a0f42a 437 freeTD((volatile uint8_t*)ep->getTDList()[0]);
thedo 166:240bc5a0f42a 438 freeTD((volatile uint8_t*)ep->getTDList()[1]);
thedo 166:240bc5a0f42a 439
thedo 166:240bc5a0f42a 440 freeED((uint8_t *)ep->getHCED());
thedo 166:240bc5a0f42a 441 }
thedo 166:240bc5a0f42a 442 printList(BULK_ENDPOINT);
thedo 166:240bc5a0f42a 443 printList(INTERRUPT_ENDPOINT);
thedo 166:240bc5a0f42a 444 }
thedo 166:240bc5a0f42a 445 USB_INFO("Device disconnected [%p - %s - hub: %d - port: %d]", dev, dev->getName(j), dev->getHub(), dev->getPort());
thedo 166:240bc5a0f42a 446 }
thedo 166:240bc5a0f42a 447 }
thedo 166:240bc5a0f42a 448 dev->disconnect();
thedo 166:240bc5a0f42a 449 }
thedo 166:240bc5a0f42a 450 }
thedo 166:240bc5a0f42a 451
thedo 166:240bc5a0f42a 452
thedo 166:240bc5a0f42a 453 void USBHost::unqueueEndpoint(USBEndpoint * ep)
thedo 166:240bc5a0f42a 454 {
thedo 166:240bc5a0f42a 455 USBEndpoint * prec = NULL;
thedo 166:240bc5a0f42a 456 USBEndpoint * current = NULL;
thedo 166:240bc5a0f42a 457
thedo 166:240bc5a0f42a 458 for (int i = 0; i < 2; i++) {
thedo 166:240bc5a0f42a 459 current = (i == 0) ? (USBEndpoint*)headBulkEndpoint : (USBEndpoint*)headInterruptEndpoint;
thedo 166:240bc5a0f42a 460 prec = current;
thedo 166:240bc5a0f42a 461 while (current != NULL) {
thedo 166:240bc5a0f42a 462 if (current == ep) {
thedo 166:240bc5a0f42a 463 if (current->nextEndpoint() != NULL) {
thedo 166:240bc5a0f42a 464 prec->queueEndpoint(current->nextEndpoint());
thedo 166:240bc5a0f42a 465 if (current == headBulkEndpoint) {
thedo 166:240bc5a0f42a 466 updateBulkHeadED((uint32_t)current->nextEndpoint()->getHCED());
thedo 166:240bc5a0f42a 467 headBulkEndpoint = current->nextEndpoint();
thedo 166:240bc5a0f42a 468 } else if (current == headInterruptEndpoint) {
thedo 166:240bc5a0f42a 469 updateInterruptHeadED((uint32_t)current->nextEndpoint()->getHCED());
thedo 166:240bc5a0f42a 470 headInterruptEndpoint = current->nextEndpoint();
thedo 166:240bc5a0f42a 471 }
thedo 166:240bc5a0f42a 472 }
thedo 166:240bc5a0f42a 473 // here we are dequeuing the queue of ed
thedo 166:240bc5a0f42a 474 // we need to update the tail pointer
thedo 166:240bc5a0f42a 475 else {
thedo 166:240bc5a0f42a 476 prec->queueEndpoint(NULL);
thedo 166:240bc5a0f42a 477 if (current == headBulkEndpoint) {
thedo 166:240bc5a0f42a 478 updateBulkHeadED(0);
thedo 166:240bc5a0f42a 479 headBulkEndpoint = current->nextEndpoint();
thedo 166:240bc5a0f42a 480 } else if (current == headInterruptEndpoint) {
thedo 166:240bc5a0f42a 481 updateInterruptHeadED(0);
thedo 166:240bc5a0f42a 482 headInterruptEndpoint = current->nextEndpoint();
thedo 166:240bc5a0f42a 483 }
thedo 166:240bc5a0f42a 484
thedo 166:240bc5a0f42a 485 // modify tail
thedo 166:240bc5a0f42a 486 switch (current->getType()) {
thedo 166:240bc5a0f42a 487 case BULK_ENDPOINT:
thedo 166:240bc5a0f42a 488 tailBulkEndpoint = prec;
thedo 166:240bc5a0f42a 489 break;
thedo 166:240bc5a0f42a 490 case INTERRUPT_ENDPOINT:
thedo 166:240bc5a0f42a 491 tailInterruptEndpoint = prec;
thedo 166:240bc5a0f42a 492 break;
thedo 166:240bc5a0f42a 493 default:
thedo 166:240bc5a0f42a 494 break;
thedo 166:240bc5a0f42a 495 }
thedo 166:240bc5a0f42a 496 }
thedo 166:240bc5a0f42a 497 current->setState(USB_TYPE_FREE);
thedo 166:240bc5a0f42a 498 return;
thedo 166:240bc5a0f42a 499 }
thedo 166:240bc5a0f42a 500 prec = current;
thedo 166:240bc5a0f42a 501 current = current->nextEndpoint();
thedo 166:240bc5a0f42a 502 }
thedo 166:240bc5a0f42a 503 }
thedo 166:240bc5a0f42a 504 }
thedo 166:240bc5a0f42a 505
thedo 166:240bc5a0f42a 506
thedo 166:240bc5a0f42a 507 USBDeviceConnected * USBHost::getDevice(uint8_t index)
thedo 166:240bc5a0f42a 508 {
thedo 166:240bc5a0f42a 509 if ((index >= MAX_DEVICE_CONNECTED) || (!deviceInUse[index])) {
thedo 166:240bc5a0f42a 510 return NULL;
thedo 166:240bc5a0f42a 511 }
thedo 166:240bc5a0f42a 512 return (USBDeviceConnected*)&devices[index];
thedo 166:240bc5a0f42a 513 }
thedo 166:240bc5a0f42a 514
thedo 166:240bc5a0f42a 515 // create an USBEndpoint descriptor. the USBEndpoint is not linked
thedo 166:240bc5a0f42a 516 USBEndpoint * USBHost::newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr)
thedo 166:240bc5a0f42a 517 {
thedo 166:240bc5a0f42a 518 int i = 0;
thedo 166:240bc5a0f42a 519 HCED * ed = (HCED *)getED();
thedo 166:240bc5a0f42a 520 HCTD* td_list[2] = { (HCTD*)getTD(), (HCTD*)getTD() };
thedo 166:240bc5a0f42a 521
thedo 166:240bc5a0f42a 522 memset((void *)td_list[0], 0x00, sizeof(HCTD));
thedo 166:240bc5a0f42a 523 memset((void *)td_list[1], 0x00, sizeof(HCTD));
thedo 166:240bc5a0f42a 524
thedo 166:240bc5a0f42a 525 // search a free USBEndpoint
thedo 166:240bc5a0f42a 526 for (i = 0; i < MAX_ENDPOINT; i++) {
thedo 166:240bc5a0f42a 527 if (endpoints[i].getState() == USB_TYPE_FREE) {
thedo 166:240bc5a0f42a 528 endpoints[i].init(ed, type, dir, size, addr, td_list);
thedo 166:240bc5a0f42a 529 USB_DBG("USBEndpoint created (%p): type: %d, dir: %d, size: %d, addr: %d, state: %s", &endpoints[i], type, dir, size, addr, endpoints[i].getStateString());
thedo 166:240bc5a0f42a 530 return &endpoints[i];
thedo 166:240bc5a0f42a 531 }
thedo 166:240bc5a0f42a 532 }
thedo 166:240bc5a0f42a 533 USB_ERR("could not allocate more endpoints!!!!");
thedo 166:240bc5a0f42a 534 return NULL;
thedo 166:240bc5a0f42a 535 }
thedo 166:240bc5a0f42a 536
thedo 166:240bc5a0f42a 537
thedo 166:240bc5a0f42a 538 USB_TYPE USBHost::resetDevice(USBDeviceConnected * dev)
thedo 166:240bc5a0f42a 539 {
thedo 166:240bc5a0f42a 540 int index = findDevice(dev);
thedo 166:240bc5a0f42a 541 if (index != -1) {
thedo 166:240bc5a0f42a 542 USB_DBG("Resetting hub %d, port %d\n", dev->getHub(), dev->getPort());
thedo 166:240bc5a0f42a 543 Thread::wait(100);
thedo 166:240bc5a0f42a 544 if (dev->getHub() == 0) {
thedo 166:240bc5a0f42a 545 resetRootHub();
thedo 166:240bc5a0f42a 546 }
thedo 166:240bc5a0f42a 547 #if MAX_HUB_NB
thedo 166:240bc5a0f42a 548 else {
thedo 166:240bc5a0f42a 549 dev->getHubParent()->portReset(dev->getPort());
thedo 166:240bc5a0f42a 550 }
thedo 166:240bc5a0f42a 551 #endif
thedo 166:240bc5a0f42a 552 Thread::wait(100);
thedo 166:240bc5a0f42a 553 deviceReset[index] = true;
thedo 166:240bc5a0f42a 554 return USB_TYPE_OK;
thedo 166:240bc5a0f42a 555 }
thedo 166:240bc5a0f42a 556
thedo 166:240bc5a0f42a 557 return USB_TYPE_ERROR;
thedo 166:240bc5a0f42a 558 }
thedo 166:240bc5a0f42a 559
thedo 166:240bc5a0f42a 560 // link the USBEndpoint to the linked list and attach an USBEndpoint to a device
thedo 166:240bc5a0f42a 561 bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep)
thedo 166:240bc5a0f42a 562 {
thedo 166:240bc5a0f42a 563
thedo 166:240bc5a0f42a 564 if (ep == NULL) {
thedo 166:240bc5a0f42a 565 return false;
thedo 166:240bc5a0f42a 566 }
thedo 166:240bc5a0f42a 567
thedo 166:240bc5a0f42a 568 HCED * prevEd;
thedo 166:240bc5a0f42a 569
thedo 166:240bc5a0f42a 570 // set device address in the USBEndpoint descriptor
thedo 166:240bc5a0f42a 571 if (dev == NULL) {
thedo 166:240bc5a0f42a 572 ep->setDeviceAddress(0);
thedo 166:240bc5a0f42a 573 } else {
thedo 166:240bc5a0f42a 574 ep->setDeviceAddress(dev->getAddress());
thedo 166:240bc5a0f42a 575 }
thedo 166:240bc5a0f42a 576
thedo 166:240bc5a0f42a 577 if ((dev != NULL) && dev->getSpeed()) {
thedo 166:240bc5a0f42a 578 ep->setSpeed(dev->getSpeed());
thedo 166:240bc5a0f42a 579 }
thedo 166:240bc5a0f42a 580
thedo 166:240bc5a0f42a 581 ep->setIntfNb(intf_nb);
thedo 166:240bc5a0f42a 582
thedo 166:240bc5a0f42a 583 // queue the new USBEndpoint on the ED list
thedo 166:240bc5a0f42a 584 switch (ep->getType()) {
thedo 166:240bc5a0f42a 585
thedo 166:240bc5a0f42a 586 case CONTROL_ENDPOINT:
thedo 166:240bc5a0f42a 587 prevEd = ( HCED*) controlHeadED();
thedo 166:240bc5a0f42a 588 if (!prevEd) {
thedo 166:240bc5a0f42a 589 updateControlHeadED((uint32_t) ep->getHCED());
thedo 166:240bc5a0f42a 590 USB_DBG_TRANSFER("First control USBEndpoint: %08X", (uint32_t) ep->getHCED());
thedo 166:240bc5a0f42a 591 headControlEndpoint = ep;
thedo 166:240bc5a0f42a 592 tailControlEndpoint = ep;
thedo 166:240bc5a0f42a 593 return true;
thedo 166:240bc5a0f42a 594 }
thedo 166:240bc5a0f42a 595 tailControlEndpoint->queueEndpoint(ep);
thedo 166:240bc5a0f42a 596 tailControlEndpoint = ep;
thedo 166:240bc5a0f42a 597 return true;
thedo 166:240bc5a0f42a 598
thedo 166:240bc5a0f42a 599 case BULK_ENDPOINT:
thedo 166:240bc5a0f42a 600 prevEd = ( HCED*) bulkHeadED();
thedo 166:240bc5a0f42a 601 if (!prevEd) {
thedo 166:240bc5a0f42a 602 updateBulkHeadED((uint32_t) ep->getHCED());
thedo 166:240bc5a0f42a 603 USB_DBG_TRANSFER("First bulk USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED());
thedo 166:240bc5a0f42a 604 headBulkEndpoint = ep;
thedo 166:240bc5a0f42a 605 tailBulkEndpoint = ep;
thedo 166:240bc5a0f42a 606 break;
thedo 166:240bc5a0f42a 607 }
thedo 166:240bc5a0f42a 608 USB_DBG_TRANSFER("Queue BULK Ed %p after %p\r\n",ep->getHCED(), prevEd);
thedo 166:240bc5a0f42a 609 tailBulkEndpoint->queueEndpoint(ep);
thedo 166:240bc5a0f42a 610 tailBulkEndpoint = ep;
thedo 166:240bc5a0f42a 611 break;
thedo 166:240bc5a0f42a 612
thedo 166:240bc5a0f42a 613 case INTERRUPT_ENDPOINT:
thedo 166:240bc5a0f42a 614 prevEd = ( HCED*) interruptHeadED();
thedo 166:240bc5a0f42a 615 if (!prevEd) {
thedo 166:240bc5a0f42a 616 updateInterruptHeadED((uint32_t) ep->getHCED());
thedo 166:240bc5a0f42a 617 USB_DBG_TRANSFER("First interrupt USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED());
thedo 166:240bc5a0f42a 618 headInterruptEndpoint = ep;
thedo 166:240bc5a0f42a 619 tailInterruptEndpoint = ep;
thedo 166:240bc5a0f42a 620 break;
thedo 166:240bc5a0f42a 621 }
thedo 166:240bc5a0f42a 622 USB_DBG_TRANSFER("Queue INTERRUPT Ed %p after %p\r\n",ep->getHCED(), prevEd);
thedo 166:240bc5a0f42a 623 tailInterruptEndpoint->queueEndpoint(ep);
thedo 166:240bc5a0f42a 624 tailInterruptEndpoint = ep;
thedo 166:240bc5a0f42a 625 break;
thedo 166:240bc5a0f42a 626 default:
thedo 166:240bc5a0f42a 627 return false;
thedo 166:240bc5a0f42a 628 }
thedo 166:240bc5a0f42a 629
thedo 166:240bc5a0f42a 630 ep->dev = dev;
thedo 166:240bc5a0f42a 631 dev->addEndpoint(intf_nb, ep);
thedo 166:240bc5a0f42a 632
thedo 166:240bc5a0f42a 633 return true;
thedo 166:240bc5a0f42a 634 }
thedo 166:240bc5a0f42a 635
thedo 166:240bc5a0f42a 636
thedo 166:240bc5a0f42a 637 int USBHost::findDevice(USBDeviceConnected * dev)
thedo 166:240bc5a0f42a 638 {
thedo 166:240bc5a0f42a 639 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
thedo 166:240bc5a0f42a 640 if (dev == &devices[i]) {
thedo 166:240bc5a0f42a 641 return i;
thedo 166:240bc5a0f42a 642 }
thedo 166:240bc5a0f42a 643 }
thedo 166:240bc5a0f42a 644 return -1;
thedo 166:240bc5a0f42a 645 }
thedo 166:240bc5a0f42a 646
thedo 166:240bc5a0f42a 647 int USBHost::findDevice(uint8_t hub, uint8_t port, USBHostHub * hub_parent)
thedo 166:240bc5a0f42a 648 {
thedo 166:240bc5a0f42a 649 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
thedo 166:240bc5a0f42a 650 if (devices[i].getHub() == hub && devices[i].getPort() == port) {
thedo 166:240bc5a0f42a 651 if (hub_parent != NULL) {
thedo 166:240bc5a0f42a 652 if (hub_parent == devices[i].getHubParent())
thedo 166:240bc5a0f42a 653 return i;
thedo 166:240bc5a0f42a 654 } else {
thedo 166:240bc5a0f42a 655 return i;
thedo 166:240bc5a0f42a 656 }
thedo 166:240bc5a0f42a 657 }
thedo 166:240bc5a0f42a 658 }
thedo 166:240bc5a0f42a 659 return -1;
thedo 166:240bc5a0f42a 660 }
thedo 166:240bc5a0f42a 661
thedo 166:240bc5a0f42a 662 void USBHost::printList(ENDPOINT_TYPE type)
thedo 166:240bc5a0f42a 663 {
thedo 166:240bc5a0f42a 664 #if DEBUG_EP_STATE
thedo 166:240bc5a0f42a 665 volatile HCED * hced;
thedo 166:240bc5a0f42a 666 switch(type) {
thedo 166:240bc5a0f42a 667 case CONTROL_ENDPOINT:
thedo 166:240bc5a0f42a 668 hced = (HCED *)controlHeadED();
thedo 166:240bc5a0f42a 669 break;
thedo 166:240bc5a0f42a 670 case BULK_ENDPOINT:
thedo 166:240bc5a0f42a 671 hced = (HCED *)bulkHeadED();
thedo 166:240bc5a0f42a 672 break;
thedo 166:240bc5a0f42a 673 case INTERRUPT_ENDPOINT:
thedo 166:240bc5a0f42a 674 hced = (HCED *)interruptHeadED();
thedo 166:240bc5a0f42a 675 break;
thedo 166:240bc5a0f42a 676 }
thedo 166:240bc5a0f42a 677 volatile HCTD * hctd = NULL;
thedo 166:240bc5a0f42a 678 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" :
thedo 166:240bc5a0f42a 679 ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" :
thedo 166:240bc5a0f42a 680 ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS"));
thedo 166:240bc5a0f42a 681 printf("State of %s:\r\n", type_str);
thedo 166:240bc5a0f42a 682 while (hced != NULL) {
thedo 166:240bc5a0f42a 683 uint8_t dir = ((hced->control & (3 << 11)) >> 11);
thedo 166:240bc5a0f42a 684 printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced,
thedo 166:240bc5a0f42a 685 hced->control & 0x7f,
thedo 166:240bc5a0f42a 686 (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"),
thedo 166:240bc5a0f42a 687 (hced->control & (0xf << 7)) >> 7);
thedo 166:240bc5a0f42a 688 hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf));
thedo 166:240bc5a0f42a 689 while (hctd != hced->tailTD) {
thedo 166:240bc5a0f42a 690 printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN");
thedo 166:240bc5a0f42a 691 hctd = hctd->nextTD;
thedo 166:240bc5a0f42a 692 }
thedo 166:240bc5a0f42a 693 printf("\thctd: %p\r\n", hctd);
thedo 166:240bc5a0f42a 694 hced = hced->nextED;
thedo 166:240bc5a0f42a 695 }
thedo 166:240bc5a0f42a 696 printf("\r\n\r\n");
thedo 166:240bc5a0f42a 697 #endif
thedo 166:240bc5a0f42a 698 }
thedo 166:240bc5a0f42a 699
thedo 166:240bc5a0f42a 700
thedo 166:240bc5a0f42a 701 // add a transfer on the TD linked list
thedo 166:240bc5a0f42a 702 USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
thedo 166:240bc5a0f42a 703 {
thedo 166:240bc5a0f42a 704 td_mutex.lock();
thedo 166:240bc5a0f42a 705
thedo 166:240bc5a0f42a 706 // allocate a TD which will be freed in TDcompletion
thedo 166:240bc5a0f42a 707 volatile HCTD * td = ed->getNextTD();
thedo 166:240bc5a0f42a 708 if (td == NULL) {
thedo 166:240bc5a0f42a 709 return USB_TYPE_ERROR;
thedo 166:240bc5a0f42a 710 }
thedo 166:240bc5a0f42a 711
thedo 166:240bc5a0f42a 712 uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT ));
thedo 166:240bc5a0f42a 713
thedo 166:240bc5a0f42a 714 uint32_t td_toggle;
thedo 166:240bc5a0f42a 715
thedo 166:240bc5a0f42a 716 if (ed->getType() == CONTROL_ENDPOINT) {
thedo 166:240bc5a0f42a 717 if (ed->isSetup()) {
thedo 166:240bc5a0f42a 718 td_toggle = TD_TOGGLE_0;
thedo 166:240bc5a0f42a 719 } else {
thedo 166:240bc5a0f42a 720 td_toggle = TD_TOGGLE_1;
thedo 166:240bc5a0f42a 721 }
thedo 166:240bc5a0f42a 722 } else {
thedo 166:240bc5a0f42a 723 td_toggle = 0;
thedo 166:240bc5a0f42a 724 }
thedo 166:240bc5a0f42a 725
thedo 166:240bc5a0f42a 726 td->control = (TD_ROUNDING | token | TD_DELAY_INT(0) | td_toggle | TD_CC);
thedo 166:240bc5a0f42a 727 td->currBufPtr = buf;
thedo 166:240bc5a0f42a 728 td->bufEnd = (buf + (len - 1));
thedo 166:240bc5a0f42a 729
thedo 166:240bc5a0f42a 730 ENDPOINT_TYPE type = ed->getType();
thedo 166:240bc5a0f42a 731
thedo 166:240bc5a0f42a 732 disableList(type);
thedo 166:240bc5a0f42a 733 ed->queueTransfer();
thedo 166:240bc5a0f42a 734 printList(type);
thedo 166:240bc5a0f42a 735 enableList(type);
thedo 166:240bc5a0f42a 736
thedo 166:240bc5a0f42a 737 td_mutex.unlock();
thedo 166:240bc5a0f42a 738
thedo 166:240bc5a0f42a 739 return USB_TYPE_PROCESSING;
thedo 166:240bc5a0f42a 740 }
thedo 166:240bc5a0f42a 741
thedo 166:240bc5a0f42a 742
thedo 166:240bc5a0f42a 743
thedo 166:240bc5a0f42a 744 USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr)
thedo 166:240bc5a0f42a 745 {
thedo 166:240bc5a0f42a 746 USB_TYPE t = controlRead( dev,
thedo 166:240bc5a0f42a 747 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
thedo 166:240bc5a0f42a 748 GET_DESCRIPTOR,
thedo 166:240bc5a0f42a 749 (DEVICE_DESCRIPTOR << 8) | (0),
thedo 166:240bc5a0f42a 750 0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf));
thedo 166:240bc5a0f42a 751 if (len_dev_descr)
thedo 166:240bc5a0f42a 752 *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf);
thedo 166:240bc5a0f42a 753
thedo 166:240bc5a0f42a 754 return t;
thedo 166:240bc5a0f42a 755 }
thedo 166:240bc5a0f42a 756
thedo 166:240bc5a0f42a 757 USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_conf_descr)
thedo 166:240bc5a0f42a 758 {
thedo 166:240bc5a0f42a 759 USB_TYPE res;
thedo 166:240bc5a0f42a 760 uint16_t total_conf_descr_length = 0;
thedo 166:240bc5a0f42a 761
thedo 166:240bc5a0f42a 762 // fourth step: get the beginning of the configuration descriptor to have the total length of the conf descr
thedo 166:240bc5a0f42a 763 res = controlRead( dev,
thedo 166:240bc5a0f42a 764 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
thedo 166:240bc5a0f42a 765 GET_DESCRIPTOR,
thedo 166:240bc5a0f42a 766 (CONFIGURATION_DESCRIPTOR << 8) | (0),
thedo 166:240bc5a0f42a 767 0, buf, CONFIGURATION_DESCRIPTOR_LENGTH);
thedo 166:240bc5a0f42a 768
thedo 166:240bc5a0f42a 769 if (res != USB_TYPE_OK) {
thedo 166:240bc5a0f42a 770 USB_ERR("GET CONF 1 DESCR FAILED");
thedo 166:240bc5a0f42a 771 return res;
thedo 166:240bc5a0f42a 772 }
thedo 166:240bc5a0f42a 773 total_conf_descr_length = buf[2] | (buf[3] << 8);
thedo 166:240bc5a0f42a 774 total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length);
thedo 166:240bc5a0f42a 775
thedo 166:240bc5a0f42a 776 if (len_conf_descr)
thedo 166:240bc5a0f42a 777 *len_conf_descr = total_conf_descr_length;
thedo 166:240bc5a0f42a 778
thedo 166:240bc5a0f42a 779 USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]);
thedo 166:240bc5a0f42a 780
thedo 166:240bc5a0f42a 781 return controlRead( dev,
thedo 166:240bc5a0f42a 782 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
thedo 166:240bc5a0f42a 783 GET_DESCRIPTOR,
thedo 166:240bc5a0f42a 784 (CONFIGURATION_DESCRIPTOR << 8) | (0),
thedo 166:240bc5a0f42a 785 0, buf, total_conf_descr_length);
thedo 166:240bc5a0f42a 786 }
thedo 166:240bc5a0f42a 787
thedo 166:240bc5a0f42a 788
thedo 166:240bc5a0f42a 789 USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address) {
thedo 166:240bc5a0f42a 790 return controlWrite( dev,
thedo 166:240bc5a0f42a 791 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
thedo 166:240bc5a0f42a 792 SET_ADDRESS,
thedo 166:240bc5a0f42a 793 address,
thedo 166:240bc5a0f42a 794 0, NULL, 0);
thedo 166:240bc5a0f42a 795
thedo 166:240bc5a0f42a 796 }
thedo 166:240bc5a0f42a 797
thedo 166:240bc5a0f42a 798 USB_TYPE USBHost::setConfiguration(USBDeviceConnected * dev, uint8_t conf)
thedo 166:240bc5a0f42a 799 {
thedo 166:240bc5a0f42a 800 return controlWrite( dev,
thedo 166:240bc5a0f42a 801 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
thedo 166:240bc5a0f42a 802 SET_CONFIGURATION,
thedo 166:240bc5a0f42a 803 conf,
thedo 166:240bc5a0f42a 804 0, NULL, 0);
thedo 166:240bc5a0f42a 805 }
thedo 166:240bc5a0f42a 806
thedo 166:240bc5a0f42a 807 uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev) {
thedo 166:240bc5a0f42a 808 int index = findDevice(dev);
thedo 166:240bc5a0f42a 809 uint8_t cnt = 0;
thedo 166:240bc5a0f42a 810 if (index == -1)
thedo 166:240bc5a0f42a 811 return 0;
thedo 166:240bc5a0f42a 812 for (uint8_t i = 0; i < MAX_INTF; i++) {
thedo 166:240bc5a0f42a 813 if (deviceAttachedDriver[index][i])
thedo 166:240bc5a0f42a 814 cnt++;
thedo 166:240bc5a0f42a 815 }
thedo 166:240bc5a0f42a 816 return cnt;
thedo 166:240bc5a0f42a 817 }
thedo 166:240bc5a0f42a 818
thedo 166:240bc5a0f42a 819 // enumerate a device with the control USBEndpoint
thedo 166:240bc5a0f42a 820 USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator)
thedo 166:240bc5a0f42a 821 {
thedo 166:240bc5a0f42a 822 uint16_t total_conf_descr_length = 0;
thedo 166:240bc5a0f42a 823 USB_TYPE res;
thedo 166:240bc5a0f42a 824
thedo 166:240bc5a0f42a 825 do
thedo 166:240bc5a0f42a 826 {
thedo 166:240bc5a0f42a 827 Lock lock(this);
thedo 166:240bc5a0f42a 828
thedo 166:240bc5a0f42a 829 // don't enumerate a device which all interfaces are registered to a specific driver
thedo 166:240bc5a0f42a 830 int index = findDevice(dev);
thedo 166:240bc5a0f42a 831
thedo 166:240bc5a0f42a 832 if (index == -1) {
thedo 166:240bc5a0f42a 833 return USB_TYPE_ERROR;
thedo 166:240bc5a0f42a 834 }
thedo 166:240bc5a0f42a 835
thedo 166:240bc5a0f42a 836 uint8_t nb_intf_attached = numberDriverAttached(dev);
thedo 166:240bc5a0f42a 837 USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf());
thedo 166:240bc5a0f42a 838 USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached);
thedo 166:240bc5a0f42a 839 if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) {
thedo 166:240bc5a0f42a 840 USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev);
thedo 166:240bc5a0f42a 841 return USB_TYPE_OK;
thedo 166:240bc5a0f42a 842 }
thedo 166:240bc5a0f42a 843
thedo 166:240bc5a0f42a 844 USB_DBG("Enumerate dev: %p", dev);
thedo 166:240bc5a0f42a 845
thedo 166:240bc5a0f42a 846 // third step: get the whole device descriptor to see vid, pid
thedo 166:240bc5a0f42a 847 res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH);
thedo 166:240bc5a0f42a 848
thedo 166:240bc5a0f42a 849 if (res != USB_TYPE_OK) {
thedo 166:240bc5a0f42a 850 USB_DBG("GET DEV DESCR FAILED");
thedo 166:240bc5a0f42a 851 return res;
thedo 166:240bc5a0f42a 852 }
thedo 166:240bc5a0f42a 853
thedo 166:240bc5a0f42a 854 dev->setClass(data[4]);
thedo 166:240bc5a0f42a 855 dev->setSubClass(data[5]);
thedo 166:240bc5a0f42a 856 dev->setProtocol(data[6]);
thedo 166:240bc5a0f42a 857 dev->setVid(data[8] | (data[9] << 8));
thedo 166:240bc5a0f42a 858 dev->setPid(data[10] | (data[11] << 8));
thedo 166:240bc5a0f42a 859 USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8));
thedo 166:240bc5a0f42a 860
thedo 166:240bc5a0f42a 861 pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) );
thedo 166:240bc5a0f42a 862
thedo 166:240bc5a0f42a 863 res = getConfigurationDescriptor(dev, data, sizeof(data), &total_conf_descr_length);
thedo 166:240bc5a0f42a 864 if (res != USB_TYPE_OK) {
thedo 166:240bc5a0f42a 865 return res;
thedo 166:240bc5a0f42a 866 }
thedo 166:240bc5a0f42a 867
thedo 166:240bc5a0f42a 868 #if (DEBUG > 3)
thedo 166:240bc5a0f42a 869 USB_DBG("CONFIGURATION DESCRIPTOR:\r\n");
thedo 166:240bc5a0f42a 870 for (int i = 0; i < total_conf_descr_length; i++)
thedo 166:240bc5a0f42a 871 printf("%02X ", data[i]);
thedo 166:240bc5a0f42a 872 printf("\r\n\r\n");
thedo 166:240bc5a0f42a 873 #endif
thedo 166:240bc5a0f42a 874
thedo 166:240bc5a0f42a 875 // Parse the configuration descriptor
thedo 166:240bc5a0f42a 876 parseConfDescr(dev, data, total_conf_descr_length, pEnumerator);
thedo 166:240bc5a0f42a 877
thedo 166:240bc5a0f42a 878 // only set configuration if not enumerated before
thedo 166:240bc5a0f42a 879 if (!dev->isEnumerated()) {
thedo 166:240bc5a0f42a 880
thedo 166:240bc5a0f42a 881 USB_DBG("Set configuration 1 on dev: %p", dev);
thedo 166:240bc5a0f42a 882 // sixth step: set configuration (only 1 supported)
thedo 166:240bc5a0f42a 883 res = setConfiguration(dev, 1);
thedo 166:240bc5a0f42a 884
thedo 166:240bc5a0f42a 885 if (res != USB_TYPE_OK) {
thedo 166:240bc5a0f42a 886 USB_DBG("SET CONF FAILED");
thedo 166:240bc5a0f42a 887 return res;
thedo 166:240bc5a0f42a 888 }
thedo 166:240bc5a0f42a 889 }
thedo 166:240bc5a0f42a 890
thedo 166:240bc5a0f42a 891 dev->setEnumerated();
thedo 166:240bc5a0f42a 892
thedo 166:240bc5a0f42a 893 // Now the device is enumerated!
thedo 166:240bc5a0f42a 894 USB_DBG("dev %p is enumerated\r\n", dev);
thedo 166:240bc5a0f42a 895
thedo 166:240bc5a0f42a 896 } while(0);
thedo 166:240bc5a0f42a 897
thedo 166:240bc5a0f42a 898 // Some devices may require this delay
thedo 166:240bc5a0f42a 899 Thread::wait(100);
thedo 166:240bc5a0f42a 900
thedo 166:240bc5a0f42a 901 return USB_TYPE_OK;
thedo 166:240bc5a0f42a 902 }
thedo 166:240bc5a0f42a 903 // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor.
thedo 166:240bc5a0f42a 904 void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator)
thedo 166:240bc5a0f42a 905 {
thedo 166:240bc5a0f42a 906 uint32_t index = 0;
thedo 166:240bc5a0f42a 907 uint32_t len_desc = 0;
thedo 166:240bc5a0f42a 908 uint8_t id = 0;
thedo 166:240bc5a0f42a 909 int nb_endpoints_used = 0;
thedo 166:240bc5a0f42a 910 USBEndpoint * ep = NULL;
thedo 166:240bc5a0f42a 911 uint8_t intf_nb = 0;
thedo 166:240bc5a0f42a 912 bool parsing_intf = false;
thedo 166:240bc5a0f42a 913 uint8_t current_intf = 0;
thedo 166:240bc5a0f42a 914
thedo 166:240bc5a0f42a 915 while (index < len) {
thedo 166:240bc5a0f42a 916 len_desc = conf_descr[index];
thedo 166:240bc5a0f42a 917 id = conf_descr[index+1];
thedo 166:240bc5a0f42a 918 switch (id) {
thedo 166:240bc5a0f42a 919 case CONFIGURATION_DESCRIPTOR:
thedo 166:240bc5a0f42a 920 USB_DBG("dev: %p has %d intf", dev, conf_descr[4]);
thedo 166:240bc5a0f42a 921 dev->setNbIntf(conf_descr[4]);
thedo 166:240bc5a0f42a 922 break;
thedo 166:240bc5a0f42a 923 case INTERFACE_DESCRIPTOR:
thedo 166:240bc5a0f42a 924 if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) {
thedo 166:240bc5a0f42a 925 if (intf_nb++ <= MAX_INTF) {
thedo 166:240bc5a0f42a 926 current_intf = conf_descr[index + 2];
thedo 166:240bc5a0f42a 927 dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]);
thedo 166:240bc5a0f42a 928 nb_endpoints_used = 0;
thedo 166:240bc5a0f42a 929 USB_DBG("ADD INTF %d on device %p: class: %d, subclass: %d, proto: %d", current_intf, dev, conf_descr[index + 5],conf_descr[index + 6],conf_descr[index + 7]);
thedo 166:240bc5a0f42a 930 } else {
thedo 166:240bc5a0f42a 931 USB_DBG("Drop intf...");
thedo 166:240bc5a0f42a 932 }
thedo 166:240bc5a0f42a 933 parsing_intf = true;
thedo 166:240bc5a0f42a 934 } else {
thedo 166:240bc5a0f42a 935 parsing_intf = false;
thedo 166:240bc5a0f42a 936 }
thedo 166:240bc5a0f42a 937 break;
thedo 166:240bc5a0f42a 938 case ENDPOINT_DESCRIPTOR:
thedo 166:240bc5a0f42a 939 if (parsing_intf && (intf_nb <= MAX_INTF) ) {
thedo 166:240bc5a0f42a 940 if (nb_endpoints_used < MAX_ENDPOINT_PER_INTERFACE) {
thedo 166:240bc5a0f42a 941 if( pEnumerator->useEndpoint(current_intf, (ENDPOINT_TYPE)(conf_descr[index + 3] & 0x03), (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1)) ) {
thedo 166:240bc5a0f42a 942 // if the USBEndpoint is isochronous -> skip it (TODO: fix this)
thedo 166:240bc5a0f42a 943 if ((conf_descr[index + 3] & 0x03) != ISOCHRONOUS_ENDPOINT) {
thedo 166:240bc5a0f42a 944 ep = newEndpoint((ENDPOINT_TYPE)(conf_descr[index+3] & 0x03),
thedo 166:240bc5a0f42a 945 (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1),
thedo 166:240bc5a0f42a 946 conf_descr[index + 4] | (conf_descr[index + 5] << 8),
thedo 166:240bc5a0f42a 947 conf_descr[index + 2] & 0x0f);
thedo 166:240bc5a0f42a 948 USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev);
thedo 166:240bc5a0f42a 949 if (ep != NULL && dev != NULL) {
thedo 166:240bc5a0f42a 950 addEndpoint(dev, current_intf, ep);
thedo 166:240bc5a0f42a 951 } else {
thedo 166:240bc5a0f42a 952 USB_DBG("EP NULL");
thedo 166:240bc5a0f42a 953 }
thedo 166:240bc5a0f42a 954 nb_endpoints_used++;
thedo 166:240bc5a0f42a 955 } else {
thedo 166:240bc5a0f42a 956 USB_DBG("ISO USBEndpoint NOT SUPPORTED");
thedo 166:240bc5a0f42a 957 }
thedo 166:240bc5a0f42a 958 }
thedo 166:240bc5a0f42a 959 }
thedo 166:240bc5a0f42a 960 }
thedo 166:240bc5a0f42a 961 break;
thedo 166:240bc5a0f42a 962 case HID_DESCRIPTOR:
thedo 166:240bc5a0f42a 963 lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8);
thedo 166:240bc5a0f42a 964 break;
thedo 166:240bc5a0f42a 965 default:
thedo 166:240bc5a0f42a 966 break;
thedo 166:240bc5a0f42a 967 }
thedo 166:240bc5a0f42a 968 index += len_desc;
thedo 166:240bc5a0f42a 969 }
thedo 166:240bc5a0f42a 970 }
thedo 166:240bc5a0f42a 971
thedo 166:240bc5a0f42a 972
thedo 166:240bc5a0f42a 973 USB_TYPE USBHost::bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
thedo 166:240bc5a0f42a 974 {
thedo 166:240bc5a0f42a 975 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, true);
thedo 166:240bc5a0f42a 976 }
thedo 166:240bc5a0f42a 977
thedo 166:240bc5a0f42a 978 USB_TYPE USBHost::bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
thedo 166:240bc5a0f42a 979 {
thedo 166:240bc5a0f42a 980 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, false);
thedo 166:240bc5a0f42a 981 }
thedo 166:240bc5a0f42a 982
thedo 166:240bc5a0f42a 983 USB_TYPE USBHost::interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
thedo 166:240bc5a0f42a 984 {
thedo 166:240bc5a0f42a 985 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, true);
thedo 166:240bc5a0f42a 986 }
thedo 166:240bc5a0f42a 987
thedo 166:240bc5a0f42a 988 USB_TYPE USBHost::interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
thedo 166:240bc5a0f42a 989 {
thedo 166:240bc5a0f42a 990 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false);
thedo 166:240bc5a0f42a 991 }
thedo 166:240bc5a0f42a 992
thedo 166:240bc5a0f42a 993 USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write) {
thedo 166:240bc5a0f42a 994
thedo 166:240bc5a0f42a 995 #if DEBUG_TRANSFER
thedo 166:240bc5a0f42a 996 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS");
thedo 166:240bc5a0f42a 997 USB_DBG_TRANSFER("----- %s %s [dev: %p - %s - hub: %d - port: %d - addr: %d - ep: %02X]------", type_str, (write) ? "WRITE" : "READ", dev, dev->getName(ep->getIntfNb()), dev->getHub(), dev->getPort(), dev->getAddress(), ep->getAddress());
thedo 166:240bc5a0f42a 998 #endif
thedo 166:240bc5a0f42a 999
thedo 166:240bc5a0f42a 1000 Lock lock(this);
thedo 166:240bc5a0f42a 1001
thedo 166:240bc5a0f42a 1002 USB_TYPE res;
thedo 166:240bc5a0f42a 1003 ENDPOINT_DIRECTION dir = (write) ? OUT : IN;
thedo 166:240bc5a0f42a 1004
thedo 166:240bc5a0f42a 1005 if (dev == NULL) {
thedo 166:240bc5a0f42a 1006 USB_ERR("dev NULL");
thedo 166:240bc5a0f42a 1007 return USB_TYPE_ERROR;
thedo 166:240bc5a0f42a 1008 }
thedo 166:240bc5a0f42a 1009
thedo 166:240bc5a0f42a 1010 if (ep == NULL) {
thedo 166:240bc5a0f42a 1011 USB_ERR("ep NULL");
thedo 166:240bc5a0f42a 1012 return USB_TYPE_ERROR;
thedo 166:240bc5a0f42a 1013 }
thedo 166:240bc5a0f42a 1014
thedo 166:240bc5a0f42a 1015 if (ep->getState() != USB_TYPE_IDLE) {
thedo 166:240bc5a0f42a 1016 USB_WARN("[ep: %p - dev: %p - %s] NOT IDLE: %s", ep, ep->dev, ep->dev->getName(ep->getIntfNb()), ep->getStateString());
thedo 166:240bc5a0f42a 1017 return ep->getState();
thedo 166:240bc5a0f42a 1018 }
thedo 166:240bc5a0f42a 1019
thedo 166:240bc5a0f42a 1020 if ((ep->getDir() != dir) || (ep->getType() != type)) {
thedo 166:240bc5a0f42a 1021 USB_ERR("[ep: %p - dev: %p] wrong dir or bad USBEndpoint type", ep, ep->dev);
thedo 166:240bc5a0f42a 1022 return USB_TYPE_ERROR;
thedo 166:240bc5a0f42a 1023 }
thedo 166:240bc5a0f42a 1024
thedo 166:240bc5a0f42a 1025 if (dev->getAddress() != ep->getDeviceAddress()) {
thedo 166:240bc5a0f42a 1026 USB_ERR("[ep: %p - dev: %p] USBEndpoint addr and device addr don't match", ep, ep->dev);
thedo 166:240bc5a0f42a 1027 return USB_TYPE_ERROR;
thedo 166:240bc5a0f42a 1028 }
thedo 166:240bc5a0f42a 1029
thedo 166:240bc5a0f42a 1030 #if DEBUG_TRANSFER
thedo 166:240bc5a0f42a 1031 if (write) {
thedo 166:240bc5a0f42a 1032 USB_DBG_TRANSFER("%s WRITE buffer", type_str);
thedo 166:240bc5a0f42a 1033 for (int i = 0; i < ep->getLengthTransferred(); i++)
thedo 166:240bc5a0f42a 1034 printf("%02X ", buf[i]);
thedo 166:240bc5a0f42a 1035 printf("\r\n\r\n");
thedo 166:240bc5a0f42a 1036 }
thedo 166:240bc5a0f42a 1037 #endif
thedo 166:240bc5a0f42a 1038 addTransfer(ep, buf, len);
thedo 166:240bc5a0f42a 1039
thedo 166:240bc5a0f42a 1040 if (blocking) {
thedo 166:240bc5a0f42a 1041
thedo 166:240bc5a0f42a 1042 ep->ep_queue.get();
thedo 166:240bc5a0f42a 1043 res = ep->getState();
thedo 166:240bc5a0f42a 1044
thedo 166:240bc5a0f42a 1045 USB_DBG_TRANSFER("%s TRANSFER res: %s on ep: %p\r\n", type_str, ep->getStateString(), ep);
thedo 166:240bc5a0f42a 1046
thedo 166:240bc5a0f42a 1047 if (res != USB_TYPE_IDLE) {
thedo 166:240bc5a0f42a 1048 return res;
thedo 166:240bc5a0f42a 1049 }
thedo 166:240bc5a0f42a 1050
thedo 166:240bc5a0f42a 1051 return USB_TYPE_OK;
thedo 166:240bc5a0f42a 1052 }
thedo 166:240bc5a0f42a 1053
thedo 166:240bc5a0f42a 1054 return USB_TYPE_PROCESSING;
thedo 166:240bc5a0f42a 1055
thedo 166:240bc5a0f42a 1056 }
thedo 166:240bc5a0f42a 1057
thedo 166:240bc5a0f42a 1058
thedo 166:240bc5a0f42a 1059 USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
thedo 166:240bc5a0f42a 1060 return controlTransfer(dev, requestType, request, value, index, buf, len, false);
thedo 166:240bc5a0f42a 1061 }
thedo 166:240bc5a0f42a 1062
thedo 166:240bc5a0f42a 1063 USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
thedo 166:240bc5a0f42a 1064 return controlTransfer(dev, requestType, request, value, index, buf, len, true);
thedo 166:240bc5a0f42a 1065 }
thedo 166:240bc5a0f42a 1066
thedo 166:240bc5a0f42a 1067 USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len, bool write)
thedo 166:240bc5a0f42a 1068 {
thedo 166:240bc5a0f42a 1069 Lock lock(this);
thedo 166:240bc5a0f42a 1070 USB_DBG_TRANSFER("----- CONTROL %s [dev: %p - hub: %d - port: %d] ------", (write) ? "WRITE" : "READ", dev, dev->getHub(), dev->getPort());
thedo 166:240bc5a0f42a 1071
thedo 166:240bc5a0f42a 1072 int length_transfer = len;
thedo 166:240bc5a0f42a 1073 USB_TYPE res;
thedo 166:240bc5a0f42a 1074 uint32_t token;
thedo 166:240bc5a0f42a 1075
thedo 166:240bc5a0f42a 1076 control->setSpeed(dev->getSpeed());
thedo 166:240bc5a0f42a 1077 control->setSize(dev->getSizeControlEndpoint());
thedo 166:240bc5a0f42a 1078 if (dev->isActiveAddress()) {
thedo 166:240bc5a0f42a 1079 control->setDeviceAddress(dev->getAddress());
thedo 166:240bc5a0f42a 1080 } else {
thedo 166:240bc5a0f42a 1081 control->setDeviceAddress(0);
thedo 166:240bc5a0f42a 1082 }
thedo 166:240bc5a0f42a 1083
thedo 166:240bc5a0f42a 1084 USB_DBG_TRANSFER("Control transfer on device: %d\r\n", control->getDeviceAddress());
thedo 166:240bc5a0f42a 1085 fillControlBuf(requestType, request, value, index, len);
thedo 166:240bc5a0f42a 1086
thedo 166:240bc5a0f42a 1087 #if DEBUG_TRANSFER
thedo 166:240bc5a0f42a 1088 USB_DBG_TRANSFER("SETUP PACKET: ");
thedo 166:240bc5a0f42a 1089 for (int i = 0; i < 8; i++)
thedo 166:240bc5a0f42a 1090 printf("%01X ", setupPacket[i]);
thedo 166:240bc5a0f42a 1091 printf("\r\n");
thedo 166:240bc5a0f42a 1092 #endif
thedo 166:240bc5a0f42a 1093
thedo 166:240bc5a0f42a 1094 control->setNextToken(TD_SETUP);
thedo 166:240bc5a0f42a 1095 addTransfer(control, (uint8_t*)setupPacket, 8);
thedo 166:240bc5a0f42a 1096
thedo 166:240bc5a0f42a 1097 control->ep_queue.get();
thedo 166:240bc5a0f42a 1098 res = control->getState();
thedo 166:240bc5a0f42a 1099
thedo 166:240bc5a0f42a 1100 USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString());
thedo 166:240bc5a0f42a 1101
thedo 166:240bc5a0f42a 1102 if (res != USB_TYPE_IDLE) {
thedo 166:240bc5a0f42a 1103 return res;
thedo 166:240bc5a0f42a 1104 }
thedo 166:240bc5a0f42a 1105
thedo 166:240bc5a0f42a 1106 if (length_transfer) {
thedo 166:240bc5a0f42a 1107 token = (write) ? TD_OUT : TD_IN;
thedo 166:240bc5a0f42a 1108 control->setNextToken(token);
thedo 166:240bc5a0f42a 1109 addTransfer(control, (uint8_t *)buf, length_transfer);
thedo 166:240bc5a0f42a 1110
thedo 166:240bc5a0f42a 1111 control->ep_queue.get();
thedo 166:240bc5a0f42a 1112 res = control->getState();
thedo 166:240bc5a0f42a 1113
thedo 166:240bc5a0f42a 1114 #if DEBUG_TRANSFER
thedo 166:240bc5a0f42a 1115 USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString());
thedo 166:240bc5a0f42a 1116 if (write) {
thedo 166:240bc5a0f42a 1117 USB_DBG_TRANSFER("CONTROL WRITE buffer");
thedo 166:240bc5a0f42a 1118 for (int i = 0; i < control->getLengthTransferred(); i++)
thedo 166:240bc5a0f42a 1119 printf("%02X ", buf[i]);
thedo 166:240bc5a0f42a 1120 printf("\r\n\r\n");
thedo 166:240bc5a0f42a 1121 } else {
thedo 166:240bc5a0f42a 1122 USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred());
thedo 166:240bc5a0f42a 1123 for (int i = 0; i < control->getLengthTransferred(); i++)
thedo 166:240bc5a0f42a 1124 printf("%02X ", buf[i]);
thedo 166:240bc5a0f42a 1125 printf("\r\n\r\n");
thedo 166:240bc5a0f42a 1126 }
thedo 166:240bc5a0f42a 1127 #endif
thedo 166:240bc5a0f42a 1128
thedo 166:240bc5a0f42a 1129 if (res != USB_TYPE_IDLE) {
thedo 166:240bc5a0f42a 1130 return res;
thedo 166:240bc5a0f42a 1131 }
thedo 166:240bc5a0f42a 1132 }
thedo 166:240bc5a0f42a 1133
thedo 166:240bc5a0f42a 1134 token = (write) ? TD_IN : TD_OUT;
thedo 166:240bc5a0f42a 1135 control->setNextToken(token);
thedo 166:240bc5a0f42a 1136 addTransfer(control, NULL, 0);
thedo 166:240bc5a0f42a 1137
thedo 166:240bc5a0f42a 1138 control->ep_queue.get();
thedo 166:240bc5a0f42a 1139 res = control->getState();
thedo 166:240bc5a0f42a 1140
thedo 166:240bc5a0f42a 1141 USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString());
thedo 166:240bc5a0f42a 1142
thedo 166:240bc5a0f42a 1143 if (res != USB_TYPE_IDLE)
thedo 166:240bc5a0f42a 1144 return res;
thedo 166:240bc5a0f42a 1145
thedo 166:240bc5a0f42a 1146 return USB_TYPE_OK;
thedo 166:240bc5a0f42a 1147 }
thedo 166:240bc5a0f42a 1148
thedo 166:240bc5a0f42a 1149
thedo 166:240bc5a0f42a 1150 void USBHost::fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len)
thedo 166:240bc5a0f42a 1151 {
thedo 166:240bc5a0f42a 1152 setupPacket[0] = requestType;
thedo 166:240bc5a0f42a 1153 setupPacket[1] = request;
thedo 166:240bc5a0f42a 1154 setupPacket[2] = (uint8_t) value;
thedo 166:240bc5a0f42a 1155 setupPacket[3] = (uint8_t) (value >> 8);
thedo 166:240bc5a0f42a 1156 setupPacket[4] = (uint8_t) index;
thedo 166:240bc5a0f42a 1157 setupPacket[5] = (uint8_t) (index >> 8);
thedo 166:240bc5a0f42a 1158 setupPacket[6] = (uint8_t) len;
thedo 166:240bc5a0f42a 1159 setupPacket[7] = (uint8_t) (len >> 8);
thedo 166:240bc5a0f42a 1160 }