@samitouri / QOSamiQemu / commits / b4b511bd19

audio/alsa: convert to QOM lifecycle methods

Migrate the ALSA audio backend from the legacy driver init/fini callbacks to proper QOM realize method. The alsa_audio_init() function is replaced with audio_alsa_realize(), which performs the same ALSA option initialization before delegating to the parent class realize method. The empty alsa_audio_fini() is removed. The Audiodev pointer stored in ALSAVoiceOut and ALSAVoiceIn is removed as it can now be accessed through hw->s->dev. Reviewed-by: Mark Cave-Ayland <mark.caveayland@nutanix.com> Reviewed-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

Marc-André Lureau committed Jan 16, 2026 at 11:52 UTC b4b511bd19f717fb4c6d2928987b9b2d8b417e7b
1 file changed +20 -23
audio/alsaaudio.c
+20 -23
@@ -40,19 +40,14 @@
40 #define TYPE_AUDIO_ALSA "audio-alsa"
41 OBJECT_DECLARE_SIMPLE_TYPE(AudioALSA, AUDIO_ALSA)
42
43 +static AudioBackendClass *audio_alsa_parent_class;
44 +
45 struct AudioALSA {
46 AudioMixengBackend parent_obj;
47 };
48
49 static struct audio_driver alsa_audio_driver;
50
49 -static void audio_alsa_class_init(ObjectClass *klass, const void *data)
50 -{
51 - AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_CLASS(klass);
52 -
53 - k->driver = &alsa_audio_driver;
54 -}
55 -
51 struct pollhlp {
52 snd_pcm_t *handle;
53 struct pollfd *pfds;
@@ -65,14 +60,12 @@ typedef struct ALSAVoiceOut {
60 HWVoiceOut hw;
61 snd_pcm_t *handle;
62 struct pollhlp pollhlp;
68 - Audiodev *dev;
63 } ALSAVoiceOut;
64
65 typedef struct ALSAVoiceIn {
66 HWVoiceIn hw;
67 snd_pcm_t *handle;
68 struct pollhlp pollhlp;
75 - Audiodev *dev;
69 } ALSAVoiceIn;
70
71 struct alsa_params_req {
@@ -707,7 +700,7 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
700 struct alsa_params_obt obt;
701 snd_pcm_t *handle;
702 struct audsettings obt_as;
710 - Audiodev *dev = drv_opaque;
703 + Audiodev *dev = hw->s->dev;
704
705 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
706 req.freq = as->freq;
@@ -727,7 +720,6 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
720
721 alsa->pollhlp.s = hw->s;
722 alsa->handle = handle;
730 - alsa->dev = dev;
723 return 0;
724 }
725
@@ -766,7 +758,7 @@ static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
758 static void alsa_enable_out(HWVoiceOut *hw, bool enable)
759 {
760 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
769 - AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.out;
761 + AudiodevAlsaPerDirectionOptions *apdo = hw->s->dev->u.alsa.out;
762
763 if (enable) {
764 bool poll_mode = apdo->try_poll;
@@ -794,7 +786,7 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
786 struct alsa_params_obt obt;
787 snd_pcm_t *handle;
788 struct audsettings obt_as;
797 - Audiodev *dev = drv_opaque;
789 + Audiodev *dev = hw->s->dev;
790
791 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
792 req.freq = as->freq;
@@ -814,7 +806,6 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
806
807 alsa->pollhlp.s = hw->s;
808 alsa->handle = handle;
817 - alsa->dev = dev;
809 return 0;
810 }
811
@@ -871,7 +862,7 @@ static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len)
862 static void alsa_enable_in(HWVoiceIn *hw, bool enable)
863 {
864 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
874 - AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.in;
865 + AudiodevAlsaPerDirectionOptions *apdo = hw->s->dev->u.alsa.in;
866
867 if (enable) {
868 bool poll_mode = apdo->try_poll;
@@ -901,7 +892,8 @@ static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions *apdo)
892 }
893 }
894
904 -static void *alsa_audio_init(Audiodev *dev, Error **errp)
895 +static bool
896 +audio_alsa_realize(AudioBackend *abe, Audiodev *dev, Error **errp)
897 {
898 AudiodevAlsaOptions *aopts;
899 assert(dev->driver == AUDIODEV_DRIVER_ALSA);
@@ -929,11 +921,7 @@ static void *alsa_audio_init(Audiodev *dev, Error **errp)
921 dev->u.alsa.in->buffer_length = 92880;
922 }
923
932 - return dev;
933 -}
934 -
935 -static void alsa_audio_fini (void *opaque)
936 -{
924 + return audio_alsa_parent_class->realize(abe, dev, errp);
925 }
926
927 static struct audio_pcm_ops alsa_pcm_ops = {
@@ -953,8 +941,6 @@ static struct audio_pcm_ops alsa_pcm_ops = {
941
942 static struct audio_driver alsa_audio_driver = {
943 .name = "alsa",
956 - .init = alsa_audio_init,
957 - .fini = alsa_audio_fini,
944 .pcm_ops = &alsa_pcm_ops,
945 .max_voices_out = INT_MAX,
946 .max_voices_in = INT_MAX,
@@ -962,6 +948,17 @@ static struct audio_driver alsa_audio_driver = {
948 .voice_size_in = sizeof (ALSAVoiceIn)
949 };
950
951 +static void audio_alsa_class_init(ObjectClass *klass, const void *data)
952 +{
953 + AudioBackendClass *b = AUDIO_BACKEND_CLASS(klass);
954 + AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_CLASS(klass);
955 +
956 + audio_alsa_parent_class = AUDIO_BACKEND_CLASS(object_class_get_parent(klass));
957 +
958 + b->realize = audio_alsa_realize;
959 + k->driver = &alsa_audio_driver;
960 +}
961 +
962 static const TypeInfo audio_types[] = {
963 {
964 .name = TYPE_AUDIO_ALSA,