2 * Input layer to RF Kill interface connector
4 * Copyright (c) 2007 Dmitry Torokhov
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
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.
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".
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>
23 #include <linux/rfkill_backport.h>
25 #include <linux/sched.h>
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
36 /* Delay (in ms) between consecutive switch ops */
37 #define RFKILL_OPS_DELAY 200
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");
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)];
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,
57 static enum rfkill_sched_op rfkill_master_switch_op;
58 static enum rfkill_sched_op rfkill_op;
60 static void __rfkill_handle_global_op(enum rfkill_sched_op op)
65 case RFKILL_GLOBAL_OP_EPO:
68 case RFKILL_GLOBAL_OP_RESTORE:
69 rfkill_restore_states();
71 case RFKILL_GLOBAL_OP_UNLOCK:
72 rfkill_remove_epo_lock();
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);
80 /* memory corruption or bug, fail safely */
82 WARN(1, "Unknown requested operation %d! "
83 "rfkill Emergency Power Off activated\n",
88 static void __rfkill_handle_normal_op(const enum rfkill_type type,
89 const bool complement)
93 blocked = rfkill_get_global_sw_state(type);
97 rfkill_switch_all(type, blocked);
100 static void rfkill_op_handler(struct work_struct *work)
105 spin_lock_irq(&rfkill_op_lock);
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);
114 __rfkill_handle_global_op(op);
116 spin_lock_irq(&rfkill_op_lock);
119 * handle global ops first -- during unlocked period
120 * we might have gotten a new global op.
122 if (rfkill_op_pending)
126 if (rfkill_is_epo_lock_active())
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);
134 __rfkill_handle_normal_op(i, c);
136 spin_lock_irq(&rfkill_op_lock);
139 } while (rfkill_op_pending);
140 spin_unlock_irq(&rfkill_op_lock);
143 static DECLARE_DELAYED_WORK(rfkill_op_work, rfkill_op_handler);
144 static unsigned long rfkill_last_scheduled;
146 static unsigned long rfkill_ratelimit(const unsigned long last)
148 const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY);
149 return (time_after(jiffies, last + delay)) ? 0 : delay;
152 static void rfkill_schedule_ratelimited(void)
154 if (delayed_work_pending(&rfkill_op_work))
156 schedule_delayed_work(&rfkill_op_work,
157 rfkill_ratelimit(rfkill_last_scheduled));
158 rfkill_last_scheduled = jiffies;
161 static void rfkill_schedule_global_op(enum rfkill_sched_op op)
165 spin_lock_irqsave(&rfkill_op_lock, flags);
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;
174 rfkill_schedule_ratelimited();
175 spin_unlock_irqrestore(&rfkill_op_lock, flags);
178 static void rfkill_schedule_toggle(enum rfkill_type type)
182 if (rfkill_is_epo_lock_active())
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();
191 spin_unlock_irqrestore(&rfkill_op_lock, flags);
194 static void rfkill_schedule_evsw_rfkillall(int state)
197 rfkill_schedule_global_op(rfkill_master_switch_op);
199 rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
202 static void rfkill_event(struct input_handle *handle, unsigned int type,
203 unsigned int code, int data)
205 if (type == EV_KEY && data == 1) {
208 rfkill_schedule_toggle(RFKILL_TYPE_WLAN);
211 rfkill_schedule_toggle(RFKILL_TYPE_BLUETOOTH);
214 rfkill_schedule_toggle(RFKILL_TYPE_UWB);
217 rfkill_schedule_toggle(RFKILL_TYPE_WIMAX);
220 rfkill_schedule_toggle(RFKILL_TYPE_ALL);
223 } else if (type == EV_SW && code == SW_RFKILL_ALL)
224 rfkill_schedule_evsw_rfkillall(data);
227 static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
228 const struct input_device_id *id)
230 struct input_handle *handle;
233 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
238 handle->handler = handler;
239 handle->name = "rfkill_backport";
241 /* causes rfkill_start() to be called */
242 error = input_register_handle(handle);
244 goto err_free_handle;
246 error = input_open_device(handle);
248 goto err_unregister_handle;
252 err_unregister_handle:
253 input_unregister_handle(handle);
259 static void rfkill_start(struct input_handle *handle)
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).
266 spin_lock_irq(&handle->dev->event_lock);
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,
273 spin_unlock_irq(&handle->dev->event_lock);
276 static void rfkill_disconnect(struct input_handle *handle)
278 input_close_device(handle);
279 input_unregister_handle(handle);
283 static const struct input_device_id rfkill_ids[] = {
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) },
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) },
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) },
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) },
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) },
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) },
317 static struct input_handler rfkill_handler = {
319 .event = rfkill_event,
320 .connect = rfkill_connect,
321 .start = rfkill_start,
322 .disconnect = rfkill_disconnect,
323 .id_table = rfkill_ids,
326 int __init rfkill_handler_init(void)
328 switch (rfkill_master_switch_mode) {
329 case RFKILL_INPUT_MASTER_UNBLOCKALL:
330 rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNBLOCK;
332 case RFKILL_INPUT_MASTER_RESTORE:
333 rfkill_master_switch_op = RFKILL_GLOBAL_OP_RESTORE;
335 case RFKILL_INPUT_MASTER_UNLOCK:
336 rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNLOCK;
342 spin_lock_init(&rfkill_op_lock);
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);
350 void __exit rfkill_handler_exit(void)
352 input_unregister_handler(&rfkill_handler);
353 cancel_delayed_work_sync(&rfkill_op_work);