The three remaining orphan-creating sites in hw/usb are all
command-line and hotplug-adjacent code paths that always run after
the machine is up:

  usb_claim_port() auto-hub  -> child of the USB host controller
                                 (bus->qbus.parent)
  usbdevice_create()         -> /machine/peripheral-anon, same as
                                 -device without id=
  usb_braille_init()          -> ditto (a .usbdevice_init callback)

Switch to qdev_new()/qdev_try_new() with an explicit parent and use
plain qdev_realize() since the reference is now held by the parent's
child<> property.

Assisted-by: Kiro
Signed-off-by: Alexander Graf <[email protected]>
---
 hw/usb/bus.c        | 15 ++++++++++-----
 hw/usb/dev-serial.c |  3 ++-
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/hw/usb/bus.c b/hw/usb/bus.c
index d45cce7666..725b0303b1 100644
--- a/hw/usb/bus.c
+++ b/hw/usb/bus.c
@@ -410,9 +410,10 @@ 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 */
-            hub = USB_DEVICE(qdev_try_new_orphan("usb-hub"));
+            hub = USB_DEVICE(qdev_try_new(OBJECT(bus->qbus.parent),
+                                          "auto-hub[*]", "usb-hub"));
             if (hub) {
-                usb_realize_and_unref(hub, bus, NULL);
+                qdev_realize(DEVICE(hub), &bus->qbus, NULL);
             }
         }
         if (bus->nfree == 0) {
@@ -661,13 +662,17 @@ USBDevice *usbdevice_create(const char *driver)
         return NULL;
     }
 
-    dev = f->usbdevice_init ? f->usbdevice_init()
-                            : USB_DEVICE(qdev_new_orphan(f->name));
+    if (f->usbdevice_init) {
+        dev = f->usbdevice_init();
+    } else {
+        dev = USB_DEVICE(qdev_new(machine_get_container("peripheral-anon"),
+                                  "usbdevice[*]", f->name));
+    }
     if (!dev) {
         error_report("Failed to create USB device '%s'", f->name);
         return NULL;
     }
-    if (!usb_realize_and_unref(dev, bus, &err)) {
+    if (!qdev_realize(DEVICE(dev), &bus->qbus, &err)) {
         error_reportf_err(err, "Failed to initialize USB device '%s': ",
                           f->name);
         object_unparent(OBJECT(dev));
diff --git a/hw/usb/dev-serial.c b/hw/usb/dev-serial.c
index 81897b2d76..4ae3faf656 100644
--- a/hw/usb/dev-serial.c
+++ b/hw/usb/dev-serial.c
@@ -622,7 +622,8 @@ static USBDevice *usb_braille_init(void)
         return NULL;
     }
 
-    dev = USB_DEVICE(qdev_new_orphan("usb-braille"));
+    dev = USB_DEVICE(qdev_new(machine_get_container("peripheral-anon"),
+                              "usbdevice[*]", "usb-braille"));
     qdev_prop_set_chr(&dev->qdev, "chardev", cdrv);
     return dev;
 }
-- 
2.47.1


Reply via email to