USB BARCODE READER

Fork of USBHOST by ST

Revision:
5:fc157e6bd5a5
Parent:
1:ab240722d7ef
--- a/USBHost/USBHost.cpp	Fri Feb 17 12:40:57 2017 +0100
+++ b/USBHost/USBHost.cpp	Wed Apr 26 18:11:37 2017 +0200
@@ -46,7 +46,8 @@
 *       - when the usb_thread receives the event, it:
 *           - call the callback attached to the endpoint where the td is attached
 */
-void USBHost::usb_process() {
+void USBHost::usb_process()
+{
 
     bool controlListState;
     bool bulkListState;
@@ -79,110 +80,123 @@
                     too_many_hub = false;
                     buf[4] = 0;
 
-                    do
-                    {
-                      Lock lock(this);
+                    do {
+                        Lock lock(this);
+                        bool hub_unplugged = true;
 
-                      for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
-                          if (!deviceInUse[i]) {
-                              USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]);
-                              devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed);
-                              deviceReset[i] = false;
-                              deviceInited[i] = true;
-                              break;
-                          }
-                      }
+                        int idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent));
+                        /*  check that hub is connected to root port  */
+                        if (usb_msg->hub_parent) {
+                            /*  a hub device must be present */
+                            for (k = 0; k < MAX_HUB_NB; k++) {
+                                if ((&hubs[k] == usb_msg->hub_parent) && (hub_in_use[k]))
+                                    hub_unplugged=false;
+                            }
+                        } else hub_unplugged = false;
 
-                      if (i == MAX_DEVICE_CONNECTED) {
-                          USB_ERR("Too many device connected!!\r\n");
-                          continue;
-                      }
+                        if (((idx!=-1) && deviceInUse[idx] ) || ((idx == -1) && hub_unplugged))
+                            break;
 
-                      if (!controlEndpointAllocated) {
-                          control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00);
-                          addEndpoint(NULL, 0, (USBEndpoint*)control);
-                          controlEndpointAllocated = true;
-                      }
+                        for (i =0 ; i < MAX_DEVICE_CONNECTED; i++) {
+                            if (!deviceInUse[i]) {
+                                USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]);
+                                devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed);
+                                deviceReset[i] = false;
+                                deviceInited[i] = true;
+                                break;
+                            }
+                        }
 
-  #if MAX_HUB_NB
-                      if (usb_msg->hub_parent)
-                          devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent));
-  #endif
+                        if (i == MAX_DEVICE_CONNECTED) {
+                            USB_ERR("Too many device connected!!\r\n");
+                            continue;
+                        }
 
-                      for (j = 0; j < timeout_set_addr; j++) {
-
-                          resetDevice(&devices[i]);
-
-                          // set size of control endpoint
-                          devices[i].setSizeControlEndpoint(8);
+                        if (!controlEndpointAllocated) {
+                            control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00);
+                            addEndpoint(NULL, 0, (USBEndpoint*)control);
+                            controlEndpointAllocated = true;
+                        }
 
-                          devices[i].activeAddress(false);
+#if MAX_HUB_NB
+                        if (usb_msg->hub_parent)
+                            devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent));
+#endif
 
-                          // get first 8 bit of device descriptor
-                          // and check if we deal with a hub
-                          USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]);
-                          res = getDeviceDescriptor(&devices[i], buf, 8);
+                        for (j = 0; j < timeout_set_addr; j++) {
+
+                            resetDevice(&devices[i]);
 
-                          if (res != USB_TYPE_OK) {
-                              USB_ERR("usb_thread could not read dev descr");
-                              continue;
-                          }
+                            // set size of control endpoint
+                            devices[i].setSizeControlEndpoint(8);
+
+                            devices[i].activeAddress(false);
 
-                          // set size of control endpoint
-                          devices[i].setSizeControlEndpoint(buf[7]);
+                            // get first 8 bit of device descriptor
+                            // and check if we deal with a hub
+                            USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]);
+                            res = getDeviceDescriptor(&devices[i], buf, 8);
 
-                          // second step: set an address to the device
-                          res = setAddress(&devices[i], devices[i].getAddress());
+                            if (res != USB_TYPE_OK) {
+                                USB_ERR("usb_thread could not read dev descr");
+                                continue;
+                            }
 
-                          if (res != USB_TYPE_OK) {
-                              USB_ERR("SET ADDR FAILED");
-                              continue;
-                          }
-                          devices[i].activeAddress(true);
-                          USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress());
+                            // set size of control endpoint
+                            devices[i].setSizeControlEndpoint(buf[7]);
+
+                            // second step: set an address to the device
+                            res = setAddress(&devices[i], devices[i].getAddress());
 
