blob: 2398b1a951d4c3a1a90e39683b411e08e981dab2 [file] [log] [blame]
Colin Crossab246b82011-06-24 15:58:51 -07001/*
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
23static DEFINE_MUTEX(otg_id_lock);
24static struct plist_head otg_id_plist =
25 PLIST_HEAD_INIT(otg_id_plist);
26static struct otg_id_notifier_block *otg_id_active;
27static bool otg_id_cancelling;
28static bool otg_id_inited;
29
30static 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
43static void __otg_id_notify(void)
44{
45 int ret;
46 struct otg_id_notifier_block *otg_id_nb;
47
48 if (plist_head_empty(&otg_id_plist))
49 return;
50
51 plist_for_each_entry(otg_id_nb, &otg_id_plist, p) {
52 ret = otg_id_nb->detect(otg_id_nb);
53 if (ret == OTG_ID_HANDLED) {
54 otg_id_active = otg_id_nb;
55 return;
56 }
57 }
58
59 WARN(1, "otg id event not handled");
60 otg_id_active = NULL;
61}
62
63int otg_id_init(void)
64{
65 mutex_lock(&otg_id_lock);
66
67 otg_id_inited = true;
68 __otg_id_notify();
69
70 mutex_unlock(&otg_id_lock);
71 return 0;
72}
73late_initcall(otg_id_init);
74
75/**
76 * otg_id_register_notifier
77 * @otg_id_nb: notifier block containing priority and callback function
78 *
79 * Register a notifier that will be called on any USB cable state change.
80 * The priority determines the order the callback will be called in, a higher
81 * number will be called first. A callback function needs to determine the
82 * type of USB cable that is connected. If it can determine the type, it
83 * should notify the appropriate drivers (for example, call an otg notifier
84 * with USB_EVENT_VBUS), and return OTG_ID_HANDLED. Once a callback has
85 * returned OTG_ID_HANDLED, it is responsible for calling otg_id_notify() when
86 * the detected USB cable is disconnected.
87 */
88int otg_id_register_notifier(struct otg_id_notifier_block *otg_id_nb)
89{
90 plist_node_init(&otg_id_nb->p, otg_id_nb->priority);
91
92 mutex_lock(&otg_id_lock);
93 plist_add(&otg_id_nb->p, &otg_id_plist);
94
95 if (otg_id_inited) {
96 otg_id_cancel();
97 __otg_id_notify();
98 }
99
100 mutex_unlock(&otg_id_lock);
101
102 return 0;
103}
104
105void otg_id_unregister_notifier(struct otg_id_notifier_block *otg_id_nb)
106{
107 mutex_lock(&otg_id_lock);
108
109 plist_del(&otg_id_nb->p, &otg_id_plist);
110
111 if (otg_id_inited && (otg_id_active == otg_id_nb)) {
112 otg_id_cancel();
113 __otg_id_notify();
114 }
115
116 mutex_unlock(&otg_id_lock);
117}
118
119/**
120 * otg_id_notify
121 *
122 * Notify listeners on any USB cable state change.
123 *
124 * A driver may only call otg_id_notify if it returned OTG_ID_HANDLED the last
125 * time it's notifier was called, and it's cancel function has not been called.
126 */
127void otg_id_notify(void)
128{
129 mutex_lock(&otg_id_lock);
130
131 if (otg_id_cancelling)
132 goto out;
133
134 __otg_id_notify();
135
136out:
137 mutex_unlock(&otg_id_lock);
138}