diff options
Diffstat (limited to 'drivers/usb/otg')
-rw-r--r-- | drivers/usb/otg/Kconfig | 8 | ||||
-rw-r--r-- | drivers/usb/otg/Makefile | 2 | ||||
-rw-r--r-- | drivers/usb/otg/otg-wakelock.c | 169 | ||||
-rw-r--r-- | drivers/usb/otg/otg_id.c | 205 |
4 files changed, 384 insertions, 0 deletions
diff --git a/drivers/usb/otg/Kconfig b/drivers/usb/otg/Kconfig index c66481a..cd77719 100644 --- a/drivers/usb/otg/Kconfig +++ b/drivers/usb/otg/Kconfig @@ -12,6 +12,14 @@ config USB_OTG_UTILS Select this to make sure the build includes objects from the OTG infrastructure directory. +config USB_OTG_WAKELOCK + bool "Hold a wakelock when USB connected" + depends on WAKELOCK + select USB_OTG_UTILS + help + Select this to automatically hold a wakelock when USB is + connected, preventing suspend. + if USB || USB_GADGET # diff --git a/drivers/usb/otg/Makefile b/drivers/usb/otg/Makefile index 566655c..d2c0a7b 100644 --- a/drivers/usb/otg/Makefile +++ b/drivers/usb/otg/Makefile @@ -7,6 +7,8 @@ ccflags-$(CONFIG_USB_GADGET_DEBUG) += -DDEBUG # infrastructure obj-$(CONFIG_USB_OTG_UTILS) += otg.o +obj-$(CONFIG_USB_OTG_WAKELOCK) += otg-wakelock.o +obj-$(CONFIG_USB_OTG_UTILS) += otg_id.o # transceiver drivers obj-$(CONFIG_USB_GPIO_VBUS) += gpio_vbus.o diff --git a/drivers/usb/otg/otg-wakelock.c b/drivers/usb/otg/otg-wakelock.c new file mode 100644 index 0000000..2f11472 --- /dev/null +++ b/drivers/usb/otg/otg-wakelock.c @@ -0,0 +1,169 @@ +/* + * otg-wakelock.c + * + * Copyright (C) 2011 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include <linux/kernel.h> +#include <linux/device.h> +#include <linux/notifier.h> +#include <linux/wakelock.h> +#include <linux/spinlock.h> +#include <linux/usb/otg.h> + +#define TEMPORARY_HOLD_TIME 2000 + +static bool enabled = true; +static struct otg_transceiver *otgwl_xceiv; +static struct notifier_block otgwl_nb; + +/* + * otgwl_spinlock is held while the VBUS lock is grabbed or dropped and the + * held field is updated to match. + */ + +static DEFINE_SPINLOCK(otgwl_spinlock); + +/* + * Only one lock, but since these 3 fields are associated with each other... + */ + +struct otgwl_lock { + char name[40]; + struct wake_lock wakelock; + bool held; +}; + +/* + * 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 otgwl_lock vbus_lock; + +static void otgwl_hold(struct otgwl_lock *lock) +{ + if (!lock->held) { + wake_lock(&lock->wakelock); + lock->held = true; + } +} + +static void otgwl_temporary_hold(struct otgwl_lock *lock) +{ + wake_lock_timeout(&lock->wakelock, + msecs_to_jiffies(TEMPORARY_HOLD_TIME)); + lock->held = false; +} + +static void otgwl_drop(struct otgwl_lock *lock) +{ + if (lock->held) { + wake_unlock(&lock->wakelock); + lock->held = false; + } +} + +static void otgwl_handle_event(unsigned long event) +{ + unsigned long irqflags; + + spin_lock_irqsave(&otgwl_spinlock, irqflags); + + if (!enabled) { + otgwl_drop(&vbus_lock); + spin_unlock_irqrestore(&otgwl_spinlock, irqflags); + return; + } + + switch (event) { + case USB_EVENT_VBUS: + case USB_EVENT_ENUMERATED: + otgwl_hold(&vbus_lock); + break; + + case USB_EVENT_NONE: + case USB_EVENT_ID: + case USB_EVENT_CHARGER: + otgwl_temporary_hold(&vbus_lock); + break; + + default: + break; + } + + spin_unlock_irqrestore(&otgwl_spinlock, irqflags); +} + +static int otgwl_otg_notifications(struct notifier_block *nb, + unsigned long event, void *unused) +{ + otgwl_handle_event(event); + return NOTIFY_OK; +} + +static int set_enabled(const char *val, const struct kernel_param *kp) +{ + int rv = param_set_bool(val, kp); + + if (rv) + return rv; + + if (otgwl_xceiv) + otgwl_handle_event(otgwl_xceiv->last_event); + + return 0; +} + +static struct kernel_param_ops enabled_param_ops = { + .set = set_enabled, + .get = param_get_bool, +}; + +module_param_cb(enabled, &enabled_param_ops, &enabled, 0644); +MODULE_PARM_DESC(enabled, "enable wakelock when VBUS present"); + +static int __init otg_wakelock_init(void) +{ + int ret; + + otgwl_xceiv = otg_get_transceiver(); + + if (!otgwl_xceiv) { + pr_err("%s: No OTG transceiver found\n", __func__); + return -ENODEV; + } + + snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s", + dev_name(otgwl_xceiv->dev)); + wake_lock_init(&vbus_lock.wakelock, WAKE_LOCK_SUSPEND, + vbus_lock.name); + + otgwl_nb.notifier_call = otgwl_otg_notifications; + ret = otg_register_notifier(otgwl_xceiv, &otgwl_nb); + + if (ret) { + pr_err("%s: otg_register_notifier on transceiver %s" + " failed\n", __func__, + dev_name(otgwl_xceiv->dev)); + otgwl_xceiv = NULL; + wake_lock_destroy(&vbus_lock.wakelock); + return ret; + } + + otgwl_handle_event(otgwl_xceiv->last_event); + return ret; +} + +late_initcall(otg_wakelock_init); diff --git a/drivers/usb/otg/otg_id.c b/drivers/usb/otg/otg_id.c new file mode 100644 index 0000000..8037edb --- /dev/null +++ b/drivers/usb/otg/otg_id.c @@ -0,0 +1,205 @@ +/* + * Copyright (C) 2011 Google, Inc. + * + * Author: + * Colin Cross <ccross@android.com> + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include <linux/kernel.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/usb/otg_id.h> + +static DEFINE_MUTEX(otg_id_lock); +static struct plist_head otg_id_plist = + PLIST_HEAD_INIT(otg_id_plist); +static struct otg_id_notifier_block *otg_id_active; +static bool otg_id_cancelling; +static bool otg_id_inited; +static int otg_id_suspended; +static bool otg_id_pending; + +static void otg_id_cancel(void) +{ + if (otg_id_active) { + otg_id_cancelling = true; + mutex_unlock(&otg_id_lock); + + otg_id_active->cancel(otg_id_active); + + mutex_lock(&otg_id_lock); + otg_id_cancelling = false; + } +} + +static void __otg_id_notify(void) +{ + int ret; + struct otg_id_notifier_block *otg_id_nb; + bool proxy_wait = false; + if (plist_head_empty(&otg_id_plist)) + return; + + plist_for_each_entry(otg_id_nb, &otg_id_plist, p) { + if (proxy_wait) { + if (otg_id_nb->proxy_wait) + ret = otg_id_nb->proxy_wait(otg_id_nb); + } else { + ret = otg_id_nb->detect(otg_id_nb); + } + if (ret == OTG_ID_HANDLED) { + otg_id_active = otg_id_nb; + return; + } + if (ret == OTG_ID_PROXY_WAIT) + proxy_wait = true; + + } + + WARN(1, "otg id event not handled"); + otg_id_active = NULL; +} + +int otg_id_init(void) +{ + mutex_lock(&otg_id_lock); + + otg_id_inited = true; + __otg_id_notify(); + + mutex_unlock(&otg_id_lock); + return 0; +} +late_initcall(otg_id_init); + +/** + * otg_id_register_notifier + * @otg_id_nb: notifier block containing priority and callback function + * + * Register a notifier that will be called on any USB cable state change. + * The priority determines the order the callback will be called in, a higher + * number will be called first. A callback function needs to determine the + * type of USB cable that is connected. If it can determine the type, it + * should notify the appropriate drivers (for example, call an otg notifier + * with USB_EVENT_VBUS), and return OTG_ID_HANDLED. Once a callback has + * returned OTG_ID_HANDLED, it is responsible for calling otg_id_notify() when + * the detected USB cable is disconnected. + */ +int otg_id_register_notifier(struct otg_id_notifier_block *otg_id_nb) +{ + plist_node_init(&otg_id_nb->p, otg_id_nb->priority); + + mutex_lock(&otg_id_lock); + plist_add(&otg_id_nb->p, &otg_id_plist); + + if (otg_id_inited) { + otg_id_cancel(); + __otg_id_notify(); + } + + mutex_unlock(&otg_id_lock); + + return 0; +} + +void otg_id_unregister_notifier(struct otg_id_notifier_block *otg_id_nb) +{ + mutex_lock(&otg_id_lock); + + plist_del(&otg_id_nb->p, &otg_id_plist); + + if (otg_id_inited && (otg_id_active == otg_id_nb)) { + otg_id_cancel(); + __otg_id_notify(); + } + + mutex_unlock(&otg_id_lock); +} + +/** + * otg_id_notify + * + * Notify listeners on any USB cable state change. + * + * A driver may only call otg_id_notify if it returned OTG_ID_HANDLED the last + * time it's notifier was called, and it's cancel function has not been called. + */ +void otg_id_notify(void) +{ + mutex_lock(&otg_id_lock); + + if (otg_id_cancelling) + goto out; + + if (otg_id_suspended != 0) { + otg_id_pending = true; + goto out; + } + + __otg_id_notify(); +out: + mutex_unlock(&otg_id_lock); +} + +/** + * otg_id_suspend + * + * Mark the otg_id subsystem as going into suspend. From here on out, + * any notifications will be deferred until the last otg_id client resumes. + * If there is a pending notification when calling this function, it will + * return a negative errno and expects that the caller will abort suspend. + * Returs 0 on success. + */ +int otg_id_suspend(void) +{ + int ret = 0; + + mutex_lock(&otg_id_lock); + + /* + * if there's a pending notification, tell the caller to abort suspend + */ + if (otg_id_suspended != 0 && otg_id_pending) { + pr_info("otg_id: pending notification, should abort suspend\n"); + ret = -EBUSY; + goto out; + } + + otg_id_suspended++; +out: + mutex_unlock(&otg_id_lock); + return ret; +} + +/** + * otg_id_resume + * + * Inform the otg_id subsystem that a client is resuming. If this is the + * last client to be resumed and there's a pending notification, + * otg_id_notify() is called. + */ +void otg_id_resume(void) +{ + mutex_lock(&otg_id_lock); + if (WARN(!otg_id_suspended, "unbalanced otg_id_resume\n")) + goto out; + if (--otg_id_suspended == 0) { + if (otg_id_pending) { + pr_info("otg_id: had pending notification\n"); + otg_id_pending = false; + __otg_id_notify(); + } + } +out: + mutex_unlock(&otg_id_lock); +} |