Linux USB
 help / color / mirror / Atom feed
From: Roger Quadros <rogerq@kernel.org>
To: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
Cc: "gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: dwc3 gadget: controller stop times out on system sleep
Date: Thu, 27 Apr 2023 19:26:05 +0300	[thread overview]
Message-ID: <dafed705-292e-6c03-7599-1b1374b95122@kernel.org> (raw)
In-Reply-To: <20230426200140.xa4unnbaw2vog2je@synopsys.com>

Hi,

On 26/04/2023 23:01, Thinh Nguyen wrote:
> Hi,
> 
> On Wed, Apr 26, 2023, Roger Quadros wrote:
>> 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
> 
> 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

  reply	other threads:[~2023-04-27 16:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-26 16:17 dwc3 gadget: controller stop times out on system sleep Roger Quadros
2023-04-26 20:01 ` Thinh Nguyen
2023-04-27 16:26   ` Roger Quadros [this message]
2023-04-27 22:13     ` Thinh Nguyen
2023-04-28 12:04       ` Roger Quadros
2023-04-28 18:33         ` Thinh Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dafed705-292e-6c03-7599-1b1374b95122@kernel.org \
    --to=rogerq@kernel.org \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox