From patchwork Mon Sep 19 11:52:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 76534 Delivered-To: patch@linaro.org Received: by 10.140.106.72 with SMTP id d66csp918705qgf; Mon, 19 Sep 2016 04:53:56 -0700 (PDT) X-Received: by 10.98.8.197 with SMTP id 66mr46496700pfi.76.1474286035970; Mon, 19 Sep 2016 04:53:55 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c5si5671485pfk.205.2016.09.19.04.53.55; Mon, 19 Sep 2016 04:53:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759021AbcISLxa (ORCPT + 4 others); Mon, 19 Sep 2016 07:53:30 -0400 Received: from mail-pa0-f52.google.com ([209.85.220.52]:33921 "EHLO mail-pa0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756905AbcISLxU (ORCPT ); Mon, 19 Sep 2016 07:53:20 -0400 Received: by mail-pa0-f52.google.com with SMTP id wk8so48059148pab.1 for ; Mon, 19 Sep 2016 04:53:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=mSnCeD1f7w1owlLhs8BuoysZJvrODEGT0YxdiCD+cfw=; b=OlBJmuCf/QVtpUigdgaS9tvwwgQR0tpthvbrDlwIgpGupNe6SfG/ShrOMdc2jLGF/h el31sB3gblkscMXouoqhKwoMbzZ8hGoohMJFPVZV1/bMABStav794uRSOATOyxrC8sTO A9uOEtNU3MyKCcDrZGq1nXqn22Qpx67QjbWR8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=mSnCeD1f7w1owlLhs8BuoysZJvrODEGT0YxdiCD+cfw=; b=DRRUzCkKTzjPPTbykfF7QpR8I5S+lPnQoBOeNdZZLKLMvImxy3TyXKio8FKUlHIlSb /WQQ9VTo2Ows8Et0pclOXlE/eei2jtdxXLTw0oNVEsMXyHp+MdFuVizOibZvCM1S6cTG TA30BFZsJclM1A+q6jXTyz75g/ozhCtosB7G4Zpprdk+jhFrzloTihs59mtarKVLS3Zx QxmBi6Dzt1C5HcNKQ4F2pTgjVFiH6neh+mbFg75uYRhck8+mltXIRCOwKRoeRa1/bulb lMTedWGyk6zKhOs8XI8L9ywatRF/cwKoFe2C9IvfCk4Qz6D7Ho6TAk7ASTl0rIeM90c4 NwAA== X-Gm-Message-State: AE9vXwOoXIQgXQizhI517XB8fwsjF39fLlcVZ2eM1T130S+770nwiMcfV/EKeRGKrFOfj3rj X-Received: by 10.66.194.196 with SMTP id hy4mr46328860pac.63.1474285999886; Mon, 19 Sep 2016 04:53:19 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id cu2sm31130169pad.18.2016.09.19.04.53.17 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 19 Sep 2016 04:53:19 -0700 (PDT) From: Baolin Wang To: balbi@kernel.org, gregkh@linuxfoundation.org Cc: broonie@kernel.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org Subject: [PATCH v3 2/2] usb: dwc3: Wait for control tranfer completed when stopping gadget Date: Mon, 19 Sep 2016 19:52:18 +0800 Message-Id: <5e6afd65e16d3177b994b5c42632ee14c9080119.1474285737.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <521625dd7f5e335e2a681ec65ebffc5832207e5f.1474285737.git.baolin.wang@linaro.org> References: <521625dd7f5e335e2a681ec65ebffc5832207e5f.1474285737.git.baolin.wang@linaro.org> In-Reply-To: <521625dd7f5e335e2a681ec65ebffc5832207e5f.1474285737.git.baolin.wang@linaro.org> References: <521625dd7f5e335e2a681ec65ebffc5832207e5f.1474285737.git.baolin.wang@linaro.org> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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. Signed-off-by: Baolin Wang --- drivers/usb/dwc3/core.h | 2 ++ drivers/usb/dwc3/ep0.c | 2 ++ drivers/usb/dwc3/gadget.c | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+) -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index b2317e7..01a6fbd 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -745,6 +745,7 @@ struct dwc3_scratchpad_array { * @ep0_usb_req: dummy req used while handling STD USB requests * @ep0_bounce_addr: dma address of ep0_bounce * @scratch_addr: dma address of scratchbuf + * @ep0_in_setup: One control tranfer is completed and enter setup phase * @lock: for synchronizing * @dev: pointer to our struct device * @xhci: pointer to our xHCI child @@ -843,6 +844,7 @@ struct dwc3 { dma_addr_t ep0_bounce_addr; dma_addr_t scratch_addr; struct dwc3_request ep0_usb_req; + struct completion ep0_in_setup; /* device lock */ spinlock_t lock; diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c index fe79d77..06c167a 100644 --- a/drivers/usb/dwc3/ep0.c +++ b/drivers/usb/dwc3/ep0.c @@ -311,6 +311,8 @@ void dwc3_ep0_out_start(struct dwc3 *dwc) ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, DWC3_TRBCTL_CONTROL_SETUP, false); WARN_ON(ret < 0); + + complete(&dwc->ep0_in_setup); } static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index ca2ae5b..3a30d51 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1437,6 +1437,15 @@ static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) if (pm_runtime_suspended(dwc->dev)) return 0; + /* + * 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); + return -EBUSY; + } + reg = dwc3_readl(dwc->regs, DWC3_DCTL); if (is_on) { if (dwc->revision <= DWC3_REVISION_187A) { @@ -1487,10 +1496,22 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) is_on = !!is_on; +try_again: spin_lock_irqsave(&dwc->lock, flags); ret = dwc3_gadget_run_stop(dwc, is_on, false); spin_unlock_irqrestore(&dwc->lock, flags); + if (ret == -EBUSY) { + ret = wait_for_completion_timeout(&dwc->ep0_in_setup, + msecs_to_jiffies(500)); + if (ret == 0) { + dev_err(dwc->dev, "timeout to stop gadget.\n"); + return -ETIMEDOUT; + } else { + goto try_again; + } + } + return ret; } @@ -2914,6 +2935,8 @@ int dwc3_gadget_init(struct dwc3 *dwc) goto err4; } + init_completion(&dwc->ep0_in_setup); + dwc->gadget.ops = &dwc3_gadget_ops; dwc->gadget.speed = USB_SPEED_UNKNOWN; dwc->gadget.sg_supported = true;