Skip to contentSkip to conversation

usb: musb: fix idling for suspend after disconnect interrupt

PATCH In review Tony Lindgren 4 participants 13 messages Nov 26, 2019 – Oct 29, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: musb: fix idling for suspend after disconnect interrupt

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we have an active session. We have no other interrupts after disconnect coming in this case at least with the omap2430 glue.

Let's fix the issue by checking the interrupt status again with delayed work for the devctl 99 case. In the suspend after disconnect case the devctl session bit has cleared by then and musb can idle. For a typical USB B-device connect case we just continue with normal interrupts.

Cc Merlijn Wajer <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Sebastian Reichel <[email protected]>
---
 drivers/usb/musb/musb_core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/usb/musb/musb_core.c +8 -0
@@ -1943,6 +1943,9 @@ ATTRIBUTE_GROUPS(musb);
 #define MUSB_QUIRK_B_INVALID_VBUS_91	(MUSB_DEVCTL_BDEVICE | \
 					 (2 << MUSB_DEVCTL_VBUS_SHIFT) | \
 					 MUSB_DEVCTL_SESSION)
+#define MUSB_QUIRK_B_DISCONNECT_99	(MUSB_DEVCTL_BDEVICE | \
+					 (3 << MUSB_DEVCTL_VBUS_SHIFT) | \
+					 MUSB_DEVCTL_SESSION)
 #define MUSB_QUIRK_A_DISCONNECT_19	((3 << MUSB_DEVCTL_VBUS_SHIFT) | \
 					 MUSB_DEVCTL_SESSION)
 
@@ -1965,6 +1968,11 @@ static void musb_pm_runtime_check_session(struct musb *musb)
 	s = MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV |
 		MUSB_DEVCTL_HR;
 	switch (devctl & ~s) {
+	case MUSB_QUIRK_B_DISCONNECT_99:
+		musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
+		schedule_delayed_work(&musb->irq_work,
+				      msecs_to_jiffies(1000));
+		break;
 	case MUSB_QUIRK_B_INVALID_VBUS_91:
 		if (musb->quirk_retries && !musb->flush_irq_work) {
 			musb_dbg(musb,
Signature · 1 line
2.24.0
#1 lore raw

Hello!

Quoted 3 lines · Tony Lindgren

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we

Thinks?

Quoted 9 lines

have an active session. We have no other interrupts after disconnect coming in this case at least with the omap2430 glue.

Let's fix the issue by checking the interrupt status again with delayed work for the devctl 99 case. In the suspend after disconnect case the devctl session bit has cleared by then and musb can idle. For a typical USB B-device connect case we just continue with normal interrupts.

Quoted 4 lines
Cc Merlijn Wajer <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Sebastian Reichel <[email protected]>

[...]

MBR, Sergei

#2 lore raw
Quoted 9 lines · Sergei Shtylyov

Hello!

Quoted 3 lines · Tony Lindgren

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we

Thinks?

Thanks will fix.

Tony

maintainer
#5 lore raw
In reply to Tony Lindgren

Hi Tony,

Sorry for my late response.

Quoted 5 lines · Tony Lindgren

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we have an active session. We have no other interrupts after disconnect coming in this case at least with the omap2430 glue.

I don't have an omap2430 platform to test, but its musb doesn't generate DISCONNECT interrupt at all when disconnected from USB host in any case? It is a surprise, the musb core driver expects the DISCONNECT interrupt after VBUS is lost and relies on it to tear down the gadget driver and the state machine. I am wondering how its USB is functional without the DISCONNECT event...

-Bin.

Quoted 42 lines

Let's fix the issue by checking the interrupt status again with delayed work for the devctl 99 case. In the suspend after disconnect case the devctl session bit has cleared by then and musb can idle. For a typical USB B-device connect case we just continue with normal interrupts.

Cc Merlijn Wajer <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Sebastian Reichel <[email protected]>
---
 drivers/usb/musb/musb_core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/usb/musb/musb_core.c +8 -0
@@ -1943,6 +1943,9 @@ ATTRIBUTE_GROUPS(musb);
 #define MUSB_QUIRK_B_INVALID_VBUS_91	(MUSB_DEVCTL_BDEVICE | \
 					 (2 << MUSB_DEVCTL_VBUS_SHIFT) | \
 					 MUSB_DEVCTL_SESSION)
+#define MUSB_QUIRK_B_DISCONNECT_99	(MUSB_DEVCTL_BDEVICE | \
+					 (3 << MUSB_DEVCTL_VBUS_SHIFT) | \
+					 MUSB_DEVCTL_SESSION)
 #define MUSB_QUIRK_A_DISCONNECT_19	((3 << MUSB_DEVCTL_VBUS_SHIFT) | \
 					 MUSB_DEVCTL_SESSION)
 
