diff mbox series

[v2,2/2] usb: dwc3: Modify runtime pm ops to handle bus suspend

Message ID 1685141140-26228-3-git-send-email-quic_eserrao@quicinc.com (mailing list archive)
State Superseded
Headers show
Series Support dwc3 runtime suspend during bus suspend | expand

Commit Message

Elson Roy Serrao May 26, 2023, 10:45 p.m. UTC
For bus suspend scenario we should skip controller halt
as we still have the usb cable connected. So modify the
runtime pm ops to handle bus suspend scenario. Also invoke
autosuspend when device receives U3 notification so that
controller can enter runtime suspended state. Ensure that
the controller is brought out of runtime suspend before
triggering remote wakeup.

Signed-off-by: Elson Roy Serrao <quic_eserrao@quicinc.com>
---
 drivers/usb/dwc3/core.c   | 19 +++++++++++++++++++
 drivers/usb/dwc3/gadget.c | 40 +++++++++++++++++++++++++++++++++-------
 2 files changed, 52 insertions(+), 7 deletions(-)

Comments

Thinh Nguyen May 30, 2023, 10:51 p.m. UTC | #1
Hi,

On Fri, May 26, 2023, Elson Roy Serrao wrote:
> For bus suspend scenario we should skip controller halt
> as we still have the usb cable connected. So modify the

How can you know when the host requests for resume to wakeup the device?
We haven't implemented hibernation to handle that. If it's communicated
through the glue layer specific via the phy's event, then how do you
plan to make it generic and not specific to your platform?

The reason we halt the controller and force a soft-disconnect is because
we don't have a handle for this yet. Perhaps I'm missing something here
because I don't see you handle it here either.

