[RFC PATCH v2 008/137] ssi, usb: Make bus-layer creators take a QOM parent

Alexander Graf <[email protected]>
Newsgroups org.nongnu.qemu-arm,org.nongnu.qemu-devel,org.nongnu.qemu-riscv
Message-ID <[email protected]>
Apply the rename-and-reintroduce treatment to the remaining
bus-layer helpers that wrap qdev_new_orphan():

  ssi_create_peripheral()  -> ssi_create_peripheral_orphan()
  usb_create_simple()      -> usb_create_simple_orphan()

and reintroduce the short names with a leading (parent, id, ...)
pair.  Also add a usb_new(parent, id, type) helper for callers that
need to set properties before realize.

sd-bus, apple-desktop-bus, HDA, aux-bus and spapr-vio have no bus-
layer creation wrapper: their onboard devices are open-coded via
qdev_new_orphan() at each call site and will be converted directly
to qdev_new(parent, id, type) in the per-subdirectory conversion
patches.

The mechanical rename part is generated by

  spatch --sp-file scripts/coccinelle/qom-parent/ssi-usb-orphan.cocci \
         --in-place --include-headers --dir .

or equivalently

  git ls-files '*.[ch]' '*.[ch].inc' '*.rst' '*.py' | \
      grep -v '^subprojects/' | \
      xargs sed -i \
          's/\bssi_create_peripheral\b/&_orphan/g;
           s/\busb_create_simple\b/&_orphan/g'

AI-used-for: code (refactoring)
Signed-off-by: Alexander Graf <[email protected]>
---
 hw/arm/stellaris.c                             |  2 +-
 hw/hppa/machine.c                              |  4 ++--
 hw/mips/loongson3_virt.c                       |  4 ++--
 hw/ppc/mac_newworld.c                          |  4 ++--
 hw/ppc/sam460ex.c                              |  4 ++--
 hw/ppc/spapr.c                                 |  4 ++--
 hw/riscv/sifive_u.c                            |  2 +-
 hw/sh4/r2d.c                                   |  2 +-
 hw/ssi/ssi.c                                   | 11 ++++++++++-
 hw/vmapple/vmapple.c                           |  4 ++--
 include/hw/ssi/ssi.h                           |  6 ++++--
 include/hw/usb/usb.h                           | 18 +++++++++++++++++-
 .../coccinelle/qom-parent/ssi-usb-orphan.cocci | 18 ++++++++++++++++++
 13 files changed, 64 insertions(+), 19 deletions(-)
 create mode 100644 scripts/coccinelle/qom-parent/ssi-usb-orphan.cocci

diff --git a/hw/arm/stellaris.c b/hw/arm/stellaris.c
index f9d2a61f9c..7a18d98477 100644
--- a/hw/arm/stellaris.c
+++ b/hw/arm/stellaris.c
@@ -1300,7 +1300,7 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
              *  - Make the ssd0323 OLED controller chipselect active-low
              */
             bus = qdev_get_child_bus(dev, "ssi");
-            sddev = ssi_create_peripheral(bus, "ssi-sd");
+            sddev = ssi_create_peripheral_orphan(bus, "ssi-sd");
 
             dinfo = drive_get(IF_SD, 0, 0);
             blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
diff --git a/hw/hppa/machine.c b/hw/hppa/machine.c
index 91fe4845c4..44dab9225a 100644
--- a/hw/hppa/machine.c
+++ b/hw/hppa/machine.c
@@ -446,8 +446,8 @@ static void machine_HP_common_init_tail(MachineState *machine, PCIBus *pci_bus,
         pci_create_simple_orphan(pci_bus, -1, "pci-ohci");
         usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                           &error_abort));
-        usb_create_simple(usb_bus, "usb-kbd");
-        usb_create_simple(usb_bus, "usb-mouse");
+        usb_create_simple_orphan(usb_bus, "usb-kbd");
+        usb_create_simple_orphan(usb_bus, "usb-mouse");
     }
 
     /* register power switch emulation */
diff --git a/hw/mips/loongson3_virt.c b/hw/mips/loongson3_virt.c
index 1729eb1c0b..f43c231004 100644
--- a/hw/mips/loongson3_virt.c
+++ b/hw/mips/loongson3_virt.c
@@ -468,8 +468,8 @@ static inline void loongson3_virt_devices_init(MachineState *machine,
         pci_create_simple_orphan(pci_bus, -1, "pci-ohci");
         usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                           &error_abort));
