Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1700217pxb; Thu, 16 Sep 2021 13:20:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVh0UaynIn5XLmhRShxeAgCk4TAO+VGTNWuI6HamHfv0y3bljNiscrnWWmi+tzPIfhLnjX X-Received: by 2002:aa7:dbd2:: with SMTP id v18mr8630704edt.315.1631823613237; Thu, 16 Sep 2021 13:20:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631823613; cv=none; d=google.com; s=arc-20160816; b=YtJliewKKZ+oEphd0OmOWb4T3X/J7a9hZQxcAoHgkBQsGQDtaYdVeyxa4kKzcV/Pg7 yQLphcMFryzq4IeAN3a1b7i3LBc0dIDWXJ7EN3A8eZfynH9OB6LTfMj9sYFKrXnSDWpv TJczG3dkxrHAyN1CNP1rHJN+1laNhsio9YZrE6JJ/zf4xRK0evxa8HY3noxBTkQvu4Ou NooarInUotVdbTwRazDk2U5H9dKPo5Gx4JHidJyv40U+CcpEFVT3JG4zLGeuar612sQa g6OpHtntawRVtX6n0NeKmE24GRtC7AcFapCwVsCr8O/zGhcknxE5OHqiNyAELUy5RCrd w9vw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=xf0nreU53tKQmzGrncfP4os5DrB4MJqn6qbW2Uy1SzY=; b=eMmkQdJkBeKC6maWimBWvlSc1y0NN3zoyGqqD3sW4uzf0HC6CUJugEvXys7WARLPPD fDPBuAF6XhAj+zXWHHiAWl7hot0j2Pk6UIV6++TPUcyWl6kzt79P38uo7iF0gWuvAkb4 SM6GM6x8XkAAojdbQBDfqfZYIJdSAM7Ym8L62CN3X48AK3NfSAEyuGFIPmX+/nRIA7Gf lOB9mR8rHaEA1Th1MSTZbniKRXTpfUc7Ol9xWPUAuMG+VAzapfBCT6hRNfwkCQozYUp6 vL5mf621tM3w6hpZKrzhnh1SDxxpNrDY6gI9Ay+l2iRq6CFSQMVnCiB21qPqP3iIXNgJ j/dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WKUx8xJB; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b12si4406507ede.366.2021.09.16.13.19.48; Thu, 16 Sep 2021 13:20:13 -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=@linuxfoundation.org header.s=korg header.b=WKUx8xJB; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240987AbhIPQVP (ORCPT + 99 others); Thu, 16 Sep 2021 12:21:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:47416 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236775AbhIPQMO (ORCPT ); Thu, 16 Sep 2021 12:12:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B86156137B; Thu, 16 Sep 2021 16:09:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631808562; bh=ei9+rvRbmfFyYLzz70tRzEyrFIUp1vtpS67bRJuwIWI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WKUx8xJBnUyW5L92dDN38mGMqV/zFh+Cif2QAX4ooQbJd6H7PELtdPyRQo97cMrHV MEdzwPwSZaTHqDrqX/Kh9LmTLSREqH2rBCnwhezOFdV+/G33A9pSrBX4slBsmSviM1 n1tqvh+L16kAMoajJrUIrv5+lwummgB58Y8UwID4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Felipe Balbi , Ronak Vijay Raheja , Jack Pham , Sasha Levin Subject: [PATCH 5.10 136/306] usb: gadget: composite: Allow bMaxPower=0 if self-powered Date: Thu, 16 Sep 2021 17:58:01 +0200 Message-Id: <20210916155758.682562069@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155753.903069397@linuxfoundation.org> References: <20210916155753.903069397@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jack Pham [ Upstream commit bcacbf06c891374e7fdd7b72d11cda03b0269b43 ] Currently the composite driver encodes the MaxPower field of the configuration descriptor by reading the c->MaxPower of the usb_configuration only if it is non-zero, otherwise it falls back to using the value hard-coded in CONFIG_USB_GADGET_VBUS_DRAW. However, there are cases when a configuration must explicitly set bMaxPower to 0, particularly if its bmAttributes also has the Self-Powered bit set, which is a valid combination. This is specifically called out in the USB PD specification section 9.1, in which a PDUSB device "shall report zero in the bMaxPower field after negotiating a mutually agreeable Contract", and also verified by the USB Type-C Functional Test TD.4.10.2 Sink Power Precedence Test. The fix allows the c->MaxPower to be used for encoding the bMaxPower even if it is 0, if the self-powered bit is also set. An example usage of this would be for a ConfigFS gadget to be dynamically updated by userspace when the Type-C connection is determined to be operating in Power Delivery mode. Co-developed-by: Ronak Vijay Raheja Acked-by: Felipe Balbi Signed-off-by: Ronak Vijay Raheja Signed-off-by: Jack Pham Link: https://lore.kernel.org/r/20210720080907.30292-1-jackp@codeaurora.org Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/composite.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c index 1a556a628971..3ffa939678d7 100644 --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -481,7 +481,7 @@ static u8 encode_bMaxPower(enum usb_device_speed speed, { unsigned val; - if (c->MaxPower) + if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER)) val = c->MaxPower; else val = CONFIG_USB_GADGET_VBUS_DRAW; @@ -905,7 +905,11 @@ static int set_config(struct usb_composite_dev *cdev, } /* when we return, be sure our power usage is valid */ - power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW; + if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER)) + power = c->MaxPower; + else + power = CONFIG_USB_GADGET_VBUS_DRAW; + if (gadget->speed < USB_SPEED_SUPER) power = min(power, 500U); else -- 2.30.2