> runtime pm ops to handle bus suspend scenario. Also invoke
> autosuspend when device receives U3 notification so that
> controller can enter runtime suspended state. Ensure that
> the controller is brought out of runtime suspend before
> triggering remote wakeup.
> 
> Signed-off-by: Elson Roy Serrao <quic_eserrao@quicinc.com>
> ---
>  drivers/usb/dwc3/core.c   | 19 +++++++++++++++++++
>  drivers/usb/dwc3/gadget.c | 40 +++++++++++++++++++++++++++++++++-------
>  2 files changed, 52 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index 278cd1c..7787805 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -2119,6 +2119,12 @@ static int dwc3_runtime_suspend(struct device *dev)
>  {
>  	struct dwc3     *dwc = dev_get_drvdata(dev);
>  	int		ret;
> +	u32		reg;
> +
> +	reg = dwc3_readl(dwc->regs, DWC3_DSTS);

This function isn't device mode specific, but you're checking device
mode specific status here.

> +	/* For bus suspend case do not halt the controller */
> +	if (dwc->connected && (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U3))
> +		return 0;
>  
>  	if (dwc3_runtime_checks(dwc))
>  		return -EBUSY;
> @@ -2135,6 +2141,12 @@ static int dwc3_runtime_resume(struct device *dev)
>  	struct dwc3     *dwc = dev_get_drvdata(dev);
>  	int		ret;
>  
> +	/* resume from bus suspend */
> +	if (dwc->connected) {
> +		dwc3_gadget_process_pending_events(dwc);
> +		goto resume;
> +	}
> +
>  	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
>  	if (ret)
>  		return ret;
> @@ -2149,6 +2161,7 @@ static int dwc3_runtime_resume(struct device *dev)
>  		break;
>  	}
>  
> +resume:
>  	pm_runtime_mark_last_busy(dev);
>  
>  	return 0;
> @@ -2157,9 +2170,14 @@ static int dwc3_runtime_resume(struct device *dev)
>  static int dwc3_runtime_idle(struct device *dev)
>  {
>  	struct dwc3     *dwc = dev_get_drvdata(dev);
> +	u32		reg;
>  
>  	switch (dwc->current_dr_role) {
>  	case DWC3_GCTL_PRTCAP_DEVICE:
> +		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
> +		/* for bus suspend case return success */
> +		if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U3 && dwc->connected)
> +			goto autosuspend;
>  		if (dwc3_runtime_checks(dwc))
>  			return -EBUSY;
>  		break;
> @@ -2169,6 +2187,7 @@ static int dwc3_runtime_idle(struct device *dev)
>  		break;
>  	}
>  
> +autosuspend:
>  	pm_runtime_mark_last_busy(dev);
>  	pm_runtime_autosuspend(dev);
>  
> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index 5965796..7587912 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -2400,15 +2400,21 @@ static int dwc3_gadget_wakeup(struct usb_gadget *g)
>  		return -EINVAL;
>  	}
>  
> -	spin_lock_irqsave(&dwc->lock, flags);
>  	if (!dwc->gadget->wakeup_armed) {
>  		dev_err(dwc->dev, "not armed for remote wakeup\n");
> -		spin_unlock_irqrestore(&dwc->lock, flags);
>  		return -EINVAL;
>  	}
> -	ret = __dwc3_gadget_wakeup(dwc, true);
>  
> +	ret = pm_runtime_resume_and_get(dwc->dev);
> +	if (ret < 0) {
> +		pm_runtime_set_suspended(dwc->dev);
> +		return ret;
> +	}
> +
> +	spin_lock_irqsave(&dwc->lock, flags);
> +	ret = __dwc3_gadget_wakeup(dwc, true);
>  	spin_unlock_irqrestore(&dwc->lock, flags);
> +	pm_runtime_put_noidle(dwc->dev);
>  
>  	return ret;
>  }
> @@ -2427,6 +2433,12 @@ static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
>  		return -EINVAL;
>  	}
>  
> +	ret = pm_runtime_resume_and_get(dwc->dev);
> +	if (ret < 0) {
> +		pm_runtime_set_suspended(dwc->dev);
> +		return ret;
> +	}
> +
>  	spin_lock_irqsave(&dwc->lock, flags);
>  	/*
>  	 * If the link is in U3, signal for remote wakeup and wait for the
> @@ -2437,6 +2449,7 @@ static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
>  		ret = __dwc3_gadget_wakeup(dwc, false);
>  		if (ret) {
>  			spin_unlock_irqrestore(&dwc->lock, flags);
> +			pm_runtime_put_noidle(dwc->dev);
>  			return -EINVAL;
>  		}
>  		dwc3_resume_gadget(dwc);
> @@ -2450,6 +2463,7 @@ static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
>  		dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret);
>  
>  	spin_unlock_irqrestore(&dwc->lock, flags);
> +	pm_runtime_put_noidle(dwc->dev);
>  
>  	return ret;
>  }
> @@ -2711,21 +2725,23 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
>  	/*
>  	 * Avoid issuing a runtime resume if the device is already in the
>  	 * suspended state during gadget disconnect.  DWC3 gadget was already
> -	 * halted/stopped during runtime suspend.
> +	 * halted/stopped during runtime suspend except for bus suspend case
> +	 * where we would have skipped the controller halt.
>  	 */
>  	if (!is_on) {
>  		pm_runtime_barrier(dwc->dev);
> -		if (pm_runtime_suspended(dwc->dev))
> +		if (pm_runtime_suspended(dwc->dev) && !dwc->connected)
>  			return 0;
>  	}
>  
>  	/*
>  	 * Check the return value for successful resume, or error.  For a
>  	 * successful resume, the DWC3 runtime PM resume routine will handle
> -	 * the run stop sequence, so avoid duplicate operations here.
> +	 * the run stop sequence except for bus resume case, so avoid
> +	 * duplicate operations here.
>  	 */
>  	ret = pm_runtime_get_sync(dwc->dev);
> -	if (!ret || ret < 0) {
> +	if ((!ret && !dwc->connected) || ret < 0) {
>  		pm_runtime_put(dwc->dev);
>  		return 0;
>  	}
> @@ -4313,6 +4329,8 @@ static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
>  		dwc3_suspend_gadget(dwc);
>  
>  	dwc->link_state = next;
> +	pm_runtime_mark_last_busy(dwc->dev);
> +	pm_request_autosuspend(dwc->dev);
>  }
>  
>  static void dwc3_gadget_interrupt(struct dwc3 *dwc,
> @@ -4703,7 +4721,15 @@ void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
>  {
>  	if (dwc->pending_events) {
>  		dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
> +		pm_runtime_put(dwc->dev);
>  		dwc->pending_events = false;
>  		enable_irq(dwc->irq_gadget);
> +		/*
> +		 * We have only stored the pending events as part
> +		 * of dwc3_interrupt() above, but those events are
> +		 * not yet handled. So explicitly invoke the
> +		 * interrupt handler for handling those events.
> +		 */
> +		dwc3_thread_interrupt(dwc->irq_gadget, dwc->ev_buf);
>  	}
>  }
> -- 
> 2.7.4
> 

BR,
Thinh
Elson Roy Serrao June 1, 2023, 11:04 p.m. UTC | #2
On 5/30/2023 3:51 PM, Thinh Nguyen wrote:
> Hi,
> 
> On Fri, May 26, 2023, Elson Roy Serrao wrote:
>> For bus suspend scenario we should skip controller halt
>> as we still have the usb cable connected. So modify the
> 
> How can you know when the host requests for resume to wakeup the device?
> We haven't implemented hibernation to handle that. If it's communicated
> through the glue layer specific via the phy's event, then how do you
> plan to make it generic and not specific to your platform?
> 

The wakeup/resume path is platform dependent and is handled by phy 
sideband signalling through an external interrupt. This patch handles 
the dwc3 RT resume once the glue driver resume is triggered for bus 
resume scenario.
According to the dwc3 data book (Section7.2) hibernation is an optional 
feature. So we may not be able to make it generic?
Adding a dt node (say snps,external-wakeup) to distinguish the wakeup 
path and having a check against that flag in the dwc3 RT pm ops to 
handle bus suspend/resume, would that be an acceptable solution ?

Regards
Elson

> The reason we halt the controller and force a soft-disconnect is because
> we don't have a handle for this yet. Perhaps I'm missing something here
> because I don't see you handle it here either.
> 
>> runtime pm ops to handle bus suspend scenario. Also invoke
>> autosuspend when device receives U3 notification so that
>> controller can enter runtime suspended state. Ensure that
>> the controller is brought out of runtime suspend before
>> triggering remote wakeup.
>>
>> Signed-off-by: Elson Roy Serrao <quic_eserrao@quicinc.com>
>> ---
>>   drivers/usb/dwc3/core.c   | 19 +++++++++++++++++++
>>   drivers/usb/dwc3/gadget.c | 40 +++++++++++++++++++++++++++++++++-------
>>   2 files changed, 52 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
>> index 278cd1c..7787805 100644
>> --- a/drivers/usb/dwc3/core.c
>> +++ b/drivers/usb/dwc3/core.c
>> @@ -2119,6 +2119,12 @@ static int dwc3_runtime_suspend(struct device *dev)
>>   {
>>   	struct dwc3     *dwc = dev_get_drvdata(dev);
>>   	int		ret;
>> +	u32		reg;
>> +
>> +	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
> 
> This function isn't device mode specific, but you're checking device
> mode specific status here.
> 
Will move this under DEVICE role.

Thanks
Elson
>> +	/* For bus suspend case do not halt the controller */
>> +	if (dwc->connected && (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U3))
>> +		return 0;
>>   
>>   	if (dwc3_runtime_checks(dwc))
>>   		return -EBUSY;
>> @@ -2135,6 +2141,12 @@ static int dwc3_runtime_resume(struct device *dev)
>>   	struct dwc3     *dwc = dev_get_drvdata(dev);
>>   	int		ret;
>>   
>> +	/* resume from bus suspend */
>> +	if (dwc->connected) {
>> +		dwc3_gadget_process_pending_events(dwc);
>> +		goto resume;
>> +	}
>> +
>>   	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
>>   	if (ret)
>>   		return ret;
>> @@ -2149,6 +2161,7 @@ static int dwc3_runtime_resume(struct device *dev)
>>   		break;
>>   	}
>>   
>> +resume:
>>   	pm_runtime_mark_last_busy(dev);
>>   
>>   	return 0;
>> @@ -2157,9 +2170,14 @@ static int dwc3_runtime_resume(struct device *dev)
>>   static int dwc3_runtime_idle(struct device *dev)
>>   {
>>   	struct dwc3     *dwc = dev_get_drvdata(dev);
>> +	u32		reg;
>>   
>>   	switch (dwc->current_dr_role) {
>>   	case DWC3_GCTL_PRTCAP_DEVICE:
>> +		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
>> +		/* for bus suspend case return success */
>> +		if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U3 && dwc->connected)
>> +			goto autosuspend;
>>   		if (dwc3_runtime_checks(dwc))
>>   			return -EBUSY;
>>   		break;
>> @@ -2169,6 +2187,7 @@ static int dwc3_runtime_idle(struct device *dev)
>>   		break;
>>   	}
>>   
>> +autosuspend:
>>   	pm_runtime_mark_last_busy(dev);
>>   	pm_runtime_autosuspend(dev);
>>   
>> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
>> index 5965796..7587912 100644
>> --- a/drivers/usb/dwc3/gadget.c
>> +++ b/drivers/usb/dwc3/gadget.c
>> @@ -2400,15 +2400,21 @@ static int dwc3_gadget_wakeup(struct usb_gadget *g)
>>   		return -EINVAL;
>>   	}
>>   
>> -	spin_lock_irqsave(&dwc->lock, flags);
>>   	if (!dwc->gadget->wakeup_armed) {
>>   		dev_err(dwc->dev, "not armed for remote wakeup\n");
>> -		spin_unlock_irqrestore(&dwc->lock, flags);
>>   		return -EINVAL;
>>   	}
>> -	ret = __dwc3_gadget_wakeup(dwc, true);
>>   
>> +	ret = pm_runtime_resume_and_get(dwc->dev);
>> +	if (ret < 0) {
>> +		pm_runtime_set_suspended(dwc->dev);
>> +		return ret;
>> +	}
>> +
>> +	spin_lock_irqsave(&dwc->lock, flags);
>> +	ret = __dwc3_gadget_wakeup(dwc, true);
>>   	spin_unlock_irqrestore(&dwc->lock, flags);
>> +	pm_runtime_put_noidle(dwc->dev);
>>   
>>   	return ret;
>>   }
>> @@ -2427,6 +2433,12 @@ static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
>>   		return -EINVAL;
>>   	}
>>   
>> +	ret = pm_runtime_resume_and_get(dwc->dev);
>> +	if (ret < 0) {
>> +		pm_runtime_set_suspended(dwc->dev);
>> +		return ret;
>> +	}
>> +
>>   	spin_lock_irqsave(&dwc->lock, flags);
>>   	/*
>>   	 * If the link is in U3, signal for remote wakeup and wait for the
>> @@ -2437,6 +2449,7 @@ static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
>>   		ret = __dwc3_gadget_wakeup(dwc, false);
>>   		if (ret) {
>>   			spin_unlock_irqrestore(&dwc->lock, flags);
>> +			pm_runtime_put_noidle(dwc->dev);
>>   			return -EINVAL;
>>   		}
>>   		dwc3_resume_gadget(dwc);
>> @@ -2450,6 +2463,7 @@ static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
>>   		dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret);
>>   
>>   	spin_unlock_irqrestore(&dwc->lock, flags);
>> +	pm_runtime_put_noidle(dwc->dev);
>>   
>>   	return ret;
>>   }
>> @@ -2711,21 +2725,23 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
>>   	/*
>>   	 * Avoid issuing a runtime resume if the device is already in the
>>   	 * suspended state during gadget disconnect.  DWC3 gadget was already
>> -	 * halted/stopped during runtime suspend.
>> +	 * halted/stopped during runtime suspend except for bus suspend case
>> +	 * where we would have skipped the controller halt.
>>   	 */
>>   	if (!is_on) {
>>   		pm_runtime_barrier(dwc->dev);
>> -		if (pm_runtime_suspended(dwc->dev))
>> +		if (pm_runtime_suspended(dwc->dev) && !dwc->connected)
>>   			return 0;
>>   	}
>>   
>>   	/*
>>   	 * Check the return value for successful resume, or error.  For a
>>   	 * successful resume, the DWC3 runtime PM resume routine will handle
>> -	 * the run stop sequence, so avoid duplicate operations here.
>> +	 * the run stop sequence except for bus resume case, so avoid
>> +	 * duplicate operations here.
>>   	 */
>>   	ret = pm_runtime_get_sync(dwc->dev);
>> -	if (!ret || ret < 0) {
>> +	if ((!ret && !dwc->connected) || ret < 0) {
>>   		pm_runtime_put(dwc->dev);
>>   		return 0;
>>   	}
>> @@ -4313,6 +4329,8 @@ static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
>>   		dwc3_suspend_gadget(dwc);
>>   
>>   	dwc->link_state = next;
>> +	pm_runtime_mark_last_busy(dwc->dev);
>> +	pm_request_autosuspend(dwc->dev);
>>   }
>>   
>>   static void dwc3_gadget_interrupt(struct dwc3 *dwc,
>> @@ -4703,7 +4721,15 @@ void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
>>   {
>>   	if (dwc->pending_events) {
>>   		dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
>> +		pm_runtime_put(dwc->dev);
>>   		dwc->pending_events = false;
>>   		enable_irq(dwc->irq_gadget);
>> +		/*
>> +		 * We have only stored the pending events as part
>> +		 * of dwc3_interrupt() above, but those events are
>> +		 * not yet handled. So explicitly invoke the
>> +		 * interrupt handler for handling those events.
>> +		 */
>> +		dwc3_thread_interrupt(dwc->irq_gadget, dwc->ev_buf);
>>   	}
>>   }
>> -- 
>> 2.7.4
>>
> 
> BR,
> Thinh
Thinh Nguyen June 2, 2023, 6:16 p.m. UTC | #3
On Thu, Jun 01, 2023, Elson Serrao wrote:
> 
> 
> On 5/30/2023 3:51 PM, Thinh Nguyen wrote:
> > Hi,
> > 
> > On Fri, May 26, 2023, Elson Roy Serrao wrote:
> > > For bus suspend scenario we should skip controller halt
> > > as we still have the usb cable connected. So modify the
> > 
> > How can you know when the host requests for resume to wakeup the device?
> > We haven't implemented hibernation to handle that. If it's communicated
> > through the glue layer specific via the phy's event, then how do you
> > plan to make it generic and not specific to your platform?
> > 
> 
> The wakeup/resume path is platform dependent and is handled by phy sideband
> signalling through an external interrupt. This patch handles the dwc3 RT
> resume once the glue driver resume is triggered for bus resume scenario.
> According to the dwc3 data book (Section7.2) hibernation is an optional
> feature. So we may not be able to make it generic?

It would only do so if hibernation is enabled. The driver can detect if
the controller is capable of hibernation and if it is enabled.

> Adding a dt node (say snps,external-wakeup) to distinguish the wakeup path
> and having a check against that flag in the dwc3 RT pm ops to handle bus
> suspend/resume, would that be an acceptable solution ?
> 
> 
> > The reason we halt the controller and force a soft-disconnect is because
> > we don't have a handle for this yet. Perhaps I'm missing something here
> > because I don't see you handle it here either.
> > 
> > > runtime pm ops to handle bus suspend scenario. Also invoke
> > > autosuspend when device receives U3 notification so that
> > > controller can enter runtime suspended state. Ensure that
> > > the controller is brought out of runtime suspend before
> > > triggering remote wakeup.
> > > 


If the change is only specific to your platform, make sure to target
only your platform. Perhaps that can be done through a dt property.

Also, document how your platform works to capture the host resume event.
There are usb2 and usb3 phys. What can your platform capture to detect
host resume for each phy.

For usb3 phy, can your phy assert interrupt on LFPS signal of host
resume?

For example, Roger's patch related to TI platform can capture UTMI
linestate [*].

BR,
Thinh

[*] https://lore.kernel.org/linux-usb/fdfe707e-7689-373a-3aa4-e81cfeac1562@kernel.org/
diff mbox series

Patch

diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 278cd1c..7787805 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -2119,6 +2119,12 @@  static int dwc3_runtime_suspend(struct device *dev)
 {
 	struct dwc3     *dwc = dev_get_drvdata(dev);
 	int		ret;
+	u32		reg;
+
+	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
+	/* For bus suspend case do not halt the controller */
+	if (dwc->connected && (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U3))
+		return 0;
 
 	if (dwc3_runtime_checks(dwc))
 		return -EBUSY;
@@ -2135,6 +2141,12 @@  static int dwc3_runtime_resume(struct device *dev)
 	struct dwc3     *dwc = dev_get_drvdata(dev);
 	int		ret;
 
+	/* resume from bus suspend */
+	if (dwc->connected) {
+		dwc3_gadget_process_pending_events(dwc);
+		goto resume;
+	}
+
 	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
 	if (ret)
 		return ret;
@@ -2149,6 +2161,7 @@  static int dwc3_runtime_resume(struct device *dev)
 		break;
 	}
 
+resume:
 	pm_runtime_mark_last_busy(dev);
 
 	return 0;
@@ -2157,9 +2170,14 @@  static int dwc3_runtime_resume(struct device *dev)
 static int dwc3_runtime_idle(struct device *dev)
 {
 	struct dwc3     *dwc = dev_get_drvdata(dev);
+	u32		reg;
 
 	switch (dwc->current_dr_role) {
 	case DWC3_GCTL_PRTCAP_DEVICE:
+		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
+		/* for bus suspend case return success */
+		if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U3 && dwc->connected)
+			goto autosuspend;
 		if (dwc3_runtime_checks(dwc))
 			return -EBUSY;
 		break;
@@ -2169,6 +2187,7 @@  static int dwc3_runtime_idle(struct device *dev)
 		break;
 	}
 
+autosuspend:
 	pm_runtime_mark_last_busy(dev);
 	pm_runtime_autosuspend(dev);
 
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 5965796..7587912 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -2400,15 +2400,21 @@  static int dwc3_gadget_wakeup(struct usb_gadget *g)
 		return -EINVAL;
 	}
 
