5d4c8b2446f787d47a85dec2cff7c6e97fe2c392
[pandora-kernel.git] / net / rfkill / rfkill-input.c
1 /*
2  * Input layer to RF Kill interface connector
3  *
4  * Copyright (c) 2007 Dmitry Torokhov
5  */
6
7 /*
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published
10  * by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/input.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/init.h>
18 #include <linux/rfkill.h>
19
20 #include "rfkill-input.h"
21
22 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
23 MODULE_DESCRIPTION("Input layer to RF switch connector");
24 MODULE_LICENSE("GPL");
25
26 struct rfkill_task {
27         struct work_struct work;
28         enum rfkill_type type;
29         struct mutex mutex; /* ensures that task is serialized */
30         spinlock_t lock; /* for accessing last and desired state */
31         unsigned long last; /* last schedule */
32         enum rfkill_state desired_state; /* on/off */
33 };
34
35 static void rfkill_task_handler(struct work_struct *work)
36 {
37         struct rfkill_task *task = container_of(work, struct rfkill_task, work);
38
39         mutex_lock(&task->mutex);
40
41         rfkill_switch_all(task->type, task->desired_state);
42
43         mutex_unlock(&task->mutex);
44 }
45
46 static void rfkill_task_epo_handler(struct work_struct *work)
47 {
48         rfkill_epo();
49 }
50
51 static DECLARE_WORK(epo_work, rfkill_task_epo_handler);
52
53 static void rfkill_schedule_epo(void)
54 {
55         schedule_work(&epo_work);
56 }
57
58 static void rfkill_schedule_set(struct rfkill_task *task,
59                                 enum rfkill_state desired_state)
60 {
61         unsigned long flags;
62
63         if (unlikely(work_pending(&epo_work)))
64                 return;
65
66         spin_lock_irqsave(&task->lock, flags);
67
68         if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
69                 task->desired_state = desired_state;
70                 task->last = jiffies;
71                 schedule_work(&task->work);
72         }
73
74         spin_unlock_irqrestore(&task->lock, flags);
75 }
76
77 static void rfkill_schedule_toggle(struct rfkill_task *task)
78 {
79         unsigned long flags;
80
81         if (unlikely(work_pending(&epo_work)))
82                 return;
83
84         spin_lock_irqsave(&task->lock, flags);
85
86         if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
87                 task->desired_state = !task->desired_state;
88                 task->last = jiffies;
89                 schedule_work(&task->work);
90         }
91
92         spin_unlock_irqrestore(&task->lock, flags);
93 }
94
95 #define DEFINE_RFKILL_TASK(n, t)                        \
96         struct rfkill_task n = {                        \
97                 .work = __WORK_INITIALIZER(n.work,      \
98                                 rfkill_task_handler),   \
99                 .type = t,                              \
100                 .mutex = __MUTEX_INITIALIZER(n.mutex),  \
101                 .lock = __SPIN_LOCK_UNLOCKED(n.lock),   \
102                 .desired_state = RFKILL_STATE_ON,       \
103         }
104
105 static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
106 static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
107 static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
108 static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX);
109 static DEFINE_RFKILL_TASK(rfkill_wwan, RFKILL_TYPE_WWAN);
110
111 static void rfkill_event(struct input_handle *handle, unsigned int type,
112                         unsigned int code, int data)
113 {
114         if (type == EV_KEY && data == 1) {
115                 switch (code) {
116                 case KEY_WLAN:
117                         rfkill_schedule_toggle(&rfkill_wlan);
118                         break;
119                 case KEY_BLUETOOTH:
120                         rfkill_schedule_toggle(&rfkill_bt);
121                         break;
122                 case KEY_UWB:
123                         rfkill_schedule_toggle(&rfkill_uwb);
124                         break;
125                 case KEY_WIMAX:
126                         rfkill_schedule_toggle(&rfkill_wimax);
127                         break;
128                 default:
129                         break;
130                 }
131         } else if (type == EV_SW) {
132                 switch (code) {
133                 case SW_RFKILL_ALL:
134                         /* EVERY radio type. data != 0 means radios ON */
135                         /* handle EPO (emergency power off) through shortcut */
136                         if (data) {
137                                 rfkill_schedule_set(&rfkill_wwan,
138                                                     RFKILL_STATE_ON);
139                                 rfkill_schedule_set(&rfkill_wimax,
140                                                     RFKILL_STATE_ON);
141                                 rfkill_schedule_set(&rfkill_uwb,
142                                                     RFKILL_STATE_ON);
143                                 rfkill_schedule_set(&rfkill_bt,
144                                                     RFKILL_STATE_ON);
145                                 rfkill_schedule_set(&rfkill_wlan,
146                                                     RFKILL_STATE_ON);
147                         } else
148                                 rfkill_schedule_epo();
149                         break;
150                 default:
151                         break;
152                 }
153         }
154 }
155
156 static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
157                           const struct input_device_id *id)
158 {
159         struct input_handle *handle;
160         int error;
161
162         handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
163         if (!handle)
164                 return -ENOMEM;
165
166         handle->dev = dev;
167         handle->handler = handler;
168         handle->name = "rfkill";
169
170         error = input_register_handle(handle);
171         if (error)
172                 goto err_free_handle;
173
174         error = input_open_device(handle);
175         if (error)
176                 goto err_unregister_handle;
177
178         return 0;
179
180  err_unregister_handle:
181         input_unregister_handle(handle);
182  err_free_handle:
183         kfree(handle);
184         return error;
185 }
186
187 static void rfkill_disconnect(struct input_handle *handle)
188 {
189         input_close_device(handle);
190         input_unregister_handle(handle);
191         kfree(handle);
192 }
193
194 static const struct input_device_id rfkill_ids[] = {
195         {
196                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
197                 .evbit = { BIT_MASK(EV_KEY) },
198                 .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
199         },
200         {
201                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
202                 .evbit = { BIT_MASK(EV_KEY) },
203                 .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
204         },
205         {
206                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
207                 .evbit = { BIT_MASK(EV_KEY) },
208                 .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
209         },
210         {
211                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
212                 .evbit = { BIT_MASK(EV_KEY) },
213                 .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
214         },
215         {
216                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
217                 .evbit = { BIT(EV_SW) },
218                 .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
219         },
220         { }
221 };
222
223 static struct input_handler rfkill_handler = {
224         .event =        rfkill_event,
225         .connect =      rfkill_connect,
226         .disconnect =   rfkill_disconnect,
227         .name =         "rfkill",
228         .id_table =     rfkill_ids,
229 };
230
231 static int __init rfkill_handler_init(void)
232 {
233         return input_register_handler(&rfkill_handler);
234 }
235
236 static void __exit rfkill_handler_exit(void)
237 {
238         input_unregister_handler(&rfkill_handler);
239         flush_scheduled_work();
240 }
241
242 module_init(rfkill_handler_init);
243 module_exit(rfkill_handler_exit);