Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2204722pxb; Fri, 17 Sep 2021 04:51:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxncpnrsXQYJsnRy+jiympreAlm7lg3cJAF7sHjiNA5/x8UxUlRdQTF2o3SEHNTWpaEhFQ6 X-Received: by 2002:a05:6e02:2167:: with SMTP id s7mr7592857ilv.314.1631879468877; Fri, 17 Sep 2021 04:51:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631879468; cv=none; d=google.com; s=arc-20160816; b=yv8/QA16c8hhs3KTAGiDSHAyRNbTP2hwHwYtb5kI59rn5qqYC9pGcBCKHfsK8htm1x ZDb3O3vtJ1XuLlnaNbK2nMb+UxG8P7PdJBT5854b9ctuXg1i8BkKMXZrXKyMVYwN0e23 cw3Kt5YkKptrKxWsM3M//0/rLOf5Bh3g9XdZiVv82tgDS/ftauMoSCN/pHIfWud42Vwb gCPdO5smCNCor/1lbT2FvInNoZb25ZI/uQckhgVk/kXh4f3hTCPfzyta60q3o3T2C1e0 jtU9sAJywlKoBtrfTSRrX3N0ZAboVqIN7EAxCCfsPHh/Xsa84itPF8c0TBkzhzMVZPxV Z+SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dmarc-filter:sender :dkim-signature; bh=GftnvfJ3SMv+74jmuw1hkdvaG9vSrA0CUqCx3JsIlho=; b=TKjQnO87qbeHlYQEvY6PYnxONp2oXYgB+6fnAbGPurAzGZanTmjKscGZMNKjzuWRx1 tLP57iQD/hLUo89J55UFx2fvPmxarwl1mfk8HaCAi1lcjyjw2Zz7VjtI6i8TzaJW5W8z HCJhcVsDSfS5vJhDKsuQid1ma5Yw0H3KNNhQ6kVqurVlb+krEYO4TDg0D2diZaMT9F3D s12GX7wawfgVsmQNLNTjOCA1ZbmPfVWoGB/K6krHyqhDqL1gffNqhJ+Y2lrY0fE7OpDv DI+wEiLdGBGxMIT1jk6PSBQMP0C8HgowpLlGa6aN9DlLNKyuiyRaueWlIlzNUKk4rEnG 90gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=i53IZSUd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y12si5225354ilh.146.2021.09.17.04.50.57; Fri, 17 Sep 2021 04:51:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=i53IZSUd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242657AbhIQCUY (ORCPT + 99 others); Thu, 16 Sep 2021 22:20:24 -0400 Received: from so254-9.mailgun.net ([198.61.254.9]:25818 "EHLO so254-9.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229775AbhIQCUX (ORCPT ); Thu, 16 Sep 2021 22:20:23 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1631845142; h=Content-Transfer-Encoding: MIME-Version: Message-Id: Date: Subject: Cc: To: From: Sender; bh=GftnvfJ3SMv+74jmuw1hkdvaG9vSrA0CUqCx3JsIlho=; b=i53IZSUdfKMrxLng+eQwEC6SqGHru24C9pDvUqeQKEixt/zreKCJLXBii8qRW53hLEN2FRsv JfWdOisFLQPxgSxFSeBRptHGKNuUZBqlTiBCig96Xi5qxwsfsO9U/4JfWjfPKLJ4duXq5Y1c kuy5c8mk1QnbKUDmW8AdUCMuj9g= X-Mailgun-Sending-Ip: 198.61.254.9 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n05.prod.us-east-1.postgun.com with SMTP id 6143fb12c1b30e2f02e0b01e (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Fri, 17 Sep 2021 02:18:58 GMT Sender: wcheng=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 9E1D4C4338F; Fri, 17 Sep 2021 02:18:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00,SPF_FAIL, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from wcheng-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: wcheng) by smtp.codeaurora.org (Postfix) with ESMTPSA id 7F667C4338F; Fri, 17 Sep 2021 02:18:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 smtp.codeaurora.org 7F667C4338F Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=fail smtp.mailfrom=codeaurora.org From: Wesley Cheng To: balbi@kernel.org, gregkh@linuxfoundation.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Wesley Cheng Subject: [PATCH] usb: dwc3: gadget: Avoid starting DWC3 gadget during UDC unbind Date: Thu, 16 Sep 2021 19:18:52 -0700 Message-Id: <20210917021852.2037-1-wcheng@codeaurora.org> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is a race present where the DWC3 runtime resume runs in parallel to the UDC unbind sequence. This will eventually lead to a possible scenario where we are enabling the run/stop bit, without a valid composition defined. Thread#1 (handling UDC unbind): usb_gadget_remove_driver() -->usb_gadget_disconnect() -->dwc3_gadget_pullup(0) --> continue UDC unbind sequence -->Thread#2 is running in parallel here Thread#2 (handing next cable connect) __dwc3_set_mode() -->pm_runtime_get_sync() -->dwc3_gadget_resume() -->dwc->gadget_driver is NOT NULL yet -->dwc3_gadget_run_stop(1) --> _dwc3gadget_start() ... Fix this by tracking the pullup disable routine, and avoiding resuming of the DWC3 gadget. Once the UDC is re-binded, that will trigger the pullup enable routine, which would handle enabling the DWC3 gadget. Signed-off-by: Wesley Cheng --- drivers/usb/dwc3/core.h | 2 ++ drivers/usb/dwc3/gadget.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 324ca5f5e98d..ee854697c300 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -1032,6 +1032,7 @@ struct dwc3_scratchpad_array { * @tx_fifo_resize_max_num: max number of fifos allocated during txfifo resize * @hsphy_interface: "utmi" or "ulpi" * @connected: true when we're connected to a host, false otherwise + * @softconnect: true when gadget connect is called, false when disconnect runs * @delayed_status: true when gadget driver asks for delayed status * @ep0_bounced: true when we used bounce buffer * @ep0_expect_in: true when we expect a DATA IN transfer @@ -1252,6 +1253,7 @@ struct dwc3 { const char *hsphy_interface; unsigned connected:1; + unsigned softconnect:1; unsigned delayed_status:1; unsigned ep0_bounced:1; unsigned ep0_expect_in:1; diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 804b50548163..32e01885b211 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2418,7 +2418,7 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) int ret; is_on = !!is_on; - + dwc->softconnect = 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. @@ -4352,7 +4352,7 @@ int dwc3_gadget_resume(struct dwc3 *dwc) { int ret; - if (!dwc->gadget_driver) + if (!dwc->gadget_driver || !dwc->softconnect) return 0; ret = __dwc3_gadget_start(dwc); -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project