From 9ffbd89c4a1a19fb58a8e9ae202d124b8161ca48 Mon Sep 17 00:00:00 2001 From: Ronak Vijay Raheja Date: Mon, 3 Aug 2020 14:24:06 -0700 Subject: [PATCH] USB: gadget: composite: Allow bMaxPower=0 if self-powered 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 may be times when a configuration may need to explicitly set the bMaxPower to 0, particularly if its bmAttributes also has the Self-Powered bit set, which is a legal combination. This is specifically called out in the USB PD specification section 9.1, where a PDUSB device "shall report zero in the bMaxPower field after negotiating a mutually agreeable Contract", and is also verified by the USB Type-C Functional Test TD.4.10.2 Sink Power Precedence Test. This 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. A typical usage of this would be for the configFS gadget to be dynamically updated by userspace when the Type-C connection is determined to be operating in Power Delivery mode. Change-Id: I9fce2123b23ca3fe250940757002669ab92cb21f Signed-off-by: Ronak Vijay Raheja --- drivers/usb/gadget/composite.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c index aa5d141b04a2..bb47d39aa0ff 100644 --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -520,7 +520,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;