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