On Tue, 2020-06-02 at 14:46 +0200, Takashi Iwai wrote:
On Tue, 02 Jun 2020 13:53:41 +0200, Macpaul Lin wrote:
This patch fix incorrect power state changed by usb_audio_suspend() when CONFIG_PM is enabled.
After receiving suspend PM message with auto flag, usb_audio_suspend() change card's power state to SNDRV_CTL_POWER_D3hot. Only when the other resume PM message with auto flag can change power state to SNDRV_CTL_POWER_D0 in __usb_audio_resume().
However, when system is not under auto suspend, resume PM message with auto flag might not be able to receive on time which cause the power state was incorrect. At this time, if a player starts to play sound, will cause snd_usb_pcm_open() to access the card and setup_hw_info() will resume the card.
But even the card is back to work and all function normal, the power state is still in SNDRV_CTL_POWER_D3hot.
Hm, in exactly which situation does this happen? I still don't get it. Could you elaborate how to trigger this?
I'm not sure if this will happen on laptop or on PC. We've found this issue on Android phone (I'm not sure if each Android phone can reproduce this.).
After booting the android phone, insert type-c headset without charging and play music at any duration, say, 1 second, then stop. Put phone away to idle about 17~18 minutes. Wait auto pm happened and the power state change to SNDRV_CTL_POWER_D3hot in sound/usb/card.c. Then wake up the phone, play music again. Then you'll probably found the music was not playing and the progress bar keep at the same position. It only happen when power state is SNDRV_CTL_POWER_D3hot. If not (the power state is SNDRV_CTL_POWER_D0), repeat the steps for several times, then it will be produced at some time.
When it happened, sound_usb_pcm_open() will wake up the sound card by setup_hw_info()->__usb_audio_resume(). However, the card and the interface is function properly right now, the power state keeps remain SNDRV_CTL_POWER_D3hot. The suggestive parameter settings from upper sound request will be pending since later snd_power_wait() call will still wait the card awaken. Ideally, auto PM should be recovered by sound card itself. But once the card is awaken at this circumstance, it looks like there are not more auto pm event. And the sound system of this interface will stuck here forever until user plug out the headset (reset the hardware).
The root cause is that once the card has been resumed, it should inform auto pm change the state back into SNDRV_CTL_POWER_D0 and mark the device is using by some one.
Which cause the infinite loop happened in snd_power_wait() to check the power state. Thus the successive setting ioctl cannot be passed to card.
Hence we suggest to change power state to SNDRV_CTL_POWER_D0 when card has been resumed successfully.
This doesn't look like a right solution for the problem, sorry. The card PM status must be recovered to D0 when the autoresume succeeds. If not, something is broken there, and it must be fixed instead of fiddling the status flag externally.
Yes, I agreed, but after checking the code in sound drivers, it looks like there is only chance that auto pm triggered by low-level code in sound/usb/card.c. In kernel 4.14, auto pm suspend is triggered by snd_pcm_suspend_all(). In later kernel, it is triggered by snd_usb_pcm_suspend(). However, it looks like there are no any resume trigger to recover auto pm state when the card has been waken by sound_usb_pcm_open(). The remain resume trigger in sound/core/pcm_native.c were all static. I've tried to use these resume function in sound/usb/card.c but it seems cannot get better result than changing the power state when sound card is in use.
I've replied another mail earlier includes debug patch and the other work around to verify this issue. The issue has been found on kernel-4.14, but check the code logic here in sound/usb/card.c and sound/usb/pcm.c, I think the same problem still existed in 4.19, 5.4 (used by android), and in current kernel tree.
thanks,
Takashi
If the above explanation were not clear enough, I'll try my best to explain it in more detail. Maybe the better way is to send both auto pm resume and runtime resume when sound_usb_pcm_open() is called. But according to the current codes in card.c, we might need to call __usb_audio_resume() twice in setup_hw_info().
Thanks Macpaul Lin