ASoC: stm32: spdifrx: Fix resource handling and startup
Subsystems (4)
cover ASoC: stm32: spdifrx: Fix resource handling and startup
From: bui duc phuc <[email protected]>
Hi all,
This series improves resource management and error handling in the STM32 SPDIFRX driver.
It also fixes the refcount handling on synchronization failure and ensures the clock is enabled before setting the substream pointer.
Compile-tested only.
Best regards,
Phuc
bui duc phuc (3):
ASoC: stm32: spdifrx: Use devm helpers and drop remove callback
ASoC: stm32: spdifrx: Fix refcount on sync failure
ASoC: stm32: spdifrx: Enable clock before setting substream
sound/soc/stm/stm32_spdifrx.c | 87 ++++++++++++----------------------- 1 file changed, 30 insertions(+), 57 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 ASoC: stm32: spdifrx: Use devm helpers and drop remove callback
From: bui duc phuc <[email protected]>
Use devm helpers to manage the DMA channel, DMA buffer, runtime PM, PCM and component registration.
This allows removing the remove callback and simplifies error handling in the probe function.
--- sound/soc/stm/stm32_spdifrx.c | 67 +++++++++-------------------------- 1 file changed, 16 insertions(+), 51 deletions(-)
sound/soc/stm/stm32_spdifrx.c +16 -51
@@ -396,24 +396,16 @@ static int stm32_spdifrx_dma_ctrl_register(struct device *dev, {int ret; - spdifrx->ctrl_chan =dma_request_chan (dev,"rx-ctrl" ); + spdifrx->ctrl_chan =devm_dma_request_chan (dev,"rx-ctrl" );if (IS_ERR (spdifrx->ctrl_chan))return dev_err_probe (dev,PTR_ERR (spdifrx->ctrl_chan),"dma_request_chan error\n" ); - spdifrx->dmab = devm_kzalloc_obj(dev, struct snd_dma_buffer); + spdifrx->dmab =snd_devm_alloc_pages (dev,SNDRV_DMA_TYPE_DEV_IRAM , +SPDIFRX_CSR_BUF_LENGTH );if (!spdifrx->dmab)return -ENOMEM ; - spdifrx->dmab->dev.type =SNDRV_DMA_TYPE_DEV_IRAM ; - spdifrx->dmab->dev.dev = dev; - ret =snd_dma_alloc_pages (spdifrx->dmab->dev.type, dev, -SPDIFRX_CSR_BUF_LENGTH , spdifrx->dmab); -if (ret <0 ) { -dev_err (dev,"snd_dma_alloc_pages returned error %d\n" , ret); -return ret; - } - spdifrx->slave_config.direction =DMA_DEV_TO_MEM ; spdifrx->slave_config.src_addr = (dma_addr_t)(spdifrx->phys_addr +STM32_SPDIFRX_CSR );
@@ -919,26 +911,10 @@ static int stm32_spdifrx_parse_of(struct platform_device *pdev,return 0 ; } -static void stm32_spdifrx_remove (struct platform_device *pdev) -{ -struct stm32_spdifrx_data *spdifrx =platform_get_drvdata (pdev); - -if (!IS_ERR (spdifrx->ctrl_chan)) -dma_release_channel (spdifrx->ctrl_chan); - -if (spdifrx->dmab) -snd_dma_free_pages (spdifrx->dmab); - -snd_dmaengine_pcm_unregister (&pdev->dev); -snd_soc_unregister_component (&pdev->dev); -pm_runtime_disable (&pdev->dev); -} -static int stm32_spdifrx_probe (struct platform_device *pdev) {struct stm32_spdifrx_data *spdifrx;struct reset_control *rst; -const struct snd_dmaengine_pcm_config *pcm_config =NULL ;u32 ver, idr;int ret;
@@ -978,46 +954,36 @@ static int stm32_spdifrx_probe(struct platform_device *pdev)udelay (2 );reset_control_deassert (rst); - pcm_config = &stm32_spdifrx_pcm_config; - ret =snd_dmaengine_pcm_register (&pdev->dev, pcm_config,0 ); -if (ret) -return ret; - - ret =snd_soc_register_component (&pdev->dev, - &stm32_spdifrx_component, - stm32_spdifrx_dai, -ARRAY_SIZE (stm32_spdifrx_dai)); -if (ret) { -snd_dmaengine_pcm_unregister (&pdev->dev); -return ret; - } - ret =stm32_spdifrx_dma_ctrl_register (&pdev->dev, spdifrx);if (ret) - goto error; +return ret; ret =regmap_read (spdifrx->regmap,STM32_SPDIFRX_IDR , &idr);if (ret) - goto error; +return ret;if (idr ==SPDIFRX_IPIDR_NUMBER ) { ret =regmap_read (spdifrx->regmap,STM32_SPDIFRX_VERR , &ver);if (ret) - goto error; +return ret;dev_dbg (&pdev->dev,"SPDIFRX version: %lu.%lu registered\n" ,FIELD_GET (SPDIFRX_VERR_MAJ_MASK , ver),FIELD_GET (SPDIFRX_VERR_MIN_MASK , ver)); } -pm_runtime_enable (&pdev->dev); + ret =devm_pm_runtime_enable (&pdev->dev); +if (ret) +return ret; -return ret; + ret =devm_snd_dmaengine_pcm_register (&pdev->dev, &stm32_spdifrx_pcm_config,0 ); +if (ret) +return ret; -error: -stm32_spdifrx_remove (pdev); - -return ret; +return devm_snd_soc_register_component (&pdev->dev, + &stm32_spdifrx_component, + stm32_spdifrx_dai, +ARRAY_SIZE (stm32_spdifrx_dai)); }MODULE_DEVICE_TABLE (of, stm32_spdifrx_ids);
@@ -1052,7 +1018,6 @@ static struct platform_driver stm32_spdifrx_driver = { .pm = pm_ptr(&stm32_spdifrx_pm_ops), }, .probe = stm32_spdifrx_probe, - .remove = stm32_spdifrx_remove, };module_platform_driver (stm32_spdifrx_driver);
Signature · 1 line
2.43.0
PATCH 2/3 ASoC: stm32: spdifrx: Fix refcount on sync failure
In reply to [email protected]From: bui duc phuc <[email protected]>
Roll back the refcount when starting synchronization fails. Disable interrupts if no users remain.
--- sound/soc/stm/stm32_spdifrx.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
sound/soc/stm/stm32_spdifrx.c +7 -1
@@ -358,9 +358,15 @@ static int stm32_spdifrx_start_sync(struct stm32_spdifrx_data *spdifrx) cr_mask |=SPDIFRX_CR_SPDIFEN_MASK ; ret =regmap_update_bits (spdifrx->regmap,STM32_SPDIFRX_CR , cr_mask, cr); -if (ret <0 ) +if (ret <0 ) {dev_err (&spdifrx->pdev->dev,"Failed to start synchronization\n" ); + spdifrx->refcount--; +if (!spdifrx->refcount) +regmap_update_bits (spdifrx->regmap, +STM32_SPDIFRX_IMR , +SPDIFRX_XIMR_MASK ,0 ); + } }return ret;
Signature · 1 line
2.43.0
PATCH 3/3 ASoC: stm32: spdifrx: Enable clock before setting substream
In reply to [email protected]From: bui duc phuc <[email protected]>
Enable the clock before assigning the substream to avoid leaving a stale substream pointer if clock enabling fails.
--- sound/soc/stm/stm32_spdifrx.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
sound/soc/stm/stm32_spdifrx.c +7 -5
@@ -753,14 +753,16 @@ static int stm32_spdifrx_startup(struct snd_pcm_substream *substream,struct stm32_spdifrx_data *spdifrx =snd_soc_dai_get_drvdata (cpu_dai);int ret; + ret =clk_prepare_enable (spdifrx->kclk); +if (ret) { +dev_err (&spdifrx->pdev->dev,"Enable kclk failed: %d\n" , ret); +return ret; + } +scoped_guard (spinlock_irqsave, &spdifrx->irq_lock) spdifrx->substream = substream; - ret =clk_prepare_enable (spdifrx->kclk); -if (ret) -dev_err (&spdifrx->pdev->dev,"Enable kclk failed: %d\n" , ret); - -return ret; +return 0 ; } static int stm32_spdifrx_hw_params(struct snd_pcm_substream *substream,
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 3/3] ASoC: stm32: spdifrx: Enable clock before setting substream
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ASoC: stm32: spdifrx: missing Fixes trailer on bug-fix commit --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ASoC: stm32: spdifrx: missing Fixes trailer on bug-fix commit --
commit d1f8e52fc45d0e44e5727d051ea0cae138dd34fd
Author: bui duc phuc <[email protected]>
Quoted 6 lines
ASoC: stm32: spdifrx: Enable clock before setting substream
Enable the clock before assigning the substream to avoid leaving a stale substream pointer if clock enabling fails.
Signed-off-by bui duc phuc <[email protected]>
[Severity: Low]
This fixes a bug introduced when the driver was added. Should the commit message include a Fixes tag?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH 2/3] ASoC: stm32: spdifrx: Fix refcount on sync failure
In reply to [email protected]Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ASoC: stm32: spdifrx: missing Fixes trailer for stm32_spdifrx_start_sync() fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ASoC: stm32: spdifrx: missing Fixes trailer for stm32_spdifrx_start_sync() fix
--
commit ae61d099c35451778ed63b763187aaa9cf5f2bbb
Author: bui duc phuc <[email protected]>
Quoted 6 lines
ASoC: stm32: spdifrx: Fix refcount on sync failure
Roll back the refcount when starting synchronization fails. Disable interrupts if no users remain.
Signed-off-by bui duc phuc <[email protected]>
[Severity: Low]
This fixes the refcount and interrupt mask leak in stm32_spdifrx_start_sync() introduced by commit 03e4d5d56fa5 ("ASoC:
stm32: Add SPDIFRX support"). Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/3] ASoC: stm32: spdifrx: Fix refcount on sync failure
Quoted 7 lines
[Severity: Low]
This fixes the refcount and interrupt mask leak instm32_spdifrx_start_sync()introduced by commit03e4d5d56fa5("ASoC:
stm32: Add SPDIFRX support"). Should this include:Fixes 03e4d5d56fa5 ("ASoC: stm32: Add SPDIFRX support")
I don't think it's particularly important for this low-severity issue, but I'm happy to add the Fixes tag if the maintainer thinks it's necessary.
I've been getting quite a lot of stable backport notifications for my previous patches,
so I'm trying to be a bit more selective with Fixes tags now. :)
Re: [PATCH 3/3] ASoC: stm32: spdifrx: Enable clock before setting substream
In reply to [email protected]Quoted 7 lines
[Severity: Low]
This fixes a bug introduced when the driver was added. Should the commit message include a Fixes tag?Fixes 03e4d5d56fa5 ("ASoC: stm32: Add SPDIFRX support")
I don't think it's particularly important for this low-severity issue, but I'm happy to add the Fixes tag if the maintainer thinks it's necessary.
I've been getting quite a lot of stable backport notifications for my previous patches,
so I'm trying to be a bit more selective with Fixes tags now. :)
Olivier MOYSAN
maintainerRe: [PATCH 2/3] ASoC: stm32: spdifrx: Fix refcount on sync failure
In reply to [email protected]Hi,
Thanks for your patches.
Quoted 21 lines · On 11/27/26 10:23, [email protected] wrote:
From: bui duc phuc <[email protected]>
Roll back the refcount when starting synchronization fails. Disable interrupts if no users remain.
Signed-off-by bui duc phuc <[email protected]>--- sound/soc/stm/stm32_spdifrx.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)sound/soc/stm/stm32_spdifrx.c +1
-1@@ -358,9 +358,15 @@ static int stm32_spdifrx_start_sync(struct stm32_spdifrx_data *spdifrx) cr_mask |=SPDIFRX_CR_SPDIFEN_MASK ; ret =regmap_update_bits (spdifrx->regmap,STM32_SPDIFRX_CR , cr_mask, cr); -if (ret <0 ) +if (ret <0 ) { dev_err(&spdifrx->pdev->dev,
sound/soc/stm/stm32_spdifrx.c static int stm32_spdifrx_start_sync(struct stm32_spdifrx_data *spdifrx) · line 372
Quoted 10 lines
sound/soc/stm/stm32_spdifrx.c +6
-0"Failed to start synchronization\n"); + spdifrx->refcount--; +if (!spdifrx->refcount) +regmap_update_bits (spdifrx->regmap, +STM32_SPDIFRX_IMR , +SPDIFRX_XIMR_MASK ,0 ); + } }return ret;
stm32_spdifrx_start_sync() is called in both control and data path.
The patch fixes the stm32_spdifrx_get_ctrl_data() path but it seems that it can break the stm32_spdifrx_trigger path.
If the trigger START fails (on SPDIFRX_CR update), ALSA calls trigger STOP and runs stm32_spdifrx_stop() a second time. The refcount goes to -1, so stm32_spdifrx_stop() returns early and the spdifrx is never disabled.
An alternate could be to ensure that stm32_spdifrx_stop() is called in stm32_spdifrx_get_ctrl_data() when stm32_spdifrx_start_sync() fails. Below is the suggested change:
sound/soc/stm/stm32_spdifrx.c +5 -0
@@ -322,16 +322,17 @@ static int stm32_spdifrx_start_sync(struct stm32_spdifrx_data *spdifrx) {int cr, cr_mask, imr, ret; +guard (spinlock_irqsave)(&spdifrx->lock); + +/* Callers always balance refcount with stm32_spdifrx_stop(). */ + spdifrx->refcount++; +/* Enable IRQs */ imr =SPDIFRX_IMR_IFEIE |SPDIFRX_IMR_SYNCDIE |SPDIFRX_IMR_PERRIE ; ret = regmap_update_bits(spdifrx->regmap, STM32_SPDIFRX_IMR,
imr, imr);
if (ret)
return ret;- guard(spinlock_irqsave)(&spdifrx->lock);
-
- spdifrx->refcount++;
-
regmap_read(spdifrx->regmap, STM32_SPDIFRX_CR, &cr);
if (!(cr & SPDIFRX_CR_SPDIFEN_MASK)) {@@ -480,7 +481,7 @@ static int stm32_spdifrx_get_ctrl_data(struct stm32_spdifrx_data *spdifrx)
ret = stm32_spdifrx_start_sync(spdifrx);
if (ret < 0)- goto end;
+ goto stop;
if
(wait_for_completion_interruptible_timeout(&spdifrx->cs_completion,
msecs_to_jiffies(100))
@@ -489,6 +490,7 @@ static int stm32_spdifrx_get_ctrl_data(struct stm32_spdifrx_data *spdifrx)
ret = -EAGAIN;
}+stop:
stm32_spdifrx_stop(spdifrx);
stm32_spdifrx_dma_ctrl_stop(spdifrx);I realize that cleanup path needs to be at least re-ordered. I will think about another patch to fix this.
Regards
Olivier