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 */ |
| 33 | enum rfkill_state current_state; /* on/off */ |
| 34 | }; |
| 35 | |
| 36 | static 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 | |
| 58 | static void rfkill_schedule_toggle(struct rfkill_task *task) |
| 59 | { |
Ingo Molnar | e6c9116 | 2007-07-14 18:50:15 -0700 | [diff] [blame] | 60 | unsigned long flags; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 61 | |
| 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 | |
| 84 | static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN); |
| 85 | static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH); |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 86 | 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^] | 87 | static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 88 | |
| 89 | static void rfkill_event(struct input_handle *handle, unsigned int type, |
YOSHIFUJI Hideaki | 2b81bff | 2007-07-19 10:44:38 +0900 | [diff] [blame] | 90 | unsigned int code, int down) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 91 | { |
| 92 | if (type == EV_KEY && down == 1) { |
| 93 | switch (code) { |
| 94 | case KEY_WLAN: |
| 95 | rfkill_schedule_toggle(&rfkill_wlan); |
| 96 | break; |
| 97 | case KEY_BLUETOOTH: |
| 98 | rfkill_schedule_toggle(&rfkill_bt); |
| 99 | break; |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 100 | case KEY_UWB: |
| 101 | rfkill_schedule_toggle(&rfkill_uwb); |
| 102 | break; |
Iñaky Pérez-González | 303d9bf | 2008-01-23 13:40:27 -0800 | [diff] [blame^] | 103 | case KEY_WIMAX: |
| 104 | rfkill_schedule_toggle(&rfkill_wimax); |
| 105 | break; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 106 | default: |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static int rfkill_connect(struct input_handler *handler, struct input_dev *dev, |
| 113 | const struct input_device_id *id) |
| 114 | { |
| 115 | struct input_handle *handle; |
| 116 | int error; |
| 117 | |
| 118 | handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); |
| 119 | if (!handle) |
| 120 | return -ENOMEM; |
| 121 | |
| 122 | handle->dev = dev; |
| 123 | handle->handler = handler; |
| 124 | handle->name = "rfkill"; |
| 125 | |
| 126 | error = input_register_handle(handle); |
| 127 | if (error) |
| 128 | goto err_free_handle; |
| 129 | |
| 130 | error = input_open_device(handle); |
| 131 | if (error) |
| 132 | goto err_unregister_handle; |
| 133 | |
| 134 | return 0; |
| 135 | |
| 136 | err_unregister_handle: |
| 137 | input_unregister_handle(handle); |
| 138 | err_free_handle: |
| 139 | kfree(handle); |
| 140 | return error; |
| 141 | } |
| 142 | |
| 143 | static void rfkill_disconnect(struct input_handle *handle) |
| 144 | { |
| 145 | input_close_device(handle); |
| 146 | input_unregister_handle(handle); |
| 147 | kfree(handle); |
| 148 | } |
| 149 | |
| 150 | static const struct input_device_id rfkill_ids[] = { |
| 151 | { |
| 152 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 153 | .evbit = { BIT_MASK(EV_KEY) }, |
| 154 | .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) }, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 155 | }, |
| 156 | { |
| 157 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 158 | .evbit = { BIT_MASK(EV_KEY) }, |
| 159 | .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) }, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 160 | }, |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 161 | { |
| 162 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 163 | .evbit = { BIT_MASK(EV_KEY) }, |
| 164 | .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) }, |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 165 | }, |
Iñaky Pérez-González | 303d9bf | 2008-01-23 13:40:27 -0800 | [diff] [blame^] | 166 | { |
| 167 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
| 168 | .evbit = { BIT_MASK(EV_KEY) }, |
| 169 | .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) }, |
| 170 | }, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 171 | { } |
| 172 | }; |
| 173 | |
| 174 | static struct input_handler rfkill_handler = { |
| 175 | .event = rfkill_event, |
| 176 | .connect = rfkill_connect, |
| 177 | .disconnect = rfkill_disconnect, |
| 178 | .name = "rfkill", |
| 179 | .id_table = rfkill_ids, |
| 180 | }; |
| 181 | |
| 182 | static int __init rfkill_handler_init(void) |
| 183 | { |
| 184 | return input_register_handler(&rfkill_handler); |
| 185 | } |
| 186 | |
| 187 | static void __exit rfkill_handler_exit(void) |
| 188 | { |
| 189 | input_unregister_handler(&rfkill_handler); |
| 190 | flush_scheduled_work(); |
| 191 | } |
| 192 | |
| 193 | module_init(rfkill_handler_init); |
| 194 | module_exit(rfkill_handler_exit); |