Skip to contentSkip to conversation

dwc3 gadget: controller stop times out on system sleep

DISCUSSION Roger Quadros 2 participants 6 messages Apr 26, 2023 – Apr 28, 2023

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

Hi Thinh,

On Linux kernel v6.3
Test procedure:

- modprobe g_zero
- Connect to PC host
- systemctl suspend

A large delay of 3 seconds is observed. The delay comes from dwc3_gadget_suspend()->dwc3_gadget_run_stop() waiting for DWC3_DSTS_DEVCTRLHLT to be set. It returns -ETIMEDOUT.

Are we missing something to do a clean stop during suspend?

FYI. Unloading g_zero does not show this delay on stop.

cheers,
-roger

maintainer
#1 lore raw

Hi,

Quoted 12 lines · Roger Quadros

Hi Thinh,

On Linux kernel v6.3
Test procedure:

- modprobe g_zero
- Connect to PC host
- systemctl suspend

A large delay of 3 seconds is observed. The delay comes from dwc3_gadget_suspend()->dwc3_gadget_run_stop() waiting for DWC3_DSTS_DEVCTRLHLT to be set. It returns -ETIMEDOUT.

Quoted 6 lines

Are we missing something to do a clean stop during suspend?

FYI. Unloading g_zero does not show this delay on stop.

cheers,
-roger

When clearing run_stop bit and the controller doesn't halt, that usually means there are active transfers/endpoints that aren't ended yet.

The dwc3_gadget_suspend() doesn't properly do all the cleanup before clearing the run_stop bit. I think you just need to call dwc3_gadget_soft_disconnect() in dwc3_gadget_suspend() to fix this.

Thanks,
Thinh

#2 lore raw

Hi,

Quoted 22 lines · Thinh Nguyen

Hi,

Quoted 12 lines · Roger Quadros

Hi Thinh,

On Linux kernel v6.3
Test procedure:

- modprobe g_zero
- Connect to PC host
- systemctl suspend

A large delay of 3 seconds is observed. The delay comes from dwc3_gadget_suspend()->dwc3_gadget_run_stop() waiting for DWC3_DSTS_DEVCTRLHLT to be set. It returns -ETIMEDOUT.

Quoted 6 lines

Are we missing something to do a clean stop during suspend?

FYI. Unloading g_zero does not show this delay on stop.

cheers,
-roger

Quoted 6 lines

When clearing run_stop bit and the controller doesn't halt, that usually means there are active transfers/endpoints that aren't ended yet.

The dwc3_gadget_suspend() doesn't properly do all the cleanup before clearing the run_stop bit. I think you just need to call dwc3_gadget_soft_disconnect() in dwc3_gadget_suspend() to fix this.

That seems to do the trick.
How does this look?

-------------------------- drivers/usb/dwc3/gadget.c -------------------------- @@ -4674,11 +4676,18 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
int dwc3_gadget_suspend(struct dwc3 *dwc)
{
unsigned long flags;
+ int ret;

- if (!dwc->gadget_driver)
+ if (!dwc->gadget_driver || !dwc->softconnect)
return 0;

- dwc3_gadget_run_stop(dwc, false, false);
+ ret = dwc3_gadget_soft_disconnect(dwc);
+ if (ret)
+ goto err0;
+
+ ret = dwc3_gadget_run_stop(dwc, false, false);
+ if (ret)
+ goto err1;

spin_lock_irqsave(&dwc->lock, flags); dwc3_disconnect_gadget(dwc);
@@ -4686,6 +4695,22 @@ int dwc3_gadget_suspend(struct dwc3 *dwc)
spin_unlock_irqrestore(&dwc->lock, flags);

return 0;
+
+err1:
+ /*
+ * In the Synopsys DWC_usb31 1.90a programming guide section
+ * 4.1.9, it specifies that for a reconnect after a
+ * device-initiated disconnect requires a core soft reset
+ * (DCTL.CSftRst) before enabling the run/stop bit.
+ */
+ dwc3_core_soft_reset(dwc);
+ dwc3_event_buffers_setup(dwc);
+
+ ret = dwc3_gadget_run_stop(dwc, true, false);
+
+err0:
+ dev_info(dwc->dev, "%s error %d\n", __func__, ret);
+ return ret;
}

