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
);
85 static void rfkill_event(struct input_handle
*handle
, unsigned int type
,
86 unsigned int code
, int down
)
88 if (type
== EV_KEY
&& down
== 1) {
91 rfkill_schedule_toggle(&rfkill_wlan
);
94 rfkill_schedule_toggle(&rfkill_bt
);
102 static int rfkill_connect(struct input_handler
*handler
, struct input_dev
*dev
,
103 const struct input_device_id
*id
)
105 struct input_handle
*handle
;
108 handle
= kzalloc(sizeof(struct input_handle
), GFP_KERNEL
);
113 handle
->handler
= handler
;
114 handle
->name
= "rfkill";
116 error
= input_register_handle(handle
);
118 goto err_free_handle
;
120 error
= input_open_device(handle
);
122 goto err_unregister_handle
;
126 err_unregister_handle
:
127 input_unregister_handle(handle
);
133 static void rfkill_disconnect(struct input_handle
*handle
)
135 input_close_device(handle
);
136 input_unregister_handle(handle
);
140 static const struct input_device_id rfkill_ids
[] = {
142 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
,
143 .evbit
= { BIT(EV_KEY
) },
144 .keybit
= { [LONG(KEY_WLAN
)] = BIT(KEY_WLAN
) },
147 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
,
148 .evbit
= { BIT(EV_KEY
) },
149 .keybit
= { [LONG(KEY_BLUETOOTH
)] = BIT(KEY_BLUETOOTH
) },
154 static struct input_handler rfkill_handler
= {
155 .event
= rfkill_event
,
156 .connect
= rfkill_connect
,
157 .disconnect
= rfkill_disconnect
,
159 .id_table
= rfkill_ids
,
162 static int __init
rfkill_handler_init(void)
164 return input_register_handler(&rfkill_handler
);
167 static void __exit
rfkill_handler_exit(void)
169 input_unregister_handler(&rfkill_handler
);
170 flush_scheduled_work();
173 module_init(rfkill_handler_init
);
174 module_exit(rfkill_handler_exit
);