@@ -1965,6 +1968,11 @@ static void musb_pm_runtime_check_session(struct musb *musb)
 	s = MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV |
 		MUSB_DEVCTL_HR;
 	switch (devctl & ~s) {
+	case MUSB_QUIRK_B_DISCONNECT_99:
+		musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
+		schedule_delayed_work(&musb->irq_work,
+				      msecs_to_jiffies(1000));
+		break;
 	case MUSB_QUIRK_B_INVALID_VBUS_91:
 		if (musb->quirk_retries && !musb->flush_irq_work) {
 			musb_dbg(musb,
Signature · 1 line
2.24.0
#6 lore raw

Hi,

Quoted 3 lines · Bin Liu

Hi Tony,

Sorry for my late response.

No worries.

Quoted 7 lines · Tony Lindgren
Quoted 5 lines

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we have an active session. We have no other interrupts after disconnect coming in this case at least with the omap2430 glue.

Quoted 6 lines

I don't have an omap2430 platform to test, but its musb doesn't generate DISCONNECT interrupt at all when disconnected from USB host in any case? It is a surprise, the musb core driver expects the DISCONNECT interrupt after VBUS is lost and relies on it to tear down the gadget driver and the state machine. I am wondering how its USB is functional without the DISCONNECT event...

We do get DISCONNECT, but we can then get a SUSPEND after DISCONNECT has already happened..

That will wake up musb waiting for further interrupts thinking it's connected. But after that there are no more interrupts as the cable is disconnected so we need to poll the status again.

If we see SUSPEND before DISCONNECT, then things idle fine.

Regards,

Tony

maintainer
#7 lore raw
Quoted 23 lines · Tony Lindgren

Hi,

Quoted 3 lines · Bin Liu

Hi Tony,

Sorry for my late response.

No worries.

Quoted 7 lines · Tony Lindgren
Quoted 5 lines

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we have an active session. We have no other interrupts after disconnect coming in this case at least with the omap2430 glue.

Quoted 6 lines

I don't have an omap2430 platform to test, but its musb doesn't generate DISCONNECT interrupt at all when disconnected from USB host in any case? It is a surprise, the musb core driver expects the DISCONNECT interrupt after VBUS is lost and relies on it to tear down the gadget driver and the state machine. I am wondering how its USB is functional without the DISCONNECT event...

Quoted 2 lines

We do get DISCONNECT, but we can then get a SUSPEND after DISCONNECT has already happened..

Ahh, I missed the first sentence in the commit log... Now I understand the issue. Thanks.

Quoted 6 lines

That will wake up musb waiting for further interrupts thinking it's connected. But after that there are no more interrupts as the cable is disconnected so we need to poll the status again.

If we see SUSPEND before DISCONNECT, then things idle fine.

Does SUSPEND always comes after DISCONNECT on omap2430, or just sometimes? I guess the USB connector has some issue - when DP/DM pins are disconnected, SUSPEND interrupt is generated; when VBUS/GND pins are disconnected, DISCONNECT interrupt is generated. Because DP/DM pins are shorter than VBUS/GND, SUSPEND should come before DISCONNECT.

-Bin.

#8 lore raw
Quoted 5 lines · Bin Liu
Quoted 4 lines · Tony Lindgren

That will wake up musb waiting for further interrupts thinking it's connected. But after that there are no more interrupts as the cable is disconnected so we need to poll the status again.

Quoted 7 lines
Quoted 1 line

If we see SUSPEND before DISCONNECT, then things idle fine.

Does SUSPEND always comes after DISCONNECT on omap2430, or just sometimes? I guess the USB connector has some issue - when DP/DM pins are disconnected, SUSPEND interrupt is generated; when VBUS/GND pins are disconnected, DISCONNECT interrupt is generated. Because DP/DM pins are shorter than VBUS/GND, SUSPEND should come before DISCONNECT.

No this does not always happen, but happens annoyingly often for me to notice. Your explanation sounds good though, and also sounds that can be affected by the wear on a micro-USB connector.

Regards,

Tony

maintainer
#3 lore raw
In reply to Tony Lindgren
Quoted 5 lines · Tony Lindgren
Quoted 4 lines · Sergei Shtylyov

Hello!

On 26.11.2019 6:41, Tony Lindgren wrote:

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we

Thinks?

Thanks will fix.

Is this the only update needed? I can fix it locally when queuing the patch.

-Bin.

#4 lore raw
Quoted 10 lines · Bin Liu
Quoted 9 lines · Tony Lindgren
Quoted 8 lines · Sergei Shtylyov

Hello!

Quoted 3 lines · Tony Lindgren

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Thinks?

Thanks will fix.

Is this the only update needed? I can fix it locally when queuing the patch.

So it seems. Maybe also add your explanation for why the wrong ordering of interrupts can happen while at it.

Regards,

Tony

#9 lore raw
In reply to Tony Lindgren

Hi Tony,

Quoted 5 lines · Tony Lindgren

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we have an active session. We have no other interrupts after disconnect coming in this case at least with the omap2430 glue.

So I had been debugging a issue with musb_hrdc driver preventing a suspend on the pinephone, which is Allwinner A64 platform.

Namely, if I have USB connected, and I try to suspend, it would hang until USB is disconnected. After enabling tracing, I realized that is hanging after this commit. Reverting it makes device suspend and resume correctly.

Some more of debugging notes can be found at,

https://gitlab.com/postmarketOS/pmaports/-/issues/839

I wonder what would be right solution here? Disable this quirk somehow for device?

Regards

Quoted 41 lines

Let's fix the issue by checking the interrupt status again with delayed work for the devctl 99 case. In the suspend after disconnect case the devctl session bit has cleared by then and musb can idle. For a typical USB B-device connect case we just continue with normal interrupts.

Cc Merlijn Wajer <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Sebastian Reichel <[email protected]>
---
 drivers/usb/musb/musb_core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/usb/musb/musb_core.c +8 -0
@@ -1943,6 +1943,9 @@ ATTRIBUTE_GROUPS(musb);
 #define MUSB_QUIRK_B_INVALID_VBUS_91	(MUSB_DEVCTL_BDEVICE | \
 					 (2 << MUSB_DEVCTL_VBUS_SHIFT) | \
 					 MUSB_DEVCTL_SESSION)
+#define MUSB_QUIRK_B_DISCONNECT_99	(MUSB_DEVCTL_BDEVICE | \
+					 (3 << MUSB_DEVCTL_VBUS_SHIFT) | \
+					 MUSB_DEVCTL_SESSION)
 #define MUSB_QUIRK_A_DISCONNECT_19	((3 << MUSB_DEVCTL_VBUS_SHIFT) | \
 					 MUSB_DEVCTL_SESSION)
 