int dwc3_gadget_resume(struct dwc3 *dwc)
@@ -4695,6 +4720,15 @@ int dwc3_gadget_resume(struct dwc3 *dwc)
if (!dwc->gadget_driver || !dwc->softconnect)
return 0;

+ /*
+ * In the Synopsys DWC_usb31 1.90a programming guide section
+ * 4.1.9, it specifies that for a reconnect after a
+ * device-initiated disconnect requires a core soft reset
+ * (DCTL.CSftRst) before enabling the run/stop bit.
+ */
+ dwc3_core_soft_reset(dwc);
+ dwc3_event_buffers_setup(dwc);
+
ret = __dwc3_gadget_start(dwc);
if (ret < 0)
goto err0;

--
cheers,
-roger

maintainer
#3 lore raw
Quoted 45 lines · Roger Quadros

Hi,

Quoted 22 lines · Thinh Nguyen

Hi,

Quoted 12 lines · Roger Quadros

Hi Thinh,

On Linux kernel v6.3
Test procedure:

- modprobe g_zero
- Connect to PC host
- systemctl suspend

A large delay of 3 seconds is observed. The delay comes from dwc3_gadget_suspend()->dwc3_gadget_run_stop() waiting for DWC3_DSTS_DEVCTRLHLT to be set. It returns -ETIMEDOUT.

Quoted 6 lines

Are we missing something to do a clean stop during suspend?

FYI. Unloading g_zero does not show this delay on stop.

cheers,
-roger

Quoted 6 lines

When clearing run_stop bit and the controller doesn't halt, that usually means there are active transfers/endpoints that aren't ended yet.

The dwc3_gadget_suspend() doesn't properly do all the cleanup before clearing the run_stop bit. I think you just need to call dwc3_gadget_soft_disconnect() in dwc3_gadget_suspend() to fix this.

That seems to do the trick.
How does this look?

-------------------------- drivers/usb/dwc3/gadget.c --------------------------

(quoted diff) +2 -1
@@ -4674,11 +4676,18 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 int dwc3_gadget_suspend(struct dwc3 *dwc)
 {
 	unsigned long flags;
+	int ret;
 
-	if (!dwc->gadget_driver)
+	if (!dwc->gadget_driver || !dwc->softconnect)
 		return 0;

Review void dwc3_gadget_exit(struct dwc3 *dwc) · line 4691

Quoted 10 lines
(quoted diff) +7 -1
 
-	dwc3_gadget_run_stop(dwc, false, false);
+	ret = dwc3_gadget_soft_disconnect(dwc);
+	if (ret)
+		goto err0;
+
+	ret = dwc3_gadget_run_stop(dwc, false, false);
+	if (ret)
+		goto err1;
 

We already clear run_stop in dwc3_gadget_soft_disconnect().

Can you try the following change (not tested):

drivers/usb/dwc3/gadget.c +34 -33
index c0ca4d12f95d..2996bcb4d53d 100644
@@ -2699,6 +2699,21 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
 	return ret;
 }
 
