Skip to contentSkip to conversation

ALSA: pcm: Release paused streams before suspend if resume is not supported

PATCH In review Peter Ujfalusi 3 participants 36 messages Apr 1, 2025 – Apr 8, 2025 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: pcm: Release paused streams before suspend if resume is not supported

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

From this point user space will do the normal (hw_params) prepare and START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

To fix this issue, release the paused stream before handling the suspend if the resume is not supported.
This will ensure that the stream is properly suspended for suspend entry. On resume no attempt will be taken to resume the suspended stream as the resume is not supported, userspace will eventually going restart the audio.

---
 sound/core/pcm_native.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
sound/core/pcm_native.c +13 -0
index 6c2b6a62d9d2..a16d15ee98fa 100644
@@ -1730,7 +1730,20 @@ static const struct action_ops snd_pcm_action_suspend = {
  */
 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
 {
+	struct snd_pcm_runtime *runtime = substream->runtime;
+
 	guard(pcm_stream_lock_irqsave)(substream);
+	/*
+	 * Release the paused stream before suspending if resume is not
+	 * supported, because:
+	 * PAUSED streams will not receive the suspend trigger and since the
+	 * driver does not support resuming streams, it is not going to be able
+	 * to handle them properly when the system resumes.
+	 */
+	if (runtime->state == SNDRV_PCM_STATE_PAUSED &&
+	    !(runtime->info & SNDRV_PCM_INFO_RESUME))
+		snd_pcm_pause(substream, false);
+
 	return snd_pcm_action(&snd_pcm_action_suspend, substream,
 			      ACTION_ARG_IGNORE);
 }
Signature · 1 line
2.49.0
#1 lore raw
Quoted 17 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

From this point user space will do the normal (hw_params) prepare and START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

To fix this issue, release the paused stream before handling the suspend if the resume is not supported.
This will ensure that the stream is properly suspended for suspend entry. On resume no attempt will be taken to resume the suspended stream as the resume is not supported, userspace will eventually going restart the audio.

Quoted 4 lines

Applied now. Thanks.

Takashi

maintainer
#2 lore raw
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

Quoted 6 lines
Quoted 6 lines

To fix this issue, release the paused stream before handling the suspend if the resume is not supported.
This will ensure that the stream is properly suspended for suspend entry. On resume no attempt will be taken to resume the suspended stream as the resume is not supported, userspace will eventually going restart the audio.

Quoted 6 lines
Quoted 4 lines

Applied now. Thanks.

I would consider revert if my objection is appropriate.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#3 lore raw
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

To fix this issue, release the paused stream before handling the suspend if the resume is not supported.
This will ensure that the stream is properly suspended for suspend entry. On resume no attempt will be taken to resume the suspended stream as the resume is not supported, userspace will eventually going restart the audio.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Applied now. Thanks.

I would consider revert if my objection is appropriate.

OK, I dropped the patch for now until we get agreement.

thanks,

Takashi

maintainer
#4 lore raw
Quoted 39 lines · Takashi Iwai
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

Quoted 4 lines

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Thanks for those hints. I don't think that the pause release is sufficient for this case - the stream must be stopped, too. Actually, all mentioned prepare/drop/drain routines immediately changes state when the pause is released.

Maybe a new trigger may be added to notify drivers like:

sound/core/pcm_native.c +3 -1
@@ -1694,8 +1694,10 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
        struct snd_pcm_runtime *runtime = substream->runtime;
        if (runtime->trigger_master != substream)
                return 0;
-       if (! snd_pcm_running(substream))
+       if (! snd_pcm_running(substream)) {
+               substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND_WHEN_IDLE);
                return 0;
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
        runtime->stop_operating = true;
        return 0; /* suspend unconditionally */

With this, the states won't be changed - the driver without resume support can shut down all necessary things.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#5 lore raw
Quoted 46 lines · Jaroslav Kysela
Quoted 39 lines · Takashi Iwai
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

Quoted 4 lines

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Quoted 4 lines

Thanks for those hints. I don't think that the pause release is sufficient for this case - the stream must be stopped, too. Actually, all mentioned prepare/drop/drain routines immediately changes state when the pause is released.

Actually, this patch follows the same pattern, too. It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

Quoted 17 lines

Maybe a new trigger may be added to notify drivers like:

sound/core/pcm_native.c +3 -1
@@ -1694,8 +1694,10 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
        struct snd_pcm_runtime *runtime = substream->runtime;
        if (runtime->trigger_master != substream)
                return 0;
-       if (! snd_pcm_running(substream))
+       if (! snd_pcm_running(substream)) {
+               substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND_WHEN_IDLE);
                return 0;
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
        runtime->stop_operating = true;
        return 0; /* suspend unconditionally */
Quoted 2 lines

With this, the states won't be changed - the driver without resume support can shut down all necessary things.

That's another possibility, yes. Though, IMO, it makes the pause-handling a bit more cumbersome. With Peter's proposal, the pause state change is always paired, so the driver doesn't consider about that too much; that's the biggest merit.

But I'm all ears about this issue. Let's see.

(BTW, I thought the state change PAUSED -> XRUN were possible, but actually it's not; this is another corner case to be addressed, I guess.)

Takashi

#6 lore raw

Hi,

Quoted 48 lines · Takashi Iwai
Quoted 46 lines · Jaroslav Kysela
Quoted 43 lines · Takashi Iwai
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Quoted 8 lines
Quoted 4 lines

Thanks for those hints. I don't think that the pause release is sufficient for this case - the stream must be stopped, too. Actually, all mentioned prepare/drop/drain routines immediately changes state when the pause is released.

Actually, this patch follows the same pattern, too. It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

My original RFC patch did the PAUSE_RELEASE followed by STOP in SOF code to clear out the paused streams on suspend:
https://lore.kernel.org/linux-sound/[email protected]/

Which if I convert it on top of the applied patch would look like this in core (+ pending comment update):

sound/core/pcm_native.c +0 -0
index a16d15ee98fa..c1bd993c650c 100644
@@ -1741,8 +1741,10 @@ static int snd_pcm_suspend(struct

snd_pcm_substream *substream)
* to handle them properly when the system resumes. */
if (runtime->state == SNDRV_PCM_STATE_PAUSED &&
- !(runtime->info & SNDRV_PCM_INFO_RESUME))
+ !(runtime->info & SNDRV_PCM_INFO_RESUME)) {
snd_pcm_pause(substream, false);
+ return snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
+ }

return snd_pcm_action(&snd_pcm_action_suspend, substream,
ACTION_ARG_IGNORE);

But what I like about the current way is that it is bringing the paused stream handling in line with other triggers as Takashi-san mentioned. Paused cannot be directly stopped for example, it needs to be released, then stopped. Drivers don't need special state machines to handle all permutations of state changes.

Quoted 17 lines
Quoted 17 lines

Maybe a new trigger may be added to notify drivers like:

sound/core/pcm_native.c +3 -1
@@ -1694,8 +1694,10 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
        struct snd_pcm_runtime *runtime = substream->runtime;
        if (runtime->trigger_master != substream)
                return 0;
-       if (! snd_pcm_running(substream))
+       if (! snd_pcm_running(substream)) {
+               substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND_WHEN_IDLE);
                return 0;
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
        runtime->stop_operating = true;
        return 0; /* suspend unconditionally */
Quoted 7 lines
Quoted 2 lines

With this, the states won't be changed - the driver without resume support can shut down all necessary things.

That's another possibility, yes. Though, IMO, it makes the pause-handling a bit more cumbersome. With Peter's proposal, the pause state change is always paired, so the driver doesn't consider about that too much; that's the biggest merit.

I agree with this sentiment. If we do something like this then all drivers (which might have similar issue, a note on this later) needs to be updated and have some magic done, and in case of SOF that would be:
substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE); substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP/SUSPEND);

to handle the messy FE/BE/DSP sequencing.

Quoted 5 lines

But I'm all ears about this issue. Let's see.

(BTW, I thought the state change PAUSED -> XRUN were possible, but actually it's not; this is another corner case to be addressed, I guess.)

Overnight I was thinking of how the suspend while a stream is handled or can be handled if the PCM supports RESUME.
The paused stream is not a stopped stream and it is not receiving SUSPEND as it is not running either, but a paused stream might be only having it's DMA trigger source disabled to stop the hw_ptr moving while a stopped one have the DMA disabled along with all analog/digital components.
I'm not sure if sending the SUSPEND trigger to a paused stream is a solution either as that will surely need different handling as it is not expected currently (and again SOF have a delicate dance around triggers starting in ASoC level, but might work).

Note on the pause in general: for few years now pause is in reality a niche feature and most drivers will never see it used. PA. PW, Chrome, Android don't use it much or at all, so hitting a suspend while the stream is paused is a CI thing mostly. mplayer/mpv (kodi might as well) do pause directly when using ALSA and the PCM supports it, but they work w/o SNDRV_PCM_INFO_PAUSE just fine.

Signature · 1 line
Péter
maintainer
#7 lore raw
Quoted 4 lines · Péter Ujfalusi

I'm not sure if sending the SUSPEND trigger to a paused stream is a solution either as that will surely need different handling as it is not expected currently (and again SOF have a delicate dance around triggers starting in ASoC level, but might work).

It's unfortunate, but things should be resolved in the appropriate code levels IMHO. The core code should allow all mechanisms.

Quoted 6 lines

Note on the pause in general: for few years now pause is in reality a niche feature and most drivers will never see it used. PA. PW, Chrome, Android don't use it much or at all, so hitting a suspend while the stream is paused is a CI thing mostly. mplayer/mpv (kodi might as well) do pause directly when using ALSA and the PCM supports it, but they work w/o SNDRV_PCM_INFO_PAUSE just fine.

We are providing API, so we should not judge, if things are widely used. I believe that some users use the perfect pause instead full stream restart.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
maintainer
#8 lore raw
In reply to Takashi Iwai
Quoted 48 lines · Takashi Iwai
Quoted 46 lines · Jaroslav Kysela
Quoted 43 lines · Takashi Iwai
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Quoted 8 lines
Quoted 4 lines

Thanks for those hints. I don't think that the pause release is sufficient for this case - the stream must be stopped, too. Actually, all mentioned prepare/drop/drain routines immediately changes state when the pause is released.

Actually, this patch follows the same pattern, too. It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

The previous state (suspended_state) will be confusing from the application with the proposed patch, because there will be RUNNING state instead PAUSED. This previous state is exported via API.

The pause release / stop patterns provide a short time window to process some samples which may cause unwanted pops (especially for playback). So perhaps, we can address this in core (new triggers like PAUSE_RELEASE_AND_STOP), too.

Quoted 17 lines
Quoted 17 lines

Maybe a new trigger may be added to notify drivers like:

sound/core/pcm_native.c +3 -1
@@ -1694,8 +1694,10 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
         struct snd_pcm_runtime *runtime = substream->runtime;
         if (runtime->trigger_master != substream)
                 return 0;
-       if (! snd_pcm_running(substream))
+       if (! snd_pcm_running(substream)) {
+               substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND_WHEN_IDLE);
                 return 0;
+       }
         substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
         runtime->stop_operating = true;
         return 0; /* suspend unconditionally */
