qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH 05/18] usb-host: fix configuration tracking.
Date: Fri,  2 Sep 2011 11:56:34 +0200	[thread overview]
Message-ID: <1314957407-29508-6-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1314957407-29508-1-git-send-email-kraxel@redhat.com>

It is perfectly fine to leave the usb device in unconfigured state
(USBHostDevice->configuration == 0).  Just do that and wait for the
guest to explicitly set a configuration.  This is closer to what real
hardware does and it also simplifies the device initialization.  There
is no need to figure how the device is configured on the host.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 usb-linux.c |   82 +++++++++++++---------------------------------------------
 1 files changed, 19 insertions(+), 63 deletions(-)

diff --git a/usb-linux.c b/usb-linux.c
index 36d25d7..a903023 100644
--- a/usb-linux.c
+++ b/usb-linux.c
@@ -407,8 +407,11 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
     int interface, nb_interfaces;
     int ret, i;
 
-    if (configuration == 0) /* address state - ignore */
+    if (configuration == 0) { /* address state - ignore */
+        dev->ninterfaces   = 0;
+        dev->configuration = 0;
         return 1;
+    }
 
     DPRINTF("husb: claiming interfaces. config %d\n", configuration);
 
@@ -433,7 +436,7 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
 
         DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
 
-        if (configuration < 0 || configuration == dev->descr[i + 5]) {
+        if (configuration == dev->descr[i + 5]) {
             configuration = dev->descr[i + 5];
             break;
         }
@@ -513,7 +516,7 @@ static void usb_host_handle_reset(USBDevice *dev)
 
     ioctl(s->fd, USBDEVFS_RESET);
 
-    usb_host_claim_interfaces(s, s->configuration);
+    usb_host_claim_interfaces(s, 0);
     usb_linux_update_endp_table(s);
 }
 
@@ -835,6 +838,7 @@ static int usb_host_set_config(USBHostDevice *s, int config)
         return ctrl_error();
     }
     usb_host_claim_interfaces(s, config);
+    usb_linux_update_endp_table(s);
     return 0;
 }
 
@@ -941,51 +945,6 @@ static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
     return USB_RET_ASYNC;
 }
 
-static int usb_linux_get_configuration(USBHostDevice *s)
-{
-    uint8_t configuration;
-    struct usb_ctrltransfer ct;
-    int ret;
-
-    if (usb_fs_type == USB_FS_SYS) {
-        char device_name[32], line[1024];
-        int configuration;
-
-        sprintf(device_name, "%d-%s", s->bus_num, s->port);
-
-        if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue",
-                                device_name)) {
-            goto usbdevfs;
-        }
-        if (sscanf(line, "%d", &configuration) != 1) {
-            goto usbdevfs;
-        }
-        return configuration;
-    }
-
-usbdevfs:
-    ct.bRequestType = USB_DIR_IN;
-    ct.bRequest = USB_REQ_GET_CONFIGURATION;
-    ct.wValue = 0;
-    ct.wIndex = 0;
-    ct.wLength = 1;
-    ct.data = &configuration;
-    ct.timeout = 50;
-
-    ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
-    if (ret < 0) {
-        perror("usb_linux_get_configuration");
-        return -1;
-    }
-
-    /* in address state */
-    if (configuration == 0) {
-        return -1;
-    }
-
-    return configuration;
-}
-
 static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
     uint8_t configuration, uint8_t interface)
 {
@@ -1031,16 +990,16 @@ usbdevfs:
 static int usb_linux_update_endp_table(USBHostDevice *s)
 {
     uint8_t *descriptors;
-    uint8_t devep, type, configuration, alt_interface;
+    uint8_t devep, type, alt_interface;
     int interface, length, i;
 
     for (i = 0; i < MAX_ENDPOINTS; i++)
         s->endp_table[i].type = INVALID_EP_TYPE;
 
-    i = usb_linux_get_configuration(s);
-    if (i < 0)
-        return 1;
-    configuration = i;
+    if (s->configuration == 0) {
+        /* not configured yet -- leave all endpoints disabled */
+        return 0;
+    }
 
     /* get the desired configuration, interface, and endpoint descriptors
      * from device description */
@@ -1049,8 +1008,9 @@ static int usb_linux_update_endp_table(USBHostDevice *s)
     i = 0;
 
     if (descriptors[i + 1] != USB_DT_CONFIG ||
-        descriptors[i + 5] != configuration) {
-        DPRINTF("invalid descriptor data - configuration\n");
+        descriptors[i + 5] != s->configuration) {
+        fprintf(stderr, "invalid descriptor data - configuration %d\n",
+                s->configuration);
         return 1;
     }
     i += descriptors[i];
@@ -1064,7 +1024,8 @@ static int usb_linux_update_endp_table(USBHostDevice *s)
         }
 
         interface = descriptors[i + 2];
-        alt_interface = usb_linux_get_alt_setting(s, configuration, interface);
+        alt_interface = usb_linux_get_alt_setting(s, s->configuration,
+                                                  interface);
 
         /* the current interface descriptor is the active interface
          * and has endpoints */
@@ -1204,13 +1165,8 @@ static int usb_host_open(USBHostDevice *dev, int bus_num,
 #endif
 
 
-    /*
-     * Initial configuration is -1 which makes us claim first
-     * available config. We used to start with 1, which does not
-     * always work. I've seen devices where first config starts
-     * with 2.
-     */
-    if (!usb_host_claim_interfaces(dev, -1)) {
+    /* start unconfigured -- we'll wait for the guest to set a configuration */
+    if (!usb_host_claim_interfaces(dev, 0)) {
         goto fail;
     }
 
-- 
1.7.1

  parent reply	other threads:[~2011-09-02  9:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-02  9:56 [Qemu-devel] [PULL] usb patch queue Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 01/18] usb-host: start tracing support Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 02/18] usb-host: reapurb error report fix Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 03/18] usb-host: fix halted endpoints Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 04/18] usb-host: limit open retries Gerd Hoffmann
2011-09-02  9:56 ` Gerd Hoffmann [this message]
2011-09-02  9:56 ` [Qemu-devel] [PATCH 06/18] usb-host: claim port Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 07/18] usb-host: endpoint table fixup Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 08/18] usb-ehci: handle siTDs Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 09/18] usb-host: constify port Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 10/18] usb-host: parse port in /proc/bus/usb/devices scan Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 11/18] usb: fix use after free Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 12/18] usb-ccid: switch to USBDesc* Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 13/18] usb-ccid: remote wakeup support Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 14/18] usb: claim port at device initialization time Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 15/18] usb-host: tag as unmigratable Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 16/18] usb: Remove leading underscores from __musb_irq_max Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 17/18] usb-musb: Take a DeviceState* in init function Gerd Hoffmann
2011-09-02  9:56 ` [Qemu-devel] [PATCH 18/18] usb-musb: Add reset function Gerd Hoffmann
2011-09-07  8:41 ` [Qemu-devel] [PULL] usb patch queue Gerd Hoffmann
2011-09-08 14:23 ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1314957407-29508-6-git-send-email-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).