Commit | Line | Data |
---|---|---|
cf4328cd ID |
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> | |
56f26f7b | 19 | #include <linux/sched.h> |
cf4328cd | 20 | |
fe242cfd ID |
21 | #include "rfkill-input.h" |
22 | ||
cf4328cd ID |
23 | MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>"); |
24 | MODULE_DESCRIPTION("Input layer to RF switch connector"); | |
25 | MODULE_LICENSE("GPL"); | |
26 | ||
d003922d HMH |
27 | enum rfkill_input_master_mode { |
28 | RFKILL_INPUT_MASTER_DONOTHING = 0, | |
29 | RFKILL_INPUT_MASTER_RESTORE = 1, | |
30 | RFKILL_INPUT_MASTER_UNBLOCKALL = 2, | |
31 | RFKILL_INPUT_MASTER_MAX, /* marker */ | |
32 | }; | |
33 | ||
78236571 HMH |
34 | /* Delay (in ms) between consecutive switch ops */ |
35 | #define RFKILL_OPS_DELAY 200 | |
36 | ||
d003922d HMH |
37 | static enum rfkill_input_master_mode rfkill_master_switch_mode = |
38 | RFKILL_INPUT_MASTER_UNBLOCKALL; | |
39 | module_param_named(master_switch_mode, rfkill_master_switch_mode, uint, 0); | |
40 | MODULE_PARM_DESC(master_switch_mode, | |
41 | "SW_RFKILL_ALL ON should: 0=do nothing; 1=restore; 2=unblock all"); | |
42 | ||
43 | enum rfkill_global_sched_op { | |
44 | RFKILL_GLOBAL_OP_EPO = 0, | |
45 | RFKILL_GLOBAL_OP_RESTORE, | |
46 | RFKILL_GLOBAL_OP_UNLOCK, | |
47 | RFKILL_GLOBAL_OP_UNBLOCK, | |
48 | }; | |
49 | ||
50 | /* | |
51 | * Currently, the code marked with RFKILL_NEED_SWSET is inactive. | |
52 | * If handling of EV_SW SW_WLAN/WWAN/BLUETOOTH/etc is needed in the | |
53 | * future, when such events are added, that code will be necessary. | |
54 | */ | |
55 | ||
cf4328cd | 56 | struct rfkill_task { |
78236571 | 57 | struct delayed_work dwork; |
d003922d HMH |
58 | |
59 | /* ensures that task is serialized */ | |
60 | struct mutex mutex; | |
61 | ||
62 | /* protects everything below */ | |
63 | spinlock_t lock; | |
64 | ||
65 | /* pending regular switch operations (1=pending) */ | |
66 | unsigned long sw_pending[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; | |
67 | ||
68 | #ifdef RFKILL_NEED_SWSET | |
69 | /* set operation pending (1=pending) */ | |
70 | unsigned long sw_setpending[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; | |
71 | ||
72 | /* desired state for pending set operation (1=unblock) */ | |
73 | unsigned long sw_newstate[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; | |
74 | #endif | |
75 | ||
76 | /* should the state be complemented (1=yes) */ | |
77 | unsigned long sw_togglestate[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; | |
78 | ||
79 | bool global_op_pending; | |
80 | enum rfkill_global_sched_op op; | |
78236571 HMH |
81 | |
82 | /* last time it was scheduled */ | |
83 | unsigned long last_scheduled; | |
cf4328cd ID |
84 | }; |
85 | ||
d003922d | 86 | static void __rfkill_handle_global_op(enum rfkill_global_sched_op op) |
cf4328cd | 87 | { |
d003922d HMH |
88 | unsigned int i; |
89 | ||
90 | switch (op) { | |
91 | case RFKILL_GLOBAL_OP_EPO: | |
92 | rfkill_epo(); | |
93 | break; | |
94 | case RFKILL_GLOBAL_OP_RESTORE: | |
95 | rfkill_restore_states(); | |
96 | break; | |
97 | case RFKILL_GLOBAL_OP_UNLOCK: | |
98 | rfkill_remove_epo_lock(); | |
99 | break; | |
100 | case RFKILL_GLOBAL_OP_UNBLOCK: | |
101 | rfkill_remove_epo_lock(); | |
102 | for (i = 0; i < RFKILL_TYPE_MAX; i++) | |
103 | rfkill_switch_all(i, RFKILL_STATE_UNBLOCKED); | |
104 | break; | |
105 | default: | |
106 | /* memory corruption or bug, fail safely */ | |
107 | rfkill_epo(); | |
108 | WARN(1, "Unknown requested operation %d! " | |
109 | "rfkill Emergency Power Off activated\n", | |
110 | op); | |
111 | } | |
112 | } | |
cf4328cd | 113 | |
d003922d HMH |
114 | #ifdef RFKILL_NEED_SWSET |
115 | static void __rfkill_handle_normal_op(const enum rfkill_type type, | |
116 | const bool sp, const bool s, const bool c) | |
117 | { | |
118 | enum rfkill_state state; | |
cf4328cd | 119 | |
d003922d HMH |
120 | if (sp) |
121 | state = (s) ? RFKILL_STATE_UNBLOCKED : | |
122 | RFKILL_STATE_SOFT_BLOCKED; | |
123 | else | |
124 | state = rfkill_get_global_state(type); | |
cf4328cd | 125 | |
d003922d HMH |
126 | if (c) |
127 | state = rfkill_state_complement(state); | |
128 | ||
129 | rfkill_switch_all(type, state); | |
cf4328cd | 130 | } |
d003922d HMH |
131 | #else |
132 | static void __rfkill_handle_normal_op(const enum rfkill_type type, | |
133 | const bool c) | |
134 | { | |
135 | enum rfkill_state state; | |
136 | ||
137 | state = rfkill_get_global_state(type); | |
138 | if (c) | |
139 | state = rfkill_state_complement(state); | |
140 | ||
141 | rfkill_switch_all(type, state); | |
142 | } | |
143 | #endif | |
cf4328cd | 144 | |
d003922d | 145 | static void rfkill_task_handler(struct work_struct *work) |
4081f00d | 146 | { |
78236571 HMH |
147 | struct rfkill_task *task = container_of(work, |
148 | struct rfkill_task, dwork.work); | |
d003922d HMH |
149 | bool doit = true; |
150 | ||
151 | mutex_lock(&task->mutex); | |
152 | ||
153 | spin_lock_irq(&task->lock); | |
154 | while (doit) { | |
155 | if (task->global_op_pending) { | |
156 | enum rfkill_global_sched_op op = task->op; | |
157 | task->global_op_pending = false; | |
158 | memset(task->sw_pending, 0, sizeof(task->sw_pending)); | |
159 | spin_unlock_irq(&task->lock); | |
160 | ||
161 | __rfkill_handle_global_op(op); | |
162 | ||
163 | /* make sure we do at least one pass with | |
164 | * !task->global_op_pending */ | |
165 | spin_lock_irq(&task->lock); | |
166 | continue; | |
167 | } else if (!rfkill_is_epo_lock_active()) { | |
168 | unsigned int i = 0; | |
169 | ||
170 | while (!task->global_op_pending && | |
171 | i < RFKILL_TYPE_MAX) { | |
172 | if (test_and_clear_bit(i, task->sw_pending)) { | |
173 | bool c; | |
174 | #ifdef RFKILL_NEED_SWSET | |
175 | bool sp, s; | |
176 | sp = test_and_clear_bit(i, | |
177 | task->sw_setpending); | |
178 | s = test_bit(i, task->sw_newstate); | |
179 | #endif | |
180 | c = test_and_clear_bit(i, | |
181 | task->sw_togglestate); | |
182 | spin_unlock_irq(&task->lock); | |
183 | ||
184 | #ifdef RFKILL_NEED_SWSET | |
185 | __rfkill_handle_normal_op(i, sp, s, c); | |
186 | #else | |
187 | __rfkill_handle_normal_op(i, c); | |
188 | #endif | |
189 | ||
190 | spin_lock_irq(&task->lock); | |
191 | } | |
192 | i++; | |
193 | } | |
194 | } | |
195 | doit = task->global_op_pending; | |
196 | } | |
197 | spin_unlock_irq(&task->lock); | |
198 | ||
199 | mutex_unlock(&task->mutex); | |
4081f00d HMH |
200 | } |
201 | ||
d003922d | 202 | static struct rfkill_task rfkill_task = { |
78236571 | 203 | .dwork = __DELAYED_WORK_INITIALIZER(rfkill_task.dwork, |
d003922d HMH |
204 | rfkill_task_handler), |
205 | .mutex = __MUTEX_INITIALIZER(rfkill_task.mutex), | |
206 | .lock = __SPIN_LOCK_UNLOCKED(rfkill_task.lock), | |
207 | }; | |
4081f00d | 208 | |
78236571 HMH |
209 | static unsigned long rfkill_ratelimit(const unsigned long last) |
210 | { | |
211 | const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY); | |
212 | return (time_after(jiffies, last + delay)) ? 0 : delay; | |
213 | } | |
214 | ||
215 | static void rfkill_schedule_ratelimited(void) | |
216 | { | |
217 | if (!delayed_work_pending(&rfkill_task.dwork)) { | |
218 | schedule_delayed_work(&rfkill_task.dwork, | |
219 | rfkill_ratelimit(rfkill_task.last_scheduled)); | |
220 | rfkill_task.last_scheduled = jiffies; | |
221 | } | |
222 | } | |
223 | ||
d003922d | 224 | static void rfkill_schedule_global_op(enum rfkill_global_sched_op op) |
4081f00d | 225 | { |
d003922d HMH |
226 | unsigned long flags; |
227 | ||
228 | spin_lock_irqsave(&rfkill_task.lock, flags); | |
229 | rfkill_task.op = op; | |
230 | rfkill_task.global_op_pending = true; | |
78236571 HMH |
231 | if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) { |
232 | /* bypass the limiter for EPO */ | |
233 | cancel_delayed_work(&rfkill_task.dwork); | |
234 | schedule_delayed_work(&rfkill_task.dwork, 0); | |
235 | rfkill_task.last_scheduled = jiffies; | |
236 | } else | |
237 | rfkill_schedule_ratelimited(); | |
d003922d | 238 | spin_unlock_irqrestore(&rfkill_task.lock, flags); |
4081f00d HMH |
239 | } |
240 | ||
d003922d HMH |
241 | #ifdef RFKILL_NEED_SWSET |
242 | /* Use this if you need to add EV_SW SW_WLAN/WWAN/BLUETOOTH/etc handling */ | |
243 | ||
244 | static void rfkill_schedule_set(enum rfkill_type type, | |
28f089c1 HMH |
245 | enum rfkill_state desired_state) |
246 | { | |
247 | unsigned long flags; | |
248 | ||
d003922d | 249 | if (rfkill_is_epo_lock_active()) |
4081f00d HMH |
250 | return; |
251 | ||
d003922d HMH |
252 | spin_lock_irqsave(&rfkill_task.lock, flags); |
253 | if (!rfkill_task.global_op_pending) { | |
254 | set_bit(type, rfkill_task.sw_pending); | |
255 | set_bit(type, rfkill_task.sw_setpending); | |
256 | clear_bit(type, rfkill_task.sw_togglestate); | |
257 | if (desired_state) | |
258 | set_bit(type, rfkill_task.sw_newstate); | |
259 | else | |
260 | clear_bit(type, rfkill_task.sw_newstate); | |
78236571 | 261 | rfkill_schedule_ratelimited(); |
28f089c1 | 262 | } |
d003922d | 263 | spin_unlock_irqrestore(&rfkill_task.lock, flags); |
28f089c1 | 264 | } |
d003922d | 265 | #endif |
28f089c1 | 266 | |
d003922d | 267 | static void rfkill_schedule_toggle(enum rfkill_type type) |
cf4328cd | 268 | { |
e6c9116d | 269 | unsigned long flags; |
cf4328cd | 270 | |
d003922d | 271 | if (rfkill_is_epo_lock_active()) |
4081f00d HMH |
272 | return; |
273 | ||
d003922d HMH |
274 | spin_lock_irqsave(&rfkill_task.lock, flags); |
275 | if (!rfkill_task.global_op_pending) { | |
276 | set_bit(type, rfkill_task.sw_pending); | |
277 | change_bit(type, rfkill_task.sw_togglestate); | |
78236571 | 278 | rfkill_schedule_ratelimited(); |
cf4328cd | 279 | } |
d003922d | 280 | spin_unlock_irqrestore(&rfkill_task.lock, flags); |
cf4328cd ID |
281 | } |
282 | ||
6e28fbef HMH |
283 | static void rfkill_schedule_evsw_rfkillall(int state) |
284 | { | |
6e28fbef | 285 | if (state) { |
d003922d HMH |
286 | switch (rfkill_master_switch_mode) { |
287 | case RFKILL_INPUT_MASTER_UNBLOCKALL: | |
288 | rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNBLOCK); | |
289 | break; | |
290 | case RFKILL_INPUT_MASTER_RESTORE: | |
291 | rfkill_schedule_global_op(RFKILL_GLOBAL_OP_RESTORE); | |
292 | break; | |
293 | case RFKILL_INPUT_MASTER_DONOTHING: | |
294 | rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNLOCK); | |
295 | break; | |
296 | default: | |
297 | /* memory corruption or driver bug! fail safely */ | |
298 | rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO); | |
299 | WARN(1, "Unknown rfkill_master_switch_mode (%d), " | |
300 | "driver bug or memory corruption detected!\n", | |
301 | rfkill_master_switch_mode); | |
302 | break; | |
303 | } | |
6e28fbef | 304 | } else |
d003922d | 305 | rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO); |
6e28fbef HMH |
306 | } |
307 | ||
cf4328cd | 308 | static void rfkill_event(struct input_handle *handle, unsigned int type, |
28f089c1 | 309 | unsigned int code, int data) |
cf4328cd | 310 | { |
28f089c1 | 311 | if (type == EV_KEY && data == 1) { |
d003922d HMH |
312 | enum rfkill_type t; |
313 | ||
cf4328cd ID |
314 | switch (code) { |
315 | case KEY_WLAN: | |
d003922d | 316 | t = RFKILL_TYPE_WLAN; |
cf4328cd ID |
317 | break; |
318 | case KEY_BLUETOOTH: | |
d003922d | 319 | t = RFKILL_TYPE_BLUETOOTH; |
cf4328cd | 320 | break; |
e0665486 | 321 | case KEY_UWB: |
d003922d | 322 | t = RFKILL_TYPE_UWB; |
e0665486 | 323 | break; |
303d9bf6 | 324 | case KEY_WIMAX: |
d003922d | 325 | t = RFKILL_TYPE_WIMAX; |
303d9bf6 | 326 | break; |
cf4328cd | 327 | default: |
d003922d | 328 | return; |
cf4328cd | 329 | } |
d003922d HMH |
330 | rfkill_schedule_toggle(t); |
331 | return; | |
28f089c1 HMH |
332 | } else if (type == EV_SW) { |
333 | switch (code) { | |
334 | case SW_RFKILL_ALL: | |
6e28fbef | 335 | rfkill_schedule_evsw_rfkillall(data); |
d003922d | 336 | return; |
28f089c1 | 337 | default: |
d003922d | 338 | return; |
28f089c1 | 339 | } |
cf4328cd ID |
340 | } |
341 | } | |
342 | ||
343 | static int rfkill_connect(struct input_handler *handler, struct input_dev *dev, | |
344 | const struct input_device_id *id) | |
345 | { | |
346 | struct input_handle *handle; | |
347 | int error; | |
348 | ||
349 | handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); | |
350 | if (!handle) | |
351 | return -ENOMEM; | |
352 | ||
353 | handle->dev = dev; | |
354 | handle->handler = handler; | |
355 | handle->name = "rfkill"; | |
356 | ||
6e28fbef | 357 | /* causes rfkill_start() to be called */ |
cf4328cd ID |
358 | error = input_register_handle(handle); |
359 | if (error) | |
360 | goto err_free_handle; | |
361 | ||
362 | error = input_open_device(handle); | |
363 | if (error) | |
364 | goto err_unregister_handle; | |
365 | ||
366 | return 0; | |
367 | ||
368 | err_unregister_handle: | |
369 | input_unregister_handle(handle); | |
370 | err_free_handle: | |
371 | kfree(handle); | |
372 | return error; | |
373 | } | |
374 | ||
6e28fbef HMH |
375 | static void rfkill_start(struct input_handle *handle) |
376 | { | |
377 | /* Take event_lock to guard against configuration changes, we | |
378 | * should be able to deal with concurrency with rfkill_event() | |
379 | * just fine (which event_lock will also avoid). */ | |
380 | spin_lock_irq(&handle->dev->event_lock); | |
381 | ||
382 | if (test_bit(EV_SW, handle->dev->evbit)) { | |
383 | if (test_bit(SW_RFKILL_ALL, handle->dev->swbit)) | |
384 | rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL, | |
385 | handle->dev->sw)); | |
386 | /* add resync for further EV_SW events here */ | |
387 | } | |
388 | ||
389 | spin_unlock_irq(&handle->dev->event_lock); | |
390 | } | |
391 | ||
cf4328cd ID |
392 | static void rfkill_disconnect(struct input_handle *handle) |
393 | { | |
394 | input_close_device(handle); | |
395 | input_unregister_handle(handle); | |
396 | kfree(handle); | |
397 | } | |
398 | ||
399 | static const struct input_device_id rfkill_ids[] = { | |
400 | { | |
401 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, | |
7b19ada2 JS |
402 | .evbit = { BIT_MASK(EV_KEY) }, |
403 | .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) }, | |
cf4328cd ID |
404 | }, |
405 | { | |
406 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, | |
7b19ada2 JS |
407 | .evbit = { BIT_MASK(EV_KEY) }, |
408 | .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) }, | |
cf4328cd | 409 | }, |
e0665486 ID |
410 | { |
411 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, | |
7b19ada2 JS |
412 | .evbit = { BIT_MASK(EV_KEY) }, |
413 | .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) }, | |
e0665486 | 414 | }, |
303d9bf6 IPG |
415 | { |
416 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, | |
417 | .evbit = { BIT_MASK(EV_KEY) }, | |
418 | .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) }, | |
419 | }, | |
28f089c1 HMH |
420 | { |
421 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT, | |
422 | .evbit = { BIT(EV_SW) }, | |
423 | .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) }, | |
424 | }, | |
cf4328cd ID |
425 | { } |
426 | }; | |
427 | ||
428 | static struct input_handler rfkill_handler = { | |
429 | .event = rfkill_event, | |
430 | .connect = rfkill_connect, | |
431 | .disconnect = rfkill_disconnect, | |
6e28fbef | 432 | .start = rfkill_start, |
cf4328cd ID |
433 | .name = "rfkill", |
434 | .id_table = rfkill_ids, | |
435 | }; | |
436 | ||
437 | static int __init rfkill_handler_init(void) | |
438 | { | |
d003922d HMH |
439 | if (rfkill_master_switch_mode >= RFKILL_INPUT_MASTER_MAX) |
440 | return -EINVAL; | |
441 | ||
78236571 HMH |
442 | /* |
443 | * The penalty to not doing this is a possible RFKILL_OPS_DELAY delay | |
444 | * at the first use. Acceptable, but if we can avoid it, why not? | |
445 | */ | |
446 | rfkill_task.last_scheduled = | |
447 | jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1; | |
cf4328cd ID |
448 | return input_register_handler(&rfkill_handler); |
449 | } | |
450 | ||
451 | static void __exit rfkill_handler_exit(void) | |
452 | { | |
453 | input_unregister_handler(&rfkill_handler); | |
78236571 | 454 | cancel_delayed_work_sync(&rfkill_task.dwork); |
d003922d | 455 | rfkill_remove_epo_lock(); |
cf4328cd ID |
456 | } |
457 | ||
458 | module_init(rfkill_handler_init); | |
459 | module_exit(rfkill_handler_exit); |