Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp446679pxb; Thu, 9 Sep 2021 04:48:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz50pkVGgFq5Ab//BsXgRcTG+8uXbsYtj9eC9GrWjv01bK3PLsyjPOOZhQd5i+IJ31tKsny X-Received: by 2002:a05:6402:1cb9:: with SMTP id cz25mr2777586edb.11.1631188082373; Thu, 09 Sep 2021 04:48:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631188082; cv=none; d=google.com; s=arc-20160816; b=YL7utqa1shSUQLwRpVbX4RyV4EGxSgopu/BYSzxITpEqPRiFhlF+ttDtV8DXJHtaOZ P67Eo/+QtnMPAl6gMSbyaPmHSDi1oBR1oOmINw9fLU9VNbgv5wjjEnTVksEanXz5Le3Q yZfqnpRz7dNCU/ehe+wHbvS6ch503HvtVg94Gtsk8zp72Sqj/qGvKGIeIFXGdgnak5sE AH68oBx9hbtNUV8JkO1WClw3khEbPY4IH6wf/1yUM6Rg/yFyINUQsy0gbUIxqbWrBNfm XNUx0tX8fLV7z4d1c4xO2yxOKJaoITkXcyTSzF/yJVz/afcQyxvdkGq8oc5WqHHA50Fv h6NA== 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=soh4CCm8BI6OMpP9aKr3fJXk3sq6ahKhxK7a2BgwIMk=; b=NevJO5vTArNICCOQoWryLQfKaS7oChV0NeU20wsb9IWOH2Hzr1HFTvekvJyRBXE40v du0+ZZBTV0eOV1E9aiDtWnjXp0xYX2+X4JJ/tRX7looUWytsRBadTmZDouXgAGire9l9 x8ZyHeGhSzDZokXF888ShQ1E4aWSJGOyJ8BvHdep98opl6bG4XVXBQfnv3euYQ4LhA3i /tKdwtgaMXHMj5t3JUTK88s5f3zbNedJmC1u0z8gAMK6cbm6A04103WAH12Kk4yDZPAU pzh6Cp4zc8r6DWfvTqehNTcn1TYCNzseFhnlFspSUi5QZhnTOyGp/vCrUk2x2XLgexPS 5WRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Edmx1nPw; 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 f25si1864520ejh.250.2021.09.09.04.47.36; Thu, 09 Sep 2021 04:48:02 -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=Edmx1nPw; 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 S238521AbhIILpL (ORCPT + 99 others); Thu, 9 Sep 2021 07:45:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:47266 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237891AbhIILnV (ORCPT ); Thu, 9 Sep 2021 07:43:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7647C61206; Thu, 9 Sep 2021 11:42:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631187722; bh=9dp2HsjXh0v3QGCZzbXCEO3dbuO7d+bUPgzfJXClYpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Edmx1nPwlguFye9xoq4n+QkkdTGfCFldw+IpmR1In5hpK89uJKsk1/B381fkwJ27g /XjGciN+zfTPHX5EenSYddUghOaGzY2jKWh6z94r4qyn8Vl4Otr/sB8eL/g+NhCjrC 63zsCRuzl67ZNQcMcpKUQCthc2q0pX/+AKhzkhzv47EQ8Xt4mBgE/n1l1QyS4tcKYU RvksA4zBHnWLt+N+4l07arYRQxF311GPr7TQOU55ouq6iuXdKW/Pwp+XhfdjPZpGMM /YWAcUjpD8WYmtIq7ZN7Rf64m09BedeTHOzjg1CPxn20cTpBoJiEke+KTZvlPWbq9u lL84T+FgTGufg== 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 5.14 043/252] usb: gadget: composite: Allow bMaxPower=0 if self-powered Date: Thu, 9 Sep 2021 07:37:37 -0400 Message-Id: <20210909114106.141462-43-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909114106.141462-1-sashal@kernel.org> References: <20210909114106.141462-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 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