@@ -1965,6 +1968,11 @@ static void musb_pm_runtime_check_session(struct musb *musb)
 	s = MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV |
 		MUSB_DEVCTL_HR;
 	switch (devctl & ~s) {
+	case MUSB_QUIRK_B_DISCONNECT_99:
+		musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
+		schedule_delayed_work(&musb->irq_work,
+				      msecs_to_jiffies(1000));
+		break;
 	case MUSB_QUIRK_B_INVALID_VBUS_91:
 		if (musb->quirk_retries && !musb->flush_irq_work) {
 			musb_dbg(musb,
Signature · 1 line
2.24.0
Signature · 4 lines
Bhushan Shah
http://blog.bshah.in
IRC Nick : bshah on Freenode
GPG key fingerprint : 0AAC 775B B643 7A8D 9AF7 A3AC FE07 8411 7FBC E11D
#10 lore raw

Hi,

Quoted 11 lines · Bhushan Shah

Hi Tony,

Quoted 5 lines · Tony Lindgren

When disconnected as USB B-device, we sometimes get a suspend interrupt after disconnect interrupt. In that case we have devctl set to 99 with VBUS still valid and musb_pm_runtime_check_session() wrongly things we have an active session. We have no other interrupts after disconnect coming in this case at least with the omap2430 glue.

So I had been debugging a issue with musb_hrdc driver preventing a suspend on the pinephone, which is Allwinner A64 platform.

Great. After that you might want to take a look and make sure that musb also gets idled properly during runtime with no devices connected :)

Quoted 11 lines

Namely, if I have USB connected, and I try to suspend, it would hang until USB is disconnected. After enabling tracing, I realized that is hanging after this commit. Reverting it makes device suspend and resume correctly.

Some more of debugging notes can be found at,

https://gitlab.com/postmarketOS/pmaports/-/issues/839

I wonder what would be right solution here? Disable this quirk somehow for device?

Hmm maybe we're just missing the check for suspend here. Maybe give the following untested patch a try?

I'll give it a try here too but it might be few days.

Seems like we might be able to eventually simplify the suspend and quirk check stuff, but let's fix the $subject issue first.

Regards,

Tony

8< ----------------------

drivers/usb/musb/musb_core.c +8 -4
@@ -2005,10 +2005,14 @@ static void musb_pm_runtime_check_session(struct musb *musb)
 		MUSB_DEVCTL_HR;
 	switch (devctl & ~s) {
 	case MUSB_QUIRK_B_DISCONNECT_99:
-		musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
-		schedule_delayed_work(&musb->irq_work,
-				      msecs_to_jiffies(1000));
-		break;
+		if (musb->quirk_retries && !musb->flush_irq_work) {
+			musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
+			schedule_delayed_work(&musb->irq_work,
+					      msecs_to_jiffies(1000));
+			musb->quirk_retries--;
+			break;
+		}
+		/* fall through */
 	case MUSB_QUIRK_B_INVALID_VBUS_91:
 		if (musb->quirk_retries && !musb->flush_irq_work) {
 			musb_dbg(musb,
Tested-by
#11 lore raw

Hello!

Quoted 4 lines · Tony Lindgren

Hmm maybe we're just missing the check for suspend here. Maybe give the following untested patch a try?

I'll give it a try here too but it might be few days.

Thanks for quick patch! I tested this on my device and I can confirm that it fixes issue for me.

So from my side,

Thanks!

Quoted 30 lines

Seems like we might be able to eventually simplify the suspend and quirk check stuff, but let's fix the $subject issue first.

Regards,

Tony

8< ----------------------

drivers/usb/musb/musb_core.c +8 -4
@@ -2005,10 +2005,14 @@ static void musb_pm_runtime_check_session(struct musb *musb)
 		MUSB_DEVCTL_HR;
 	switch (devctl & ~s) {
 	case MUSB_QUIRK_B_DISCONNECT_99:
-		musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
-		schedule_delayed_work(&musb->irq_work,
-				      msecs_to_jiffies(1000));
-		break;
+		if (musb->quirk_retries && !musb->flush_irq_work) {
+			musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
+			schedule_delayed_work(&musb->irq_work,
+					      msecs_to_jiffies(1000));
+			musb->quirk_retries--;
+			break;
+		}
+		/* fall through */
 	case MUSB_QUIRK_B_INVALID_VBUS_91:
 		if (musb->quirk_retries && !musb->flush_irq_work) {
 			musb_dbg(musb,
Signature · 4 lines
Bhushan Shah
http://blog.bshah.in
IRC Nick : bshah on Freenode
GPG key fingerprint : 0AAC 775B B643 7A8D 9AF7 A3AC FE07 8411 7FBC E11D
#12 lore raw
Quoted 6 lines · Bhushan Shah

Hello!

Quoted 3 lines · Tony Lindgren

Hmm maybe we're just missing the check for suspend here. Maybe give the following untested patch a try?

Quoted 8 lines
Quoted 1 line

I'll give it a try here too but it might be few days.

Thanks for quick patch! I tested this on my device and I can confirm that it fixes issue for me.

So from my side,

OK good to hear :) Will post a proper patch after testing here too.

Regards,

Tony

Quoted 36 lines
Quoted 30 lines

Seems like we might be able to eventually simplify the suspend and quirk check stuff, but let's fix the $subject issue first.

Regards,

Tony

8< ----------------------

drivers/usb/musb/musb_core.c +8 -4
@@ -2005,10 +2005,14 @@ static void musb_pm_runtime_check_session(struct musb *musb)
 		MUSB_DEVCTL_HR;
 	switch (devctl & ~s) {
 	case MUSB_QUIRK_B_DISCONNECT_99:
-		musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
-		schedule_delayed_work(&musb->irq_work,
-				      msecs_to_jiffies(1000));
-		break;
+		if (musb->quirk_retries && !musb->flush_irq_work) {
+			musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
+			schedule_delayed_work(&musb->irq_work,
+					      msecs_to_jiffies(1000));
+			musb->quirk_retries--;
+			break;
+		}
+		/* fall through */
 	case MUSB_QUIRK_B_INVALID_VBUS_91:
 		if (musb->quirk_retries && !musb->flush_irq_work) {
 			musb_dbg(musb,
Signature · 4 lines
Bhushan Shah
http://blog.bshah.in
IRC Nick : bshah on Freenode
GPG key fingerprint : 0AAC 775B B643 7A8D 9AF7 A3AC FE07 8411 7FBC E11D