Commit bb014736 authored by Baolin Wang's avatar Baolin Wang Committed by Felipe Balbi

usb: dwc3: gadget: don't clear RUN/STOP when it's invalid to do so

When we change the USB function with configfs dynamically, we possibly
met this situation: one core is doing the control transfer, another core
is trying to unregister the USB gadget from userspace, we must wait for
completing this control tranfer, or it will hang the controller to set
the DEVCTRLHLT flag.

[ felipe.balbi@linux.intel.com: several fixes to the patch
	- call complete() before starting following SETUP transfer
	- add a macro for ep0_in_setup's timeout
	- change commit subject slightly
	- break lines at 72 characters (git adds an 8-character tab)
	- avoid changes to dwc3_gadget_run_stop() ]
Signed-off-by: default avatarBaolin Wang <baolin.wang@linaro.org>
Signed-off-by: default avatarFelipe Balbi <felipe.balbi@linux.intel.com>
parent cf689230
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#define DWC3_MSG_MAX 500 #define DWC3_MSG_MAX 500
/* Global constants */ /* Global constants */
#define DWC3_PULL_UP_TIMEOUT 500 /* ms */
#define DWC3_ZLP_BUF_SIZE 1024 /* size of a superspeed bulk */ #define DWC3_ZLP_BUF_SIZE 1024 /* size of a superspeed bulk */
#define DWC3_EP0_BOUNCE_SIZE 512 #define DWC3_EP0_BOUNCE_SIZE 512
#define DWC3_ENDPOINTS_NUM 32 #define DWC3_ENDPOINTS_NUM 32
...@@ -751,6 +752,7 @@ struct dwc3_scratchpad_array { ...@@ -751,6 +752,7 @@ struct dwc3_scratchpad_array {
* @ep0_usb_req: dummy req used while handling STD USB requests * @ep0_usb_req: dummy req used while handling STD USB requests
* @ep0_bounce_addr: dma address of ep0_bounce * @ep0_bounce_addr: dma address of ep0_bounce
* @scratch_addr: dma address of scratchbuf * @scratch_addr: dma address of scratchbuf
* @ep0_in_setup: one control transfer is completed and enter setup phase
* @lock: for synchronizing * @lock: for synchronizing
* @dev: pointer to our struct device * @dev: pointer to our struct device
* @xhci: pointer to our xHCI child * @xhci: pointer to our xHCI child
...@@ -848,6 +850,7 @@ struct dwc3 { ...@@ -848,6 +850,7 @@ struct dwc3 {
dma_addr_t ep0_bounce_addr; dma_addr_t ep0_bounce_addr;
dma_addr_t scratch_addr; dma_addr_t scratch_addr;
struct dwc3_request ep0_usb_req; struct dwc3_request ep0_usb_req;
struct completion ep0_in_setup;
/* device lock */ /* device lock */
spinlock_t lock; spinlock_t lock;
......
...@@ -284,6 +284,8 @@ void dwc3_ep0_out_start(struct dwc3 *dwc) ...@@ -284,6 +284,8 @@ void dwc3_ep0_out_start(struct dwc3 *dwc)
{ {
int ret; int ret;
complete(&dwc->ep0_in_setup);
ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
DWC3_TRBCTL_CONTROL_SETUP, false); DWC3_TRBCTL_CONTROL_SETUP, false);
WARN_ON(ret < 0); WARN_ON(ret < 0);
......
...@@ -1550,6 +1550,21 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) ...@@ -1550,6 +1550,21 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
is_on = !!is_on; is_on = !!is_on;
/*
* Per databook, when we want to stop the gadget, if a control transfer
* is still in process, complete it and get the core into setup phase.
*/
if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
reinit_completion(&dwc->ep0_in_setup);
ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
if (ret == 0) {
dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
return -ETIMEDOUT;
}
}
spin_lock_irqsave(&dwc->lock, flags); spin_lock_irqsave(&dwc->lock, flags);
ret = dwc3_gadget_run_stop(dwc, is_on, false); ret = dwc3_gadget_run_stop(dwc, is_on, false);
spin_unlock_irqrestore(&dwc->lock, flags); spin_unlock_irqrestore(&dwc->lock, flags);
...@@ -2945,6 +2960,8 @@ int dwc3_gadget_init(struct dwc3 *dwc) ...@@ -2945,6 +2960,8 @@ int dwc3_gadget_init(struct dwc3 *dwc)
goto err4; goto err4;
} }
init_completion(&dwc->ep0_in_setup);
dwc->gadget.ops = &dwc3_gadget_ops; dwc->gadget.ops = &dwc3_gadget_ops;
dwc->gadget.speed = USB_SPEED_UNKNOWN; dwc->gadget.speed = USB_SPEED_UNKNOWN;
dwc->gadget.sg_supported = true; dwc->gadget.sg_supported = true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment