test public
Dependencies: HttpServer_snapshot_mbed-os
USBHost.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 00018 #include "USBHost.h" 00019 #include "USBHostHub.h" 00020 00021 USBHost * USBHost::instHost = NULL; 00022 00023 #define DEVICE_CONNECTED_EVENT (1 << 0) 00024 #define DEVICE_DISCONNECTED_EVENT (1 << 1) 00025 #define TD_PROCESSED_EVENT (1 << 2) 00026 00027 #define MAX_TRY_ENUMERATE_HUB 3 00028 00029 #define MIN(a, b) ((a > b) ? b : a) 00030 00031 /** 00032 * How interrupts are processed: 00033 * - new device connected: 00034 * - a message is queued in queue_usb_event with the id DEVICE_CONNECTED_EVENT 00035 * - when the usb_thread receives the event, it: 00036 * - resets the device 00037 * - reads the device descriptor 00038 * - sets the address of the device 00039 * - if it is a hub, enumerates it 00040 * - device disconnected: 00041 * - a message is queued in queue_usb_event with the id DEVICE_DISCONNECTED_EVENT 00042 * - when the usb_thread receives the event, it: 00043 * - free the device and all its children (hub) 00044 * - td processed 00045 * - a message is queued in queue_usb_event with the id TD_PROCESSED_EVENT 00046 * - when the usb_thread receives the event, it: 00047 * - call the callback attached to the endpoint where the td is attached 00048 */ 00049 void USBHost::usb_process() { 00050 00051 bool controlListState; 00052 bool bulkListState; 00053 bool interruptListState; 00054 USBEndpoint * ep; 00055 uint8_t i, j, res, timeout_set_addr = 10; 00056 #if defined(TARGET_RZ_A2XX) 00057 uint8_t * buf = (uint8_t *)AllocNonCacheMem(8); 00058 #else 00059 uint8_t buf[8]; 00060 #endif 00061 bool too_many_hub; 00062 int idx; 00063 00064 #if DEBUG_TRANSFER 00065 uint8_t * buf_transfer; 00066 #endif 00067 00068 #if MAX_HUB_NB 00069 uint8_t k; 00070 #endif 00071 00072 while(1) { 00073 osEvent evt = mail_usb_event.get(); 00074 00075 if (evt.status == osEventMail) { 00076 00077 message_t * usb_msg = (message_t*)evt.value.p; 00078 00079 switch (usb_msg->event_id) { 00080 00081 // a new device has been connected 00082 case DEVICE_CONNECTED_EVENT: 00083 too_many_hub = false; 00084 buf[4] = 0; 00085 00086 do 00087 { 00088 Lock lock(this); 00089 00090 for (i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00091 if (!deviceInUse[i]) { 00092 USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]); 00093 devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed); 00094 deviceReset[i] = false; 00095 deviceInited[i] = true; 00096 break; 00097 } 00098 } 00099 00100 if (i == MAX_DEVICE_CONNECTED) { 00101 USB_ERR("Too many device connected!!\r\n"); 00102 continue; 00103 } 00104 00105 if (!controlEndpointAllocated) { 00106 control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00); 00107 addEndpoint(NULL, 0, (USBEndpoint*)control); 00108 controlEndpointAllocated = true; 00109 } 00110 00111 #if MAX_HUB_NB 00112 if (usb_msg->hub_parent) 00113 devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent)); 00114 #endif 00115 00116 for (j = 0; j < timeout_set_addr; j++) { 00117 00118 resetDevice(&devices[i]); 00119 00120 // set size of control endpoint 00121 devices[i].setSizeControlEndpoint(8); 00122 00123 devices[i].activeAddress(false); 00124 00125 // get first 8 bit of device descriptor 00126 // and check if we deal with a hub 00127 USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]); 00128 res = getDeviceDescriptor(&devices[i], buf, 8); 00129 00130 if (res != USB_TYPE_OK) { 00131 USB_ERR("usb_thread could not read dev descr"); 00132 continue; 00133 } 00134 00135 // set size of control endpoint 00136 devices[i].setSizeControlEndpoint(buf[7]); 00137 00138 // second step: set an address to the device 00139 res = setAddress(&devices[i], devices[i].getAddress()); 00140 00141 if (res != USB_TYPE_OK) { 00142 USB_ERR("SET ADDR FAILED"); 00143 continue; 00144 } 00145 devices[i].activeAddress(true); 00146 USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress()); 00147 00148 // try to read again the device descriptor to check if the device 00149 // answers to its new address 00150 res = getDeviceDescriptor(&devices[i], buf, 8); 00151 00152 if (res == USB_TYPE_OK) { 00153 break; 00154 } 00155 00156 ThisThread::sleep_for(100); 00157 } 00158 00159 USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port); 00160 00161 #if MAX_HUB_NB 00162 if (buf[4] == HUB_CLASS) { 00163 for (k = 0; k < MAX_HUB_NB; k++) { 00164 if (hub_in_use[k] == false) { 00165 for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) { 00166 if (hubs[k].connect(&devices[i])) { 00167 devices[i].hub = &hubs[k]; 00168 hub_in_use[k] = true; 00169 break; 00170 } 00171 } 00172 if (hub_in_use[k] == true) 00173 break; 00174 } 00175 } 00176 00177 if (k == MAX_HUB_NB) { 00178 USB_ERR("Too many hubs connected!!\r\n"); 00179 too_many_hub = true; 00180 } 00181 } 00182 00183 if (usb_msg->hub_parent) 00184 ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]); 00185 #endif 00186 00187 if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) { 00188 deviceInUse[i] = true; 00189 } 00190 00191 } while(0); 00192 00193 break; 00194 00195 // a device has been disconnected 00196 case DEVICE_DISCONNECTED_EVENT: 00197 00198 do 00199 { 00200 Lock lock(this); 00201 00202 controlListState = disableList(CONTROL_ENDPOINT); 00203 bulkListState = disableList(BULK_ENDPOINT); 00204 interruptListState = disableList(INTERRUPT_ENDPOINT); 00205 00206 idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent)); 00207 if (idx != -1) { 00208 freeDevice((USBDeviceConnected*)&devices[idx]); 00209 } 00210 00211 if (controlListState) enableList(CONTROL_ENDPOINT); 00212 if (bulkListState) enableList(BULK_ENDPOINT); 00213 if (interruptListState) enableList(INTERRUPT_ENDPOINT); 00214 00215 } while(0); 00216 00217 break; 00218 00219 // a td has been processed 00220 // call callback on the ed associated to the td 00221 // we are not in ISR -> users can use printf in their callback method 00222 case TD_PROCESSED_EVENT: 00223 ep = (USBEndpoint *) ((HCTD *)usb_msg->td_addr)->ep; 00224 if (usb_msg->td_state == USB_TYPE_IDLE) { 00225 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())); 00226 00227 #if DEBUG_TRANSFER 00228 if (ep->getDir() == IN) { 00229 buf_transfer = ep->getBufStart(); 00230 printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress()); 00231 for (int i = 0; i < ep->getLengthTransferred(); i++) 00232 printf("%02X ", buf_transfer[i]); 00233 printf("\r\n\r\n"); 00234 } 00235 #endif 00236 ep->call(); 00237 } else { 00238 idx = findDevice(ep->dev); 00239 if (idx != -1) { 00240 if (deviceInUse[idx]) { 00241 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())); 00242 ep->setState(USB_TYPE_IDLE); 00243 } 00244 } 00245 } 00246 break; 00247 } 00248 00249 mail_usb_event.free(usb_msg); 00250 } 00251 } 00252 } 00253 00254 USBHost::USBHost() : usbThread(osPriorityNormal, USB_THREAD_STACK) 00255 { 00256 #ifndef USBHOST_OTHER 00257 headControlEndpoint = NULL; 00258 headBulkEndpoint = NULL; 00259 headInterruptEndpoint = NULL; 00260 tailControlEndpoint = NULL; 00261 tailBulkEndpoint = NULL; 00262 tailInterruptEndpoint = NULL; 00263 #endif 00264 lenReportDescr = 0; 00265 00266 controlEndpointAllocated = false; 00267 00268 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00269 deviceInUse[i] = false; 00270 devices[i].setAddress(i + 1); 00271 deviceReset[i] = false; 00272 deviceInited[i] = false; 00273 for (uint8_t j = 0; j < MAX_INTF; j++) 00274 deviceAttachedDriver[i][j] = false; 00275 } 00276 00277 #if MAX_HUB_NB 00278 for (uint8_t i = 0; i < MAX_HUB_NB; i++) { 00279 hubs[i].setHost(this); 00280 hub_in_use[i] = false; 00281 } 00282 #endif 00283 00284 #if defined(TARGET_RZ_A2XX) 00285 setupPacket = (uint8_t *)AllocNonCacheMem(8); 00286 data = (uint8_t *)AllocNonCacheMem(415); 00287 #endif 00288 usbThread.start(callback(this, &USBHost::usb_process)); 00289 } 00290 00291 USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost) 00292 { 00293 m_pHost->usb_mutex.lock(); 00294 } 00295 00296 USBHost::Lock::~Lock() 00297 { 00298 m_pHost->usb_mutex.unlock(); 00299 } 00300 00301 void USBHost::transferCompleted(volatile uint32_t addr) 00302 { 00303 uint8_t state; 00304 00305 if(addr == 0) 00306 return; 00307 00308 volatile HCTD* tdList = NULL; 00309 00310 //First we must reverse the list order and dequeue each TD 00311 do { 00312 volatile HCTD* td = (volatile HCTD*)addr; 00313 addr = (uint32_t)td->nextTD; //Dequeue from physical list 00314 td->nextTD = (hcTd*)tdList; //Enqueue into reversed list 00315 tdList = td; 00316 } while(addr); 00317 00318 while(tdList != NULL) { 00319 volatile HCTD* td = tdList; 00320 tdList = (volatile HCTD*)td->nextTD; //Dequeue element now as it could be modified below 00321 if (td->ep != NULL) { 00322 USBEndpoint * ep = (USBEndpoint *)(td->ep); 00323 00324 #ifdef USBHOST_OTHER 00325 state = ((HCTD *)td)->state; 00326 if (state == USB_TYPE_IDLE) 00327 ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); 00328 00329 #else 00330 if (((HCTD *)td)->control >> 28) { 00331 state = ((HCTD *)td)->control >> 28; 00332 } else { 00333 if (td->currBufPtr) { 00334 ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); 00335 } 00336 state = 16 /*USB_TYPE_IDLE*/; 00337 } 00338 #endif 00339 00340 ep->unqueueTransfer(td); 00341 00342 if (ep->getType() != CONTROL_ENDPOINT) { 00343 // callback on the processed td will be called from the usb_thread (not in ISR) 00344 message_t * usb_msg = mail_usb_event.alloc(); 00345 usb_msg->event_id = TD_PROCESSED_EVENT; 00346 usb_msg->td_addr = (void *)td; 00347 usb_msg->td_state = state; 00348 mail_usb_event.put(usb_msg); 00349 } 00350 ep->setState(state); 00351 ep->ep_queue.put((uint8_t*)1); 00352 } 00353 } 00354 } 00355 00356 USBHost * USBHost::getHostInst() 00357 { 00358 if (instHost == NULL) { 00359 instHost = new USBHost(); 00360 instHost->init(); 00361 } 00362 return instHost; 00363 } 00364 00365 00366 /* 00367 * Called when a device has been connected 00368 * Called in ISR!!!! (no printf) 00369 */ 00370 /* virtual */ void USBHost::deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent) 00371 { 00372 // be sure that the new device connected is not already connected... 00373 int idx = findDevice(hub, port, hub_parent); 00374 if (idx != -1) { 00375 if (deviceInited[idx]) 00376 return; 00377 } 00378 00379 message_t * usb_msg = mail_usb_event.alloc(); 00380 usb_msg->event_id = DEVICE_CONNECTED_EVENT; 00381 usb_msg->hub = hub; 00382 usb_msg->port = port; 00383 usb_msg->lowSpeed = lowSpeed; 00384 usb_msg->hub_parent = hub_parent; 00385 mail_usb_event.put(usb_msg); 00386 } 00387 00388 /* 00389 * Called when a device has been disconnected 00390 * Called in ISR!!!! (no printf) 00391 */ 00392 /* virtual */ void USBHost::deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr) 00393 { 00394 // be sure that the device disconnected is connected... 00395 int idx = findDevice(hub, port, hub_parent); 00396 if (idx != -1) { 00397 if (!deviceInUse[idx]) 00398 return; 00399 } else { 00400 return; 00401 } 00402 00403 message_t * usb_msg = mail_usb_event.alloc(); 00404 usb_msg->event_id = DEVICE_DISCONNECTED_EVENT; 00405 usb_msg->hub = hub; 00406 usb_msg->port = port; 00407 usb_msg->hub_parent = hub_parent; 00408 mail_usb_event.put(usb_msg); 00409 } 00410 00411 void USBHost::freeDevice(USBDeviceConnected * dev) 00412 { 00413 USBEndpoint * ep = NULL; 00414 HCED * ed = NULL; 00415 00416 #if MAX_HUB_NB 00417 if (dev->getClass() == HUB_CLASS) { 00418 if (dev->hub == NULL) { 00419 USB_ERR("HUB NULL!!!!!\r\n"); 00420 } else { 00421 dev->hub->hubDisconnected(); 00422 for (uint8_t i = 0; i < MAX_HUB_NB; i++) { 00423 if (dev->hub == &hubs[i]) { 00424 hub_in_use[i] = false; 00425 break; 00426 } 00427 } 00428 } 00429 } 00430 00431 // notify hub parent that this device has been disconnected 00432 if (dev->getHubParent()) 00433 dev->getHubParent()->deviceDisconnected(dev); 00434 00435 #endif 00436 00437 int idx = findDevice(dev); 00438 if (idx != -1) { 00439 deviceInUse[idx] = false; 00440 deviceReset[idx] = false; 00441 00442 for (uint8_t j = 0; j < MAX_INTF; j++) { 00443 deviceAttachedDriver[idx][j] = false; 00444 if (dev->getInterface(j) != NULL) { 00445 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)); 00446 for (int i = 0; i < dev->getInterface(j)->nb_endpoint; i++) { 00447 if ((ep = dev->getEndpoint(j, i)) != NULL) { 00448 #ifndef USBHOST_OTHER 00449 ed = (HCED *)ep->getHCED(); 00450 ed->control |= (1 << 14); //sKip bit 00451 #endif 00452 unqueueEndpoint(ep); 00453 00454 freeTD((volatile uint8_t*)ep->getTDList()[0]); 00455 freeTD((volatile uint8_t*)ep->getTDList()[1]); 00456 00457 freeED((uint8_t *)ep->getHCED()); 00458 } 00459 printList(BULK_ENDPOINT); 00460 printList(INTERRUPT_ENDPOINT); 00461 } 00462 USB_INFO("Device disconnected [%p - %s - hub: %d - port: %d]", dev, dev->getName(j), dev->getHub(), dev->getPort()); 00463 } 00464 } 00465 dev->disconnect(); 00466 } 00467 } 00468 00469 00470 void USBHost::unqueueEndpoint(USBEndpoint * ep) 00471 { 00472 #ifdef USBHOST_OTHER 00473 ep->setState(USB_TYPE_FREE); 00474 #else 00475 USBEndpoint * prec = NULL; 00476 USBEndpoint * current = NULL; 00477 00478 for (int i = 0; i < 2; i++) { 00479 current = (i == 0) ? (USBEndpoint*)headBulkEndpoint : (USBEndpoint*)headInterruptEndpoint; 00480 prec = current; 00481 while (current != NULL) { 00482 if (current == ep) { 00483 if (current->nextEndpoint() != NULL) { 00484 prec->queueEndpoint(current->nextEndpoint()); 00485 if (current == headBulkEndpoint) { 00486 updateBulkHeadED((uint32_t)current->nextEndpoint()->getHCED()); 00487 headBulkEndpoint = current->nextEndpoint(); 00488 } else if (current == headInterruptEndpoint) { 00489 updateInterruptHeadED((uint32_t)current->nextEndpoint()->getHCED()); 00490 headInterruptEndpoint = current->nextEndpoint(); 00491 } 00492 } 00493 // here we are dequeuing the queue of ed 00494 // we need to update the tail pointer 00495 else { 00496 prec->queueEndpoint(NULL); 00497 if (current == headBulkEndpoint) { 00498 updateBulkHeadED(0); 00499 headBulkEndpoint = current->nextEndpoint(); 00500 } else if (current == headInterruptEndpoint) { 00501 updateInterruptHeadED(0); 00502 headInterruptEndpoint = current->nextEndpoint(); 00503 } 00504 00505 // modify tail 00506 switch (current->getType()) { 00507 case BULK_ENDPOINT: 00508 tailBulkEndpoint = prec; 00509 break; 00510 case INTERRUPT_ENDPOINT: 00511 tailInterruptEndpoint = prec; 00512 break; 00513 default: 00514 break; 00515 } 00516 } 00517 current->setState(USB_TYPE_FREE); 00518 return; 00519 } 00520 prec = current; 00521 current = current->nextEndpoint(); 00522 } 00523 } 00524 #endif 00525 } 00526 00527 00528 USBDeviceConnected * USBHost::getDevice(uint8_t index) 00529 { 00530 if ((index >= MAX_DEVICE_CONNECTED) || (!deviceInUse[index])) { 00531 return NULL; 00532 } 00533 return (USBDeviceConnected*)&devices[index]; 00534 } 00535 00536 // create an USBEndpoint descriptor. the USBEndpoint is not linked 00537 USBEndpoint * USBHost::newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr) 00538 { 00539 int i = 0; 00540 HCED * ed = (HCED *)getED(); 00541 HCTD* td_list[2] = { (HCTD*)getTD(), (HCTD*)getTD() }; 00542 00543 memset((void *)td_list[0], 0x00, sizeof(HCTD)); 00544 memset((void *)td_list[1], 0x00, sizeof(HCTD)); 00545 00546 // search a free USBEndpoint 00547 for (i = 0; i < MAX_ENDPOINT; i++) { 00548 if (endpoints[i].getState() == USB_TYPE_FREE) { 00549 endpoints[i].init(ed, type, dir, size, addr, td_list); 00550 USB_DBG("USBEndpoint created (%p): type: %d, dir: %d, size: %d, addr: %d, state: %s", &endpoints[i], type, dir, size, addr, endpoints[i].getStateString()); 00551 return &endpoints[i]; 00552 } 00553 } 00554 USB_ERR("could not allocate more endpoints!!!!"); 00555 return NULL; 00556 } 00557 00558 00559 USB_TYPE USBHost::resetDevice(USBDeviceConnected * dev) 00560 { 00561 int index = findDevice(dev); 00562 if (index != -1) { 00563 USB_DBG("Resetting hub %d, port %d\n", dev->getHub(), dev->getPort()); 00564 ThisThread::sleep_for(100); 00565 if (dev->getHub() == 0) { 00566 resetRootHub(); 00567 } 00568 #if MAX_HUB_NB 00569 else { 00570 dev->getHubParent()->portReset(dev->getPort()); 00571 } 00572 #endif 00573 ThisThread::sleep_for(100); 00574 deviceReset[index] = true; 00575 return USB_TYPE_OK; 00576 } 00577 00578 return USB_TYPE_ERROR; 00579 } 00580 00581 // link the USBEndpoint to the linked list and attach an USBEndpoint to a device 00582 bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep) 00583 { 00584 00585 if (ep == NULL) { 00586 return false; 00587 } 00588 00589 #ifndef USBHOST_OTHER 00590 HCED * prevEd; 00591 00592 #endif 00593 // set device address in the USBEndpoint descriptor 00594 if (dev == NULL) { 00595 ep->setDeviceAddress(0); 00596 } else { 00597 ep->setDeviceAddress(dev->getAddress()); 00598 } 00599 00600 if ((dev != NULL) && dev->getSpeed()) { 00601 ep->setSpeed(dev->getSpeed()); 00602 } 00603 00604 ep->setIntfNb(intf_nb); 00605 00606 #ifndef USBHOST_OTHER 00607 // queue the new USBEndpoint on the ED list 00608 switch (ep->getType()) { 00609 00610 case CONTROL_ENDPOINT: 00611 prevEd = ( HCED*) controlHeadED(); 00612 if (!prevEd) { 00613 updateControlHeadED((uint32_t) ep->getHCED()); 00614 USB_DBG_TRANSFER("First control USBEndpoint: %08X", (uint32_t) ep->getHCED()); 00615 headControlEndpoint = ep; 00616 tailControlEndpoint = ep; 00617 return true; 00618 } 00619 tailControlEndpoint->queueEndpoint(ep); 00620 tailControlEndpoint = ep; 00621 return true; 00622 00623 case BULK_ENDPOINT: 00624 prevEd = ( HCED*) bulkHeadED(); 00625 if (!prevEd) { 00626 updateBulkHeadED((uint32_t) ep->getHCED()); 00627 USB_DBG_TRANSFER("First bulk USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED()); 00628 headBulkEndpoint = ep; 00629 tailBulkEndpoint = ep; 00630 break; 00631 } 00632 USB_DBG_TRANSFER("Queue BULK Ed %p after %p\r\n",ep->getHCED(), prevEd); 00633 tailBulkEndpoint->queueEndpoint(ep); 00634 tailBulkEndpoint = ep; 00635 break; 00636 00637 case INTERRUPT_ENDPOINT: 00638 prevEd = ( HCED*) interruptHeadED(); 00639 if (!prevEd) { 00640 updateInterruptHeadED((uint32_t) ep->getHCED()); 00641 USB_DBG_TRANSFER("First interrupt USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED()); 00642 headInterruptEndpoint = ep; 00643 tailInterruptEndpoint = ep; 00644 break; 00645 } 00646 USB_DBG_TRANSFER("Queue INTERRUPT Ed %p after %p\r\n",ep->getHCED(), prevEd); 00647 tailInterruptEndpoint->queueEndpoint(ep); 00648 tailInterruptEndpoint = ep; 00649 break; 00650 default: 00651 return false; 00652 } 00653 00654 #endif 00655 ep->dev = dev; 00656 dev->addEndpoint(intf_nb, ep); 00657 00658 return true; 00659 } 00660 00661 00662 int USBHost::findDevice(USBDeviceConnected * dev) 00663 { 00664 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00665 if (dev == &devices[i]) { 00666 return i; 00667 } 00668 } 00669 return -1; 00670 } 00671 00672 int USBHost::findDevice(uint8_t hub, uint8_t port, USBHostHub * hub_parent) 00673 { 00674 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00675 if (devices[i].getHub() == hub && devices[i].getPort() == port) { 00676 if (hub_parent != NULL) { 00677 if (hub_parent == devices[i].getHubParent()) 00678 return i; 00679 } else { 00680 return i; 00681 } 00682 } 00683 } 00684 return -1; 00685 } 00686 00687 void USBHost::printList(ENDPOINT_TYPE type) 00688 { 00689 #if (DEBUG_EP_STATE) && !defined(USBHOST_OTHER) 00690 volatile HCED * hced; 00691 switch(type) { 00692 case CONTROL_ENDPOINT: 00693 hced = (HCED *)controlHeadED(); 00694 break; 00695 case BULK_ENDPOINT: 00696 hced = (HCED *)bulkHeadED(); 00697 break; 00698 case INTERRUPT_ENDPOINT: 00699 hced = (HCED *)interruptHeadED(); 00700 break; 00701 } 00702 volatile HCTD * hctd = NULL; 00703 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : 00704 ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : 00705 ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS")); 00706 printf("State of %s:\r\n", type_str); 00707 while (hced != NULL) { 00708 uint8_t dir = ((hced->control & (3 << 11)) >> 11); 00709 printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced, 00710 hced->control & 0x7f, 00711 (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"), 00712 (hced->control & (0xf << 7)) >> 7); 00713 hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf)); 00714 while (hctd != hced->tailTD) { 00715 printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN"); 00716 hctd = hctd->nextTD; 00717 } 00718 printf("\thctd: %p\r\n", hctd); 00719 hced = hced->nextED; 00720 } 00721 printf("\r\n\r\n"); 00722 #endif 00723 } 00724 00725 00726 // add a transfer on the TD linked list 00727 USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len) 00728 { 00729 USB_TYPE ret=USB_TYPE_PROCESSING; 00730 td_mutex.lock(); 00731 00732 // allocate a TD which will be freed in TDcompletion 00733 volatile HCTD * td = ed->getNextTD(); 00734 if (td == NULL) { 00735 return USB_TYPE_ERROR; 00736 } 00737 00738 #ifndef USBHOST_OTHER 00739 uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT )); 00740 00741 uint32_t td_toggle; 00742 00743 if (ed->getType() == CONTROL_ENDPOINT) { 00744 if (ed->isSetup()) { 00745 td_toggle = TD_TOGGLE_0; 00746 } else { 00747 td_toggle = TD_TOGGLE_1; 00748 } 00749 } else { 00750 td_toggle = 0; 00751 } 00752 00753 td->control = (TD_ROUNDING | token | TD_DELAY_INT(0) | td_toggle | TD_CC); 00754 td->currBufPtr = buf; 00755 td->bufEnd = (buf + (len - 1)); 00756 00757 ENDPOINT_TYPE type = ed->getType(); 00758 00759 disableList(type); 00760 ed->queueTransfer(); 00761 printList(type); 00762 enableList(type); 00763 #else 00764 /* call method specific for endpoint */ 00765 td->currBufPtr = buf; 00766 td->size = len; 00767 ret = ed->queueTransfer(); 00768 #endif 00769 00770 td_mutex.unlock(); 00771 00772 return ret; 00773 } 00774 00775 00776 00777 USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr) 00778 { 00779 USB_TYPE t = controlRead( dev, 00780 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, 00781 GET_DESCRIPTOR, 00782 (DEVICE_DESCRIPTOR << 8) | (0), 00783 0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf)); 00784 if (len_dev_descr) 00785 *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf); 00786 00787 return t; 00788 } 00789 00790 USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_conf_descr) 00791 { 00792 USB_TYPE res; 00793 uint16_t total_conf_descr_length = 0; 00794 00795 // fourth step: get the beginning of the configuration descriptor to have the total length of the conf descr 00796 res = controlRead( dev, 00797 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, 00798 GET_DESCRIPTOR, 00799 (CONFIGURATION_DESCRIPTOR << 8) | (0), 00800 0, buf, CONFIGURATION_DESCRIPTOR_LENGTH); 00801 00802 if (res != USB_TYPE_OK) { 00803 USB_ERR("GET CONF 1 DESCR FAILED"); 00804 return res; 00805 } 00806 total_conf_descr_length = buf[2] | (buf[3] << 8); 00807 total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length); 00808 00809 if (len_conf_descr) 00810 *len_conf_descr = total_conf_descr_length; 00811 00812 USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]); 00813 00814 return controlRead( dev, 00815 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, 00816 GET_DESCRIPTOR, 00817 (CONFIGURATION_DESCRIPTOR << 8) | (0), 00818 0, buf, total_conf_descr_length); 00819 } 00820 00821 00822 USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address) { 00823 return controlWrite( dev, 00824 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, 00825 SET_ADDRESS, 00826 address, 00827 0, NULL, 0); 00828 00829 } 00830 00831 USB_TYPE USBHost::setConfiguration(USBDeviceConnected * dev, uint8_t conf) 00832 { 00833 return controlWrite( dev, 00834 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, 00835 SET_CONFIGURATION, 00836 conf, 00837 0, NULL, 0); 00838 } 00839 00840 uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev) { 00841 int index = findDevice(dev); 00842 uint8_t cnt = 0; 00843 if (index == -1) 00844 return 0; 00845 for (uint8_t i = 0; i < MAX_INTF; i++) { 00846 if (deviceAttachedDriver[index][i]) 00847 cnt++; 00848 } 00849 return cnt; 00850 } 00851 00852 // enumerate a device with the control USBEndpoint 00853 USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator) 00854 { 00855 uint16_t total_conf_descr_length = 0; 00856 USB_TYPE res; 00857 00858 do 00859 { 00860 Lock lock(this); 00861 00862 // don't enumerate a device which all interfaces are registered to a specific driver 00863 int index = findDevice(dev); 00864 00865 if (index == -1) { 00866 return USB_TYPE_ERROR; 00867 } 00868 00869 uint8_t nb_intf_attached = numberDriverAttached(dev); 00870 USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf()); 00871 USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached); 00872 if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) { 00873 USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev); 00874 return USB_TYPE_OK; 00875 } 00876 00877 USB_DBG("Enumerate dev: %p", dev); 00878 00879 // third step: get the whole device descriptor to see vid, pid 00880 res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH); 00881 00882 if (res != USB_TYPE_OK) { 00883 USB_DBG("GET DEV DESCR FAILED"); 00884 return res; 00885 } 00886 00887 dev->setClass(data[4]); 00888 dev->setSubClass(data[5]); 00889 dev->setProtocol(data[6]); 00890 dev->setVid(data[8] | (data[9] << 8)); 00891 dev->setPid(data[10] | (data[11] << 8)); 00892 USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8)); 00893 00894 pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) ); 00895 00896 res = getConfigurationDescriptor(dev, data, 415, &total_conf_descr_length); 00897 if (res != USB_TYPE_OK) { 00898 return res; 00899 } 00900 00901 #if (DEBUG > 3) 00902 USB_DBG("CONFIGURATION DESCRIPTOR:\r\n"); 00903 for (int i = 0; i < total_conf_descr_length; i++) 00904 printf("%02X ", data[i]); 00905 printf("\r\n\r\n"); 00906 #endif 00907 00908 // Parse the configuration descriptor 00909 parseConfDescr(dev, data, total_conf_descr_length, pEnumerator); 00910 00911 // only set configuration if not enumerated before 00912 if (!dev->isEnumerated()) { 00913 00914 USB_DBG("Set configuration 1 on dev: %p", dev); 00915 // sixth step: set configuration (only 1 supported) 00916 res = setConfiguration(dev, 1); 00917 00918 if (res != USB_TYPE_OK) { 00919 USB_DBG("SET CONF FAILED"); 00920 return res; 00921 } 00922 } 00923 00924 dev->setEnumerated(); 00925 00926 // Now the device is enumerated! 00927 USB_DBG("dev %p is enumerated\r\n", dev); 00928 00929 } while(0); 00930 00931 // Some devices may require this delay 00932 ThisThread::sleep_for(100); 00933 00934 return USB_TYPE_OK; 00935 } 00936 // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor. 00937 void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator) 00938 { 00939 uint32_t index = 0; 00940 uint32_t len_desc = 0; 00941 uint8_t id = 0; 00942 int nb_endpoints_used = 0; 00943 USBEndpoint * ep = NULL; 00944 uint8_t intf_nb = 0; 00945 bool parsing_intf = false; 00946 uint8_t current_intf = 0; 00947 00948 while (index < len) { 00949 len_desc = conf_descr[index]; 00950 id = conf_descr[index+1]; 00951 switch (id) { 00952 case CONFIGURATION_DESCRIPTOR: 00953 USB_DBG("dev: %p has %d intf", dev, conf_descr[4]); 00954 dev->setNbIntf(conf_descr[4]); 00955 break; 00956 case INTERFACE_DESCRIPTOR: 00957 if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) { 00958 if (intf_nb++ <= MAX_INTF) { 00959 current_intf = conf_descr[index + 2]; 00960 dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]); 00961 nb_endpoints_used = 0; 00962 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]); 00963 } else { 00964 USB_DBG("Drop intf..."); 00965 } 00966 parsing_intf = true; 00967 } else { 00968 parsing_intf = false; 00969 } 00970 break; 00971 case ENDPOINT_DESCRIPTOR: 00972 if (parsing_intf && (intf_nb <= MAX_INTF) ) { 00973 if (nb_endpoints_used < MAX_ENDPOINT_PER_INTERFACE) { 00974 if( pEnumerator->useEndpoint(current_intf, (ENDPOINT_TYPE)(conf_descr[index + 3] & 0x03), (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1)) ) { 00975 // if the USBEndpoint is isochronous -> skip it (TODO: fix this) 00976 if ((conf_descr[index + 3] & 0x03) != ISOCHRONOUS_ENDPOINT) { 00977 ep = newEndpoint((ENDPOINT_TYPE)(conf_descr[index+3] & 0x03), 00978 (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1), 00979 conf_descr[index + 4] | (conf_descr[index + 5] << 8), 00980 conf_descr[index + 2] & 0x0f); 00981 USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev); 00982 if (ep != NULL && dev != NULL) { 00983 addEndpoint(dev, current_intf, ep); 00984 } else { 00985 USB_DBG("EP NULL"); 00986 } 00987 nb_endpoints_used++; 00988 } else { 00989 USB_DBG("ISO USBEndpoint NOT SUPPORTED"); 00990 } 00991 } 00992 } 00993 } 00994 break; 00995 case HID_DESCRIPTOR: 00996 lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8); 00997 break; 00998 default: 00999 break; 01000 } 01001 index += len_desc; 01002 } 01003 } 01004 01005 01006 USB_TYPE USBHost::bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) 01007 { 01008 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, true); 01009 } 01010 01011 USB_TYPE USBHost::bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) 01012 { 01013 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, false); 01014 } 01015 01016 USB_TYPE USBHost::interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) 01017 { 01018 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, true); 01019 } 01020 01021 USB_TYPE USBHost::interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) 01022 { 01023 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false); 01024 } 01025 01026 USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write) { 01027 01028 #if DEBUG_TRANSFER 01029 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS"); 01030 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()); 01031 #endif 01032 01033 Lock lock(this); 01034 01035 USB_TYPE res; 01036 ENDPOINT_DIRECTION dir = (write) ? OUT : IN; 01037 01038 if (dev == NULL) { 01039 USB_ERR("dev NULL"); 01040 return USB_TYPE_ERROR; 01041 } 01042 01043 if (ep == NULL) { 01044 USB_ERR("ep NULL"); 01045 return USB_TYPE_ERROR; 01046 } 01047 01048 if (ep->getState() != USB_TYPE_IDLE) { 01049 USB_WARN("[ep: %p - dev: %p - %s] NOT IDLE: %s", ep, ep->dev, ep->dev->getName(ep->getIntfNb()), ep->getStateString()); 01050 return ep->getState(); 01051 } 01052 01053 if ((ep->getDir() != dir) || (ep->getType() != type)) { 01054 USB_ERR("[ep: %p - dev: %p] wrong dir or bad USBEndpoint type", ep, ep->dev); 01055 return USB_TYPE_ERROR; 01056 } 01057 01058 if (dev->getAddress() != ep->getDeviceAddress()) { 01059 USB_ERR("[ep: %p - dev: %p] USBEndpoint addr and device addr don't match", ep, ep->dev); 01060 return USB_TYPE_ERROR; 01061 } 01062 01063 #if DEBUG_TRANSFER 01064 if (write) { 01065 USB_DBG_TRANSFER("%s WRITE buffer", type_str); 01066 for (int i = 0; i < ep->getLengthTransferred(); i++) 01067 printf("%02X ", buf[i]); 01068 printf("\r\n\r\n"); 01069 } 01070 #endif 01071 res = addTransfer(ep, buf, len); 01072 01073 if ((blocking)&& (res == USB_TYPE_PROCESSING)) { 01074 01075 ep->ep_queue.get(); 01076 res = ep->getState(); 01077 01078 USB_DBG_TRANSFER("%s TRANSFER res: %s on ep: %p\r\n", type_str, ep->getStateString(), ep); 01079 01080 if (res != USB_TYPE_IDLE) { 01081 return res; 01082 } 01083 01084 return USB_TYPE_OK; 01085 } 01086 01087 return res; 01088 01089 } 01090 01091 01092 USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { 01093 return controlTransfer(dev, requestType, request, value, index, buf, len, false); 01094 } 01095 01096 USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { 01097 return controlTransfer(dev, requestType, request, value, index, buf, len, true); 01098 } 01099 01100 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) 01101 { 01102 Lock lock(this); 01103 USB_DBG_TRANSFER("----- CONTROL %s [dev: %p - hub: %d - port: %d] ------", (write) ? "WRITE" : "READ", dev, dev->getHub(), dev->getPort()); 01104 01105 int length_transfer = len; 01106 USB_TYPE res; 01107 uint32_t token; 01108 01109 control->setSpeed(dev->getSpeed()); 01110 control->setSize(dev->getSizeControlEndpoint()); 01111 if (dev->isActiveAddress()) { 01112 control->setDeviceAddress(dev->getAddress()); 01113 } else { 01114 control->setDeviceAddress(0); 01115 } 01116 01117 USB_DBG_TRANSFER("Control transfer on device: %d\r\n", control->getDeviceAddress()); 01118 fillControlBuf(requestType, request, value, index, len); 01119 01120 #if DEBUG_TRANSFER 01121 USB_DBG_TRANSFER("SETUP PACKET: "); 01122 for (int i = 0; i < 8; i++) 01123 printf("%01X ", setupPacket[i]); 01124 printf("\r\n"); 01125 #endif 01126 01127 control->setNextToken(TD_SETUP); 01128 res = addTransfer(control, (uint8_t*)setupPacket, 8); 01129 01130 if (res == USB_TYPE_PROCESSING) control->ep_queue.get(); 01131 res = control->getState(); 01132 01133 USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString()); 01134 01135 if (res != USB_TYPE_IDLE) { 01136 return res; 01137 } 01138 01139 if (length_transfer) { 01140 token = (write) ? TD_OUT : TD_IN; 01141 control->setNextToken(token); 01142 res = addTransfer(control, (uint8_t *)buf, length_transfer); 01143 01144 if (res == USB_TYPE_PROCESSING) control->ep_queue.get(); 01145 res = control->getState(); 01146 01147 #if DEBUG_TRANSFER 01148 USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString()); 01149 if (write) { 01150 USB_DBG_TRANSFER("CONTROL WRITE buffer"); 01151 for (int i = 0; i < control->getLengthTransferred(); i++) 01152 printf("%02X ", buf[i]); 01153 printf("\r\n\r\n"); 01154 } else { 01155 USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred()); 01156 for (int i = 0; i < control->getLengthTransferred(); i++) 01157 printf("%02X ", buf[i]); 01158 printf("\r\n\r\n"); 01159 } 01160 #endif 01161 01162 if (res != USB_TYPE_IDLE) { 01163 return res; 01164 } 01165 } 01166 01167 token = (write) ? TD_IN : TD_OUT; 01168 control->setNextToken(token); 01169 res = addTransfer(control, NULL, 0); 01170 01171 if (res == USB_TYPE_PROCESSING) control->ep_queue.get(); 01172 res = control->getState(); 01173 01174 USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString()); 01175 01176 if (res != USB_TYPE_IDLE) 01177 return res; 01178 01179 return USB_TYPE_OK; 01180 } 01181 01182 01183 void USBHost::fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len) 01184 { 01185 setupPacket[0] = requestType; 01186 setupPacket[1] = request; 01187 setupPacket[2] = (uint8_t) value; 01188 setupPacket[3] = (uint8_t) (value >> 8); 01189 setupPacket[4] = (uint8_t) index; 01190 setupPacket[5] = (uint8_t) (index >> 8); 01191 setupPacket[6] = (uint8_t) len; 01192 setupPacket[7] = (uint8_t) (len >> 8); 01193 }
Generated on Wed Jul 13 2022 05:33:37 by 1.7.2