-                          // try to read again the device descriptor to check if the device
-                          // answers to its new address
-                          res = getDeviceDescriptor(&devices[i], buf, 8);
+                            if (res != USB_TYPE_OK) {
+                                USB_ERR("SET ADDR FAILED");
+                                continue;
+                            }
+                            devices[i].activeAddress(true);
+                            USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress());
 
-                          if (res == USB_TYPE_OK) {
-                              break;
-                          }
+                            // try to read again the device descriptor to check if the device
+                            // answers to its new address
+                            res = getDeviceDescriptor(&devices[i], buf, 8);
 
-                          Thread::wait(100);
-                      }
+                            if (res == USB_TYPE_OK) {
+                                break;
+                            }
 
-                      USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
+                            Thread::wait(100);
+                        }
 
-  #if MAX_HUB_NB
-                      if (buf[4] == HUB_CLASS) {
-                          for (k = 0; k < MAX_HUB_NB; k++) {
-                              if (hub_in_use[k] == false) {
-                                  for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) {
-                                      if (hubs[k].connect(&devices[i])) {
-                                          devices[i].hub = &hubs[k];
-                                          hub_in_use[k] = true;
-                                          break;
-                                      }
-                                  }
-                                  if (hub_in_use[k] == true)
-                                      break;
-                              }
-                          }
+                        USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
 
-                          if (k == MAX_HUB_NB) {
-                              USB_ERR("Too many hubs connected!!\r\n");
-                              too_many_hub = true;
-                          }
-                      }
+#if MAX_HUB_NB
+                        if (buf[4] == HUB_CLASS) {
+                            for (k = 0; k < MAX_HUB_NB; k++) {
+                                if (hub_in_use[k] == false) {
+                                    for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) {
+                                        if (hubs[k].connect(&devices[i])) {
+                                            devices[i].hub = &hubs[k];
+                                            hub_in_use[k] = true;
+                                            break;
+                                        }
+                                    }
+                                    if (hub_in_use[k] == true)
+                                        break;
+                                }
+                            }
 
-                      if (usb_msg->hub_parent)
-                          ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]);
-  #endif
+                            if (k == MAX_HUB_NB) {
+                                USB_ERR("Too many hubs connected!!\r\n");
+                                too_many_hub = true;
+                            }
+                        }
 
-                      if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) {
-                          deviceInUse[i] = true;
-                      }
+                        if (usb_msg->hub_parent)
+                            ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]);
+#endif
+
+                        if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) {
+                            deviceInUse[i] = true;
+                        }
 
                     } while(0);
 
@@ -191,22 +205,22 @@
                 // a device has been disconnected
                 case DEVICE_DISCONNECTED_EVENT:
 
-                    do
-                    {
-                      Lock lock(this);
+                    do {
+                        Lock lock(this);
 
-                      controlListState = disableList(CONTROL_ENDPOINT);
-                      bulkListState = disableList(BULK_ENDPOINT);
-                      interruptListState = disableList(INTERRUPT_ENDPOINT);
+                        controlListState = disableList(CONTROL_ENDPOINT);
+                        bulkListState = disableList(BULK_ENDPOINT);
+                        interruptListState = disableList(INTERRUPT_ENDPOINT);
 
-                      idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent));
-                      if (idx != -1) {
-                          freeDevice((USBDeviceConnected*)&devices[idx]);
-                      }
+                        idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent));
+                        if (idx != -1) {
+                            freeDevice((USBDeviceConnected*)&devices[idx]);
+                            deviceInited[idx]=false;
+                        }
 
-                      if (controlListState) enableList(CONTROL_ENDPOINT);
-                      if (bulkListState) enableList(BULK_ENDPOINT);
-                      if (interruptListState) enableList(INTERRUPT_ENDPOINT);
+                        if (controlListState) enableList(CONTROL_ENDPOINT);
+                        if (bulkListState) enableList(BULK_ENDPOINT);
+                        if (interruptListState) enableList(INTERRUPT_ENDPOINT);
 
                     } while(0);
 
@@ -236,6 +250,12 @@
                             if (deviceInUse[idx]) {
                                 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()));
                                 ep->setState(USB_TYPE_IDLE);
