Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp454547pxb; Tue, 9 Feb 2021 04:50:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJx4NSJgF+xZ9p57WVOl5BO3Z49WwV3wfav2AKhLFiePH/V1bxTbTn3wKbC5xBqbaJeKnOAq X-Received: by 2002:aa7:d618:: with SMTP id c24mr8056719edr.7.1612875017017; Tue, 09 Feb 2021 04:50:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612875017; cv=none; d=google.com; s=arc-20160816; b=vGuRuqpAfHEdI+RqJJrN+mDT7P2z1rfG54hqQOr0Zy8UxONcKRGK16iE1IYgoUQxhv pppeFOrGANCdWUY8u23pjZc02+LdbMs4yKjobbYJmS4L1/f/nvA8+fGntcFFSnHoJ4Ui NVgmrCfypT24INHMV7GuvNnBBk6tEH/K7uCrlrXTx8eO8iEz5t/IpuiBD6FNGM3X4iFL 6vsLO1eZYIZJQj9qWzHyESCATM9Y4fXkSMq/3kf6Z+F6YZC2grqWl4SFEEWPZrLQuqwM MCc9h2tiLPVyiJeYUymrb1BEDyMIaKkRFmCfmzWE5nhPccuqVQDf4ikGdq4PMUebt1B3 fQwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Kf3NoYTKMR9cKxKhivPIKRwuTjyeEW86UKMD9wJXhYg=; b=gn2U98JG1UibrVyk7Y23ENIfQRdWMkr4ijpPW0Jg+6E+OpbqHpndWDBV5aLeDK0hoR oJTd2nvNrAJbtu6s4szCBbsscvIZc8jEqHDEGYMiqfeSpIfhQXe/fVV57PphVAMMOHvl BuRdx9sELWxdXyHN4nGoY38pu91EFI3ruWLq5lRoQIHbBCxT9UVe8aR5aQnyzHeGbOze 3Kvut+GmXeNSd2RupEgtkQxjX1XHU1D6foFD/PsBSvUdqC+F7TYpRB9AfKMduSY/B2dG 2fgUqErnh/gkdgJJqq0FLAvQ9S15BXXEtctLlkN0zNv6DuvLixDFj3QjD1duyDApKceK E2Yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@opensynergy.com header.s=srmailgate02 header.b=SXWaVvwz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=opensynergy.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u19si5533380edo.338.2021.02.09.04.49.53; Tue, 09 Feb 2021 04:50:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@opensynergy.com header.s=srmailgate02 header.b=SXWaVvwz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=opensynergy.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230216AbhBIMpR (ORCPT + 99 others); Tue, 9 Feb 2021 07:45:17 -0500 Received: from mx1.opensynergy.com ([217.66.60.4]:47974 "EHLO mx1.opensynergy.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230362AbhBIMmu (ORCPT ); Tue, 9 Feb 2021 07:42:50 -0500 Received: from SR-MAILGATE-02.opensynergy.com (localhost.localdomain [127.0.0.1]) by mx1.opensynergy.com (Proxmox) with ESMTP id E2B93A15F7; Tue, 9 Feb 2021 13:41:09 +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=Kf3NoYTKMR9c KxKhivPIKRwuTjyeEW86UKMD9wJXhYg=; b=SXWaVvwzKI9BNgLAL1EQJKR211hc WyuKWuvWAt8HwBQixKM8+xr3ts9WmVli/4LZxvHOzxHlTRFoOt3K4Pz7fMauc3AN 0s3OXtUGmp66Bw95Q23lLFt17F1WzD+RgA9IAohvA9TiW5kwZDglCiYFCBOspHnU 9t31Z9QwKJBnM2ANTtckNlk/3PahJLCErR0mxe+IyzGvTvSgegZgtH762bqw950d XOD1aQn7vHzyXQyCcd10Ey2+hJn4+0YBqqQZ/J5h5Y2haL8Og+azF+aDIo+MsrOV GmjrRJ09V/vAVC+CfC5ni4+EAS5CeJT/A4ukHDLAmFLUH0T4eJ4QHSJM9A== From: Anton Yakovlev To: , , CC: "Michael S. Tsirkin" , Jaroslav Kysela , Takashi Iwai , Subject: [PATCH v3 9/9] ALSA: virtio: introduce device suspend/resume support Date: Tue, 9 Feb 2021 13:40:10 +0100 Message-ID: <20210209124011.1224628-10-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 All running PCM substreams are stopped on device suspend and restarted on device resume. Signed-off-by: Anton Yakovlev --- sound/virtio/virtio_card.c | 57 +++++++++++++++++++++++++++++++++++ sound/virtio/virtio_pcm.c | 1 + sound/virtio/virtio_pcm_ops.c | 44 ++++++++++++++++++++------- 3 files changed, 91 insertions(+), 11 deletions(-) diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c index 787a4dec1da8..1f0a0fa7bbc0 100644 --- a/sound/virtio/virtio_card.c +++ b/sound/virtio/virtio_card.c @@ -373,6 +373,59 @@ static void virtsnd_config_changed(struct virtio_device *vdev) "sound device configuration was changed\n"); } +#ifdef CONFIG_PM_SLEEP +/** + * virtsnd_freeze() - Suspend device. + * @vdev: VirtIO parent device. + * + * Context: Any context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_freeze(struct virtio_device *vdev) +{ + struct virtio_snd *snd = vdev->priv; + + /* Stop all the virtqueues. */ + vdev->config->reset(vdev); + vdev->config->del_vqs(vdev); + + virtsnd_ctl_msg_cancel_all(snd); + + kfree(snd->event_msgs); + + /* + * If the virtsnd_restore() fails before re-allocating events, then we + * get a dangling pointer here. + */ + snd->event_msgs = NULL; + + return 0; +} + +/** + * virtsnd_restore() - Resume device. + * @vdev: VirtIO parent device. + * + * Context: Any context. + * Return: 0 on success, -errno on failure. + */ +static int virtsnd_restore(struct virtio_device *vdev) +{ + struct virtio_snd *snd = vdev->priv; + int rc; + + rc = virtsnd_find_vqs(snd); + if (rc) + return rc; + + virtio_device_ready(vdev); + + virtsnd_enable_event_vq(snd); + + return 0; +} +#endif /* CONFIG_PM_SLEEP */ + static const struct virtio_device_id id_table[] = { { VIRTIO_ID_SOUND, VIRTIO_DEV_ANY_ID }, { 0 }, @@ -386,6 +439,10 @@ static struct virtio_driver virtsnd_driver = { .probe = virtsnd_probe, .remove = virtsnd_remove, .config_changed = virtsnd_config_changed, +#ifdef CONFIG_PM_SLEEP + .freeze = virtsnd_freeze, + .restore = virtsnd_restore, +#endif }; static int __init init(void) diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index 1d98de878385..401d4c975d2b 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, SNDRV_PCM_INFO_BATCH | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_PAUSE; if (!info->channels_min || info->channels_min > info->channels_max) { diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c index c2224f5461c4..207f4877a5ec 100644 --- a/sound/virtio/virtio_pcm_ops.c +++ b/sound/virtio/virtio_pcm_ops.c @@ -220,6 +220,10 @@ static int virtsnd_pcm_hw_params(struct snd_pcm_substream *substream, if (rc) return rc; + /* If messages have already been allocated before, do nothing. */ + if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) + return 0; + return virtsnd_pcm_msg_alloc(vss, periods, period_bytes); } @@ -260,19 +264,21 @@ static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream) } 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; + if (runtime->status->state != SNDRV_PCM_STATE_SUSPENDED) { + /* + * 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->hw_ptr = 0; + vss->msg_last_enqueued = -1; + } vss->xfer_xrun = false; - vss->msg_last_enqueued = -1; vss->msg_count = 0; spin_unlock_irqrestore(&vss->lock, flags); @@ -302,6 +308,21 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) int rc; switch (command) { + case SNDRV_PCM_TRIGGER_RESUME: { + /* + * We restart the substream by executing the standard command + * sequence. + */ + rc = virtsnd_pcm_hw_params(substream, NULL); + if (rc) + return rc; + + rc = virtsnd_pcm_prepare(substream); + if (rc) + return rc; + + fallthrough; + } case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: { struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); @@ -328,6 +349,7 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) return virtsnd_ctl_msg_send_sync(snd, msg); } + case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_PAUSE_PUSH: { spin_lock_irqsave(&vss->lock, flags); -- 2.30.0