qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Markus Armbruster <armbru@redhat.com>, Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PULL 4/8] usb: Suppress bogus error when automatic usb-hub creation fails
Date: Wed, 18 Feb 2015 11:08:21 +0100	[thread overview]
Message-ID: <1424254105-30237-5-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1424254105-30237-1-git-send-email-kraxel@redhat.com>

From: Markus Armbruster <armbru@redhat.com>

USBDevice's realize method usb_qdev_realize() automatically creates a
usb-hub when only one port is left.  Creating devices in realize
methods is questionable, but works.

If usb-hub creation fails, an error is reported to stderr, but the
failure is otherwise ignored.  We then create the actual device using
the last port, which may well succeed.

Example:

    $ qemu -nodefaults -S -display none -machine usb=on -monitor stdio
    QEMU 2.2.50 monitor - type 'help' for more information
    (qemu) device_add usb-mouse
    [Repeat 36 times]
    (qemu) info usb
      Device 0.0, Port 1, Speed 12 Mb/s, Product QEMU USB Mouse
      Device 0.0, Port 2, Speed 12 Mb/s, Product QEMU USB Hub
      Device 0.0, Port 2.1, Speed 12 Mb/s, Product QEMU USB Mouse
    [More mice and hubs omitted...]
      Device 0.0, Port 2.8.8.8.8.7, Speed 12 Mb/s, Product QEMU USB Mouse
    (qemu) device_add usb-mouse
    usb hub chain too deep
    Failed to initialize USB device 'usb-hub'
    (qemu) info usb
    [...]
      Device 0.0, Port 2.8.8.8.8.7, Speed 12 Mb/s, Product QEMU USB Mouse
      Device 0.0, Port 2.8.8.8.8.8, Speed 12 Mb/s, Product QEMU USB Mouse

Despite the "Failed" message, the command actually succeeded.

In QMP, it's worse.  When adding the 37th mouse via QMP, the command
fails with

    {"error": {"class": "GenericError", "desc": "usb hub chain too deep"}}

Additionally, "Failed to initialize USB device 'usb-hub'" is reported
on stderr.  Despite the command failure, the device was created.  This
is wrong.

Fix by avoiding qdev_init() for usb-hub creation, so we can ignore
errors cleanly.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/usb/bus.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/hw/usb/bus.c b/hw/usb/bus.c
index 3e85afe..5abfac0 100644
--- a/hw/usb/bus.c
+++ b/hw/usb/bus.c
@@ -315,19 +315,40 @@ USBDevice *usb_create(USBBus *bus, const char *name)
     return USB_DEVICE(dev);
 }
 
-USBDevice *usb_create_simple(USBBus *bus, const char *name)
+static USBDevice *usb_try_create_simple(USBBus *bus, const char *name,
+                                        Error **errp)
 {
-    USBDevice *dev = usb_create(bus, name);
-    int rc;
+    Error *err = NULL;
+    USBDevice *dev;
 
-    rc = qdev_init(&dev->qdev);
-    if (rc < 0) {
-        error_report("Failed to initialize USB device '%s'", name);
+    dev = USB_DEVICE(qdev_try_create(&bus->qbus, name));
+    if (!dev) {
+        error_setg(errp, "Failed to create USB device '%s'", name);
+        return NULL;
+    }
+    object_property_set_bool(OBJECT(dev), true, "realized", &err);
+    if (err) {
+        error_setg(errp, "Failed to initialize USB device '%s': %s",
+                   name, error_get_pretty(err));
+        error_free(err);
+        object_unparent(OBJECT(dev));
         return NULL;
     }
     return dev;
 }
 
+USBDevice *usb_create_simple(USBBus *bus, const char *name)
+{
+    Error *err = NULL;
+    USBDevice *dev = usb_try_create_simple(bus, name, &err);
+
+    if (!dev) {
+        error_report("%s", error_get_pretty(err));
+        error_free(err);
+    }
+    return dev;
+}
+
 static void usb_fill_port(USBPort *port, void *opaque, int index,
                           USBPortOps *ops, int speedmask)
 {
@@ -419,7 +440,7 @@ void usb_claim_port(USBDevice *dev, Error **errp)
     } else {
         if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
             /* Create a new hub and chain it on */
-            usb_create_simple(bus, "usb-hub");
+            usb_try_create_simple(bus, "usb-hub", NULL);
         }
         if (bus->nfree == 0) {
             error_setg(errp, "tried to attach usb device %s to a bus "
-- 
1.8.3.1

  parent reply	other threads:[~2015-02-18 10:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-18 10:08 [Qemu-devel] [PULL 0/8] usb: error handling fixes from Markus, make sysbus ehci arm-only Gerd Hoffmann
2015-02-18 10:08 ` [Qemu-devel] [PULL 1/8] usb: usb_create() can't fail, drop useless error handling Gerd Hoffmann
2015-02-18 10:08 ` [Qemu-devel] [PULL 2/8] usb: Improve -usbdevice error reporting a bit Gerd Hoffmann
2015-02-18 10:08 ` [Qemu-devel] [PULL 3/8] usb: Do not prefix error_setg() messages with "Error: " Gerd Hoffmann
2015-02-18 10:08 ` Gerd Hoffmann [this message]
2015-02-18 10:08 ` [Qemu-devel] [PULL 5/8] usb: Change usb_create_simple() to abort on failure Gerd Hoffmann
2015-02-18 10:08 ` [Qemu-devel] [PULL 6/8] r2d: Don't use legacy -usbdevice support for setting up board Gerd Hoffmann
2015-02-18 10:08 ` [Qemu-devel] [PULL 7/8] PPC: " Gerd Hoffmann
2015-02-18 10:08 ` [Qemu-devel] [PULL 8/8] Make sysbus EHCI devices ARM only by default Gerd Hoffmann
2015-02-25 13:13 ` [Qemu-devel] [PULL 0/8] usb: error handling fixes from Markus, make sysbus ehci arm-only Peter Maydell

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=1424254105-30237-5-git-send-email-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=armbru@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).