+                                /* as error, on interrupt endpoint can be
+                                 * reported, call the call back registered ,
+                                 * if  device still in use, this call back
+                                 * shall ask again an interrupt request.
+                                 */
+                                ep->call(); 
                             }
                         }
                     }
@@ -282,12 +302,12 @@
 
 USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost)
 {
-  m_pHost->usb_mutex.lock();
+    m_pHost->usb_mutex.lock();
 }
 
 USBHost::Lock::~Lock()
 {
-  m_pHost->usb_mutex.unlock();
+    m_pHost->usb_mutex.unlock();
 }
 
 void USBHost::transferCompleted(volatile uint32_t addr)
@@ -340,7 +360,7 @@
                 usb_msg->td_state = state;
                 mail_usb_event.put(usb_msg);
             }
-            ep->setState(state);
+            ep->setState((USB_TYPE)state);
             ep->ep_queue.put((uint8_t*)1);
         }
     }
@@ -363,12 +383,16 @@
 /* virtual */ void USBHost::deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent)
 {
     // be sure that the new device connected is not already connected...
+    disableList(CONTROL_ENDPOINT);
+
     int idx = findDevice(hub, port, hub_parent);
     if (idx != -1) {
-        if (deviceInited[idx])
+        if (deviceInited[idx]) {
+            enableList(CONTROL_ENDPOINT);
+
             return;
+        }
     }
-
     message_t * usb_msg = mail_usb_event.alloc();
     usb_msg->event_id = DEVICE_CONNECTED_EVENT;
     usb_msg->hub = hub;
@@ -376,6 +400,9 @@
     usb_msg->lowSpeed = lowSpeed;
     usb_msg->hub_parent = hub_parent;
     mail_usb_event.put(usb_msg);
+    enableList(CONTROL_ENDPOINT);
+
+
 }
 
 /*
@@ -385,11 +412,17 @@
 /* virtual */ void USBHost::deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr)
 {
     // be sure that the device disconnected is connected...
+
+    disableList(CONTROL_ENDPOINT);
+
     int idx = findDevice(hub, port, hub_parent);
     if (idx != -1) {
-        if (!deviceInUse[idx])
+        if (!deviceInUse[idx]) {
+            enableList(CONTROL_ENDPOINT);
             return;
+        }
     } else {
+        enableList(CONTROL_ENDPOINT);
         return;
     }
 
@@ -399,6 +432,9 @@
     usb_msg->port = port;
     usb_msg->hub_parent = hub_parent;
     mail_usb_event.put(usb_msg);
+    enableList(CONTROL_ENDPOINT);
+
+
 }
 
 void USBHost::freeDevice(USBDeviceConnected * dev)
@@ -695,14 +731,14 @@
     volatile HCTD * hctd = NULL;
     const char * type_str = (type == BULK_ENDPOINT) ? "BULK" :
                             ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" :
-                            ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS"));
+                             ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS"));
     printf("State of %s:\r\n", type_str);
     while (hced != NULL) {
         uint8_t dir = ((hced->control & (3 << 11)) >> 11);
         printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced,
-                                                   hced->control & 0x7f,
-                                                   (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"),
-                                                    (hced->control & (0xf << 7)) >> 7);
+               hced->control & 0x7f,
+               (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"),
+               (hced->control & (0xf << 7)) >> 7);
         hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf));
         while (hctd != hced->tailTD) {
             printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN");
@@ -720,7 +756,7 @@
 USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
 {
     USB_TYPE ret=USB_TYPE_PROCESSING;
-	td_mutex.lock();
+    td_mutex.lock();
 
     // allocate a TD which will be freed in TDcompletion
     volatile HCTD * td = ed->getNextTD();
@@ -728,7 +764,7 @@
         return USB_TYPE_ERROR;
     }
 
-#ifndef USBHOST_OTHER 
+#ifndef USBHOST_OTHER
     uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT ));
 
     uint32_t td_toggle;
@@ -754,10 +790,10 @@
     printList(type);
     enableList(type);
 #else
-	/*  call method specific for endpoint  */
-	td->currBufPtr   = buf;
-	td->size = len;
-	ret = ed->queueTransfer();
+    /*  call method specific for endpoint  */
+    td->currBufPtr   = buf;
+    td->size = len;
+    ret = ed->queueTransfer();
 #endif
 
     td_mutex.unlock();
@@ -770,10 +806,10 @@
 USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr)
 {
     USB_TYPE t = controlRead(  dev,
-                         USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
-                         GET_DESCRIPTOR,
-                         (DEVICE_DESCRIPTOR << 8) | (0),
-                         0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf));
+                               USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
+                               GET_DESCRIPTOR,
+                               (DEVICE_DESCRIPTOR << 8) | (0),
+                               0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf));
     if (len_dev_descr)
         *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf);
 
