Skip to contentSkip to conversation

usb-host: some little tweaks to hopefully reduce usb pass-through problems.

PATCH New Gerd Hoffmann 1 participant 4 messages May 22, 2019

lore
Share & tools
lore
#0 lore raw

cover usb-host: some little tweaks to hopefully reduce usb pass-through problems.

Gerd Hoffmann (3):
usb: call reset handler before updating state
usb-host: skip reset for untouched devices
usb-host: avoid libusb_set_configuration calls

 hw/usb/core.c        |  2 +-
 hw/usb/host-libusb.c | 21 +++++++++++++--------
 2 files changed, 14 insertions(+), 9 deletions(-)
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/3 usb: call reset handler before updating state

That way the device reset handler can see what
the before-reset state of the device is.

---
 hw/usb/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/core.c +1 -1
index 8fbd9c7d573b..3ab48a16074b 100644
@@ -87,10 +87,10 @@ void usb_device_reset(USBDevice *dev)
     if (dev == NULL || !dev->attached) {
         return;
     }
+    usb_device_handle_reset(dev);
     dev->remote_wakeup = 0;
     dev->addr = 0;
     dev->state = USB_STATE_DEFAULT;
-    usb_device_handle_reset(dev);
 }
 
 void usb_wakeup(USBEndpoint *ep, unsigned int stream)
Signature · 1 line
2.18.1
#2 lore raw

PATCH 2/3 usb-host: skip reset for untouched devices

In reply to Gerd Hoffmann

If the guest didn't talk to the device yet, skip the reset. Without this usb-host devices get resetted a number of times at boot time for no good reason.

---
 hw/usb/host-libusb.c | 3 +++
 1 file changed, 3 insertions(+)
hw/usb/host-libusb.c +3 -0
index 67b7465915f5..4e9a45a5d26f 100644
@@ -1459,6 +1459,9 @@ static void usb_host_handle_reset(USBDevice *udev)
     if (!s->allow_guest_reset) {
         return;
     }
+    if (udev->addr == 0) {
+        return;
+    }
 
     trace_usb_host_reset(s->bus_num, s->addr);
 
Signature · 1 line
2.18.1
#3 lore raw

PATCH 3/3 usb-host: avoid libusb_set_configuration calls

In reply to Gerd Hoffmann

Seems some devices become confused when we call libusb_set_configuration(). So before calling the function check whenever the device has multiple configurations in the first place, and in case it hasn't (which is the case for the majority of devices) simply skip the call as it will have no effect anyway.

---
 hw/usb/host-libusb.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
hw/usb/host-libusb.c +10 -8
index 4e9a45a5d26f..4f765d7f9a2f 100644
@@ -1225,19 +1225,21 @@ static void usb_host_set_address(USBHostDevice *s, int addr)
 
 static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
 {
-    int rc;
+    int rc = 0;
 
     trace_usb_host_set_config(s->bus_num, s->addr, config);
 
     usb_host_release_interfaces(s);
-    rc = libusb_set_configuration(s->dh, config);
-    if (rc != 0) {
-        usb_host_libusb_error("libusb_set_configuration", rc);
-        p->status = USB_RET_STALL;
-        if (rc == LIBUSB_ERROR_NO_DEVICE) {
-            usb_host_nodev(s);
+    if (s->ddesc.bNumConfigurations != 1) {
+        rc = libusb_set_configuration(s->dh, config);
+        if (rc != 0) {
+            usb_host_libusb_error("libusb_set_configuration", rc);
+            p->status = USB_RET_STALL;
+            if (rc == LIBUSB_ERROR_NO_DEVICE) {
+                usb_host_nodev(s);
+            }
+            return;
         }
-        return;
     }
     p->status = usb_host_claim_interfaces(s, config);
     if (p->status != USB_RET_SUCCESS) {
Signature · 1 line
2.18.1