Quoted 7 lines
Quoted 2 lines

With this, the states won't be changed - the driver without resume support can shut down all necessary things.

That's another possibility, yes. Though, IMO, it makes the pause-handling a bit more cumbersome. With Peter's proposal, the pause state change is always paired, so the driver doesn't consider about that too much; that's the biggest merit.

It depends, if we agree that releasing pause is a extra thing to do when we know that this step is just to minimize state changes for drivers.

For my view, the drivers may also receive those triggers:

SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP
SNDRV_PCM_TRIGGER_SUSPEND_IN_PAUSE
SNDRV_PCM_TRIGGER_RELEASE_IN_PAUSE

This will allow to handle all states properly without any side effects (like short unwanted DMA transfers).

The drivers should probably activate those triggers conditionally to not break current state sequences.

Quoted 3 lines

(BTW, I thought the state change PAUSED -> XRUN were possible, but actually it's not; this is another corner case to be addressed, I guess.)

Which code (lines) do you mean?

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#9 lore raw
Quoted 55 lines · Jaroslav Kysela
Quoted 53 lines · Takashi Iwai
Quoted 50 lines · Jaroslav Kysela
Quoted 39 lines · Takashi Iwai
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

Quoted 4 lines

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Thanks for those hints. I don't think that the pause release is sufficient for this case - the stream must be stopped, too. Actually, all mentioned prepare/drop/drain routines immediately changes state when the pause is released.

Quoted 7 lines
Quoted 3 lines

Actually, this patch follows the same pattern, too. It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

The previous state (suspended_state) will be confusing from the application with the proposed patch, because there will be RUNNING state instead PAUSED. This previous state is exported via API.

No, it won't happen. The condition of the new behavior is only when SNDRV_PCM_INFO_RESUME isn't set -- that is, no resume is possible, hence no state recovery happens after resume. From the application POV, it won't change.

Quoted 4 lines

The pause release / stop patterns provide a short time window to process some samples which may cause unwanted pops (especially for playback). So perhaps,
we can address this in core (new triggers like PAUSE_RELEASE_AND_STOP), too.

That's true. To be noted, the very same thing is applied to other operations like snd_pcm_drop() or snd_pcm_prepare(), so it's not only about the case of suspend.

Quoted 35 lines
Quoted 17 lines
Quoted 17 lines

Maybe a new trigger may be added to notify drivers like:

sound/core/pcm_native.c +3 -1
@@ -1694,8 +1694,10 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
         struct snd_pcm_runtime *runtime = substream->runtime;
         if (runtime->trigger_master != substream)
                 return 0;
-       if (! snd_pcm_running(substream))
+       if (! snd_pcm_running(substream)) {
+               substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND_WHEN_IDLE);
                 return 0;
+       }
         substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
         runtime->stop_operating = true;
         return 0; /* suspend unconditionally */
Quoted 7 lines
Quoted 2 lines

With this, the states won't be changed - the driver without resume support can shut down all necessary things.

That's another possibility, yes. Though, IMO, it makes the pause-handling a bit more cumbersome. With Peter's proposal, the pause state change is always paired, so the driver doesn't consider about that too much; that's the biggest merit.

It depends, if we agree that releasing pause is a extra thing to do when we know that this step is just to minimize state changes for drivers.

For my view, the drivers may also receive those triggers:

SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP
SNDRV_PCM_TRIGGER_SUSPEND_IN_PAUSE
SNDRV_PCM_TRIGGER_RELEASE_IN_PAUSE

Quoted 5 lines

This will allow to handle all states properly without any side effects (like short unwanted DMA transfers).

The drivers should probably activate those triggers conditionally to not break current state sequences.

Hmm, that looks too complex, IMO.

Another slightly more straightforward fix would be just to allow the direct state transition from PAUSED to any state. Then the remaining piece is all about driver implementations. And, this can be conditionally operated, e.g. only when some extra flag is set. When no flag is set, we keep applying PAUSE_RELEASE before the transition like now, including Peter's patch, that can work generically (but not always ideally).

Quoted 5 lines
Quoted 3 lines

(BTW, I thought the state change PAUSED -> XRUN were possible, but actually it's not; this is another corner case to be addressed, I guess.)

Which code (lines) do you mean?

snd_pcm_xrun() and snd_pcm_stop_xrun(). As far as I read the code, the state transition won't happen when it's in PAUSED state.

thanks,

Takashi

maintainer
#10 lore raw
Quoted 61 lines · Takashi Iwai
Quoted 59 lines · Jaroslav Kysela
Quoted 56 lines · Takashi Iwai
Quoted 46 lines · Jaroslav Kysela
Quoted 39 lines · Takashi Iwai
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

Quoted 4 lines

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Quoted 4 lines

Thanks for those hints. I don't think that the pause release is sufficient for this case - the stream must be stopped, too. Actually, all mentioned prepare/drop/drain routines immediately changes state when the pause is released.

Actually, this patch follows the same pattern, too. It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

Quoted 8 lines
Quoted 3 lines

The previous state (suspended_state) will be confusing from the application with the proposed patch, because there will be RUNNING state instead PAUSED. This previous state is exported via API.

No, it won't happen. The condition of the new behavior is only when SNDRV_PCM_INFO_RESUME isn't set -- that is, no resume is possible, hence no state recovery happens after resume. From the application POV, it won't change.

The suspended_state can be obtained in snd_pcm_status64(). With Peter's patch, there will be RUNNING instead PAUSED, don't?

Quoted 8 lines
Quoted 4 lines

The pause release / stop patterns provide a short time window to process some samples which may cause unwanted pops (especially for playback). So perhaps,
we can address this in core (new triggers like PAUSE_RELEASE_AND_STOP), too.

That's true. To be noted, the very same thing is applied to other operations like snd_pcm_drop() or snd_pcm_prepare(), so it's not only about the case of suspend.

Yes, I meant all places.

Quoted 43 lines
Quoted 35 lines
Quoted 17 lines
Quoted 17 lines

Maybe a new trigger may be added to notify drivers like:

sound/core/pcm_native.c +3 -1
@@ -1694,8 +1694,10 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
          struct snd_pcm_runtime *runtime = substream->runtime;
          if (runtime->trigger_master != substream)
                  return 0;
-       if (! snd_pcm_running(substream))
+       if (! snd_pcm_running(substream)) {
+               substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND_WHEN_IDLE);
                  return 0;
+       }
          substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
          runtime->stop_operating = true;
          return 0; /* suspend unconditionally */
Quoted 7 lines
Quoted 2 lines

With this, the states won't be changed - the driver without resume support can shut down all necessary things.

That's another possibility, yes. Though, IMO, it makes the pause-handling a bit more cumbersome. With Peter's proposal, the pause state change is always paired, so the driver doesn't consider about that too much; that's the biggest merit.

It depends, if we agree that releasing pause is a extra thing to do when we know that this step is just to minimize state changes for drivers.

For my view, the drivers may also receive those triggers:

SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP
SNDRV_PCM_TRIGGER_SUSPEND_IN_PAUSE
SNDRV_PCM_TRIGGER_RELEASE_IN_PAUSE

Quoted 5 lines

This will allow to handle all states properly without any side effects (like short unwanted DMA transfers).

The drivers should probably activate those triggers conditionally to not break current state sequences.

Hmm, that looks too complex, IMO.

Quoted 7 lines

Another slightly more straightforward fix would be just to allow the direct state transition from PAUSED to any state. Then the remaining piece is all about driver implementations. And, this can be conditionally operated, e.g. only when some extra flag is set. When no flag is set, we keep applying PAUSE_RELEASE before the transition like now, including Peter's patch, that can work generically (but not always ideally).

It looks also feasible. My proposal just allows the straight state identification from the trigger value in the drivers.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

(BTW, I thought the state change PAUSED -> XRUN were possible, but actually it's not; this is another corner case to be addressed, I guess.)

Which code (lines) do you mean?

snd_pcm_xrun() and snd_pcm_stop_xrun(). As far as I read the code, the state transition won't happen when it's in PAUSED state.

Yes, paused state should be handled there, too. But the pause release operation looks really suspicious for xrun.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#11 lore raw
Quoted 67 lines · Jaroslav Kysela
Quoted 65 lines · Takashi Iwai
Quoted 62 lines · Jaroslav Kysela
Quoted 48 lines · Takashi Iwai
Quoted 46 lines · Jaroslav Kysela
Quoted 43 lines · Takashi Iwai
Quoted 24 lines · Jaroslav Kysela
Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Peter Ujfalusi

Streams moved to SUSPENDED state from PAUSED without trigger. If a stream does not support RESUME then on system resume the RESUME trigger is not sent, the stream's state and suspended_state remains untouched. When the user space releases the pause then the core will reject this because the state of the stream is _not_ PAUSED, it is still SUSPENDED.

Quoted 3 lines
Quoted 3 lines

From this point user space will do the normal (hw_params) prepare and
START, PAUSE_RELEASE trigger will not be sent by the core after the system has resumed.

Why you expect to see PAUSE_RELEASE trigger before SUSPEND trigger when resume is not supported ? I think that the driver has complete information:

PAUSE -> SUSPEND [-> no RESUME]

The driver should stay in the suspend state until a new stream re-initialization is invoked.

Quoted 6 lines

It looks like that other logic should be moved to the end driver (if some parts must be reinitialized in the suspend phase when the resume is not supported). I don't think that this is job for PCM core nor SoC PCM core routines.

The SUSPEND trigger is always invoked, isn't?

No, that's a part of the problems. The ops->suspend is called only for the running state, while at snd_pcm_post_suspend(), the state is changed to SUSPENDED. The original state is saved as suspended_state, and this can be restored via snd_pcm_resume(), but only if the driver supports the full resume.

We may allow calling ops->suspend also for PAUSED state, too. But then each driver would need to have to handle this state change, too. Currently, when snd_pcm_prepare(), *_drop() or *_drain() is called at PAUSED state, the core automatically releases the pause beforehand.

Quoted 8 lines
Quoted 4 lines

Thanks for those hints. I don't think that the pause release is sufficient for this case - the stream must be stopped, too. Actually, all mentioned prepare/drop/drain routines immediately changes state when the pause is released.

Actually, this patch follows the same pattern, too. It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

The previous state (suspended_state) will be confusing from the application with the proposed patch, because there will be RUNNING state instead PAUSED. This previous state is exported via API.

Quoted 7 lines
Quoted 4 lines

No, it won't happen. The condition of the new behavior is only when SNDRV_PCM_INFO_RESUME isn't set -- that is, no resume is possible, hence no state recovery happens after resume. From the application POV, it won't change.

The suspended_state can be obtained in snd_pcm_status64(). With Peter's patch, there will be RUNNING instead PAUSED, don't?

I see, that should be better fixed.

Quoted 62 lines
Quoted 8 lines
Quoted 4 lines

The pause release / stop patterns provide a short time window to process some samples which may cause unwanted pops (especially for playback). So perhaps,
we can address this in core (new triggers like PAUSE_RELEASE_AND_STOP), too.

That's true. To be noted, the very same thing is applied to other operations like snd_pcm_drop() or snd_pcm_prepare(), so it's not only about the case of suspend.

Yes, I meant all places.

Quoted 43 lines
Quoted 35 lines
Quoted 17 lines
Quoted 17 lines

Maybe a new trigger may be added to notify drivers like:

sound/core/pcm_native.c +3 -1
@@ -1694,8 +1694,10 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
          struct snd_pcm_runtime *runtime = substream->runtime;
          if (runtime->trigger_master != substream)
                  return 0;
-       if (! snd_pcm_running(substream))
+       if (! snd_pcm_running(substream)) {
+               substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND_WHEN_IDLE);
                  return 0;
+       }
          substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
          runtime->stop_operating = true;
          return 0; /* suspend unconditionally */
