Skip to contentSkip to conversation

hw/audio: Cleanups around QOM style

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

lore
Share & tools
lore
#0 lore raw

cover hw/audio: Cleanups around QOM style

- Use QOM's OBJECT_DECLARE*TYPE() to declare typedef / macros
- Use QOM cast macros instead of container_of()
- Open-code QDev helpers used few times

Philippe Mathieu-Daudé (5):
hw/audio/hda-codec: Avoid forward-declaring HDAAudioState hw/audio/es1370: Avoid forward-declaring ES1370State hw/audio/es1370: Replace container_of() by ES1370() QOM cast macro hw/audio/ac97: Replace container_of() by AC97() QOM cast macro hw/audio/pcspk: Open-code pcspk_init()

 hw/audio/ac97.c          |  2 +-
 hw/audio/es1370.c        | 10 +++++-----
 hw/audio/hda-codec.c     |  7 +++----
 hw/i386/pc.c             |  3 ++-
 hw/isa/i82378.c          |  5 ++++-
 hw/mips/jazz.c           |  5 ++++-
 include/hw/audio/pcspk.h | 10 ----------
 7 files changed, 19 insertions(+), 23 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/5 hw/audio/hda-codec: Avoid forward-declaring HDAAudioState

To avoid forward-declaring HDAAudioState, declare HDA_AUDIO QOM definitions before its use in the HDAAudioStream structure.

---
 hw/audio/hda-codec.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/audio/hda-codec.c +3 -4
index feb8f9e2bb..c51d8ba617 100644
@@ -145,7 +145,9 @@ static const char *fmt2name[] = {
     [ AUDIO_FORMAT_S32 ] = "PCM-S32",
 };
 
-typedef struct HDAAudioState HDAAudioState;
+#define TYPE_HDA_AUDIO "hda-audio"
+OBJECT_DECLARE_SIMPLE_TYPE(HDAAudioState, HDA_AUDIO)
+
 typedef struct HDAAudioStream HDAAudioStream;
 
 struct HDAAudioStream {
@@ -171,9 +173,6 @@ struct HDAAudioStream {
     int64_t buft_start;
 };
 
-#define TYPE_HDA_AUDIO "hda-audio"
-OBJECT_DECLARE_SIMPLE_TYPE(HDAAudioState, HDA_AUDIO)
-
 struct HDAAudioState {
     HDACodecDevice hda;
     const char *name;
Signature · 1 line
2.38.1
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] hw/audio/hda-codec: Avoid forward-declaring HDAAudioState

Quoted 7 lines · Philippe Mathieu-Daudé

To avoid forward-declaring HDAAudioState, declare HDA_AUDIO QOM definitions before its use in the HDAAudioStream structure.

---
  hw/audio/hda-codec.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)

r~

#3 lore raw

PATCH 2/5 hw/audio/es1370: Avoid forward-declaring ES1370State

In reply to Philippe Mathieu-Daudé

To avoid forward-declaring ES1370State, declare ES1370 QOM definitions before its use in the chan_bits structure.

---
 hw/audio/es1370.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/audio/es1370.c +3 -4
index 54cc19a637..ea76c9a734 100644
@@ -256,6 +256,9 @@ static void print_sctl (uint32_t val)
 #define lwarn(...)
 #endif
 
