From patchwork Mon Aug 7 12:22:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Andrushchenko X-Patchwork-Id: 9885259 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CB20960363 for ; Mon, 7 Aug 2017 12:25:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BC1C628670 for ; Mon, 7 Aug 2017 12:25:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B0BFD28675; Mon, 7 Aug 2017 12:25:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EB3C128670 for ; Mon, 7 Aug 2017 12:25:37 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1deh43-0007zg-JY; Mon, 07 Aug 2017 12:23:19 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1deh42-0007xX-HK for xen-devel@lists.xen.org; Mon, 07 Aug 2017 12:23:18 +0000 Received: from [85.158.143.35] by server-6.bemta-6.messagelabs.com id 2B/E0-03937-5BB58895; Mon, 07 Aug 2017 12:23:17 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrMIsWRWlGSWpSXmKPExsVyMfS6i+6W6I5 Ig6aTVhZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bKb19YCv6EVky68YKpgfGdVxcjF4eQQD+j xPlHs5hBHBaBlywSJ25NZgNxJAT6WSV+dB8HcjiBnDiJ9yeus0LYVRJnl/9kB7GFBBQlvj6bz gQxahWTxLWru1lAEmwCRhLLb/wAs0UEIiSOPf4ANohZoFTixv45zCC2sIC/xLqdzxlBbBYBVY kJ71Yxgdi8Ak4SfRdvQS2Wk7h5rhOsnhMo/rz3MRvEYkeJz2dmsE5gFFjAyLCKUaM4tagstUj X2FAvqSgzPaMkNzEzR9fQwEwvN7W4ODE9NScxqVgvOT93EyMwuBiAYAdj06LAQ4ySHExKorzx K9oihfiS8lMqMxKLM+KLSnNSiw8xynBwKEnwFkZ1RAoJFqWmp1akZeYAwxwmLcHBoyTCqwKS5 i0uSMwtzkyHSJ1itOe4cmXdFyaODavXA8kpB7YDyVcT/n9jEmLJy89LlRLnvQvSJgDSllGaBz cUFpeXGGWlhHkZgc4U4ilILcrNLEGVf8UozsGoJMw7H2QKT2ZeCdzuV0BnMQGd9SaxFeSskkS ElFQDY5ikgZN1935z7zfKJ7hmhDxNL92g2/nD8X3zko3b132asnFX7jT3eB3F+2emVF1n7V3e u6jFK2P9nZZ+zWWek/dpSRjcDRfsCn7fvntfqXfip4c6d7suSDh03bHnX3I86sS5SWp28/U5D u6aqFt2eFfKqeSA2V/CnhZemn8iOr/p+8n/F/6XVymxFGckGmoxFxUnAgDSbRvpxgIAAA== X-Env-Sender: andr2000@gmail.com X-Msg-Ref: server-3.tower-21.messagelabs.com!1502108596!73311230!1 X-Originating-IP: [209.85.215.68] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 64414 invoked from network); 7 Aug 2017 12:23:16 -0000 Received: from mail-lf0-f68.google.com (HELO mail-lf0-f68.google.com) (209.85.215.68) by server-3.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 7 Aug 2017 12:23:16 -0000 Received: by mail-lf0-f68.google.com with SMTP id o85so222492lff.1 for ; Mon, 07 Aug 2017 05:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=DpzgR9/XQL1KGtZDcxYEyxAJfzhH+y6vvaRCN9gS+R0=; b=KAFrEzqQTkPLAAo60YMBq+/4uEbB91HCJtXWkTiGM5LvULQBeQmMcD7jhs7rwNg1GA M3+A/6XCyQHfTGawisoJp1aarhLrpr1dJSaUaxBvgUqX5k+jBgrbDyt40/ISycNuheEY gmMdQMXVWJhXXvd6cmVk+nSZKg7LrXb3DTbyrGSOxHW55KmtFac1Smt616knpR8xUdUG diFklXiXdi2sjJH2oaq0loPAF5PgWCWoW5MZePj484Evx8VlHwfR01w5bR5TLtt0KMBx C7+z17SpgA2jhbGYDmzjGYxagrKhpewXHVX5/Q5yOq63+KazuYfx5zucjWCsX2z6yqcO D3mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=DpzgR9/XQL1KGtZDcxYEyxAJfzhH+y6vvaRCN9gS+R0=; b=jEIkCBjh8OiAqhXhOHxBUvCq53JILWs7AdXr15/6XIZjhZdWNVoJMm7KnsPL1xTzFa MRvqFYRx8v1nLoemw969jj49bNKEnA06/hC5PARPCkVpB08vIfF/T0xu/UJhe6mmgGNm tSrpKGya1NtQBvJFZJCbR6je9ELGvz6M9R6OmzSIDEVIe3FtSW7xlc1Ypznnw0TSj8gY Qkd49dGUWthIpbsp7rowMN11iS4EVXPv5jOVfhh8qq9StFB4sUa3lW3/g0RpiM2VlhZN Ge3CiguCjDrEwkL3iLYtAR1/lIpH2AUBBTAko6lsleEtgghD+NthDLPOWH/645OMPBXM oNEg== X-Gm-Message-State: AHYfb5jDdx29d9OCRC1hIvqYXzaz3wGk8ldpIDTDd5IqJ1yPhA3qZuvV 6km6lF9h6nk4uw== X-Received: by 10.25.152.205 with SMTP id a196mr118279lfe.165.1502108596180; Mon, 07 Aug 2017 05:23:16 -0700 (PDT) Received: from a2k-HP-ProDesk-600-G2-SFF.kyiv.epam.com (ll-53.209.223.85.sovam.net.ua. [85.223.209.53]) by smtp.gmail.com with ESMTPSA id m143sm1031821lfm.77.2017.08.07.05.23.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 07 Aug 2017 05:23:15 -0700 (PDT) From: Oleksandr Andrushchenko To: alsa-devel@alsa-project.org, xen-devel@lists.xen.org, linux-kernel@vger.kernel.org Date: Mon, 7 Aug 2017 15:22:56 +0300 Message-Id: <1502108577-8099-12-git-send-email-andr2000@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1502108577-8099-1-git-send-email-andr2000@gmail.com> References: <1502108577-8099-1-git-send-email-andr2000@gmail.com> Cc: andr2000@gmail.com, Oleksandr Andrushchenko , tiwai@suse.com, perex@perex.cz Subject: [Xen-devel] [PATCH RESEND1 11/12] ALSA: vsnd: Implement communication with backend X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Oleksandr Andrushchenko Implement frontend to backend communication according to the para-virtualized sound protocol: xen/interface/io/sndif.h. Signed-off-by: Oleksandr Andrushchenko --- sound/drivers/xen-front.c | 302 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 288 insertions(+), 14 deletions(-) diff --git a/sound/drivers/xen-front.c b/sound/drivers/xen-front.c index 7275e9cb38c0..8bfec43ef03a 100644 --- a/sound/drivers/xen-front.c +++ b/sound/drivers/xen-front.c @@ -38,6 +38,8 @@ * because of the fact it is already in use/reserved by the PV console. */ #define GRANT_INVALID_REF 0 +/* timeout in ms to wait for backend to respond */ +#define VSND_WAIT_BACK_MS 3000 /* maximum number of supported streams */ #define VSND_MAX_STREAM 8 @@ -151,10 +153,12 @@ struct xdrv_info { struct sdev_card_plat_data cfg_plat_data; }; +static inline void xdrv_evtchnl_flush(struct xdrv_evtchnl_info *channel); static inline void sh_buf_clear(struct sh_buf_info *buf); static void sh_buf_free(struct sh_buf_info *buf); static int sh_buf_alloc(struct xenbus_device *xb_dev, struct sh_buf_info *buf, unsigned int buffer_size); +static grant_ref_t sh_buf_get_dir_start(struct sh_buf_info *buf); static struct sdev_pcm_stream_info *sdrv_stream_get( struct snd_pcm_substream *substream) @@ -314,6 +318,234 @@ static void sdrv_copy_pcm_hw(struct snd_pcm_hardware *dst, } } +struct ALSA_SNDIF_SAMPLE_FORMAT { + uint8_t sndif; + snd_pcm_format_t alsa; +}; + +static struct ALSA_SNDIF_SAMPLE_FORMAT alsa_sndif_formats[] = { + { + .sndif = XENSND_PCM_FORMAT_U8, + .alsa = SNDRV_PCM_FORMAT_U8 + }, + { + .sndif = XENSND_PCM_FORMAT_S8, + .alsa = SNDRV_PCM_FORMAT_S8 + }, + { + .sndif = XENSND_PCM_FORMAT_U16_LE, + .alsa = SNDRV_PCM_FORMAT_U16_LE + }, + { + .sndif = XENSND_PCM_FORMAT_U16_BE, + .alsa = SNDRV_PCM_FORMAT_U16_BE + }, + { + .sndif = XENSND_PCM_FORMAT_S16_LE, + .alsa = SNDRV_PCM_FORMAT_S16_LE + }, + { + .sndif = XENSND_PCM_FORMAT_S16_BE, + .alsa = SNDRV_PCM_FORMAT_S16_BE + }, + { + .sndif = XENSND_PCM_FORMAT_U24_LE, + .alsa = SNDRV_PCM_FORMAT_U24_LE + }, + { + .sndif = XENSND_PCM_FORMAT_U24_BE, + .alsa = SNDRV_PCM_FORMAT_U24_BE + }, + { + .sndif = XENSND_PCM_FORMAT_S24_LE, + .alsa = SNDRV_PCM_FORMAT_S24_LE + }, + { + .sndif = XENSND_PCM_FORMAT_S24_BE, + .alsa = SNDRV_PCM_FORMAT_S24_BE + }, + { + .sndif = XENSND_PCM_FORMAT_U32_LE, + .alsa = SNDRV_PCM_FORMAT_U32_LE + }, + { + .sndif = XENSND_PCM_FORMAT_U32_BE, + .alsa = SNDRV_PCM_FORMAT_U32_BE + }, + { + .sndif = XENSND_PCM_FORMAT_S32_LE, + .alsa = SNDRV_PCM_FORMAT_S32_LE + }, + { + .sndif = XENSND_PCM_FORMAT_S32_BE, + .alsa = SNDRV_PCM_FORMAT_S32_BE + }, + { + .sndif = XENSND_PCM_FORMAT_A_LAW, + .alsa = SNDRV_PCM_FORMAT_A_LAW + }, + { + .sndif = XENSND_PCM_FORMAT_MU_LAW, + .alsa = SNDRV_PCM_FORMAT_MU_LAW + }, + { + .sndif = XENSND_PCM_FORMAT_F32_LE, + .alsa = SNDRV_PCM_FORMAT_FLOAT_LE + }, + { + .sndif = XENSND_PCM_FORMAT_F32_BE, + .alsa = SNDRV_PCM_FORMAT_FLOAT_BE + }, + { + .sndif = XENSND_PCM_FORMAT_F64_LE, + .alsa = SNDRV_PCM_FORMAT_FLOAT64_LE + }, + { + .sndif = XENSND_PCM_FORMAT_F64_BE, + .alsa = SNDRV_PCM_FORMAT_FLOAT64_BE + }, + { + .sndif = XENSND_PCM_FORMAT_IEC958_SUBFRAME_LE, + .alsa = SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE + }, + { + .sndif = XENSND_PCM_FORMAT_IEC958_SUBFRAME_BE, + .alsa = SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE + }, + { + .sndif = XENSND_PCM_FORMAT_IMA_ADPCM, + .alsa = SNDRV_PCM_FORMAT_IMA_ADPCM + }, + { + .sndif = XENSND_PCM_FORMAT_MPEG, + .alsa = SNDRV_PCM_FORMAT_MPEG + }, + { + .sndif = XENSND_PCM_FORMAT_GSM, + .alsa = SNDRV_PCM_FORMAT_GSM + }, +}; + +static int alsa_to_sndif_format(snd_pcm_format_t format) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(alsa_sndif_formats); i++) + if (alsa_sndif_formats[i].alsa == format) + return alsa_sndif_formats[i].sndif; + return -EINVAL; +} + +static void sdrv_stream_clear(struct sdev_pcm_stream_info *stream) +{ + stream->is_open = false; + stream->req_next_id = 0; + sh_buf_clear(&stream->sh_buf); +} + +static struct xensnd_req *sdrv_be_stream_prepare_req( + struct sdev_pcm_stream_info *stream, uint8_t operation) +{ + struct xensnd_req *req; + + req = RING_GET_REQUEST(&stream->evt_chnl->ring, + stream->evt_chnl->ring.req_prod_pvt); + req->operation = operation; + req->id = stream->req_next_id++; + stream->evt_chnl->resp_id = req->id; + return req; +} + +static void sdrv_be_stream_free(struct sdev_pcm_stream_info *stream) +{ + sh_buf_free(&stream->sh_buf); + sdrv_stream_clear(stream); +} + +static int sdrv_be_stream_do_io(struct xdrv_evtchnl_info *evtchnl) +{ + if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED)) + return -EIO; + + reinit_completion(&evtchnl->completion); + xdrv_evtchnl_flush(evtchnl); + return 0; +} + +static inline int sdrv_be_stream_wait_io(struct xdrv_evtchnl_info *evtchnl) +{ + if (wait_for_completion_timeout( + &evtchnl->completion, + msecs_to_jiffies(VSND_WAIT_BACK_MS)) <= 0) + return -ETIMEDOUT; + return 0; +} + +static int sdrv_be_stream_open(struct snd_pcm_substream *substream, + struct sdev_pcm_stream_info *stream) +{ + struct sdev_pcm_instance_info *pcm_instance = + snd_pcm_substream_chip(substream); + struct snd_pcm_runtime *runtime = substream->runtime; + struct xdrv_info *xdrv_info; + struct xensnd_req *req; + unsigned long flags; + int ret; + + xdrv_info = pcm_instance->card_info->xdrv_info; + + ret = alsa_to_sndif_format(runtime->format); + if (ret < 0) { + dev_err(&xdrv_info->xb_dev->dev, + "Unsupported sample format: %d\n", runtime->format); + return ret; + } + + spin_lock_irqsave(&xdrv_info->io_lock, flags); + stream->is_open = false; + req = sdrv_be_stream_prepare_req(stream, XENSND_OP_OPEN); + req->op.open.pcm_format = (uint8_t)ret; + req->op.open.pcm_channels = runtime->channels; + req->op.open.pcm_rate = runtime->rate; + req->op.open.buffer_sz = stream->sh_buf.vbuffer_sz; + req->op.open.gref_directory = sh_buf_get_dir_start(&stream->sh_buf); + + ret = sdrv_be_stream_do_io(stream->evt_chnl); + spin_unlock_irqrestore(&xdrv_info->io_lock, flags); + + if (ret < 0) + return ret; + + ret = sdrv_be_stream_wait_io(stream->evt_chnl); + stream->is_open = ret < 0 ? false : true; + return ret; +} + +static int sdrv_be_stream_close(struct snd_pcm_substream *substream, + struct sdev_pcm_stream_info *stream) +{ + struct sdev_pcm_instance_info *pcm_instance = + snd_pcm_substream_chip(substream); + struct xdrv_info *xdrv_info; + struct xensnd_req *req; + unsigned long flags; + int ret; + + xdrv_info = pcm_instance->card_info->xdrv_info; + + spin_lock_irqsave(&xdrv_info->io_lock, flags); + stream->is_open = false; + req = sdrv_be_stream_prepare_req(stream, XENSND_OP_CLOSE); + + ret = sdrv_be_stream_do_io(stream->evt_chnl); + spin_unlock_irqrestore(&xdrv_info->io_lock, flags); + + if (ret < 0) + return ret; + + return sdrv_be_stream_wait_io(stream->evt_chnl); +} + static int sdrv_alsa_open(struct snd_pcm_substream *substream) { struct sdev_pcm_instance_info *pcm_instance = @@ -339,7 +571,7 @@ static int sdrv_alsa_open(struct snd_pcm_substream *substream) ret = sdrv_alsa_timer_create(substream); spin_lock_irqsave(&xdrv_info->io_lock, flags); - sh_buf_clear(&stream->sh_buf); + sdrv_stream_clear(stream); stream->evt_chnl = &xdrv_info->evt_chnls[stream->unique_id]; if (ret < 0) stream->evt_chnl->state = EVTCHNL_STATE_DISCONNECTED; @@ -378,7 +610,7 @@ static int sdrv_alsa_hw_params(struct snd_pcm_substream *substream, int ret; buffer_size = params_buffer_bytes(params); - sh_buf_clear(&stream->sh_buf); + sdrv_stream_clear(stream); xdrv_info = pcm_instance->card_info->xdrv_info; ret = sh_buf_alloc(xdrv_info->xb_dev, &stream->sh_buf, buffer_size); @@ -390,22 +622,18 @@ static int sdrv_alsa_hw_params(struct snd_pcm_substream *substream, dev_err(&xdrv_info->xb_dev->dev, "Failed to allocate buffers for stream idx %d\n", stream->unique_id); + sdrv_be_stream_free(stream); return ret; } static int sdrv_alsa_hw_free(struct snd_pcm_substream *substream) { - struct sdev_pcm_instance_info *pcm_instance = - snd_pcm_substream_chip(substream); struct sdev_pcm_stream_info *stream = sdrv_stream_get(substream); - struct xdrv_info *xdrv_info; - unsigned long flags; + int ret; - xdrv_info = pcm_instance->card_info->xdrv_info; - spin_lock_irqsave(&xdrv_info->io_lock, flags); - sh_buf_free(&stream->sh_buf); - spin_unlock_irqrestore(&xdrv_info->io_lock, flags); - return 0; + ret = sdrv_be_stream_close(substream, stream); + sdrv_be_stream_free(stream); + return ret; } static int sdrv_alsa_prepare(struct snd_pcm_substream *substream) @@ -413,8 +641,12 @@ static int sdrv_alsa_prepare(struct snd_pcm_substream *substream) struct sdev_pcm_stream_info *stream = sdrv_stream_get(substream); int ret = 0; - if (!stream->is_open) + if (!stream->is_open) { + ret = sdrv_be_stream_open(substream, stream); + if (ret < 0) + return ret; ret = sdrv_alsa_timer_prepare(substream); + } return ret; } @@ -446,7 +678,28 @@ static inline snd_pcm_uframes_t sdrv_alsa_pointer( static int sdrv_alsa_playback_do_write(struct snd_pcm_substream *substream, unsigned long pos, unsigned long count) { - return 0; + struct sdev_pcm_stream_info *stream = sdrv_stream_get(substream); + struct sdev_pcm_instance_info *pcm_instance = + snd_pcm_substream_chip(substream); + struct xdrv_info *xdrv_info; + struct xensnd_req *req; + unsigned long flags; + int ret; + + xdrv_info = pcm_instance->card_info->xdrv_info; + + spin_lock_irqsave(&xdrv_info->io_lock, flags); + req = sdrv_be_stream_prepare_req(stream, XENSND_OP_WRITE); + req->op.rw.length = count; + req->op.rw.offset = pos; + + ret = sdrv_be_stream_do_io(stream->evt_chnl); + spin_unlock_irqrestore(&xdrv_info->io_lock, flags); + + if (ret < 0) + return ret; + + return sdrv_be_stream_wait_io(stream->evt_chnl); } static int sdrv_alsa_playback_copy_user(struct snd_pcm_substream *substream, @@ -479,7 +732,28 @@ static int sdrv_alsa_playback_copy_kernel(struct snd_pcm_substream *substream, static int sdrv_alsa_playback_do_read(struct snd_pcm_substream *substream, unsigned long pos, unsigned long count) { - return 0; + struct sdev_pcm_stream_info *stream = sdrv_stream_get(substream); + struct sdev_pcm_instance_info *pcm_instance = + snd_pcm_substream_chip(substream); + struct xdrv_info *xdrv_info; + struct xensnd_req *req; + unsigned long flags; + int ret; + + xdrv_info = pcm_instance->card_info->xdrv_info; + + spin_lock_irqsave(&xdrv_info->io_lock, flags); + req = sdrv_be_stream_prepare_req(stream, XENSND_OP_READ); + req->op.rw.length = count; + req->op.rw.offset = pos; + + ret = sdrv_be_stream_do_io(stream->evt_chnl); + spin_unlock_irqrestore(&xdrv_info->io_lock, flags); + + if (ret < 0) + return ret; + + return sdrv_be_stream_wait_io(stream->evt_chnl); } static int sdrv_alsa_capture_copy_user(struct snd_pcm_substream *substream,