virtio: Backport virtio sound driver.
[AGL/meta-agl.git] / meta-agl-bsp / virtualization-layer / recipes-kernel / linux / linux-yocto / virtio-kmeta / bsp / virtio / virtio-snd / 0006-ALSA-virtio-PCM-substream-operators.patch
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
5
6 Introduce the operators required for the operation of substreams.
7
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>
12 ---
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
19
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 := \
25         virtio_card.o \
26         virtio_ctl_msg.o \
27         virtio_pcm.o \
28 -       virtio_pcm_msg.o
29 +       virtio_pcm_msg.o \
30 +       virtio_pcm_ops.o
31  
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)
37  
38                         for (kss = ks->substream; kss; kss = kss->next)
39                                 vs->substreams[kss->number]->substream = kss;
40 +
41 +                       snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops);
42                 }
43  
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
54 + *           side.
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 {
59         size_t hw_ptr;
60         bool xfer_enabled;
61         bool xfer_xrun;
62 +       bool stopped;
63         struct virtio_pcm_msg **msgs;
64         unsigned int nmsgs;
65         int msg_last_enqueued;
66 @@ -80,6 +83,8 @@ struct virtio_pcm {
67         struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1];
68  };
69  
70 +extern const struct snd_pcm_ops virtsnd_pcm_ops;
71 +
72  int virtsnd_pcm_validate(struct virtio_device *vdev);
73  
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
76 new file mode 100644
77 index 000000000000..0682a2df6c8c
78 --- /dev/null
79 +++ b/sound/virtio/virtio_pcm_ops.c
80 @@ -0,0 +1,445 @@
81 +// SPDX-License-Identifier: GPL-2.0+
82 +/*
83 + * virtio-snd: Virtio sound device
84 + * Copyright (C) 2021 OpenSynergy GmbH
85 + */
86 +#include <sound/pcm_params.h>
87 +
88 +#include "virtio_card.h"
89 +
90 +/*
91 + * I/O messages lifetime
92 + * ---------------------
93 + *
94 + * Allocation:
95 + *   Messages are initially allocated in the ops->hw_params() after the size and
96 + *   number of periods have been successfully negotiated.
97 + *
98 + * Freeing:
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
101 + *   called.
102 + *
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.
108 + *
109 + *   Therefore, freeing of already allocated messages occurs:
110 + *
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;
113 + *
114 + *   - in ops->hw_free(), if the queue has been successfully flushed;
115 + *
116 + *   - in dev->release().
117 + */
118 +
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;
123 +};
124 +
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 }
152 +};
153 +
154 +/* Map for converting ALSA frame rate to VirtIO frame rate. */
155 +struct virtsnd_a2v_rate {
156 +       unsigned int rate;
157 +       unsigned int vio_bit;
158 +};
159 +
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 }
174 +};
175 +
176 +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream);
177 +
178 +/**
179 + * virtsnd_pcm_open() - Open the PCM substream.
180 + * @substream: Kernel ALSA substream.
181 + *
182 + * Context: Process context.
183 + * Return: 0 on success, -errno on failure.
184 + */
185 +static int virtsnd_pcm_open(struct snd_pcm_substream *substream)
186 +{
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];
190 +
191 +       substream->runtime->hw = vss->hw;
192 +       substream->private_data = vss;
193 +
194 +       snd_pcm_hw_constraint_integer(substream->runtime,
195 +                                     SNDRV_PCM_HW_PARAM_PERIODS);
196 +
197 +       vss->stopped = !!virtsnd_pcm_msg_pending_num(vss);
198 +
199 +       /*
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.
203 +        */
204 +       return virtsnd_pcm_sync_stop(substream);
205 +}
206 +
207 +/**
208 + * virtsnd_pcm_close() - Close the PCM substream.
209 + * @substream: Kernel ALSA substream.
210 + *
211 + * Context: Process context.
212 + * Return: 0.
213 + */
214 +static int virtsnd_pcm_close(struct snd_pcm_substream *substream)
215 +{
216 +       return 0;
217 +}
218 +
219 +/**
220 + * virtsnd_pcm_dev_set_params() - Set the parameters of the PCM substream on
221 + *                                the device side.
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.
228 + *
229 + * Context: Any context that permits to sleep.
230 + * Return: 0 on success, -errno on failure.
231 + */
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,
237 +                                     unsigned int rate)
238 +{
239 +       struct virtio_snd_msg *msg;
240 +       struct virtio_snd_pcm_set_params *request;
241 +       unsigned int i;
242 +       int vformat = -1;
243 +       int vrate = -1;
244 +
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;
248 +
249 +                       break;
250 +               }
251 +
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;
255 +
256 +                       break;
257 +               }
258 +
259 +       if (vformat == -1 || vrate == -1)
260 +               return -EINVAL;
261 +
262 +       msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_SET_PARAMS,
263 +                                       GFP_KERNEL);
264 +       if (!msg)
265 +               return -ENOMEM;
266 +
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;
273 +
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);
277 +
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);
281 +
282 +       return virtsnd_ctl_msg_send_sync(vss->snd, msg);
283 +}
284 +
285 +/**
286 + * virtsnd_pcm_hw_params() - Set the parameters of the PCM substream.
287 + * @substream: Kernel ALSA substream.
288 + * @hw_params: Hardware parameters.
289 + *
290 + * Context: Process context.
291 + * Return: 0 on success, -errno on failure.
292 + */
293 +static int virtsnd_pcm_hw_params(struct snd_pcm_substream *substream,
294 +                                struct snd_pcm_hw_params *hw_params)
295 +{
296 +       struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
297 +       struct virtio_device *vdev = vss->snd->vdev;
298 +       int rc;
299 +
300 +       if (virtsnd_pcm_msg_pending_num(vss)) {
301 +               dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n",
302 +                       vss->sid);
303 +               return -EBADFD;
304 +       }
305 +
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));
311 +       if (rc)
312 +               return rc;
313 +
314 +       /*
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.
317 +        */
318 +       virtsnd_pcm_msg_free(vss);
319 +
320 +       return virtsnd_pcm_msg_alloc(vss, params_periods(hw_params),
321 +                                    params_period_bytes(hw_params));
322 +}
323 +
324 +/**
325 + * virtsnd_pcm_hw_free() - Reset the parameters of the PCM substream.
326 + * @substream: Kernel ALSA substream.
327 + *
328 + * Context: Process context.
329 + * Return: 0
330 + */
331 +static int virtsnd_pcm_hw_free(struct snd_pcm_substream *substream)
332 +{
333 +       struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
334 +
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);
338 +
339 +       return 0;
340 +}
341 +
342 +/**
343 + * virtsnd_pcm_prepare() - Prepare the PCM substream.
344 + * @substream: Kernel ALSA substream.
345 + *
346 + * Context: Process context.
347 + * Return: 0 on success, -errno on failure.
348 + */
349 +static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream)
350 +{
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;
354 +
355 +       if (virtsnd_pcm_msg_pending_num(vss)) {
356 +               dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n",
357 +                       vss->sid);
358 +               return -EBADFD;
359 +       }
360 +
361 +       vss->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
362 +       vss->hw_ptr = 0;
363 +       vss->xfer_xrun = false;
364 +       vss->msg_last_enqueued = -1;
365 +       vss->msg_count = 0;
366 +
367 +       msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_PREPARE,
368 +                                       GFP_KERNEL);
369 +       if (!msg)
370 +               return -ENOMEM;
371 +
372 +       return virtsnd_ctl_msg_send_sync(vss->snd, msg);
373 +}
374 +
375 +/**
376 + * virtsnd_pcm_trigger() - Process command for the PCM substream.
377 + * @substream: Kernel ALSA substream.
378 + * @command: Substream command (SNDRV_PCM_TRIGGER_XXX).
379 + *
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.
383 + */
384 +static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
385 +{
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;
391 +       int rc;
392 +
393 +       switch (command) {
394 +       case SNDRV_PCM_TRIGGER_START:
395 +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
396 +               queue = virtsnd_pcm_queue(vss);
397 +
398 +               spin_lock_irqsave(&queue->lock, flags);
399 +               spin_lock(&vss->lock);
400 +               rc = virtsnd_pcm_msg_send(vss);
401 +               if (!rc)
402 +                       vss->xfer_enabled = true;
403 +               spin_unlock(&vss->lock);
404 +               spin_unlock_irqrestore(&queue->lock, flags);
405 +               if (rc)
406 +                       return rc;
407 +
408 +               msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START,
409 +                                               GFP_KERNEL);
410 +               if (!msg) {
411 +                       spin_lock_irqsave(&vss->lock, flags);
412 +                       vss->xfer_enabled = false;
413 +                       spin_unlock_irqrestore(&vss->lock, flags);
414 +
415 +                       return -ENOMEM;
416 +               }
417 +
418 +               return virtsnd_ctl_msg_send_sync(snd, msg);
419 +       case SNDRV_PCM_TRIGGER_STOP:
420 +               vss->stopped = true;
421 +               fallthrough;
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);
426 +
427 +               msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP,
428 +                                               GFP_KERNEL);
429 +               if (!msg)
430 +                       return -ENOMEM;
431 +
432 +               return virtsnd_ctl_msg_send_sync(snd, msg);
433 +       default:
434 +               return -EINVAL;
435 +       }
436 +}
437 +
438 +/**
439 + * virtsnd_pcm_sync_stop() - Synchronous PCM substream stop.
440 + * @substream: Kernel ALSA substream.
441 + *
442 + * The function can be called both from the upper level or from the driver
443 + * itself.
444 + *
445 + * Context: Process context. Takes and releases the VirtIO substream spinlock.
446 + * Return: 0 on success, -errno on failure.
447 + */
448 +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream)
449 +{
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);
454 +       int rc;
455 +
456 +       cancel_work_sync(&vss->elapsed_period);
457 +
458 +       if (!vss->stopped)
459 +               return 0;
460 +
461 +       msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE,
462 +                                       GFP_KERNEL);
463 +       if (!msg)
464 +               return -ENOMEM;
465 +
466 +       rc = virtsnd_ctl_msg_send_sync(snd, msg);
467 +       if (rc)
468 +               return rc;
469 +
470 +       /*
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.
475 +        */
476 +       rc = wait_event_interruptible_timeout(vss->msg_empty,
477 +                                             !virtsnd_pcm_msg_pending_num(vss),
478 +                                             js);
479 +       if (rc <= 0) {
480 +               dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n",
481 +                        vss->sid);
482 +
483 +               return !rc ? -ETIMEDOUT : rc;
484 +       }
485 +
486 +       vss->stopped = false;
487 +
488 +       return 0;
489 +}
490 +
491 +/**
492 + * virtsnd_pcm_pointer() - Get the current hardware position for the PCM
493 + *                         substream.
494 + * @substream: Kernel ALSA substream.
495 + *
496 + * Context: Any context. Takes and releases the VirtIO substream spinlock.
497 + * Return: Hardware position in frames inside [0 ... buffer_size) range.
498 + */
499 +static snd_pcm_uframes_t
500 +virtsnd_pcm_pointer(struct snd_pcm_substream *substream)
501 +{
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;
505 +
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);
510 +
511 +       return hw_ptr;
512 +}
513 +
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,
525 +};