Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp520347pxb; Thu, 9 Sep 2021 06:20:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5hgsUg8ym48wu14DccuTxhhx3BzFTovJGQs5R4X9LieoGHapOkRHnfKETQ4bjiLnfHrIw X-Received: by 2002:a02:a10b:: with SMTP id f11mr2855877jag.16.1631193610423; Thu, 09 Sep 2021 06:20:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631193610; cv=none; d=google.com; s=arc-20160816; b=goebZ5pUlrKDUnqUWWbqSdcDGCe1ItjUMd/i0xwdqtwfEpVdvQYkCsEDV1ct5I9Hue PxBc0GlviKu0TXlpVJFkoeaidZELzPkKM13sIpEDsum9SNSLaH2NjXnN4z3Cj3tMs7/d Q9GtR7IDLrPuVGl0723TfkN9fqhwpbCIv74/NkHUcmlS1FYUWWJt5y/W8W+w17NqVfvl cFYUAbPvD6+mfbez5ANsaRG4HHCJ4zD/n35RHCnpQbs37B9mxK5SvCl6D75S+1CyjYUv ApyJ04OdcwnNzd6kOjrSolQdlXlHogI3pEe+uj8bBbbcFSCh5OzCdo7XTXTK7t9jjtWj pp6A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=03GirvfQKDGNIN2yhc1d1K7lXIYzC7NGlOeLaeyf9/U=; b=t88lA7DOcNX3ZjeNq/Ye/g8eATtUQdX+ulxsh6VNe5C8uWDqaFWJKgm3Ub9gIM9Qeh osEe2d/zRz+ZM5jfDoRJLFx0k6zZUoWCPXCWpV5NVZBQT0qMpE9t2MLmktPGxpVHUi3u SZjsN5Z4qgsI2EyoHoGgvxdWXMOSmqDH1HPhOy5fRAdVbrQBG8Y/v6C701mUJ/+8/0Ae XUl7/oFe88kzZx1ebxlPNcUKy1nFCPpWdozBuDgMpdhHgDUYU3m3EVEbBfXhVTN+YyfA P8PULjioWgXP3dwX8yQr6Z8VlgqrYNAnC6LUASYWTNgwgJsN2Pzsj/kggFewLpzLxiqD tpvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JuxDOfLa; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t22si1837093iom.43.2021.09.09.06.19.58; Thu, 09 Sep 2021 06:20:10 -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=@kernel.org header.s=k20201202 header.b=JuxDOfLa; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352864AbhIINQ5 (ORCPT + 99 others); Thu, 9 Sep 2021 09:16:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:42454 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355949AbhIINBD (ORCPT ); Thu, 9 Sep 2021 09:01:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CC5A9611C4; Thu, 9 Sep 2021 11:59:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631188756; bh=ai2UwHHR2KnN4Z1vR6UHpHl8bW2pEuba/Zv/+xcAt04=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JuxDOfLaksVXeR6nVBYweC+sG2RsYO/6KfdUBsPVFHDu/B8egeBi9Ayrw0ArYMVXj OwEE5tn+mUC3EyppWgNdqoR3/kIVRaOhHzG8m93INLR8Z4EcMmp0I5P42+85ELQk0M nPtOys15mEc/ngqXB+0LScDSFk45aF6YZeJYqsf+mrQhCV5Ij1+tRTUVPyfgEUZ4Bn pf6Kz0mG6y521KCHQvQoh4gJgxPmUht7v2rZF2b0CM5Mkjx5CkrBHdJeeZbpkuiUoV M8N6gWlczODm3Eh6qu1pCqhAqHeJyHjNvJ3fXXAW4HZceG8GQRW1pUAdLkZDCz1geP MLzcI6NygkS4Q== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jack Pham , Ronak Vijay Raheja , Felipe Balbi , Greg Kroah-Hartman , Sasha Levin , linux-usb@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 12/59] usb: gadget: composite: Allow bMaxPower=0 if self-powered Date: Thu, 9 Sep 2021 07:58:13 -0400 Message-Id: <20210909115900.149795-12-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909115900.149795-1-sashal@kernel.org> References: <20210909115900.149795-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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