+static int dwc3_gadget_soft_connect(struct dwc3 *dwc)
+{
+	/*
+	 * In the Synopsys DWC_usb31 1.90a programming guide section
+	 * 4.1.9, it specifies that for a reconnect after a
+	 * device-initiated disconnect requires a core soft reset
+	 * (DCTL.CSftRst) before enabling the run/stop bit.
+	 */
+	dwc3_core_soft_reset(dwc);
+
+	dwc3_event_buffers_setup(dwc);
+	__dwc3_gadget_start(dwc);
+	return dwc3_gadget_run_stop(dwc, true);
+}
+
 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
 {
 	struct dwc3		*dwc = gadget_to_dwc(g);
@@ -2737,21 +2752,10 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
 
 	synchronize_irq(dwc->irq_gadget);
 
-	if (!is_on) {
+	if (!is_on)
 		ret = dwc3_gadget_soft_disconnect(dwc);
-	} else {
-		/*
-		 * In the Synopsys DWC_usb31 1.90a programming guide section
-		 * 4.1.9, it specifies that for a reconnect after a
-		 * device-initiated disconnect requires a core soft reset
-		 * (DCTL.CSftRst) before enabling the run/stop bit.
-		 */
-		dwc3_core_soft_reset(dwc);
-
-		dwc3_event_buffers_setup(dwc);
-		__dwc3_gadget_start(dwc);
-		ret = dwc3_gadget_run_stop(dwc, true);
-	}
+	else
+		ret = dwc3_gadget_soft_connect(dwc);
 
 	pm_runtime_put(dwc->dev);
 
@@ -4655,42 +4659,39 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 int dwc3_gadget_suspend(struct dwc3 *dwc)
 {
 	unsigned long flags;
+	int ret;
 
 	if (!dwc->gadget_driver)
 		return 0;
 
-	dwc3_gadget_run_stop(dwc, false);
+	ret = dwc3_gadget_soft_disconnect(dwc);
+	if (ret)
+		goto err;
 
 	spin_lock_irqsave(&dwc->lock, flags);
 	dwc3_disconnect_gadget(dwc);
-	__dwc3_gadget_stop(dwc);
 	spin_unlock_irqrestore(&dwc->lock, flags);
 
 	return 0;
+
+err:
+	/*
+	 * Attempt to reset the controller's state. Likely no
+	 * communication can be established until the host
+	 * performs a port reset.
+	 */
+	if (dwc->softconnect)
+		dwc3_gadget_soft_connect(dwc);
+
+	return ret;
 }
 
 int dwc3_gadget_resume(struct dwc3 *dwc)
 {
-	int			ret;
-
 	if (!dwc->gadget_driver || !dwc->softconnect)
 		return 0;
 
-	ret = __dwc3_gadget_start(dwc);
-	if (ret < 0)
-		goto err0;
-
-	ret = dwc3_gadget_run_stop(dwc, true);
-	if (ret < 0)
-		goto err1;
-
-	return 0;
-
-err1:
-	__dwc3_gadget_stop(dwc);
-
-err0:
-	return ret;
+	return dwc3_gadget_soft_connect(dwc);
 }
 
 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)

--

Thanks,
Thinh

#4 lore raw
Quoted 72 lines · Thinh Nguyen
Quoted 45 lines · Roger Quadros

Hi,

Quoted 22 lines · Thinh Nguyen

Hi,

Quoted 12 lines · Roger Quadros

Hi Thinh,

On Linux kernel v6.3
Test procedure:

- modprobe g_zero
- Connect to PC host
- systemctl suspend

A large delay of 3 seconds is observed. The delay comes from dwc3_gadget_suspend()->dwc3_gadget_run_stop() waiting for DWC3_DSTS_DEVCTRLHLT to be set. It returns -ETIMEDOUT.

Quoted 6 lines

Are we missing something to do a clean stop during suspend?

FYI. Unloading g_zero does not show this delay on stop.

cheers,
-roger

Quoted 6 lines

When clearing run_stop bit and the controller doesn't halt, that usually means there are active transfers/endpoints that aren't ended yet.

The dwc3_gadget_suspend() doesn't properly do all the cleanup before clearing the run_stop bit. I think you just need to call dwc3_gadget_soft_disconnect() in dwc3_gadget_suspend() to fix this.

That seems to do the trick.
How does this look?

-------------------------- drivers/usb/dwc3/gadget.c --------------------------

