ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHost.cpp Source File

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