1 From 12e4e501f9662a02e61acb5966fdceeffb0ff16d Mon Sep 17 00:00:00 2001
2 From: Anton Yakovlev <anton.yakovlev@opensynergy.com>
3 Date: Tue, 2 Mar 2021 17:47:04 +0100
4 Subject: [PATCH] ALSA: virtio: build PCM devices and substream hardware
7 Like the HDA specification, the virtio sound device specification links
8 PCM substreams, jacks and PCM channel maps into functional groups. For
9 each discovered group, a PCM device is created, the number of which
10 coincides with the group number.
12 Introduce the module parameters for setting the hardware buffer
17 pcm_period_ms_min [=10]
18 pcm_period_ms_max [=80]
20 Signed-off-by: Anton Yakovlev <anton.yakovlev@opensynergy.com>
21 Link: https://lore.kernel.org/r/20210302164709.3142702-5-anton.yakovlev@opensynergy.com
22 Signed-off-by: Takashi Iwai <tiwai@suse.de>
23 Signed-off-by: Vasyl Vavrychuk <vasyl.vavrychuk@opensynergy.com>
25 sound/virtio/Makefile | 3 +-
26 sound/virtio/virtio_card.c | 18 ++
27 sound/virtio/virtio_card.h | 10 +
28 sound/virtio/virtio_pcm.c | 479 +++++++++++++++++++++++++++++++++++++
29 sound/virtio/virtio_pcm.h | 72 ++++++
30 5 files changed, 581 insertions(+), 1 deletion(-)
31 create mode 100644 sound/virtio/virtio_pcm.c
32 create mode 100644 sound/virtio/virtio_pcm.h
34 diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile
35 index dc551e637441..69162a545a41 100644
36 --- a/sound/virtio/Makefile
37 +++ b/sound/virtio/Makefile
38 @@ -4,5 +4,6 @@ obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
46 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c
47 index b757b2444078..11c76ee311b7 100644
48 --- a/sound/virtio/virtio_card.c
49 +++ b/sound/virtio/virtio_card.c
50 @@ -209,6 +209,16 @@ static int virtsnd_build_devs(struct virtio_snd *snd)
51 VIRTIO_SND_CARD_NAME " at %s/%s",
52 dev_name(dev->parent), dev_name(dev));
54 + rc = virtsnd_pcm_parse_cfg(snd);
58 + if (snd->nsubstreams) {
59 + rc = virtsnd_pcm_build_devs(snd);
64 return snd_card_register(snd->card);
67 @@ -237,6 +247,9 @@ static int virtsnd_validate(struct virtio_device *vdev)
71 + if (virtsnd_pcm_validate(vdev))
77 @@ -259,6 +272,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
80 INIT_LIST_HEAD(&snd->ctl_msgs);
81 + INIT_LIST_HEAD(&snd->pcm_list);
85 @@ -293,6 +307,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
86 static void virtsnd_remove(struct virtio_device *vdev)
88 struct virtio_snd *snd = vdev->priv;
91 virtsnd_disable_event_vq(snd);
92 virtsnd_ctl_msg_cancel_all(snd);
93 @@ -303,6 +318,9 @@ static void virtsnd_remove(struct virtio_device *vdev)
94 vdev->config->del_vqs(vdev);
95 vdev->config->reset(vdev);
97 + for (i = 0; snd->substreams && i < snd->nsubstreams; ++i)
98 + cancel_work_sync(&snd->substreams[i].elapsed_period);
100 kfree(snd->event_msgs);
103 diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h
104 index 1e76eeff160f..77a1b7255370 100644
105 --- a/sound/virtio/virtio_card.h
106 +++ b/sound/virtio/virtio_card.h
108 #include <uapi/linux/virtio_snd.h>
110 #include "virtio_ctl_msg.h"
111 +#include "virtio_pcm.h"
113 #define VIRTIO_SND_CARD_DRIVER "virtio-snd"
114 #define VIRTIO_SND_CARD_NAME "VirtIO SoundCard"
115 +#define VIRTIO_SND_PCM_NAME "VirtIO PCM"
117 +struct virtio_pcm_substream;
120 * struct virtio_snd_queue - Virtqueue wrapper structure.
121 @@ -33,6 +37,9 @@ struct virtio_snd_queue {
122 * @card: ALSA sound card.
123 * @ctl_msgs: Pending control request list.
124 * @event_msgs: Device events.
125 + * @pcm_list: VirtIO PCM device list.
126 + * @substreams: VirtIO PCM substreams.
127 + * @nsubstreams: Number of PCM substreams.
130 struct virtio_device *vdev;
131 @@ -40,6 +47,9 @@ struct virtio_snd {
132 struct snd_card *card;
133 struct list_head ctl_msgs;
134 struct virtio_snd_event *event_msgs;
135 + struct list_head pcm_list;
136 + struct virtio_pcm_substream *substreams;
140 /* Message completion timeout in milliseconds (module parameter). */
141 diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c
143 index 000000000000..e16567e2e214
145 +++ b/sound/virtio/virtio_pcm.c
147 +// SPDX-License-Identifier: GPL-2.0+
149 + * virtio-snd: Virtio sound device
150 + * Copyright (C) 2021 OpenSynergy GmbH
152 +#include <linux/moduleparam.h>
153 +#include <linux/virtio_config.h>
155 +#include "virtio_card.h"
157 +static u32 pcm_buffer_ms = 160;
158 +module_param(pcm_buffer_ms, uint, 0644);
159 +MODULE_PARM_DESC(pcm_buffer_ms, "PCM substream buffer time in milliseconds");
161 +static u32 pcm_periods_min = 2;
162 +module_param(pcm_periods_min, uint, 0644);
163 +MODULE_PARM_DESC(pcm_periods_min, "Minimum number of PCM periods");
165 +static u32 pcm_periods_max = 16;
166 +module_param(pcm_periods_max, uint, 0644);
167 +MODULE_PARM_DESC(pcm_periods_max, "Maximum number of PCM periods");
169 +static u32 pcm_period_ms_min = 10;
170 +module_param(pcm_period_ms_min, uint, 0644);
171 +MODULE_PARM_DESC(pcm_period_ms_min, "Minimum PCM period time in milliseconds");
173 +static u32 pcm_period_ms_max = 80;
174 +module_param(pcm_period_ms_max, uint, 0644);
175 +MODULE_PARM_DESC(pcm_period_ms_max, "Maximum PCM period time in milliseconds");
177 +/* Map for converting VirtIO format to ALSA format. */
178 +static const snd_pcm_format_t g_v2a_format_map[] = {
179 + [VIRTIO_SND_PCM_FMT_IMA_ADPCM] = SNDRV_PCM_FORMAT_IMA_ADPCM,
180 + [VIRTIO_SND_PCM_FMT_MU_LAW] = SNDRV_PCM_FORMAT_MU_LAW,
181 + [VIRTIO_SND_PCM_FMT_A_LAW] = SNDRV_PCM_FORMAT_A_LAW,
182 + [VIRTIO_SND_PCM_FMT_S8] = SNDRV_PCM_FORMAT_S8,
183 + [VIRTIO_SND_PCM_FMT_U8] = SNDRV_PCM_FORMAT_U8,
184 + [VIRTIO_SND_PCM_FMT_S16] = SNDRV_PCM_FORMAT_S16_LE,
185 + [VIRTIO_SND_PCM_FMT_U16] = SNDRV_PCM_FORMAT_U16_LE,
186 + [VIRTIO_SND_PCM_FMT_S18_3] = SNDRV_PCM_FORMAT_S18_3LE,
187 + [VIRTIO_SND_PCM_FMT_U18_3] = SNDRV_PCM_FORMAT_U18_3LE,
188 + [VIRTIO_SND_PCM_FMT_S20_3] = SNDRV_PCM_FORMAT_S20_3LE,
189 + [VIRTIO_SND_PCM_FMT_U20_3] = SNDRV_PCM_FORMAT_U20_3LE,
190 + [VIRTIO_SND_PCM_FMT_S24_3] = SNDRV_PCM_FORMAT_S24_3LE,
191 + [VIRTIO_SND_PCM_FMT_U24_3] = SNDRV_PCM_FORMAT_U24_3LE,
192 + [VIRTIO_SND_PCM_FMT_S20] = SNDRV_PCM_FORMAT_S20_LE,
193 + [VIRTIO_SND_PCM_FMT_U20] = SNDRV_PCM_FORMAT_U20_LE,
194 + [VIRTIO_SND_PCM_FMT_S24] = SNDRV_PCM_FORMAT_S24_LE,
195 + [VIRTIO_SND_PCM_FMT_U24] = SNDRV_PCM_FORMAT_U24_LE,
196 + [VIRTIO_SND_PCM_FMT_S32] = SNDRV_PCM_FORMAT_S32_LE,
197 + [VIRTIO_SND_PCM_FMT_U32] = SNDRV_PCM_FORMAT_U32_LE,
198 + [VIRTIO_SND_PCM_FMT_FLOAT] = SNDRV_PCM_FORMAT_FLOAT_LE,
199 + [VIRTIO_SND_PCM_FMT_FLOAT64] = SNDRV_PCM_FORMAT_FLOAT64_LE,
200 + [VIRTIO_SND_PCM_FMT_DSD_U8] = SNDRV_PCM_FORMAT_DSD_U8,
201 + [VIRTIO_SND_PCM_FMT_DSD_U16] = SNDRV_PCM_FORMAT_DSD_U16_LE,
202 + [VIRTIO_SND_PCM_FMT_DSD_U32] = SNDRV_PCM_FORMAT_DSD_U32_LE,
203 + [VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME] =
204 + SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE
207 +/* Map for converting VirtIO frame rate to ALSA frame rate. */
208 +struct virtsnd_v2a_rate {
209 + unsigned int alsa_bit;
213 +static const struct virtsnd_v2a_rate g_v2a_rate_map[] = {
214 + [VIRTIO_SND_PCM_RATE_5512] = { SNDRV_PCM_RATE_5512, 5512 },
215 + [VIRTIO_SND_PCM_RATE_8000] = { SNDRV_PCM_RATE_8000, 8000 },
216 + [VIRTIO_SND_PCM_RATE_11025] = { SNDRV_PCM_RATE_11025, 11025 },
217 + [VIRTIO_SND_PCM_RATE_16000] = { SNDRV_PCM_RATE_16000, 16000 },
218 + [VIRTIO_SND_PCM_RATE_22050] = { SNDRV_PCM_RATE_22050, 22050 },
219 + [VIRTIO_SND_PCM_RATE_32000] = { SNDRV_PCM_RATE_32000, 32000 },
220 + [VIRTIO_SND_PCM_RATE_44100] = { SNDRV_PCM_RATE_44100, 44100 },
221 + [VIRTIO_SND_PCM_RATE_48000] = { SNDRV_PCM_RATE_48000, 48000 },
222 + [VIRTIO_SND_PCM_RATE_64000] = { SNDRV_PCM_RATE_64000, 64000 },
223 + [VIRTIO_SND_PCM_RATE_88200] = { SNDRV_PCM_RATE_88200, 88200 },
224 + [VIRTIO_SND_PCM_RATE_96000] = { SNDRV_PCM_RATE_96000, 96000 },
225 + [VIRTIO_SND_PCM_RATE_176400] = { SNDRV_PCM_RATE_176400, 176400 },
226 + [VIRTIO_SND_PCM_RATE_192000] = { SNDRV_PCM_RATE_192000, 192000 }
230 + * virtsnd_pcm_build_hw() - Parse substream config and build HW descriptor.
231 + * @vss: VirtIO substream.
232 + * @info: VirtIO substream information entry.
234 + * Context: Any context.
235 + * Return: 0 on success, -EINVAL if configuration is invalid.
237 +static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
238 + struct virtio_snd_pcm_info *info)
240 + struct virtio_device *vdev = vss->snd->vdev;
243 + size_t sample_max = 0;
244 + size_t sample_min = 0;
246 + vss->features = le32_to_cpu(info->features);
249 + * TODO: set SNDRV_PCM_INFO_{BATCH,BLOCK_TRANSFER} if device supports
250 + * only message-based transport.
253 + SNDRV_PCM_INFO_MMAP |
254 + SNDRV_PCM_INFO_MMAP_VALID |
255 + SNDRV_PCM_INFO_BATCH |
256 + SNDRV_PCM_INFO_BLOCK_TRANSFER |
257 + SNDRV_PCM_INFO_INTERLEAVED |
258 + SNDRV_PCM_INFO_PAUSE;
260 + if (!info->channels_min || info->channels_min > info->channels_max) {
261 + dev_err(&vdev->dev,
262 + "SID %u: invalid channel range [%u %u]\n",
263 + vss->sid, info->channels_min, info->channels_max);
267 + vss->hw.channels_min = info->channels_min;
268 + vss->hw.channels_max = info->channels_max;
270 + values = le64_to_cpu(info->formats);
272 + vss->hw.formats = 0;
274 + for (i = 0; i < ARRAY_SIZE(g_v2a_format_map); ++i)
275 + if (values & (1ULL << i)) {
276 + snd_pcm_format_t alsa_fmt = g_v2a_format_map[i];
277 + int bytes = snd_pcm_format_physical_width(alsa_fmt) / 8;
279 + if (!sample_min || sample_min > bytes)
280 + sample_min = bytes;
282 + if (sample_max < bytes)
283 + sample_max = bytes;
285 + vss->hw.formats |= pcm_format_to_bits(alsa_fmt);
288 + if (!vss->hw.formats) {
289 + dev_err(&vdev->dev,
290 + "SID %u: no supported PCM sample formats found\n",
295 + values = le64_to_cpu(info->rates);
299 + for (i = 0; i < ARRAY_SIZE(g_v2a_rate_map); ++i)
300 + if (values & (1ULL << i)) {
301 + if (!vss->hw.rate_min ||
302 + vss->hw.rate_min > g_v2a_rate_map[i].rate)
303 + vss->hw.rate_min = g_v2a_rate_map[i].rate;
305 + if (vss->hw.rate_max < g_v2a_rate_map[i].rate)
306 + vss->hw.rate_max = g_v2a_rate_map[i].rate;
308 + vss->hw.rates |= g_v2a_rate_map[i].alsa_bit;
311 + if (!vss->hw.rates) {
312 + dev_err(&vdev->dev,
313 + "SID %u: no supported PCM frame rates found\n",
318 + vss->hw.periods_min = pcm_periods_min;
319 + vss->hw.periods_max = pcm_periods_max;
322 + * We must ensure that there is enough space in the buffer to store
323 + * pcm_buffer_ms ms for the combination (Cmax, Smax, Rmax), where:
324 + * Cmax = maximum supported number of channels,
325 + * Smax = maximum supported sample size in bytes,
326 + * Rmax = maximum supported frame rate.
328 + vss->hw.buffer_bytes_max =
329 + PAGE_ALIGN(sample_max * vss->hw.channels_max * pcm_buffer_ms *
330 + (vss->hw.rate_max / MSEC_PER_SEC));
333 + * We must ensure that the minimum period size is enough to store
334 + * pcm_period_ms_min ms for the combination (Cmin, Smin, Rmin), where:
335 + * Cmin = minimum supported number of channels,
336 + * Smin = minimum supported sample size in bytes,
337 + * Rmin = minimum supported frame rate.
339 + vss->hw.period_bytes_min =
340 + sample_min * vss->hw.channels_min * pcm_period_ms_min *
341 + (vss->hw.rate_min / MSEC_PER_SEC);
344 + * We must ensure that the maximum period size is enough to store
345 + * pcm_period_ms_max ms for the combination (Cmax, Smax, Rmax).
347 + vss->hw.period_bytes_max =
348 + sample_max * vss->hw.channels_max * pcm_period_ms_max *
349 + (vss->hw.rate_max / MSEC_PER_SEC);
355 + * virtsnd_pcm_find() - Find the PCM device for the specified node ID.
356 + * @snd: VirtIO sound device.
357 + * @nid: Function node ID.
359 + * Context: Any context.
360 + * Return: a pointer to the PCM device or ERR_PTR(-ENOENT).
362 +struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid)
364 + struct virtio_pcm *vpcm;
366 + list_for_each_entry(vpcm, &snd->pcm_list, list)
367 + if (vpcm->nid == nid)
370 + return ERR_PTR(-ENOENT);
374 + * virtsnd_pcm_find_or_create() - Find or create the PCM device for the
375 + * specified node ID.
376 + * @snd: VirtIO sound device.
377 + * @nid: Function node ID.
379 + * Context: Any context that permits to sleep.
380 + * Return: a pointer to the PCM device or ERR_PTR(-errno).
382 +struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid)
384 + struct virtio_device *vdev = snd->vdev;
385 + struct virtio_pcm *vpcm;
387 + vpcm = virtsnd_pcm_find(snd, nid);
391 + vpcm = devm_kzalloc(&vdev->dev, sizeof(*vpcm), GFP_KERNEL);
393 + return ERR_PTR(-ENOMEM);
396 + list_add_tail(&vpcm->list, &snd->pcm_list);
402 + * virtsnd_pcm_validate() - Validate if the device can be started.
403 + * @vdev: VirtIO parent device.
405 + * Context: Any context.
406 + * Return: 0 on success, -EINVAL on failure.
408 +int virtsnd_pcm_validate(struct virtio_device *vdev)
410 + if (pcm_periods_min < 2 || pcm_periods_min > pcm_periods_max) {
411 + dev_err(&vdev->dev,
412 + "invalid range [%u %u] of the number of PCM periods\n",
413 + pcm_periods_min, pcm_periods_max);
417 + if (!pcm_period_ms_min || pcm_period_ms_min > pcm_period_ms_max) {
418 + dev_err(&vdev->dev,
419 + "invalid range [%u %u] of the size of the PCM period\n",
420 + pcm_period_ms_min, pcm_period_ms_max);
424 + if (pcm_buffer_ms < pcm_periods_min * pcm_period_ms_min) {
425 + dev_err(&vdev->dev,
426 + "pcm_buffer_ms(=%u) value cannot be < %u ms\n",
427 + pcm_buffer_ms, pcm_periods_min * pcm_period_ms_min);
431 + if (pcm_period_ms_max > pcm_buffer_ms / 2) {
432 + dev_err(&vdev->dev,
433 + "pcm_period_ms_max(=%u) value cannot be > %u ms\n",
434 + pcm_period_ms_max, pcm_buffer_ms / 2);
442 + * virtsnd_pcm_period_elapsed() - Kernel work function to handle the elapsed
444 + * @work: Elapsed period work.
446 + * The main purpose of this function is to call snd_pcm_period_elapsed() in
447 + * a process context, not in an interrupt context. This is necessary because PCM
448 + * devices operate in non-atomic mode.
450 + * Context: Process context.
452 +static void virtsnd_pcm_period_elapsed(struct work_struct *work)
454 + struct virtio_pcm_substream *vss =
455 + container_of(work, struct virtio_pcm_substream, elapsed_period);
457 + snd_pcm_period_elapsed(vss->substream);
461 + * virtsnd_pcm_parse_cfg() - Parse the stream configuration.
462 + * @snd: VirtIO sound device.
464 + * This function is called during initial device initialization.
466 + * Context: Any context that permits to sleep.
467 + * Return: 0 on success, -errno on failure.
469 +int virtsnd_pcm_parse_cfg(struct virtio_snd *snd)
471 + struct virtio_device *vdev = snd->vdev;
472 + struct virtio_snd_pcm_info *info;
476 + virtio_cread_le(vdev, struct virtio_snd_config, streams,
477 + &snd->nsubstreams);
478 + if (!snd->nsubstreams)
481 + snd->substreams = devm_kcalloc(&vdev->dev, snd->nsubstreams,
482 + sizeof(*snd->substreams), GFP_KERNEL);
483 + if (!snd->substreams)
486 + info = kcalloc(snd->nsubstreams, sizeof(*info), GFP_KERNEL);
490 + rc = virtsnd_ctl_query_info(snd, VIRTIO_SND_R_PCM_INFO, 0,
491 + snd->nsubstreams, sizeof(*info), info);
495 + for (i = 0; i < snd->nsubstreams; ++i) {
496 + struct virtio_pcm_substream *vss = &snd->substreams[i];
497 + struct virtio_pcm *vpcm;
501 + INIT_WORK(&vss->elapsed_period, virtsnd_pcm_period_elapsed);
503 + rc = virtsnd_pcm_build_hw(vss, &info[i]);
507 + vss->nid = le32_to_cpu(info[i].hdr.hda_fn_nid);
509 + vpcm = virtsnd_pcm_find_or_create(snd, vss->nid);
510 + if (IS_ERR(vpcm)) {
511 + rc = PTR_ERR(vpcm);
515 + switch (info[i].direction) {
516 + case VIRTIO_SND_D_OUTPUT:
517 + vss->direction = SNDRV_PCM_STREAM_PLAYBACK;
519 + case VIRTIO_SND_D_INPUT:
520 + vss->direction = SNDRV_PCM_STREAM_CAPTURE;
523 + dev_err(&vdev->dev, "SID %u: unknown direction (%u)\n",
524 + vss->sid, info[i].direction);
529 + vpcm->streams[vss->direction].nsubstreams++;
539 + * virtsnd_pcm_build_devs() - Build ALSA PCM devices.
540 + * @snd: VirtIO sound device.
542 + * Context: Any context that permits to sleep.
543 + * Return: 0 on success, -errno on failure.
545 +int virtsnd_pcm_build_devs(struct virtio_snd *snd)
547 + struct virtio_device *vdev = snd->vdev;
548 + struct virtio_pcm *vpcm;
552 + list_for_each_entry(vpcm, &snd->pcm_list, list) {
553 + unsigned int npbs =
554 + vpcm->streams[SNDRV_PCM_STREAM_PLAYBACK].nsubstreams;
555 + unsigned int ncps =
556 + vpcm->streams[SNDRV_PCM_STREAM_CAPTURE].nsubstreams;
558 + if (!npbs && !ncps)
561 + rc = snd_pcm_new(snd->card, VIRTIO_SND_CARD_DRIVER, vpcm->nid,
562 + npbs, ncps, &vpcm->pcm);
564 + dev_err(&vdev->dev, "snd_pcm_new[%u] failed: %d\n",
569 + vpcm->pcm->info_flags = 0;
570 + vpcm->pcm->dev_class = SNDRV_PCM_CLASS_GENERIC;
571 + vpcm->pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
572 + snprintf(vpcm->pcm->name, sizeof(vpcm->pcm->name),
573 + VIRTIO_SND_PCM_NAME " %u", vpcm->pcm->device);
574 + vpcm->pcm->private_data = vpcm;
575 + vpcm->pcm->nonatomic = true;
577 + for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
578 + struct virtio_pcm_stream *stream = &vpcm->streams[i];
580 + if (!stream->nsubstreams)
583 + stream->substreams =
584 + devm_kcalloc(&vdev->dev, stream->nsubstreams,
585 + sizeof(*stream->substreams),
587 + if (!stream->substreams)
590 + stream->nsubstreams = 0;
594 + for (i = 0; i < snd->nsubstreams; ++i) {
595 + struct virtio_pcm_stream *vs;
596 + struct virtio_pcm_substream *vss = &snd->substreams[i];
598 + vpcm = virtsnd_pcm_find(snd, vss->nid);
600 + return PTR_ERR(vpcm);
602 + vs = &vpcm->streams[vss->direction];
603 + vs->substreams[vs->nsubstreams++] = vss;
606 + list_for_each_entry(vpcm, &snd->pcm_list, list) {
607 + for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
608 + struct virtio_pcm_stream *vs = &vpcm->streams[i];
609 + struct snd_pcm_str *ks = &vpcm->pcm->streams[i];
610 + struct snd_pcm_substream *kss;
612 + if (!vs->nsubstreams)
615 + for (kss = ks->substream; kss; kss = kss->next)
616 + vs->substreams[kss->number]->substream = kss;
619 + snd_pcm_set_managed_buffer_all(vpcm->pcm,
620 + SNDRV_DMA_TYPE_VMALLOC, NULL,
626 diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h
628 index 000000000000..84f2f3f14f48
630 +++ b/sound/virtio/virtio_pcm.h
632 +/* SPDX-License-Identifier: GPL-2.0+ */
634 + * virtio-snd: Virtio sound device
635 + * Copyright (C) 2021 OpenSynergy GmbH
637 +#ifndef VIRTIO_SND_PCM_H
638 +#define VIRTIO_SND_PCM_H
640 +#include <linux/atomic.h>
641 +#include <linux/virtio_config.h>
642 +#include <sound/pcm.h>
645 +struct virtio_pcm_msg;
648 + * struct virtio_pcm_substream - VirtIO PCM substream.
649 + * @snd: VirtIO sound device.
650 + * @nid: Function group node identifier.
651 + * @sid: Stream identifier.
652 + * @direction: Stream data flow direction (SNDRV_PCM_STREAM_XXX).
653 + * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX).
654 + * @substream: Kernel ALSA substream.
655 + * @hw: Kernel ALSA substream hardware descriptor.
656 + * @elapsed_period: Kernel work to handle the elapsed period state.
658 +struct virtio_pcm_substream {
659 + struct virtio_snd *snd;
664 + struct snd_pcm_substream *substream;
665 + struct snd_pcm_hardware hw;
666 + struct work_struct elapsed_period;
670 + * struct virtio_pcm_stream - VirtIO PCM stream.
671 + * @substreams: VirtIO substreams belonging to the stream.
672 + * @nsubstreams: Number of substreams.
674 +struct virtio_pcm_stream {
675 + struct virtio_pcm_substream **substreams;
680 + * struct virtio_pcm - VirtIO PCM device.
681 + * @list: VirtIO PCM list entry.
682 + * @nid: Function group node identifier.
683 + * @pcm: Kernel PCM device.
684 + * @streams: VirtIO PCM streams (playback and capture).
687 + struct list_head list;
689 + struct snd_pcm *pcm;
690 + struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1];
693 +int virtsnd_pcm_validate(struct virtio_device *vdev);
695 +int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
697 +int virtsnd_pcm_build_devs(struct virtio_snd *snd);
699 +struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid);
701 +struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid);
703 +#endif /* VIRTIO_SND_PCM_H */