drivers/usb/dwc3/gadget.c +2 -1
@@ -4674,11 +4676,18 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 int dwc3_gadget_suspend(struct dwc3 *dwc)
 {
 	unsigned long flags;
+	int ret;
 
-	if (!dwc->gadget_driver)
+	if (!dwc->gadget_driver || !dwc->softconnect)
 		return 0;

drivers/usb/dwc3/gadget.c void dwc3_gadget_exit(struct dwc3 *dwc) · line 4691

Quoted 10 lines
drivers/usb/dwc3/gadget.c +7 -1
 
-	dwc3_gadget_run_stop(dwc, false, false);
+	ret = dwc3_gadget_soft_disconnect(dwc);
+	if (ret)
+		goto err0;
+
+	ret = dwc3_gadget_run_stop(dwc, false, false);
+	if (ret)
+		goto err1;
 

We already clear run_stop in dwc3_gadget_soft_disconnect().

Can you try the following change (not tested):

drivers/usb/dwc3/gadget.c +3 -0
index c0ca4d12f95d..2996bcb4d53d 100644
@@ -2699,6 +2699,21 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
 	return ret;
 }
 
+static int dwc3_gadget_soft_connect(struct dwc3 *dwc)
+{
+	/*

drivers/usb/dwc3/gadget.c static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) · line 2714

Quoted 10 lines
drivers/usb/dwc3/gadget.c +10 -0
+	 * In the Synopsys DWC_usb31 1.90a programming guide section
+	 * 4.1.9, it specifies that for a reconnect after a
+	 * device-initiated disconnect requires a core soft reset
+	 * (DCTL.CSftRst) before enabling the run/stop bit.
+	 */
+	dwc3_core_soft_reset(dwc);
+
+	dwc3_event_buffers_setup(dwc);
+	__dwc3_gadget_start(dwc);
+	return dwc3_gadget_run_stop(dwc, true);

return dwc3_gadget_run_stop(dwc, true, false);

Quoted 30 lines
drivers/usb/dwc3/gadget.c +4 -14
+}> +
 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
 {
 	struct dwc3		*dwc = gadget_to_dwc(g);
@@ -2737,21 +2752,10 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
 
 	synchronize_irq(dwc->irq_gadget);
 
-	if (!is_on) {
+	if (!is_on)
 		ret = dwc3_gadget_soft_disconnect(dwc);
-	} else {
-		/*
-		 * In the Synopsys DWC_usb31 1.90a programming guide section
-		 * 4.1.9, it specifies that for a reconnect after a
-		 * device-initiated disconnect requires a core soft reset
-		 * (DCTL.CSftRst) before enabling the run/stop bit.
-		 */
-		dwc3_core_soft_reset(dwc);
-
-		dwc3_event_buffers_setup(dwc);
-		__dwc3_gadget_start(dwc);
-		ret = dwc3_gadget_run_stop(dwc, true);
-	}
+	else
+		ret = dwc3_gadget_soft_connect(dwc);
 
 	pm_runtime_put(dwc->dev);
 

drivers/usb/dwc3/gadget.c void dwc3_gadget_exit(struct dwc3 *dwc) · line 4664

Quoted 6 lines
drivers/usb/dwc3/gadget.c +1 -0
@@ -4655,42 +4659,39 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 int dwc3_gadget_suspend(struct dwc3 *dwc)
 {
 	unsigned long flags;
+	int ret;
 
 	if (!dwc->gadget_driver)

We need to check for dwc->softconnect here. If it is not set that means controller has already stopped so we can simply exit.

Quoted 42 lines
drivers/usb/dwc3/gadget.c +14 -13
 		return 0;
 
-	dwc3_gadget_run_stop(dwc, false);
+	ret = dwc3_gadget_soft_disconnect(dwc);
+	if (ret)
+		goto err;
 
 	spin_lock_irqsave(&dwc->lock, flags);
 	dwc3_disconnect_gadget(dwc);
-	__dwc3_gadget_stop(dwc);
 	spin_unlock_irqrestore(&dwc->lock, flags);
 
 	return 0;
+
+err:
+	/*
+	 * Attempt to reset the controller's state. Likely no
+	 * communication can be established until the host
+	 * performs a port reset.
+	 */
+	if (dwc->softconnect)
+		dwc3_gadget_soft_connect(dwc);
+
+	return ret;
 }
 
 int dwc3_gadget_resume(struct dwc3 *dwc)
 {
-	int			ret;
-
 	if (!dwc->gadget_driver || !dwc->softconnect)
 		return 0;
 
-	ret = __dwc3_gadget_start(dwc);
-	if (ret < 0)
-		goto err0;
-
-	ret = dwc3_gadget_run_stop(dwc, true);
-	if (ret < 0)
-		goto err1;
-
-	return 0;

drivers/usb/dwc3/gadget.c void dwc3_gadget_exit(struct dwc3 *dwc) · line 4697

Quoted 10 lines
drivers/usb/dwc3/gadget.c +1 -6
-
-err1:
-	__dwc3_gadget_stop(dwc);
-
-err0:
-	return ret;
+	return dwc3_gadget_soft_connect(dwc);
 }
 
 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)

