[PATCH v10 3/4] gadget: Integrate with the usb gadget supporting for usb charger

From: Baolin Wang
Date: Thu Apr 07 2016 - 07:50:28 EST


When the usb gadget supporting for usb charger is ready, the usb charger
can implement the usb_charger_plug_by_gadget() function and usb_charger_exit()
function by getting 'struct usb_charger' from 'struct gadget'.

Signed-off-by: Baolin Wang <baolin.wang@xxxxxxxxxx>
---
drivers/usb/gadget/udc/charger.c | 39 +++++++++++++++++++++++++++++++++++++-
1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/udc/charger.c b/drivers/usb/gadget/udc/charger.c
index e24cb0a..ee711cd 100644
--- a/drivers/usb/gadget/udc/charger.c
+++ b/drivers/usb/gadget/udc/charger.c
@@ -527,6 +527,30 @@ usb_charger_plug_by_extcon(struct notifier_block *nb,
int usb_charger_plug_by_gadget(struct usb_gadget *gadget,
unsigned long state)
{
+ struct usb_charger *uchger = gadget->charger;
+ enum usb_charger_state uchger_state;
+
+ if (WARN(!uchger, "charger can not be NULL"))
+ return -EINVAL;
+
+ /*
+ * Report event to power to setting the current limitation
+ * for this usb charger when one usb charger state is changed
+ * with detecting by usb gadget state.
+ */
+ if (uchger->old_gadget_state != state) {
+ uchger->old_gadget_state = state;
+
+ if (state >= USB_STATE_ATTACHED)
+ uchger_state = USB_CHARGER_PRESENT;
+ else if (state == USB_STATE_NOTATTACHED)
+ uchger_state = USB_CHARGER_REMOVE;
+ else
+ uchger_state = USB_CHARGER_DEFAULT;
+
+ usb_charger_notify_others(uchger, uchger_state);
+ }
+
return 0;
}
EXPORT_SYMBOL_GPL(usb_charger_plug_by_gadget);
@@ -683,6 +707,7 @@ int usb_charger_init(struct usb_gadget *ugadget)

/* register a notifier on a usb gadget device */
uchger->gadget = ugadget;
+ ugadget->charger = uchger;
uchger->old_gadget_state = ugadget->state;

/* register a new usb charger */
@@ -703,7 +728,19 @@ fail:

int usb_charger_exit(struct usb_gadget *ugadget)
{
- return 0;
+ struct usb_charger *uchger = ugadget->charger;
+
+ if (WARN(!uchger, "charger can not be NULL"))
+ return -EINVAL;
+
+ if (uchger->extcon_dev)
+ extcon_unregister_notifier(uchger->extcon_dev,
+ EXTCON_USB,
+ &uchger->extcon_nb.nb);
+
+ ida_simple_remove(&usb_charger_ida, uchger->id);
+
+ return usb_charger_unregister(uchger);
}

static int __init usb_charger_class_init(void)
--
1.7.9.5