Skip to content

Commit

Permalink
ASoC: soc-pcm: test refcount before triggering
Browse files Browse the repository at this point in the history
On start/pause_release/resume, when more than one FE is connected to
the same BE, it's possible that the trigger is sent more than
once. This is not desirable, we only want to trigger a BE once, which
is straightforward to implement with a refcount.

For stop/pause/suspend, the problem is more complicated: the check
implemented in snd_soc_dpcm_can_be_free_stop() may fail due to a
conceptual deadlock when we trigger the BE before the FE. In this
case, the FE states have not yet changed, so there are corner cases
where the TRIGGER_STOP is never sent - the dual case of start where
multiple triggers might be sent.

This patch suggests an unconditional trigger in all cases, without
checking the FE states, using a refcount protected by the BE PCM
stream lock.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Link: https://lore.kernel.org/r/20211207173745.15850-6-pierre-louis.bossart@linux.intel.com
Signed-off-by: Mark Brown <broonie@kernel.org>
  • Loading branch information
Pierre-Louis Bossart authored and Mark Brown committed Dec 14, 2021
1 parent b2ae806 commit 848aedf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/sound/soc-dpcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ struct snd_soc_dpcm_runtime {
enum snd_soc_dpcm_state state;

int trigger_pending; /* trigger cmd + 1 if pending, 0 if not */

int be_start; /* refcount protected by BE stream pcm lock */
};

#define for_each_dpcm_fe(be, stream, _dpcm) \
Expand Down
53 changes: 42 additions & 11 deletions sound/soc/soc-pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
goto unwind;
}

be->dpcm[stream].be_start = 0;
be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
count++;
}
Expand Down Expand Up @@ -2105,34 +2105,53 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,

switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
if (!be->dpcm[stream].be_start &&
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
goto next;

be->dpcm[stream].be_start++;
if (be->dpcm[stream].be_start != 1)
goto next;

ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
be->dpcm[stream].be_start--;
goto next;
}

be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
break;
case SNDRV_PCM_TRIGGER_RESUME:
if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
goto next;

be->dpcm[stream].be_start++;
if (be->dpcm[stream].be_start != 1)
goto next;

ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
be->dpcm[stream].be_start--;
goto next;
}

be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
break;
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
goto next;

be->dpcm[stream].be_start++;
if (be->dpcm[stream].be_start != 1)
goto next;

ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
be->dpcm[stream].be_start--;
goto next;
}

be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
break;
Expand All @@ -2141,38 +2160,50 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
goto next;

if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
be->dpcm[stream].be_start--;

if (be->dpcm[stream].be_start != 0)
goto next;

ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
be->dpcm[stream].be_start++;
goto next;
}

be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
break;
case SNDRV_PCM_TRIGGER_SUSPEND:
if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
goto next;

if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
be->dpcm[stream].be_start--;
if (be->dpcm[stream].be_start != 0)
goto next;

ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
be->dpcm[stream].be_start++;
goto next;
}

be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
break;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
goto next;

if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
be->dpcm[stream].be_start--;
if (be->dpcm[stream].be_start != 0)
goto next;

ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
be->dpcm[stream].be_start++;
goto next;
}

be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
break;
Expand Down

0 comments on commit 848aedf

Please sign in to comment.