blob: 29c13d308b3104a18bf5bcf28dc556c62b99fe62 [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
Ivo van Doornfe242cf2007-09-27 14:57:05 -070020#include "rfkill-input.h"
21
Ivo van Doorncf4328c2007-05-07 00:34:20 -070022MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
23MODULE_DESCRIPTION("Input layer to RF switch connector");
24MODULE_LICENSE("GPL");
25
26struct rfkill_task {
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 */
34};
35
36static void rfkill_task_handler(struct work_struct *work)
37{
38 struct rfkill_task *task = container_of(work, struct rfkill_task, work);
39 enum rfkill_state state;
40
41 mutex_lock(&task->mutex);
42
43 /*
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.
47 */
48 state = task->desired_state;
49
50 if (state != task->current_state) {
51 rfkill_switch_all(task->type, state);
52 task->current_state = state;
53 }
54
55 mutex_unlock(&task->mutex);
56}
57
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -030058static void rfkill_schedule_set(struct rfkill_task *task,
59 enum rfkill_state desired_state)
60{
61 unsigned long flags;
62
63 spin_lock_irqsave(&task->lock, flags);
64
65 if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
66 task->desired_state = desired_state;
67 task->last = jiffies;
68 schedule_work(&task->work);
69 }
70
71 spin_unlock_irqrestore(&task->lock, flags);
72}
73
Ivo van Doorncf4328c2007-05-07 00:34:20 -070074static void rfkill_schedule_toggle(struct rfkill_task *task)
75{
Ingo Molnare6c91162007-07-14 18:50:15 -070076 unsigned long flags;
Ivo van Doorncf4328c2007-05-07 00:34:20 -070077
78 spin_lock_irqsave(&task->lock, flags);
79
80 if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
81 task->desired_state = !task->desired_state;
82 task->last = jiffies;
83 schedule_work(&task->work);
84 }
85
86 spin_unlock_irqrestore(&task->lock, flags);
87}
88
89#define DEFINE_RFKILL_TASK(n, t) \
90 struct rfkill_task n = { \
91 .work = __WORK_INITIALIZER(n.work, \
92 rfkill_task_handler), \
93 .type = t, \
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, \
98 }
99
100static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
101static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
Ivo van Doorne06654862007-09-13 09:21:31 +0200102static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800103static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX);
Henrique de Moraes Holschuh477576a2008-06-23 17:23:01 -0300104static DEFINE_RFKILL_TASK(rfkill_wwan, RFKILL_TYPE_WWAN);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700105
106static void rfkill_event(struct input_handle *handle, unsigned int type,
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300107 unsigned int code, int data)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700108{
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300109 if (type == EV_KEY && data == 1) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700110 switch (code) {
111 case KEY_WLAN:
112 rfkill_schedule_toggle(&rfkill_wlan);
113 break;
114 case KEY_BLUETOOTH:
115 rfkill_schedule_toggle(&rfkill_bt);
116 break;
Ivo van Doorne06654862007-09-13 09:21:31 +0200117 case KEY_UWB:
118 rfkill_schedule_toggle(&rfkill_uwb);
119 break;
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800120 case KEY_WIMAX:
121 rfkill_schedule_toggle(&rfkill_wimax);
122 break;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700123 default:
124 break;
125 }
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300126 } else if (type == EV_SW) {
127 switch (code) {
128 case SW_RFKILL_ALL:
129 /* EVERY radio type. data != 0 means radios ON */
Henrique de Moraes Holschuh477576a2008-06-23 17:23:01 -0300130 rfkill_schedule_set(&rfkill_wwan,
131 (data)? RFKILL_STATE_ON:
132 RFKILL_STATE_OFF);
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300133 rfkill_schedule_set(&rfkill_wimax,
134 (data)? RFKILL_STATE_ON:
135 RFKILL_STATE_OFF);
136 rfkill_schedule_set(&rfkill_uwb,
137 (data)? RFKILL_STATE_ON:
138 RFKILL_STATE_OFF);
139 rfkill_schedule_set(&rfkill_bt,
140 (data)? RFKILL_STATE_ON:
141 RFKILL_STATE_OFF);
142 rfkill_schedule_set(&rfkill_wlan,
143 (data)? RFKILL_STATE_ON:
144 RFKILL_STATE_OFF);
145 break;
146 default:
147 break;
148 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700149 }
150}
151
152static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
153 const struct input_device_id *id)
154{
155 struct input_handle *handle;
156 int error;
157
158 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
159 if (!handle)
160 return -ENOMEM;
161
162 handle->dev = dev;
163 handle->handler = handler;
164 handle->name = "rfkill";
165
166 error = input_register_handle(handle);
167 if (error)
168 goto err_free_handle;
169
170 error = input_open_device(handle);
171 if (error)
172 goto err_unregister_handle;
173
174 return 0;
175
176 err_unregister_handle:
177 input_unregister_handle(handle);
178 err_free_handle:
179 kfree(handle);
180 return error;
181}
182
183static void rfkill_disconnect(struct input_handle *handle)
184{
185 input_close_device(handle);
186 input_unregister_handle(handle);
187 kfree(handle);
188}
189
190static const struct input_device_id rfkill_ids[] = {
191 {
192 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700193 .evbit = { BIT_MASK(EV_KEY) },
194 .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700195 },
196 {
197 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700198 .evbit = { BIT_MASK(EV_KEY) },
199 .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700200 },
Ivo van Doorne06654862007-09-13 09:21:31 +0200201 {
202 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700203 .evbit = { BIT_MASK(EV_KEY) },
204 .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
Ivo van Doorne06654862007-09-13 09:21:31 +0200205 },
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800206 {
207 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
208 .evbit = { BIT_MASK(EV_KEY) },
209 .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
210 },
Henrique de Moraes Holschuh28f089c2008-06-23 17:22:58 -0300211 {
212 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
213 .evbit = { BIT(EV_SW) },
214 .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
215 },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700216 { }
217};
218
219static struct input_handler rfkill_handler = {
220 .event = rfkill_event,
221 .connect = rfkill_connect,
222 .disconnect = rfkill_disconnect,
223 .name = "rfkill",
224 .id_table = rfkill_ids,
225};
226
227static int __init rfkill_handler_init(void)
228{
229 return input_register_handler(&rfkill_handler);
230}
231
232static void __exit rfkill_handler_exit(void)
233{
234 input_unregister_handler(&rfkill_handler);
235 flush_scheduled_work();
236}
237
238module_init(rfkill_handler_init);
239module_exit(rfkill_handler_exit);