Skip to contentSkip to conversation

USB: automatically choose bus to connect to based on speed

Qemu-devel New Hans de Goede 2 participants 7 messages May 31, 2011

lore
Share & tools
lore
#0 lore raw

This patch series makes the usb subsystem automatically attach a usb device to the right bus, based on the device's and the bus' speed, unless a bus is explicitly specified by the user. This makes the uhci controller more or less act as a companion controller to the ehci controller (if enabled), and makes it easier for end users to redirect usb devices (as they don't need to think about which bus they need to redirect to).

Regards,

Hans

#1 lore raw

PATCH 1/4 usb: Add a speedmask to devices

This is used to indicate at which speed[s] the device can operate, so that this can be checked to match the ports capabilities when it gets attached to a bus.

Note that currently all usb1 emulated device claim to be fullspeed, this seems to not cause any problems, but still seems wrong, because with real hardware keyboards, mice and tablets usually are lo-speed, so reporting these as fullspeed devices seems wrong.

---
 hw/usb-ccid.c |    1 +
 hw/usb-desc.c |   10 ++++++++++
 hw/usb.h      |    3 +++
 usb-bsd.c     |    2 ++
 usb-linux.c   |    1 +
 5 files changed, 17 insertions(+), 0 deletions(-)
hw/usb-ccid.c +1 -0
index 5b6878b..9e7d6d2 100644
@@ -1283,6 +1283,7 @@ static int ccid_initfn(USBDevice *dev)
     s->migration_target_ip = 0;
     s->migration_target_port = 0;
     s->dev.speed = USB_SPEED_FULL;
+    s->dev.speedmask = USB_SPEED_MASK_FULL;
     s->notify_slot_change = false;
     s->powered = true;
     s->pending_answers_num = 0;
hw/usb-desc.c +10 -0
index 57e8d6b..4f58ae8 100644
@@ -242,7 +242,17 @@ static void usb_desc_setdefaults(USBDevice *dev)
 
 void usb_desc_init(USBDevice *dev)
 {
+    const USBDesc *desc = dev->info->usb_desc;
+
+    assert(desc != NULL);
     dev->speed = USB_SPEED_FULL;
+    dev->speedmask = 0;
+    if (desc->full) {
+        dev->speedmask |= USB_SPEED_MASK_FULL;
+    }
+    if (desc->high) {
+        dev->speedmask |= USB_SPEED_MASK_HIGH;
+    }
     usb_desc_setdefaults(dev);
 }
 
hw/usb.h +3 -0
index 097d789..5623f34 100644
@@ -169,7 +169,10 @@ struct USBDevice {
     char *port_path;
     void *opaque;
 
+    /* Actual connected speed */
     int speed;
+    /* Supported speeds, not in info because it may be variable (hostdevs) */
+    int speedmask;
     uint8_t addr;
     char product_desc[32];
     int auto_attach;
usb-bsd.c +2 -0
index 9bab6e3..11829a2 100644
@@ -368,8 +368,10 @@ USBDevice *usb_host_device_open(const char *devname)
 
     if (dev_info.udi_speed == 1) {
         dev->dev.speed = USB_SPEED_LOW - 1;
+        dev->dev.speedmask = USB_SPEED_MASK_LOW;
     } else {
         dev->dev.speed = USB_SPEED_FULL - 1;
+        dev->dev.speedmask = USB_SPEED_MASK_FULL;
     }
 
     if (strncmp(dev_info.udi_product, "product", 7) != 0) {
usb-linux.c +1 -0
index fe21da1..71e5f4d 100644
@@ -1141,6 +1141,7 @@ static int usb_host_open(USBHostDevice *dev, int bus_num,
         }
     }
     dev->dev.speed = speed;
+    dev->dev.speedmask = (1 << speed);
 
     printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
 
Signature · 1 line
1.7.5.1
#2 lore raw

PATCH 2/4 usb-bus: Don't allow speed mismatch while attaching devices

In reply to Hans de Goede
---
 hw/usb-bus.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
hw/usb-bus.c +5 -0
index 2ae2678..1817d5c 100644
@@ -200,6 +200,11 @@ static int do_attach(USBDevice *dev)
     } else {
         port = QTAILQ_FIRST(&bus->free);
     }
+    if (!(port->speedmask & dev->speedmask)) {
+        fprintf(stderr, "Warning: speed mismatch trying to attach usb device %s to bus %s\n",
+                dev->product_desc, bus->qbus.name);
+        return -1;
+    }
 
     dev->attached++;
     QTAILQ_REMOVE(&bus->free, port, next);
Signature · 1 line
1.7.5.1
#3 lore raw

PATCH 3/4 usb-bus: Automatically select right usb bus based on device and bus speed

In reply to Hans de Goede

If the bus was not explictly specified by the user, automatically select the right usb bus based on device and bus speed.

---
 hw/qdev.c    |    2 +-
 hw/usb-bus.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 hw/usb.h     |    1 +
 3 files changed, 54 insertions(+), 2 deletions(-)
hw/qdev.c +1 -1
index 9519f5d..f032412 100644
@@ -260,11 +260,11 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         qdev_free(qdev);
         return NULL;
     }
