wl1251: fix a memory leak in probe
[pandora-wifi.git] / net / rfkill / input.c
1 /*
2  * Input layer to RF Kill interface connector
3  *
4  * Copyright (c) 2007 Dmitry Torokhov
5  * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published
9  * by the Free Software Foundation.
10  *
11  * If you ever run into a situation in which you have a SW_ type rfkill
12  * input device, then you can revive code that was removed in the patch
13  * "rfkill-input: remove unused code".
14  */
15
16 #include <linux/input.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19 #include <linux/init.h>
20 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31))
21 #include <linux/rfkill.h>
22 #else
23 #include <linux/rfkill_backport.h>
24 #endif
25 #include <linux/sched.h>
26
27 #include "rfkill.h"
28
29 enum rfkill_input_master_mode {
30         RFKILL_INPUT_MASTER_UNLOCK = 0,
31         RFKILL_INPUT_MASTER_RESTORE = 1,
32         RFKILL_INPUT_MASTER_UNBLOCKALL = 2,
33         NUM_RFKILL_INPUT_MASTER_MODES
34 };
35
36 /* Delay (in ms) between consecutive switch ops */
37 #define RFKILL_OPS_DELAY 200
38
39 static enum rfkill_input_master_mode rfkill_master_switch_mode =
40                                         RFKILL_INPUT_MASTER_UNBLOCKALL;
41 module_param_named(master_switch_mode, rfkill_master_switch_mode, uint, 0);
42 MODULE_PARM_DESC(master_switch_mode,
43         "SW_RFKILL_ALL ON should: 0=do nothing (only unlock); 1=restore; 2=unblock all");
44
45 static spinlock_t rfkill_op_lock;
46 static bool rfkill_op_pending;
47 static unsigned long rfkill_sw_pending[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
48 static unsigned long rfkill_sw_state[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
49
50 enum rfkill_sched_op {
51         RFKILL_GLOBAL_OP_EPO = 0,
52         RFKILL_GLOBAL_OP_RESTORE,
53         RFKILL_GLOBAL_OP_UNLOCK,
54         RFKILL_GLOBAL_OP_UNBLOCK,
55 };
56
57 static enum rfkill_sched_op rfkill_master_switch_op;
58 static enum rfkill_sched_op rfkill_op;
59
60 static void __rfkill_handle_global_op(enum rfkill_sched_op op)
61 {
62         unsigned int i;
63
64         switch (op) {
65         case RFKILL_GLOBAL_OP_EPO:
66                 rfkill_epo();
67                 break;
68         case RFKILL_GLOBAL_OP_RESTORE:
69                 rfkill_restore_states();
70                 break;
71         case RFKILL_GLOBAL_OP_UNLOCK:
72                 rfkill_remove_epo_lock();
73                 break;
74         case RFKILL_GLOBAL_OP_UNBLOCK:
75                 rfkill_remove_epo_lock();
76                 for (i = 0; i < NUM_RFKILL_TYPES; i++)
77                         rfkill_switch_all(i, false);
78                 break;
79         default:
80                 /* memory corruption or bug, fail safely */
81                 rfkill_epo();
82                 WARN(1, "Unknown requested operation %d! "
83                         "rfkill Emergency Power Off activated\n",
84                         op);
85         }
86 }
87
88 static void __rfkill_handle_normal_op(const enum rfkill_type type,
89                                       const bool complement)
90 {
91         bool blocked;
92
93         blocked = rfkill_get_global_sw_state(type);
94         if (complement)
95                 blocked = !blocked;
96
97         rfkill_switch_all(type, blocked);
98 }
99
100 static void rfkill_op_handler(struct work_struct *work)
101 {
102         unsigned int i;
103         bool c;
104
105         spin_lock_irq(&rfkill_op_lock);
106         do {
107                 if (rfkill_op_pending) {
108                         enum rfkill_sched_op op = rfkill_op;
109                         rfkill_op_pending = false;
110                         memset(rfkill_sw_pending, 0,
111                                 sizeof(rfkill_sw_pending));
112                         spin_unlock_irq(&rfkill_op_lock);
113
114                         __rfkill_handle_global_op(op);
115
116                         spin_lock_irq(&rfkill_op_lock);
117
118                         /*
119                          * handle global ops first -- during unlocked period
120                          * we might have gotten a new global op.
121                          */
122                         if (rfkill_op_pending)
123                                 continue;
124                 }
125
126                 if (rfkill_is_epo_lock_active())
127                         continue;
128
129                 for (i = 0; i < NUM_RFKILL_TYPES; i++) {
130                         if (__test_and_clear_bit(i, rfkill_sw_pending)) {
131                                 c = __test_and_clear_bit(i, rfkill_sw_state);
132                                 spin_unlock_irq(&rfkill_op_lock);
133
134                                 __rfkill_handle_normal_op(i, c);
135
136                                 spin_lock_irq(&rfkill_op_lock);
137                         }
138                 }
139         } while (rfkill_op_pending);
140         spin_unlock_irq(&rfkill_op_lock);
141 }
142
143 static DECLARE_DELAYED_WORK(rfkill_op_work, rfkill_op_handler);
144 static unsigned long rfkill_last_scheduled;
145
146 static unsigned long rfkill_ratelimit(const unsigned long last)
147 {
148         const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY);
149         return (time_after(jiffies, last + delay)) ? 0 : delay;
150 }
151
152 static void rfkill_schedule_ratelimited(void)
153 {
154         if (delayed_work_pending(&rfkill_op_work))
155                 return;
156         schedule_delayed_work(&rfkill_op_work,
157                               rfkill_ratelimit(rfkill_last_scheduled));
158         rfkill_last_scheduled = jiffies;
159 }
160
161 static void rfkill_schedule_global_op(enum rfkill_sched_op op)
162 {
163         unsigned long flags;
164
165         spin_lock_irqsave(&rfkill_op_lock, flags);
166         rfkill_op = op;
167         rfkill_op_pending = true;
168         if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) {
169                 /* bypass the limiter for EPO */
170                 cancel_delayed_work(&rfkill_op_work);
171                 schedule_delayed_work(&rfkill_op_work, 0);
172                 rfkill_last_scheduled = jiffies;
173         } else
174                 rfkill_schedule_ratelimited();
175         spin_unlock_irqrestore(&rfkill_op_lock, flags);
176 }
177
178 static void rfkill_schedule_toggle(enum rfkill_type type)
179 {
180         unsigned long flags;
181
182         if (rfkill_is_epo_lock_active())
183                 return;
184
185         spin_lock_irqsave(&rfkill_op_lock, flags);
186         if (!rfkill_op_pending) {
187                 __set_bit(type, rfkill_sw_pending);
188                 __change_bit(type, rfkill_sw_state);
189                 rfkill_schedule_ratelimited();
190         }
191         spin_unlock_irqrestore(&rfkill_op_lock, flags);
192 }
193
194 static void rfkill_schedule_evsw_rfkillall(int state)
195 {
196         if (state)
197                 rfkill_schedule_global_op(rfkill_master_switch_op);
198         else
199                 rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
200 }
201
202 static void rfkill_event(struct input_handle *handle, unsigned int type,
203                         unsigned int code, int data)
204 {
205         if (type == EV_KEY && data == 1) {
206                 switch (code) {
207                 case KEY_WLAN:
208                         rfkill_schedule_toggle(RFKILL_TYPE_WLAN);
209                         break;
210                 case KEY_BLUETOOTH:
211                         rfkill_schedule_toggle(RFKILL_TYPE_BLUETOOTH);
212                         break;
213                 case KEY_UWB:
214                         rfkill_schedule_toggle(RFKILL_TYPE_UWB);
215                         break;
216                 case KEY_WIMAX:
217                         rfkill_schedule_toggle(RFKILL_TYPE_WIMAX);
218                         break;
219                 case KEY_RFKILL:
220                         rfkill_schedule_toggle(RFKILL_TYPE_ALL);
221                         break;
222                 }
223         } else if (type == EV_SW && code == SW_RFKILL_ALL)
224                 rfkill_schedule_evsw_rfkillall(data);
225 }
226
227 static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
228                           const struct input_device_id *id)
229 {
230         struct input_handle *handle;
231         int error;
232
233         handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
234         if (!handle)
235                 return -ENOMEM;
236
237         handle->dev = dev;
238         handle->handler = handler;
239         handle->name = "rfkill_backport";
240
241         /* causes rfkill_start() to be called */
242         error = input_register_handle(handle);
243         if (error)
244                 goto err_free_handle;
245
246         error = input_open_device(handle);
247         if (error)
248                 goto err_unregister_handle;
249
250         return 0;
251
252  err_unregister_handle:
253         input_unregister_handle(handle);
254  err_free_handle:
255         kfree(handle);
256         return error;
257 }
258
259 static void rfkill_start(struct input_handle *handle)
260 {
261         /*
262          * Take event_lock to guard against configuration changes, we
263          * should be able to deal with concurrency with rfkill_event()
264          * just fine (which event_lock will also avoid).
265          */
266         spin_lock_irq(&handle->dev->event_lock);
267
268         if (test_bit(EV_SW, handle->dev->evbit) &&
269             test_bit(SW_RFKILL_ALL, handle->dev->swbit))
270                 rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
271                                                         handle->dev->sw));
272
273         spin_unlock_irq(&handle->dev->event_lock);
274 }
275
276 static void rfkill_disconnect(struct input_handle *handle)
277 {
278         input_close_device(handle);
279         input_unregister_handle(handle);
280         kfree(handle);
281 }
282
283 static const struct input_device_id rfkill_ids[] = {
284         {
285                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
286                 .evbit = { BIT_MASK(EV_KEY) },
287                 .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
288         },
289         {
290                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
291                 .evbit = { BIT_MASK(EV_KEY) },
292                 .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
293         },
294         {
295                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
296                 .evbit = { BIT_MASK(EV_KEY) },
297                 .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
298         },
299         {
300                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
301                 .evbit = { BIT_MASK(EV_KEY) },
302                 .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
303         },
304         {
305                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
306                 .evbit = { BIT_MASK(EV_KEY) },
307                 .keybit = { [BIT_WORD(KEY_RFKILL)] = BIT_MASK(KEY_RFKILL) },
308         },
309         {
310                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
311                 .evbit = { BIT(EV_SW) },
312                 .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
313         },
314         { }
315 };
316
317 static struct input_handler rfkill_handler = {
318         .name = "rfkill",
319         .event = rfkill_event,
320         .connect = rfkill_connect,
321         .start = rfkill_start,
322         .disconnect = rfkill_disconnect,
323         .id_table = rfkill_ids,
324 };
325
326 int __init rfkill_handler_init(void)
327 {
328         switch (rfkill_master_switch_mode) {
329         case RFKILL_INPUT_MASTER_UNBLOCKALL:
330                 rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNBLOCK;
331                 break;
332         case RFKILL_INPUT_MASTER_RESTORE:
333                 rfkill_master_switch_op = RFKILL_GLOBAL_OP_RESTORE;
334                 break;
335         case RFKILL_INPUT_MASTER_UNLOCK:
336                 rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNLOCK;
337                 break;
338         default:
339                 return -EINVAL;
340         }
341
342         spin_lock_init(&rfkill_op_lock);
343
344         /* Avoid delay at first schedule */
345         rfkill_last_scheduled =
346                         jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
347         return input_register_handler(&rfkill_handler);
348 }
349
350 void __exit rfkill_handler_exit(void)
351 {
352         input_unregister_handler(&rfkill_handler);
353         cancel_delayed_work_sync(&rfkill_op_work);
354 }