2020-11-14 08:16:47

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH 0/3] 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(-)

--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project


2020-11-14 08:17:31

by Wesley Cheng

[permalink] [raw]
Subject: [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.

Signed-off-by: Wesley Cheng <[email protected]>
---
drivers/usb/dwc3/gadget.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index c145da1d8ba5..69122f66978e 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -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,
};

/* -------------------------------------------------------------------------- */
--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project

2020-11-14 08:18:22

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH 3/3] usb: gadget: configfs: Add a specific configFS reset callback

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]>
---
drivers/usb/gadget/configfs.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 56051bb97349..80ca7ff2fb97 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -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,
--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project

2020-11-16 13:48:13

by Peter Chen

[permalink] [raw]
Subject: Re: [PATCH 1/3] usb: dwc3: gadget: Introduce a DWC3 VBUS draw callback

On 20-11-14 00:12:45, Wesley Cheng wrote:
> 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(+)
>
> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index c145da1d8ba5..69122f66978e 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -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,
> };
>

Reviewed-by: Peter Chen <[email protected]>

--

Thanks,
Peter Chen

2020-11-16 14:00:29

by Peter Chen

[permalink] [raw]
Subject: Re: [PATCH 3/3] usb: gadget: configfs: Add a specific configFS reset callback

On 20-11-14 00:12:47, Wesley Cheng wrote:
> 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]>

Reviewed-by: Peter Chen <[email protected]>

> ---
> drivers/usb/gadget/configfs.c | 24 +++++++++++++++++++++++-
> 1 file changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
> index 56051bb97349..80ca7ff2fb97 100644
> --- a/drivers/usb/gadget/configfs.c
> +++ b/drivers/usb/gadget/configfs.c
> @@ -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,
> --
> The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
> a Linux Foundation Collaborative Project
>

--

Thanks,
Peter Chen