2 * Input layer to RF Kill interface connector
4 * Copyright (c) 2007 Dmitry Torokhov
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.
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>
20 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21 MODULE_DESCRIPTION("Input layer to RF switch connector");
22 MODULE_LICENSE("GPL");
25 struct work_struct work;
26 enum rfkill_type type;
27 struct mutex mutex; /* ensures that task is serialized */
28 spinlock_t lock; /* for accessing last and desired state */
29 unsigned long last; /* last schedule */
30 enum rfkill_state desired_state; /* on/off */
31 enum rfkill_state current_state; /* on/off */
34 static void rfkill_task_handler(struct work_struct *work)
36 struct rfkill_task *task = container_of(work, struct rfkill_task, work);
37 enum rfkill_state state;
39 mutex_lock(&task->mutex);
42 * Use temp variable to fetch desired state to keep it
43 * consistent even if rfkill_schedule_toggle() runs in
44 * another thread or interrupts us.
46 state = task->desired_state;
48 if (state != task->current_state) {
49 rfkill_switch_all(task->type, state);
50 task->current_state = state;
53 mutex_unlock(&task->mutex);
56 static void rfkill_schedule_toggle(struct rfkill_task *task)
60 spin_lock_irqsave(&task->lock, flags);
62 if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
63 task->desired_state = !task->desired_state;
65 schedule_work(&task->work);
68 spin_unlock_irqrestore(&task->lock, flags);
71 #define DEFINE_RFKILL_TASK(n, t) \
72 struct rfkill_task n = { \
73 .work = __WORK_INITIALIZER(n.work, \
74 rfkill_task_handler), \
76 .mutex = __MUTEX_INITIALIZER(n.mutex), \
77 .lock = __SPIN_LOCK_UNLOCKED(n.lock), \
78 .desired_state = RFKILL_STATE_ON, \
79 .current_state = RFKILL_STATE_ON, \
82 static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
83 static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
84 static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
86 static void rfkill_event(struct input_handle *handle, unsigned int type,
87 unsigned int code, int down)
89 if (type == EV_KEY && down == 1) {
92 rfkill_schedule_toggle(&rfkill_wlan);
95 rfkill_schedule_toggle(&rfkill_bt);
98 rfkill_schedule_toggle(&rfkill_uwb);
106 static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
107 const struct input_device_id *id)
109 struct input_handle *handle;
112 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
117 handle->handler = handler;
118 handle->name = "rfkill";
120 error = input_register_handle(handle);
122 goto err_free_handle;
124 error = input_open_device(handle);
126 goto err_unregister_handle;
130 err_unregister_handle:
131 input_unregister_handle(handle);
137 static void rfkill_disconnect(struct input_handle *handle)
139 input_close_device(handle);
140 input_unregister_handle(handle);
144 static const struct input_device_id rfkill_ids[] = {
146 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
147 .evbit = { BIT(EV_KEY) },
148 .keybit = { [LONG(KEY_WLAN)] = BIT(KEY_WLAN) },
151 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
152 .evbit = { BIT(EV_KEY) },
153 .keybit = { [LONG(KEY_BLUETOOTH)] = BIT(KEY_BLUETOOTH) },
156 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
157 .evbit = { BIT(EV_KEY) },
158 .keybit = { [LONG(KEY_UWB)] = BIT(KEY_UWB) },
163 static struct input_handler rfkill_handler = {
164 .event = rfkill_event,
165 .connect = rfkill_connect,
166 .disconnect = rfkill_disconnect,
168 .id_table = rfkill_ids,
171 static int __init rfkill_handler_init(void)
173 return input_register_handler(&rfkill_handler);
176 static void __exit rfkill_handler_exit(void)
178 input_unregister_handler(&rfkill_handler);
179 flush_scheduled_work();
182 module_init(rfkill_handler_init);
183 module_exit(rfkill_handler_exit);