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 <rraheja@codeaurora.org>
This commit is contained in:
Ronak Vijay Raheja 2020-08-03 14:24:06 -07:00
commit 9ffbd89c4a

View file

@ -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;