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 #include "rfkill-input.h"
22 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
23 MODULE_DESCRIPTION("Input layer to RF switch connector");
24 MODULE_LICENSE("GPL");
27 struct work_struct work
;
28 enum rfkill_type type
;
29 struct mutex mutex
; /* ensures that task is serialized */
30 spinlock_t lock
; /* for accessing last and desired state */
31 unsigned long last
; /* last schedule */
32 enum rfkill_state desired_state
; /* on/off */
33 enum rfkill_state current_state
; /* on/off */
36 static void rfkill_task_handler(struct work_struct
*work
)
38 struct rfkill_task
*task
= container_of(work
, struct rfkill_task
, work
);
39 enum rfkill_state state
;
41 mutex_lock(&task
->mutex
);
44 * Use temp variable to fetch desired state to keep it
45 * consistent even if rfkill_schedule_toggle() runs in
46 * another thread or interrupts us.
48 state
= task
->desired_state
;
50 if (state
!= task
->current_state
) {
51 rfkill_switch_all(task
->type
, state
);
52 task
->current_state
= state
;
55 mutex_unlock(&task
->mutex
);
58 static void rfkill_schedule_set(struct rfkill_task
*task
,
59 enum rfkill_state desired_state
)
63 spin_lock_irqsave(&task
->lock
, flags
);
65 if (time_after(jiffies
, task
->last
+ msecs_to_jiffies(200))) {
66 task
->desired_state
= desired_state
;
68 schedule_work(&task
->work
);
71 spin_unlock_irqrestore(&task
->lock
, flags
);
74 static void rfkill_schedule_toggle(struct rfkill_task
*task
)
78 spin_lock_irqsave(&task
->lock
, flags
);
80 if (time_after(jiffies
, task
->last
+ msecs_to_jiffies(200))) {
81 task
->desired_state
= !task
->desired_state
;
83 schedule_work(&task
->work
);
86 spin_unlock_irqrestore(&task
->lock
, flags
);
89 #define DEFINE_RFKILL_TASK(n, t) \
90 struct rfkill_task n = { \
91 .work = __WORK_INITIALIZER(n.work, \
92 rfkill_task_handler), \
94 .mutex = __MUTEX_INITIALIZER(n.mutex), \
95 .lock = __SPIN_LOCK_UNLOCKED(n.lock), \
96 .desired_state = RFKILL_STATE_ON, \
97 .current_state = RFKILL_STATE_ON, \
100 static DEFINE_RFKILL_TASK(rfkill_wlan
, RFKILL_TYPE_WLAN
);
101 static DEFINE_RFKILL_TASK(rfkill_bt
, RFKILL_TYPE_BLUETOOTH
);
102 static DEFINE_RFKILL_TASK(rfkill_uwb
, RFKILL_TYPE_UWB
);
103 static DEFINE_RFKILL_TASK(rfkill_wimax
, RFKILL_TYPE_WIMAX
);
105 static void rfkill_event(struct input_handle
*handle
, unsigned int type
,
106 unsigned int code
, int data
)
108 if (type
== EV_KEY
&& data
== 1) {
111 rfkill_schedule_toggle(&rfkill_wlan
);
114 rfkill_schedule_toggle(&rfkill_bt
);
117 rfkill_schedule_toggle(&rfkill_uwb
);
120 rfkill_schedule_toggle(&rfkill_wimax
);
125 } else if (type
== EV_SW
) {
128 /* EVERY radio type. data != 0 means radios ON */
129 rfkill_schedule_set(&rfkill_wimax
,
130 (data
)? RFKILL_STATE_ON
:
132 rfkill_schedule_set(&rfkill_uwb
,
133 (data
)? RFKILL_STATE_ON
:
135 rfkill_schedule_set(&rfkill_bt
,
136 (data
)? RFKILL_STATE_ON
:
138 rfkill_schedule_set(&rfkill_wlan
,
139 (data
)? RFKILL_STATE_ON
:
148 static int rfkill_connect(struct input_handler
*handler
, struct input_dev
*dev
,
149 const struct input_device_id
*id
)
151 struct input_handle
*handle
;
154 handle
= kzalloc(sizeof(struct input_handle
), GFP_KERNEL
);
159 handle
->handler
= handler
;
160 handle
->name
= "rfkill";
162 error
= input_register_handle(handle
);
164 goto err_free_handle
;
166 error
= input_open_device(handle
);
168 goto err_unregister_handle
;
172 err_unregister_handle
:
173 input_unregister_handle(handle
);
179 static void rfkill_disconnect(struct input_handle
*handle
)
181 input_close_device(handle
);
182 input_unregister_handle(handle
);
186 static const struct input_device_id rfkill_ids
[] = {
188 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
,
189 .evbit
= { BIT_MASK(EV_KEY
) },
190 .keybit
= { [BIT_WORD(KEY_WLAN
)] = BIT_MASK(KEY_WLAN
) },
193 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
,
194 .evbit
= { BIT_MASK(EV_KEY
) },
195 .keybit
= { [BIT_WORD(KEY_BLUETOOTH
)] = BIT_MASK(KEY_BLUETOOTH
) },
198 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
,
199 .evbit
= { BIT_MASK(EV_KEY
) },
200 .keybit
= { [BIT_WORD(KEY_UWB
)] = BIT_MASK(KEY_UWB
) },
203 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
,
204 .evbit
= { BIT_MASK(EV_KEY
) },
205 .keybit
= { [BIT_WORD(KEY_WIMAX
)] = BIT_MASK(KEY_WIMAX
) },
208 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_SWBIT
,
209 .evbit
= { BIT(EV_SW
) },
210 .swbit
= { [BIT_WORD(SW_RFKILL_ALL
)] = BIT_MASK(SW_RFKILL_ALL
) },
215 static struct input_handler rfkill_handler
= {
216 .event
= rfkill_event
,
217 .connect
= rfkill_connect
,
218 .disconnect
= rfkill_disconnect
,
220 .id_table
= rfkill_ids
,
223 static int __init
rfkill_handler_init(void)
225 return input_register_handler(&rfkill_handler
);
228 static void __exit
rfkill_handler_exit(void)
230 input_unregister_handler(&rfkill_handler
);
231 flush_scheduled_work();
234 module_init(rfkill_handler_init
);
235 module_exit(rfkill_handler_exit
);