Skip to contentSkip to conversation

hw/usb: Cleanups around QOM style

PATCH In review Philippe Mathieu-Daudé 2 participants 17 messages Feb 20, 2023

lore
Share & tools
lore
#0 lore raw

cover hw/usb: Cleanups around QOM style

- Use QOM's OBJECT_DECLARE*TYPE() to declare typedef / macros
- Use QOM cast macros instead of container_of()

Philippe Mathieu-Daudé (9):
hw/usb/dev-smartcard-reader: Avoid forward-declaring CCIDBus hw/usb/hcd-ohci: Use OHCIState type definition hw/usb/hcd-ohci-pci: Use QOM type-safe cast conversion macro hw/usb/hcd-uhci: Declare QOM macros using OBJECT_DECLARE_TYPE() hw/usb/hcd-uhci: Replace container_of() by UHCI_GET_CLASS() QOM macro hw/usb/hcd-xhci-nec: Declare QOM macros for NEC_XHCI hw/usb/hcd-xhci-nec: Replace container_of() by NEC_XHCI() QOM cast
macro
hw/usb/u2f: Declare QOM macros using OBJECT_DECLARE_TYPE() hw/usb/u2f-passthru: Use QOM type-safe cast conversion macros

 hw/usb/dev-smartcard-reader.c |  7 +++----
 hw/usb/hcd-ohci-pci.c         |  4 ++--
 hw/usb/hcd-ohci.c             |  2 +-
 hw/usb/hcd-ohci.h             | 10 ++++++----
 hw/usb/hcd-uhci.c             |  7 ++-----
 hw/usb/hcd-uhci.h             |  2 +-
 hw/usb/hcd-xhci-nec.c         |  8 +++++---
 hw/usb/u2f-passthru.c         |  7 ++++---
 hw/usb/u2f.h                  | 16 +++++-----------
 9 files changed, 29 insertions(+), 34 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/9 hw/usb/dev-smartcard-reader: Avoid forward-declaring CCIDBus

To avoid forward-declaring CCIDBus, declare CCID_BUS QOM definitions before its use in the USBCCIDState structure.

---
 hw/usb/dev-smartcard-reader.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/usb/dev-smartcard-reader.c +3 -4
