Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964848AbcJQKKu (ORCPT ); Mon, 17 Oct 2016 06:10:50 -0400 Received: from mga06.intel.com ([134.134.136.31]:39993 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932687AbcJQKKp (ORCPT ); Mon, 17 Oct 2016 06:10:45 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,357,1473145200"; d="asc'?scan'208";a="1071452880" From: Felipe Balbi To: Baolin Wang Cc: gregkh@linuxfoundation.org, broonie@kernel.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org Subject: Re: [PATCH v4] usb: dwc3: Wait for control tranfer completed when stopping gadget In-Reply-To: References: Date: Mon, 17 Oct 2016 13:10:00 +0300 Message-ID: <87mvi3s2vb.fsf@linux.intel.com> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5232 Lines: 154 --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Hi, Baolin Wang writes: > When we change the USB function with configfs dynamically, we possibly me= t 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 fla= g. > > Signed-off-by: Baolin Wang Can you make sure this still works? 8<------------------------------------------------------------------------ From=20797c7caab630f650b9ab5e462e8588462e055073 Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Fri, 14 Oct 2016 17:11:33 +0800 Subject: [PATCH] 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: Baolin Wang Signed-off-by: Felipe Balbi =2D-- drivers/usb/dwc3/core.h | 3 +++ drivers/usb/dwc3/ep0.c | 2 ++ drivers/usb/dwc3/gadget.c | 17 +++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 2f6f7c4bc8d4..80e4e9aa2d33 100644 =2D-- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -38,6 +38,7 @@ #define DWC3_MSG_MAX 500 =20 /* Global constants */ +#define DWC3_PULL_UP_TIMEOUT 500 /* ms */ #define DWC3_ZLP_BUF_SIZE 1024 /* size of a superspeed bulk */ #define DWC3_EP0_BOUNCE_SIZE 512 #define DWC3_ENDPOINTS_NUM 32 @@ -756,6 +757,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 transfer is completed and enter setup phase * @lock: for synchronizing * @dev: pointer to our struct device * @xhci: pointer to our xHCI child @@ -853,6 +855,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; =20 /* device lock */ spinlock_t lock; diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c index 5e642d65a3b2..417cfd3f04ab 100644 =2D-- a/drivers/usb/dwc3/ep0.c +++ b/drivers/usb/dwc3/ep0.c @@ -284,6 +284,8 @@ void dwc3_ep0_out_start(struct dwc3 *dwc) { int ret; =20 + complete(&dwc->ep0_in_setup); + ret =3D dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, DWC3_TRBCTL_CONTROL_SETUP, false); WARN_ON(ret < 0); diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index b53712cbc499..da79716be50d 100644 =2D-- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1557,6 +1557,21 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, = int is_on) =20 is_on =3D !!is_on; =20 + /* + * 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 !=3D EP0_SETUP_PHASE) { + reinit_completion(&dwc->ep0_in_setup); + + ret =3D wait_for_completion_timeout(&dwc->ep0_in_setup, + msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); + if (ret =3D=3D 0) { + dev_err(dwc->dev, "timed out waiting for SETUP phase\n"); + return -ETIMEDOUT; + } + } + spin_lock_irqsave(&dwc->lock, flags); ret =3D dwc3_gadget_run_stop(dwc, is_on, false); spin_unlock_irqrestore(&dwc->lock, flags); @@ -2954,6 +2969,8 @@ int dwc3_gadget_init(struct dwc3 *dwc) goto err4; } =20 + init_completion(&dwc->ep0_in_setup); + dwc->gadget.ops =3D &dwc3_gadget_ops; dwc->gadget.speed =3D USB_SPEED_UNKNOWN; dwc->gadget.sg_supported =3D true; =2D-=20 2.10.0.440.g21f862b =2D-=20 balbi --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIcBAEBCAAGBQJYBKN4AAoJEMy+uJnhGpkGSL0QANL5FGq7LYZ7gXw0SIHEF4tB NRtUbBOmoBOYWBJhOzbbcdhJvsZUTftFTApuN03Vf+3uYBRJjhDH24yhsAD3BUGY O8qQrpdIbRObuxOcCBxOR+qkLGwUEC7iVsaKynOPWVY6ouWetbxz8C+3LgX4BAyc XZcq/DN5bpUiJRhUnKW1/n/BgUSrFaKKMhirQsBHM3BL3WhkQKVsaSTww3B1HWeN GZjZoAHBr0k8NRwIRtC7kp2L/2FORUysWg4wBHUD7jKSCfXYsWQgyYt4938jirF8 BC7u9qDBEP3wyLuGVYQ0OTQedX41xbesPg/QL4m+rA7VXCnPdmmt14AJ1S38qATN 01fZnQ8xmmcAo84EjLHLheU0a0hoZ0Mr41xNsDbrehlpq2gl7TAdYWxsnahSXM0C fhXwCKu8QYRk7lGm8iJtz1leMjzfhu61xrtBGJLw9nHeiEknemFThGjMKMbk9Zis EEHRiz8U38WvlVxIQuJ+YpD8I2IWReTNR+TZej0DKyoiv0X7TEPw4fhxQf7Xqo0Z P6fh5Fe0g3MB85hAZoQd9uBZaFSaavkBEWSDPKq/MA2e/oYBCFHzQtWxffHwhBj9 y3wIz+/HyjW3qVq1L9B8vxeqS2iXPsOh5J6shwce4ox3HbBvMgtEIIQdJDYH1Zyt GF+t9Tk0HdVsceZUDngW =jB8o -----END PGP SIGNATURE----- --=-=-=--