blob: 60a34f3b5f656978337faaa193301be9648a6653 [file] [log] [blame]
Ivo van Doorncf4328c2007-05-07 00:34:20 -07001/*
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>
Geert Uytterhoeven56f26f72008-10-13 21:59:03 +020019#include <linux/sched.h>
Ivo van Doorncf4328c2007-05-07 00:34:20 -070020
Ivo van Doornfe242cf2007-09-27 14:57:05 -070021#include "rfkill-input.h"
22
Ivo van Doorncf4328c2007-05-07 00:34:20 -070023MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
24MODULE_DESCRIPTION("Input layer to RF switch connector");
25MODULE_LICENSE("GPL");
26
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -030027enum 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
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -030034/* Delay (in ms) between consecutive switch ops */
35#define RFKILL_OPS_DELAY 200
36
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -030037static enum rfkill_input_master_mode rfkill_master_switch_mode =
38 RFKILL_INPUT_MASTER_UNBLOCKALL;
39module_param_named(master_switch_mode, rfkill_master_switch_mode, uint, 0);
40MODULE_PARM_DESC(master_switch_mode,
41 "SW_RFKILL_ALL ON should: 0=do nothing; 1=restore; 2=unblock all");
42
43enum 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
Ivo van Doorncf4328c2007-05-07 00:34:20 -070050struct rfkill_task {
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -030051 struct delayed_work dwork;
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -030052
53 /* ensures that task is serialized */
54 struct mutex mutex;
55
56 /* protects everything below */
57 spinlock_t lock;
58
59 /* pending regular switch operations (1=pending) */
60 unsigned long sw_pending[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
61
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -030062 /* should the state be complemented (1=yes) */
63 unsigned long sw_togglestate[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
64
65 bool global_op_pending;
66 enum rfkill_global_sched_op op;
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -030067
68 /* last time it was scheduled */
69 unsigned long last_scheduled;
Ivo van Doorncf4328c2007-05-07 00:34:20 -070070};
71
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -030072static void __rfkill_handle_global_op(enum rfkill_global_sched_op op)
73{
74 unsigned int i;
75
76 switch (op) {
77 case RFKILL_GLOBAL_OP_EPO:
78 rfkill_epo();
79 break;
80 case RFKILL_GLOBAL_OP_RESTORE:
81 rfkill_restore_states();
82 break;
83 case RFKILL_GLOBAL_OP_UNLOCK:
84 rfkill_remove_epo_lock();
85 break;
86 case RFKILL_GLOBAL_OP_UNBLOCK:
87 rfkill_remove_epo_lock();
88 for (i = 0; i < RFKILL_TYPE_MAX; i++)
89 rfkill_switch_all(i, RFKILL_STATE_UNBLOCKED);
90 break;
91 default:
92 /* memory corruption or bug, fail safely */
93 rfkill_epo();
94 WARN(1, "Unknown requested operation %d! "
95 "rfkill Emergency Power Off activated\n",
96 op);
97 }
98}
99
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300100static void __rfkill_handle_normal_op(const enum rfkill_type type,
101 const bool c)
102{
103 enum rfkill_state state;
104
105 state = rfkill_get_global_state(type);
106 if (c)
107 state = rfkill_state_complement(state);
108
109 rfkill_switch_all(type, state);
110}
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300111
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700112static void rfkill_task_handler(struct work_struct *work)
113{
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -0300114 struct rfkill_task *task = container_of(work,
115 struct rfkill_task, dwork.work);
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300116 bool doit = true;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700117
118 mutex_lock(&task->mutex);
119
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300120 spin_lock_irq(&task->lock);
121 while (doit) {
122 if (task->global_op_pending) {
123 enum rfkill_global_sched_op op = task->op;
124 task->global_op_pending = false;
125 memset(task->sw_pending, 0, sizeof(task->sw_pending));
126 spin_unlock_irq(&task->lock);
127
128 __rfkill_handle_global_op(op);
129
130 /* make sure we do at least one pass with
131 * !task->global_op_pending */
132 spin_lock_irq(&task->lock);
133 continue;
134 } else if (!rfkill_is_epo_lock_active()) {
135 unsigned int i = 0;
136
137 while (!task->global_op_pending &&
138 i < RFKILL_TYPE_MAX) {
139 if (test_and_clear_bit(i, task->sw_pending)) {
140 bool c;
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300141 c = test_and_clear_bit(i,
142 task->sw_togglestate);
143 spin_unlock_irq(&task->lock);
144
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300145 __rfkill_handle_normal_op(i, c);
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300146
147 spin_lock_irq(&task->lock);
148 }
149 i++;
150 }
151 }
152 doit = task->global_op_pending;
153 }
154 spin_unlock_irq(&task->lock);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700155
156 mutex_unlock(&task->mutex);
157}
158
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300159static struct rfkill_task rfkill_task = {
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -0300160 .dwork = __DELAYED_WORK_INITIALIZER(rfkill_task.dwork,
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300161 rfkill_task_handler),
162 .mutex = __MUTEX_INITIALIZER(rfkill_task.mutex),
163 .lock = __SPIN_LOCK_UNLOCKED(rfkill_task.lock),
164};
165
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -0300166static unsigned long rfkill_ratelimit(const unsigned long last)
167{
168 const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY);
169 return (time_after(jiffies, last + delay)) ? 0 : delay;
170}
171
172static void rfkill_schedule_ratelimited(void)
173{
174 if (!delayed_work_pending(&rfkill_task.dwork)) {
175 schedule_delayed_work(&rfkill_task.dwork,
176 rfkill_ratelimit(rfkill_task.last_scheduled));
177 rfkill_task.last_scheduled = jiffies;
178 }
179}
180
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300181static void rfkill_schedule_global_op(enum rfkill_global_sched_op op)
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300182{
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300183 unsigned long flags;
184
185 spin_lock_irqsave(&rfkill_task.lock, flags);
186 rfkill_task.op = op;
187 rfkill_task.global_op_pending = true;
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -0300188 if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) {
189 /* bypass the limiter for EPO */
190 cancel_delayed_work(&rfkill_task.dwork);
191 schedule_delayed_work(&rfkill_task.dwork, 0);
192 rfkill_task.last_scheduled = jiffies;
193 } else
194 rfkill_schedule_ratelimited();
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300195 spin_unlock_irqrestore(&rfkill_task.lock, flags);
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300196}
197
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300198static void rfkill_schedule_toggle(enum rfkill_type type)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700199{
Ingo Molnare6c91162007-07-14 18:50:15 -0700200 unsigned long flags;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700201
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300202 if (rfkill_is_epo_lock_active())
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300203 return;
204
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300205 spin_lock_irqsave(&rfkill_task.lock, flags);
206 if (!rfkill_task.global_op_pending) {
207 set_bit(type, rfkill_task.sw_pending);
208 change_bit(type, rfkill_task.sw_togglestate);
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -0300209 rfkill_schedule_ratelimited();
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700210 }
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300211 spin_unlock_irqrestore(&rfkill_task.lock, flags);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700212}
213
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300214static void rfkill_schedule_evsw_rfkillall(int state)
215{
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300216 if (state) {
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300217 switch (rfkill_master_switch_mode) {
218 case RFKILL_INPUT_MASTER_UNBLOCKALL:
219 rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNBLOCK);
220 break;
221 case RFKILL_INPUT_MASTER_RESTORE:
222 rfkill_schedule_global_op(RFKILL_GLOBAL_OP_RESTORE);
223 break;
224 case RFKILL_INPUT_MASTER_DONOTHING:
225 rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNLOCK);
226 break;
227 default:
228 /* memory corruption or driver bug! fail safely */
229 rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
230 WARN(1, "Unknown rfkill_master_switch_mode (%d), "
231 "driver bug or memory corruption detected!\n",
232 rfkill_master_switch_mode);
233 break;
234 }
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300235 } else
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300236 rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300237}
238
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700239static void rfkill_event(struct input_handle *handle, unsigned int type,
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300240 unsigned int code, int data)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700241{
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300242 if (type == EV_KEY && data == 1) {
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300243 enum rfkill_type t;
244
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700245 switch (code) {
246 case KEY_WLAN:
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300247 t = RFKILL_TYPE_WLAN;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700248 break;
249 case KEY_BLUETOOTH:
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300250 t = RFKILL_TYPE_BLUETOOTH;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700251 break;
Ivo van Doorne06654862007-09-13 09:21:31 +0200252 case KEY_UWB:
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300253 t = RFKILL_TYPE_UWB;
Ivo van Doorne06654862007-09-13 09:21:31 +0200254 break;
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800255 case KEY_WIMAX:
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300256 t = RFKILL_TYPE_WIMAX;
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800257 break;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700258 default:
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300259 return;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700260 }
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300261 rfkill_schedule_toggle(t);
262 return;
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300263 } else if (type == EV_SW) {
264 switch (code) {
265 case SW_RFKILL_ALL:
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300266 rfkill_schedule_evsw_rfkillall(data);
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300267 return;
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300268 default:
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300269 return;
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300270 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700271 }
272}
273
274static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
275 const struct input_device_id *id)
276{
277 struct input_handle *handle;
278 int error;
279
280 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
281 if (!handle)
282 return -ENOMEM;
283
284 handle->dev = dev;
285 handle->handler = handler;
286 handle->name = "rfkill";
287
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300288 /* causes rfkill_start() to be called */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700289 error = input_register_handle(handle);
290 if (error)
291 goto err_free_handle;
292
293 error = input_open_device(handle);
294 if (error)
295 goto err_unregister_handle;
296
297 return 0;
298
299 err_unregister_handle:
300 input_unregister_handle(handle);
301 err_free_handle:
302 kfree(handle);
303 return error;
304}
305
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300306static void rfkill_start(struct input_handle *handle)
307{
308 /* Take event_lock to guard against configuration changes, we
309 * should be able to deal with concurrency with rfkill_event()
310 * just fine (which event_lock will also avoid). */
311 spin_lock_irq(&handle->dev->event_lock);
312
313 if (test_bit(EV_SW, handle->dev->evbit)) {
314 if (test_bit(SW_RFKILL_ALL, handle->dev->swbit))
315 rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
316 handle->dev->sw));
317 /* add resync for further EV_SW events here */
318 }
319
320 spin_unlock_irq(&handle->dev->event_lock);
321}
322
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700323static void rfkill_disconnect(struct input_handle *handle)
324{
325 input_close_device(handle);
326 input_unregister_handle(handle);
327 kfree(handle);
328}
329
330static const struct input_device_id rfkill_ids[] = {
331 {
332 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700333 .evbit = { BIT_MASK(EV_KEY) },
334 .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700335 },
336 {
337 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700338 .evbit = { BIT_MASK(EV_KEY) },
339 .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700340 },
Ivo van Doorne06654862007-09-13 09:21:31 +0200341 {
342 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700343 .evbit = { BIT_MASK(EV_KEY) },
344 .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
Ivo van Doorne06654862007-09-13 09:21:31 +0200345 },
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800346 {
347 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
348 .evbit = { BIT_MASK(EV_KEY) },
349 .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
350 },
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300351 {
352 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
353 .evbit = { BIT(EV_SW) },
354 .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
355 },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700356 { }
357};
358
359static struct input_handler rfkill_handler = {
360 .event = rfkill_event,
361 .connect = rfkill_connect,
362 .disconnect = rfkill_disconnect,
Henrique de Moraes Holschuh6e28fbe2008-07-31 10:53:57 -0300363 .start = rfkill_start,
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700364 .name = "rfkill",
365 .id_table = rfkill_ids,
366};
367
368static int __init rfkill_handler_init(void)
369{
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300370 if (rfkill_master_switch_mode >= RFKILL_INPUT_MASTER_MAX)
371 return -EINVAL;
372
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -0300373 /*
374 * The penalty to not doing this is a possible RFKILL_OPS_DELAY delay
375 * at the first use. Acceptable, but if we can avoid it, why not?
376 */
377 rfkill_task.last_scheduled =
378 jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700379 return input_register_handler(&rfkill_handler);
380}
381
382static void __exit rfkill_handler_exit(void)
383{
384 input_unregister_handler(&rfkill_handler);
Henrique de Moraes Holschuh78236572008-10-09 18:15:33 -0300385 cancel_delayed_work_sync(&rfkill_task.dwork);
Henrique de Moraes Holschuhd0039222008-10-09 21:49:33 -0300386 rfkill_remove_epo_lock();
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700387}
388
389module_init(rfkill_handler_init);
390module_exit(rfkill_handler_exit);