@@ -812,7 +848,8 @@
 }
 
 
-USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address) {
+USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address)
+{
     return controlWrite(    dev,
                             USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
                             SET_ADDRESS,
@@ -830,7 +867,8 @@
                          0, NULL, 0);
 }
 
-uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev) {
+uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev)
+{
     int index = findDevice(dev);
     uint8_t cnt = 0;
     if (index == -1)
@@ -848,76 +886,75 @@
     uint16_t total_conf_descr_length = 0;
     USB_TYPE res;
 
-    do
-    {
-      Lock lock(this);
+    do {
+        Lock lock(this);
 
-      // don't enumerate a device which all interfaces are registered to a specific driver
-      int index = findDevice(dev);
+        // don't enumerate a device which all interfaces are registered to a specific driver
+        int index = findDevice(dev);
 
-      if (index == -1) {
-          return USB_TYPE_ERROR;
-      }
+        if (index == -1) {
+            return USB_TYPE_ERROR;
+        }
 
-      uint8_t nb_intf_attached = numberDriverAttached(dev);
-      USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf());
-      USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached);
-      if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) {
-          USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev);
-          return USB_TYPE_OK;
-      }
+        uint8_t nb_intf_attached = numberDriverAttached(dev);
+        USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf());
+        USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached);
+        if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) {
+            USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev);
+            return USB_TYPE_OK;
+        }
+
+        USB_DBG("Enumerate dev: %p", dev);
 
-      USB_DBG("Enumerate dev: %p", dev);
-
-      // third step: get the whole device descriptor to see vid, pid
-      res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH);
+        // third step: get the whole device descriptor to see vid, pid
+        res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH);
 
-      if (res != USB_TYPE_OK) {
-          USB_DBG("GET DEV DESCR FAILED");
-          return res;
-      }
+        if (res != USB_TYPE_OK) {
+            USB_DBG("GET DEV DESCR FAILED");
+            return res;
+        }
+
+        dev->setClass(data[4]);
+        dev->setSubClass(data[5]);
+        dev->setProtocol(data[6]);
+        dev->setVid(data[8] | (data[9] << 8));
+        dev->setPid(data[10] | (data[11] << 8));
+        USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8));
 
-      dev->setClass(data[4]);
-      dev->setSubClass(data[5]);
-      dev->setProtocol(data[6]);
-      dev->setVid(data[8] | (data[9] << 8));
-      dev->setPid(data[10] | (data[11] << 8));
-      USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8));
+        pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) );
 
-      pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) );
+        res = getConfigurationDescriptor(dev, data, sizeof(data), &total_conf_descr_length);
+        if (res != USB_TYPE_OK) {
+            return res;
+        }
 
-      res = getConfigurationDescriptor(dev, data, sizeof(data), &total_conf_descr_length);
-      if (res != USB_TYPE_OK) {
-          return res;
-      }
+#if (DEBUG > 3)
+        USB_DBG("CONFIGURATION DESCRIPTOR:\r\n");
+        for (int i = 0; i < total_conf_descr_length; i++)
+            printf("%02X ", data[i]);
+        printf("\r\n\r\n");
+#endif
 
-  #if (DEBUG > 3)
-      USB_DBG("CONFIGURATION DESCRIPTOR:\r\n");
-      for (int i = 0; i < total_conf_descr_length; i++)
-          printf("%02X ", data[i]);
-      printf("\r\n\r\n");
-  #endif
+        // Parse the configuration descriptor
+        parseConfDescr(dev, data, total_conf_descr_length, pEnumerator);
 
-      // Parse the configuration descriptor
-      parseConfDescr(dev, data, total_conf_descr_length, pEnumerator);
+        // only set configuration if not enumerated before
+        if (!dev->isEnumerated()) {
 
-      // only set configuration if not enumerated before
-      if (!dev->isEnumerated()) {
-
-          USB_DBG("Set configuration 1 on dev: %p", dev);
-          // sixth step: set configuration (only 1 supported)
-          res = setConfiguration(dev, 1);
+            USB_DBG("Set configuration 1 on dev: %p", dev);
+            // sixth step: set configuration (only 1 supported)
+            res = setConfiguration(dev, 1);
 
-          if (res != USB_TYPE_OK) {
-              USB_DBG("SET CONF FAILED");
-              return res;
-          }
-      }
+            if (res != USB_TYPE_OK) {
+                USB_DBG("SET CONF FAILED");
+                return res;
+            }
+        }
 
