From 392c1d2563750419c5152afa852db425e1bf4c01 Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Fri, 17 Feb 2012 09:52:24 +0100
Subject: [PATCH 3/5] usb: make usb_create_simple catch and pass up errors.

RH-Author: Gerd Hoffmann <kraxel@redhat.com>
Message-id: <1329472346-21224-3-git-send-email-kraxel@redhat.com>
Patchwork-id: 37403
O-Subject: [RHEL-6.3 qemu-kvm PATCH 2/4] usb: make usb_create_simple catch and pass up errors.
Bugzilla: 754349
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
RH-Acked-by: Amit Shah <amit.shah@redhat.com>
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>

Use qdev_init() instead of qdev_init_nofail(), usb device initialization
can fail, most common case being port and device speed mismatch.  Handle
failures correctly and pass up NULL pointers then.

Also fixup usb_create_simple() callers (only one was buggy) to properly
check for NULL pointers before referncing the usb_create_simple() return
value.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
(cherry picked from commit 2af2a1b8d05a1a64c5005ed930137632b9d5aa22)
---
 hw/usb-bt.c  |    3 +++
 hw/usb-bus.c |   11 +++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/usb-bt.c  |    3 +++
 hw/usb-bus.c |   11 +++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/hw/usb-bt.c b/hw/usb-bt.c
index e364513..021121f 100644
--- a/hw/usb-bt.c
+++ b/hw/usb-bt.c
@@ -535,6 +535,9 @@ USBDevice *usb_bt_init(HCIInfo *hci)
     if (!hci)
         return NULL;
     dev = usb_create_simple(NULL /* FIXME */, "usb-bt-dongle");
+    if (!dev) {
+        return NULL;
+    }
     s = DO_UPCAST(struct USBBtState, dev, dev);
     s->dev.opaque = s;
 
diff --git a/hw/usb-bus.c b/hw/usb-bus.c
index 67e0531..bca746f 100644
--- a/hw/usb-bus.c
+++ b/hw/usb-bus.c
@@ -139,10 +139,17 @@ USBDevice *usb_create(USBBus *bus, const char *name)
 USBDevice *usb_create_simple(USBBus *bus, const char *name)
 {
     USBDevice *dev = usb_create(bus, name);
+    int rc;
+
     if (!dev) {
-        hw_error("Failed to create USB device '%s'\n", name);
+        error_report("Failed to create USB device '%s'\n", name);
+        return NULL;
+    }
+    rc = qdev_init(&dev->qdev);
+    if (rc < 0) {
+        error_report("Failed to initialize USB device '%s'\n", name);
+        return NULL;
     }
-    qdev_init_nofail(&dev->qdev);
     return dev;
 }
 
-- 
1.7.7.6