ALSA: Fix XRUN handling of USB-audio implicit fb
cover ALSA: Fix XRUN handling of USB-audio implicit fb
Hi,
this is a patch set to cover a corner-case bug that happens with the USB-audio implicit feedback mode. When XRUN happens in the implicit fb full-duplex streams, the driver may get EPIPE error at prepare but re-issuing fails again. The essential fix is the 3rd patch but the former two are covering other corner cases I spotted (only theoretical).
Takashi
===
Takashi Iwai (3):
ALSA: pcm: Set missing stop_operating flag at undoing trigger start
ALSA: pcm: Handle XRUN at trigger START
ALSA: usb-audio: Workaround for XRUN at prepare
sound/core/pcm_native.c | 12 ++++++++++-- sound/usb/pcm.c | 13 +++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-)
Signature · 1 line
2.35.3
PATCH 1/3 ALSA: pcm: Set missing stop_operating flag at undoing trigger start
When a PCM trigger-start fails at snd_pcm_do_start(), PCM core tries to undo the action at snd_pcm_undo_start() by issuing the trigger STOP manually. At that point, we forgot to set the stop_operating flag, hence the sync-stop won't be issued at the next prepare or other calls.
This patch adds the missing stop_operating flag at snd_pcm_undo_start().
--- sound/core/pcm_native.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
sound/core/pcm_native.c +3 -1
@@ -1432,8 +1432,10 @@ static int snd_pcm_do_start(struct snd_pcm_substream *substream,static void snd_pcm_undo_start (struct snd_pcm_substream *substream,snd_pcm_state_t state) { -if (substream->runtime->trigger_master == substream) +if (substream->runtime->trigger_master == substream) { substream->ops->trigger (substream,SNDRV_PCM_TRIGGER_STOP ); + substream->runtime->stop_operating = true; + } } static void snd_pcm_post_start(struct snd_pcm_substream *substream,
Signature · 1 line
2.35.3
PATCH 2/3 ALSA: pcm: Handle XRUN at trigger START
In reply to Takashi IwaiWhen the driver returns -EPIPE for indicating an XRUN already at PCM trigger START, we should treat properly and set it to the XRUN state. Otherwise the state is missing and the application would try to issue trigger again without knowing that it's in an error state.
This is just for a theoretical bug, and it won't happen in most cases.
--- sound/core/pcm_native.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
sound/core/pcm_native.c +7 -1
@@ -1424,9 +1424,15 @@ static int snd_pcm_pre_start(struct snd_pcm_substream *substream,static int snd_pcm_do_start (struct snd_pcm_substream *substream,snd_pcm_state_t state) { +int err; +if (substream->runtime->trigger_master != substream)return 0 ; -return substream->ops->trigger (substream,SNDRV_PCM_TRIGGER_START ); + err = substream->ops->trigger (substream,SNDRV_PCM_TRIGGER_START ); +/* XRUN happened during the start */ +if (err == -EPIPE ) +__snd_pcm_set_state (substream->runtime,SNDRV_PCM_STATE_XRUN ); +return err; } static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
Signature · 1 line
2.35.3
PATCH 3/3 ALSA: usb-audio: Workaround for XRUN at prepare
In reply to Takashi IwaiUnder certain situations (typically in the implicit feedback mode), USB-audio driver starts a playback stream already at PCM prepare call even before the actual PCM trigger-START call. For implicit feedback mode, this effectively starts two streams for data and sync endpoints, and if a coupled sync stream gets XRUN at this point, it results in an error -EPIPE.
The problem is that currently we return -EPIPE error as is from the prepare. Then application tries to recover again via the prepare call, but it'll fail again because the sync-stop is missing. The sync-stop is missing because it's an internal trigger call (hence the PCM core isn't involved).
Since we'll need to re-issue the prepare in anyway when trapped into this pitfall, this patch attempts to address it in a bit different way; namely, the driver tries to prepare once again after syncing the stop manually by itself -- so applications don't see the internal error. At the second failure, we report the error as is, but this shouldn't happen in normal situations.
--- sound/usb/pcm.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
sound/usb/pcm.c +11 -2
@@ -604,6 +604,7 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)struct snd_pcm_runtime *runtime = substream->runtime;struct snd_usb_substream *subs = runtime->private_data;struct snd_usb_audio *chip = subs->stream->chip; +int retry =0 ;int ret; ret =snd_usb_lock_shutdown (chip);
@@ -614,6 +615,7 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) goto unlock; } + again:if (subs->sync_endpoint) { ret =snd_usb_endpoint_prepare (chip, subs->sync_endpoint);if (ret <0 )
@@ -638,9 +640,16 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) subs->lowlatency_playback =lowlatency_playback_available (runtime, subs);if (substream->stream ==SNDRV_PCM_STREAM_PLAYBACK && - !subs->lowlatency_playback) + !subs->lowlatency_playback) { ret =start_endpoints (subs); - +/* if XRUN happens at starting streams (possibly with implicit +* fb case), restart again, but only try once. +*/ +if (ret == -EPIPE && !retry++) { +sync_pending_stops (subs); + goto again; + } + } unlock:snd_usb_unlock_shutdown (chip);return ret;
Signature · 1 line
2.35.3