Add vbus draw support to DWC3
Subsystems (2)
cover Add vbus draw support to DWC3
Some devices are connected to standard downstream ports (SDP) and draw current from them. The current rates are defined in the BC1.2 specification, and highlights the different charge rates depending on the device state. The DWC3 gadget does not currently have a mechanism to notify external drivers about how much current can be drawn.
The current rates are notified by the USB gadget layer, and the DWC3 gadget will propagate this potentially to external charger drivers. Also, the USB gadget needs to be fixed to only allow 100mA current draw when receiving a bus reset from the host, as the BC1.2 specification states that this is the max current draw possible when in the connected and unconfigured state.
Wesley Cheng (3):
usb: dwc3: gadget: Introduce a DWC3 VBUS draw callback
usb: gadget: composite: Split composite reset and disconnect
usb: gadget: configfs: Add a specific configFS reset callback
drivers/usb/dwc3/gadget.c | 11 +++++++++++ drivers/usb/gadget/composite.c | 21 +++++++++++++++++++-- drivers/usb/gadget/configfs.c | 24 +++++++++++++++++++++++- include/linux/usb/composite.h | 2 ++ 4 files changed, 55 insertions(+), 3 deletions(-)
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 1/3 usb: dwc3: gadget: Introduce a DWC3 VBUS draw callback
Some devices support charging while in device mode. In these situations, the USB gadget will notify the DWC3 gadget driver to modify the current based on the enumeration and device state. The usb_phy_set_power() API will allow external charger entities to adjust the charge current through the notifier block.
--- drivers/usb/dwc3/gadget.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/usb/dwc3/gadget.c +11 -0
@@ -2387,6 +2387,16 @@ static void dwc3_gadget_set_speed(struct usb_gadget *g,spin_unlock_irqrestore (&dwc->lock, flags); } +static int dwc3_gadget_vbus_draw (struct usb_gadget *g,unsigned int mA) +{ +struct dwc3 *dwc =gadget_to_dwc (g); + +if (dwc->usb2_phy) +return usb_phy_set_power (dwc->usb2_phy, mA); + +return 0 ; +} +static const struct usb_gadget_ops dwc3_gadget_ops = { .get_frame = dwc3_gadget_get_frame, .wakeup = dwc3_gadget_wakeup,
@@ -2396,6 +2406,7 @@ static const struct usb_gadget_ops dwc3_gadget_ops = { .udc_stop = dwc3_gadget_stop, .udc_set_speed = dwc3_gadget_set_speed, .get_config_params = dwc3_gadget_config_params, + .vbus_draw = dwc3_gadget_vbus_draw, };/* -------------------------------------------------------------------------- */
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Re: [PATCH 1/3] usb: dwc3: gadget: Introduce a DWC3 VBUS draw callback
Quoted 33 lines · Wesley Cheng
Some devices support charging while in device mode. In these situations, the USB gadget will notify the DWC3 gadget driver to modify the current based on the enumeration and device state. The
usb_phy_set_power()API will allow external charger entities to adjust the charge current through the notifier block.Signed-off-by Wesley Cheng <[email protected]>--- drivers/usb/dwc3/gadget.c | 11 +++++++++++ 1 file changed, 11 insertions(+)drivers/usb/dwc3/gadget.c +10
-0@@ -2387,6 +2387,16 @@ static void dwc3_gadget_set_speed(struct usb_gadget *g,spin_unlock_irqrestore (&dwc->lock, flags); } +static int dwc3_gadget_vbus_draw (struct usb_gadget *g,unsigned int mA) +{ +struct dwc3 *dwc =gadget_to_dwc (g); + +if (dwc->usb2_phy) +return usb_phy_set_power (dwc->usb2_phy, mA); + +return 0 ; +} +static const struct usb_gadget_ops dwc3_gadget_ops = { .get_frame = dwc3_gadget_get_frame, .wakeup = dwc3_gadget_wakeup,
drivers/usb/dwc3/gadget.c static const struct usb_gadget_ops dwc3_gadget_ops = { · line 2411
Quoted 6 lines
drivers/usb/dwc3/gadget.c +1
-0@@ -2396,6 +2406,7 @@ static const struct usb_gadget_ops dwc3_gadget_ops = { .udc_stop = dwc3_gadget_stop, .udc_set_speed = dwc3_gadget_set_speed, .get_config_params = dwc3_gadget_config_params, + .vbus_draw = dwc3_gadget_vbus_draw, };
Signature · 2 lines
Thanks, Peter Chen
PATCH 2/3 usb: gadget: composite: Split composite reset and disconnect
In reply to Wesley ChengAdd a specific composite reset API to differentiate between disconnect and reset events. This is needed for adjusting the current draw accordingly based on the USB battery charging specification. The device is only allowed to draw the 500/900 mA (HS/SS) while in the CONFIGURED state, and only 100 mA in the connected and UNCONFIGURED state.
--- drivers/usb/gadget/composite.c | 21 +++++++++++++++++++-- include/linux/usb/composite.h | 2 ++ 2 files changed, 21 insertions(+), 2 deletions(-)
drivers/usb/gadget/composite.c +19 -2
@@ -2036,7 +2036,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)return value; } -void composite_disconnect (struct usb_gadget *gadget) +static void __composite_disconnect (struct usb_gadget *gadget) {struct usb_composite_dev *cdev =get_gadget_data (gadget);unsigned long flags;
@@ -2053,6 +2053,23 @@ void composite_disconnect(struct usb_gadget *gadget)spin_unlock_irqrestore (&cdev->lock, flags); } +void composite_disconnect (struct usb_gadget *gadget) +{ +usb_gadget_vbus_draw (gadget,0 ); +__composite_disconnect (gadget); +} + +void composite_reset (struct usb_gadget *gadget) +{ +/* +* Section 1.4.13 Standard Downstream Port of the USB battery charging +* specification v1.2 states that a device connected on a SDP shall only +* draw at max 100mA while in a connected, but unconfigured state. +*/ +usb_gadget_vbus_draw (gadget,100 ); +__composite_disconnect (gadget); +} +/*-------------------------------------------------------------------------*/ static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
@@ -2373,7 +2390,7 @@ static const struct usb_gadget_driver composite_driver_template = { .unbind = composite_unbind, .setup = composite_setup, - .reset = composite_disconnect, + .reset = composite_reset, .disconnect = composite_disconnect, .suspend = composite_suspend,
include/linux/usb/composite.h +2 -0
@@ -525,6 +525,8 @@ extern struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,extern int usb_string_ids_n (struct usb_composite_dev *c,unsigned n);extern void composite_disconnect (struct usb_gadget *gadget); +extern void composite_reset (struct usb_gadget *gadget); +extern int composite_setup (struct usb_gadget *gadget,const struct usb_ctrlrequest *ctrl);extern void composite_suspend (struct usb_gadget *gadget);
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Re: [PATCH 2/3] usb: gadget: composite: Split composite reset and disconnect
Quoted 7 lines · Wesley Cheng
Add a specific composite reset API to differentiate between disconnect and reset events. This is needed for adjusting the current draw accordingly based on the USB battery charging specification. The device is only allowed to draw the 500/900 mA (HS/SS) while in the CONFIGURED state, and only 100 mA in the connected and UNCONFIGURED state.
Signed-off-by Wesley Cheng <[email protected]>
Peter
Quoted 68 lines
--- drivers/usb/gadget/composite.c | 21 +++++++++++++++++++-- include/linux/usb/composite.h | 2 ++ 2 files changed, 21 insertions(+), 2 deletions(-)drivers/usb/gadget/composite.c +19
-2@@ -2036,7 +2036,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)return value; } -void composite_disconnect (struct usb_gadget *gadget) +static void __composite_disconnect (struct usb_gadget *gadget) {struct usb_composite_dev *cdev =get_gadget_data (gadget);unsigned long flags;@@ -2053,6 +2053,23 @@ void composite_disconnect(struct usb_gadget *gadget)spin_unlock_irqrestore (&cdev->lock, flags); } +void composite_disconnect (struct usb_gadget *gadget) +{ +usb_gadget_vbus_draw (gadget,0 ); +__composite_disconnect (gadget); +} + +void composite_reset (struct usb_gadget *gadget) +{ +/* +* Section 1.4.13 Standard Downstream Port of the USB battery charging +* specification v1.2 states that a device connected on a SDP shall only +* draw at max 100mA while in a connected, but unconfigured state. +*/ +usb_gadget_vbus_draw (gadget,100 ); +__composite_disconnect (gadget); +} +/*-------------------------------------------------------------------------*/ static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,@@ -2373,7 +2390,7 @@ static const struct usb_gadget_driver composite_driver_template = { .unbind = composite_unbind, .setup = composite_setup, - .reset = composite_disconnect, + .reset = composite_reset, .disconnect = composite_disconnect, .suspend = composite_suspend,include/linux/usb/composite.h +2
-0@@ -525,6 +525,8 @@ extern struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,extern int usb_string_ids_n (struct usb_composite_dev *c,unsigned n);extern void composite_disconnect (struct usb_gadget *gadget); +extern void composite_reset (struct usb_gadget *gadget); +extern int composite_setup (struct usb_gadget *gadget,const struct usb_ctrlrequest *ctrl);extern void composite_suspend (struct usb_gadget *gadget);Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Signature · 2 lines
Thanks, Peter Chen
PATCH 3/3 usb: gadget: configfs: Add a specific configFS reset callback
In reply to Wesley ChengIn order for configFS based USB gadgets to set the proper charge current for bus reset scenarios, expose a separate reset callback to set the current to 100mA based on the USB battery charging specification.
--- drivers/usb/gadget/configfs.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/usb/gadget/configfs.c +23 -1
@@ -1481,6 +1481,28 @@ static void configfs_composite_disconnect(struct usb_gadget *gadget)spin_unlock_irqrestore (&gi->spinlock, flags); } +static void configfs_composite_reset (struct usb_gadget *gadget) +{ +struct usb_composite_dev *cdev; +struct gadget_info *gi; +unsigned long flags; + + cdev =get_gadget_data (gadget); +if (!cdev) +return ; + + gi = container_of(cdev, struct gadget_info, cdev); +spin_lock_irqsave (&gi->spinlock, flags); + cdev =get_gadget_data (gadget); +if (!cdev || gi->unbind) { +spin_unlock_irqrestore (&gi->spinlock, flags); +return ; + } + +composite_reset (gadget); +spin_unlock_irqrestore (&gi->spinlock, flags); +} +static void configfs_composite_suspend (struct usb_gadget *gadget) {struct usb_composite_dev *cdev;
@@ -1530,7 +1552,7 @@ static const struct usb_gadget_driver configfs_driver_template = { .unbind = configfs_composite_unbind, .setup = configfs_composite_setup, - .reset = configfs_composite_disconnect, + .reset = configfs_composite_reset, .disconnect = configfs_composite_disconnect, .suspend = configfs_composite_suspend,
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Re: [PATCH 3/3] usb: gadget: configfs: Add a specific configFS reset callback
Quoted 5 lines · Wesley Cheng
In order for configFS based USB gadgets to set the proper charge current for bus reset scenarios, expose a separate reset callback to set the current to 100mA based on the USB battery charging specification.
Signed-off-by Wesley Cheng <[email protected]>
Quoted 50 lines
--- drivers/usb/gadget/configfs.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)drivers/usb/gadget/configfs.c +23
-1@@ -1481,6 +1481,28 @@ static void configfs_composite_disconnect(struct usb_gadget *gadget)spin_unlock_irqrestore (&gi->spinlock, flags); } +static void configfs_composite_reset (struct usb_gadget *gadget) +{ +struct usb_composite_dev *cdev; +struct gadget_info *gi; +unsigned long flags; + + cdev =get_gadget_data (gadget); +if (!cdev) +return ; + + gi = container_of(cdev, struct gadget_info, cdev); +spin_lock_irqsave (&gi->spinlock, flags); + cdev =get_gadget_data (gadget); +if (!cdev || gi->unbind) { +spin_unlock_irqrestore (&gi->spinlock, flags); +return ; + } + +composite_reset (gadget); +spin_unlock_irqrestore (&gi->spinlock, flags); +} +static void configfs_composite_suspend (struct usb_gadget *gadget) {struct usb_composite_dev *cdev;@@ -1530,7 +1552,7 @@ static const struct usb_gadget_driver configfs_driver_template = { .unbind = configfs_composite_unbind, .setup = configfs_composite_setup, - .reset = configfs_composite_disconnect, + .reset = configfs_composite_reset, .disconnect = configfs_composite_disconnect, .suspend = configfs_composite_suspend,Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Signature · 2 lines
Thanks, Peter Chen