index 28164d89be..be0a4fc3bc 100644
@@ -278,7 +278,9 @@ typedef struct BulkIn {
 struct CCIDBus {
     BusState qbus;
 };
-typedef struct CCIDBus CCIDBus;
+
+#define TYPE_CCID_BUS "ccid-bus"
+OBJECT_DECLARE_SIMPLE_TYPE(CCIDBus, CCID_BUS)
 
 /*
  * powered - defaults to true, changed by PowerOn/PowerOff messages
@@ -1174,9 +1176,6 @@ static Property ccid_props[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-#define TYPE_CCID_BUS "ccid-bus"
-OBJECT_DECLARE_SIMPLE_TYPE(CCIDBus, CCID_BUS)
-
 static const TypeInfo ccid_bus_info = {
     .name = TYPE_CCID_BUS,
     .parent = TYPE_BUS,
Signature · 1 line
2.38.1
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] hw/usb/dev-smartcard-reader: Avoid forward-declaring CCIDBus

Quoted 7 lines · Philippe Mathieu-Daudé

To avoid forward-declaring CCIDBus, declare CCID_BUS QOM definitions before its use in the USBCCIDState structure.

---
  hw/usb/dev-smartcard-reader.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)

r~

#3 lore raw

PATCH 2/9 hw/usb/hcd-ohci: Use OHCIState type definition

In reply to Philippe Mathieu-Daudé

Forward-define the type first, then use it for the ohci_die() handler.

---
 hw/usb/hcd-ohci.c |  2 +-
 hw/usb/hcd-ohci.h | 10 ++++++----
 2 files changed, 7 insertions(+), 5 deletions(-)
hw/usb/hcd-ohci.c +1 -1
index 9d68036d23..7ecf9b15b7 100644
@@ -1827,7 +1827,7 @@ static USBBusOps ohci_bus_ops = {
 void usb_ohci_init(OHCIState *ohci, DeviceState *dev, uint32_t num_ports,
                    dma_addr_t localmem_base, char *masterbus,
                    uint32_t firstport, AddressSpace *as,
-                   void (*ohci_die_fn)(struct OHCIState *), Error **errp)
+                   void (*ohci_die_fn)(OHCIState *), Error **errp)
 {
     Error *err = NULL;
     int i;
hw/usb/hcd-ohci.h +6 -4
index 11ac57058d..8a9ba8b959 100644
@@ -33,7 +33,9 @@ typedef struct OHCIPort {
     uint32_t ctrl;
 } OHCIPort;
 
-typedef struct OHCIState {
+typedef struct OHCIState OHCIState;
+
+struct OHCIState {
     USBBus bus;
     qemu_irq irq;
     MemoryRegion mem;
@@ -89,8 +91,8 @@ typedef struct OHCIState {
     uint32_t async_td;
     bool async_complete;
 
-    void (*ohci_die)(struct OHCIState *ohci);
-} OHCIState;
+    void (*ohci_die)(OHCIState *ohci);
+};
 
 #define TYPE_SYSBUS_OHCI "sysbus-ohci"
 OBJECT_DECLARE_SIMPLE_TYPE(OHCISysBusState, SYSBUS_OHCI)
@@ -112,7 +114,7 @@ extern const VMStateDescription vmstate_ohci_state;
 void usb_ohci_init(OHCIState *ohci, DeviceState *dev, uint32_t num_ports,
                    dma_addr_t localmem_base, char *masterbus,
                    uint32_t firstport, AddressSpace *as,
-                   void (*ohci_die_fn)(struct OHCIState *), Error **errp);
+                   void (*ohci_die_fn)(OHCIState *), Error **errp);
 void ohci_bus_stop(OHCIState *ohci);
 void ohci_stop_endpoints(OHCIState *ohci);
 void ohci_hard_reset(OHCIState *ohci);
Signature · 1 line
2.38.1
#5 lore raw

PATCH 3/9 hw/usb/hcd-ohci-pci: Use QOM type-safe cast conversion macro

In reply to Philippe Mathieu-Daudé

Use the PCI_DEVICE() QOM cast macros to avoid accessing internal fields.

---
 hw/usb/hcd-ohci-pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/usb/hcd-ohci-pci.c +2 -2
index 6b630d35a7..3234408f1a 100644
@@ -52,11 +52,11 @@ struct OHCIPCIState {
 static void ohci_pci_die(struct OHCIState *ohci)
 {
     OHCIPCIState *dev = container_of(ohci, OHCIPCIState, state);
+    PCIDevice *pdev = PCI_DEVICE(dev);
 
     ohci_sysbus_die(ohci);
 
-    pci_set_word(dev->parent_obj.config + PCI_STATUS,
-                 PCI_STATUS_DETECTED_PARITY);
+    pci_set_word(pdev->config + PCI_STATUS, PCI_STATUS_DETECTED_PARITY);
 }
 
 static void usb_ohci_realize_pci(PCIDevice *dev, Error **errp)
Signature · 1 line
2.38.1
#6 lore raw

PATCH 4/9 hw/usb/hcd-uhci: Declare QOM macros using OBJECT_DECLARE_TYPE()

In reply to Philippe Mathieu-Daudé

The automatic conversion done during commit a489d1951c ("Use OBJECT_DECLARE_TYPE when possible") missed this model because the typedefs are in a different file unit (hcd-uhci.c) than where the DECLARE_INSTANCE_CHECKER() is (hcd-uhci.h). Manually convert to OBJECT_DECLARE_TYPE().

---
 hw/usb/hcd-uhci.c | 2 --
 hw/usb/hcd-uhci.h | 2 +-
 2 files changed, 1 insertion(+), 3 deletions(-)
hw/usb/hcd-uhci.c +0 -2
index 30ae0104bb..f77ffda69a 100644
@@ -60,9 +60,7 @@ enum {
     TD_RESULT_ASYNC_CONT,
 };
 
-typedef struct UHCIState UHCIState;
 typedef struct UHCIAsync UHCIAsync;
-typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass;
 
 struct UHCIPCIDeviceClass {
     PCIDeviceClass parent_class;
hw/usb/hcd-uhci.h +1 -1
index e0fdb98ef1..69f8b40c49 100644
@@ -75,7 +75,7 @@ typedef struct UHCIState {
 } UHCIState;
 
 #define TYPE_UHCI "pci-uhci-usb"
-DECLARE_INSTANCE_CHECKER(UHCIState, UHCI, TYPE_UHCI)
+OBJECT_DECLARE_TYPE(UHCIState, UHCIPCIDeviceClass, UHCI)
 
 typedef struct UHCIInfo {
     const char *name;
Signature · 1 line
2.38.1
Reviewed-by on v1 4/9
#7 lore raw

Re: [PATCH 4/9] hw/usb/hcd-uhci: Declare QOM macros using OBJECT_DECLARE_TYPE()

Quoted 11 lines · Philippe Mathieu-Daudé

The automatic conversion done during commit a489d1951c ("Use OBJECT_DECLARE_TYPE when possible") missed this model because the typedefs are in a different file unit (hcd-uhci.c) than where the DECLARE_INSTANCE_CHECKER() is (hcd-uhci.h). Manually convert to OBJECT_DECLARE_TYPE().

---
  hw/usb/hcd-uhci.c | 2 --
  hw/usb/hcd-uhci.h | 2 +-
  2 files changed, 1 insertion(+), 3 deletions(-)

r~

#8 lore raw

PATCH 5/9 hw/usb/hcd-uhci: Replace container_of() by UHCI_GET_CLASS() QOM macro

In reply to Philippe Mathieu-Daudé

By using the QOM UHCI_GET_CLASS() cast macro we don't to use the intermediate PCIDeviceClass variable.

---
 hw/usb/hcd-uhci.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
hw/usb/hcd-uhci.c +2 -3
index f77ffda69a..8ac1175ad2 100644
@@ -1159,8 +1159,7 @@ static USBBusOps uhci_bus_ops = {
 void usb_uhci_common_realize(PCIDevice *dev, Error **errp)
 {
     Error *err = NULL;
-    PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
-    UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class);
+    UHCIPCIDeviceClass *u = UHCI_GET_CLASS(dev);
     UHCIState *s = UHCI(dev);
     uint8_t *pci_conf = s->dev.config;
     int i;
@@ -1267,7 +1266,7 @@ void uhci_data_class_init(ObjectClass *klass, void *data)
 {
     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
     DeviceClass *dc = DEVICE_CLASS(klass);
-    UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class);
+    UHCIPCIDeviceClass *u = UHCI_CLASS(klass);
     UHCIInfo *info = data;
 
     k->realize = info->realize ? info->realize : usb_uhci_common_realize;
Signature · 1 line
2.38.1
#10 lore raw

PATCH 6/9 hw/usb/hcd-xhci-nec: Declare QOM macros for NEC_XHCI

In reply to Philippe Mathieu-Daudé

NEC_XHCI is a QOM object type. Declare its macros / typedefs using OBJECT_DECLARE_SIMPLE_TYPE().

---
 hw/usb/hcd-xhci-nec.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/usb/hcd-xhci-nec.c +4 -2
index 13c9ac5dbd..d732be0ef9 100644
@@ -27,14 +27,16 @@
 
 #include "hcd-xhci-pci.h"
 
-typedef struct XHCINecState {
+OBJECT_DECLARE_SIMPLE_TYPE(XHCINecState, NEC_XHCI)
+
+struct XHCINecState {
     /*< private >*/
     XHCIPciState parent_obj;
     /*< public >*/
     uint32_t flags;
     uint32_t intrs;
     uint32_t slots;
-} XHCINecState;
+};
 
 static Property nec_xhci_properties[] = {
     DEFINE_PROP_ON_OFF_AUTO("msi", XHCIPciState, msi, ON_OFF_AUTO_AUTO),
Signature · 1 line
2.38.1
#12 lore raw

PATCH 7/9 hw/usb/hcd-xhci-nec: Replace container_of() by NEC_XHCI() QOM cast macro

In reply to Philippe Mathieu-Daudé
---
 hw/usb/hcd-xhci-nec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/hcd-xhci-nec.c +1 -1
index d732be0ef9..328e5bfe7c 100644
@@ -53,7 +53,7 @@ static Property nec_xhci_properties[] = {
 static void nec_xhci_instance_init(Object *obj)
 {
     XHCIPciState *pci = XHCI_PCI(obj);
-    XHCINecState *nec = container_of(pci, XHCINecState, parent_obj);
+    XHCINecState *nec = NEC_XHCI(obj);
 
     pci->xhci.flags    = nec->flags;
     pci->xhci.numintrs = nec->intrs;
Signature · 1 line
2.38.1
#14 lore raw

PATCH 8/9 hw/usb/u2f: Declare QOM macros using OBJECT_DECLARE_TYPE()

In reply to Philippe Mathieu-Daudé

hw/usb/u2f.h was added by commit 80e267f1d1 ("hw/usb: Add U2F key base class"), almost the same time of the automatic conversion done by commit c821774a3b ("Use OBJECT_DECLARE_TYPE where posible"). Manually convert to OBJECT_DECLARE_TYPE().

---
 hw/usb/u2f.h | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
hw/usb/u2f.h +5 -11
index a408a82927..8bff13141a 100644
@@ -31,22 +31,16 @@
 #define U2FHID_PACKET_SIZE 64
 #define U2FHID_PENDING_IN_NUM 32
 
-typedef struct U2FKeyState U2FKeyState;
 typedef struct U2FKeyInfo U2FKeyInfo;
 
 #define TYPE_U2F_KEY "u2f-key"
-#define U2F_KEY(obj) \
-    OBJECT_CHECK(U2FKeyState, (obj), TYPE_U2F_KEY)
-#define U2F_KEY_CLASS(klass) \
-    OBJECT_CLASS_CHECK(U2FKeyClass, (klass), TYPE_U2F_KEY)
-#define U2F_KEY_GET_CLASS(obj) \
-    OBJECT_GET_CLASS(U2FKeyClass, (obj), TYPE_U2F_KEY)
+OBJECT_DECLARE_TYPE(U2FKeyState, U2FKeyClass, U2F_KEY)
 
 /*
  * Callbacks to be used by the U2F key base device (i.e. hw/u2f.c)
  * to interact with its variants (i.e. hw/u2f-*.c)
  */
-typedef struct U2FKeyClass {
+struct U2FKeyClass {
     /*< private >*/
     USBDeviceClass parent_class;
 
@@ -55,12 +49,12 @@ typedef struct U2FKeyClass {
                             const uint8_t packet[U2FHID_PACKET_SIZE]);
     void (*realize)(U2FKeyState *key, Error **errp);
     void (*unrealize)(U2FKeyState *key);
-} U2FKeyClass;
+};
 
 /*
  * State of the U2F key base device (i.e. hw/u2f.c)
  */
-typedef struct U2FKeyState {
+struct U2FKeyState {
     USBDevice dev;
     USBEndpoint *ep;
     uint8_t idle;
@@ -70,7 +64,7 @@ typedef struct U2FKeyState {
     uint8_t pending_in_start;
     uint8_t pending_in_end;
     uint8_t pending_in_num;
-} U2FKeyState;
+};
 
 /*
  * API to be used by the U2F key device variants (i.e. hw/u2f-*.c)
Signature · 1 line
2.38.1
Reviewed-by on v1 8/9
#15 lore raw

Re: [PATCH 8/9] hw/usb/u2f: Declare QOM macros using OBJECT_DECLARE_TYPE()

Quoted 9 lines · Philippe Mathieu-Daudé

hw/usb/u2f.h was added by commit 80e267f1d1 ("hw/usb: Add U2F key base class"), almost the same time of the automatic conversion done by commit c821774a3b ("Use OBJECT_DECLARE_TYPE where posible"). Manually convert to OBJECT_DECLARE_TYPE().

---
  hw/usb/u2f.h | 16 +++++-----------
  1 file changed, 5 insertions(+), 11 deletions(-)

r~

#16 lore raw

PATCH 9/9 hw/usb/u2f-passthru: Use QOM type-safe cast conversion macros

In reply to Philippe Mathieu-Daudé

Use the QOM cast macros to avoid accessing internal fields.

---
 hw/usb/u2f-passthru.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
hw/usb/u2f-passthru.c +4 -3
index fc93429c9c..b99c0b622f 100644
@@ -299,7 +299,7 @@ static void u2f_passthru_recv_from_host(U2FPassthruState *key,
     if (transaction->resp_size >= transaction->resp_bcnt) {
         u2f_transaction_close(key, cid);
     }
-    u2f_send_to_guest(&key->base, packet);
+    u2f_send_to_guest(U2F_KEY(key), packet);
 }
 
 static void u2f_passthru_read(void *opaque)
@@ -316,9 +316,10 @@ static void u2f_passthru_read(void *opaque)
 
     ret = read(key->hidraw_fd, packet, sizeof(packet));
     if (ret < 0) {
+        USBDevice *udev = USB_DEVICE(key);
         /* Detach */
-        if (base->dev.attached) {
-            usb_device_detach(&base->dev);
+        if (udev->attached) {
+            usb_device_detach(udev);
             u2f_passthru_reset(key);
         }
         return;
Signature · 1 line
2.38.1