Everything else looks ok. I will send a patch soon.

--
cheers,
-roger

maintainer
#5 lore raw
Quoted 72 lines · Roger Quadros
Quoted 69 lines · Thinh Nguyen
Quoted 45 lines · Roger Quadros

Hi,

Quoted 22 lines · Thinh Nguyen

Hi,

Quoted 12 lines · Roger Quadros

Hi Thinh,

On Linux kernel v6.3
Test procedure:

- modprobe g_zero
- Connect to PC host
- systemctl suspend

A large delay of 3 seconds is observed. The delay comes from dwc3_gadget_suspend()->dwc3_gadget_run_stop() waiting for DWC3_DSTS_DEVCTRLHLT to be set. It returns -ETIMEDOUT.

Quoted 6 lines

Are we missing something to do a clean stop during suspend?

FYI. Unloading g_zero does not show this delay on stop.

cheers,
-roger

Quoted 6 lines

When clearing run_stop bit and the controller doesn't halt, that usually means there are active transfers/endpoints that aren't ended yet.

The dwc3_gadget_suspend() doesn't properly do all the cleanup before clearing the run_stop bit. I think you just need to call dwc3_gadget_soft_disconnect() in dwc3_gadget_suspend() to fix this.

That seems to do the trick.
How does this look?

-------------------------- drivers/usb/dwc3/gadget.c --------------------------

drivers/usb/dwc3/gadget.c +2 -1
@@ -4674,11 +4676,18 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 int dwc3_gadget_suspend(struct dwc3 *dwc)
 {
 	unsigned long flags;
+	int ret;
 
-	if (!dwc->gadget_driver)
+	if (!dwc->gadget_driver || !dwc->softconnect)
 		return 0;

drivers/usb/dwc3/gadget.c void dwc3_gadget_exit(struct dwc3 *dwc) · line 4691

Quoted 10 lines
drivers/usb/dwc3/gadget.c +7 -1
 
-	dwc3_gadget_run_stop(dwc, false, false);
+	ret = dwc3_gadget_soft_disconnect(dwc);
+	if (ret)
+		goto err0;
+
+	ret = dwc3_gadget_run_stop(dwc, false, false);
+	if (ret)
+		goto err1;
 

We already clear run_stop in dwc3_gadget_soft_disconnect().

Can you try the following change (not tested):

drivers/usb/dwc3/gadget.c +0 -0
index c0ca4d12f95d..2996bcb4d53d 100644
@@ -2699,6 +2699,21 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
 	return ret;
 }
 
Quoted 15 lines

drivers/usb/dwc3/gadget.c static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) · line 2714