-      dev->setEnumerated();
+        dev->setEnumerated();
 
-      // Now the device is enumerated!
-      USB_DBG("dev %p is enumerated\r\n", dev);
+        // Now the device is enumerated!
+        USB_DBG("dev %p is enumerated\r\n", dev);
 
     } while(0);
 
@@ -1016,7 +1053,8 @@
     return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false);
 }
 
-USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write) {
+USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write)
+{
 
 #if DEBUG_TRANSFER
     const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS");
@@ -1064,8 +1102,20 @@
     res = addTransfer(ep, buf, len);
 
     if ((blocking)&& (res == USB_TYPE_PROCESSING)) {
-
+#ifdef USBHOST_OTHER
+        osEvent  event = ep->ep_queue.get(TD_TIMEOUT);
+        if (event.status == osEventTimeout)
+        {
+            /*  control endpoint is confusing for merge on b */
+            disableList(CONTROL_ENDPOINT);
+            ep->setState(USB_TYPE_ERROR);
+            ep->ep_queue.get(0);
+            ep->unqueueTransfer(ep->getProcessedTD());
+            enableList(CONTROL_ENDPOINT);
+        }
+#else
         ep->ep_queue.get();
+#endif
         res = ep->getState();
 
         USB_DBG_TRANSFER("%s TRANSFER res: %s on ep: %p\r\n", type_str, ep->getStateString(), ep);
@@ -1082,11 +1132,13 @@
 }
 
 
-USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
+USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len)
+{
     return controlTransfer(dev, requestType, request, value, index, buf, len, false);
 }
 
-USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
+USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len)
+{
     return controlTransfer(dev, requestType, request, value, index, buf, len, true);
 }
 
@@ -1120,7 +1172,20 @@
     control->setNextToken(TD_SETUP);
     res = addTransfer(control, (uint8_t*)setupPacket, 8);
 
-    if (res == USB_TYPE_PROCESSING) control->ep_queue.get();
+    if (res == USB_TYPE_PROCESSING)
+#ifdef USBHOST_OTHER
+    {   osEvent  event = control->ep_queue.get(TD_TIMEOUT_CTRL);
+        if (event.status == osEventTimeout) {
+            disableList(CONTROL_ENDPOINT);
+            control->setState(USB_TYPE_ERROR);
+            control->ep_queue.get(0);
+            control->unqueueTransfer(control->getProcessedTD());
+            enableList(CONTROL_ENDPOINT);
+        }
+    }
+#else
+        control->ep_queue.get();
+#endif
     res = control->getState();
 
     USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString());
@@ -1134,7 +1199,21 @@
         control->setNextToken(token);
         res = addTransfer(control, (uint8_t *)buf, length_transfer);
 
-        if (res == USB_TYPE_PROCESSING) control->ep_queue.get();
+        if (res == USB_TYPE_PROCESSING)
+#ifdef USBHOST_OTHER
+        {   osEvent  event = control->ep_queue.get(TD_TIMEOUT_CTRL);
+            if (event.status == osEventTimeout)
+            {
+                disableList(CONTROL_ENDPOINT);
+                control->setState(USB_TYPE_ERROR);
+                control->ep_queue.get(0);
+                control->unqueueTransfer(control->getProcessedTD());
+                enableList(CONTROL_ENDPOINT);
+            }
+        }
+#else
+        control->ep_queue.get();
+#endif
         res = control->getState();
 
 #if DEBUG_TRANSFER
@@ -1160,8 +1239,22 @@
     token = (write) ? TD_IN : TD_OUT;
     control->setNextToken(token);
     res = addTransfer(control, NULL, 0);
-
-    if (res == USB_TYPE_PROCESSING) control->ep_queue.get();
+    if (res == USB_TYPE_PROCESSING)
+#ifdef USBHOST_OTHER
+    {
+        osEvent  event = control->ep_queue.get(TD_TIMEOUT_CTRL);
+        if (event.status == osEventTimeout)
+        {
+            disableList(CONTROL_ENDPOINT);
+            control->setState(USB_TYPE_ERROR);
+            control->ep_queue.get(0);
+            control->unqueueTransfer(control->getProcessedTD());
+            enableList(CONTROL_ENDPOINT);
+        }
+    }
+#else
+        control->ep_queue.get();
+#endif
     res = control->getState();
 
     USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString());