@@ -39,21 +39,25 @@ struct otgws_lock {
};
/*
- * VBUS present lock.
++ * VBUS present lock. Also used as a timed lock on charger
++ * connect/disconnect and USB host disconnect, to allow the system
++ * to react to the change in power.
*/
static struct otgws_lock vbus_lock;
-static int otgws_otg_notifications(struct notifier_block *nb,
- unsigned long event, void *unused)
+static void otgws_handle_event(unsigned long event)
{
unsigned long irqflags;
- if (!enabled)
- return NOTIFY_OK;
-
spin_lock_irqsave(&otgws_spinlock, irqflags);
+ if (!enabled) {
+ __pm_relax(&vbus_lock.wsource);
+ spin_unlock_irqrestore(&otgws_spinlock, irqflags);
+ return;
+ }
+
switch (event) {
case USB_EVENT_VBUS:
case USB_EVENT_ENUMERATED:
@@ -63,7 +67,8 @@ static int otgws_otg_notifications(struct notifier_block *nb,
case USB_EVENT_NONE:
case USB_EVENT_ID:
case USB_EVENT_CHARGER:
- __pm_relax(&vbus_lock.wsource);
+ __pm_wakeup_event(&vbus_lock.wsource,
+ msecs_to_jiffies(TEMPORARY_HOLD_TIME));
break;
default:
@@ -71,72 +76,25 @@ static int otgws_otg_notifications(struct notifier_block *nb,
}
spin_unlock_irqrestore(&otgws_spinlock, irqflags);
- return NOTIFY_OK;
-}
-
-static void sync_with_xceiv_state(void)
-{
- if ((otgws_xceiv->last_event == USB_EVENT_VBUS) ||
- (otgws_xceiv->last_event == USB_EVENT_ENUMERATED))
- __pm_stay_awake(&vbus_lock.wsource);
- else
- __pm_relax(&vbus_lock.wsource);
}
-static int init_for_xceiv(void)
+static int otgws_otg_notifications(struct notifier_block *nb,
+ unsigned long event, void *unused)
{
- int rv;
- struct usb_phy *phy;
-
- if (!otgws_xceiv) {
- phy = usb_get_phy(USB_PHY_TYPE_USB2);
-
- if (IS_ERR(phy)) {
- pr_err("%s: No USB transceiver found\n", __func__);
- return PTR_ERR(phy);
- }
- otgws_xceiv = phy;
-
- snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s",
- dev_name(otgws_xceiv->dev));
- wakeup_source_init(&vbus_lock.wsource, vbus_lock.name);
-
- rv = usb_register_notifier(otgws_xceiv, &otgws_nb);
-
- if (rv) {
- pr_err("%s: usb_register_notifier on transceiver %s
- failed\n", __func__,
- dev_name(otgws_xceiv->dev));
- otgws_xceiv = NULL;
- wakeup_source_trash(&vbus_lock.wsource);
- return rv;
- }
- }
-
- return 0;
+ otgws_handle_event(event);
+ return NOTIFY_OK;
}
static int set_enabled(const char *val, const struct kernel_param *kp)
{
- unsigned long irqflags;
int rv = param_set_bool(val, kp);
if (rv)
return rv;
- rv = init_for_xceiv();
-
- if (rv)
- return rv;
-
- spin_lock_irqsave(&otgws_spinlock, irqflags);
-
- if (enabled)
- sync_with_xceiv_state();
- else
- __pm_relax(&vbus_lock.wsource);
+ if (otgws_xceiv)
+ otgws_handle_event(otgws_xceiv->last_event);
- spin_unlock_irqrestore(&otgws_spinlock, irqflags);
return 0;
}
@@ -150,19 +108,31 @@ MODULE_PARM_DESC(enabled, "enable wakelock when VBUS present");
static int __init otg_wakeupsource_init(void)
{
- unsigned long irqflags;
+ int ret;
+ struct usb_phy *phy;
- otgws_nb.notifier_call = otgws_otg_notifications;
+ phy = usb_get_phy(USB_PHY_TYPE_USB2);
- if (!init_for_xceiv()) {
- spin_lock_irqsave(&otgws_spinlock, irqflags);
+ if (IS_ERR(phy)) {
+ pr_err("%s: No OTG transceiver found\n", __func__);
+ return PTR_ERR(phy);
+ }
- if (enabled)
- sync_with_xceiv_state();
+ otgws_xceiv = phy;
- spin_unlock_irqrestore(&otgws_spinlock, irqflags);
- } else {
- enabled = false;
+ snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s",
+ dev_name(otgws_xceiv->dev));
+ wakeup_source_init(&vbus_lock.wsource, vbus_lock.name);
+
+ otgws_nb.notifier_call = otgws_otg_notifications;
+ ret = usb_register_notifier(otgws_xceiv, &otgws_nb);
+
+ if (ret) {
+ pr_err("%s: usb_register_notifier on transceiver %s failed\n",
+ __func__, dev_name(otgws_xceiv->dev));
+ otgws_xceiv = NULL;
+ wakeup_source_trash(&vbus_lock.wsource);
+ return ret;
}
return 0;
@@ -11,6 +11,8 @@
#include <linux/usb/phy.h>
+#define TEMPORARY_HOLD_TIME 2000
+
struct usb_otg {
u8 default_a;