Quoted 7 lines
Quoted 2 lines

With this, the states won't be changed - the driver without resume support can shut down all necessary things.

That's another possibility, yes. Though, IMO, it makes the pause-handling a bit more cumbersome. With Peter's proposal, the pause state change is always paired, so the driver doesn't consider about that too much; that's the biggest merit.

It depends, if we agree that releasing pause is a extra thing to do when we know that this step is just to minimize state changes for drivers.

For my view, the drivers may also receive those triggers:

SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP
SNDRV_PCM_TRIGGER_SUSPEND_IN_PAUSE
SNDRV_PCM_TRIGGER_RELEASE_IN_PAUSE

Quoted 5 lines

This will allow to handle all states properly without any side effects (like short unwanted DMA transfers).

The drivers should probably activate those triggers conditionally to not break current state sequences.

Hmm, that looks too complex, IMO.

Quoted 7 lines

Another slightly more straightforward fix would be just to allow the direct state transition from PAUSED to any state. Then the remaining piece is all about driver implementations. And, this can be conditionally operated, e.g. only when some extra flag is set. When no flag is set, we keep applying PAUSE_RELEASE before the transition like now, including Peter's patch, that can work generically (but not always ideally).

Quoted 2 lines

It looks also feasible. My proposal just allows the straight state identification from the trigger value in the drivers.

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