+#define TYPE_ES1370 "ES1370"
+OBJECT_DECLARE_SIMPLE_TYPE(ES1370State, ES1370)
+
 struct chan {
     uint32_t shift;
     uint32_t leftover;
@@ -278,7 +281,6 @@ struct ES1370State {
     uint32_t codec;
     uint32_t sctl;
 };
-typedef struct ES1370State ES1370State;
 
 struct chan_bits {
     uint32_t ctl_en;
@@ -292,9 +294,6 @@ struct chan_bits {
                        uint32_t *old_freq, uint32_t *new_freq);
 };
 
-#define TYPE_ES1370 "ES1370"
-OBJECT_DECLARE_SIMPLE_TYPE(ES1370State, ES1370)
-
 static void es1370_dac1_calc_freq (ES1370State *s, uint32_t ctl,
                                    uint32_t *old_freq, uint32_t *new_freq);
 static void es1370_dac2_and_adc_calc_freq (ES1370State *s, uint32_t ctl,
Signature · 1 line
2.38.1
#5 lore raw

PATCH 3/5 hw/audio/es1370: Replace container_of() by ES1370() QOM cast macro

In reply to Philippe Mathieu-Daudé
---
 hw/audio/es1370.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/audio/es1370.c +2 -1
index ea76c9a734..4f738a0ad8 100644
@@ -843,7 +843,8 @@ static const VMStateDescription vmstate_es1370 = {
 
 static void es1370_on_reset(DeviceState *dev)
 {
-    ES1370State *s = container_of(dev, ES1370State, dev.qdev);
+    ES1370State *s = ES1370(dev);
+
     es1370_reset (s);
 }
 
Signature · 1 line
2.38.1
#7 lore raw

PATCH 4/5 hw/audio/ac97: Replace container_of() by AC97() QOM cast macro

In reply to Philippe Mathieu-Daudé
---
 hw/audio/ac97.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/audio/ac97.c +1 -1
index 364cdfa733..b3a45bb49f 100644
@@ -1295,7 +1295,7 @@ static const MemoryRegionOps ac97_io_nabm_ops = {
 
 static void ac97_on_reset(DeviceState *dev)
 {
-    AC97LinkState *s = container_of(dev, AC97LinkState, dev.qdev);
+    AC97LinkState *s = AC97(dev);
 
     reset_bm_regs(s, &s->bm_regs[0]);
     reset_bm_regs(s, &s->bm_regs[1]);
Signature · 1 line
2.38.1
#9 lore raw

PATCH 5/5 hw/audio/pcspk: Open-code pcspk_init()

In reply to Philippe Mathieu-Daudé

pcspk_init() is used in 3 places and is 2-lines of code.

---
 hw/i386/pc.c             |  3 ++-
 hw/isa/i82378.c          |  5 ++++-
 hw/mips/jazz.c           |  5 ++++-
 include/hw/audio/pcspk.h | 10 ----------
 4 files changed, 10 insertions(+), 13 deletions(-)
hw/i386/pc.c +2 -1
index 6e592bd969..73bd98997f 100644
@@ -1319,7 +1319,8 @@ void pc_basic_device_init(struct PCMachineState *pcms,
             /* connect PIT to output control line of the HPET */
             qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
         }
-        pcspk_init(pcms->pcspk, isa_bus, pit);
+        object_property_set_link(OBJECT(pcms->pcspk), "pit", OBJECT(pit), NULL);
+        isa_realize_and_unref(pcms->pcspk, isa_bus, &error_fatal);
     }
 
     /* Super I/O */
hw/isa/i82378.c +4 -1
index e3322e03bf..f0964cdcd9 100644
@@ -68,6 +68,7 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
     uint8_t *pci_conf;
     ISABus *isabus;
     ISADevice *pit;
+    ISADevice *snd;
 
     pci_conf = pci->config;
     pci_set_word(pci_conf + PCI_COMMAND,
@@ -102,7 +103,9 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
     pit = i8254_pit_init(isabus, 0x40, 0, NULL);
 
     /* speaker */
-    pcspk_init(isa_new(TYPE_PC_SPEAKER), isabus, pit);
+    snd = isa_new(TYPE_PC_SPEAKER);
+    object_property_set_link(OBJECT(snd), "pit", OBJECT(pit), NULL);
+    isa_realize_and_unref(snd, isabus, &error_fatal);
 
     /* 2 82C37 (dma) */
     isa_create_simple(isabus, "i82374");
hw/mips/jazz.c +4 -1
index 6aefe9a61b..a4e035cd66 100644
@@ -144,6 +144,7 @@ static void mips_jazz_init(MachineState *machine,
     SysBusDevice *sysbus;
     ISABus *isa_bus;
     ISADevice *pit;
+    ISADevice *snd;
     DriveInfo *fds[MAX_FD];
     MemoryRegion *bios = g_new(MemoryRegion, 1);
     MemoryRegion *bios2 = g_new(MemoryRegion, 1);
@@ -252,7 +253,9 @@ static void mips_jazz_init(MachineState *machine,
     isa_bus_irqs(isa_bus, i8259);
     i8257_dma_init(isa_bus, 0);
     pit = i8254_pit_init(isa_bus, 0x40, 0, NULL);
-    pcspk_init(isa_new(TYPE_PC_SPEAKER), isa_bus, pit);
+    snd = isa_new(TYPE_PC_SPEAKER);
+    object_property_set_link(OBJECT(snd), "pit", OBJECT(pit), NULL);
+    isa_realize_and_unref(snd, isa_bus, &error_fatal);
 
     /* Video card */
     switch (jazz_model) {
include/hw/audio/pcspk.h +0 -10
index 9506179587..6be75a6b86 100644
@@ -25,16 +25,6 @@
 #ifndef HW_PCSPK_H
 #define HW_PCSPK_H
 
-#include "hw/isa/isa.h"
-#include "hw/qdev-properties.h"
-#include "qapi/error.h"
-
 #define TYPE_PC_SPEAKER "isa-pcspk"
 
-static inline void pcspk_init(ISADevice *isadev, ISABus *bus, ISADevice *pit)
-{
-    object_property_set_link(OBJECT(isadev), "pit", OBJECT(pit), NULL);
-    isa_realize_and_unref(isadev, bus, &error_fatal);
-}
-
 #endif /* HW_PCSPK_H */
Signature · 1 line
2.38.1