blob: 8e4516a5fe3237a4e93fa8946841341c71309f48 [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>
19
20MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21MODULE_DESCRIPTION("Input layer to RF switch connector");
22MODULE_LICENSE("GPL");
23
24struct rfkill_task {
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 */
32};
33
34static void rfkill_task_handler(struct work_struct *work)
35{
36 struct rfkill_task *task = container_of(work, struct rfkill_task, work);
37 enum rfkill_state state;
38
39 mutex_lock(&task->mutex);
40
41 /*
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.
45 */
46 state = task->desired_state;
47
48 if (state != task->current_state) {
49 rfkill_switch_all(task->type, state);
50 task->current_state = state;
51 }
52
53 mutex_unlock(&task->mutex);
54}
55
56static void rfkill_schedule_toggle(struct rfkill_task *task)
57{
Ingo Molnare6c91162007-07-14 18:50:15 -070058 unsigned long flags;
Ivo van Doorncf4328c2007-05-07 00:34:20 -070059
60 spin_lock_irqsave(&task->lock, flags);
61
62 if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
63 task->desired_state = !task->desired_state;
64 task->last = jiffies;
65 schedule_work(&task->work);
66 }
67
68 spin_unlock_irqrestore(&task->lock, flags);
69}
70
71#define DEFINE_RFKILL_TASK(n, t) \
72 struct rfkill_task n = { \
73 .work = __WORK_INITIALIZER(n.work, \
74 rfkill_task_handler), \
75 .type = t, \
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, \
80 }
81
82static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
83static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
Ivo van Doorne0665482007-09-13 09:21:31 +020084static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
Ivo van Doorncf4328c2007-05-07 00:34:20 -070085
86static void rfkill_event(struct input_handle *handle, unsigned int type,
YOSHIFUJI Hideaki2b81bff2007-07-19 10:44:38 +090087 unsigned int code, int down)
Ivo van Doorncf4328c2007-05-07 00:34:20 -070088{
89 if (type == EV_KEY && down == 1) {
90 switch (code) {
91 case KEY_WLAN:
92 rfkill_schedule_toggle(&rfkill_wlan);
93 break;
94 case KEY_BLUETOOTH:
95 rfkill_schedule_toggle(&rfkill_bt);
96 break;
Ivo van Doorne0665482007-09-13 09:21:31 +020097 case KEY_UWB:
98 rfkill_schedule_toggle(&rfkill_uwb);
99 break;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700100 default:
101 break;
102 }
103 }
104}
105
106static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
107 const struct input_device_id *id)
108{
109 struct input_handle *handle;
110 int error;
111
112 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
113 if (!handle)
114 return -ENOMEM;
115
116 handle->dev = dev;
117 handle->handler = handler;
118 handle->name = "rfkill";
119
120 error = input_register_handle(handle);
121 if (error)
122 goto err_free_handle;
123
124 error = input_open_device(handle);
125 if (error)
126 goto err_unregister_handle;
127
128 return 0;
129
130 err_unregister_handle:
131 input_unregister_handle(handle);
132 err_free_handle:
133 kfree(handle);
134 return error;
135}
136
137static void rfkill_disconnect(struct input_handle *handle)
138{
139 input_close_device(handle);
140 input_unregister_handle(handle);
141 kfree(handle);
142}
143
144static const struct input_device_id rfkill_ids[] = {
145 {
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) },
149 },
150 {
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) },
154 },
Ivo van Doorne0665482007-09-13 09:21:31 +0200155 {
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) },
159 },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700160 { }
161};
162
163static struct input_handler rfkill_handler = {
164 .event = rfkill_event,
165 .connect = rfkill_connect,
166 .disconnect = rfkill_disconnect,
167 .name = "rfkill",
168 .id_table = rfkill_ids,
169};
170
171static int __init rfkill_handler_init(void)
172{
173 return input_register_handler(&rfkill_handler);
174}
175
176static void __exit rfkill_handler_exit(void)
177{
178 input_unregister_handler(&rfkill_handler);
179 flush_scheduled_work();
180}
181
182module_init(rfkill_handler_init);
183module_exit(rfkill_handler_exit);