rfkill: drop current_state from tasks in rfkill-input
[safe/jmp/linux-2.6] / net / rfkill / rfkill-input.c
1 /*
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
20 #include "rfkill-input.h"
21
22 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
23 MODULE_DESCRIPTION("Input layer to RF switch connector");
24 MODULE_LICENSE("GPL");
25
26 struct 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 };
34
35 static void rfkill_task_handler(struct work_struct *work)
36 {
37         struct rfkill_task *task = container_of(work, struct rfkill_task, work);
38
39         mutex_lock(&task->mutex);
40
41         rfkill_switch_all(task->type, task->desired_state);
42
43         mutex_unlock(&task->mutex);
44 }
45
46 static void rfkill_schedule_set(struct rfkill_task *task,
47                                 enum rfkill_state desired_state)
48 {
49         unsigned long flags;
50
51         spin_lock_irqsave(&task->lock, flags);
52
53         if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
54                 task->desired_state = desired_state;
55                 task->last = jiffies;
56                 schedule_work(&task->work);
57         }
58
59         spin_unlock_irqrestore(&task->lock, flags);
60 }
61
62 static void rfkill_schedule_toggle(struct rfkill_task *task)
63 {
64         unsigned long flags;
65
66         spin_lock_irqsave(&task->lock, flags);
67
68         if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
69                 task->desired_state = !task->desired_state;
70                 task->last = jiffies;
71                 schedule_work(&task->work);
72         }
73
74         spin_unlock_irqrestore(&task->lock, flags);
75 }
76
77 #define DEFINE_RFKILL_TASK(n, t)                        \
78         struct rfkill_task n = {                        \
79                 .work = __WORK_INITIALIZER(n.work,      \
80                                 rfkill_task_handler),   \
81                 .type = t,                              \
82                 .mutex = __MUTEX_INITIALIZER(n.mutex),  \
83                 .lock = __SPIN_LOCK_UNLOCKED(n.lock),   \
84                 .desired_state = RFKILL_STATE_ON,       \
85         }
86
87 static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
88 static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
89 static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
90 static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX);
91 static DEFINE_RFKILL_TASK(rfkill_wwan, RFKILL_TYPE_WWAN);
92
93 static void rfkill_event(struct input_handle *handle, unsigned int type,
94                         unsigned int code, int data)
95 {
96         if (type == EV_KEY && data == 1) {
97                 switch (code) {
98                 case KEY_WLAN:
99                         rfkill_schedule_toggle(&rfkill_wlan);
100                         break;
101                 case KEY_BLUETOOTH:
102                         rfkill_schedule_toggle(&rfkill_bt);
103                         break;
104                 case KEY_UWB:
105                         rfkill_schedule_toggle(&rfkill_uwb);
106                         break;
107                 case KEY_WIMAX:
108                         rfkill_schedule_toggle(&rfkill_wimax);
109                         break;
110                 default:
111                         break;
112                 }
113         } else if (type == EV_SW) {
114                 switch (code) {
115                 case SW_RFKILL_ALL:
116                         /* EVERY radio type. data != 0 means radios ON */
117                         rfkill_schedule_set(&rfkill_wwan,
118                                             (data)? RFKILL_STATE_ON:
119                                                     RFKILL_STATE_OFF);
120                         rfkill_schedule_set(&rfkill_wimax,
121                                             (data)? RFKILL_STATE_ON:
122                                                     RFKILL_STATE_OFF);
123                         rfkill_schedule_set(&rfkill_uwb,
124                                             (data)? RFKILL_STATE_ON:
125                                                     RFKILL_STATE_OFF);
126                         rfkill_schedule_set(&rfkill_bt,
127                                             (data)? RFKILL_STATE_ON:
128                                                     RFKILL_STATE_OFF);
129                         rfkill_schedule_set(&rfkill_wlan,
130                                             (data)? RFKILL_STATE_ON:
131                                                     RFKILL_STATE_OFF);
132                         break;
133                 default:
134                         break;
135                 }
136         }
137 }
138
139 static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
140                           const struct input_device_id *id)
141 {
142         struct input_handle *handle;
143         int error;
144
145         handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
146         if (!handle)
147                 return -ENOMEM;
148
149         handle->dev = dev;
150         handle->handler = handler;
151         handle->name = "rfkill";
152
153         error = input_register_handle(handle);
154         if (error)
155                 goto err_free_handle;
156
157         error = input_open_device(handle);
158         if (error)
159                 goto err_unregister_handle;
160
161         return 0;
162
163  err_unregister_handle:
164         input_unregister_handle(handle);
165  err_free_handle:
166         kfree(handle);
167         return error;
168 }
169
170 static void rfkill_disconnect(struct input_handle *handle)
171 {
172         input_close_device(handle);
173         input_unregister_handle(handle);
174         kfree(handle);
175 }
176
177 static const struct input_device_id rfkill_ids[] = {
178         {
179                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
180                 .evbit = { BIT_MASK(EV_KEY) },
181                 .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
182         },
183         {
184                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
185                 .evbit = { BIT_MASK(EV_KEY) },
186                 .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
187         },
188         {
189                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
190                 .evbit = { BIT_MASK(EV_KEY) },
191                 .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
192         },
193         {
194                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
195                 .evbit = { BIT_MASK(EV_KEY) },
196                 .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
197         },
198         {
199                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
200                 .evbit = { BIT(EV_SW) },
201                 .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
202         },
203         { }
204 };
205
206 static struct input_handler rfkill_handler = {
207         .event =        rfkill_event,
208         .connect =      rfkill_connect,
209         .disconnect =   rfkill_disconnect,
210         .name =         "rfkill",
211         .id_table =     rfkill_ids,
212 };
213
214 static int __init rfkill_handler_init(void)
215 {
216         return input_register_handler(&rfkill_handler);
217 }
218
219 static void __exit rfkill_handler_exit(void)
220 {
221         input_unregister_handler(&rfkill_handler);
222         flush_scheduled_work();
223 }
224
225 module_init(rfkill_handler_init);
226 module_exit(rfkill_handler_exit);