-        usb_create_simple(usb_bus, "usb-kbd");
-        usb_create_simple(usb_bus, "usb-tablet");
+        usb_create_simple_orphan(usb_bus, "usb-kbd");
+        usb_create_simple_orphan(usb_bus, "usb-tablet");
     }
 
     pci_init_nic_devices(pci_bus, mc->default_nic);
diff --git a/hw/ppc/mac_newworld.c b/hw/ppc/mac_newworld.c
index 0df953b9e1..72baeef60a 100644
--- a/hw/ppc/mac_newworld.c
+++ b/hw/ppc/mac_newworld.c
@@ -425,8 +425,8 @@ static void ppc_core99_init(MachineState *machine)
 
             usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                               &error_abort));
-            usb_create_simple(usb_bus, "usb-kbd");
-            usb_create_simple(usb_bus, "usb-mouse");
+            usb_create_simple_orphan(usb_bus, "usb-kbd");
+            usb_create_simple_orphan(usb_bus, "usb-mouse");
         }
     }
 
diff --git a/hw/ppc/sam460ex.c b/hw/ppc/sam460ex.c
index 767a5eb596..39e704dfaa 100644
--- a/hw/ppc/sam460ex.c
+++ b/hw/ppc/sam460ex.c
@@ -398,8 +398,8 @@ static void sam460ex_init(MachineState *machine)
     sysbus_connect_irq(sbdev, 0, qdev_get_gpio_in(uic[2], 30));
     usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                       &error_abort));
-    usb_create_simple(usb_bus, "usb-kbd");
-    usb_create_simple(usb_bus, "usb-mouse");
+    usb_create_simple_orphan(usb_bus, "usb-kbd");
+    usb_create_simple_orphan(usb_bus, "usb-mouse");
 
     /* PCIe buses */
     dev = qdev_new_orphan(TYPE_PPC460EX_PCIE_HOST);
diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
index 4ead6ba664..e09b529ebc 100644
--- a/hw/ppc/spapr.c
+++ b/hw/ppc/spapr.c
@@ -3082,8 +3082,8 @@ static void spapr_machine_init(MachineState *machine)
 
             usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                               &error_abort));
-            usb_create_simple(usb_bus, "usb-kbd");
-            usb_create_simple(usb_bus, "usb-mouse");
+            usb_create_simple_orphan(usb_bus, "usb-kbd");
+            usb_create_simple_orphan(usb_bus, "usb-mouse");
         }
     }
 
diff --git a/hw/riscv/sifive_u.c b/hw/riscv/sifive_u.c
index db6d984f94..47ce75ef11 100644
--- a/hw/riscv/sifive_u.c
+++ b/hw/riscv/sifive_u.c
@@ -606,7 +606,7 @@ static void sifive_u_machine_init(MachineState *machine)
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs);
 
     /* Connect an SD card to SPI2 */
-    sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd");
+    sd_dev = ssi_create_peripheral_orphan(s->soc.spi2.spi, "ssi-sd");
 
     sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
diff --git a/hw/sh4/r2d.c b/hw/sh4/r2d.c
index 0b502ca392..6f8d270fae 100644
--- a/hw/sh4/r2d.c
+++ b/hw/sh4/r2d.c
@@ -320,7 +320,7 @@ static void r2d_init(MachineState *machine)
     /* USB keyboard */
     usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                       &error_abort));
-    usb_create_simple(usb_bus, "usb-kbd");
+    usb_create_simple_orphan(usb_bus, "usb-kbd");
 
     /* Todo: register on board registers */
     memset(&boot_params, 0, sizeof(boot_params));
diff --git a/hw/ssi/ssi.c b/hw/ssi/ssi.c
index b1935271f0..6031cde7b0 100644
--- a/hw/ssi/ssi.c
+++ b/hw/ssi/ssi.c
@@ -138,7 +138,16 @@ bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp)
     return qdev_realize_and_unref(dev, &bus->parent_obj, errp);
 }
 
