From patchwork Wed Jul 1 18:24:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 11637027 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7C61413B4 for ; Wed, 1 Jul 2020 18:25:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6B1FB20870 for ; Wed, 1 Jul 2020 18:25:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732946AbgGASZC (ORCPT ); Wed, 1 Jul 2020 14:25:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732939AbgGASY7 (ORCPT ); Wed, 1 Jul 2020 14:24:59 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 652EFC08C5DB for ; Wed, 1 Jul 2020 11:24:59 -0700 (PDT) Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jqhPj-0004Pc-R0; Wed, 01 Jul 2020 20:24:55 +0200 Received: from mgr by dude.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1jqhPj-0007R4-0C; Wed, 01 Jul 2020 20:24:55 +0200 From: Michael Grzeschik To: linux-usb@vger.kernel.org Cc: Thinh.Nguyen@synopsys.com, gregkh@linuxfoundation.org, kernel@pengutronix.de, balbi@kernel.org Subject: [PATCH v5 1/3] usb: dwc3: gadget: add frame number mask Date: Wed, 1 Jul 2020 20:24:51 +0200 Message-Id: <20200701182453.25299-2-m.grzeschik@pengutronix.de> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200701182453.25299-1-m.grzeschik@pengutronix.de> References: <20200701182453.25299-1-m.grzeschik@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: mgr@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-usb@vger.kernel.org Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org This patch adds a define DWC3_FRNUMBER_MASK for the commonly used 0x3fff mask and uses it. Signed-off-by: Michael Grzeschik --- v4: - first version v4 -> v5: - fixed wording in commit message --- drivers/usb/dwc3/gadget.c | 4 ++-- drivers/usb/dwc3/gadget.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 8de6f10d335e1c0..5fb78535efa9bdc 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1403,7 +1403,7 @@ static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) * Check if we can start isoc transfer on the next interval or * 4 uframes in the future with BIT[15:14] as dep->combo_num */ - test_frame_number = dep->frame_number & 0x3fff; + test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; test_frame_number |= dep->combo_num << 14; test_frame_number += max_t(u32, 4, dep->interval); @@ -1450,7 +1450,7 @@ static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) else if (test0 && test1) dep->combo_num = 0; - dep->frame_number &= 0x3fff; + dep->frame_number &= DWC3_FRNUMBER_MASK; dep->frame_number |= dep->combo_num << 14; dep->frame_number += max_t(u32, 4, dep->interval); diff --git a/drivers/usb/dwc3/gadget.h b/drivers/usb/dwc3/gadget.h index 24dca38720225dd..a0fc46f72b9e30a 100644 --- a/drivers/usb/dwc3/gadget.h +++ b/drivers/usb/dwc3/gadget.h @@ -54,6 +54,8 @@ struct dwc3; /* U2 Device exit Latency */ #define DWC3_DEFAULT_U2_DEV_EXIT_LAT 0x1FF /* Less then 511 microsec */ +/* Frame/Microframe Number Mask */ +#define DWC3_FRNUMBER_MASK 0x3fff /* -------------------------------------------------------------------------- */ #define to_dwc3_request(r) (container_of(r, struct dwc3_request, request)) From patchwork Wed Jul 1 18:24:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 11637031 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7CD316C1 for ; Wed, 1 Jul 2020 18:25:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6EA6020870 for ; Wed, 1 Jul 2020 18:25:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732969AbgGASZB (ORCPT ); Wed, 1 Jul 2020 14:25:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732959AbgGASY7 (ORCPT ); Wed, 1 Jul 2020 14:24:59 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70635C08C5DD for ; Wed, 1 Jul 2020 11:24:59 -0700 (PDT) Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jqhPj-0004Pd-RJ; Wed, 01 Jul 2020 20:24:55 +0200 Received: from mgr by dude.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1jqhPj-0007R6-2c; Wed, 01 Jul 2020 20:24:55 +0200 From: Michael Grzeschik To: linux-usb@vger.kernel.org Cc: Thinh.Nguyen@synopsys.com, gregkh@linuxfoundation.org, kernel@pengutronix.de, balbi@kernel.org, Michael Olbrich , Thinh Nguyen Subject: [PATCH v5 2/3] usb: dwc3: gadget: make starting isoc transfers more robust Date: Wed, 1 Jul 2020 20:24:52 +0200 Message-Id: <20200701182453.25299-3-m.grzeschik@pengutronix.de> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200701182453.25299-1-m.grzeschik@pengutronix.de> References: <20200701182453.25299-1-m.grzeschik@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: mgr@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-usb@vger.kernel.org Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Michael Olbrich Currently __dwc3_gadget_start_isoc must be called very shortly after XferNotReady. Otherwise the frame number is outdated and start transfer will fail, even with several retries. DSTS provides the lower 14 bit of the frame number. Use it in combination with the frame number provided by XferNotReady to guess the current frame number. This will succeed unless more than one 14 rollover has happened since XferNotReady. Start transfer might still fail if the frame number is increased immediately after DSTS is read. So retries are still needed. Don't drop the current request if this happens. This way it is not lost and can be used immediately to try again with the next frame number. With this change, __dwc3_gadget_start_isoc is still not successfully in all cases bit it increases the acceptable delay after XferNotReady significantly. Reviewed-by: Thinh Nguyen Signed-off-by: Michael Olbrich Signed-off-by: Michael Tretter Signed-off-by: Michael Grzeschik --- v1 -> v2: - removed last_frame_number from struct - included rollover variable v2 -> v3: - moved the calculation before the retry loop - skipping the calculation if bInterval > 14 v3 -> v4: - used define for 0x3fff framenumber mask - added limitation for this to usb > fullspeed v4 -> v5: - fixed limitation > fullspeed rather than > highspeed --- drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 5fb78535efa9bdc..b8240ab0ccdaf2a 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1463,6 +1463,7 @@ static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) { + const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; struct dwc3 *dwc = dep->dwc; int ret; int i; @@ -1480,6 +1481,27 @@ static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) return dwc3_gadget_start_isoc_quirk(dep); } + if (desc->bInterval <= 14 && + dwc->gadget.speed >= USB_SPEED_HIGH) { + u32 frame = __dwc3_gadget_get_frame(dwc); + bool rollover = frame < + (dep->frame_number & DWC3_FRNUMBER_MASK); + + /* + * frame_number is set from XferNotReady and may be already + * out of date. DSTS only provides the lower 14 bit of the + * current frame number. So add the upper two bits of + * frame_number and handle a possible rollover. + * This will provide the correct frame_number unless more than + * rollover has happened since XferNotReady. + */ + + dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | + frame; + if (rollover) + dep->frame_number += BIT(14); + } + for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1); From patchwork Wed Jul 1 18:24:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 11637033 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C027213B4 for ; Wed, 1 Jul 2020 18:25:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A79B720870 for ; Wed, 1 Jul 2020 18:25:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732965AbgGASZA (ORCPT ); Wed, 1 Jul 2020 14:25:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732960AbgGASY7 (ORCPT ); Wed, 1 Jul 2020 14:24:59 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77C4BC08C5DE for ; Wed, 1 Jul 2020 11:24:59 -0700 (PDT) Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jqhPj-0004Pe-RH; Wed, 01 Jul 2020 20:24:55 +0200 Received: from mgr by dude.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1jqhPj-0007RC-55; Wed, 01 Jul 2020 20:24:55 +0200 From: Michael Grzeschik To: linux-usb@vger.kernel.org Cc: Thinh.Nguyen@synopsys.com, gregkh@linuxfoundation.org, kernel@pengutronix.de, balbi@kernel.org, Thinh Nguyen Subject: [PATCH v5 3/3] usb: dwc3: gadget: when the started list is empty stop the active xfer Date: Wed, 1 Jul 2020 20:24:53 +0200 Message-Id: <20200701182453.25299-4-m.grzeschik@pengutronix.de> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200701182453.25299-1-m.grzeschik@pengutronix.de> References: <20200701182453.25299-1-m.grzeschik@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: mgr@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-usb@vger.kernel.org Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org When we have nothing left to be queued after handling the last trb we have to stop the current transfer. This way we can ensure that the next request will be queued with a new and valid timestamp and will not directly run into an missed xfer. Reviewed-by: Thinh Nguyen Signed-off-by: Michael Grzeschik --- v1 -> v2: - This Patch replaces the following patch by Michael Olbrich: usb: dwc3: gadget: restart the transfer if a isoc request is queued too late v2 -> v3: - rearranged condition for easier read (suggested by Tinh Nguyen) v3 -> v4: - no changes v4 -> v5: - fixed wording in commit message --- drivers/usb/dwc3/gadget.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index b8240ab0ccdaf2a..f6098ce9e7d4ac0 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2738,7 +2738,9 @@ static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) goto out; - if (status == -EXDEV && list_empty(&dep->started_list)) + if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && + list_empty(&dep->started_list) && + (list_empty(&dep->pending_list) || status == -EXDEV)) dwc3_stop_active_transfer(dep, true, true); else if (dwc3_gadget_ep_should_continue(dep)) if (__dwc3_gadget_kick_transfer(dep) == 0)