xref: /openbmc/qemu/hw/audio/virtio-snd.c (revision 551ef0fa)
1 /*
2  * VIRTIO Sound Device conforming to
3  *
4  * "Virtual I/O Device (VIRTIO) Version 1.2
5  * Committee Specification Draft 01
6  * 09 May 2022"
7  *
8  * <https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-52900014>
9  *
10  * Copyright (c) 2023 Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org>
11  * Copyright (C) 2019 OpenSynergy GmbH
12  *
13  * This work is licensed under the terms of the GNU GPL, version 2 or
14  * (at your option) any later version.  See the COPYING file in the
15  * top-level directory.
16  */
17 
18 #include "qemu/osdep.h"
19 #include "qemu/iov.h"
20 #include "qemu/log.h"
21 #include "qemu/error-report.h"
22 #include "include/qemu/lockable.h"
23 #include "sysemu/runstate.h"
24 #include "trace.h"
25 #include "qapi/error.h"
26 #include "hw/audio/virtio-snd.h"
27 #include "hw/core/cpu.h"
28 
29 #define VIRTIO_SOUND_VM_VERSION 1
30 #define VIRTIO_SOUND_JACK_DEFAULT 0
31 #define VIRTIO_SOUND_STREAM_DEFAULT 2
32 #define VIRTIO_SOUND_CHMAP_DEFAULT 0
33 #define VIRTIO_SOUND_HDA_FN_NID 0
34 
35 static void virtio_snd_pcm_out_cb(void *data, int available);
36 static void virtio_snd_process_cmdq(VirtIOSound *s);
37 static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream);
38 static void virtio_snd_pcm_in_cb(void *data, int available);
39 
40 static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
41                                   | BIT(VIRTIO_SND_PCM_FMT_U8)
42                                   | BIT(VIRTIO_SND_PCM_FMT_S16)
43                                   | BIT(VIRTIO_SND_PCM_FMT_U16)
44                                   | BIT(VIRTIO_SND_PCM_FMT_S32)
45                                   | BIT(VIRTIO_SND_PCM_FMT_U32)
46                                   | BIT(VIRTIO_SND_PCM_FMT_FLOAT);
47 
48 static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512)
49                                 | BIT(VIRTIO_SND_PCM_RATE_8000)
50                                 | BIT(VIRTIO_SND_PCM_RATE_11025)
51                                 | BIT(VIRTIO_SND_PCM_RATE_16000)
52                                 | BIT(VIRTIO_SND_PCM_RATE_22050)
53                                 | BIT(VIRTIO_SND_PCM_RATE_32000)
54                                 | BIT(VIRTIO_SND_PCM_RATE_44100)
55                                 | BIT(VIRTIO_SND_PCM_RATE_48000)
56                                 | BIT(VIRTIO_SND_PCM_RATE_64000)
57                                 | BIT(VIRTIO_SND_PCM_RATE_88200)
58                                 | BIT(VIRTIO_SND_PCM_RATE_96000)
59                                 | BIT(VIRTIO_SND_PCM_RATE_176400)
60                                 | BIT(VIRTIO_SND_PCM_RATE_192000)
61                                 | BIT(VIRTIO_SND_PCM_RATE_384000);
62 
63 static const VMStateDescription vmstate_virtio_snd_device = {
64     .name = TYPE_VIRTIO_SND,
65     .version_id = VIRTIO_SOUND_VM_VERSION,
66     .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
67 };
68 
69 static const VMStateDescription vmstate_virtio_snd = {
70     .name = TYPE_VIRTIO_SND,
71     .unmigratable = 1,
72     .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
73     .version_id = VIRTIO_SOUND_VM_VERSION,
74     .fields = (VMStateField[]) {
75         VMSTATE_VIRTIO_DEVICE,
76         VMSTATE_END_OF_LIST()
77     },
78 };
79 
80 static Property virtio_snd_properties[] = {
81     DEFINE_AUDIO_PROPERTIES(VirtIOSound, card),
82     DEFINE_PROP_UINT32("jacks", VirtIOSound, snd_conf.jacks,
83                        VIRTIO_SOUND_JACK_DEFAULT),
84     DEFINE_PROP_UINT32("streams", VirtIOSound, snd_conf.streams,
85                        VIRTIO_SOUND_STREAM_DEFAULT),
86     DEFINE_PROP_UINT32("chmaps", VirtIOSound, snd_conf.chmaps,
87                        VIRTIO_SOUND_CHMAP_DEFAULT),
88     DEFINE_PROP_END_OF_LIST(),
89 };
90 
91 static void
92 virtio_snd_get_config(VirtIODevice *vdev, uint8_t *config)
93 {
94     VirtIOSound *s = VIRTIO_SND(vdev);
95     virtio_snd_config *sndconfig =
96         (virtio_snd_config *)config;
97     trace_virtio_snd_get_config(vdev,
98                                 s->snd_conf.jacks,
99                                 s->snd_conf.streams,
100                                 s->snd_conf.chmaps);
101 
102     memcpy(sndconfig, &s->snd_conf, sizeof(s->snd_conf));
103     cpu_to_le32s(&sndconfig->jacks);
104     cpu_to_le32s(&sndconfig->streams);
105     cpu_to_le32s(&sndconfig->chmaps);
106 
107 }
108 
109 static void
110 virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config)
111 {
112     VirtIOSound *s = VIRTIO_SND(vdev);
113     const virtio_snd_config *sndconfig =
114         (const virtio_snd_config *)config;
115 
116 
117    trace_virtio_snd_set_config(vdev,
118                                s->snd_conf.jacks,
119                                sndconfig->jacks,
120                                s->snd_conf.streams,
121                                sndconfig->streams,
122                                s->snd_conf.chmaps,
123                                sndconfig->chmaps);
124 
125     memcpy(&s->snd_conf, sndconfig, sizeof(virtio_snd_config));
126     le32_to_cpus(&s->snd_conf.jacks);
127     le32_to_cpus(&s->snd_conf.streams);
128     le32_to_cpus(&s->snd_conf.chmaps);
129 
130 }
131 
132 static void
133 virtio_snd_pcm_buffer_free(VirtIOSoundPCMBuffer *buffer)
134 {
135     g_free(buffer->elem);
136     g_free(buffer);
137 }
138 
139 static void
140 virtio_snd_ctrl_cmd_free(virtio_snd_ctrl_command *cmd)
141 {
142     g_free(cmd->elem);
143     g_free(cmd);
144 }
145 
146 /*
147  * Get a specific stream from the virtio sound card device.
148  * Returns NULL if @stream_id is invalid or not allocated.
149  *
150  * @s: VirtIOSound device
151  * @stream_id: stream id
152  */
153 static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s,
154                                                        uint32_t stream_id)
155 {
156     return stream_id >= s->snd_conf.streams ? NULL :
157         s->pcm->streams[stream_id];
158 }
159 
160 /*
161  * Get params for a specific stream.
162  *
163  * @s: VirtIOSound device
164  * @stream_id: stream id
165  */
166 static virtio_snd_pcm_set_params *virtio_snd_pcm_get_params(VirtIOSound *s,
167                                                             uint32_t stream_id)
168 {
169     return stream_id >= s->snd_conf.streams ? NULL
170         : &s->pcm->pcm_params[stream_id];
171 }
172 
173 /*
174  * Handle the VIRTIO_SND_R_PCM_INFO request.
175  * The function writes the info structs to the request element.
176  *
177  * @s: VirtIOSound device
178  * @cmd: The request command queue element from VirtIOSound cmdq field
179  */
180 static void virtio_snd_handle_pcm_info(VirtIOSound *s,
181                                        virtio_snd_ctrl_command *cmd)
182 {
183     uint32_t stream_id, start_id, count, size;
184     virtio_snd_pcm_info val;
185     virtio_snd_query_info req;
186     VirtIOSoundPCMStream *stream = NULL;
187     g_autofree virtio_snd_pcm_info *pcm_info = NULL;
188     size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
189                                cmd->elem->out_num,
190                                0,
191                                &req,
192                                sizeof(virtio_snd_query_info));
193 
194     if (msg_sz != sizeof(virtio_snd_query_info)) {
195         /*
196          * TODO: do we need to set DEVICE_NEEDS_RESET?
197          */
198         qemu_log_mask(LOG_GUEST_ERROR,
199                 "%s: virtio-snd command size incorrect %zu vs \
200                 %zu\n", __func__, msg_sz, sizeof(virtio_snd_query_info));
201         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
202         return;
203     }
204 
205     start_id = le32_to_cpu(req.start_id);
206     count = le32_to_cpu(req.count);
207     size = le32_to_cpu(req.size);
208 
209     if (iov_size(cmd->elem->in_sg, cmd->elem->in_num) <
210         sizeof(virtio_snd_hdr) + size * count) {
211         /*
212          * TODO: do we need to set DEVICE_NEEDS_RESET?
213          */
214         error_report("pcm info: buffer too small, got: %zu, needed: %zu",
215                 iov_size(cmd->elem->in_sg, cmd->elem->in_num),
216                 sizeof(virtio_snd_pcm_info));
217         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
218         return;
219     }
220 
221     pcm_info = g_new0(virtio_snd_pcm_info, count);
222     for (uint32_t i = 0; i < count; i++) {
223         stream_id = i + start_id;
224         trace_virtio_snd_handle_pcm_info(stream_id);
225         stream = virtio_snd_pcm_get_stream(s, stream_id);
226         if (!stream) {
227             error_report("Invalid stream id: %"PRIu32, stream_id);
228             cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
229             return;
230         }
231         val = stream->info;
232         val.hdr.hda_fn_nid = cpu_to_le32(val.hdr.hda_fn_nid);
233         val.features = cpu_to_le32(val.features);
234         val.formats = cpu_to_le64(val.formats);
235         val.rates = cpu_to_le64(val.rates);
236         /*
237          * 5.14.6.6.2.1 Device Requirements: Stream Information The device MUST
238          * NOT set undefined feature, format, rate and direction values. The
239          * device MUST initialize the padding bytes to 0.
240          */
241         pcm_info[i] = val;
242         memset(&pcm_info[i].padding, 0, 5);
243     }
244 
245     cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
246     iov_from_buf(cmd->elem->in_sg,
247                  cmd->elem->in_num,
248                  sizeof(virtio_snd_hdr),
249                  pcm_info,
250                  sizeof(virtio_snd_pcm_info) * count);
251 }
252 
253 /*
254  * Set the given stream params.
255  * Called by both virtio_snd_handle_pcm_set_params and during device
256  * initialization.
257  * Returns the response status code. (VIRTIO_SND_S_*).
258  *
259  * @s: VirtIOSound device
260  * @params: The PCM params as defined in the virtio specification
261  */
262 static
263 uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,
264                                    uint32_t stream_id,
265                                    virtio_snd_pcm_set_params *params)
266 {
267     virtio_snd_pcm_set_params *st_params;
268 
269     if (stream_id >= s->snd_conf.streams || s->pcm->pcm_params == NULL) {
270         /*
271          * TODO: do we need to set DEVICE_NEEDS_RESET?
272          */
273         virtio_error(VIRTIO_DEVICE(s), "Streams have not been initialized.\n");
274         return cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
275     }
276 
277     st_params = virtio_snd_pcm_get_params(s, stream_id);
278 
279     if (params->channels < 1 || params->channels > AUDIO_MAX_CHANNELS) {
280         error_report("Number of channels is not supported.");
281         return cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
282     }
283     if (!(supported_formats & BIT(params->format))) {
284         error_report("Stream format is not supported.");
285         return cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
286     }
287     if (!(supported_rates & BIT(params->rate))) {
288         error_report("Stream rate is not supported.");
289         return cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
290     }
291 
292     st_params->buffer_bytes = le32_to_cpu(params->buffer_bytes);
293     st_params->period_bytes = le32_to_cpu(params->period_bytes);
294     st_params->features = le32_to_cpu(params->features);
295     /* the following are uint8_t, so there's no need to bswap the values. */
296     st_params->channels = params->channels;
297     st_params->format = params->format;
298     st_params->rate = params->rate;
299 
300     return cpu_to_le32(VIRTIO_SND_S_OK);
301 }
302 
303 /*
304  * Handles the VIRTIO_SND_R_PCM_SET_PARAMS request.
305  *
306  * @s: VirtIOSound device
307  * @cmd: The request command queue element from VirtIOSound cmdq field
308  */
309 static void virtio_snd_handle_pcm_set_params(VirtIOSound *s,
310                                              virtio_snd_ctrl_command *cmd)
311 {
312     virtio_snd_pcm_set_params req = { 0 };
313     uint32_t stream_id;
314     size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
315                                cmd->elem->out_num,
316                                0,
317                                &req,
318                                sizeof(virtio_snd_pcm_set_params));
319 
320     if (msg_sz != sizeof(virtio_snd_pcm_set_params)) {
321         /*
322          * TODO: do we need to set DEVICE_NEEDS_RESET?
323          */
324         qemu_log_mask(LOG_GUEST_ERROR,
325                 "%s: virtio-snd command size incorrect %zu vs \
326                 %zu\n", __func__, msg_sz, sizeof(virtio_snd_pcm_set_params));
327         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
328         return;
329     }
330     stream_id = le32_to_cpu(req.hdr.stream_id);
331     trace_virtio_snd_handle_pcm_set_params(stream_id);
332     cmd->resp.code = virtio_snd_set_pcm_params(s, stream_id, &req);
333 }
334 
335 /*
336  * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_*
337  */
338 static AudioFormat virtio_snd_get_qemu_format(uint32_t format)
339 {
340     #define CASE(FMT)               \
341     case VIRTIO_SND_PCM_FMT_##FMT:  \
342         return AUDIO_FORMAT_##FMT;
343 
344     switch (format) {
345     CASE(U8)
346     CASE(S8)
347     CASE(U16)
348     CASE(S16)
349     CASE(U32)
350     CASE(S32)
351     case VIRTIO_SND_PCM_FMT_FLOAT:
352         return AUDIO_FORMAT_F32;
353     default:
354         g_assert_not_reached();
355     }
356 
357     #undef CASE
358 }
359 
360 /*
361  * Get a QEMU Audiosystem compatible frequency value from a
362  * VIRTIO_SND_PCM_RATE_*
363  */
364 static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
365 {
366     #define CASE(RATE)               \
367     case VIRTIO_SND_PCM_RATE_##RATE: \
368         return RATE;
369 
370     switch (rate) {
371     CASE(5512)
372     CASE(8000)
373     CASE(11025)
374     CASE(16000)
375     CASE(22050)
376     CASE(32000)
377     CASE(44100)
378     CASE(48000)
379     CASE(64000)
380     CASE(88200)
381     CASE(96000)
382     CASE(176400)
383     CASE(192000)
384     CASE(384000)
385     default:
386         g_assert_not_reached();
387     }
388 
389     #undef CASE
390 }
391 
392 /*
393  * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
394  * params.
395  */
396 static void virtio_snd_get_qemu_audsettings(audsettings *as,
397                                             virtio_snd_pcm_set_params *params)
398 {
399     as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
400     as->fmt = virtio_snd_get_qemu_format(params->format);
401     as->freq = virtio_snd_get_qemu_freq(params->rate);
402     as->endianness = target_words_bigendian() ? 1 : 0;
403 }
404 
405 /*
406  * Close a stream and free all its resources.
407  *
408  * @stream: VirtIOSoundPCMStream *stream
409  */
410 static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
411 {
412     if (stream) {
413         virtio_snd_pcm_flush(stream);
414         if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
415             AUD_close_out(&stream->pcm->snd->card, stream->voice.out);
416             stream->voice.out = NULL;
417         } else if (stream->info.direction == VIRTIO_SND_D_INPUT) {
418             AUD_close_in(&stream->pcm->snd->card, stream->voice.in);
419             stream->voice.in = NULL;
420         }
421     }
422 }
423 
424 /*
425  * Prepares a VirtIOSound card stream.
426  * Returns the response status code. (VIRTIO_SND_S_*).
427  *
428  * @s: VirtIOSound device
429  * @stream_id: stream id
430  */
431 static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
432 {
433     audsettings as;
434     virtio_snd_pcm_set_params *params;
435     VirtIOSoundPCMStream *stream;
436 
437     if (s->pcm->streams == NULL ||
438         s->pcm->pcm_params == NULL ||
439         stream_id >= s->snd_conf.streams) {
440         return cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
441     }
442 
443     params = virtio_snd_pcm_get_params(s, stream_id);
444     if (params == NULL) {
445         return cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
446     }
447 
448     stream = virtio_snd_pcm_get_stream(s, stream_id);
449     if (stream == NULL) {
450         stream = g_new0(VirtIOSoundPCMStream, 1);
451         stream->active = false;
452         stream->id = stream_id;
453         stream->pcm = s->pcm;
454         stream->s = s;
455         qemu_mutex_init(&stream->queue_mutex);
456         QSIMPLEQ_INIT(&stream->queue);
457         QSIMPLEQ_INIT(&stream->invalid);
458 
459         /*
460          * stream_id >= s->snd_conf.streams was checked before so this is
461          * in-bounds
462          */
463         s->pcm->streams[stream_id] = stream;
464     }
465 
466     virtio_snd_get_qemu_audsettings(&as, params);
467     stream->info.direction = stream_id < s->snd_conf.streams / 2 +
468         (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
469     stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
470     stream->info.features = 0;
471     stream->info.channels_min = 1;
472     stream->info.channels_max = as.nchannels;
473     stream->info.formats = supported_formats;
474     stream->info.rates = supported_rates;
475     stream->params = *params;
476 
477     stream->positions[0] = VIRTIO_SND_CHMAP_FL;
478     stream->positions[1] = VIRTIO_SND_CHMAP_FR;
479     stream->as = as;
480 
481     if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
482         stream->voice.out = AUD_open_out(&s->card,
483                                          stream->voice.out,
484                                          "virtio-sound.out",
485                                          stream,
486                                          virtio_snd_pcm_out_cb,
487                                          &as);
488         AUD_set_volume_out(stream->voice.out, 0, 255, 255);
489     } else {
490         stream->voice.in = AUD_open_in(&s->card,
491                                         stream->voice.in,
492                                         "virtio-sound.in",
493                                         stream,
494                                         virtio_snd_pcm_in_cb,
495                                         &as);
496         AUD_set_volume_in(stream->voice.in, 0, 255, 255);
497     }
498 
499     return cpu_to_le32(VIRTIO_SND_S_OK);
500 }
501 
502 static const char *print_code(uint32_t code)
503 {
504     #define CASE(CODE)            \
505     case VIRTIO_SND_R_##CODE:     \
506         return "VIRTIO_SND_R_"#CODE
507 
508     switch (code) {
509     CASE(JACK_INFO);
510     CASE(JACK_REMAP);
511     CASE(PCM_INFO);
512     CASE(PCM_SET_PARAMS);
513     CASE(PCM_PREPARE);
514     CASE(PCM_RELEASE);
515     CASE(PCM_START);
516     CASE(PCM_STOP);
517     CASE(CHMAP_INFO);
518     default:
519         return "invalid code";
520     }
521 
522     #undef CASE
523 };
524 
525 /*
526  * Handles VIRTIO_SND_R_PCM_PREPARE.
527  *
528  * @s: VirtIOSound device
529  * @cmd: The request command queue element from VirtIOSound cmdq field
530  */
531 static void virtio_snd_handle_pcm_prepare(VirtIOSound *s,
532                                           virtio_snd_ctrl_command *cmd)
533 {
534     uint32_t stream_id;
535     size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
536                                cmd->elem->out_num,
537                                sizeof(virtio_snd_hdr),
538                                &stream_id,
539                                sizeof(stream_id));
540 
541     stream_id = le32_to_cpu(stream_id);
542     cmd->resp.code = msg_sz == sizeof(stream_id)
543                    ? virtio_snd_pcm_prepare(s, stream_id)
544                    : cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
545 }
546 
547 /*
548  * Handles VIRTIO_SND_R_PCM_START.
549  *
550  * @s: VirtIOSound device
551  * @cmd: The request command queue element from VirtIOSound cmdq field
552  * @start: whether to start or stop the device
553  */
554 static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s,
555                                              virtio_snd_ctrl_command *cmd,
556                                              bool start)
557 {
558     VirtIOSoundPCMStream *stream;
559     virtio_snd_pcm_hdr req;
560     uint32_t stream_id;
561     size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
562                                cmd->elem->out_num,
563                                0,
564                                &req,
565                                sizeof(virtio_snd_pcm_hdr));
566 
567     if (msg_sz != sizeof(virtio_snd_pcm_hdr)) {
568         qemu_log_mask(LOG_GUEST_ERROR,
569                 "%s: virtio-snd command size incorrect %zu vs \
570                 %zu\n", __func__, msg_sz, sizeof(virtio_snd_pcm_hdr));
571         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
572         return;
573     }
574 
575     stream_id = le32_to_cpu(req.stream_id);
576     cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
577     trace_virtio_snd_handle_pcm_start_stop(start ? "VIRTIO_SND_R_PCM_START" :
578             "VIRTIO_SND_R_PCM_STOP", stream_id);
579 
580     stream = virtio_snd_pcm_get_stream(s, stream_id);
581     if (stream) {
582         WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
583             stream->active = start;
584         }
585         if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
586             AUD_set_active_out(stream->voice.out, start);
587         } else {
588             AUD_set_active_in(stream->voice.in, start);
589         }
590     } else {
591         error_report("Invalid stream id: %"PRIu32, stream_id);
592         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
593         return;
594     }
595     stream->active = start;
596 }
597 
598 /*
599  * Returns the number of I/O messages that are being processed.
600  *
601  * @stream: VirtIOSoundPCMStream
602  */
603 static size_t virtio_snd_pcm_get_io_msgs_count(VirtIOSoundPCMStream *stream)
604 {
605     VirtIOSoundPCMBuffer *buffer, *next;
606     size_t count = 0;
607 
608     WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
609         QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) {
610             count += 1;
611         }
612         QSIMPLEQ_FOREACH_SAFE(buffer, &stream->invalid, entry, next) {
613             count += 1;
614         }
615     }
616     return count;
617 }
618 
619 /*
620  * Handles VIRTIO_SND_R_PCM_RELEASE.
621  *
622  * @s: VirtIOSound device
623  * @cmd: The request command queue element from VirtIOSound cmdq field
624  */
625 static void virtio_snd_handle_pcm_release(VirtIOSound *s,
626                                           virtio_snd_ctrl_command *cmd)
627 {
628     uint32_t stream_id;
629     VirtIOSoundPCMStream *stream;
630     size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
631                                cmd->elem->out_num,
632                                sizeof(virtio_snd_hdr),
633                                &stream_id,
634                                sizeof(stream_id));
635 
636     if (msg_sz != sizeof(stream_id)) {
637         /*
638          * TODO: do we need to set DEVICE_NEEDS_RESET?
639          */
640         qemu_log_mask(LOG_GUEST_ERROR,
641                 "%s: virtio-snd command size incorrect %zu vs \
642                 %zu\n", __func__, msg_sz, sizeof(stream_id));
643         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
644         return;
645     }
646 
647     stream_id = le32_to_cpu(stream_id);
648     trace_virtio_snd_handle_pcm_release(stream_id);
649     stream = virtio_snd_pcm_get_stream(s, stream_id);
650     if (stream == NULL) {
651         /*
652          * TODO: do we need to set DEVICE_NEEDS_RESET?
653          */
654         error_report("already released stream %"PRIu32, stream_id);
655         virtio_error(VIRTIO_DEVICE(s),
656                      "already released stream %"PRIu32,
657                      stream_id);
658         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
659         return;
660     }
661 
662     if (virtio_snd_pcm_get_io_msgs_count(stream)) {
663         /*
664          * virtio-v1.2-csd01, 5.14.6.6.5.1,
665          * Device Requirements: Stream Release
666          *
667          * - The device MUST complete all pending I/O messages for the
668          *   specified stream ID.
669          * - The device MUST NOT complete the control request while there
670          *   are pending I/O messages for the specified stream ID.
671          */
672         trace_virtio_snd_pcm_stream_flush(stream_id);
673         virtio_snd_pcm_flush(stream);
674     }
675 
676     cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
677 }
678 
679 /*
680  * The actual processing done in virtio_snd_process_cmdq().
681  *
682  * @s: VirtIOSound device
683  * @cmd: control command request
684  */
685 static inline void
686 process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)
687 {
688     uint32_t code;
689     size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
690                                cmd->elem->out_num,
691                                0,
692                                &cmd->ctrl,
693                                sizeof(virtio_snd_hdr));
694 
695     if (msg_sz != sizeof(virtio_snd_hdr)) {
696         /*
697          * TODO: do we need to set DEVICE_NEEDS_RESET?
698          */
699         qemu_log_mask(LOG_GUEST_ERROR,
700                 "%s: virtio-snd command size incorrect %zu vs \
701                 %zu\n", __func__, msg_sz, sizeof(virtio_snd_hdr));
702         return;
703     }
704 
705     code = le32_to_cpu(cmd->ctrl.code);
706 
707     trace_virtio_snd_handle_code(code, print_code(code));
708 
709     switch (code) {
710     case VIRTIO_SND_R_JACK_INFO:
711     case VIRTIO_SND_R_JACK_REMAP:
712         qemu_log_mask(LOG_UNIMP,
713                      "virtio_snd: jack functionality is unimplemented.\n");
714         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
715         break;
716     case VIRTIO_SND_R_PCM_INFO:
717         virtio_snd_handle_pcm_info(s, cmd);
718         break;
719     case VIRTIO_SND_R_PCM_START:
720         virtio_snd_handle_pcm_start_stop(s, cmd, true);
721         break;
722     case VIRTIO_SND_R_PCM_STOP:
723         virtio_snd_handle_pcm_start_stop(s, cmd, false);
724         break;
725     case VIRTIO_SND_R_PCM_SET_PARAMS:
726         virtio_snd_handle_pcm_set_params(s, cmd);
727         break;
728     case VIRTIO_SND_R_PCM_PREPARE:
729         virtio_snd_handle_pcm_prepare(s, cmd);
730         break;
731     case VIRTIO_SND_R_PCM_RELEASE:
732         virtio_snd_handle_pcm_release(s, cmd);
733         break;
734     case VIRTIO_SND_R_CHMAP_INFO:
735         qemu_log_mask(LOG_UNIMP,
736                      "virtio_snd: chmap info functionality is unimplemented.\n");
737         trace_virtio_snd_handle_chmap_info();
738         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
739         break;
740     default:
741         /* error */
742         error_report("virtio snd header not recognized: %"PRIu32, code);
743         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
744     }
745 
746     iov_from_buf(cmd->elem->in_sg,
747                  cmd->elem->in_num,
748                  0,
749                  &cmd->resp,
750                  sizeof(virtio_snd_hdr));
751     virtqueue_push(cmd->vq, cmd->elem, sizeof(virtio_snd_hdr));
752     virtio_notify(VIRTIO_DEVICE(s), cmd->vq);
753 }
754 
755 /*
756  * Consume all elements in command queue.
757  *
758  * @s: VirtIOSound device
759  */
760 static void virtio_snd_process_cmdq(VirtIOSound *s)
761 {
762     virtio_snd_ctrl_command *cmd;
763 
764     if (unlikely(qatomic_read(&s->processing_cmdq))) {
765         return;
766     }
767 
768     WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) {
769         qatomic_set(&s->processing_cmdq, true);
770         while (!QTAILQ_EMPTY(&s->cmdq)) {
771             cmd = QTAILQ_FIRST(&s->cmdq);
772 
773             /* process command */
774             process_cmd(s, cmd);
775 
776             QTAILQ_REMOVE(&s->cmdq, cmd, next);
777 
778             virtio_snd_ctrl_cmd_free(cmd);
779         }
780         qatomic_set(&s->processing_cmdq, false);
781     }
782 }
783 
784 /*
785  * The control message handler. Pops an element from the control virtqueue,
786  * and stores them to VirtIOSound's cmdq queue and finally calls
787  * virtio_snd_process_cmdq() for processing.
788  *
789  * @vdev: VirtIOSound device
790  * @vq: Control virtqueue
791  */
792 static void virtio_snd_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
793 {
794     VirtIOSound *s = VIRTIO_SND(vdev);
795     VirtQueueElement *elem;
796     virtio_snd_ctrl_command *cmd;
797 
798     trace_virtio_snd_handle_ctrl(vdev, vq);
799 
800     if (!virtio_queue_ready(vq)) {
801         return;
802     }
803 
804     elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
805     while (elem) {
806         cmd = g_new0(virtio_snd_ctrl_command, 1);
807         cmd->elem = elem;
808         cmd->vq = vq;
809         cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
810         QTAILQ_INSERT_TAIL(&s->cmdq, cmd, next);
811         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
812     }
813 
814     virtio_snd_process_cmdq(s);
815 }
816 
817 /*
818  * The event virtqueue handler.
819  * Not implemented yet.
820  *
821  * @vdev: VirtIOSound device
822  * @vq: event vq
823  */
824 static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq)
825 {
826     qemu_log_mask(LOG_UNIMP, "virtio_snd: event queue is unimplemented.\n");
827     trace_virtio_snd_handle_event();
828 }
829 
830 static inline void empty_invalid_queue(VirtIODevice *vdev, VirtQueue *vq)
831 {
832     VirtIOSoundPCMBuffer *buffer = NULL;
833     VirtIOSoundPCMStream *stream = NULL;
834     virtio_snd_pcm_status resp = { 0 };
835     VirtIOSound *vsnd = VIRTIO_SND(vdev);
836     bool any = false;
837 
838     for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
839         stream = vsnd->pcm->streams[i];
840         if (stream) {
841             any = false;
842             WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
843                 while (!QSIMPLEQ_EMPTY(&stream->invalid)) {
844                     buffer = QSIMPLEQ_FIRST(&stream->invalid);
845                     if (buffer->vq != vq) {
846                         break;
847                     }
848                     any = true;
849                     resp.status = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
850                     iov_from_buf(buffer->elem->in_sg,
851                                  buffer->elem->in_num,
852                                  0,
853                                  &resp,
854                                  sizeof(virtio_snd_pcm_status));
855                     virtqueue_push(vq,
856                                    buffer->elem,
857                                    sizeof(virtio_snd_pcm_status));
858                     QSIMPLEQ_REMOVE_HEAD(&stream->invalid, entry);
859                     virtio_snd_pcm_buffer_free(buffer);
860                 }
861                 if (any) {
862                     /*
863                      * Notify vq about virtio_snd_pcm_status responses.
864                      * Buffer responses must be notified separately later.
865                      */
866                     virtio_notify(vdev, vq);
867                 }
868             }
869         }
870     }
871 }
872 
873 /*
874  * The tx virtqueue handler. Makes the buffers available to their respective
875  * streams for consumption.
876  *
877  * @vdev: VirtIOSound device
878  * @vq: tx virtqueue
879  */
880 static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
881 {
882     VirtIOSound *s = VIRTIO_SND(vdev);
883     VirtIOSoundPCMStream *stream = NULL;
884     VirtIOSoundPCMBuffer *buffer;
885     VirtQueueElement *elem;
886     size_t msg_sz, size;
887     virtio_snd_pcm_xfer hdr;
888     uint32_t stream_id;
889     /*
890      * If any of the I/O messages are invalid, put them in stream->invalid and
891      * return them after the for loop.
892      */
893     bool must_empty_invalid_queue = false;
894 
895     if (!virtio_queue_ready(vq)) {
896         return;
897     }
898     trace_virtio_snd_handle_tx_xfer();
899 
900     for (;;) {
901         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
902         if (!elem) {
903             break;
904         }
905         /* get the message hdr object */
906         msg_sz = iov_to_buf(elem->out_sg,
907                             elem->out_num,
908                             0,
909                             &hdr,
910                             sizeof(virtio_snd_pcm_xfer));
911         if (msg_sz != sizeof(virtio_snd_pcm_xfer)) {
912             goto tx_err;
913         }
914         stream_id = le32_to_cpu(hdr.stream_id);
915 
916         if (stream_id >= s->snd_conf.streams
917             || s->pcm->streams[stream_id] == NULL) {
918             goto tx_err;
919         }
920 
921         stream = s->pcm->streams[stream_id];
922         if (stream->info.direction != VIRTIO_SND_D_OUTPUT) {
923             goto tx_err;
924         }
925 
926         WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
927             size = iov_size(elem->out_sg, elem->out_num) - msg_sz;
928 
929             buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
930             buffer->elem = elem;
931             buffer->populated = false;
932             buffer->vq = vq;
933             buffer->size = size;
934             buffer->offset = 0;
935 
936             QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
937         }
938         continue;
939 
940 tx_err:
941         WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
942             must_empty_invalid_queue = true;
943             buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer));
944             buffer->elem = elem;
945             buffer->vq = vq;
946             QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry);
947         }
948     }
949 
950     if (must_empty_invalid_queue) {
951         empty_invalid_queue(vdev, vq);
952     }
953 }
954 
955 /*
956  * The rx virtqueue handler. Makes the buffers available to their respective
957  * streams for consumption.
958  *
959  * @vdev: VirtIOSound device
960  * @vq: rx virtqueue
961  */
962 static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)
963 {
964     VirtIOSound *s = VIRTIO_SND(vdev);
965     VirtIOSoundPCMStream *stream = NULL;
966     VirtIOSoundPCMBuffer *buffer;
967     VirtQueueElement *elem;
968     size_t msg_sz, size;
969     virtio_snd_pcm_xfer hdr;
970     uint32_t stream_id;
971     /*
972      * if any of the I/O messages are invalid, put them in stream->invalid and
973      * return them after the for loop.
974      */
975     bool must_empty_invalid_queue = false;
976 
977     if (!virtio_queue_ready(vq)) {
978         return;
979     }
980     trace_virtio_snd_handle_rx_xfer();
981 
982     for (;;) {
983         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
984         if (!elem) {
985             break;
986         }
987         /* get the message hdr object */
988         msg_sz = iov_to_buf(elem->out_sg,
989                             elem->out_num,
990                             0,
991                             &hdr,
992                             sizeof(virtio_snd_pcm_xfer));
993         if (msg_sz != sizeof(virtio_snd_pcm_xfer)) {
994             goto rx_err;
995         }
996         stream_id = le32_to_cpu(hdr.stream_id);
997 
998         if (stream_id >= s->snd_conf.streams
999             || !s->pcm->streams[stream_id]) {
1000             goto rx_err;
1001         }
1002 
1003         stream = s->pcm->streams[stream_id];
1004         if (stream == NULL || stream->info.direction != VIRTIO_SND_D_INPUT) {
1005             goto rx_err;
1006         }
1007         WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
1008             size = iov_size(elem->in_sg, elem->in_num) -
1009                 sizeof(virtio_snd_pcm_status);
1010             buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
1011             buffer->elem = elem;
1012             buffer->vq = vq;
1013             buffer->size = 0;
1014             buffer->offset = 0;
1015             QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
1016         }
1017         continue;
1018 
1019 rx_err:
1020         WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
1021             must_empty_invalid_queue = true;
1022             buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer));
1023             buffer->elem = elem;
1024             buffer->vq = vq;
1025             QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry);
1026         }
1027     }
1028 
1029     if (must_empty_invalid_queue) {
1030         empty_invalid_queue(vdev, vq);
1031     }
1032 }
1033 
1034 static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
1035                              Error **errp)
1036 {
1037     /*
1038      * virtio-v1.2-csd01, 5.14.3,
1039      * Feature Bits
1040      * None currently defined.
1041      */
1042     VirtIOSound *s = VIRTIO_SND(vdev);
1043     features |= s->features;
1044 
1045     trace_virtio_snd_get_features(vdev, features);
1046 
1047     return features;
1048 }
1049 
1050 static void
1051 virtio_snd_vm_state_change(void *opaque, bool running,
1052                                        RunState state)
1053 {
1054     if (running) {
1055         trace_virtio_snd_vm_state_running();
1056     } else {
1057         trace_virtio_snd_vm_state_stopped();
1058     }
1059 }
1060 
1061 static void virtio_snd_realize(DeviceState *dev, Error **errp)
1062 {
1063     ERRP_GUARD();
1064     VirtIOSound *vsnd = VIRTIO_SND(dev);
1065     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1066     virtio_snd_pcm_set_params default_params = { 0 };
1067     uint32_t status;
1068 
1069     vsnd->pcm = NULL;
1070     vsnd->vmstate =
1071         qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd);
1072 
1073     trace_virtio_snd_realize(vsnd);
1074 
1075     vsnd->pcm = g_new0(VirtIOSoundPCM, 1);
1076     vsnd->pcm->snd = vsnd;
1077     vsnd->pcm->streams =
1078         g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams);
1079     vsnd->pcm->pcm_params =
1080         g_new0(virtio_snd_pcm_set_params, vsnd->snd_conf.streams);
1081 
1082     virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
1083     virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
1084 
1085     /* set number of jacks and streams */
1086     if (vsnd->snd_conf.jacks > 8) {
1087         error_setg(errp,
1088                    "Invalid number of jacks: %"PRIu32,
1089                    vsnd->snd_conf.jacks);
1090         return;
1091     }
1092     if (vsnd->snd_conf.streams < 1 || vsnd->snd_conf.streams > 10) {
1093         error_setg(errp,
1094                    "Invalid number of streams: %"PRIu32,
1095                     vsnd->snd_conf.streams);
1096         return;
1097     }
1098 
1099     if (vsnd->snd_conf.chmaps > VIRTIO_SND_CHMAP_MAX_SIZE) {
1100         error_setg(errp,
1101                    "Invalid number of channel maps: %"PRIu32,
1102                    vsnd->snd_conf.chmaps);
1103         return;
1104     }
1105 
1106     AUD_register_card("virtio-sound", &vsnd->card, errp);
1107 
1108     /* set default params for all streams */
1109     default_params.features = 0;
1110     default_params.buffer_bytes = cpu_to_le32(8192);
1111     default_params.period_bytes = cpu_to_le32(2048);
1112     default_params.channels = 2;
1113     default_params.format = VIRTIO_SND_PCM_FMT_S16;
1114     default_params.rate = VIRTIO_SND_PCM_RATE_48000;
1115     vsnd->queues[VIRTIO_SND_VQ_CONTROL] =
1116         virtio_add_queue(vdev, 64, virtio_snd_handle_ctrl);
1117     vsnd->queues[VIRTIO_SND_VQ_EVENT] =
1118         virtio_add_queue(vdev, 64, virtio_snd_handle_event);
1119     vsnd->queues[VIRTIO_SND_VQ_TX] =
1120         virtio_add_queue(vdev, 64, virtio_snd_handle_tx_xfer);
1121     vsnd->queues[VIRTIO_SND_VQ_RX] =
1122         virtio_add_queue(vdev, 64, virtio_snd_handle_rx_xfer);
1123     qemu_mutex_init(&vsnd->cmdq_mutex);
1124     QTAILQ_INIT(&vsnd->cmdq);
1125 
1126     for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
1127         status = virtio_snd_set_pcm_params(vsnd, i, &default_params);
1128         if (status != cpu_to_le32(VIRTIO_SND_S_OK)) {
1129             error_setg(errp,
1130                        "Can't initialize stream params, device responded with %s.",
1131                        print_code(status));
1132             return;
1133         }
1134         status = virtio_snd_pcm_prepare(vsnd, i);
1135         if (status != cpu_to_le32(VIRTIO_SND_S_OK)) {
1136             error_setg(errp,
1137                        "Can't prepare streams, device responded with %s.",
1138                        print_code(status));
1139             return;
1140         }
1141     }
1142 }
1143 
1144 static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
1145                                     VirtIOSoundPCMBuffer *buffer)
1146 {
1147     virtio_snd_pcm_status resp = { 0 };
1148     resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
1149     resp.latency_bytes = cpu_to_le32((uint32_t)buffer->size);
1150     iov_from_buf(buffer->elem->in_sg,
1151                  buffer->elem->in_num,
1152                  0,
1153                  &resp,
1154                  sizeof(virtio_snd_pcm_status));
1155     virtqueue_push(buffer->vq,
1156                    buffer->elem,
1157                    sizeof(virtio_snd_pcm_status));
1158     virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
1159     QSIMPLEQ_REMOVE(&stream->queue,
1160                     buffer,
1161                     VirtIOSoundPCMBuffer,
1162                     entry);
1163     virtio_snd_pcm_buffer_free(buffer);
1164 }
1165 
1166 /*
1167  * AUD_* output callback.
1168  *
1169  * @data: VirtIOSoundPCMStream stream
1170  * @available: number of bytes that can be written with AUD_write()
1171  */
1172 static void virtio_snd_pcm_out_cb(void *data, int available)
1173 {
1174     VirtIOSoundPCMStream *stream = data;
1175     VirtIOSoundPCMBuffer *buffer;
1176     size_t size;
1177 
1178     WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
1179         while (!QSIMPLEQ_EMPTY(&stream->queue)) {
1180             buffer = QSIMPLEQ_FIRST(&stream->queue);
1181             if (!virtio_queue_ready(buffer->vq)) {
1182                 return;
1183             }
1184             if (!stream->active) {
1185                 /* Stream has stopped, so do not perform AUD_write. */
1186                 return_tx_buffer(stream, buffer);
1187                 continue;
1188             }
1189             if (!buffer->populated) {
1190                 iov_to_buf(buffer->elem->out_sg,
1191                            buffer->elem->out_num,
1192                            sizeof(virtio_snd_pcm_xfer),
1193                            buffer->data,
1194                            buffer->size);
1195                 buffer->populated = true;
1196             }
1197             for (;;) {
1198                 size = AUD_write(stream->voice.out,
1199                                  buffer->data + buffer->offset,
1200                                  MIN(buffer->size, available));
1201                 assert(size <= MIN(buffer->size, available));
1202                 if (size == 0) {
1203                     /* break out of both loops */
1204                     available = 0;
1205                     break;
1206                 }
1207                 buffer->size -= size;
1208                 buffer->offset += size;
1209                 available -= size;
1210                 if (buffer->size < 1) {
1211                     return_tx_buffer(stream, buffer);
1212                     break;
1213                 }
1214                 if (!available) {
1215                     break;
1216                 }
1217             }
1218             if (!available) {
1219                 break;
1220             }
1221         }
1222     }
1223 }
1224 
1225 /*
1226  * Flush all buffer data from this input stream's queue into the driver's
1227  * virtual queue.
1228  *
1229  * @stream: VirtIOSoundPCMStream *stream
1230  */
1231 static inline void return_rx_buffer(VirtIOSoundPCMStream *stream,
1232                                     VirtIOSoundPCMBuffer *buffer)
1233 {
1234     virtio_snd_pcm_status resp = { 0 };
1235     resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
1236     resp.latency_bytes = 0;
1237     /* Copy data -if any- to guest */
1238     iov_from_buf(buffer->elem->in_sg,
1239                  buffer->elem->in_num,
1240                  0,
1241                  buffer->data,
1242                  buffer->size);
1243     iov_from_buf(buffer->elem->in_sg,
1244                  buffer->elem->in_num,
1245                  buffer->size,
1246                  &resp,
1247                  sizeof(virtio_snd_pcm_status));
1248     virtqueue_push(buffer->vq,
1249                    buffer->elem,
1250                    sizeof(virtio_snd_pcm_status) + buffer->size);
1251     virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
1252     QSIMPLEQ_REMOVE(&stream->queue,
1253                     buffer,
1254                     VirtIOSoundPCMBuffer,
1255                     entry);
1256     virtio_snd_pcm_buffer_free(buffer);
1257 }
1258 
1259 
1260 /*
1261  * AUD_* input callback.
1262  *
1263  * @data: VirtIOSoundPCMStream stream
1264  * @available: number of bytes that can be read with AUD_read()
1265  */
1266 static void virtio_snd_pcm_in_cb(void *data, int available)
1267 {
1268     VirtIOSoundPCMStream *stream = data;
1269     VirtIOSoundPCMBuffer *buffer;
1270     size_t size;
1271 
1272     WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
1273         while (!QSIMPLEQ_EMPTY(&stream->queue)) {
1274             buffer = QSIMPLEQ_FIRST(&stream->queue);
1275             if (!virtio_queue_ready(buffer->vq)) {
1276                 return;
1277             }
1278             if (!stream->active) {
1279                 /* Stream has stopped, so do not perform AUD_read. */
1280                 return_rx_buffer(stream, buffer);
1281                 continue;
1282             }
1283 
1284             for (;;) {
1285                 size = AUD_read(stream->voice.in,
1286                         buffer->data + buffer->size,
1287                         MIN(available, (stream->params.period_bytes -
1288                                         buffer->size)));
1289                 if (!size) {
1290                     available = 0;
1291                     break;
1292                 }
1293                 buffer->size += size;
1294                 available -= size;
1295                 if (buffer->size >= stream->params.period_bytes) {
1296                     return_rx_buffer(stream, buffer);
1297                     break;
1298                 }
1299                 if (!available) {
1300                     break;
1301                 }
1302             }
1303             if (!available) {
1304                 break;
1305             }
1306         }
1307     }
1308 }
1309 
1310 /*
1311  * Flush all buffer data from this output stream's queue into the driver's
1312  * virtual queue.
1313  *
1314  * @stream: VirtIOSoundPCMStream *stream
1315  */
1316 static inline void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
1317 {
1318     VirtIOSoundPCMBuffer *buffer;
1319     void (*cb)(VirtIOSoundPCMStream *, VirtIOSoundPCMBuffer *) =
1320         (stream->info.direction == VIRTIO_SND_D_OUTPUT) ? return_tx_buffer :
1321         return_rx_buffer;
1322 
1323     WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
1324         while (!QSIMPLEQ_EMPTY(&stream->queue)) {
1325             buffer = QSIMPLEQ_FIRST(&stream->queue);
1326             cb(stream, buffer);
1327         }
1328     }
1329 }
1330 
1331 static void virtio_snd_unrealize(DeviceState *dev)
1332 {
1333     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1334     VirtIOSound *vsnd = VIRTIO_SND(dev);
1335     VirtIOSoundPCMStream *stream;
1336 
1337     qemu_del_vm_change_state_handler(vsnd->vmstate);
1338     trace_virtio_snd_unrealize(vsnd);
1339 
1340     if (vsnd->pcm) {
1341         if (vsnd->pcm->streams) {
1342             for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
1343                 stream = vsnd->pcm->streams[i];
1344                 if (stream) {
1345                     virtio_snd_process_cmdq(stream->s);
1346                     virtio_snd_pcm_close(stream);
1347                     qemu_mutex_destroy(&stream->queue_mutex);
1348                     g_free(stream);
1349                 }
1350             }
1351             g_free(vsnd->pcm->streams);
1352         }
1353         g_free(vsnd->pcm->pcm_params);
1354         g_free(vsnd->pcm);
1355         vsnd->pcm = NULL;
1356     }
1357     AUD_remove_card(&vsnd->card);
1358     qemu_mutex_destroy(&vsnd->cmdq_mutex);
1359     virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_CONTROL]);
1360     virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_EVENT]);
1361     virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_TX]);
1362     virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_RX]);
1363     virtio_cleanup(vdev);
1364 }
1365 
1366 
1367 static void virtio_snd_reset(VirtIODevice *vdev)
1368 {
1369     VirtIOSound *s = VIRTIO_SND(vdev);
1370     virtio_snd_ctrl_command *cmd;
1371 
1372     WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) {
1373         while (!QTAILQ_EMPTY(&s->cmdq)) {
1374             cmd = QTAILQ_FIRST(&s->cmdq);
1375             QTAILQ_REMOVE(&s->cmdq, cmd, next);
1376             virtio_snd_ctrl_cmd_free(cmd);
1377         }
1378     }
1379 }
1380 
1381 static void virtio_snd_class_init(ObjectClass *klass, void *data)
1382 {
1383     DeviceClass *dc = DEVICE_CLASS(klass);
1384     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1385 
1386 
1387     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
1388     device_class_set_props(dc, virtio_snd_properties);
1389 
1390     dc->vmsd = &vmstate_virtio_snd;
1391     vdc->vmsd = &vmstate_virtio_snd_device;
1392     vdc->realize = virtio_snd_realize;
1393     vdc->unrealize = virtio_snd_unrealize;
1394     vdc->get_config = virtio_snd_get_config;
1395     vdc->set_config = virtio_snd_set_config;
1396     vdc->get_features = get_features;
1397     vdc->reset = virtio_snd_reset;
1398     vdc->legacy_features = 0;
1399 }
1400 
1401 static const TypeInfo virtio_snd_types[] = {
1402     {
1403       .name          = TYPE_VIRTIO_SND,
1404       .parent        = TYPE_VIRTIO_DEVICE,
1405       .instance_size = sizeof(VirtIOSound),
1406       .class_init    = virtio_snd_class_init,
1407     }
1408 };
1409 
1410 DEFINE_TYPES(virtio_snd_types)
1411