+    qdev->opts = opts;
     if (qdev_init(qdev) < 0) {
         qerror_report(QERR_DEVICE_INIT_FAILED, driver);
         return NULL;
     }
-    qdev->opts = opts;
     return qdev;
 }
 
hw/usb-bus.c +52 -1

87 lines not on this page · raw message

hw/usb.h +1 -0
index 5623f34..807f9e3 100644
@@ -177,6 +177,7 @@ struct USBDevice {
     char product_desc[32];
     int auto_attach;
     int attached;
+    int bus_specified;
 
     int32_t state;
     uint8_t setup_buf[8];
Signature · 1 line
1.7.5.1
#4 lore raw

PATCH 4/4 usb-linux: allow "compatible" high speed devices to connect at fullspeed

In reply to Hans de Goede

Some usb2 highspeed devices, like usb-msd devices, work fine when redirected to a usb1 virtual controller. Allow this to avoid the new speedhecks causing regressions for users who do not enable the new experimental ehci code.

---
 usb-linux.c |   39 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 39 insertions(+), 0 deletions(-)
usb-linux.c +39 -0
index 71e5f4d..187e6ae 100644
@@ -1062,6 +1062,42 @@ static int usb_linux_update_endp_table(USBHostDevice *s)
     return 0;
 }
 
+/*
+ * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
+ * this function assumes this is safe, if:
+ * 1) There are no isoc endpoints
+ * 2) There are no interrupt endpoints with a max_packet_size > 64
+ * Note bulk endpoints with a max_packet_size > 64 in theory also are not
+ * usb1 compatible, but in practice this seems to work fine.
+ */
+static int usb_linux_full_speed_compat(USBHostDevice *dev)
+{
+    int i, packet_size;
+
+    /*
+     * usb_linux_update_endp_table only registers info about ep in the current
+     * interface altsettings, so we need to parse the descriptors again.
+     */
+    for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
+        if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
+            switch (dev->descr[i + 3] & 0x3) {
+            case 0x00: /* CONTROL */
+                break;
+            case 0x01: /* ISO */
+                return 0;
+            case 0x02: /* BULK */
+                break;
+            case 0x03: /* INTERRUPT */
+                packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
+                if (packet_size > 64)
+                    return 0;
+                break;
+            }
+        }
+    }
+    return 1;
+}
+
 static int usb_host_open(USBHostDevice *dev, int bus_num,
                         int addr, char *port, const char *prod_name, int speed)
 {
@@ -1142,6 +1178,9 @@ static int usb_host_open(USBHostDevice *dev, int bus_num,
     }
     dev->dev.speed = speed;
     dev->dev.speedmask = (1 << speed);
+    if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
+        dev->dev.speedmask |= USB_SPEED_MASK_FULL;
+    }
 
     printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
 
Signature · 1 line
1.7.5.1
#5 lore raw
In reply to Hans de Goede
Quoted 6 lines · Hans de Goede

This patch series makes the usb subsystem automatically attach a usb device to the right bus, based on the device's and the bus' speed, unless a bus is explicitly specified by the user. This makes the uhci controller more or less act as a companion controller to the ehci controller (if enabled), and makes it easier for end users to redirect usb devices (as they don't need to think about which bus they need to redirect to).

Shouldn't the correct order for the patches be 1, 4, 2, 3?

Paolo

#6 lore raw

Hi,

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Hans de Goede

This patch series makes the usb subsystem automatically attach a usb device to the right bus, based on the device's and the bus' speed, unless a bus is explicitly specified by the user. This makes the uhci controller more or less act as a companion controller to the ehci controller (if enabled), and makes it easier for end users to redirect usb devices (as they don't need to think about which bus they need to redirect to).

Shouldn't the correct order for the patches be 1, 4, 2, 3?

That is not the order in which they were written :) But I see your point.

Regards,

Hans