1 From 93c313dc4fc78b077bb0911afe3a77ffa845ad58 Mon Sep 17 00:00:00 2001
2 From: Anton Yakovlev <anton.yakovlev@opensynergy.com>
3 Date: Tue, 2 Mar 2021 17:47:06 +0100
4 Subject: [PATCH] ALSA: virtio: PCM substream operators
6 Introduce the operators required for the operation of substreams.
8 Signed-off-by: Anton Yakovlev <anton.yakovlev@opensynergy.com>
9 Link: https://lore.kernel.org/r/20210302164709.3142702-7-anton.yakovlev@opensynergy.com
10 Signed-off-by: Takashi Iwai <tiwai@suse.de>
11 Signed-off-by: Vasyl Vavrychuk <vasyl.vavrychuk@opensynergy.com>
13 sound/virtio/Makefile | 3 +-
14 sound/virtio/virtio_pcm.c | 2 +
15 sound/virtio/virtio_pcm.h | 5 +
16 sound/virtio/virtio_pcm_ops.c | 445 ++++++++++++++++++++++++++++++++++
17 4 files changed, 454 insertions(+), 1 deletion(-)
18 create mode 100644 sound/virtio/virtio_pcm_ops.c
20 diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile
21 index 626af3cc3ed7..34493226793f 100644
22 --- a/sound/virtio/Makefile
23 +++ b/sound/virtio/Makefile
24 @@ -6,5 +6,6 @@ virtio_snd-objs := \
32 diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c
33 index 2dcd763efa29..c10d91fff2fb 100644
34 --- a/sound/virtio/virtio_pcm.c
35 +++ b/sound/virtio/virtio_pcm.c
36 @@ -470,6 +470,8 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd)
38 for (kss = ks->substream; kss; kss = kss->next)
39 vs->substreams[kss->number]->substream = kss;
41 + snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops);
44 snd_pcm_set_managed_buffer_all(vpcm->pcm,
45 diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h
46 index 6722f1139666..efd0228746cf 100644
47 --- a/sound/virtio/virtio_pcm.h
48 +++ b/sound/virtio/virtio_pcm.h
49 @@ -29,6 +29,8 @@ struct virtio_pcm_msg;
50 * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes).
51 * @xfer_enabled: Data transfer state (0 - off, 1 - on).
52 * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun).
53 + * @stopped: True if the substream is stopped and must be released on the device
55 * @msgs: Allocated I/O messages.
56 * @nmsgs: Number of allocated I/O messages.
57 * @msg_last_enqueued: Index of the last I/O message added to the virtqueue.
58 @@ -49,6 +51,7 @@ struct virtio_pcm_substream {
63 struct virtio_pcm_msg **msgs;
65 int msg_last_enqueued;
66 @@ -80,6 +83,8 @@ struct virtio_pcm {
67 struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1];
70 +extern const struct snd_pcm_ops virtsnd_pcm_ops;
72 int virtsnd_pcm_validate(struct virtio_device *vdev);
74 int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
75 diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c
77 index 000000000000..0682a2df6c8c
79 +++ b/sound/virtio/virtio_pcm_ops.c
81 +// SPDX-License-Identifier: GPL-2.0+
83 + * virtio-snd: Virtio sound device
84 + * Copyright (C) 2021 OpenSynergy GmbH
86 +#include <sound/pcm_params.h>
88 +#include "virtio_card.h"
91 + * I/O messages lifetime
92 + * ---------------------
95 + * Messages are initially allocated in the ops->hw_params() after the size and
96 + * number of periods have been successfully negotiated.
99 + * Messages can be safely freed after the queue has been successfully flushed
100 + * (RELEASE command in the ops->sync_stop()) and the ops->hw_free() has been
103 + * When the substream stops, the ops->sync_stop() waits until the device has
104 + * completed all pending messages. This wait can be interrupted either by a
105 + * signal or due to a timeout. In this case, the device can still access
106 + * messages even after calling ops->hw_free(). It can also issue an interrupt,
107 + * and the interrupt handler will also try to access message structures.
109 + * Therefore, freeing of already allocated messages occurs:
111 + * - in ops->hw_params(), if this operator was called several times in a row,
112 + * or if ops->hw_free() failed to free messages previously;
114 + * - in ops->hw_free(), if the queue has been successfully flushed;
116 + * - in dev->release().
119 +/* Map for converting ALSA format to VirtIO format. */
120 +struct virtsnd_a2v_format {
121 + snd_pcm_format_t alsa_bit;
122 + unsigned int vio_bit;
125 +static const struct virtsnd_a2v_format g_a2v_format_map[] = {
126 + { SNDRV_PCM_FORMAT_IMA_ADPCM, VIRTIO_SND_PCM_FMT_IMA_ADPCM },
127 + { SNDRV_PCM_FORMAT_MU_LAW, VIRTIO_SND_PCM_FMT_MU_LAW },
128 + { SNDRV_PCM_FORMAT_A_LAW, VIRTIO_SND_PCM_FMT_A_LAW },
129 + { SNDRV_PCM_FORMAT_S8, VIRTIO_SND_PCM_FMT_S8 },
130 + { SNDRV_PCM_FORMAT_U8, VIRTIO_SND_PCM_FMT_U8 },
131 + { SNDRV_PCM_FORMAT_S16_LE, VIRTIO_SND_PCM_FMT_S16 },
132 + { SNDRV_PCM_FORMAT_U16_LE, VIRTIO_SND_PCM_FMT_U16 },
133 + { SNDRV_PCM_FORMAT_S18_3LE, VIRTIO_SND_PCM_FMT_S18_3 },
134 + { SNDRV_PCM_FORMAT_U18_3LE, VIRTIO_SND_PCM_FMT_U18_3 },
135 + { SNDRV_PCM_FORMAT_S20_3LE, VIRTIO_SND_PCM_FMT_S20_3 },
136 + { SNDRV_PCM_FORMAT_U20_3LE, VIRTIO_SND_PCM_FMT_U20_3 },
137 + { SNDRV_PCM_FORMAT_S24_3LE, VIRTIO_SND_PCM_FMT_S24_3 },
138 + { SNDRV_PCM_FORMAT_U24_3LE, VIRTIO_SND_PCM_FMT_U24_3 },
139 + { SNDRV_PCM_FORMAT_S20_LE, VIRTIO_SND_PCM_FMT_S20 },
140 + { SNDRV_PCM_FORMAT_U20_LE, VIRTIO_SND_PCM_FMT_U20 },
141 + { SNDRV_PCM_FORMAT_S24_LE, VIRTIO_SND_PCM_FMT_S24 },
142 + { SNDRV_PCM_FORMAT_U24_LE, VIRTIO_SND_PCM_FMT_U24 },
143 + { SNDRV_PCM_FORMAT_S32_LE, VIRTIO_SND_PCM_FMT_S32 },
144 + { SNDRV_PCM_FORMAT_U32_LE, VIRTIO_SND_PCM_FMT_U32 },
145 + { SNDRV_PCM_FORMAT_FLOAT_LE, VIRTIO_SND_PCM_FMT_FLOAT },
146 + { SNDRV_PCM_FORMAT_FLOAT64_LE, VIRTIO_SND_PCM_FMT_FLOAT64 },
147 + { SNDRV_PCM_FORMAT_DSD_U8, VIRTIO_SND_PCM_FMT_DSD_U8 },
148 + { SNDRV_PCM_FORMAT_DSD_U16_LE, VIRTIO_SND_PCM_FMT_DSD_U16 },
149 + { SNDRV_PCM_FORMAT_DSD_U32_LE, VIRTIO_SND_PCM_FMT_DSD_U32 },
150 + { SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE,
151 + VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME }
154 +/* Map for converting ALSA frame rate to VirtIO frame rate. */
155 +struct virtsnd_a2v_rate {
157 + unsigned int vio_bit;
160 +static const struct virtsnd_a2v_rate g_a2v_rate_map[] = {
161 + { 5512, VIRTIO_SND_PCM_RATE_5512 },
162 + { 8000, VIRTIO_SND_PCM_RATE_8000 },
163 + { 11025, VIRTIO_SND_PCM_RATE_11025 },
164 + { 16000, VIRTIO_SND_PCM_RATE_16000 },
165 + { 22050, VIRTIO_SND_PCM_RATE_22050 },
166 + { 32000, VIRTIO_SND_PCM_RATE_32000 },
167 + { 44100, VIRTIO_SND_PCM_RATE_44100 },
168 + { 48000, VIRTIO_SND_PCM_RATE_48000 },
169 + { 64000, VIRTIO_SND_PCM_RATE_64000 },
170 + { 88200, VIRTIO_SND_PCM_RATE_88200 },
171 + { 96000, VIRTIO_SND_PCM_RATE_96000 },
172 + { 176400, VIRTIO_SND_PCM_RATE_176400 },
173 + { 192000, VIRTIO_SND_PCM_RATE_192000 }
176 +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream);
179 + * virtsnd_pcm_open() - Open the PCM substream.
180 + * @substream: Kernel ALSA substream.
182 + * Context: Process context.
183 + * Return: 0 on success, -errno on failure.
185 +static int virtsnd_pcm_open(struct snd_pcm_substream *substream)
187 + struct virtio_pcm *vpcm = snd_pcm_substream_chip(substream);
188 + struct virtio_pcm_stream *vs = &vpcm->streams[substream->stream];
189 + struct virtio_pcm_substream *vss = vs->substreams[substream->number];
191 + substream->runtime->hw = vss->hw;
192 + substream->private_data = vss;
194 + snd_pcm_hw_constraint_integer(substream->runtime,
195 + SNDRV_PCM_HW_PARAM_PERIODS);
197 + vss->stopped = !!virtsnd_pcm_msg_pending_num(vss);
200 + * If the substream has already been used, then the I/O queue may be in
201 + * an invalid state. Just in case, we do a check and try to return the
202 + * queue to its original state, if necessary.
204 + return virtsnd_pcm_sync_stop(substream);
208 + * virtsnd_pcm_close() - Close the PCM substream.
209 + * @substream: Kernel ALSA substream.
211 + * Context: Process context.
214 +static int virtsnd_pcm_close(struct snd_pcm_substream *substream)
220 + * virtsnd_pcm_dev_set_params() - Set the parameters of the PCM substream on
222 + * @vss: VirtIO PCM substream.
223 + * @buffer_bytes: Size of the hardware buffer.
224 + * @period_bytes: Size of the hardware period.
225 + * @channels: Selected number of channels.
226 + * @format: Selected sample format (SNDRV_PCM_FORMAT_XXX).
227 + * @rate: Selected frame rate.
229 + * Context: Any context that permits to sleep.
230 + * Return: 0 on success, -errno on failure.
232 +static int virtsnd_pcm_dev_set_params(struct virtio_pcm_substream *vss,
233 + unsigned int buffer_bytes,
234 + unsigned int period_bytes,
235 + unsigned int channels,
236 + snd_pcm_format_t format,
239 + struct virtio_snd_msg *msg;
240 + struct virtio_snd_pcm_set_params *request;
245 + for (i = 0; i < ARRAY_SIZE(g_a2v_format_map); ++i)
246 + if (g_a2v_format_map[i].alsa_bit == format) {
247 + vformat = g_a2v_format_map[i].vio_bit;
252 + for (i = 0; i < ARRAY_SIZE(g_a2v_rate_map); ++i)
253 + if (g_a2v_rate_map[i].rate == rate) {
254 + vrate = g_a2v_rate_map[i].vio_bit;
259 + if (vformat == -1 || vrate == -1)
262 + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_SET_PARAMS,
267 + request = virtsnd_ctl_msg_request(msg);
268 + request->buffer_bytes = cpu_to_le32(buffer_bytes);
269 + request->period_bytes = cpu_to_le32(period_bytes);
270 + request->channels = channels;
271 + request->format = vformat;
272 + request->rate = vrate;
274 + if (vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING))
275 + request->features |=
276 + cpu_to_le32(1U << VIRTIO_SND_PCM_F_MSG_POLLING);
278 + if (vss->features & (1U << VIRTIO_SND_PCM_F_EVT_XRUNS))
279 + request->features |=
280 + cpu_to_le32(1U << VIRTIO_SND_PCM_F_EVT_XRUNS);
282 + return virtsnd_ctl_msg_send_sync(vss->snd, msg);
286 + * virtsnd_pcm_hw_params() - Set the parameters of the PCM substream.
287 + * @substream: Kernel ALSA substream.
288 + * @hw_params: Hardware parameters.
290 + * Context: Process context.
291 + * Return: 0 on success, -errno on failure.
293 +static int virtsnd_pcm_hw_params(struct snd_pcm_substream *substream,
294 + struct snd_pcm_hw_params *hw_params)
296 + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
297 + struct virtio_device *vdev = vss->snd->vdev;
300 + if (virtsnd_pcm_msg_pending_num(vss)) {
301 + dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n",
306 + rc = virtsnd_pcm_dev_set_params(vss, params_buffer_bytes(hw_params),
307 + params_period_bytes(hw_params),
308 + params_channels(hw_params),
309 + params_format(hw_params),
310 + params_rate(hw_params));
315 + * Free previously allocated messages if ops->hw_params() is called
316 + * several times in a row, or if ops->hw_free() failed to free messages.
318 + virtsnd_pcm_msg_free(vss);
320 + return virtsnd_pcm_msg_alloc(vss, params_periods(hw_params),
321 + params_period_bytes(hw_params));
325 + * virtsnd_pcm_hw_free() - Reset the parameters of the PCM substream.
326 + * @substream: Kernel ALSA substream.
328 + * Context: Process context.
331 +static int virtsnd_pcm_hw_free(struct snd_pcm_substream *substream)
333 + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
335 + /* If the queue is flushed, we can safely free the messages here. */
336 + if (!virtsnd_pcm_msg_pending_num(vss))
337 + virtsnd_pcm_msg_free(vss);
343 + * virtsnd_pcm_prepare() - Prepare the PCM substream.
344 + * @substream: Kernel ALSA substream.
346 + * Context: Process context.
347 + * Return: 0 on success, -errno on failure.
349 +static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream)
351 + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
352 + struct virtio_device *vdev = vss->snd->vdev;
353 + struct virtio_snd_msg *msg;
355 + if (virtsnd_pcm_msg_pending_num(vss)) {
356 + dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n",
361 + vss->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
363 + vss->xfer_xrun = false;
364 + vss->msg_last_enqueued = -1;
365 + vss->msg_count = 0;
367 + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_PREPARE,
372 + return virtsnd_ctl_msg_send_sync(vss->snd, msg);
376 + * virtsnd_pcm_trigger() - Process command for the PCM substream.
377 + * @substream: Kernel ALSA substream.
378 + * @command: Substream command (SNDRV_PCM_TRIGGER_XXX).
380 + * Context: Any context. Takes and releases the VirtIO substream spinlock.
381 + * May take and release the tx/rx queue spinlock.
382 + * Return: 0 on success, -errno on failure.
384 +static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
386 + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
387 + struct virtio_snd *snd = vss->snd;
388 + struct virtio_snd_queue *queue;
389 + struct virtio_snd_msg *msg;
390 + unsigned long flags;
394 + case SNDRV_PCM_TRIGGER_START:
395 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
396 + queue = virtsnd_pcm_queue(vss);
398 + spin_lock_irqsave(&queue->lock, flags);
399 + spin_lock(&vss->lock);
400 + rc = virtsnd_pcm_msg_send(vss);
402 + vss->xfer_enabled = true;
403 + spin_unlock(&vss->lock);
404 + spin_unlock_irqrestore(&queue->lock, flags);
408 + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START,
411 + spin_lock_irqsave(&vss->lock, flags);
412 + vss->xfer_enabled = false;
413 + spin_unlock_irqrestore(&vss->lock, flags);
418 + return virtsnd_ctl_msg_send_sync(snd, msg);
419 + case SNDRV_PCM_TRIGGER_STOP:
420 + vss->stopped = true;
422 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
423 + spin_lock_irqsave(&vss->lock, flags);
424 + vss->xfer_enabled = false;
425 + spin_unlock_irqrestore(&vss->lock, flags);
427 + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP,
432 + return virtsnd_ctl_msg_send_sync(snd, msg);
439 + * virtsnd_pcm_sync_stop() - Synchronous PCM substream stop.
440 + * @substream: Kernel ALSA substream.
442 + * The function can be called both from the upper level or from the driver
445 + * Context: Process context. Takes and releases the VirtIO substream spinlock.
446 + * Return: 0 on success, -errno on failure.
448 +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream)
450 + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
451 + struct virtio_snd *snd = vss->snd;
452 + struct virtio_snd_msg *msg;
453 + unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms);
456 + cancel_work_sync(&vss->elapsed_period);
461 + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE,
466 + rc = virtsnd_ctl_msg_send_sync(snd, msg);
471 + * The spec states that upon receipt of the RELEASE command "the device
472 + * MUST complete all pending I/O messages for the specified stream ID".
473 + * Thus, we consider the absence of I/O messages in the queue as an
474 + * indication that the substream has been released.
476 + rc = wait_event_interruptible_timeout(vss->msg_empty,
477 + !virtsnd_pcm_msg_pending_num(vss),
480 + dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n",
483 + return !rc ? -ETIMEDOUT : rc;
486 + vss->stopped = false;
492 + * virtsnd_pcm_pointer() - Get the current hardware position for the PCM
494 + * @substream: Kernel ALSA substream.
496 + * Context: Any context. Takes and releases the VirtIO substream spinlock.
497 + * Return: Hardware position in frames inside [0 ... buffer_size) range.
499 +static snd_pcm_uframes_t
500 +virtsnd_pcm_pointer(struct snd_pcm_substream *substream)
502 + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
503 + snd_pcm_uframes_t hw_ptr = SNDRV_PCM_POS_XRUN;
504 + unsigned long flags;
506 + spin_lock_irqsave(&vss->lock, flags);
507 + if (!vss->xfer_xrun)
508 + hw_ptr = bytes_to_frames(substream->runtime, vss->hw_ptr);
509 + spin_unlock_irqrestore(&vss->lock, flags);
514 +/* PCM substream operators map. */
515 +const struct snd_pcm_ops virtsnd_pcm_ops = {
516 + .open = virtsnd_pcm_open,
517 + .close = virtsnd_pcm_close,
518 + .ioctl = snd_pcm_lib_ioctl,
519 + .hw_params = virtsnd_pcm_hw_params,
520 + .hw_free = virtsnd_pcm_hw_free,
521 + .prepare = virtsnd_pcm_prepare,
522 + .trigger = virtsnd_pcm_trigger,
523 + .sync_stop = virtsnd_pcm_sync_stop,
524 + .pointer = virtsnd_pcm_pointer,