blob: d1e9d68f8ba0a003f0820f95800620d3030184cd [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
58static void rfkill_schedule_toggle(struct rfkill_task *task)
59{
Ingo Molnare6c91162007-07-14 18:50:15 -070060 unsigned long flags;
Ivo van Doorncf4328c2007-05-07 00:34:20 -070061
62 spin_lock_irqsave(&task->lock, flags);
63
64 if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
65 task->desired_state = !task->desired_state;
66 task->last = jiffies;
67 schedule_work(&task->work);
68 }
69
70 spin_unlock_irqrestore(&task->lock, flags);
71}
72
73#define DEFINE_RFKILL_TASK(n, t) \
74 struct rfkill_task n = { \
75 .work = __WORK_INITIALIZER(n.work, \
76 rfkill_task_handler), \
77 .type = t, \
78 .mutex = __MUTEX_INITIALIZER(n.mutex), \
79 .lock = __SPIN_LOCK_UNLOCKED(n.lock), \
80 .desired_state = RFKILL_STATE_ON, \
81 .current_state = RFKILL_STATE_ON, \
82 }
83
84static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
85static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
Ivo van Doorne06654862007-09-13 09:21:31 +020086static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
Ivo van Doorncf4328c2007-05-07 00:34:20 -070087
88static void rfkill_event(struct input_handle *handle, unsigned int type,
YOSHIFUJI Hideaki2b81bff2007-07-19 10:44:38 +090089 unsigned int code, int down)
Ivo van Doorncf4328c2007-05-07 00:34:20 -070090{
91 if (type == EV_KEY && down == 1) {
92 switch (code) {
93 case KEY_WLAN:
94 rfkill_schedule_toggle(&rfkill_wlan);
95 break;
96 case KEY_BLUETOOTH:
97 rfkill_schedule_toggle(&rfkill_bt);
98 break;
Ivo van Doorne06654862007-09-13 09:21:31 +020099 case KEY_UWB:
100 rfkill_schedule_toggle(&rfkill_uwb);
101 break;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700102 default:
103 break;
104 }
105 }
106}
107
108static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
109 const struct input_device_id *id)
110{
111 struct input_handle *handle;
112 int error;
113
114 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
115 if (!handle)
116 return -ENOMEM;
117
118 handle->dev = dev;
119 handle->handler = handler;
120 handle->name = "rfkill";
121
122 error = input_register_handle(handle);
123 if (error)
124 goto err_free_handle;
125
126 error = input_open_device(handle);
127 if (error)
128 goto err_unregister_handle;
129
130 return 0;
131
132 err_unregister_handle:
133 input_unregister_handle(handle);
134 err_free_handle:
135 kfree(handle);
136 return error;
137}
138
139static void rfkill_disconnect(struct input_handle *handle)
140{
141 input_close_device(handle);
142 input_unregister_handle(handle);
143 kfree(handle);
144}
145
146static const struct input_device_id rfkill_ids[] = {
147 {
148 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700149 .evbit = { BIT_MASK(EV_KEY) },
150 .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700151 },
152 {
153 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700154 .evbit = { BIT_MASK(EV_KEY) },
155 .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700156 },
Ivo van Doorne06654862007-09-13 09:21:31 +0200157 {
158 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700159 .evbit = { BIT_MASK(EV_KEY) },
160 .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
Ivo van Doorne06654862007-09-13 09:21:31 +0200161 },
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700162 { }
163};
164
165static struct input_handler rfkill_handler = {
166 .event = rfkill_event,
167 .connect = rfkill_connect,
168 .disconnect = rfkill_disconnect,
169 .name = "rfkill",
170 .id_table = rfkill_ids,
171};
172
173static int __init rfkill_handler_init(void)
174{
175 return input_register_handler(&rfkill_handler);
176}
177
178static void __exit rfkill_handler_exit(void)
179{
180 input_unregister_handler(&rfkill_handler);
181 flush_scheduled_work();
182}
183
184module_init(rfkill_handler_init);
185module_exit(rfkill_handler_exit);