Quoted 13 lines
drivers/usb/dwc3/gadget.c +13 -0
+static int dwc3_gadget_soft_connect(struct dwc3 *dwc)
+{
+	/*
+	 * In the Synopsys DWC_usb31 1.90a programming guide section
+	 * 4.1.9, it specifies that for a reconnect after a
+	 * device-initiated disconnect requires a core soft reset
+	 * (DCTL.CSftRst) before enabling the run/stop bit.
+	 */
+	dwc3_core_soft_reset(dwc);
+
+	dwc3_event_buffers_setup(dwc);
+	__dwc3_gadget_start(dwc);
+	return dwc3_gadget_run_stop(dwc, true);

return dwc3_gadget_run_stop(dwc, true, false);

You need to rebase your change to Greg's usb-next.

Quoted 36 lines
Quoted 36 lines
drivers/usb/dwc3/gadget.c +5 -14
+}> +
 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
 {
 	struct dwc3		*dwc = gadget_to_dwc(g);
@@ -2737,21 +2752,10 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
 
 	synchronize_irq(dwc->irq_gadget);
 
-	if (!is_on) {
+	if (!is_on)
 		ret = dwc3_gadget_soft_disconnect(dwc);
-	} else {
-		/*
-		 * In the Synopsys DWC_usb31 1.90a programming guide section
-		 * 4.1.9, it specifies that for a reconnect after a
-		 * device-initiated disconnect requires a core soft reset
-		 * (DCTL.CSftRst) before enabling the run/stop bit.
-		 */
-		dwc3_core_soft_reset(dwc);
-
-		dwc3_event_buffers_setup(dwc);
-		__dwc3_gadget_start(dwc);
-		ret = dwc3_gadget_run_stop(dwc, true);
-	}
+	else
+		ret = dwc3_gadget_soft_connect(dwc);
 
 	pm_runtime_put(dwc->dev);
 
@@ -4655,42 +4659,39 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 int dwc3_gadget_suspend(struct dwc3 *dwc)
 {
 	unsigned long flags;
+	int ret;
 
Quoted 4 lines

drivers/usb/dwc3/gadget.c void dwc3_gadget_exit(struct dwc3 *dwc) · line 4664

Quoted 1 line
drivers/usb/dwc3/gadget.c +0 -0
 	if (!dwc->gadget_driver)

We need to check for dwc->softconnect here. If it is not set that means controller has already stopped so we can simply exit.

That makes sense. Perhaps that can be a separate change?

Quoted 13 lines
Quoted 13 lines
drivers/usb/dwc3/gadget.c +3 -2
 		return 0;
 
-	dwc3_gadget_run_stop(dwc, false);
+	ret = dwc3_gadget_soft_disconnect(dwc);
+	if (ret)
+		goto err;
 
 	spin_lock_irqsave(&dwc->lock, flags);
 	dwc3_disconnect_gadget(dwc);
-	__dwc3_gadget_stop(dwc);
 	spin_unlock_irqrestore(&dwc->lock, flags);
 
Quoted 9 lines

drivers/usb/dwc3/gadget.c void dwc3_gadget_exit(struct dwc3 *dwc) · line 4683

Quoted 9 lines
drivers/usb/dwc3/gadget.c +8 -0
 	return 0;
+
+err:
+	/*
+	 * Attempt to reset the controller's state. Likely no
+	 * communication can be established until the host
+	 * performs a port reset.
+	 */
+	if (dwc->softconnect)

If we check dwc->softconnect at start, we can skip the check here.

Quoted 30 lines
Quoted 30 lines
drivers/usb/dwc3/gadget.c +4 -17
+		dwc3_gadget_soft_connect(dwc);
+
+	return ret;
 }
 
 int dwc3_gadget_resume(struct dwc3 *dwc)
 {
-	int			ret;
-
 	if (!dwc->gadget_driver || !dwc->softconnect)
 		return 0;
 
-	ret = __dwc3_gadget_start(dwc);
-	if (ret < 0)
-		goto err0;
-
-	ret = dwc3_gadget_run_stop(dwc, true);
-	if (ret < 0)
-		goto err1;
-
-	return 0;
-
-err1:
-	__dwc3_gadget_stop(dwc);
-
-err0:
-	return ret;
+	return dwc3_gadget_soft_connect(dwc);
 }
 
Quoted 4 lines

drivers/usb/dwc3/gadget.c void dwc3_gadget_exit(struct dwc3 *dwc) · line 4697

Quoted 1 line
drivers/usb/dwc3/gadget.c +0 -0
 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)

Everything else looks ok. I will send a patch soon.

Thanks,
Thinh