Colin Cross | ab246b8 | 2011-06-24 15:58:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Google, Inc. |
| 3 | * |
| 4 | * Author: |
| 5 | * Colin Cross <ccross@android.com> |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/notifier.h> |
| 21 | #include <linux/usb/otg_id.h> |
| 22 | |
| 23 | static DEFINE_MUTEX(otg_id_lock); |
| 24 | static struct plist_head otg_id_plist = |
| 25 | PLIST_HEAD_INIT(otg_id_plist); |
| 26 | static struct otg_id_notifier_block *otg_id_active; |
| 27 | static bool otg_id_cancelling; |
| 28 | static bool otg_id_inited; |
| 29 | |
| 30 | static void otg_id_cancel(void) |
| 31 | { |
| 32 | if (otg_id_active) { |
| 33 | otg_id_cancelling = true; |
| 34 | mutex_unlock(&otg_id_lock); |
| 35 | |
| 36 | otg_id_active->cancel(otg_id_active); |
| 37 | |
| 38 | mutex_lock(&otg_id_lock); |
| 39 | otg_id_cancelling = false; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static void __otg_id_notify(void) |
| 44 | { |
| 45 | int ret; |
| 46 | struct otg_id_notifier_block *otg_id_nb; |
Erik Gilling | e65d9fe | 2011-08-09 14:18:21 -0700 | [diff] [blame^] | 47 | bool proxy_wait = false; |
Colin Cross | ab246b8 | 2011-06-24 15:58:51 -0700 | [diff] [blame] | 48 | if (plist_head_empty(&otg_id_plist)) |
| 49 | return; |
| 50 | |
| 51 | plist_for_each_entry(otg_id_nb, &otg_id_plist, p) { |
Erik Gilling | e65d9fe | 2011-08-09 14:18:21 -0700 | [diff] [blame^] | 52 | if (proxy_wait) { |
| 53 | if (otg_id_nb->proxy_wait) |
| 54 | ret = otg_id_nb->proxy_wait(otg_id_nb); |
| 55 | } else { |
| 56 | ret = otg_id_nb->detect(otg_id_nb); |
| 57 | } |
Colin Cross | ab246b8 | 2011-06-24 15:58:51 -0700 | [diff] [blame] | 58 | if (ret == OTG_ID_HANDLED) { |
| 59 | otg_id_active = otg_id_nb; |
| 60 | return; |
| 61 | } |
Erik Gilling | e65d9fe | 2011-08-09 14:18:21 -0700 | [diff] [blame^] | 62 | if (ret == OTG_ID_PROXY_WAIT) |
| 63 | proxy_wait = true; |
| 64 | |
Colin Cross | ab246b8 | 2011-06-24 15:58:51 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | WARN(1, "otg id event not handled"); |
| 68 | otg_id_active = NULL; |
| 69 | } |
| 70 | |
| 71 | int otg_id_init(void) |
| 72 | { |
| 73 | mutex_lock(&otg_id_lock); |
| 74 | |
| 75 | otg_id_inited = true; |
| 76 | __otg_id_notify(); |
| 77 | |
| 78 | mutex_unlock(&otg_id_lock); |
| 79 | return 0; |
| 80 | } |
| 81 | late_initcall(otg_id_init); |
| 82 | |
| 83 | /** |
| 84 | * otg_id_register_notifier |
| 85 | * @otg_id_nb: notifier block containing priority and callback function |
| 86 | * |
| 87 | * Register a notifier that will be called on any USB cable state change. |
| 88 | * The priority determines the order the callback will be called in, a higher |
| 89 | * number will be called first. A callback function needs to determine the |
| 90 | * type of USB cable that is connected. If it can determine the type, it |
| 91 | * should notify the appropriate drivers (for example, call an otg notifier |
| 92 | * with USB_EVENT_VBUS), and return OTG_ID_HANDLED. Once a callback has |
| 93 | * returned OTG_ID_HANDLED, it is responsible for calling otg_id_notify() when |
| 94 | * the detected USB cable is disconnected. |
| 95 | */ |
| 96 | int otg_id_register_notifier(struct otg_id_notifier_block *otg_id_nb) |
| 97 | { |
| 98 | plist_node_init(&otg_id_nb->p, otg_id_nb->priority); |
| 99 | |
| 100 | mutex_lock(&otg_id_lock); |
| 101 | plist_add(&otg_id_nb->p, &otg_id_plist); |
| 102 | |
| 103 | if (otg_id_inited) { |
| 104 | otg_id_cancel(); |
| 105 | __otg_id_notify(); |
| 106 | } |
| 107 | |
| 108 | mutex_unlock(&otg_id_lock); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | void otg_id_unregister_notifier(struct otg_id_notifier_block *otg_id_nb) |
| 114 | { |
| 115 | mutex_lock(&otg_id_lock); |
| 116 | |
| 117 | plist_del(&otg_id_nb->p, &otg_id_plist); |
| 118 | |
| 119 | if (otg_id_inited && (otg_id_active == otg_id_nb)) { |
| 120 | otg_id_cancel(); |
| 121 | __otg_id_notify(); |
| 122 | } |
| 123 | |
| 124 | mutex_unlock(&otg_id_lock); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * otg_id_notify |
| 129 | * |
| 130 | * Notify listeners on any USB cable state change. |
| 131 | * |
| 132 | * A driver may only call otg_id_notify if it returned OTG_ID_HANDLED the last |
| 133 | * time it's notifier was called, and it's cancel function has not been called. |
| 134 | */ |
| 135 | void otg_id_notify(void) |
| 136 | { |
| 137 | mutex_lock(&otg_id_lock); |
| 138 | |
| 139 | if (otg_id_cancelling) |
| 140 | goto out; |
| 141 | |
| 142 | __otg_id_notify(); |
| 143 | |
| 144 | out: |
| 145 | mutex_unlock(&otg_id_lock); |
| 146 | } |