(BTW, I thought the state change PAUSED -> XRUN were possible, but actually it's not; this is another corner case to be addressed, I guess.)

Which code (lines) do you mean?

snd_pcm_xrun() and snd_pcm_stop_xrun(). As far as I read the code, the state transition won't happen when it's in PAUSED state.

Yes, paused state should be handled there, too. But the pause release operation looks really suspicious for xrun.

Yeah, I noticed it after sending out my previous post, too. The engine is stopped, so practically the XRUN must not happen. I believe we can forget about that.

thanks,

Takashi

maintainer
#12 lore raw
Quoted 25 lines · Takashi Iwai
Quoted 22 lines
Quoted 14 lines
Quoted 11 lines

For my view, the drivers may also receive those triggers:

SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP
SNDRV_PCM_TRIGGER_SUSPEND_IN_PAUSE
SNDRV_PCM_TRIGGER_RELEASE_IN_PAUSE

This will allow to handle all states properly without any side effects (like short unwanted DMA transfers).

The drivers should probably activate those triggers conditionally to not break current state sequences.

Hmm, that looks too complex, IMO.

Quoted 7 lines

Another slightly more straightforward fix would be just to allow the direct state transition from PAUSED to any state. Then the remaining piece is all about driver implementations. And, this can be conditionally operated, e.g. only when some extra flag is set. When no flag is set, we keep applying PAUSE_RELEASE before the transition like now, including Peter's patch, that can work generically (but not always ideally).

Quoted 2 lines

It looks also feasible. My proposal just allows the straight state identification from the trigger value in the drivers.

Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#13 lore raw
Quoted 35 lines · Jaroslav Kysela
Quoted 25 lines · Takashi Iwai
Quoted 22 lines
Quoted 14 lines
Quoted 11 lines

For my view, the drivers may also receive those triggers:

SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP
SNDRV_PCM_TRIGGER_SUSPEND_IN_PAUSE
SNDRV_PCM_TRIGGER_RELEASE_IN_PAUSE

This will allow to handle all states properly without any side effects (like short unwanted DMA transfers).

The drivers should probably activate those triggers conditionally to not break current state sequences.

Hmm, that looks too complex, IMO.

Quoted 7 lines

Another slightly more straightforward fix would be just to allow the direct state transition from PAUSED to any state. Then the remaining piece is all about driver implementations. And, this can be conditionally operated, e.g. only when some extra flag is set. When no flag is set, we keep applying PAUSE_RELEASE before the transition like now, including Peter's patch, that can work generically (but not always ideally).

Quoted 2 lines

It looks also feasible. My proposal just allows the straight state identification from the trigger value in the drivers.

Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

Quoted 4 lines

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

thanks,

Takashi

#14 lore raw
Quoted 12 lines · Takashi Iwai
Quoted 11 lines
Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

Quoted 4 lines

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

But what state that we move after we reset the PAUSE? is it RUNNING (iow, equals to PAUSE_RELEASE) or something else?

In case of suspend while the state is paused we will:
TRIGGER_PAUSE_RESET followed by STOP or SUSPEND?

Most drivers are doing the same thing for STOP and SUSPEND trigger, however it is possible to do extra steps for SUSPEND to make sure that the whole system is in a lowest power state.

Signature · 1 line
Péter
#15 lore raw
Quoted 16 lines · Péter Ujfalusi
Quoted 12 lines · Takashi Iwai
Quoted 11 lines
Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

Quoted 7 lines
Quoted 4 lines

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

But what state that we move after we reset the PAUSE? is it RUNNING (iow, equals to PAUSE_RELEASE) or something else?

It'll be temporarily set to RUNNING.

Quoted 2 lines

In case of suspend while the state is paused we will:
TRIGGER_PAUSE_RESET followed by STOP or SUSPEND?

For suspend, PAUSE_RESET, then SUSPEND.
For dropping, PAUSE_RESET, then STOP.

Quoted 3 lines

Most drivers are doing the same thing for STOP and SUSPEND trigger, however it is possible to do extra steps for SUSPEND to make sure that the whole system is in a lowest power state.

Yes, that's the reason I proposed PAUSE_RESET. Then the driver can handle the following stop or suspend operation differently.

That said, PAUSE_RESET is just another variant of PAUSE_RELEASE. But this shouldn't actually enable the audio output again, but prepare for the next SUSPEND or STOP trigger.

Takashi

#16 lore raw
Quoted 26 lines · Takashi Iwai
Quoted 16 lines · Péter Ujfalusi
Quoted 12 lines · Takashi Iwai
Quoted 11 lines
Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

Quoted 7 lines
Quoted 4 lines

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

But what state that we move after we reset the PAUSE? is it RUNNING (iow, equals to PAUSE_RELEASE) or something else?

Quoted 7 lines

It'll be temporarily set to RUNNING.

Quoted 2 lines

In case of suspend while the state is paused we will:
TRIGGER_PAUSE_RESET followed by STOP or SUSPEND?

For suspend, PAUSE_RESET, then SUSPEND.
For dropping, PAUSE_RESET, then STOP.

OK, but this is still the same as PAUSE_RELEASE+SUSPEND when it comes to suspended_state, no?

I mean you need to move to RUNNING to be suspend, or stop to skip the suspend.

Quoted 10 lines
Quoted 3 lines

Most drivers are doing the same thing for STOP and SUSPEND trigger, however it is possible to do extra steps for SUSPEND to make sure that the whole system is in a lowest power state.

Yes, that's the reason I proposed PAUSE_RESET. Then the driver can handle the following stop or suspend operation differently.

That said, PAUSE_RESET is just another variant of PAUSE_RELEASE. But this shouldn't actually enable the audio output again, but prepare for the next SUSPEND or STOP trigger.

I'm not sure how this would be seen in ASoC level. Surely we need to have flag per components that it is handling PAUSE_RESET as codecs would like don't want to do it or they migh, hm.
But ASoC maintains another set of state for the dpcm:
SND_SOC_DPCM_STATE_* and it has this as well among other state-machine things:
case SNDRV_PCM_TRIGGER_SUSPEND:

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

So, this is dpcm level, but one component can support PAUSE_RESET while the other does not and the transitions are not too clear to me.

But the idea of resurrecting a paused stream with mute held is not bad.

I'm still uncertain how this actually works now on systems where RESUME is supported and we suspend while a PCM is in paused...

Signature · 1 line
Péter
#17 lore raw
Quoted 35 lines · Péter Ujfalusi
Quoted 31 lines · Takashi Iwai
Quoted 16 lines · Péter Ujfalusi
Quoted 12 lines · Takashi Iwai
Quoted 11 lines
Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

Quoted 7 lines
Quoted 4 lines

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

But what state that we move after we reset the PAUSE? is it RUNNING (iow, equals to PAUSE_RELEASE) or something else?

It'll be temporarily set to RUNNING.

Quoted 2 lines

In case of suspend while the state is paused we will:
TRIGGER_PAUSE_RESET followed by STOP or SUSPEND?

Quoted 7 lines
Quoted 2 lines

For suspend, PAUSE_RESET, then SUSPEND.
For dropping, PAUSE_RESET, then STOP.

OK, but this is still the same as PAUSE_RELEASE+SUSPEND when it comes to suspended_state, no?
I mean you need to move to RUNNING to be suspend, or stop to skip the suspend.

Yes, and this needs to be addressed. e.g. save the suspended_state beforehand.

Quoted 18 lines
Quoted 10 lines
Quoted 3 lines

Most drivers are doing the same thing for STOP and SUSPEND trigger, however it is possible to do extra steps for SUSPEND to make sure that the whole system is in a lowest power state.

Yes, that's the reason I proposed PAUSE_RESET. Then the driver can handle the following stop or suspend operation differently.

That said, PAUSE_RESET is just another variant of PAUSE_RELEASE. But this shouldn't actually enable the audio output again, but prepare for the next SUSPEND or STOP trigger.

I'm not sure how this would be seen in ASoC level. Surely we need to have flag per components that it is handling PAUSE_RESET as codecs would like don't want to do it or they migh, hm.
But ASoC maintains another set of state for the dpcm:
SND_SOC_DPCM_STATE_* and it has this as well among other state-machine things:
case SNDRV_PCM_TRIGGER_SUSPEND:

Quoted 5 lines
	if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
		goto next;

So, this is dpcm level, but one component can support PAUSE_RESET while the other does not and the transitions are not too clear to me.

It's a good question. ASoC may have the own intermediate state (SND_SOC_DPCM_STATE_PAUSED_RESET) or just use SND_SOC_DPCM_STATE_START but leave all to BE trigger (by passing SNDRV_PCM_TRIGGER_PAUSE_RESET).

Quoted 4 lines

But the idea of resurrecting a paused stream with mute held is not bad.

I'm still uncertain how this actually works now on systems where RESUME is supported and we suspend while a PCM is in paused...

Is it a general question about suspend+resume during pause, or the behavior change after PAUSE_RESET? PAUSE_RESET won't be applied to the drivers that support RESUME.

BTW, I'm not pushing to sell PAUSE_RESET so much for now at all. Currently rather in a brain storming phase. Let's try to pick up the more appropriate one.

thanks,

Takashi

#18 lore raw
Quoted 82 lines · Takashi Iwai
Quoted 35 lines · Péter Ujfalusi
Quoted 31 lines · Takashi Iwai
Quoted 16 lines · Péter Ujfalusi
Quoted 12 lines · Takashi Iwai
Quoted 11 lines
Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

Quoted 7 lines
Quoted 4 lines

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

But what state that we move after we reset the PAUSE? is it RUNNING (iow, equals to PAUSE_RELEASE) or something else?

It'll be temporarily set to RUNNING.

Quoted 2 lines

In case of suspend while the state is paused we will:
TRIGGER_PAUSE_RESET followed by STOP or SUSPEND?

Quoted 7 lines
Quoted 2 lines

For suspend, PAUSE_RESET, then SUSPEND.
For dropping, PAUSE_RESET, then STOP.

OK, but this is still the same as PAUSE_RELEASE+SUSPEND when it comes to suspended_state, no?
I mean you need to move to RUNNING to be suspend, or stop to skip the suspend.

Yes, and this needs to be addressed. e.g. save the suspended_state beforehand.

Quoted 18 lines
Quoted 10 lines
Quoted 3 lines

Most drivers are doing the same thing for STOP and SUSPEND trigger, however it is possible to do extra steps for SUSPEND to make sure that the whole system is in a lowest power state.

Yes, that's the reason I proposed PAUSE_RESET. Then the driver can handle the following stop or suspend operation differently.

That said, PAUSE_RESET is just another variant of PAUSE_RELEASE. But this shouldn't actually enable the audio output again, but prepare for the next SUSPEND or STOP trigger.

I'm not sure how this would be seen in ASoC level. Surely we need to have flag per components that it is handling PAUSE_RESET as codecs would like don't want to do it or they migh, hm.
But ASoC maintains another set of state for the dpcm:
SND_SOC_DPCM_STATE_* and it has this as well among other state-machine things:
case SNDRV_PCM_TRIGGER_SUSPEND:

Quoted 5 lines
	if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
		goto next;

So, this is dpcm level, but one component can support PAUSE_RESET while the other does not and the transitions are not too clear to me.

It's a good question. ASoC may have the own intermediate state (SND_SOC_DPCM_STATE_PAUSED_RESET) or just use SND_SOC_DPCM_STATE_START but leave all to BE trigger (by passing SNDRV_PCM_TRIGGER_PAUSE_RESET).

Quoted 4 lines

But the idea of resurrecting a paused stream with mute held is not bad.

I'm still uncertain how this actually works now on systems where RESUME is supported and we suspend while a PCM is in paused...

Quoted 7 lines

Is it a general question about suspend+resume during pause, or the behavior change after PAUSE_RESET? PAUSE_RESET won't be applied to the drivers that support RESUME.

BTW, I'm not pushing to sell PAUSE_RESET so much for now at all. Currently rather in a brain storming phase. Let's try to pick up the more appropriate one.

While I think the patch is a good start to handle the current situation with a minimal change (and a possibility of dropping it when the more elaborate and likely laborious one is available), this is how it was before the patch in regards to suspend while paused:

RESUME is suported:
- Stream is PAUSED
- No SUSPEND trigger is sent (stream is not running), state changes to SUSPENDED, suspended_state is PAUSED
- On resume, no RESUME trigger is sent (suspended state is not RUNNING or DRAIN), but the state is changed to PAUSED.
- On PAUSE_RELEASE the trigger is sent (how it is handled in driver is another question as we had suspended the system in between).

RESUME is not suported:
- Stream is PAUSED
- No SUSPEND trigger is sent (stream is not running), state changes to SUSPENDED, suspended_state is PAUSED
- On resume, no RESUME trigger is sent (resume is not supported), the state remains to SUSPENDED, suspended_state is PAUSED
On PAUSE_RELEASE the core will raturn error as the state is not PAUSED, it is SUSPENDED and we go to xrun recovery
- driver received PAUSE_PUSH but never receives PAUSE_RELEASE

I think that the SUSPEND trigger (or a new similar trigger) should be sent to a PAUSED stream regardless of RESUME support. Imho it is not a correct assumption that a PAUSED stream is ready for system suspend, it might not be, it might needs to be prepared for that.
If we use the same SUSPEND trigger than the drivers need to track their paused state as well to know what to do - we rely on the enforced state changes in core to do this implicitly atm.

If there is no RESUME support, then it is a bit more complicated as after the system suspend the audio hardware's state does not matter, whenever it was RUNNMING or PAUSED we need to go to xrun recovery to restart, resume is not supported.

But if RESUME is not supported we do need to send the PAUSE_RELEASE to balance the PUSH.
Should we set XRUN as suspended_state in case of RESUME is not supported? Or some other state which indicates that whatever we had before, we are not going to be able to pick up where we were?

The fact that we discuss this shows how much this corner case have been excercised (we eneded upm disabling this in CI testing for now in SOF).

Signature · 1 line
Péter
#19 lore raw
Quoted 131 lines · Péter Ujfalusi
Quoted 82 lines · Takashi Iwai
Quoted 35 lines · Péter Ujfalusi
Quoted 31 lines · Takashi Iwai
Quoted 16 lines · Péter Ujfalusi
Quoted 12 lines · Takashi Iwai
Quoted 11 lines
Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

Quoted 7 lines
Quoted 4 lines

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

But what state that we move after we reset the PAUSE? is it RUNNING (iow, equals to PAUSE_RELEASE) or something else?

It'll be temporarily set to RUNNING.

Quoted 2 lines

In case of suspend while the state is paused we will:
TRIGGER_PAUSE_RESET followed by STOP or SUSPEND?

Quoted 7 lines
Quoted 2 lines

For suspend, PAUSE_RESET, then SUSPEND.
For dropping, PAUSE_RESET, then STOP.

OK, but this is still the same as PAUSE_RELEASE+SUSPEND when it comes to suspended_state, no?
I mean you need to move to RUNNING to be suspend, or stop to skip the suspend.

Yes, and this needs to be addressed. e.g. save the suspended_state beforehand.

Quoted 18 lines
Quoted 10 lines
Quoted 3 lines

Most drivers are doing the same thing for STOP and SUSPEND trigger, however it is possible to do extra steps for SUSPEND to make sure that the whole system is in a lowest power state.

Yes, that's the reason I proposed PAUSE_RESET. Then the driver can handle the following stop or suspend operation differently.

That said, PAUSE_RESET is just another variant of PAUSE_RELEASE. But this shouldn't actually enable the audio output again, but prepare for the next SUSPEND or STOP trigger.

I'm not sure how this would be seen in ASoC level. Surely we need to have flag per components that it is handling PAUSE_RESET as codecs would like don't want to do it or they migh, hm.
But ASoC maintains another set of state for the dpcm:
SND_SOC_DPCM_STATE_* and it has this as well among other state-machine things:
case SNDRV_PCM_TRIGGER_SUSPEND:

Quoted 5 lines
	if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
		goto next;

So, this is dpcm level, but one component can support PAUSE_RESET while the other does not and the transitions are not too clear to me.

It's a good question. ASoC may have the own intermediate state (SND_SOC_DPCM_STATE_PAUSED_RESET) or just use SND_SOC_DPCM_STATE_START but leave all to BE trigger (by passing SNDRV_PCM_TRIGGER_PAUSE_RESET).

Quoted 4 lines

But the idea of resurrecting a paused stream with mute held is not bad.

I'm still uncertain how this actually works now on systems where RESUME is supported and we suspend while a PCM is in paused...

Quoted 7 lines

Is it a general question about suspend+resume during pause, or the behavior change after PAUSE_RESET? PAUSE_RESET won't be applied to the drivers that support RESUME.

BTW, I'm not pushing to sell PAUSE_RESET so much for now at all. Currently rather in a brain storming phase. Let's try to pick up the more appropriate one.

While I think the patch is a good start to handle the current situation with a minimal change (and a possibility of dropping it when the more elaborate and likely laborious one is available), this is how it was before the patch in regards to suspend while paused:

RESUME is suported:
- Stream is PAUSED
- No SUSPEND trigger is sent (stream is not running), state changes to SUSPENDED, suspended_state is PAUSED
- On resume, no RESUME trigger is sent (suspended state is not RUNNING or DRAIN), but the state is changed to PAUSED.
- On PAUSE_RELEASE the trigger is sent (how it is handled in driver is another question as we had suspended the system in between).

RESUME is not suported:
- Stream is PAUSED
- No SUSPEND trigger is sent (stream is not running), state changes to SUSPENDED, suspended_state is PAUSED
- On resume, no RESUME trigger is sent (resume is not supported), the state remains to SUSPENDED, suspended_state is PAUSED
On PAUSE_RELEASE the core will raturn error as the state is not PAUSED, it is SUSPENDED and we go to xrun recovery
- driver received PAUSE_PUSH but never receives PAUSE_RELEASE

I think that the SUSPEND trigger (or a new similar trigger) should be sent to a PAUSED stream regardless of RESUME support. Imho it is not a correct assumption that a PAUSED stream is ready for system suspend, it might not be, it might needs to be prepared for that.
If we use the same SUSPEND trigger than the drivers need to track their paused state as well to know what to do - we rely on the enforced state changes in core to do this implicitly atm.

If there is no RESUME support, then it is a bit more complicated as after the system suspend the audio hardware's state does not matter, whenever it was RUNNMING or PAUSED we need to go to xrun recovery to restart, resume is not supported.

Quoted 8 lines

But if RESUME is not supported we do need to send the PAUSE_RELEASE to balance the PUSH.
Should we set XRUN as suspended_state in case of RESUME is not supported? Or some other state which indicates that whatever we had before, we are not going to be able to pick up where we were?

The fact that we discuss this shows how much this corner case have been excercised (we eneded upm disabling this in CI testing for now in SOF).

I agree that your patch can be a good start, at least, it addresses the existing issue with the minimal change. There are still rough edges and we'll need to address, but I believe the patch (or modified / fixed one) can be applied to 6.15 kernel, while keeping the development for 6.16.

I did some quick work on it, and now implemented SNDRV_PCM_PAUSE_RELEASE_STOP trigger. I guess it's more or less aligned with what Jaroslav suggested.

The patches are pushed out to topic/pcm-pause-rework branch of my sound.git tree. Please take a look.

thanks,

Takashi

maintainer
#20 lore raw
Quoted 5 lines · Takashi Iwai

I agree that your patch can be a good start, at least, it addresses the existing issue with the minimal change. There are still rough edges and we'll need to address, but I believe the patch (or modified / fixed one) can be applied to 6.15 kernel, while keeping the development for 6.16.

I did a quick research how is RESUME/SUSPEND implemented in current drivers and I found that it may be considered to enable SUSPEND trigger also in the paused state:

find sound/ -name "*.[ch]" -exec grep -A 2 -B 2 -H -E \
    "SNDRV_PCM_TRIGGER_(RESUME|SUSPEND)" {} \;

My comments - seems almost all drivers handles SUSPEND as STOP:

sound/pci/ymfpci/ymfpci_main.c
- STOP/SUSPEND different, should not be a problem
sound/pci/nm256/nm256.c
sound/pci/intel8x0.c
- only saves suspend flag additionaly to STOP
sound/soc
- some drivers ignores suspend/resume (ignore_suspend flag)
sound/soc/pxa/pxa-ssp.c
- should not be a problem (enable/disable hw parts)
sound/soc/sti/uniperif_player.c
- only resume trigger is supported ? but no resume info is set
sound/soc/sof/intel/hda-dai.c
- only suspend support, but resume is not supported
sound/xen/xen_snd_front_alsa.c
- it's just rpc
sound/virtio/virtio_pcm_ops.c
- only suspend support, no resume

It would be probably nice if more eyes verifies this, but the only suspicious place is the xen front end. And some drivers may be broken already (no changes for them).

I believe that this patch should be enough to resolve the issue instead the non-elegant pause release and may be applied also to 6.15:

sound/core/pcm_native.c +11 -2
index 4057f9f10aee..63a1b37cc098 100644
@@ -1694,8 +1694,17 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
        struct snd_pcm_runtime *runtime = substream->runtime;
        if (runtime->trigger_master != substream)
                return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (runtime->info & SNDRV_PCM_INFO_RESUME)
+                       return 0;
+               if (runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+               /*
+                * When resume is not supported, SUSPEND should STOP stream.
+                * For futher operation, the stream must be fully restarted
+                * to leave the permanent SUSPEND state.
+                */
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
        runtime->stop_operating = true;
        return 0; /* suspend unconditionally */

Jaroslav

Quoted 3 lines

I did some quick work on it, and now implemented SNDRV_PCM_PAUSE_RELEASE_STOP trigger. I guess it's more or less aligned with what Jaroslav suggested.

Nice cleanups. The "ALSA: pcm: Save the proper suspended_state for non-resumable case, too" should be recoded. The state should be handled in action callbacks for all streams separetely.

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#21 lore raw
Quoted 36 lines · Jaroslav Kysela
Quoted 5 lines · Takashi Iwai

I agree that your patch can be a good start, at least, it addresses the existing issue with the minimal change. There are still rough edges and we'll need to address, but I believe the patch (or modified / fixed one) can be applied to 6.15 kernel, while keeping the development for 6.16.

I did a quick research how is RESUME/SUSPEND implemented in current drivers and I found that it may be considered to enable SUSPEND trigger also in the paused state:

find sound/ -name "*.[ch]" -exec grep -A 2 -B 2 -H -E \
    "SNDRV_PCM_TRIGGER_(RESUME|SUSPEND)" {} \;

My comments - seems almost all drivers handles SUSPEND as STOP:

sound/pci/ymfpci/ymfpci_main.c
- STOP/SUSPEND different, should not be a problem
sound/pci/nm256/nm256.c
sound/pci/intel8x0.c
- only saves suspend flag additionaly to STOP
sound/soc
- some drivers ignores suspend/resume (ignore_suspend flag)
sound/soc/pxa/pxa-ssp.c
- should not be a problem (enable/disable hw parts)
sound/soc/sti/uniperif_player.c
- only resume trigger is supported ? but no resume info is set
sound/soc/sof/intel/hda-dai.c
- only suspend support, but resume is not supported
sound/xen/xen_snd_front_alsa.c
- it's just rpc
sound/virtio/virtio_pcm_ops.c
- only suspend support, no resume

Quoted 3 lines

It would be probably nice if more eyes verifies this, but the only suspicious place is the xen front end. And some drivers may be broken already (no changes for them).

The xen driver provides SNDRV_PCM_INFO_PAUSE, but it doesn't handle SNDRV_PCM_TRIGGER_PAUSE_* at all -- so the pause handling is broken there, as it seems.

Quoted 27 lines

I believe that this patch should be enough to resolve the issue instead the non-elegant pause release and may be applied also to 6.15:

sound/core/pcm_native.c +11 -2
index 4057f9f10aee..63a1b37cc098 100644
@@ -1694,8 +1694,17 @@ static int snd_pcm_do_suspend(struct
 snd_pcm_substream *substream,
        struct snd_pcm_runtime *runtime = substream->runtime;
        if (runtime->trigger_master != substream)
                return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (runtime->info & SNDRV_PCM_INFO_RESUME)
+                       return 0;
+               if (runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+               /*
+                * When resume is not supported, SUSPEND should STOP stream.
+                * For futher operation, the stream must be fully restarted
+                * to leave the permanent SUSPEND state.
+                */
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
        runtime->stop_operating = true;
Quoted 1 line

return 0; /* suspend unconditionally */

I'm afraid that it's not that straightforward, unfortunately. There are some stuff that require the pause release.

For example, pcm_dmaengine.c uses dmaengine_pause() as the suspend, and if we apply this change, it'll end up with dmaengine_pause() calls twice. As far as I see, a similar pattern is seen in ASoC Intel catpt driver (and maybe some others), too.

And, in drivers/soundwire/intel.c, there is already a workaround for suspend-at-pause, and this might conflict. We need to verify it.

Last but not least, the ASoC PCM core has its own DPCM state, and the trigger-SUSPEND skips the operation unless the stream has been running. I believe that's the very starting point of the problem Peter's patch tries to address.

Quoted 7 lines
Quoted 3 lines

I did some quick work on it, and now implemented SNDRV_PCM_PAUSE_RELEASE_STOP trigger. I guess it's more or less aligned with what Jaroslav suggested.

Nice cleanups. The "ALSA: pcm: Save the proper suspended_state for non-resumable case, too" should be recoded. The state should be handled in action callbacks for all streams separetely.

OK, I'll rewrite it. I wrote in the current way, as it'll be a bit too complex otherwise, but let me try another shot.

Quoted 3 lines

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Do you mean drivers that do share the same operation for suspend/resume and pause?

thanks,

Takashi

maintainer
#22 lore raw
Quoted 4 lines · Takashi Iwai

Last but not least, the ASoC PCM core has its own DPCM state, and the trigger-SUSPEND skips the operation unless the stream has been running. I believe that's the very starting point of the problem Peter's patch tries to address.

Unfortunately, after all discussions, I'm not convinced about Peter's patch. It smells like an workaround rather than a fix for forever. The problem is there for years so we should not concentrate on a quick fix.

Quoted 6 lines
Quoted 3 lines

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Do you mean drivers that do share the same operation for suspend/resume and pause?

Many drivers supporting pause may use it when we allow PAUSE -> SUSPEND and SUSPEND -> PAUSE transitions.

Something like:

include/sound/pcm.h +2 -0
index ac8f3aef9205..6d6882973883 100644
@@ -541,6 +541,8 @@ struct snd_pcm {
        bool internal; /* pcm is for internal use only */
        bool nonatomic; /* whole PCM operations are in non-atomic context */
        bool no_device_suspend; /* don't invoke device PM suspend */
+       bool do_suspend_and_resume_in_pause; /* allow direct PAUSED -> SUSPENDED and
+                                               SUSPENDED -> PAUSED transitions */
 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
        struct snd_pcm_oss oss;
 #endif
sound/core/pcm_native.c +10 -4
index 4057f9f10aee..3ec4ef7809ea 100644
@@ -1694,8 +1694,11 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
        struct snd_pcm_runtime *runtime = substream->runtime;
        if (runtime->trigger_master != substream)
                return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
        runtime->stop_operating = true;
        return 0; /* suspend unconditionally */
@@ -1798,8 +1801,11 @@ static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
        /* DMA not running previously? */
        if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING &&
            (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING ||
-            substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
-               return 0;
+            substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->suspend_state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
        return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
 }
 

This will allow to address this problem correctly without any pause release just to put all parts to the running state and immediately stop the stream. Ideally, all drivers should be migrated to this state scheme and the condition may be removed at the end.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#23 lore raw
Quoted 66 lines · Jaroslav Kysela
Quoted 4 lines · Takashi Iwai

Last but not least, the ASoC PCM core has its own DPCM state, and the trigger-SUSPEND skips the operation unless the stream has been running. I believe that's the very starting point of the problem Peter's patch tries to address.

Unfortunately, after all discussions, I'm not convinced about Peter's patch. It smells like an workaround rather than a fix for forever. The problem is there for years so we should not concentrate on a quick fix.

Quoted 6 lines
Quoted 3 lines

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Do you mean drivers that do share the same operation for suspend/resume and pause?

Many drivers supporting pause may use it when we allow PAUSE -> SUSPEND and SUSPEND -> PAUSE transitions.

Something like:

include/sound/pcm.h +2 -0
index ac8f3aef9205..6d6882973883 100644
@@ -541,6 +541,8 @@ struct snd_pcm {
        bool internal; /* pcm is for internal use only */
        bool nonatomic; /* whole PCM operations are in non-atomic context */
        bool no_device_suspend; /* don't invoke device PM suspend */
+       bool do_suspend_and_resume_in_pause; /* allow direct PAUSED -> SUSPENDED and
+                                               SUSPENDED -> PAUSED transitions */
 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
        struct snd_pcm_oss oss;
 #endif
sound/core/pcm_native.c +10 -4
index 4057f9f10aee..3ec4ef7809ea 100644
@@ -1694,8 +1694,11 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
        struct snd_pcm_runtime *runtime = substream->runtime;
        if (runtime->trigger_master != substream)
                return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
        runtime->stop_operating = true;
        return 0; /* suspend unconditionally */
@@ -1798,8 +1801,11 @@ static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
        /* DMA not running previously? */
        if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING &&
            (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING ||
-            substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
-               return 0;
+            substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->suspend_state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
        return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
 }
 
Quoted 1 line

This will allow to address this problem correctly without any pause release just to put all parts to the running state and immediately stop the stream. Ideally, all drivers should be migrated to this state scheme and the condition may be removed at the end.

OK, this should work, too.
If we deal with another corner case such as PAUSED -> STOP, a new flag can be added, too. (Or make one flag indicating both meanings.)

Or, there can be a driver that wants to receive trigger SUSPEND even for the non-running substreams, too. We need to check the details before moving forward...

And, if there can be multiple flags, I'd rather introduce a new field to snd_pcm_hardware, e.g. extra_flags field to keep the internal bit flags that aren't exposed to user-space. Otherwise the setup can be cumbersome for each driver.

Unfortunately, the existing flags in snd_pcm like nonatomic and no_device_suspend can't be put there, though, as those are referred for the closed substreams, too.

thanks,

Takashi

maintainer
#24 lore raw
Quoted 74 lines · Takashi Iwai
Quoted 66 lines · Jaroslav Kysela
Quoted 4 lines · Takashi Iwai

Last but not least, the ASoC PCM core has its own DPCM state, and the trigger-SUSPEND skips the operation unless the stream has been running. I believe that's the very starting point of the problem Peter's patch tries to address.

Unfortunately, after all discussions, I'm not convinced about Peter's patch. It smells like an workaround rather than a fix for forever. The problem is there for years so we should not concentrate on a quick fix.

Quoted 6 lines
Quoted 3 lines

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Do you mean drivers that do share the same operation for suspend/resume and pause?

Many drivers supporting pause may use it when we allow PAUSE -> SUSPEND and SUSPEND -> PAUSE transitions.

Something like:

include/sound/pcm.h +2 -0
index ac8f3aef9205..6d6882973883 100644
@@ -541,6 +541,8 @@ struct snd_pcm {
         bool internal; /* pcm is for internal use only */
         bool nonatomic; /* whole PCM operations are in non-atomic context */
         bool no_device_suspend; /* don't invoke device PM suspend */
+       bool do_suspend_and_resume_in_pause; /* allow direct PAUSED -> SUSPENDED and
+                                               SUSPENDED -> PAUSED transitions */
  #if IS_ENABLED(CONFIG_SND_PCM_OSS)
         struct snd_pcm_oss oss;
  #endif
sound/core/pcm_native.c +10 -4
index 4057f9f10aee..3ec4ef7809ea 100644
@@ -1694,8 +1694,11 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
         struct snd_pcm_runtime *runtime = substream->runtime;
         if (runtime->trigger_master != substream)
                 return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
         substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
         runtime->stop_operating = true;
         return 0; /* suspend unconditionally */
@@ -1798,8 +1801,11 @@ static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
         /* DMA not running previously? */
         if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING &&
             (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING ||
-            substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
-               return 0;
+            substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->suspend_state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
         return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
  }
 
Quoted 1 line

This will allow to address this problem correctly without any pause release just to put all parts to the running state and immediately stop the stream. Ideally, all drivers should be migrated to this state scheme and the condition may be removed at the end.

OK, this should work, too.
If we deal with another corner case such as PAUSED -> STOP, a new flag can be added, too. (Or make one flag indicating both meanings.)

Quoted 8 lines

Or, there can be a driver that wants to receive trigger SUSPEND even for the non-running substreams, too. We need to check the details before moving forward...

And, if there can be multiple flags, I'd rather introduce a new field to snd_pcm_hardware, e.g. extra_flags field to keep the internal bit flags that aren't exposed to user-space. Otherwise the setup can be cumbersome for each driver.

I think that one flag for new suspend/pause/resume trigger behavior should be enough. We may reuse SNDRV_PCM_INFO_DRAIN_TRIGGER flag which is no longer used. It was probably implemented only in removed Intel SST drivers.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#25 lore raw
Quoted 85 lines · Jaroslav Kysela
Quoted 74 lines · Takashi Iwai
Quoted 66 lines · Jaroslav Kysela
Quoted 4 lines · Takashi Iwai

Last but not least, the ASoC PCM core has its own DPCM state, and the trigger-SUSPEND skips the operation unless the stream has been running. I believe that's the very starting point of the problem Peter's patch tries to address.

Unfortunately, after all discussions, I'm not convinced about Peter's patch. It smells like an workaround rather than a fix for forever. The problem is there for years so we should not concentrate on a quick fix.

Quoted 6 lines
Quoted 3 lines

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Do you mean drivers that do share the same operation for suspend/resume and pause?

Many drivers supporting pause may use it when we allow PAUSE -> SUSPEND and SUSPEND -> PAUSE transitions.

Something like:

include/sound/pcm.h +2 -0
index ac8f3aef9205..6d6882973883 100644
@@ -541,6 +541,8 @@ struct snd_pcm {
         bool internal; /* pcm is for internal use only */
         bool nonatomic; /* whole PCM operations are in non-atomic context */
         bool no_device_suspend; /* don't invoke device PM suspend */
+       bool do_suspend_and_resume_in_pause; /* allow direct PAUSED -> SUSPENDED and
+                                               SUSPENDED -> PAUSED transitions */
  #if IS_ENABLED(CONFIG_SND_PCM_OSS)
         struct snd_pcm_oss oss;
  #endif
sound/core/pcm_native.c +10 -4
index 4057f9f10aee..3ec4ef7809ea 100644
@@ -1694,8 +1694,11 @@ static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
         struct snd_pcm_runtime *runtime = substream->runtime;
         if (runtime->trigger_master != substream)
                 return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
         substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
         runtime->stop_operating = true;
         return 0; /* suspend unconditionally */
@@ -1798,8 +1801,11 @@ static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
         /* DMA not running previously? */
         if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING &&
             (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING ||
-            substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
-               return 0;
+            substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->suspend_state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
         return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
  }
 
Quoted 1 line

This will allow to address this problem correctly without any pause release just to put all parts to the running state and immediately stop the stream. Ideally, all drivers should be migrated to this state scheme and the condition may be removed at the end.

OK, this should work, too.
If we deal with another corner case such as PAUSED -> STOP, a new flag can be added, too. (Or make one flag indicating both meanings.)

Quoted 8 lines

Or, there can be a driver that wants to receive trigger SUSPEND even for the non-running substreams, too. We need to check the details before moving forward...

And, if there can be multiple flags, I'd rather introduce a new field to snd_pcm_hardware, e.g. extra_flags field to keep the internal bit flags that aren't exposed to user-space. Otherwise the setup can be cumbersome for each driver.

Quoted 4 lines

I think that one flag for new suspend/pause/resume trigger behavior should be enough. We may reuse SNDRV_PCM_INFO_DRAIN_TRIGGER flag which is no longer used. It was probably implemented only in removed Intel SST drivers.

OK, this is a subtle detail and can be discussed later.
I believe the biggest obstacle in this route is the handling in ASoC DPCM. AFAIUC, it has a refcount management of paused BEs, and this transition can break it.

thanks,

Takashi

#26 lore raw
In reply to Jaroslav Kysela
Quoted 28 lines · Jaroslav Kysela
Quoted 4 lines · Takashi Iwai

Last but not least, the ASoC PCM core has its own DPCM state, and the trigger-SUSPEND skips the operation unless the stream has been running.  I believe that's the very starting point of the problem Peter's patch tries to address.

Unfortunately, after all discussions, I'm not convinced about Peter's patch. It smells like an workaround rather than a fix for forever. The problem is there for years so we should not concentrate on a quick fix.

Quoted 6 lines
Quoted 3 lines

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Do you mean drivers that do share the same operation for suspend/resume and pause?

Many drivers supporting pause may use it when we allow PAUSE -> SUSPEND and SUSPEND -> PAUSE transitions.

Something like:

include/sound/pcm.h +0 -0
index ac8f3aef9205..6d6882973883 100644
@@ -541,6 +541,8 @@ struct snd_pcm {
Quoted 8 lines

        bool internal; /* pcm is for internal use only */ bool nonatomic; /* whole PCM operations are in non-atomic context */
        bool no_device_suspend; /* don't invoke device PM suspend */ +       bool do_suspend_and_resume_in_pause; /* allow direct PAUSED -> SUSPENDED and
+                                               SUSPENDED -> PAUSED transitions */

I think the issue is bit different than this.
What needs to be flagged is that the driver is not treating the PAUSE trigger in the same way as it treats the STOP or SUSPEND, in other worlds, the PAUSED state is not equal to STOPPED/SUSPENDED.

If the PAUSED state is equal to SUSPENDED state, then the trigger could be skipped (as it is ignored before my patch unconditionally), but if the two states are not equal then the SUSPEND must be sent, but the patches from me and Takashi-san stil can be used as fallback in case the RESUME is not supported as in that case the driver will certainly not going to be able to resume to PAUSE_RESUME, so stopping a stream in that case is appropriate.

Side note: I think we might also have broken case when we stop a PAUSED stream if the driver's PAUSED state is not equal to STOPPED/SUSPENDED. It is just that applications never do this, at least I ave never seen a missing STOP trigger. aplay is doing a pause release before stop at least.

Quoted 45 lines

 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
        struct snd_pcm_oss oss;
 #endif

sound/core/pcm_native.c +5 -2
index 4057f9f10aee..3ec4ef7809ea 100644
@@ -1694,8 +1694,11 @@ static int snd_pcm_do_suspend(struct
 snd_pcm_substream *substream,
         struct snd_pcm_runtime *runtime = substream->runtime;
         if (runtime->trigger_master != substream)
                 return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (!runtime->do_suspend_and_resume_in_pause ||
+                   runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+       }
         substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
         runtime->stop_operating = true;

        return 0; /* suspend unconditionally */

sound/core/pcm_native.c +0 -0
@@ -1798,8 +1801,11 @@ static int snd_pcm_do_resume(struct
 snd_pcm_substream *substream,
         /* DMA not running previously? */

        if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING && (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING || -            substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) -               return 0;
+            substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) { +               if (!runtime->do_suspend_and_resume_in_pause || +                   runtime->suspend_state != SNDRV_PCM_STATE_PAUSED) +                       return 0;
+       }
        return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
 }

This will allow to address this problem correctly without any pause release just to put all parts to the running state and immediately stop the stream. Ideally, all drivers should be migrated to this state scheme and the condition may be removed at the end.

                    Jaroslav

Signature · 1 line
Péter
maintainer
#27 lore raw
Quoted 45 lines · Péter Ujfalusi
Quoted 30 lines · Jaroslav Kysela
Quoted 4 lines · Takashi Iwai

Last but not least, the ASoC PCM core has its own DPCM state, and the trigger-SUSPEND skips the operation unless the stream has been running.  I believe that's the very starting point of the problem Peter's patch tries to address.

Unfortunately, after all discussions, I'm not convinced about Peter's patch. It smells like an workaround rather than a fix for forever. The problem is there for years so we should not concentrate on a quick fix.

Quoted 6 lines
Quoted 3 lines

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

Do you mean drivers that do share the same operation for suspend/resume and pause?

Many drivers supporting pause may use it when we allow PAUSE -> SUSPEND and SUSPEND -> PAUSE transitions.

Something like:

include/sound/pcm.h +0 -0
index ac8f3aef9205..6d6882973883 100644
@@ -541,6 +541,8 @@ struct snd_pcm {
         bool internal; /* pcm is for internal use only */
         bool nonatomic; /* whole PCM operations are in non-atomic
Quoted 6 lines

context */
        bool no_device_suspend; /* don't invoke device PM suspend */
+       bool do_suspend_and_resume_in_pause; /* allow direct PAUSED -> SUSPENDED and
+                                               SUSPENDED -> PAUSED transitions */

I think the issue is bit different than this.
What needs to be flagged is that the driver is not treating the PAUSE trigger in the same way as it treats the STOP or SUSPEND, in other worlds, the PAUSED state is not equal to STOPPED/SUSPENDED.

Quoted 7 lines

If the PAUSED state is equal to SUSPENDED state, then the trigger could be skipped (as it is ignored before my patch unconditionally), but if the two states are not equal then the SUSPEND must be sent, but the patches from me and Takashi-san stil can be used as fallback in case the RESUME is not supported as in that case the driver will certainly not going to be able to resume to PAUSE_RESUME, so stopping a stream in that case is appropriate.

The question is if the fallback is required when we had this issue for years. Using only a new trigger - to not release pause but directly do stop from the pause state - is acceptable for me. It should be easy to fix affected drivers. But on other side, the allowing state transitions (and even multiple triggers for finer driver notifications) may have also benefits for drivers.

Quoted 4 lines

Side note: I think we might also have broken case when we stop a PAUSED stream if the driver's PAUSED state is not equal to STOPPED/SUSPENDED. It is just that applications never do this, at least I ave never seen a missing STOP trigger. aplay is doing a pause release before stop at least.

Aplay implementation is just about simplicity. The pause is handled in one routine which releases pause at the end. When the file descriptor is closed, snd_pcm_drop() is called and the pause is released there, too. Logically, it's not 100% correct thing. We know that we want to stop stream before the pause release is called.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#28 lore raw
Quoted 19 lines · Jaroslav Kysela
Quoted 11 lines
Quoted 5 lines

         bool no_device_suspend; /* don't invoke device PM suspend */ +       bool do_suspend_and_resume_in_pause; /* allow direct PAUSED -> SUSPENDED and
+                                               SUSPENDED -> PAUSED transitions */

I think the issue is bit different than this.
What needs to be flagged is that the driver is not treating the PAUSE trigger in the same way as it treats the STOP or SUSPEND, in other worlds, the PAUSED state is not equal to STOPPED/SUSPENDED.

Quoted 7 lines

If the PAUSED state is equal to SUSPENDED state, then the trigger could be skipped (as it is ignored before my patch unconditionally), but if the two states are not equal then the SUSPEND must be sent, but the patches from me and Takashi-san stil can be used as fallback in case the RESUME is not supported as in that case the driver will certainly not going to be able to resume to PAUSE_RESUME, so stopping a stream in that case is appropriate.

Quoted 2 lines

The question is if the fallback is required when we had this issue for years.

This discussion is a proof that we might want.
We ended up disabling testing the suspend while paused case entirely for quite some time and I could only get it working with my RFC attempt before Takashi-san suggestion to do this in core level.

I agree that this is a rare corner case, more academic than not, users will likely not going to face with as none of the audio servers use pause so the chances that the system goes suspend while audio is paused is close to zero.

Quoted 12 lines

Using only a new trigger - to not release pause but directly do stop from the pause state - is acceptable for me. It should be easy to fix affected drivers. But on other side, the allowing state transitions (and even multiple triggers for finer driver notifications) may have also benefits for drivers.

Quoted 5 lines

Side note: I think we might also have broken case when we stop a PAUSED stream if the driver's PAUSED state is not equal to STOPPED/SUSPENDED. It is just that applications never do this, at least I ave never seen a missing STOP trigger. aplay is doing a pause release before stop at least.

Quoted 6 lines

Aplay implementation is just about simplicity. The pause is handled in one routine which releases pause at the end. When the file descriptor is closed,
snd_pcm_drop() is called and the pause is released there, too. Logically, it's not 100% correct thing. We know that we want to stop stream before the pause release is called.

The assumption is that PAUSED == STOPPED == SUSPENDED is not correct either. This is not true for some drivers or devices.
I would go as far as to say that most drivers are mishandling the PAUSE as stop, they likely cannot resume from the exact place where the pause taken - they stop and if they have FIFO, they drop that as well...

Quoted 3 lines

                    Jaroslav

Signature · 1 line
Péter
#29 lore raw
In reply to Jaroslav Kysela
Quoted 60 lines · Jaroslav Kysela
Quoted 5 lines · Takashi Iwai

I agree that your patch can be a good start, at least, it addresses the existing issue with the minimal change.  There are still rough edges and we'll need to address, but I believe the patch (or modified / fixed one) can be applied to 6.15 kernel, while keeping the development for 6.16.

I did a quick research how is RESUME/SUSPEND implemented in current drivers and I found that it may be considered to enable SUSPEND trigger also in the paused state:

find sound/ -name "*.[ch]" -exec grep -A 2 -B 2 -H -E \
    "SNDRV_PCM_TRIGGER_(RESUME|SUSPEND)" {} \;

My comments - seems almost all drivers handles SUSPEND as STOP:

sound/pci/ymfpci/ymfpci_main.c
 - STOP/SUSPEND different, should not be a problem sound/pci/nm256/nm256.c
sound/pci/intel8x0.c
 - only saves suspend flag additionaly to STOP sound/soc
 - some drivers ignores suspend/resume (ignore_suspend flag) sound/soc/pxa/pxa-ssp.c
 - should not be a problem (enable/disable hw parts) sound/soc/sti/uniperif_player.c
 - only resume trigger is supported ? but no resume info is set sound/soc/sof/intel/hda-dai.c
 - only suspend support, but resume is not supported sound/xen/xen_snd_front_alsa.c
 - it's just rpc
sound/virtio/virtio_pcm_ops.c
 - only suspend support, no resume

It would be probably nice if more eyes verifies this, but the only suspicious place is the xen front end. And some drivers may be broken already (no changes for them).

I believe that this patch should be enough to resolve the issue instead the non-elegant pause release and may be applied also to 6.15:

sound/core/pcm_native.c +7 -2
index 4057f9f10aee..63a1b37cc098 100644
@@ -1694,8 +1694,17 @@ static int snd_pcm_do_suspend(struct
 snd_pcm_substream *substream,
         struct snd_pcm_runtime *runtime = substream->runtime;
         if (runtime->trigger_master != substream)
                 return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (runtime->info & SNDRV_PCM_INFO_RESUME)
+                       return 0;
+               if (runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+               /*
+                * When resume is not supported, SUSPEND should STOP
Quoted 8 lines

stream.
+                * For futher operation, the stream must be fully restarted +                * to leave the permanent SUSPEND state.
+                */
+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); runtime->stop_operating = true;
        return 0; /* suspend unconditionally */

I have tried similar thing and the original problem remains that we are missing the PAUSE_RELEASE trigger for the PUSH and ASoC core stops the SUSPEND trigger to reach the drivers.

Quoted 17 lines

                Jaroslav

Quoted 3 lines

I did some quick work on it, and now implemented SNDRV_PCM_PAUSE_RELEASE_STOP trigger.  I guess it's more or less aligned with what Jaroslav suggested.

Nice cleanups. The "ALSA: pcm: Save the proper suspended_state for non- resumable case, too" should be recoded. The state should be handled in action callbacks for all streams separetely.

Also, I would consider to call suspend/resume triggers (depending on a flag like can_pause_release_stop) when the stream is paused, too. Some drivers may want this scheme.

                    Jaroslav

Signature · 1 line
Péter
maintainer
#30 lore raw
Quoted 67 lines · Péter Ujfalusi
Quoted 64 lines · Jaroslav Kysela
Quoted 5 lines · Takashi Iwai

I agree that your patch can be a good start, at least, it addresses the existing issue with the minimal change.  There are still rough edges and we'll need to address, but I believe the patch (or modified / fixed one) can be applied to 6.15 kernel, while keeping the development for 6.16.

I did a quick research how is RESUME/SUSPEND implemented in current drivers and I found that it may be considered to enable SUSPEND trigger also in the paused state:

find sound/ -name "*.[ch]" -exec grep -A 2 -B 2 -H -E \
    "SNDRV_PCM_TRIGGER_(RESUME|SUSPEND)" {} \;

My comments - seems almost all drivers handles SUSPEND as STOP:

sound/pci/ymfpci/ymfpci_main.c
 - STOP/SUSPEND different, should not be a problem
sound/pci/nm256/nm256.c
sound/pci/intel8x0.c
 - only saves suspend flag additionaly to STOP
sound/soc
 - some drivers ignores suspend/resume (ignore_suspend flag)
sound/soc/pxa/pxa-ssp.c
 - should not be a problem (enable/disable hw parts)
sound/soc/sti/uniperif_player.c
 - only resume trigger is supported ? but no resume info is set
sound/soc/sof/intel/hda-dai.c
 - only suspend support, but resume is not supported
sound/xen/xen_snd_front_alsa.c
 - it's just rpc
sound/virtio/virtio_pcm_ops.c
 - only suspend support, no resume

It would be probably nice if more eyes verifies this, but the only suspicious place is the xen front end. And some drivers may be broken already (no changes for them).

I believe that this patch should be enough to resolve the issue instead the non-elegant pause release and may be applied also to 6.15:

sound/core/pcm_native.c +7 -2
index 4057f9f10aee..63a1b37cc098 100644
@@ -1694,8 +1694,17 @@ static int snd_pcm_do_suspend(struct
 snd_pcm_substream *substream,
         struct snd_pcm_runtime *runtime = substream->runtime;
         if (runtime->trigger_master != substream)
                 return 0;
-       if (! snd_pcm_running(substream))
-               return 0;
+       if (! snd_pcm_running(substream)) {
+               if (runtime->info & SNDRV_PCM_INFO_RESUME)
+                       return 0;
+               if (runtime->state != SNDRV_PCM_STATE_PAUSED)
+                       return 0;
+               /*
+                * When resume is not supported, SUSPEND should STOP

stream.
+                * For futher operation, the stream must be fully restarted +                * to leave the permanent SUSPEND state.
+                */

Quoted 8 lines
Quoted 4 lines

+       }
        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); runtime->stop_operating = true;
        return 0; /* suspend unconditionally */

I have tried similar thing and the original problem remains that we are missing the PAUSE_RELEASE trigger for the PUSH and ASoC core stops the SUSPEND trigger to reach the drivers.

All drivers must be adopted for this scheme including ASoC core, of course.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#31 lore raw
In reply to Takashi Iwai
Quoted 9 lines · Takashi Iwai
Quoted 2 lines

The fact that we discuss this shows how much this corner case have been excercised (we eneded upm disabling this in CI testing for now in SOF).

I agree that your patch can be a good start, at least, it addresses the existing issue with the minimal change. There are still rough edges and we'll need to address, but I believe the patch (or modified / fixed one) can be applied to 6.15 kernel, while keeping the development for 6.16.

Quoted 6 lines

I did some quick work on it, and now implemented SNDRV_PCM_PAUSE_RELEASE_STOP trigger. I guess it's more or less aligned with what Jaroslav suggested.

The patches are pushed out to topic/pcm-pause-rework branch of my sound.git tree. Please take a look.

Sorry for the delay, I just got to test the patches from the pcm-pause-rework and they seams to work OK.
There is only one difference: before suspend the drivers will receive STOP instead of SUSPEND if the can_pause_release_stop flag is not set, but thinking about this a bit more, it sounds the correct thing as the stream is really stopped.

Signature · 1 line
Péter
maintainer
#32 lore raw
In reply to Takashi Iwai
Quoted 23 lines · Takashi Iwai
Quoted 21 lines · Péter Ujfalusi
Quoted 16 lines · Takashi Iwai
Quoted 11 lines
Quoted 6 lines

On the second thought, yet another variant would be to introduce only one new trigger, SNDRV_PCM_TRIGGER_PAUSE_RESET, for resetting the PAUSED state as a step before stopping the stream. This can be applied to all cases for snd_pcm_drop() and snd_pcm_suspend(), too. If the driver doesn't support this mode, the PCM core may fall back to SNDRV_PCM_TRIGGER_PAUSE_RELEASE.

This looks identical to my proposed SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP which is more explanatory. It's not clear what PAUSE_RESET should do (probably the stream should be kept inactive).

There is a slight difference. My proposal, *_TRIGGER_PAUSE_RESET, doesn't have to stop the stream by itself, but it's only a preparation for stopping. That is, a proper TRIGGER_STOP always follows after TRIGGER_PAUSE_RESET.

Quoted 4 lines
Quoted 2 lines

But what state that we move after we reset the PAUSE? is it RUNNING (iow, equals to PAUSE_RELEASE) or something else?

It'll be temporarily set to RUNNING.

I don't think that there's requirement to change state at this point. It would be better to keep PAUSED state and add this state to the running state checks. Something like:

include/sound/pcm.h +6 -0
index ac8f3aef9205..166ef0376293 100644
@@ -708,6 +708,12 @@ static inline int snd_pcm_running(struct snd_pcm_substream *substream)
                 substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
 }
 
+static inline int snd_pcm_active(struct snd_pcm_substream *substream)
+{
+       return snd_pcm_running(substream) ||
+               substream->runtime->state == SNDRV_PCM_STATE_PAUSED;
+}
+
 /**
  * __snd_pcm_set_state - Change the current PCM state
  * @runtime: PCM runtime to set

But...

Quoted 5 lines
Quoted 2 lines

In case of suspend while the state is paused we will:
TRIGGER_PAUSE_RESET followed by STOP or SUSPEND?

For suspend, PAUSE_RESET, then SUSPEND.
For dropping, PAUSE_RESET, then STOP.

I'm not convinced if the functionality/information split is a good idea in this case. The preparation (code) for suspend and stop may be different. With separate triggers, drivers know exactly about the requested behavior (and we know it at trigger call time already).

Perhaps, more universal triggers like SNDRV_PCM_TRIGGER_STOP_PREPARE and
SNDRV_PCM_TRIGGER_SUSPEND_PREPARE may be invoked (always) before STOP/PREPARE triggers Drivers should handle also the pause state there on it's own to keep the DMA transfers in idle for those cases.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#33 lore raw
In reply to Jaroslav Kysela
Quoted 8 lines · Jaroslav Kysela
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Actually, this patch follows the same pattern, too.  It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

The previous state (suspended_state) will be confusing from the application with the proposed patch, because there will be RUNNING state instead PAUSED. This previous state is exported via API.

Quoted 7 lines
Quoted 4 lines

No, it won't happen.  The condition of the new behavior is only when SNDRV_PCM_INFO_RESUME isn't set -- that is, no resume is possible, hence no state recovery happens after resume.  From the application POV, it won't change.

The suspended_state can be obtained in snd_pcm_status64(). With Peter's patch, there will be RUNNING instead PAUSED, don't?

Yes, that is true, but it does not matter.
if the SNDRV_PCM_INFO_RESUME is not set, on resume nothing is going to be done, the state remains SUSPENDED and the suspended_substate also retained, so:

The stream was in RUNNING, after suspend:
state = SUSPENDED, suspended_state = RUNNING
After resume:
state = SUSPENDED, suspended_state = RUNNING
The the stream goes to xrun and got restarted.

The stream was in RUNNING, after suspend:
Without my patch / with my patch:
state = SUSPENDED, suspended_state = PAUSED / RUNNING After resume:
state = SUSPENDED, suspended_state = RUNNING / RUNNING

The stream remains "not running" as resume is not supported and application knows that it had left the stream paused.

When it tries to PAUSE_RELEASE the stream, in both case the core will look at the state, which is SUSPENDED and thus the pause release fails (it is not in paused state), so in both cases we go under a restart (suspended_state is reset, ignored).

Quoted 31 lines
Quoted 23 lines
Quoted 15 lines
Quoted 4 lines

That's another possibility, yes.  Though, IMO, it makes the pause-handling a bit more cumbersome.  With Peter's proposal, the pause state change is always paired, so the driver doesn't consider about that too much; that's the biggest merit.

It depends, if we agree that releasing pause is a extra thing to do when we know that this step is just to minimize state changes for drivers.

For my view, the drivers may also receive those triggers:

SNDRV_PCM_TRIGGER_PAUSE_RELEASE_AND_STOP
SNDRV_PCM_TRIGGER_SUSPEND_IN_PAUSE
SNDRV_PCM_TRIGGER_RELEASE_IN_PAUSE

Quoted 5 lines

This will allow to handle all states properly without any side effects (like short unwanted DMA transfers).

The drivers should probably activate those triggers conditionally to not break current state sequences.

Hmm, that looks too complex, IMO.

Quoted 7 lines

Another slightly more straightforward fix would be just to allow the direct state transition from PAUSED to any state.  Then the remaining piece is all about driver implementations.  And, this can be conditionally operated, e.g. only when some extra flag is set.  When no flag is set, we keep applying PAUSE_RELEASE before the transition like now, including Peter's patch, that can work generically (but not always ideally).

Quoted 2 lines

It looks also feasible. My proposal just allows the straight state identification from the trigger value in the drivers.

We could also add a flag that the driver can say that 'I know what I'm doing' and in that case send just simple SUSPEND and trust the driver that it really knows what it is doing.
However if the driver does not support RESUME, then things get a bit complicated. I'm not entirely sure what drivers do when they support RESUME and they went to suspend with paused state...

But, this is not that simple in ASoC, as there we can have at least four drivers involved (machine, platform, cpu dai, codec), all of them has to support this new fine graded state dependent trigger, right?

Signature · 1 line
Péter
#34 lore raw
Quoted 36 lines · Péter Ujfalusi
Quoted 8 lines · Jaroslav Kysela
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Actually, this patch follows the same pattern, too.  It calls snd_pcm_pause(false) to set the state to RUNNING again, then proceed to the suspend action immediately that sets to SUSPENDED.

The previous state (suspended_state) will be confusing from the application with the proposed patch, because there will be RUNNING state instead PAUSED. This previous state is exported via API.

Quoted 7 lines
Quoted 4 lines

No, it won't happen.  The condition of the new behavior is only when SNDRV_PCM_INFO_RESUME isn't set -- that is, no resume is possible, hence no state recovery happens after resume.  From the application POV, it won't change.

The suspended_state can be obtained in snd_pcm_status64(). With Peter's patch, there will be RUNNING instead PAUSED, don't?

Yes, that is true, but it does not matter.
if the SNDRV_PCM_INFO_RESUME is not set, on resume nothing is going to be done, the state remains SUSPENDED and the suspended_substate also retained, so:

The stream was in RUNNING, after suspend:
state = SUSPENDED, suspended_state = RUNNING
After resume:
state = SUSPENDED, suspended_state = RUNNING
The the stream goes to xrun and got restarted.

The stream was in RUNNING, after suspend:
Without my patch / with my patch:
state = SUSPENDED, suspended_state = PAUSED / RUNNING After resume:
state = SUSPENDED, suspended_state = RUNNING / RUNNING

Quoted 7 lines

The stream remains "not running" as resume is not supported and application knows that it had left the stream paused.

When it tries to PAUSE_RELEASE the stream, in both case the core will look at the state, which is SUSPENDED and thus the pause release fails (it is not in paused state), so in both cases we go under a restart (suspended_state is reset, ignored).

Well, the suspended_state itself is set no matter whether the resume is supported or not. The difference is that it's evaluated in the driver only by the suspended state, but it's free for user-space to read this out from the output of snd_pcm_status ioctl. How seriously it needs to be a different question, but there is a possibility of breakage.

Though, looking through the code, the suspended_state isn't updated at all except for the suspend, and it means this can keep a bogus state, too. We should fix the code to clear the suspended_state at init phase. It's another corner case.

thanks,

Takashi

#35 lore raw
Quoted 3 lines · Takashi Iwai

... The difference is that it's evaluated in the
driver only by the suspended state,

Here I meant "only by the resume operation".

Quoted 4 lines

but it's free for user-space to
read this out from the output of snd_pcm_status ioctl. How seriously it needs to be a different question, but there is a possibility of breakage.

Takashi