From patchwork Fri Apr 28 12:08:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Quadros X-Patchwork-Id: 88614 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp881323vqo; Fri, 28 Apr 2023 05:09:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6XTOG/CDTO63NvXjJ/Y0paER7XkRG7zjJ4Y1GyogxdJEeYuHriEuz/7OHUM21ol4j0IzTt X-Received: by 2002:a05:6a20:8f0d:b0:f2:bb0:4622 with SMTP id b13-20020a056a208f0d00b000f20bb04622mr6878820pzk.12.1682683773196; Fri, 28 Apr 2023 05:09:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682683773; cv=none; d=google.com; s=arc-20160816; b=BuhZ1nHAQYR2XqIPO2hdjJ9//z6PLxTGS9s/FtYOTQ662DpukDHF6os1k+BPhYAlYQ CVjm75vHnd5tAN0QFT44EVfLF1WSNWNHEHkr8bb/aGFnIzAx1B9V5zidxhBTfxPlTFr5 CvN7EJE6VS4Zb5kC40bp/9ZqBJOrXaWUMiCPieJNDFQMe0VEyHErH+RWCJjJYPvhXS5t 5Hp2mdLfMYq6Py5VAUaCHWFlLQiXehx0noJsYgRQjaY/fg3oapo/0FByEUo0ooFsDUq/ UZwNFvq6V+I94UAOlYUvlXFImzul6CDFryuKy9xaXl88enP/20fr6WhrRqRj0FFYCtLL U1dA== 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:dkim-signature; bh=wUoUNeaha+W7RbVT/k11tVRE+zzINNc0waRh5VH9t30=; b=tNa4u2CXDgZbJ49Jbc3EqHp16Rx/QtGU1NB46Qq84xD9ua35TSYvTOLJrjud+VUvY9 NSrfoTVaNzv4qXYtjLwoBe9Eqg4TbIVkZyZ5R4KbutY0EuoFM76ApZOvWBd7FRIExpl0 xKy7DwwnDWnwDlGszTIch8znLrZwtU1EBy1y0W4By2huUMZnfrj4WjQtLEkNV+AXexXG 1AwHDCi6yx3laa7Wcm/6shMsMyIC0QPV1qqHwXgA1r+6rAEnLFKq3jigHckmtxooKtMU jrZss0UZY4JtodYlDjeqRFBEFyG9xKC7MznxSLM7WrN1H4HWm9KC5vy18lga8Eq5tu0v JHyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eZ6vdgSR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q16-20020aa79830000000b0063b86de9d22si21341462pfl.177.2023.04.28.05.09.15; Fri, 28 Apr 2023 05:09:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eZ6vdgSR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346015AbjD1MIv (ORCPT + 99 others); Fri, 28 Apr 2023 08:08:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345965AbjD1MIt (ORCPT ); Fri, 28 Apr 2023 08:08:49 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C6F6212D; Fri, 28 Apr 2023 05:08:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0F8B064358; Fri, 28 Apr 2023 12:08:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7EAABC433D2; Fri, 28 Apr 2023 12:08:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682683704; bh=H7zz9k0fAxO598lk5RscNfG2z6M8eK1928V5K1H0KWg=; h=From:To:Cc:Subject:Date:From; b=eZ6vdgSRzzAzMSIf4YMhBs1IT/qRUwfzTpJZIPW5Djgr62R2BIt6z7yVpdq2vKeXn NRCp5pQcxFkfxVN4LrexR5svwIXbJHjvJMuaHT0/HUQEKBGv6iFHwPezvaA0rX1CRK x96vekamRthzLpc6rzoKU8gXH7rtZQABnAbi36BJsYKy0KQOOh7h8bnzrf4gmtKq07 Jgl9zgKj/Ock5b8pXn2U7Gd3udZpFnPBL8gFFbEJgmfjM/5b7sBQd4Cpv9ymbSOm3J +zJf11LZEzM7JsxWDVSKWkkQ9mmzTx9/alqB9gSBrBwDyS006DvxsJgsuG3LhR0tDW aq2vSWPgTrvzA== From: Roger Quadros To: Thinh.Nguyen@synopsys.com Cc: gregkh@linuxfoundation.org, r-gunasekaran@ti.com, srk@ti.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Roger Quadros Subject: [PATCH] usb: dwc3: gadget: Fix controller timeout at dwc3_gadget_suspend() Date: Fri, 28 Apr 2023 15:08:12 +0300 Message-Id: <20230428120812.107123-1-rogerq@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764421819954306025?= X-GMAIL-MSGID: =?utf-8?q?1764421819954306025?= If gadget driver is loaded and we are connected to a USB host, all transfers must be stopped before stopping the controller else we will not get a clean stop i.e. dwc3_gadget_run_stop() will take several seconds to complete and will return -ETIMEDOUT. Fix this. No need to stop the controller at dwc3_gadget_suspend() if dwc->softconnect is 0 as controller is already stopped. Handle error cases properly in dwc3_gadget_suspend(). Suggested-by: Thinh Nguyen Signed-off-by: Roger Quadros --- drivers/usb/dwc3/gadget.c | 69 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 287ea6ee2463..b63502afb144 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2668,6 +2668,21 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) return dwc3_gadget_run_stop(dwc, false, false); } +static int dwc3_gadget_soft_connect(struct dwc3 *dwc) +{ + /* + * In the Synopsys DWC_usb31 1.90a programming guide section + * 4.1.9, it specifies that for a reconnect after a + * device-initiated disconnect requires a core soft reset + * (DCTL.CSftRst) before enabling the run/stop bit. + */ + dwc3_core_soft_reset(dwc); + + dwc3_event_buffers_setup(dwc); + __dwc3_gadget_start(dwc); + return dwc3_gadget_run_stop(dwc, true, false); +} + static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) { struct dwc3 *dwc = gadget_to_dwc(g); @@ -2706,21 +2721,10 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) synchronize_irq(dwc->irq_gadget); - if (!is_on) { + if (!is_on) ret = dwc3_gadget_soft_disconnect(dwc); - } else { - /* - * In the Synopsys DWC_usb31 1.90a programming guide section - * 4.1.9, it specifies that for a reconnect after a - * device-initiated disconnect requires a core soft reset - * (DCTL.CSftRst) before enabling the run/stop bit. - */ - dwc3_core_soft_reset(dwc); - - dwc3_event_buffers_setup(dwc); - __dwc3_gadget_start(dwc); - ret = dwc3_gadget_run_stop(dwc, true, false); - } + else + ret = dwc3_gadget_soft_connect(dwc); pm_runtime_put(dwc->dev); @@ -4674,42 +4678,39 @@ void dwc3_gadget_exit(struct dwc3 *dwc) int dwc3_gadget_suspend(struct dwc3 *dwc) { unsigned long flags; + int ret; - if (!dwc->gadget_driver) + if (!dwc->gadget_driver || !dwc->softconnect) return 0; - dwc3_gadget_run_stop(dwc, false, false); + ret = dwc3_gadget_soft_disconnect(dwc); + if (ret) + goto err; spin_lock_irqsave(&dwc->lock, flags); dwc3_disconnect_gadget(dwc); - __dwc3_gadget_stop(dwc); spin_unlock_irqrestore(&dwc->lock, flags); return 0; + +err: + /* + * Attempt to reset the controller's state. Likely no + * communication can be established until the host + * performs a port reset. + */ + if (dwc->softconnect) + dwc3_gadget_soft_connect(dwc); + + return ret; } int dwc3_gadget_resume(struct dwc3 *dwc) { - int ret; - if (!dwc->gadget_driver || !dwc->softconnect) return 0; - ret = __dwc3_gadget_start(dwc); - if (ret < 0) - goto err0; - - ret = dwc3_gadget_run_stop(dwc, true, false); - if (ret < 0) - goto err1; - - return 0; - -err1: - __dwc3_gadget_stop(dwc); - -err0: - return ret; + return dwc3_gadget_soft_connect(dwc); } void dwc3_gadget_process_pending_events(struct dwc3 *dwc)