Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 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 | |
Ivo van Doorn | fe242cf | 2007-09-27 14:57:05 -0700 | [diff] [blame] | 20 | #include "rfkill-input.h" |
| 21 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 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 */ |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 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); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 38 | |
| 39 | mutex_lock(&task->mutex); |
| 40 | |
Fabien Crespel | fbc6af2 | 2008-06-23 17:23:06 -0300 | [diff] [blame] | 41 | rfkill_switch_all(task->type, task->desired_state); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 42 | |
| 43 | mutex_unlock(&task->mutex); |
| 44 | } |
| 45 | |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 46 | static void rfkill_task_epo_handler(struct work_struct *work) |
| 47 | { |
| 48 | rfkill_epo(); |
| 49 | } |
| 50 | |
| 51 | static DECLARE_WORK(epo_work, rfkill_task_epo_handler); |
| 52 | |
| 53 | static void rfkill_schedule_epo(void) |
| 54 | { |
| 55 | schedule_work(&epo_work); |
| 56 | } |
| 57 | |
Henrique de Moraes Holschuh | 28f089c | 2008-06-23 17:22:58 -0300 | [diff] [blame] | 58 | static void rfkill_schedule_set(struct rfkill_task *task, |
| 59 | enum rfkill_state desired_state) |
| 60 | { |
| 61 | unsigned long flags; |
| 62 | |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 63 | if (unlikely(work_pending(&epo_work))) |
| 64 | return; |
| 65 | |
Henrique de Moraes Holschuh | 28f089c | 2008-06-23 17:22:58 -0300 | [diff] [blame] | 66 | spin_lock_irqsave(&task->lock, flags); |
| 67 | |
| 68 | if (time_after(jiffies, task->last + msecs_to_jiffies(200))) { |
| 69 | task->desired_state = desired_state; |
| 70 | task->last = jiffies; |
| 71 | schedule_work(&task->work); |
| 72 | } |
| 73 | |
| 74 | spin_unlock_irqrestore(&task->lock, flags); |
| 75 | } |
| 76 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 77 | static void rfkill_schedule_toggle(struct rfkill_task *task) |
| 78 | { |
Ingo Molnar | e6c9116 | 2007-07-14 18:50:15 -0700 | [diff] [blame] | 79 | unsigned long flags; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 80 | |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 81 | if (unlikely(work_pending(&epo_work))) |
| 82 | return; |
| 83 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 84 | spin_lock_irqsave(&task->lock, flags); |
| 85 | |
| 86 | if (time_after(jiffies, task->last + msecs_to_jiffies(200))) { |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 87 | task->desired_state = |
| 88 | rfkill_state_complement(task->desired_state); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 89 | task->last = jiffies; |
| 90 | schedule_work(&task->work); |
| 91 | } |
| 92 | |
| 93 | spin_unlock_irqrestore(&task->lock, flags); |
| 94 | } |
| 95 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 96 | #define DEFINE_RFKILL_TASK(n, t) \ |
| 97 | struct rfkill_task n = { \ |
| 98 | .work = __WORK_INITIALIZER(n.work, \ |
| 99 | rfkill_task_handler), \ |
| 100 | .type = t, \ |
| 101 | .mutex = __MUTEX_INITIALIZER(n.mutex), \ |
| 102 | .lock = __SPIN_LOCK_UNLOCKED(n.lock), \ |
| 103 | .desired_state = RFKILL_STATE_UNBLOCKED, \ |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN); |
| 107 | static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH); |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 108 | static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB); |
Iñaky Pérez-González | 303d9bf | 2008-01-23 13:40:27 -0800 | [diff] [blame] | 109 | static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX); |
Henrique de Moraes Holschuh | 477576a | 2008-06-23 17:23:01 -0300 | [diff] [blame] | 110 | static DEFINE_RFKILL_TASK(rfkill_wwan, RFKILL_TYPE_WWAN); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 111 | |
| 112 | static void rfkill_event(struct input_handle *handle, unsigned int type, |
Henrique de Moraes Holschuh | 28f089c | 2008-06-23 17:22:58 -0300 | [diff] [blame] | 113 | unsigned int code, int data) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 114 | { |
Henrique de Moraes Holschuh | 28f089c | 2008-06-23 17:22:58 -0300 | [diff] [blame] | 115 | if (type == EV_KEY && data == 1) { |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 116 | switch (code) { |
| 117 | case KEY_WLAN: |
| 118 | rfkill_schedule_toggle(&rfkill_wlan); |
| 119 | break; |
| 120 | case KEY_BLUETOOTH: |
| 121 | rfkill_schedule_toggle(&rfkill_bt); |
| 122 | break; |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 123 | case KEY_UWB: |
| 124 | rfkill_schedule_toggle(&rfkill_uwb); |
| 125 | break; |
Iñaky Pérez-González | 303d9bf | 2008-01-23 13:40:27 -0800 | [diff] [blame] | 126 | case KEY_WIMAX: |
| 127 | rfkill_schedule_toggle(&rfkill_wimax); |
| 128 | break; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 129 | default: |
| 130 | break; |
| 131 | } |
Henrique de Moraes Holschuh | 28f089c | 2008-06-23 17:22:58 -0300 | [diff] [blame] | 132 | } else if (type == EV_SW) { |
| 133 | switch (code) { |
| 134 | case SW_RFKILL_ALL: |
| 135 | /* EVERY radio type. data != 0 means radios ON */ |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 136 | /* handle EPO (emergency power off) through shortcut */ |
| 137 | if (data) { |
| 138 | rfkill_schedule_set(&rfkill_wwan, |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 139 | RFKILL_STATE_UNBLOCKED); |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 140 | rfkill_schedule_set(&rfkill_wimax, |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 141 | RFKILL_STATE_UNBLOCKED); |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 142 | rfkill_schedule_set(&rfkill_uwb, |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 143 | RFKILL_STATE_UNBLOCKED); |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 144 | rfkill_schedule_set(&rfkill_bt, |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 145 | RFKILL_STATE_UNBLOCKED); |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 146 | rfkill_schedule_set(&rfkill_wlan, |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 147 | RFKILL_STATE_UNBLOCKED); |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 148 | } else |
| 149 | rfkill_schedule_epo(); |
Henrique de Moraes Holschuh | 28f089c | 2008-06-23 17:22:58 -0300 | [diff] [blame] | 150 | break; |
| 151 | default: |
| 152 | break; |
| 153 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | static int rfkill_connect(struct input_handler *handler, struct input_dev *dev, |
| 158 | const struct input_device_id *id) |
| 159 | { |
| 160 | struct input_handle *handle; |
| 161 | int error; |
| 162 | |
| 163 | handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); |
| 164 | if (!handle) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | handle->dev = dev; |
| 168 | handle->handler = handler; |
| 169 | handle->name = "rfkill"; |
| 170 | |
| 171 | error = input_register_handle(handle); |
| 172 | if (error) |
| 173 | goto err_free_handle; |
| 174 | |
| 175 | error = input_open_device(handle); |
| 176 | if (error) |
| 177 | goto err_unregister_handle; |
| 178 | |
| 179 | return 0; |
| 180 | |
| 181 | err_unregister_handle: |
| 182 | input_unregister_handle(handle); |
| 183 | err_free_handle: |
| 184 | kfree(handle); |
| 185 | return error; |
| 186 | } |
| 187 | |
| 188 | static void rfkill_disconnect(struct input_handle *handle) |
| 189 | { |
| 190 | input_close_device(handle); |
| 191 | input_unregister_handle(handle); |
| 192 | kfree(handle); |
| 193 | } |
| 194 | |
| 195 | static const struct input_device_id rfkill_ids[] = { |
| 196 | { |
| 197 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 198 | .evbit = { BIT_MASK(EV_KEY) }, |
| 199 | .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) }, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 200 | }, |
| 201 | { |
| 202 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 203 | .evbit = { BIT_MASK(EV_KEY) }, |
| 204 | .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) }, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 205 | }, |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 206 | { |
| 207 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 208 | .evbit = { BIT_MASK(EV_KEY) }, |
| 209 | .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) }, |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 210 | }, |
Iñaky Pérez-González | 303d9bf | 2008-01-23 13:40:27 -0800 | [diff] [blame] | 211 | { |
| 212 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
| 213 | .evbit = { BIT_MASK(EV_KEY) }, |
| 214 | .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) }, |
| 215 | }, |
Henrique de Moraes Holschuh | 28f089c | 2008-06-23 17:22:58 -0300 | [diff] [blame] | 216 | { |
| 217 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT, |
| 218 | .evbit = { BIT(EV_SW) }, |
| 219 | .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) }, |
| 220 | }, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 221 | { } |
| 222 | }; |
| 223 | |
| 224 | static struct input_handler rfkill_handler = { |
| 225 | .event = rfkill_event, |
| 226 | .connect = rfkill_connect, |
| 227 | .disconnect = rfkill_disconnect, |
| 228 | .name = "rfkill", |
| 229 | .id_table = rfkill_ids, |
| 230 | }; |
| 231 | |
| 232 | static int __init rfkill_handler_init(void) |
| 233 | { |
| 234 | return input_register_handler(&rfkill_handler); |
| 235 | } |
| 236 | |
| 237 | static void __exit rfkill_handler_exit(void) |
| 238 | { |
| 239 | input_unregister_handler(&rfkill_handler); |
| 240 | flush_scheduled_work(); |
| 241 | } |
| 242 | |
| 243 | module_init(rfkill_handler_init); |
| 244 | module_exit(rfkill_handler_exit); |