From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B7A5C433DB for ; Tue, 9 Feb 2021 12:45:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A698764E74 for ; Tue, 9 Feb 2021 12:45:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230244AbhBIMoo (ORCPT ); Tue, 9 Feb 2021 07:44:44 -0500 Received: from mx1.opensynergy.com ([217.66.60.4]:51800 "EHLO mx1.opensynergy.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230314AbhBIMmL (ORCPT ); Tue, 9 Feb 2021 07:42:11 -0500 Received: from SR-MAILGATE-02.opensynergy.com (localhost.localdomain [127.0.0.1]) by mx1.opensynergy.com (Proxmox) with ESMTP id 465BCA1616; Tue, 9 Feb 2021 13:40:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=opensynergy.com; h=cc:cc:content-transfer-encoding:content-type:content-type :date:from:from:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=srmailgate02; bh=5sETgeQX5b+B M42aQEpHT8LAGsjGgG2fzKxC5x0epRs=; b=C3caZpEzdtrkF9b9hlccVOIBPiF7 HpviZaaD1B3QdlxB91+tyFTPDbDoLyrOyXoKw4cUkpLqAZOMiTqMLRWE1EDgrJfs E2D5TScAIh66kTwQkbOjKOvFxLlPCb3nGQRpptvrMVy3eXbPkb6ndq4cfKWKKRN/ WQj5rEhNYoRoVdWpTQw9OJ2nlKQJwu+ctmpIkRBCntJoY1fvrI3Q6+mz57t9MRMo s+QEktyav4FYFrJk+JHA+qTC4qhx4bHuXBe0NTw6bj979+nWiY6TIbV0dG2cVo7l PMSnwMqts+F7ezO7XzFAbGfY3znThAmHtLWuudyujCKyLIL5Di9xOxnlJw== From: Anton Yakovlev To: , , CC: "Michael S. Tsirkin" , Jaroslav Kysela , Takashi Iwai , Subject: [PATCH v3 6/9] ALSA: virtio: PCM substream operators Date: Tue, 9 Feb 2021 13:40:07 +0100 Message-ID: <20210209124011.1224628-7-anton.yakovlev@opensynergy.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210209124011.1224628-1-anton.yakovlev@opensynergy.com> References: <20210209124011.1224628-1-anton.yakovlev@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SR-MAIL-02.open-synergy.com (10.26.10.22) To SR-MAIL-01.open-synergy.com (10.26.10.21) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce the operators required for the operation of substreams. Signed-off-by: Anton Yakovlev --- sound/virtio/Makefile | 3 +- sound/virtio/virtio_pcm.c | 2 + sound/virtio/virtio_pcm.h | 4 + sound/virtio/virtio_pcm_ops.c | 471 ++++++++++++++++++++++++++++++++++ 4 files changed, 479 insertions(+), 1 deletion(-) create mode 100644 sound/virtio/virtio_pcm_ops.c diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile index 626af3cc3ed7..34493226793f 100644 --- a/sound/virtio/Makefile +++ b/sound/virtio/Makefile @@ -6,5 +6,6 @@ virtio_snd-objs := \ virtio_card.o \ virtio_ctl_msg.o \ virtio_pcm.o \ - virtio_pcm_msg.o + virtio_pcm_msg.o \ + virtio_pcm_ops.o diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index a74fbfb9f35c..1d98de878385 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -455,6 +455,8 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd) for (kss = ks->substream; kss; kss = kss->next) vs->substreams[kss->number]->substream = kss; + + snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops); } snd_pcm_set_managed_buffer_all(vpcm->pcm, diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index d6be62b52421..4378918b441a 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -35,6 +35,7 @@ struct virtio_pcm_msg; * @msg_last_enqueued: Index of the last I/O message added to the virtqueue. * @msg_count: Number of pending I/O messages in the virtqueue. * @msg_empty: Notify when msg_count is zero. + * @msg_flushing: True if the I/O queue is in flushing state. */ struct virtio_pcm_substream { struct virtio_snd *snd; @@ -56,6 +57,7 @@ struct virtio_pcm_substream { int msg_last_enqueued; unsigned int msg_count; wait_queue_head_t msg_empty; + bool msg_flushing; }; /** @@ -82,6 +84,8 @@ struct virtio_pcm { struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1]; }; +extern const struct snd_pcm_ops virtsnd_pcm_ops; + int virtsnd_pcm_validate(struct virtio_device *vdev); int virtsnd_pcm_parse_cfg(struct virtio_snd *snd); diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c new file mode 100644 index 000000000000..c2224f5461c4 --- /dev/null +++ b/sound/virtio/virtio_pcm_ops.c @@ -0,0 +1,471 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * virtio-snd: Virtio sound device + * Copyright (C) 2021 OpenSynergy GmbH + */ +#include + +#include "virtio_card.h" + +/* Map for converting ALSA format to VirtIO format. */ +struct virtsnd_a2v_format { + unsigned int alsa_bit; + unsigned int vio_bit; +}; + +static const struct virtsnd_a2v_format g_a2v_format_map[] = { + { SNDRV_PCM_FORMAT_IMA_ADPCM, VIRTIO_SND_PCM_FMT_IMA_ADPCM }, + { SNDRV_PCM_FORMAT_MU_LAW, VIRTIO_SND_PCM_FMT_MU_LAW }, + { SNDRV_PCM_FORMAT_A_LAW, VIRTIO_SND_PCM_FMT_A_LAW }, + { SNDRV_PCM_FORMAT_S8, VIRTIO_SND_PCM_FMT_S8 }, + { SNDRV_PCM_FORMAT_U8, VIRTIO_SND_PCM_FMT_U8 }, + { SNDRV_PCM_FORMAT_S16_LE, VIRTIO_SND_PCM_FMT_S16 }, + { SNDRV_PCM_FORMAT_U16_LE, VIRTIO_SND_PCM_FMT_U16 }, + { SNDRV_PCM_FORMAT_S18_3LE, VIRTIO_SND_PCM_FMT_S18_3 }, + { SNDRV_PCM_FORMAT_U18_3LE, VIRTIO_SND_PCM_FMT_U18_3 }, + { SNDRV_PCM_FORMAT_S20_3LE, VIRTIO_SND_PCM_FMT_S20_3 }, + { SNDRV_PCM_FORMAT_U20_3LE, VIRTIO_SND_PCM_FMT_U20_3 }, + { SNDRV_PCM_FORMAT_S24_3LE, VIRTIO_SND_PCM_FMT_S24_3 }, + { SNDRV_PCM_FORMAT_U24_3LE, VIRTIO_SND_PCM_FMT_U24_3 }, + { SNDRV_PCM_FORMAT_S20_LE, VIRTIO_SND_PCM_FMT_S20 }, + { SNDRV_PCM_FORMAT_U20_LE, VIRTIO_SND_PCM_FMT_U20 }, + { SNDRV_PCM_FORMAT_S24_LE, VIRTIO_SND_PCM_FMT_S24 }, + { SNDRV_PCM_FORMAT_U24_LE, VIRTIO_SND_PCM_FMT_U24 }, + { SNDRV_PCM_FORMAT_S32_LE, VIRTIO_SND_PCM_FMT_S32 }, + { SNDRV_PCM_FORMAT_U32_LE, VIRTIO_SND_PCM_FMT_U32 }, + { SNDRV_PCM_FORMAT_FLOAT_LE, VIRTIO_SND_PCM_FMT_FLOAT }, + { SNDRV_PCM_FORMAT_FLOAT64_LE, VIRTIO_SND_PCM_FMT_FLOAT64 }, + { SNDRV_PCM_FORMAT_DSD_U8, VIRTIO_SND_PCM_FMT_DSD_U8 }, + { SNDRV_PCM_FORMAT_DSD_U16_LE, VIRTIO_SND_PCM_FMT_DSD_U16 }, + { SNDRV_PCM_FORMAT_DSD_U32_LE, VIRTIO_SND_PCM_FMT_DSD_U32 }, + { SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE, + VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME } +}; + +/* Map for converting ALSA frame rate to VirtIO frame rate. */ +struct virtsnd_a2v_rate { + unsigned int rate; + unsigned int vio_bit; +}; + +static const struct virtsnd_a2v_rate g_a2v_rate_map[] = { + { 5512, VIRTIO_SND_PCM_RATE_5512 }, + { 8000, VIRTIO_SND_PCM_RATE_8000 }, + { 11025, VIRTIO_SND_PCM_RATE_11025 }, + { 16000, VIRTIO_SND_PCM_RATE_16000 }, + { 22050, VIRTIO_SND_PCM_RATE_22050 }, + { 32000, VIRTIO_SND_PCM_RATE_32000 }, + { 44100, VIRTIO_SND_PCM_RATE_44100 }, + { 48000, VIRTIO_SND_PCM_RATE_48000 }, + { 64000, VIRTIO_SND_PCM_RATE_64000 }, + { 88200, VIRTIO_SND_PCM_RATE_88200 }, + { 96000, VIRTIO_SND_PCM_RATE_96000 }, + { 176400, VIRTIO_SND_PCM_RATE_176400 }, + { 192000, VIRTIO_SND_PCM_RATE_192000 } +}; + +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream); + +/** + * virtsnd_pcm_open() - Open the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_open(struct snd_pcm_substream *substream) +{ + struct virtio_pcm *vpcm = snd_pcm_substream_chip(substream); + struct virtio_pcm_substream *vss = NULL; + + if (vpcm) { + switch (substream->stream) { + case SNDRV_PCM_STREAM_PLAYBACK: + case SNDRV_PCM_STREAM_CAPTURE: { + struct virtio_pcm_stream *vs = + &vpcm->streams[substream->stream]; + + if (substream->number < vs->nsubstreams) + vss = vs->substreams[substream->number]; + break; + } + } + } + + if (!vss) + return -EBADFD; + + substream->runtime->hw = vss->hw; + substream->private_data = vss; + + snd_pcm_hw_constraint_integer(substream->runtime, + SNDRV_PCM_HW_PARAM_PERIODS); + + /* + * If the substream has already been used, then the I/O queue may be in + * an invalid state. Just in case, we do a check and try to return the + * queue to its original state, if necessary. + */ + vss->msg_flushing = true; + + return virtsnd_pcm_sync_stop(substream); +} + +/** + * virtsnd_pcm_close() - Close the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0. + */ +static int virtsnd_pcm_close(struct snd_pcm_substream *substream) +{ + return 0; +} + +/** + * virtsnd_pcm_hw_params() - Set the parameters of the PCM substream. + * @substream: Kernel ALSA substream. + * @hw_params: Hardware parameters (can be NULL). + * + * The function can be called both from the upper level (in this case, + * @hw_params is not NULL) or from the driver itself (in this case, @hw_params + * is NULL, and the parameter values are taken from the runtime structure). + * + * Context: Process context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *hw_params) +{ + struct snd_pcm_runtime *runtime = substream->runtime; + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_device *vdev = vss->snd->vdev; + struct virtio_snd_msg *msg; + struct virtio_snd_pcm_set_params *request; + snd_pcm_format_t format; + unsigned int channels; + unsigned int rate; + unsigned int buffer_bytes; + unsigned int period_bytes; + unsigned int periods; + unsigned int i; + int vformat = -1; + int vrate = -1; + int rc; + + if (vss->msg_flushing) { + dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n", + vss->sid); + return -EBADFD; + } + + /* Set hardware parameters in device */ + if (hw_params) { + format = params_format(hw_params); + channels = params_channels(hw_params); + rate = params_rate(hw_params); + buffer_bytes = params_buffer_bytes(hw_params); + period_bytes = params_period_bytes(hw_params); + periods = params_periods(hw_params); + } else { + format = runtime->format; + channels = runtime->channels; + rate = runtime->rate; + buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size); + period_bytes = frames_to_bytes(runtime, runtime->period_size); + periods = runtime->periods; + } + + for (i = 0; i < ARRAY_SIZE(g_a2v_format_map); ++i) + if (g_a2v_format_map[i].alsa_bit == format) { + vformat = g_a2v_format_map[i].vio_bit; + + break; + } + + for (i = 0; i < ARRAY_SIZE(g_a2v_rate_map); ++i) + if (g_a2v_rate_map[i].rate == rate) { + vrate = g_a2v_rate_map[i].vio_bit; + + break; + } + + if (vformat == -1 || vrate == -1) + return -EINVAL; + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_SET_PARAMS, + GFP_KERNEL); + if (!msg) + return -ENOMEM; + + request = virtsnd_ctl_msg_request(msg); + request->buffer_bytes = cpu_to_virtio32(vdev, buffer_bytes); + request->period_bytes = cpu_to_virtio32(vdev, period_bytes); + request->channels = channels; + request->format = vformat; + request->rate = vrate; + + if (vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)) + request->features |= + cpu_to_virtio32(vdev, + 1U << VIRTIO_SND_PCM_F_MSG_POLLING); + + if (vss->features & (1U << VIRTIO_SND_PCM_F_EVT_XRUNS)) + request->features |= + cpu_to_virtio32(vdev, + 1U << VIRTIO_SND_PCM_F_EVT_XRUNS); + + rc = virtsnd_ctl_msg_send_sync(vss->snd, msg); + if (rc) + return rc; + + return virtsnd_pcm_msg_alloc(vss, periods, period_bytes); +} + +/** + * virtsnd_pcm_hw_free() - Reset the parameters of the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0 + */ +static int virtsnd_pcm_hw_free(struct snd_pcm_substream *substream) +{ + return 0; +} + +/** + * virtsnd_pcm_prepare() - Prepare the PCM substream. + * @substream: Kernel ALSA substream. + * + * The function can be called both from the upper level or from the driver + * itself. + * + * Context: Process context. Takes and releases the VirtIO substream spinlock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream) +{ + struct snd_pcm_runtime *runtime = substream->runtime; + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_device *vdev = vss->snd->vdev; + struct virtio_snd_msg *msg; + unsigned long flags; + + if (vss->msg_flushing) { + dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n", + vss->sid); + return -EBADFD; + } + + spin_lock_irqsave(&vss->lock, flags); + /* + * Since I/O messages are asynchronous, they can be completed + * when the runtime structure no longer exists. Since each + * completion implies incrementing the hw_ptr, we cache all the + * current values needed to compute the new hw_ptr value. + */ + vss->frame_bytes = runtime->frame_bits >> 3; + vss->period_size = runtime->period_size; + vss->buffer_size = runtime->buffer_size; + + vss->hw_ptr = 0; + vss->xfer_xrun = false; + vss->msg_last_enqueued = -1; + vss->msg_count = 0; + spin_unlock_irqrestore(&vss->lock, flags); + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_PREPARE, + GFP_KERNEL); + if (!msg) + return -ENOMEM; + + return virtsnd_ctl_msg_send_sync(vss->snd, msg); +} + +/** + * virtsnd_pcm_trigger() - Process command for the PCM substream. + * @substream: Kernel ALSA substream. + * @command: Substream command (SNDRV_PCM_TRIGGER_XXX). + * + * Context: Any context. Takes and releases the VirtIO substream spinlock. + * May take and release the tx/rx queue spinlock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) +{ + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_snd *snd = vss->snd; + struct virtio_snd_msg *msg; + unsigned long flags; + int rc; + + switch (command) { + case SNDRV_PCM_TRIGGER_START: + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: { + struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); + + spin_lock_irqsave(&queue->lock, flags); + spin_lock(&vss->lock); + rc = virtsnd_pcm_msg_send(vss); + if (!rc) + vss->xfer_enabled = true; + spin_unlock(&vss->lock); + spin_unlock_irqrestore(&queue->lock, flags); + if (rc) + return rc; + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START, + GFP_KERNEL); + if (!msg) { + spin_lock_irqsave(&vss->lock, flags); + vss->xfer_enabled = false; + spin_unlock_irqrestore(&vss->lock, flags); + + return -ENOMEM; + } + + return virtsnd_ctl_msg_send_sync(snd, msg); + } + case SNDRV_PCM_TRIGGER_STOP: + case SNDRV_PCM_TRIGGER_PAUSE_PUSH: { + spin_lock_irqsave(&vss->lock, flags); + vss->xfer_enabled = false; + spin_unlock_irqrestore(&vss->lock, flags); + + /* + * The I/O queue needs to be flushed only when the substream is + * completely stopped. + */ + if (command == SNDRV_PCM_TRIGGER_STOP) + vss->msg_flushing = true; + + /* + * The STOP command can be issued in an atomic context after + * the drain is complete. Therefore, in general, we cannot sleep + * here. + */ + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP, + GFP_ATOMIC); + if (!msg) + return -ENOMEM; + + return virtsnd_ctl_msg_send_async(snd, msg); + } + default: { + return -EINVAL; + } + } +} + +/** + * virtsnd_pcm_msg_count() - Returns the number of pending I/O messages. + * @vss: VirtIO substream. + * + * Context: Any context. + * Return: Number of messages. + */ +static inline +unsigned int virtsnd_pcm_msg_count(struct virtio_pcm_substream *vss) +{ + unsigned int msg_count; + unsigned long flags; + + spin_lock_irqsave(&vss->lock, flags); + msg_count = vss->msg_count; + spin_unlock_irqrestore(&vss->lock, flags); + + return msg_count; +} + +/** + * virtsnd_pcm_sync_stop() - Synchronous PCM substream stop. + * @substream: Kernel ALSA substream. + * + * The function can be called both from the upper level or from the driver + * itself. + * + * Context: Process context. Takes and releases the VirtIO substream spinlock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream) +{ + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_snd *snd = vss->snd; + struct virtio_snd_msg *msg; + unsigned int js = msecs_to_jiffies(msg_timeout_ms); + int rc; + + if (!vss->msg_flushing) + return 0; + + if (!virtsnd_pcm_msg_count(vss)) + goto on_exit; + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE, + GFP_KERNEL); + if (!msg) + return -ENOMEM; + + rc = virtsnd_ctl_msg_send_sync(snd, msg); + if (rc) + return rc; + + /* + * The spec states that upon receipt of the RELEASE command "the device + * MUST complete all pending I/O messages for the specified stream ID". + * Thus, we consider the absence of I/O messages in the queue as an + * indication that the substream has been released. + */ + rc = wait_event_interruptible_timeout(vss->msg_empty, + !virtsnd_pcm_msg_count(vss), + js); + if (rc <= 0) { + dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n", + vss->sid); + + return !rc ? -ETIMEDOUT : rc; + } + +on_exit: + vss->msg_flushing = false; + + return 0; +} + +/** + * virtsnd_pcm_pointer() - Get the current hardware position for the PCM + * substream. + * @substream: Kernel ALSA substream. + * + * Context: Any context. Takes and releases the VirtIO substream spinlock. + * Return: Hardware position in frames inside [0 ... buffer_size) range. + */ +static snd_pcm_uframes_t +virtsnd_pcm_pointer(struct snd_pcm_substream *substream) +{ + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + snd_pcm_uframes_t hw_ptr = SNDRV_PCM_POS_XRUN; + unsigned long flags; + + spin_lock_irqsave(&vss->lock, flags); + if (!vss->xfer_xrun) + hw_ptr = vss->hw_ptr; + spin_unlock_irqrestore(&vss->lock, flags); + + return hw_ptr; +} + +/* PCM substream operators map. */ +const struct snd_pcm_ops virtsnd_pcm_ops = { + .open = virtsnd_pcm_open, + .close = virtsnd_pcm_close, + .ioctl = snd_pcm_lib_ioctl, + .hw_params = virtsnd_pcm_hw_params, + .hw_free = virtsnd_pcm_hw_free, + .prepare = virtsnd_pcm_prepare, + .trigger = virtsnd_pcm_trigger, + .sync_stop = virtsnd_pcm_sync_stop, + .pointer = virtsnd_pcm_pointer, +}; -- 2.30.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1872C433DB for ; Tue, 9 Feb 2021 12:44:39 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0510064E60 for ; Tue, 9 Feb 2021 12:44:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0510064E60 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=opensynergy.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 597041692; Tue, 9 Feb 2021 13:43:47 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 597041692 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1612874677; bh=x5ERao30ZkyE0+jYWx5JX3kxlQpFj752oCGgGvGJxmU=; h=From:To:Subject:Date:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=VliCCLeAPJUaY4+vvnn3/QdUXYvWCFEt6D/Yz2T9Pi5k97f4LaiweZ4CabRDvgvBy XtQZYkGcqXQcu8EkIPTBrPU3CeoxluYxMhTuhtQCoWCZvPo0ID5DISJdaK0b4Mouqn qh3hs1xdW8O9NRRX+n797+fhGJxondlGFieU9nW8= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 36F86F802E7; Tue, 9 Feb 2021 13:41:00 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 56C33F804B1; Tue, 9 Feb 2021 13:40:59 +0100 (CET) Received: from mx1.opensynergy.com (mx1.opensynergy.com [217.66.60.4]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id A7797F802E7 for ; Tue, 9 Feb 2021 13:40:55 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz A7797F802E7 Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key) header.d=opensynergy.com header.i=@opensynergy.com header.b="C3caZpEz" Received: from SR-MAILGATE-02.opensynergy.com (localhost.localdomain [127.0.0.1]) by mx1.opensynergy.com (Proxmox) with ESMTP id 465BCA1616; Tue, 9 Feb 2021 13:40:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=opensynergy.com; h=cc:cc:content-transfer-encoding:content-type:content-type :date:from:from:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=srmailgate02; bh=5sETgeQX5b+B M42aQEpHT8LAGsjGgG2fzKxC5x0epRs=; b=C3caZpEzdtrkF9b9hlccVOIBPiF7 HpviZaaD1B3QdlxB91+tyFTPDbDoLyrOyXoKw4cUkpLqAZOMiTqMLRWE1EDgrJfs E2D5TScAIh66kTwQkbOjKOvFxLlPCb3nGQRpptvrMVy3eXbPkb6ndq4cfKWKKRN/ WQj5rEhNYoRoVdWpTQw9OJ2nlKQJwu+ctmpIkRBCntJoY1fvrI3Q6+mz57t9MRMo s+QEktyav4FYFrJk+JHA+qTC4qhx4bHuXBe0NTw6bj979+nWiY6TIbV0dG2cVo7l PMSnwMqts+F7ezO7XzFAbGfY3znThAmHtLWuudyujCKyLIL5Di9xOxnlJw== From: Anton Yakovlev To: , , Subject: [PATCH v3 6/9] ALSA: virtio: PCM substream operators Date: Tue, 9 Feb 2021 13:40:07 +0100 Message-ID: <20210209124011.1224628-7-anton.yakovlev@opensynergy.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210209124011.1224628-1-anton.yakovlev@opensynergy.com> References: <20210209124011.1224628-1-anton.yakovlev@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SR-MAIL-02.open-synergy.com (10.26.10.22) To SR-MAIL-01.open-synergy.com (10.26.10.21) Cc: linux-kernel@vger.kernel.org, Takashi Iwai , "Michael S. Tsirkin" X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" Introduce the operators required for the operation of substreams. Signed-off-by: Anton Yakovlev --- sound/virtio/Makefile | 3 +- sound/virtio/virtio_pcm.c | 2 + sound/virtio/virtio_pcm.h | 4 + sound/virtio/virtio_pcm_ops.c | 471 ++++++++++++++++++++++++++++++++++ 4 files changed, 479 insertions(+), 1 deletion(-) create mode 100644 sound/virtio/virtio_pcm_ops.c diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile index 626af3cc3ed7..34493226793f 100644 --- a/sound/virtio/Makefile +++ b/sound/virtio/Makefile @@ -6,5 +6,6 @@ virtio_snd-objs := \ virtio_card.o \ virtio_ctl_msg.o \ virtio_pcm.o \ - virtio_pcm_msg.o + virtio_pcm_msg.o \ + virtio_pcm_ops.o diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index a74fbfb9f35c..1d98de878385 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -455,6 +455,8 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd) for (kss = ks->substream; kss; kss = kss->next) vs->substreams[kss->number]->substream = kss; + + snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops); } snd_pcm_set_managed_buffer_all(vpcm->pcm, diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index d6be62b52421..4378918b441a 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -35,6 +35,7 @@ struct virtio_pcm_msg; * @msg_last_enqueued: Index of the last I/O message added to the virtqueue. * @msg_count: Number of pending I/O messages in the virtqueue. * @msg_empty: Notify when msg_count is zero. + * @msg_flushing: True if the I/O queue is in flushing state. */ struct virtio_pcm_substream { struct virtio_snd *snd; @@ -56,6 +57,7 @@ struct virtio_pcm_substream { int msg_last_enqueued; unsigned int msg_count; wait_queue_head_t msg_empty; + bool msg_flushing; }; /** @@ -82,6 +84,8 @@ struct virtio_pcm { struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1]; }; +extern const struct snd_pcm_ops virtsnd_pcm_ops; + int virtsnd_pcm_validate(struct virtio_device *vdev); int virtsnd_pcm_parse_cfg(struct virtio_snd *snd); diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c new file mode 100644 index 000000000000..c2224f5461c4 --- /dev/null +++ b/sound/virtio/virtio_pcm_ops.c @@ -0,0 +1,471 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * virtio-snd: Virtio sound device + * Copyright (C) 2021 OpenSynergy GmbH + */ +#include + +#include "virtio_card.h" + +/* Map for converting ALSA format to VirtIO format. */ +struct virtsnd_a2v_format { + unsigned int alsa_bit; + unsigned int vio_bit; +}; + +static const struct virtsnd_a2v_format g_a2v_format_map[] = { + { SNDRV_PCM_FORMAT_IMA_ADPCM, VIRTIO_SND_PCM_FMT_IMA_ADPCM }, + { SNDRV_PCM_FORMAT_MU_LAW, VIRTIO_SND_PCM_FMT_MU_LAW }, + { SNDRV_PCM_FORMAT_A_LAW, VIRTIO_SND_PCM_FMT_A_LAW }, + { SNDRV_PCM_FORMAT_S8, VIRTIO_SND_PCM_FMT_S8 }, + { SNDRV_PCM_FORMAT_U8, VIRTIO_SND_PCM_FMT_U8 }, + { SNDRV_PCM_FORMAT_S16_LE, VIRTIO_SND_PCM_FMT_S16 }, + { SNDRV_PCM_FORMAT_U16_LE, VIRTIO_SND_PCM_FMT_U16 }, + { SNDRV_PCM_FORMAT_S18_3LE, VIRTIO_SND_PCM_FMT_S18_3 }, + { SNDRV_PCM_FORMAT_U18_3LE, VIRTIO_SND_PCM_FMT_U18_3 }, + { SNDRV_PCM_FORMAT_S20_3LE, VIRTIO_SND_PCM_FMT_S20_3 }, + { SNDRV_PCM_FORMAT_U20_3LE, VIRTIO_SND_PCM_FMT_U20_3 }, + { SNDRV_PCM_FORMAT_S24_3LE, VIRTIO_SND_PCM_FMT_S24_3 }, + { SNDRV_PCM_FORMAT_U24_3LE, VIRTIO_SND_PCM_FMT_U24_3 }, + { SNDRV_PCM_FORMAT_S20_LE, VIRTIO_SND_PCM_FMT_S20 }, + { SNDRV_PCM_FORMAT_U20_LE, VIRTIO_SND_PCM_FMT_U20 }, + { SNDRV_PCM_FORMAT_S24_LE, VIRTIO_SND_PCM_FMT_S24 }, + { SNDRV_PCM_FORMAT_U24_LE, VIRTIO_SND_PCM_FMT_U24 }, + { SNDRV_PCM_FORMAT_S32_LE, VIRTIO_SND_PCM_FMT_S32 }, + { SNDRV_PCM_FORMAT_U32_LE, VIRTIO_SND_PCM_FMT_U32 }, + { SNDRV_PCM_FORMAT_FLOAT_LE, VIRTIO_SND_PCM_FMT_FLOAT }, + { SNDRV_PCM_FORMAT_FLOAT64_LE, VIRTIO_SND_PCM_FMT_FLOAT64 }, + { SNDRV_PCM_FORMAT_DSD_U8, VIRTIO_SND_PCM_FMT_DSD_U8 }, + { SNDRV_PCM_FORMAT_DSD_U16_LE, VIRTIO_SND_PCM_FMT_DSD_U16 }, + { SNDRV_PCM_FORMAT_DSD_U32_LE, VIRTIO_SND_PCM_FMT_DSD_U32 }, + { SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE, + VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME } +}; + +/* Map for converting ALSA frame rate to VirtIO frame rate. */ +struct virtsnd_a2v_rate { + unsigned int rate; + unsigned int vio_bit; +}; + +static const struct virtsnd_a2v_rate g_a2v_rate_map[] = { + { 5512, VIRTIO_SND_PCM_RATE_5512 }, + { 8000, VIRTIO_SND_PCM_RATE_8000 }, + { 11025, VIRTIO_SND_PCM_RATE_11025 }, + { 16000, VIRTIO_SND_PCM_RATE_16000 }, + { 22050, VIRTIO_SND_PCM_RATE_22050 }, + { 32000, VIRTIO_SND_PCM_RATE_32000 }, + { 44100, VIRTIO_SND_PCM_RATE_44100 }, + { 48000, VIRTIO_SND_PCM_RATE_48000 }, + { 64000, VIRTIO_SND_PCM_RATE_64000 }, + { 88200, VIRTIO_SND_PCM_RATE_88200 }, + { 96000, VIRTIO_SND_PCM_RATE_96000 }, + { 176400, VIRTIO_SND_PCM_RATE_176400 }, + { 192000, VIRTIO_SND_PCM_RATE_192000 } +}; + +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream); + +/** + * virtsnd_pcm_open() - Open the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_open(struct snd_pcm_substream *substream) +{ + struct virtio_pcm *vpcm = snd_pcm_substream_chip(substream); + struct virtio_pcm_substream *vss = NULL; + + if (vpcm) { + switch (substream->stream) { + case SNDRV_PCM_STREAM_PLAYBACK: + case SNDRV_PCM_STREAM_CAPTURE: { + struct virtio_pcm_stream *vs = + &vpcm->streams[substream->stream]; + + if (substream->number < vs->nsubstreams) + vss = vs->substreams[substream->number]; + break; + } + } + } + + if (!vss) + return -EBADFD; + + substream->runtime->hw = vss->hw; + substream->private_data = vss; + + snd_pcm_hw_constraint_integer(substream->runtime, + SNDRV_PCM_HW_PARAM_PERIODS); + + /* + * If the substream has already been used, then the I/O queue may be in + * an invalid state. Just in case, we do a check and try to return the + * queue to its original state, if necessary. + */ + vss->msg_flushing = true; + + return virtsnd_pcm_sync_stop(substream); +} + +/** + * virtsnd_pcm_close() - Close the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0. + */ +static int virtsnd_pcm_close(struct snd_pcm_substream *substream) +{ + return 0; +} + +/** + * virtsnd_pcm_hw_params() - Set the parameters of the PCM substream. + * @substream: Kernel ALSA substream. + * @hw_params: Hardware parameters (can be NULL). + * + * The function can be called both from the upper level (in this case, + * @hw_params is not NULL) or from the driver itself (in this case, @hw_params + * is NULL, and the parameter values are taken from the runtime structure). + * + * Context: Process context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *hw_params) +{ + struct snd_pcm_runtime *runtime = substream->runtime; + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_device *vdev = vss->snd->vdev; + struct virtio_snd_msg *msg; + struct virtio_snd_pcm_set_params *request; + snd_pcm_format_t format; + unsigned int channels; + unsigned int rate; + unsigned int buffer_bytes; + unsigned int period_bytes; + unsigned int periods; + unsigned int i; + int vformat = -1; + int vrate = -1; + int rc; + + if (vss->msg_flushing) { + dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n", + vss->sid); + return -EBADFD; + } + + /* Set hardware parameters in device */ + if (hw_params) { + format = params_format(hw_params); + channels = params_channels(hw_params); + rate = params_rate(hw_params); + buffer_bytes = params_buffer_bytes(hw_params); + period_bytes = params_period_bytes(hw_params); + periods = params_periods(hw_params); + } else { + format = runtime->format; + channels = runtime->channels; + rate = runtime->rate; + buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size); + period_bytes = frames_to_bytes(runtime, runtime->period_size); + periods = runtime->periods; + } + + for (i = 0; i < ARRAY_SIZE(g_a2v_format_map); ++i) + if (g_a2v_format_map[i].alsa_bit == format) { + vformat = g_a2v_format_map[i].vio_bit; + + break; + } + + for (i = 0; i < ARRAY_SIZE(g_a2v_rate_map); ++i) + if (g_a2v_rate_map[i].rate == rate) { + vrate = g_a2v_rate_map[i].vio_bit; + + break; + } + + if (vformat == -1 || vrate == -1) + return -EINVAL; + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_SET_PARAMS, + GFP_KERNEL); + if (!msg) + return -ENOMEM; + + request = virtsnd_ctl_msg_request(msg); + request->buffer_bytes = cpu_to_virtio32(vdev, buffer_bytes); + request->period_bytes = cpu_to_virtio32(vdev, period_bytes); + request->channels = channels; + request->format = vformat; + request->rate = vrate; + + if (vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)) + request->features |= + cpu_to_virtio32(vdev, + 1U << VIRTIO_SND_PCM_F_MSG_POLLING); + + if (vss->features & (1U << VIRTIO_SND_PCM_F_EVT_XRUNS)) + request->features |= + cpu_to_virtio32(vdev, + 1U << VIRTIO_SND_PCM_F_EVT_XRUNS); + + rc = virtsnd_ctl_msg_send_sync(vss->snd, msg); + if (rc) + return rc; + + return virtsnd_pcm_msg_alloc(vss, periods, period_bytes); +} + +/** + * virtsnd_pcm_hw_free() - Reset the parameters of the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0 + */ +static int virtsnd_pcm_hw_free(struct snd_pcm_substream *substream) +{ + return 0; +} + +/** + * virtsnd_pcm_prepare() - Prepare the PCM substream. + * @substream: Kernel ALSA substream. + * + * The function can be called both from the upper level or from the driver + * itself. + * + * Context: Process context. Takes and releases the VirtIO substream spinlock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream) +{ + struct snd_pcm_runtime *runtime = substream->runtime; + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_device *vdev = vss->snd->vdev; + struct virtio_snd_msg *msg; + unsigned long flags; + + if (vss->msg_flushing) { + dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n", + vss->sid); + return -EBADFD; + } + + spin_lock_irqsave(&vss->lock, flags); + /* + * Since I/O messages are asynchronous, they can be completed + * when the runtime structure no longer exists. Since each + * completion implies incrementing the hw_ptr, we cache all the + * current values needed to compute the new hw_ptr value. + */ + vss->frame_bytes = runtime->frame_bits >> 3; + vss->period_size = runtime->period_size; + vss->buffer_size = runtime->buffer_size; + + vss->hw_ptr = 0; + vss->xfer_xrun = false; + vss->msg_last_enqueued = -1; + vss->msg_count = 0; + spin_unlock_irqrestore(&vss->lock, flags); + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_PREPARE, + GFP_KERNEL); + if (!msg) + return -ENOMEM; + + return virtsnd_ctl_msg_send_sync(vss->snd, msg); +} + +/** + * virtsnd_pcm_trigger() - Process command for the PCM substream. + * @substream: Kernel ALSA substream. + * @command: Substream command (SNDRV_PCM_TRIGGER_XXX). + * + * Context: Any context. Takes and releases the VirtIO substream spinlock. + * May take and release the tx/rx queue spinlock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) +{ + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_snd *snd = vss->snd; + struct virtio_snd_msg *msg; + unsigned long flags; + int rc; + + switch (command) { + case SNDRV_PCM_TRIGGER_START: + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: { + struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); + + spin_lock_irqsave(&queue->lock, flags); + spin_lock(&vss->lock); + rc = virtsnd_pcm_msg_send(vss); + if (!rc) + vss->xfer_enabled = true; + spin_unlock(&vss->lock); + spin_unlock_irqrestore(&queue->lock, flags); + if (rc) + return rc; + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START, + GFP_KERNEL); + if (!msg) { + spin_lock_irqsave(&vss->lock, flags); + vss->xfer_enabled = false; + spin_unlock_irqrestore(&vss->lock, flags); + + return -ENOMEM; + } + + return virtsnd_ctl_msg_send_sync(snd, msg); + } + case SNDRV_PCM_TRIGGER_STOP: + case SNDRV_PCM_TRIGGER_PAUSE_PUSH: { + spin_lock_irqsave(&vss->lock, flags); + vss->xfer_enabled = false; + spin_unlock_irqrestore(&vss->lock, flags); + + /* + * The I/O queue needs to be flushed only when the substream is + * completely stopped. + */ + if (command == SNDRV_PCM_TRIGGER_STOP) + vss->msg_flushing = true; + + /* + * The STOP command can be issued in an atomic context after + * the drain is complete. Therefore, in general, we cannot sleep + * here. + */ + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP, + GFP_ATOMIC); + if (!msg) + return -ENOMEM; + + return virtsnd_ctl_msg_send_async(snd, msg); + } + default: { + return -EINVAL; + } + } +} + +/** + * virtsnd_pcm_msg_count() - Returns the number of pending I/O messages. + * @vss: VirtIO substream. + * + * Context: Any context. + * Return: Number of messages. + */ +static inline +unsigned int virtsnd_pcm_msg_count(struct virtio_pcm_substream *vss) +{ + unsigned int msg_count; + unsigned long flags; + + spin_lock_irqsave(&vss->lock, flags); + msg_count = vss->msg_count; + spin_unlock_irqrestore(&vss->lock, flags); + + return msg_count; +} + +/** + * virtsnd_pcm_sync_stop() - Synchronous PCM substream stop. + * @substream: Kernel ALSA substream. + * + * The function can be called both from the upper level or from the driver + * itself. + * + * Context: Process context. Takes and releases the VirtIO substream spinlock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream) +{ + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + struct virtio_snd *snd = vss->snd; + struct virtio_snd_msg *msg; + unsigned int js = msecs_to_jiffies(msg_timeout_ms); + int rc; + + if (!vss->msg_flushing) + return 0; + + if (!virtsnd_pcm_msg_count(vss)) + goto on_exit; + + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE, + GFP_KERNEL); + if (!msg) + return -ENOMEM; + + rc = virtsnd_ctl_msg_send_sync(snd, msg); + if (rc) + return rc; + + /* + * The spec states that upon receipt of the RELEASE command "the device + * MUST complete all pending I/O messages for the specified stream ID". + * Thus, we consider the absence of I/O messages in the queue as an + * indication that the substream has been released. + */ + rc = wait_event_interruptible_timeout(vss->msg_empty, + !virtsnd_pcm_msg_count(vss), + js); + if (rc <= 0) { + dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n", + vss->sid); + + return !rc ? -ETIMEDOUT : rc; + } + +on_exit: + vss->msg_flushing = false; + + return 0; +} + +/** + * virtsnd_pcm_pointer() - Get the current hardware position for the PCM + * substream. + * @substream: Kernel ALSA substream. + * + * Context: Any context. Takes and releases the VirtIO substream spinlock. + * Return: Hardware position in frames inside [0 ... buffer_size) range. + */ +static snd_pcm_uframes_t +virtsnd_pcm_pointer(struct snd_pcm_substream *substream) +{ + struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); + snd_pcm_uframes_t hw_ptr = SNDRV_PCM_POS_XRUN; + unsigned long flags; + + spin_lock_irqsave(&vss->lock, flags); + if (!vss->xfer_xrun) + hw_ptr = vss->hw_ptr; + spin_unlock_irqrestore(&vss->lock, flags); + + return hw_ptr; +} + +/* PCM substream operators map. */ +const struct snd_pcm_ops virtsnd_pcm_ops = { + .open = virtsnd_pcm_open, + .close = virtsnd_pcm_close, + .ioctl = snd_pcm_lib_ioctl, + .hw_params = virtsnd_pcm_hw_params, + .hw_free = virtsnd_pcm_hw_free, + .prepare = virtsnd_pcm_prepare, + .trigger = virtsnd_pcm_trigger, + .sync_stop = virtsnd_pcm_sync_stop, + .pointer = virtsnd_pcm_pointer, +}; -- 2.30.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: virtio-dev-return-7989-cohuck=redhat.com@lists.oasis-open.org Sender: List-Post: List-Help: List-Unsubscribe: List-Subscribe: Received: from lists.oasis-open.org (oasis-open.org [10.110.1.242]) by lists.oasis-open.org (Postfix) with ESMTP id 3C7189864D8 for ; Tue, 9 Feb 2021 12:41:04 +0000 (UTC) From: Anton Yakovlev Date: Tue, 9 Feb 2021 13:40:07 +0100 Message-ID: <20210209124011.1224628-7-anton.yakovlev@opensynergy.com> In-Reply-To: <20210209124011.1224628-1-anton.yakovlev@opensynergy.com> References: <20210209124011.1224628-1-anton.yakovlev@opensynergy.com> MIME-Version: 1.0 Subject: [virtio-dev] [PATCH v3 6/9] ALSA: virtio: PCM substream operators Content-Type: text/plain Content-Transfer-Encoding: quoted-printable To: virtualization@lists.linux-foundation.org, alsa-devel@alsa-project.org, virtio-dev@lists.oasis-open.org Cc: "Michael S. Tsirkin" , Jaroslav Kysela , Takashi Iwai , linux-kernel@vger.kernel.org List-ID: Introduce the operators required for the operation of substreams. Signed-off-by: Anton Yakovlev --- sound/virtio/Makefile | 3 +- sound/virtio/virtio_pcm.c | 2 + sound/virtio/virtio_pcm.h | 4 + sound/virtio/virtio_pcm_ops.c | 471 ++++++++++++++++++++++++++++++++++ 4 files changed, 479 insertions(+), 1 deletion(-) create mode 100644 sound/virtio/virtio_pcm_ops.c diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile index 626af3cc3ed7..34493226793f 100644 --- a/sound/virtio/Makefile +++ b/sound/virtio/Makefile @@ -6,5 +6,6 @@ virtio_snd-objs :=3D \ =09virtio_card.o \ =09virtio_ctl_msg.o \ =09virtio_pcm.o \ -=09virtio_pcm_msg.o +=09virtio_pcm_msg.o \ +=09virtio_pcm_ops.o =20 diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index a74fbfb9f35c..1d98de878385 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -455,6 +455,8 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd) =20 =09=09=09for (kss =3D ks->substream; kss; kss =3D kss->next) =09=09=09=09vs->substreams[kss->number]->substream =3D kss; + +=09=09=09snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops); =09=09} =20 =09=09snd_pcm_set_managed_buffer_all(vpcm->pcm, diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index d6be62b52421..4378918b441a 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -35,6 +35,7 @@ struct virtio_pcm_msg; * @msg_last_enqueued: Index of the last I/O message added to the virtqueu= e. * @msg_count: Number of pending I/O messages in the virtqueue. * @msg_empty: Notify when msg_count is zero. + * @msg_flushing: True if the I/O queue is in flushing state. */ struct virtio_pcm_substream { =09struct virtio_snd *snd; @@ -56,6 +57,7 @@ struct virtio_pcm_substream { =09int msg_last_enqueued; =09unsigned int msg_count; =09wait_queue_head_t msg_empty; +=09bool msg_flushing; }; =20 /** @@ -82,6 +84,8 @@ struct virtio_pcm { =09struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1]; }; =20 +extern const struct snd_pcm_ops virtsnd_pcm_ops; + int virtsnd_pcm_validate(struct virtio_device *vdev); =20 int virtsnd_pcm_parse_cfg(struct virtio_snd *snd); diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c new file mode 100644 index 000000000000..c2224f5461c4 --- /dev/null +++ b/sound/virtio/virtio_pcm_ops.c @@ -0,0 +1,471 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * virtio-snd: Virtio sound device + * Copyright (C) 2021 OpenSynergy GmbH + */ +#include + +#include "virtio_card.h" + +/* Map for converting ALSA format to VirtIO format. */ +struct virtsnd_a2v_format { +=09unsigned int alsa_bit; +=09unsigned int vio_bit; +}; + +static const struct virtsnd_a2v_format g_a2v_format_map[] =3D { +=09{ SNDRV_PCM_FORMAT_IMA_ADPCM, VIRTIO_SND_PCM_FMT_IMA_ADPCM }, +=09{ SNDRV_PCM_FORMAT_MU_LAW, VIRTIO_SND_PCM_FMT_MU_LAW }, +=09{ SNDRV_PCM_FORMAT_A_LAW, VIRTIO_SND_PCM_FMT_A_LAW }, +=09{ SNDRV_PCM_FORMAT_S8, VIRTIO_SND_PCM_FMT_S8 }, +=09{ SNDRV_PCM_FORMAT_U8, VIRTIO_SND_PCM_FMT_U8 }, +=09{ SNDRV_PCM_FORMAT_S16_LE, VIRTIO_SND_PCM_FMT_S16 }, +=09{ SNDRV_PCM_FORMAT_U16_LE, VIRTIO_SND_PCM_FMT_U16 }, +=09{ SNDRV_PCM_FORMAT_S18_3LE, VIRTIO_SND_PCM_FMT_S18_3 }, +=09{ SNDRV_PCM_FORMAT_U18_3LE, VIRTIO_SND_PCM_FMT_U18_3 }, +=09{ SNDRV_PCM_FORMAT_S20_3LE, VIRTIO_SND_PCM_FMT_S20_3 }, +=09{ SNDRV_PCM_FORMAT_U20_3LE, VIRTIO_SND_PCM_FMT_U20_3 }, +=09{ SNDRV_PCM_FORMAT_S24_3LE, VIRTIO_SND_PCM_FMT_S24_3 }, +=09{ SNDRV_PCM_FORMAT_U24_3LE, VIRTIO_SND_PCM_FMT_U24_3 }, +=09{ SNDRV_PCM_FORMAT_S20_LE, VIRTIO_SND_PCM_FMT_S20 }, +=09{ SNDRV_PCM_FORMAT_U20_LE, VIRTIO_SND_PCM_FMT_U20 }, +=09{ SNDRV_PCM_FORMAT_S24_LE, VIRTIO_SND_PCM_FMT_S24 }, +=09{ SNDRV_PCM_FORMAT_U24_LE, VIRTIO_SND_PCM_FMT_U24 }, +=09{ SNDRV_PCM_FORMAT_S32_LE, VIRTIO_SND_PCM_FMT_S32 }, +=09{ SNDRV_PCM_FORMAT_U32_LE, VIRTIO_SND_PCM_FMT_U32 }, +=09{ SNDRV_PCM_FORMAT_FLOAT_LE, VIRTIO_SND_PCM_FMT_FLOAT }, +=09{ SNDRV_PCM_FORMAT_FLOAT64_LE, VIRTIO_SND_PCM_FMT_FLOAT64 }, +=09{ SNDRV_PCM_FORMAT_DSD_U8, VIRTIO_SND_PCM_FMT_DSD_U8 }, +=09{ SNDRV_PCM_FORMAT_DSD_U16_LE, VIRTIO_SND_PCM_FMT_DSD_U16 }, +=09{ SNDRV_PCM_FORMAT_DSD_U32_LE, VIRTIO_SND_PCM_FMT_DSD_U32 }, +=09{ SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE, +=09 VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME } +}; + +/* Map for converting ALSA frame rate to VirtIO frame rate. */ +struct virtsnd_a2v_rate { +=09unsigned int rate; +=09unsigned int vio_bit; +}; + +static const struct virtsnd_a2v_rate g_a2v_rate_map[] =3D { +=09{ 5512, VIRTIO_SND_PCM_RATE_5512 }, +=09{ 8000, VIRTIO_SND_PCM_RATE_8000 }, +=09{ 11025, VIRTIO_SND_PCM_RATE_11025 }, +=09{ 16000, VIRTIO_SND_PCM_RATE_16000 }, +=09{ 22050, VIRTIO_SND_PCM_RATE_22050 }, +=09{ 32000, VIRTIO_SND_PCM_RATE_32000 }, +=09{ 44100, VIRTIO_SND_PCM_RATE_44100 }, +=09{ 48000, VIRTIO_SND_PCM_RATE_48000 }, +=09{ 64000, VIRTIO_SND_PCM_RATE_64000 }, +=09{ 88200, VIRTIO_SND_PCM_RATE_88200 }, +=09{ 96000, VIRTIO_SND_PCM_RATE_96000 }, +=09{ 176400, VIRTIO_SND_PCM_RATE_176400 }, +=09{ 192000, VIRTIO_SND_PCM_RATE_192000 } +}; + +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream); + +/** + * virtsnd_pcm_open() - Open the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_open(struct snd_pcm_substream *substream) +{ +=09struct virtio_pcm *vpcm =3D snd_pcm_substream_chip(substream); +=09struct virtio_pcm_substream *vss =3D NULL; + +=09if (vpcm) { +=09=09switch (substream->stream) { +=09=09case SNDRV_PCM_STREAM_PLAYBACK: +=09=09case SNDRV_PCM_STREAM_CAPTURE: { +=09=09=09struct virtio_pcm_stream *vs =3D +=09=09=09=09&vpcm->streams[substream->stream]; + +=09=09=09if (substream->number < vs->nsubstreams) +=09=09=09=09vss =3D vs->substreams[substream->number]; +=09=09=09break; +=09=09} +=09=09} +=09} + +=09if (!vss) +=09=09return -EBADFD; + +=09substream->runtime->hw =3D vss->hw; +=09substream->private_data =3D vss; + +=09snd_pcm_hw_constraint_integer(substream->runtime, +=09=09=09=09 SNDRV_PCM_HW_PARAM_PERIODS); + +=09/* +=09 * If the substream has already been used, then the I/O queue may be in +=09 * an invalid state. Just in case, we do a check and try to return the +=09 * queue to its original state, if necessary. +=09 */ +=09vss->msg_flushing =3D true; + +=09return virtsnd_pcm_sync_stop(substream); +} + +/** + * virtsnd_pcm_close() - Close the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0. + */ +static int virtsnd_pcm_close(struct snd_pcm_substream *substream) +{ +=09return 0; +} + +/** + * virtsnd_pcm_hw_params() - Set the parameters of the PCM substream. + * @substream: Kernel ALSA substream. + * @hw_params: Hardware parameters (can be NULL). + * + * The function can be called both from the upper level (in this case, + * @hw_params is not NULL) or from the driver itself (in this case, @hw_pa= rams + * is NULL, and the parameter values are taken from the runtime structure)= . + * + * Context: Process context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_hw_params(struct snd_pcm_substream *substream, +=09=09=09=09 struct snd_pcm_hw_params *hw_params) +{ +=09struct snd_pcm_runtime *runtime =3D substream->runtime; +=09struct virtio_pcm_substream *vss =3D snd_pcm_substream_chip(substream); +=09struct virtio_device *vdev =3D vss->snd->vdev; +=09struct virtio_snd_msg *msg; +=09struct virtio_snd_pcm_set_params *request; +=09snd_pcm_format_t format; +=09unsigned int channels; +=09unsigned int rate; +=09unsigned int buffer_bytes; +=09unsigned int period_bytes; +=09unsigned int periods; +=09unsigned int i; +=09int vformat =3D -1; +=09int vrate =3D -1; +=09int rc; + +=09if (vss->msg_flushing) { +=09=09dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n", +=09=09=09vss->sid); +=09=09return -EBADFD; +=09} + +=09/* Set hardware parameters in device */ +=09if (hw_params) { +=09=09format =3D params_format(hw_params); +=09=09channels =3D params_channels(hw_params); +=09=09rate =3D params_rate(hw_params); +=09=09buffer_bytes =3D params_buffer_bytes(hw_params); +=09=09period_bytes =3D params_period_bytes(hw_params); +=09=09periods =3D params_periods(hw_params); +=09} else { +=09=09format =3D runtime->format; +=09=09channels =3D runtime->channels; +=09=09rate =3D runtime->rate; +=09=09buffer_bytes =3D frames_to_bytes(runtime, runtime->buffer_size); +=09=09period_bytes =3D frames_to_bytes(runtime, runtime->period_size); +=09=09periods =3D runtime->periods; +=09} + +=09for (i =3D 0; i < ARRAY_SIZE(g_a2v_format_map); ++i) +=09=09if (g_a2v_format_map[i].alsa_bit =3D=3D format) { +=09=09=09vformat =3D g_a2v_format_map[i].vio_bit; + +=09=09=09break; +=09=09} + +=09for (i =3D 0; i < ARRAY_SIZE(g_a2v_rate_map); ++i) +=09=09if (g_a2v_rate_map[i].rate =3D=3D rate) { +=09=09=09vrate =3D g_a2v_rate_map[i].vio_bit; + +=09=09=09break; +=09=09} + +=09if (vformat =3D=3D -1 || vrate =3D=3D -1) +=09=09return -EINVAL; + +=09msg =3D virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_SET_PARAMS, +=09=09=09=09=09GFP_KERNEL); +=09if (!msg) +=09=09return -ENOMEM; + +=09request =3D virtsnd_ctl_msg_request(msg); +=09request->buffer_bytes =3D cpu_to_virtio32(vdev, buffer_bytes); +=09request->period_bytes =3D cpu_to_virtio32(vdev, period_bytes); +=09request->channels =3D channels; +=09request->format =3D vformat; +=09request->rate =3D vrate; + +=09if (vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)) +=09=09request->features |=3D +=09=09=09cpu_to_virtio32(vdev, +=09=09=09=09=091U << VIRTIO_SND_PCM_F_MSG_POLLING); + +=09if (vss->features & (1U << VIRTIO_SND_PCM_F_EVT_XRUNS)) +=09=09request->features |=3D +=09=09=09cpu_to_virtio32(vdev, +=09=09=09=09=091U << VIRTIO_SND_PCM_F_EVT_XRUNS); + +=09rc =3D virtsnd_ctl_msg_send_sync(vss->snd, msg); +=09if (rc) +=09=09return rc; + +=09return virtsnd_pcm_msg_alloc(vss, periods, period_bytes); +} + +/** + * virtsnd_pcm_hw_free() - Reset the parameters of the PCM substream. + * @substream: Kernel ALSA substream. + * + * Context: Process context. + * Return: 0 + */ +static int virtsnd_pcm_hw_free(struct snd_pcm_substream *substream) +{ +=09return 0; +} + +/** + * virtsnd_pcm_prepare() - Prepare the PCM substream. + * @substream: Kernel ALSA substream. + * + * The function can be called both from the upper level or from the driver + * itself. + * + * Context: Process context. Takes and releases the VirtIO substream spinl= ock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream) +{ +=09struct snd_pcm_runtime *runtime =3D substream->runtime; +=09struct virtio_pcm_substream *vss =3D snd_pcm_substream_chip(substream); +=09struct virtio_device *vdev =3D vss->snd->vdev; +=09struct virtio_snd_msg *msg; +=09unsigned long flags; + +=09if (vss->msg_flushing) { +=09=09dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n", +=09=09=09vss->sid); +=09=09return -EBADFD; +=09} + +=09spin_lock_irqsave(&vss->lock, flags); +=09/* +=09 * Since I/O messages are asynchronous, they can be completed +=09 * when the runtime structure no longer exists. Since each +=09 * completion implies incrementing the hw_ptr, we cache all the +=09 * current values needed to compute the new hw_ptr value. +=09 */ +=09vss->frame_bytes =3D runtime->frame_bits >> 3; +=09vss->period_size =3D runtime->period_size; +=09vss->buffer_size =3D runtime->buffer_size; + +=09vss->hw_ptr =3D 0; +=09vss->xfer_xrun =3D false; +=09vss->msg_last_enqueued =3D -1; +=09vss->msg_count =3D 0; +=09spin_unlock_irqrestore(&vss->lock, flags); + +=09msg =3D virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_PREPARE, +=09=09=09=09=09GFP_KERNEL); +=09if (!msg) +=09=09return -ENOMEM; + +=09return virtsnd_ctl_msg_send_sync(vss->snd, msg); +} + +/** + * virtsnd_pcm_trigger() - Process command for the PCM substream. + * @substream: Kernel ALSA substream. + * @command: Substream command (SNDRV_PCM_TRIGGER_XXX). + * + * Context: Any context. Takes and releases the VirtIO substream spinlock. + * May take and release the tx/rx queue spinlock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int co= mmand) +{ +=09struct virtio_pcm_substream *vss =3D snd_pcm_substream_chip(substream); +=09struct virtio_snd *snd =3D vss->snd; +=09struct virtio_snd_msg *msg; +=09unsigned long flags; +=09int rc; + +=09switch (command) { +=09case SNDRV_PCM_TRIGGER_START: +=09case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: { +=09=09struct virtio_snd_queue *queue =3D virtsnd_pcm_queue(vss); + +=09=09spin_lock_irqsave(&queue->lock, flags); +=09=09spin_lock(&vss->lock); +=09=09rc =3D virtsnd_pcm_msg_send(vss); +=09=09if (!rc) +=09=09=09vss->xfer_enabled =3D true; +=09=09spin_unlock(&vss->lock); +=09=09spin_unlock_irqrestore(&queue->lock, flags); +=09=09if (rc) +=09=09=09return rc; + +=09=09msg =3D virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START, +=09=09=09=09=09=09GFP_KERNEL); +=09=09if (!msg) { +=09=09=09spin_lock_irqsave(&vss->lock, flags); +=09=09=09vss->xfer_enabled =3D false; +=09=09=09spin_unlock_irqrestore(&vss->lock, flags); + +=09=09=09return -ENOMEM; +=09=09} + +=09=09return virtsnd_ctl_msg_send_sync(snd, msg); +=09} +=09case SNDRV_PCM_TRIGGER_STOP: +=09case SNDRV_PCM_TRIGGER_PAUSE_PUSH: { +=09=09spin_lock_irqsave(&vss->lock, flags); +=09=09vss->xfer_enabled =3D false; +=09=09spin_unlock_irqrestore(&vss->lock, flags); + +=09=09/* +=09=09 * The I/O queue needs to be flushed only when the substream is +=09=09 * completely stopped. +=09=09 */ +=09=09if (command =3D=3D SNDRV_PCM_TRIGGER_STOP) +=09=09=09vss->msg_flushing =3D true; + +=09=09/* +=09=09 * The STOP command can be issued in an atomic context after +=09=09 * the drain is complete. Therefore, in general, we cannot sleep +=09=09 * here. +=09=09 */ +=09=09msg =3D virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP, +=09=09=09=09=09=09GFP_ATOMIC); +=09=09if (!msg) +=09=09=09return -ENOMEM; + +=09=09return virtsnd_ctl_msg_send_async(snd, msg); +=09} +=09default: { +=09=09return -EINVAL; +=09} +=09} +} + +/** + * virtsnd_pcm_msg_count() - Returns the number of pending I/O messages. + * @vss: VirtIO substream. + * + * Context: Any context. + * Return: Number of messages. + */ +static inline +unsigned int virtsnd_pcm_msg_count(struct virtio_pcm_substream *vss) +{ +=09unsigned int msg_count; +=09unsigned long flags; + +=09spin_lock_irqsave(&vss->lock, flags); +=09msg_count =3D vss->msg_count; +=09spin_unlock_irqrestore(&vss->lock, flags); + +=09return msg_count; +} + +/** + * virtsnd_pcm_sync_stop() - Synchronous PCM substream stop. + * @substream: Kernel ALSA substream. + * + * The function can be called both from the upper level or from the driver + * itself. + * + * Context: Process context. Takes and releases the VirtIO substream spinl= ock. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream) +{ +=09struct virtio_pcm_substream *vss =3D snd_pcm_substream_chip(substream); +=09struct virtio_snd *snd =3D vss->snd; +=09struct virtio_snd_msg *msg; +=09unsigned int js =3D msecs_to_jiffies(msg_timeout_ms); +=09int rc; + +=09if (!vss->msg_flushing) +=09=09return 0; + +=09if (!virtsnd_pcm_msg_count(vss)) +=09=09goto on_exit; + +=09msg =3D virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE, +=09=09=09=09=09GFP_KERNEL); +=09if (!msg) +=09=09return -ENOMEM; + +=09rc =3D virtsnd_ctl_msg_send_sync(snd, msg); +=09if (rc) +=09=09return rc; + +=09/* +=09 * The spec states that upon receipt of the RELEASE command "the device +=09 * MUST complete all pending I/O messages for the specified stream ID". +=09 * Thus, we consider the absence of I/O messages in the queue as an +=09 * indication that the substream has been released. +=09 */ +=09rc =3D wait_event_interruptible_timeout(vss->msg_empty, +=09=09=09=09=09 !virtsnd_pcm_msg_count(vss), +=09=09=09=09=09 js); +=09if (rc <=3D 0) { +=09=09dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n", +=09=09=09 vss->sid); + +=09=09return !rc ? -ETIMEDOUT : rc; +=09} + +on_exit: +=09vss->msg_flushing =3D false; + +=09return 0; +} + +/** + * virtsnd_pcm_pointer() - Get the current hardware position for the PCM + * substream. + * @substream: Kernel ALSA substream. + * + * Context: Any context. Takes and releases the VirtIO substream spinlock. + * Return: Hardware position in frames inside [0 ... buffer_size) range. + */ +static snd_pcm_uframes_t +virtsnd_pcm_pointer(struct snd_pcm_substream *substream) +{ +=09struct virtio_pcm_substream *vss =3D snd_pcm_substream_chip(substream); +=09snd_pcm_uframes_t hw_ptr =3D SNDRV_PCM_POS_XRUN; +=09unsigned long flags; + +=09spin_lock_irqsave(&vss->lock, flags); +=09if (!vss->xfer_xrun) +=09=09hw_ptr =3D vss->hw_ptr; +=09spin_unlock_irqrestore(&vss->lock, flags); + +=09return hw_ptr; +} + +/* PCM substream operators map. */ +const struct snd_pcm_ops virtsnd_pcm_ops =3D { +=09.open =3D virtsnd_pcm_open, +=09.close =3D virtsnd_pcm_close, +=09.ioctl =3D snd_pcm_lib_ioctl, +=09.hw_params =3D virtsnd_pcm_hw_params, +=09.hw_free =3D virtsnd_pcm_hw_free, +=09.prepare =3D virtsnd_pcm_prepare, +=09.trigger =3D virtsnd_pcm_trigger, +=09.sync_stop =3D virtsnd_pcm_sync_stop, +=09.pointer =3D virtsnd_pcm_pointer, +}; --=20 2.30.0 --------------------------------------------------------------------- To unsubscribe, e-mail: virtio-dev-unsubscribe@lists.oasis-open.org For additional commands, e-mail: virtio-dev-help@lists.oasis-open.org