-DeviceState *ssi_create_peripheral(SSIBus *bus, const char *name)
+DeviceState *ssi_create_peripheral(Object *parent, const char *id,
+                                    SSIBus *bus, const char *type)
+{
+    DeviceState *dev = qdev_new(parent, id, type);
+
+    qdev_realize(dev, BUS(bus), &error_fatal);
+    return dev;
+}
+
+DeviceState *ssi_create_peripheral_orphan(SSIBus *bus, const char *name)
 {
     DeviceState *dev = qdev_new_orphan(name);
 
diff --git a/hw/vmapple/vmapple.c b/hw/vmapple/vmapple.c
index bf19faa04a..4d218748c2 100644
--- a/hw/vmapple/vmapple.c
+++ b/hw/vmapple/vmapple.c
@@ -425,8 +425,8 @@ static void create_pcie(VMAppleMachineState *vms)
 
         usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                           &error_fatal));
-        usb_create_simple(usb_bus, "usb-kbd");
-        usb_create_simple(usb_bus, "usb-tablet");
+        usb_create_simple_orphan(usb_bus, "usb-kbd");
+        usb_create_simple_orphan(usb_bus, "usb-tablet");
     }
 }
 
diff --git a/include/hw/ssi/ssi.h b/include/hw/ssi/ssi.h
index 153bf0cb9c..36ed002281 100644
--- a/include/hw/ssi/ssi.h
+++ b/include/hw/ssi/ssi.h
@@ -81,7 +81,9 @@ extern const VMStateDescription vmstate_ssi_peripheral;
     .offset     = vmstate_offset_value(_state, _field, SSIPeripheral), \
 }
 
-DeviceState *ssi_create_peripheral(SSIBus *bus, const char *name);
+DeviceState *ssi_create_peripheral(Object *parent, const char *id,
+                                    SSIBus *bus, const char *type);
+DeviceState *ssi_create_peripheral_orphan(SSIBus *bus, const char *name);
 /**
  * ssi_realize_and_unref: realize and unref an SSI peripheral
  * @dev: SSI peripheral to realize
@@ -95,7 +97,7 @@ DeviceState *ssi_create_peripheral(SSIBus *bus, const char *name);
  * This function is useful if you have created @dev via qdev_new_orphan()
  * (which takes a reference to the device it returns to you), so that
  * you can set properties on it before realizing it. If you don't need
- * to set properties then ssi_create_peripheral() is probably better (as it
+ * to set properties then ssi_create_peripheral_orphan() is probably better (as it
  * does the create, init and realize in one step).
  *
  * If you are embedding the SSI peripheral into another QOM device and
diff --git a/include/hw/usb/usb.h b/include/hw/usb/usb.h
index 6ef190523f..1ae1f40aa9 100644
--- a/include/hw/usb/usb.h
+++ b/include/hw/usb/usb.h
@@ -584,7 +584,23 @@ static inline bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **er
     return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
 }
 
-static inline USBDevice *usb_create_simple(USBBus *bus, const char *name)
+static inline USBDevice *usb_new(Object *parent, const char *id,
+                                  const char *type)
+{
+    return USB_DEVICE(qdev_new(parent, id, type));
+}
+
+static inline USBDevice *usb_create_simple(Object *parent, const char *id,
+                                            USBBus *bus, const char *type)
+{
+    USBDevice *dev = usb_new(parent, id, type);
+
+    qdev_realize(DEVICE(dev), BUS(bus), &error_abort);
+    return dev;
+}
+
+static inline USBDevice *usb_create_simple_orphan(USBBus *bus,
+                                                    const char *name)
 {
     USBDevice *dev = USB_DEVICE(qdev_new_orphan(name));
 
diff --git a/scripts/coccinelle/qom-parent/ssi-usb-orphan.cocci b/scripts/coccinelle/qom-parent/ssi-usb-orphan.cocci
new file mode 100644
index 0000000000..7d201536d8
--- /dev/null
+++ b/scripts/coccinelle/qom-parent/ssi-usb-orphan.cocci
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+//
+// Rename ssi_create_peripheral()/usb_create_simple() to *_orphan()
+// so that the short names can be reintroduced with a mandatory
+// (parent, id, ...) signature.
+//
+// spatch --sp-file scripts/coccinelle/qom-parent/ssi-usb-orphan.cocci \
+//        --in-place --include-headers --dir .
+
+@@
+@@
+- ssi_create_peripheral
++ ssi_create_peripheral_orphan
+
+@@
+@@
+- usb_create_simple
++ usb_create_simple_orphan
-- 
2.47.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.