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