All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: composite: Allow bMaxPower=0 if self-powered
@ 2021-07-20  8:09 Jack Pham
  2021-07-20  9:31 ` Felipe Balbi
  0 siblings, 1 reply; 2+ messages in thread
From: Jack Pham @ 2021-07-20  8:09 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Felipe Balbi; +Cc: linux-usb, Jack Pham, Ronak Vijay Raheja

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 <rraheja@codeaurora.org>
Signed-off-by: Ronak Vijay Raheja <rraheja@codeaurora.org>
Signed-off-by: Jack Pham <jackp@codeaurora.org>
---
 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.24.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: gadget: composite: Allow bMaxPower=0 if self-powered
  2021-07-20  8:09 [PATCH] usb: gadget: composite: Allow bMaxPower=0 if self-powered Jack Pham
@ 2021-07-20  9:31 ` Felipe Balbi
  0 siblings, 0 replies; 2+ messages in thread
From: Felipe Balbi @ 2021-07-20  9:31 UTC (permalink / raw)
  To: Jack Pham, Greg Kroah-Hartman; +Cc: linux-usb, Jack Pham, Ronak Vijay Raheja

[-- Attachment #1: Type: text/plain, Size: 1366 bytes --]

Jack Pham <jackp@codeaurora.org> writes:

> 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 <rraheja@codeaurora.org>
> Signed-off-by: Ronak Vijay Raheja <rraheja@codeaurora.org>
> Signed-off-by: Jack Pham <jackp@codeaurora.org>

Nice catch!

Acked-by: Felipe Balbi <balbi@kernel.org>

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 511 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-20  9:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-20  8:09 [PATCH] usb: gadget: composite: Allow bMaxPower=0 if self-powered Jack Pham
2021-07-20  9:31 ` Felipe Balbi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.