usb: gadget: composite: Inform controller driver of self-powered
authorThinh Nguyen <Thinh.Nguyen@synopsys.com>
Tue, 4 Feb 2020 02:05:32 +0000 (18:05 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 24 Apr 2020 06:00:33 +0000 (08:00 +0200)
commit 5e5caf4fa8d3039140b4548b6ab23dd17fce9b2c upstream.

Different configuration/condition may draw different power. Inform the
controller driver of the change so it can respond properly (e.g.
GET_STATUS request). This fixes an issue with setting MaxPower from
configfs. The composite driver doesn't check this value when setting
self-powered.

Cc: stable@vger.kernel.org
Fixes: 88af8bbe4ef7 ("usb: gadget: the start of the configfs interface")
Signed-off-by: Thinh Nguyen <thinhn@synopsys.com>
Signed-off-by: Felipe Balbi <balbi@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/gadget/composite.c

index 5a4cf779b26927be823c1dcae50b7194472e8f3d..c1f037af970273cdab260f6cd4c70a69a8330c0c 100644 (file)
@@ -850,6 +850,11 @@ static int set_config(struct usb_composite_dev *cdev,
        else
                power = min(power, 900U);
 done:
+       if (power <= USB_SELF_POWER_VBUS_MAX_DRAW)
+               usb_gadget_set_selfpowered(gadget);
+       else
+               usb_gadget_clear_selfpowered(gadget);
+
        usb_gadget_vbus_draw(gadget, power);
        if (result >= 0 && cdev->delayed_status)
                result = USB_GADGET_DELAYED_STATUS;
@@ -2281,6 +2286,7 @@ void composite_suspend(struct usb_gadget *gadget)
 
        cdev->suspended = 1;
 
+       usb_gadget_set_selfpowered(gadget);
        usb_gadget_vbus_draw(gadget, 2);
 }
 
@@ -2309,6 +2315,9 @@ void composite_resume(struct usb_gadget *gadget)
                else
                        maxpower = min(maxpower, 900U);
 
+               if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW)
+                       usb_gadget_clear_selfpowered(gadget);
+
                usb_gadget_vbus_draw(gadget, maxpower);
        }