From patchwork Sun Mar 12 22:48:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruslan Bilovol X-Patchwork-Id: 68589 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp911854wrd; Sun, 12 Mar 2023 16:07:30 -0700 (PDT) X-Google-Smtp-Source: AK7set+DKjoI9H2jeKT7kQEuy5AzR07BXtb+CWw/+QrD1GsjzJtNgqwwjwiMR9r33mYS7U52Yog2 X-Received: by 2002:a17:902:e847:b0:19f:36b1:c35 with SMTP id t7-20020a170902e84700b0019f36b10c35mr5004485plg.64.1678662450042; Sun, 12 Mar 2023 16:07:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678662450; cv=none; d=google.com; s=arc-20160816; b=SLwoUPTfh5Y7LI9bcugIU2687WkBynBjdBVVc4Q4w5naCWJdwkdsVkC2qrrru+Lidi R4hVZlxLw1ze8L/lwKh5z1BHY9HE+2knKcCOp4bwcJ9THURGcRRL8dyKqz2n1DTPR9kP cOQjYwIxqQt3BiQjDBKDSEj+6aqMrGeM4wsS96AyK0oU418d7/U1Nqjq3a/NuH8pRJ2F wrGcuMPoazqirofjd7UojLzRU4fAJFkEoHZNTyzgQ14iDEWKhKaYVuT2chLzv4DB3/vQ eQUJDCIpTxGME9oDdwD91UhsRYzjtgZA9aOA3AJsNWMCgxuLz9TqwnSvPe+9g1PUI/vL YH7A== 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=xYIG2TIps/UChe2tmlcHWvW9bzPS2sZ9r5iw2+hEA+k=; b=rz+7pdazziaReWWnHbkAYV6zfhnBS6/pSojzdACkG9YWI0hUpQ9b1LfTts9i8P+TGg 8bpRta0pUPL88NwWpqLucSlQ6nW7ZPEDkIRl1G0V8ZUrXmhgI0DqVPvA7A2ww8rvC0Mx Lv92hpdz2JadT4lNlzxDHSrARc/En9C27y7eE55rqZjSmIOFfuO9ifBLgCQbZf2ME9Jq pLwbdYB8P7Om4GmoRGIsBYbcE41SASqAXLi/SjD3qCBhc4YMvl87h62bfmO774SYaqeb 9TnsWKgKHV3rKqO0ccIIqQRnEeVe5NpYXO60jG1Pf7oyA8ZBroU9JWoI29ap4N/CIH1w 51Eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JPVY1ePc; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t12-20020a170902e84c00b0019cc45a0958si5458312plg.149.2023.03.12.16.07.12; Sun, 12 Mar 2023 16:07:30 -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=@gmail.com header.s=20210112 header.b=JPVY1ePc; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229550AbjCLWsk (ORCPT + 99 others); Sun, 12 Mar 2023 18:48:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjCLWsj (ORCPT ); Sun, 12 Mar 2023 18:48:39 -0400 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12FED32E4D; Sun, 12 Mar 2023 15:48:38 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id ne1so7200321qvb.9; Sun, 12 Mar 2023 15:48:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678661317; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=xYIG2TIps/UChe2tmlcHWvW9bzPS2sZ9r5iw2+hEA+k=; b=JPVY1ePc1a3gjwTW8x47phBxj6NqlY9yiADR1dVl/xI9bbeO/sG01Cm6cfZu9+o+fB sfiTKThIMGhjo8YjUJWVefvRJQsoMhLu2eznPndD8obBGY8jcAp/A7pmnFrCbLByJUpz rkGaV41VkxnPS9HnqN8EnJryCGRObKC26pKkgoD5PIyjRz66lR8/XLNiNXsntwZawZ6B rBS9+6U45dACma4BCt/S1r5qfXg1L51OALkuAF9H9JyRBUemR+xBBhJvhWJS7pDPkKaa RKXVQEDk3numuPfloGw0jpKh/tAvQM+N3FD0OhCS9TPHpLoFjyHVffxGaOsBwfHmG8lm Sn8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678661317; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xYIG2TIps/UChe2tmlcHWvW9bzPS2sZ9r5iw2+hEA+k=; b=uRPwjzwoG3bu2volrUl4Wt5UjsAbA4TfWu9lmzUNegLsGiMl5NyPr0iJcHoE5566+O sONppnenk9MPGBDlnq8UG7Pe8uT/oDGFRh+zK1Dh8+4yymqar9skQ9L7zvy2vkZ2T4ay OfX6qh3VAuVoYjAzoTiuxFpNEAmUiQrMjJjl5/xbTDbW4llMl2BQCdCkuL8bVQzTgJKg ZuczBQgKV3yq9iHO7wjt4CFXyKd30GUM4neUFFdszjjMnEwHyE3yYHss/ccQm+zlbCwU EAhyUimTCKuXI9QdxaRdkWhieTpgrNWXt0yjWXS+/LKzuGKlFzRmu4t0fMHzr6jJzCOb N1oA== X-Gm-Message-State: AO0yUKXCeu/cmFpfuiCptHtynamfSMR8spDf37SFYl2Db+rDn45qgNFl Ok9o7FPX8jwsmReW/eDmSg4= X-Received: by 2002:a05:6214:238e:b0:572:5a16:51c0 with SMTP id fw14-20020a056214238e00b005725a1651c0mr10317950qvb.16.1678661317054; Sun, 12 Mar 2023 15:48:37 -0700 (PDT) Received: from localhost ([72.138.97.66]) by smtp.gmail.com with ESMTPSA id m6-20020aed27c6000000b003b62bc6cd1csm4416792qtg.82.2023.03.12.15.48.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Mar 2023 15:48:36 -0700 (PDT) From: Ruslan Bilovol To: gregkh@linuxfoundation.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, peter.chen@kernel.org Subject: [PATCH v3] usb: gadget: epautoconf: claim smallest endpoints first Date: Sun, 12 Mar 2023 18:48:36 -0400 Message-Id: <20230312224836.297793-1-ruslan.bilovol@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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?1760205156854816605?= X-GMAIL-MSGID: =?utf-8?q?1760205156854816605?= UDC hardware may have endpoints with different maxpacket size. Current endpoint matching code takes first matching endpoint from the list. It's always possible that gadget allocates endpoints for small transfers (maxpacket size) first, then larger ones. That works fine if all matching UDC endpoints have same maxpacket size or are big enough to serve that allocation. However, some UDCs have first endpoints in the list with bigger maxpacket size, whereas last endpoints are much smaller. In this case endpoint allocation will fail for the gadget (which allocates smaller endpoints first) on final endpoint allocations. To make endpoint allocation fair, pick up smallest matching endpoints first, leaving bigger ones for heavier applications. Keel old behavior when "wMaxPacketSize == 0" because it's a special case. In this case a gadget driver wants to use a whole available MaxPacketSize of claimed endpoint. Since it doesn't know what MaxPacketSize may be in a particular UDC endpoint, it just relies on epautoconf core and gets what's available Signed-off-by: Ruslan Bilovol --- v3: updated commit msg, rebased onto latest gregkh/usb-next v2: rebased onto latest balbi/next branch v1: https://lore.kernel.org/lkml/20200629200551.27040-1-ruslan.bilovol@gmail.com/ drivers/usb/gadget/epautoconf.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c index ed5a92c474e5..086bb46e3f5a 100644 --- a/drivers/usb/gadget/epautoconf.c +++ b/drivers/usb/gadget/epautoconf.c @@ -66,7 +66,7 @@ struct usb_ep *usb_ep_autoconfig_ss( struct usb_ss_ep_comp_descriptor *ep_comp ) { - struct usb_ep *ep; + struct usb_ep *ep, *ep_min = NULL; if (gadget->ops->match_ep) { ep = gadget->ops->match_ep(gadget, desc, ep_comp); @@ -74,14 +74,27 @@ struct usb_ep *usb_ep_autoconfig_ss( goto found_ep; } - /* Second, look at endpoints until an unclaimed one looks usable */ + /* + * Second, look at endpoints until an unclaimed one looks usable. + * Try to find one with smallest maxpacket limit, leaving larger + * endpoints for heavier applications + */ list_for_each_entry (ep, &gadget->ep_list, ep_list) { - if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp)) - goto found_ep; + if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp)) { + if (desc->wMaxPacketSize == 0) + goto found_ep; + else if (!ep_min) + ep_min = ep; + else if (ep->maxpacket_limit < ep_min->maxpacket_limit) + ep_min = ep; + } } /* Fail */ - return NULL; + if (!ep_min) + return NULL; + + ep = ep_min; found_ep: /*