Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1936970pxb; Thu, 16 Sep 2021 20:54:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJycoXSJJNA7ANMRz6Zm7+EaLdHx09t8dl0J/juS/m+nkMWmwZTi349RcRjmUXSTnjFjsdqv X-Received: by 2002:a17:906:9ad0:: with SMTP id ah16mr10397567ejc.43.1631850843756; Thu, 16 Sep 2021 20:54:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631850843; cv=none; d=google.com; s=arc-20160816; b=lvoLmJ9kLGEYCtJg+E72jGJn9PFYrsbZ/Z4/7/syeWWTCml4VbpUoCdivXBhuFur+a FSP8+27Fomd3qCu66DKVBRcyPoZwU54df9aEJ+JloWoY8uM0Y7xL/WyLcbEEl3ThrnEH fAOU3S58GO0xZWYdWFg7+b5V6xKLHgAVWPJ1FDOjrKKHnOKU06CQIuo7OLHO+cD1fKhz e5+Vvge/9F0WYoDDVPnGU6vUjgR0atVDlErK5U9iSb34bwh4R9eM9UEiDowido3eDjm4 /01Et7i6YM3Vl2v8iefB6vbtN+OwN2z2dP6Vb9gfa0+jqV6BzlHK52NkxwzYgE853PS7 4Alw== 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=soh4CCm8BI6OMpP9aKr3fJXk3sq6ahKhxK7a2BgwIMk=; b=svveROrO7VHh0NGYeHPqvMqkV3IyvrN0b1Ps4XxBvrKM2UoqbB/kRQPpZQRCphP8+z CXKO/iFJAjWTg1t7mPo/9YmofYyfhr2UzGjWRwGtNh4jsAy3CzTWN5AHVMenHvOBxOlO DoEG84jURNmocoSa+1e4lVy+54nTu5xcc8DpmCeCIDWZ5kigenWdsrP+SaHVd0M+iXZ5 fVL/kssrdUy5ocKmdxmBCZVLKEDFS7xX1alp7YAYTUwBSPizRPp0si9UibsSFCpGv5rq fUPoRI1SW/Tg70jn5JE62WHxm9svYkz0WDbDlKw0MVV1qP0UfqjNW3iZmQt08gHMTakA HLVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VWTXqwhX; 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 de41si5234092ejc.244.2021.09.16.20.53.40; Thu, 16 Sep 2021 20:54:03 -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=VWTXqwhX; 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 S1353486AbhIPReG (ORCPT + 99 others); Thu, 16 Sep 2021 13:34:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:47064 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352490AbhIPRYj (ORCPT ); Thu, 16 Sep 2021 13:24:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E6C0361A51; Thu, 16 Sep 2021 16:44:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631810641; bh=9dp2HsjXh0v3QGCZzbXCEO3dbuO7d+bUPgzfJXClYpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VWTXqwhXorzfMZWBL7P/zQHmIL8bxCa4hSn1AoUOkE2/X70+4qvcdY4fStRiRj8bF bOT5YCHw7z7LIuj5BTCeia9WF8919Aj6ElhJNDkskvEcSmaVI9RfGyhfHLiOdFvZYi 8jnVAJjzNsk45H/QGrSpvxF43svqO6SP2x18VENA= 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.14 198/432] usb: gadget: composite: Allow bMaxPower=0 if self-powered Date: Thu, 16 Sep 2021 17:59:07 +0200 Message-Id: <20210916155817.514315041@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155810.813340753@linuxfoundation.org> References: <20210916155810.813340753@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 72a9797dbbae..504c1cbc255d 100644 --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -482,7 +482,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; @@ -936,7 +936,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