-	spin_lock_irqsave(&dwc->lock, flags);
 	if (!dwc->gadget->wakeup_armed) {
 		dev_err(dwc->dev, "not armed for remote wakeup\n");
-		spin_unlock_irqrestore(&dwc->lock, flags);
 		return -EINVAL;
 	}
-	ret = __dwc3_gadget_wakeup(dwc, true);
 
+	ret = pm_runtime_resume_and_get(dwc->dev);
+	if (ret < 0) {
+		pm_runtime_set_suspended(dwc->dev);
+		return ret;
+	}
+
+	spin_lock_irqsave(&dwc->lock, flags);
+	ret = __dwc3_gadget_wakeup(dwc, true);
 	spin_unlock_irqrestore(&dwc->lock, flags);
+	pm_runtime_put_noidle(dwc->dev);
 
 	return ret;
 }
@@ -2427,6 +2433,12 @@  static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
 		return -EINVAL;
 	}
 
+	ret = pm_runtime_resume_and_get(dwc->dev);
+	if (ret < 0) {
+		pm_runtime_set_suspended(dwc->dev);
+		return ret;
+	}
+
 	spin_lock_irqsave(&dwc->lock, flags);
 	/*
 	 * If the link is in U3, signal for remote wakeup and wait for the
@@ -2437,6 +2449,7 @@  static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
 		ret = __dwc3_gadget_wakeup(dwc, false);
 		if (ret) {
 			spin_unlock_irqrestore(&dwc->lock, flags);
+			pm_runtime_put_noidle(dwc->dev);
 			return -EINVAL;
 		}
 		dwc3_resume_gadget(dwc);
@@ -2450,6 +2463,7 @@  static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
 		dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret);
 
 	spin_unlock_irqrestore(&dwc->lock, flags);
+	pm_runtime_put_noidle(dwc->dev);
 
 	return ret;
 }
@@ -2711,21 +2725,23 @@  static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
 	/*
 	 * Avoid issuing a runtime resume if the device is already in the
 	 * suspended state during gadget disconnect.  DWC3 gadget was already
-	 * halted/stopped during runtime suspend.
+	 * halted/stopped during runtime suspend except for bus suspend case
+	 * where we would have skipped the controller halt.
 	 */
 	if (!is_on) {
 		pm_runtime_barrier(dwc->dev);
-		if (pm_runtime_suspended(dwc->dev))
+		if (pm_runtime_suspended(dwc->dev) && !dwc->connected)
 			return 0;
 	}
 
 	/*
 	 * Check the return value for successful resume, or error.  For a
 	 * successful resume, the DWC3 runtime PM resume routine will handle
-	 * the run stop sequence, so avoid duplicate operations here.
+	 * the run stop sequence except for bus resume case, so avoid
+	 * duplicate operations here.
 	 */
 	ret = pm_runtime_get_sync(dwc->dev);
-	if (!ret || ret < 0) {
+	if ((!ret && !dwc->connected) || ret < 0) {
 		pm_runtime_put(dwc->dev);
 		return 0;
 	}
@@ -4313,6 +4329,8 @@  static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
 		dwc3_suspend_gadget(dwc);
 
 	dwc->link_state = next;
+	pm_runtime_mark_last_busy(dwc->dev);
+	pm_request_autosuspend(dwc->dev);
 }
 
 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
@@ -4703,7 +4721,15 @@  void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
 {
 	if (dwc->pending_events) {
 		dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
+		pm_runtime_put(dwc->dev);
 		dwc->pending_events = false;
 		enable_irq(dwc->irq_gadget);
+		/*
+		 * We have only stored the pending events as part
+		 * of dwc3_interrupt() above, but those events are
+		 * not yet handled. So explicitly invoke the
+		 * interrupt handler for handling those events.
+		 */
+		dwc3_thread_interrupt(dwc->irq_gadget, dwc->ev_buf);
 	}
 }