From f2545eebf062f2dddb122463daff4771ffb1fd04 Mon Sep 17 00:00:00 2001 From: Krishna Kurapati Date: Wed, 16 Aug 2023 14:32:08 +0530 Subject: [PATCH] BACKPORT: UPSTREAM: usb: dwc3: gadget: Execute gadget stop after halting the controller Do not call gadget stop until the poll for controller halt is completed. DEVTEN is cleared as part of gadget stop, so the intention to allow ep0 events to continue while waiting for controller halt is not happening. Fixes: c96683798e27 ("usb: dwc3: ep0: Don't prepare beyond Setup stage") Cc: stable@vger.kernel.org Acked-by: Thinh Nguyen Signed-off-by: Wesley Cheng Link: https://lore.kernel.org/r/20230420212759.29429-2-quic_wcheng@quicinc.com Signed-off-by: Greg Kroah-Hartman Bug: 280741172 Bug: 277690231 Bug: 296168017 [ugoswami: Modified the function dwc3_gadget_run_stop to remove dependency from cleanup patch.] (cherry picked from commit 39674be56fba1cd3a03bf4617f523a35f85fd2c1 usb-linus) Change-Id: I2a8a42e938a680cefc0d1a56274caf155a4f99cd Signed-off-by: Wesley Cheng Signed-off-by: Udipto Goswami Signed-off-by: Krishna Kurapati (cherry picked from commit 45902380021d3fe742f9e6d49337ad3746f00d64) (cherry picked from commit 1ce1603175dafecd3e19ed7b8422c16b538a849b) --- drivers/usb/dwc3/gadget.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 12385775b98f..f27cbe9a39ae 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2532,7 +2532,6 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) * bit. */ dwc3_stop_active_transfers(dwc); - __dwc3_gadget_stop(dwc); spin_unlock_irqrestore(&dwc->lock, flags); /* @@ -2569,7 +2568,19 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) * remaining event generated by the controller while polling for * DSTS.DEVCTLHLT. */ - return dwc3_gadget_run_stop(dwc, false, false); + ret = dwc3_gadget_run_stop(dwc, false, false); + + /* + * Stop the gadget after controller is halted, so that if needed, the + * events to update EP0 state can still occur while the run/stop + * routine polls for the halted state. DEVTEN is cleared as part of + * gadget stop. + */ + spin_lock_irqsave(&dwc->lock, flags); + __dwc3_gadget_stop(dwc); + spin_unlock_irqrestore(&dwc->lock, flags); + + return ret; } static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)