[PATCH 2/3] ASoC: stm32: spdifrx: Fix refcount on sync failure
Olivier MOYSAN
olivier.moysan at foss.st.com
Mon Nov 30 09:18:00 PST 2026
Hi,
Thanks for your patches.
On 11/27/26 10:23, phucduc.bui at gmail.com wrote:
> From: bui duc phuc <phucduc.bui at gmail.com>
>
> Roll back the refcount when starting synchronization fails.
> Disable interrupts if no users remain.
>
> Signed-off-by: bui duc phuc <phucduc.bui at gmail.com>
> ---
> sound/soc/stm/stm32_spdifrx.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/sound/soc/stm/stm32_spdifrx.c b/sound/soc/stm/stm32_spdifrx.c
> index 467c3c583a00..ba6c715750aa 100644
> --- a/sound/soc/stm/stm32_spdifrx.c
> +++ b/sound/soc/stm/stm32_spdifrx.c
> @@ -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;
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:
--- a/sound/soc/stm/stm32_spdifrx.c
+++ b/sound/soc/stm/stm32_spdifrx.c
@@ -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
More information about the linux-arm-kernel
mailing list