diff mbox series

[v3,1/6] ASoC: dmaengine: do not use a NULL prepare_slave_config() callback

Message ID 20220307122202.2251639-2-codrin.ciubotariu@microchip.com (mailing list archive)
State New, archived
Headers show
Series Add driver for SAMA7G5's PDMC | expand

Commit Message

Codrin Ciubotariu March 7, 2022, 12:21 p.m. UTC
Even if struct snd_dmaengine_pcm_config is used, prepare_slave_config()
callback might not be set. Check if this callback is set before using it.

Fixes: fa654e085300 ("ASoC: dmaengine-pcm: Provide default config")
Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
---

Changes in v2,v3:
 - none

 sound/soc/soc-generic-dmaengine-pcm.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Sascha Hauer April 20, 2022, 9:15 a.m. UTC | #1
Hi,

On Mon, Mar 07, 2022 at 02:21:57PM +0200, Codrin Ciubotariu wrote:
> Even if struct snd_dmaengine_pcm_config is used, prepare_slave_config()
> callback might not be set. Check if this callback is set before using it.
> 
> Fixes: fa654e085300 ("ASoC: dmaengine-pcm: Provide default config")
> Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
> ---
> 
> Changes in v2,v3:
>  - none
> 
>  sound/soc/soc-generic-dmaengine-pcm.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/sound/soc/soc-generic-dmaengine-pcm.c b/sound/soc/soc-generic-dmaengine-pcm.c
> index 285441d6aeed..2ab2ddc1294d 100644
> --- a/sound/soc/soc-generic-dmaengine-pcm.c
> +++ b/sound/soc/soc-generic-dmaengine-pcm.c
> @@ -86,10 +86,10 @@ static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
>  
>  	memset(&slave_config, 0, sizeof(slave_config));
>  
> -	if (!pcm->config)
> -		prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
> -	else
> +	if (pcm->config && pcm->config->prepare_slave_config)
>  		prepare_slave_config = pcm->config->prepare_slave_config;
> +	else
> +		prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
>  
>  	if (prepare_slave_config) {
>  		int ret = prepare_slave_config(substream, params, &slave_config);

I wonder if this patch is correct. There are drivers like
sound/soc/mxs/mxs-pcm.c which call snd_dmaengine_pcm_register() with a
config which has the prepare_slave_config callback unset. For these
drivers dmaengine_pcm_hw_params() previously was a no-op. Now with this
patch snd_dmaengine_pcm_prepare_slave_config() and
dmaengine_slave_config() are called. At least for the mxs-pcm driver
calling dmaengine_slave_config() will return -ENOSYS.

At least the "Check if this callback is set before using it" part is
wrong, the callback is checked before using it with

	if (prepare_slave_config) {
		...
	}

I don't have any mxs hardware at hand to test this. I just stumbled upon
the change of behaviour when rebasing
https://patchwork.kernel.org/project/alsa-devel/patch/20220301122111.1073174-1-s.hauer@pengutronix.de/
on current master.

Sascha
Codrin Ciubotariu April 20, 2022, 9:58 a.m. UTC | #2
On 20.04.2022 12:15, Sascha Hauer wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> Hi,

Hi Sascha,

> 
> On Mon, Mar 07, 2022 at 02:21:57PM +0200, Codrin Ciubotariu wrote:
>> Even if struct snd_dmaengine_pcm_config is used, prepare_slave_config()
>> callback might not be set. Check if this callback is set before using it.
>>
>> Fixes: fa654e085300 ("ASoC: dmaengine-pcm: Provide default config")
>> Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
>> ---
>>
>> Changes in v2,v3:
>>   - none
>>
>>   sound/soc/soc-generic-dmaengine-pcm.c | 6 +++---
>>   1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/sound/soc/soc-generic-dmaengine-pcm.c b/sound/soc/soc-generic-dmaengine-pcm.c
>> index 285441d6aeed..2ab2ddc1294d 100644
>> --- a/sound/soc/soc-generic-dmaengine-pcm.c
>> +++ b/sound/soc/soc-generic-dmaengine-pcm.c
>> @@ -86,10 +86,10 @@ static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
>>
>>        memset(&slave_config, 0, sizeof(slave_config));
>>
>> -     if (!pcm->config)
>> -             prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
>> -     else
>> +     if (pcm->config && pcm->config->prepare_slave_config)
>>                prepare_slave_config = pcm->config->prepare_slave_config;
>> +     else
>> +             prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
>>
>>        if (prepare_slave_config) {
>>                int ret = prepare_slave_config(substream, params, &slave_config);
> 
> I wonder if this patch is correct. There are drivers like
> sound/soc/mxs/mxs-pcm.c which call snd_dmaengine_pcm_register() with a
> config which has the prepare_slave_config callback unset. For these
> drivers dmaengine_pcm_hw_params() previously was a no-op. Now with this
> patch snd_dmaengine_pcm_prepare_slave_config() and
> dmaengine_slave_config() are called. At least for the mxs-pcm driver
> calling dmaengine_slave_config() will return -ENOSYS.
> 
> At least the "Check if this callback is set before using it" part is
> wrong, the callback is checked before using it with
> 
>          if (prepare_slave_config) {
>                  ...
>          }
> 
> I don't have any mxs hardware at hand to test this. I just stumbled upon
> the change of behaviour when rebasing
> https://patchwork.kernel.org/project/alsa-devel/patch/20220301122111.1073174-1-s.hauer@pengutronix.de/
> on current master.

You are right. I changed the behavior from:
if (pmc->config && !pcm->config->prepare_slave_config)
	<do nothing>
to:
if (pmc->config && !pcm->config->prepare_slave_config)
	snd_dmaengine_pcm_prepare_slave_config()

It was not intended and I agree that the commit message is not accurate. 
I guess some drivers might not need dmaengine_slave_config()...
However, in my case, for the mchp-pdmc driver, I do have pcm->config 
with pcm->config->prepare_slave_config NULL, but I still need 
snd_dmaengine_pcm_prepare_slave_config() to be called. Should we add a 
separate flag to call snd_dmaengine_pcm_prepare_slave_config() if 
pcm->config->prepare_slave_config is NULL?

Nice catch!

Best regards,
Codrin
Sascha Hauer April 20, 2022, 10:06 a.m. UTC | #3
On Wed, Apr 20, 2022 at 09:58:06AM +0000, Codrin.Ciubotariu@microchip.com wrote:
> On 20.04.2022 12:15, Sascha Hauer wrote:
> > EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> > 
> > Hi,
> 
> Hi Sascha,
> 
> > 
> > On Mon, Mar 07, 2022 at 02:21:57PM +0200, Codrin Ciubotariu wrote:
> >> Even if struct snd_dmaengine_pcm_config is used, prepare_slave_config()
> >> callback might not be set. Check if this callback is set before using it.
> >>
> >> Fixes: fa654e085300 ("ASoC: dmaengine-pcm: Provide default config")
> >> Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
> >> ---
> >>
> >> Changes in v2,v3:
> >>   - none
> >>
> >>   sound/soc/soc-generic-dmaengine-pcm.c | 6 +++---
> >>   1 file changed, 3 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/sound/soc/soc-generic-dmaengine-pcm.c b/sound/soc/soc-generic-dmaengine-pcm.c
> >> index 285441d6aeed..2ab2ddc1294d 100644
> >> --- a/sound/soc/soc-generic-dmaengine-pcm.c
> >> +++ b/sound/soc/soc-generic-dmaengine-pcm.c
> >> @@ -86,10 +86,10 @@ static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
> >>
> >>        memset(&slave_config, 0, sizeof(slave_config));
> >>
> >> -     if (!pcm->config)
> >> -             prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
> >> -     else
> >> +     if (pcm->config && pcm->config->prepare_slave_config)
> >>                prepare_slave_config = pcm->config->prepare_slave_config;
> >> +     else
> >> +             prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
> >>
> >>        if (prepare_slave_config) {
> >>                int ret = prepare_slave_config(substream, params, &slave_config);
> > 
> > I wonder if this patch is correct. There are drivers like
> > sound/soc/mxs/mxs-pcm.c which call snd_dmaengine_pcm_register() with a
> > config which has the prepare_slave_config callback unset. For these
> > drivers dmaengine_pcm_hw_params() previously was a no-op. Now with this
> > patch snd_dmaengine_pcm_prepare_slave_config() and
> > dmaengine_slave_config() are called. At least for the mxs-pcm driver
> > calling dmaengine_slave_config() will return -ENOSYS.
> > 
> > At least the "Check if this callback is set before using it" part is
> > wrong, the callback is checked before using it with
> > 
> >          if (prepare_slave_config) {
> >                  ...
> >          }
> > 
> > I don't have any mxs hardware at hand to test this. I just stumbled upon
> > the change of behaviour when rebasing
> > https://patchwork.kernel.org/project/alsa-devel/patch/20220301122111.1073174-1-s.hauer@pengutronix.de/
> > on current master.
> 
> You are right. I changed the behavior from:
> if (pmc->config && !pcm->config->prepare_slave_config)
> 	<do nothing>
> to:
> if (pmc->config && !pcm->config->prepare_slave_config)
> 	snd_dmaengine_pcm_prepare_slave_config()
> 
> It was not intended and I agree that the commit message is not accurate. 
> I guess some drivers might not need dmaengine_slave_config()...
> However, in my case, for the mchp-pdmc driver, I do have pcm->config 
> with pcm->config->prepare_slave_config NULL, but I still need 
> snd_dmaengine_pcm_prepare_slave_config() to be called. Should we add a 
> separate flag to call snd_dmaengine_pcm_prepare_slave_config() if 
> pcm->config->prepare_slave_config is NULL?

Other drivers set pcm->config->prepare_slave_config to
snd_dmaengine_pcm_prepare_slave_config() explicitly:

sound/soc/fsl/imx-pcm-dma.c:33: .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,

I think that's the way to go.

Regards,
Sascha
Codrin Ciubotariu April 20, 2022, 10:17 a.m. UTC | #4
On 20.04.2022 13:06, Sascha Hauer wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> On Wed, Apr 20, 2022 at 09:58:06AM +0000, Codrin.Ciubotariu@microchip.com wrote:
>> On 20.04.2022 12:15, Sascha Hauer wrote:
>>> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
>>>
>>> Hi,
>>
>> Hi Sascha,
>>
>>>
>>> On Mon, Mar 07, 2022 at 02:21:57PM +0200, Codrin Ciubotariu wrote:
>>>> Even if struct snd_dmaengine_pcm_config is used, prepare_slave_config()
>>>> callback might not be set. Check if this callback is set before using it.
>>>>
>>>> Fixes: fa654e085300 ("ASoC: dmaengine-pcm: Provide default config")
>>>> Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
>>>> ---
>>>>
>>>> Changes in v2,v3:
>>>>    - none
>>>>
>>>>    sound/soc/soc-generic-dmaengine-pcm.c | 6 +++---
>>>>    1 file changed, 3 insertions(+), 3 deletions(-)
>>>>
>>>> diff --git a/sound/soc/soc-generic-dmaengine-pcm.c b/sound/soc/soc-generic-dmaengine-pcm.c
>>>> index 285441d6aeed..2ab2ddc1294d 100644
>>>> --- a/sound/soc/soc-generic-dmaengine-pcm.c
>>>> +++ b/sound/soc/soc-generic-dmaengine-pcm.c
>>>> @@ -86,10 +86,10 @@ static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
>>>>
>>>>         memset(&slave_config, 0, sizeof(slave_config));
>>>>
>>>> -     if (!pcm->config)
>>>> -             prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
>>>> -     else
>>>> +     if (pcm->config && pcm->config->prepare_slave_config)
>>>>                 prepare_slave_config = pcm->config->prepare_slave_config;
>>>> +     else
>>>> +             prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
>>>>
>>>>         if (prepare_slave_config) {
>>>>                 int ret = prepare_slave_config(substream, params, &slave_config);
>>>
>>> I wonder if this patch is correct. There are drivers like
>>> sound/soc/mxs/mxs-pcm.c which call snd_dmaengine_pcm_register() with a
>>> config which has the prepare_slave_config callback unset. For these
>>> drivers dmaengine_pcm_hw_params() previously was a no-op. Now with this
>>> patch snd_dmaengine_pcm_prepare_slave_config() and
>>> dmaengine_slave_config() are called. At least for the mxs-pcm driver
>>> calling dmaengine_slave_config() will return -ENOSYS.
>>>
>>> At least the "Check if this callback is set before using it" part is
>>> wrong, the callback is checked before using it with
>>>
>>>           if (prepare_slave_config) {
>>>                   ...
>>>           }
>>>
>>> I don't have any mxs hardware at hand to test this. I just stumbled upon
>>> the change of behaviour when rebasing
>>> https://patchwork.kernel.org/project/alsa-devel/patch/20220301122111.1073174-1-s.hauer@pengutronix.de/
>>> on current master.
>>
>> You are right. I changed the behavior from:
>> if (pmc->config && !pcm->config->prepare_slave_config)
>>        <do nothing>
>> to:
>> if (pmc->config && !pcm->config->prepare_slave_config)
>>        snd_dmaengine_pcm_prepare_slave_config()
>>
>> It was not intended and I agree that the commit message is not accurate.
>> I guess some drivers might not need dmaengine_slave_config()...
>> However, in my case, for the mchp-pdmc driver, I do have pcm->config
>> with pcm->config->prepare_slave_config NULL, but I still need
>> snd_dmaengine_pcm_prepare_slave_config() to be called. Should we add a
>> separate flag to call snd_dmaengine_pcm_prepare_slave_config() if
>> pcm->config->prepare_slave_config is NULL?
> 
> Other drivers set pcm->config->prepare_slave_config to
> snd_dmaengine_pcm_prepare_slave_config() explicitly:
> 
> sound/soc/fsl/imx-pcm-dma.c:33: .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
> 
> I think that's the way to go.

That's more elegant, right. I will revert this patch and use your 
suggestion for the mchp-pdmc driver.

Thanks and best regards,
Codrin
diff mbox series

Patch

diff --git a/sound/soc/soc-generic-dmaengine-pcm.c b/sound/soc/soc-generic-dmaengine-pcm.c
index 285441d6aeed..2ab2ddc1294d 100644
--- a/sound/soc/soc-generic-dmaengine-pcm.c
+++ b/sound/soc/soc-generic-dmaengine-pcm.c
@@ -86,10 +86,10 @@  static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
 
 	memset(&slave_config, 0, sizeof(slave_config));
 
-	if (!pcm->config)
-		prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
-	else
+	if (pcm->config && pcm->config->prepare_slave_config)
 		prepare_slave_config = pcm->config->prepare_slave_config;
+	else
+		prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
 
 	if (prepare_slave_config) {
 		int ret = prepare_slave_config(substream, params, &slave_config);