Received: by 2002:a05:6520:4d:b0:139:a872:a4c9 with SMTP id i13csp2566747lkm; Mon, 20 Sep 2021 18:53:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwG6UnUdVB4DozlO9nB0WZjUe0fOsfc/UH+SedC0qOQ2CJpWv2xDlVBbq5BwRnPCRFUJUOY X-Received: by 2002:a5e:9602:: with SMTP id a2mr2955118ioq.189.1632188828442; Mon, 20 Sep 2021 18:47:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188828; cv=none; d=google.com; s=arc-20160816; b=K2L+OOECwcNHBP2wqoZnMdE3SxnRIl4W/Nlx46hHELZ7TX+npE/0aMnayA87d5cOCz QVAUnv5MioRZJsSb5lk2SmEtqg9ub24W/OmOGPCZn5POJQudniT1IBViL0hBSjdXCYqk kVeVyZ1YE51FSelSL9zOlvPy4W1TCquIYFNMZ5KjKKDwN6BlNWUFG463vm2NzZa/X9Rh nTdgY3oj3NM4ecy1oyV/BotcgBIFDes0lwYjYMcWZKgohZ9V3645LeiusDHbgRtnpL2P 3wGrDjCeY36nW0reE04mEF5Cqdi57h60i2yv+AFqT0g8ZjinBs4vhLO2s7U6PRn2VZO3 mRRQ== 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=03GirvfQKDGNIN2yhc1d1K7lXIYzC7NGlOeLaeyf9/U=; b=y0AHXm6DrPnyfQF4iG78/ScPCHuU1+faN6WoIJ+qQlkH5vvv6Oh7oxokYIQltrtqcU SMO+LAwxAD+le6Iuzy5QMJntNfnkz3of/HD0Jq6z9jHpydTeIbtgMusaFI2H6Q+lbfng rxchK+XafT0Y5VAYXPSn7dVLG9SFNjahLU7OFTCXFE/EoSUkSXnqYVOSdM5cCYMFvZct Rkr/+otshHTwJt33sfXlhP17ooX3lAm121th7OW238MYojgGXxVnTf1sjZSs+9vj2SLC Fl4Uhrb9lVCdWyBMJ3druIW/uLo7qq/ztHemh46Q3syi6uE0ueCeCgIV3AAAK09ae1Pg XQWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KEq25iL9; 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 m3si6021597iop.20.2021.09.20.18.46.57; Mon, 20 Sep 2021 18:47:08 -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=KEq25iL9; 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 S1350343AbhITR2P (ORCPT + 99 others); Mon, 20 Sep 2021 13:28:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:56030 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350035AbhITR0F (ORCPT ); Mon, 20 Sep 2021 13:26:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1CB0461A8D; Mon, 20 Sep 2021 17:02:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632157350; bh=ai2UwHHR2KnN4Z1vR6UHpHl8bW2pEuba/Zv/+xcAt04=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KEq25iL9v2+z0a3CkxpHE2bQfO+RaxIAp/JhJdr818T7T4MPqMec5zTolQowSQ/xk 6hFphjswqvCtuAlKR7WFXcoEmZ6/Hly972PVBYZL1aKSR5XJPkM/huJ7j6BDklwtCX QLVFA04dDF7BOXYeKRNfLUgaD2FMITfCNxrnSw3s= 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 4.14 141/217] usb: gadget: composite: Allow bMaxPower=0 if self-powered Date: Mon, 20 Sep 2021 18:42:42 +0200 Message-Id: <20210920163929.424520616@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163924.591371269@linuxfoundation.org> References: <20210920163924.591371269@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 6696fdd97530..49806837b98b 100644 --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -484,7 +484,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; @@ -894,7 +894,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