Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp116479pxj; Mon, 17 May 2021 22:38:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQmAmXuch5HzfbR9mh4wSoj4Mr8QYXfgx3qBqlxe21XwufBEf5jQ0g6aJtyJ1L4fKKSJeR X-Received: by 2002:aca:4dc3:: with SMTP id a186mr2143190oib.22.1621316291559; Mon, 17 May 2021 22:38:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621316291; cv=none; d=google.com; s=arc-20160816; b=H2hdlKAw0U92A2mkiko06X4thTEJzJqInGrYiPFHKcmfXEJ2vWx5QFjxYnr6CsQQjb a16LEAQpN5gbaNCYQqmt9As8o5kMLrm6wJGzxEYKJx6qLUmHCUBT8FjyZ3eafFX0+tVL i0i5i5l0lDe7csNhHMUC7k0LjmlAe6mvrk2L8vb0ZHtfkubnTYYwNZyRCRxScL247PbK YOtKYsanJOzvP4Lp838BedRvqT+yHc2UKLx1JhCIrd6X3YPeteYE20Cpq4nFixwv5qkG GVRmphJylXTDDplVtdOWLQ3qVbwF1v+jxRl/gNDPW4KlPeJlvFv2V2KQU9AlAqCEza5d zUpg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=n6dt96WVRk0uVAwC7y8BlfRxWZodxR7Tr0j4TpDy5eY=; b=FLogyPc4RVn0a9LcTEldMO6KRZFrACnUHbKyX2ljP/bEB7yM8ZrrXifuVfvC57PNan s5/4nXFxYXdeTpC4KNSs9fbqB4lzdWsAV6zD+FkIkdE5FwYZxbsadiaZ2DlfMVFsGCFw Sp7PZClKhCktAsS7aOn2i2qNWrtCq9MF48S1Ww+4mVCv1IJNGkfJFO4KL7AdHdmFZAzh eRKIR0owikbEw6KgB3LwBnTVFgz7/GJE3+JCCJA546IXPRzSzOzq8OiLYMIh8IleUPYJ EiAv/a1XZI61IE0ZCZ2lpQCKGDG98qhgZyBLaEZme0PQXgZAqodyFUt4PYacF88kX1/K g47A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aC5DcZef; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a10si21981332ilv.110.2021.05.17.22.37.58; Mon, 17 May 2021 22:38:11 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=aC5DcZef; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241051AbhEQOsD (ORCPT + 99 others); Mon, 17 May 2021 10:48:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:34328 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241118AbhEQOjh (ORCPT ); Mon, 17 May 2021 10:39:37 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9F73E61941; Mon, 17 May 2021 14:18:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621261122; bh=4KpiqIkSPdogFIPBLLg6Z0qN5SzVJxhS+i54j/9U6Kc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aC5DcZef/iL/An2I15ijvEZg/TikirA66lSmZZExcjnqe2T8Vubg+PuBTodUeiAHD OBNZNNchj9A2TSX+IiQd/TZviSytjxTSZ/CjkkehrtEz1xAuhENgR57D/Px8mhHqce c73+a7RAT+g+JRosd3RJN+BV9Xo+pgrOp0L9MV2I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mikhail Durnev , Mark Brown , Sasha Levin Subject: [PATCH 5.4 010/141] ASoC: rsnd: core: Check convert rate in rsnd_hw_params Date: Mon, 17 May 2021 16:01:02 +0200 Message-Id: <20210517140243.103397538@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140242.729269392@linuxfoundation.org> References: <20210517140242.729269392@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mikhail Durnev [ Upstream commit 19c6a63ced5e07e40f3a5255cb1f0fe0d3be7b14 ] snd_pcm_hw_params_set_rate_near can return incorrect sample rate in some cases, e.g. when the backend output rate is set to some value higher than 48000 Hz and the input rate is 8000 Hz. So passing the value returned by snd_pcm_hw_params_set_rate_near to snd_pcm_hw_params will result in "FSO/FSI ratio error" and playing no audio at all while the userland is not properly notified about the issue. If SRC is unable to convert the requested sample rate to the sample rate the backend is using, then the requested sample rate should be adjusted in rsnd_hw_params. The userland will be notified about that change in the returned hw_params structure. Signed-off-by: Mikhail Durnev Link: https://lore.kernel.org/r/1615870055-13954-1-git-send-email-mikhail_durnev@mentor.com Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- sound/soc/sh/rcar/core.c | 69 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/sound/soc/sh/rcar/core.c b/sound/soc/sh/rcar/core.c index a6c1cf987e6e..df8d7b53b760 100644 --- a/sound/soc/sh/rcar/core.c +++ b/sound/soc/sh/rcar/core.c @@ -1426,8 +1426,75 @@ static int rsnd_hw_params(struct snd_pcm_substream *substream, } if (io->converted_chan) dev_dbg(dev, "convert channels = %d\n", io->converted_chan); - if (io->converted_rate) + if (io->converted_rate) { + /* + * SRC supports convert rates from params_rate(hw_params)/k_down + * to params_rate(hw_params)*k_up, where k_up is always 6, and + * k_down depends on number of channels and SRC unit. + * So all SRC units can upsample audio up to 6 times regardless + * its number of channels. And all SRC units can downsample + * 2 channel audio up to 6 times too. + */ + int k_up = 6; + int k_down = 6; + int channel; + struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io); + dev_dbg(dev, "convert rate = %d\n", io->converted_rate); + + channel = io->converted_chan ? io->converted_chan : + params_channels(hw_params); + + switch (rsnd_mod_id(src_mod)) { + /* + * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times. + * SRC1, SRC3 and SRC4 can downsample 4 channel audio + * up to 4 times. + * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio + * no more than twice. + */ + case 1: + case 3: + case 4: + if (channel > 4) { + k_down = 2; + break; + } + fallthrough; + case 0: + if (channel > 2) + k_down = 4; + break; + + /* Other SRC units do not support more than 2 channels */ + default: + if (channel > 2) + return -EINVAL; + } + + if (params_rate(hw_params) > io->converted_rate * k_down) { + hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min = + io->converted_rate * k_down; + hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max = + io->converted_rate * k_down; + hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE; + } else if (params_rate(hw_params) * k_up < io->converted_rate) { + hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min = + (io->converted_rate + k_up - 1) / k_up; + hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max = + (io->converted_rate + k_up - 1) / k_up; + hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE; + } + + /* + * TBD: Max SRC input and output rates also depend on number + * of channels and SRC unit: + * SRC1, SRC3 and SRC4 do not support more than 128kHz + * for 6 channel and 96kHz for 8 channel audio. + * Perhaps this function should return EINVAL if the input or + * the output rate exceeds the limitation. + */ + } } ret = rsnd_dai_call(hw_params, io, substream, hw_params); -- 2.30.2