Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 1 | /* |
Ivo van Doorn | fe242cf | 2007-09-27 14:57:05 -0700 | [diff] [blame] | 2 | * Copyright (C) 2006 - 2007 Ivo van Doorn |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 3 | * Copyright (C) 2007 Dmitry Torokhov |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the |
| 17 | * Free Software Foundation, Inc., |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/capability.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/mutex.h> |
| 28 | #include <linux/rfkill.h> |
| 29 | |
Michael Buesch | 2736622 | 2007-11-02 20:18:11 +0100 | [diff] [blame] | 30 | /* Get declaration of rfkill_switch_all() to shut up sparse. */ |
| 31 | #include "rfkill-input.h" |
| 32 | |
| 33 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 34 | MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>"); |
| 35 | MODULE_VERSION("1.0"); |
| 36 | MODULE_DESCRIPTION("RF switch support"); |
| 37 | MODULE_LICENSE("GPL"); |
| 38 | |
| 39 | static LIST_HEAD(rfkill_list); /* list of registered rf switches */ |
| 40 | static DEFINE_MUTEX(rfkill_mutex); |
| 41 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 42 | static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED; |
Henrique de Moraes Holschuh | e954b0b | 2008-06-23 17:22:59 -0300 | [diff] [blame] | 43 | module_param_named(default_state, rfkill_default_state, uint, 0444); |
| 44 | MODULE_PARM_DESC(default_state, |
| 45 | "Default initial state for all radio types, 0 = radio off"); |
| 46 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 47 | static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX]; |
| 48 | |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 49 | static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list); |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * register_rfkill_notifier - Add notifier to rfkill notifier chain |
| 54 | * @nb: pointer to the new entry to add to the chain |
| 55 | * |
| 56 | * See blocking_notifier_chain_register() for return value and further |
| 57 | * observations. |
| 58 | * |
| 59 | * Adds a notifier to the rfkill notifier chain. The chain will be |
| 60 | * called with a pointer to the relevant rfkill structure as a parameter, |
| 61 | * refer to include/linux/rfkill.h for the possible events. |
| 62 | * |
| 63 | * Notifiers added to this chain are to always return NOTIFY_DONE. This |
| 64 | * chain is a blocking notifier chain: notifiers can sleep. |
| 65 | * |
| 66 | * Calls to this chain may have been done through a workqueue. One must |
| 67 | * assume unordered asynchronous behaviour, there is no way to know if |
| 68 | * actions related to the event that generated the notification have been |
| 69 | * carried out already. |
| 70 | */ |
| 71 | int register_rfkill_notifier(struct notifier_block *nb) |
| 72 | { |
| 73 | return blocking_notifier_chain_register(&rfkill_notifier_list, nb); |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(register_rfkill_notifier); |
| 76 | |
| 77 | /** |
| 78 | * unregister_rfkill_notifier - remove notifier from rfkill notifier chain |
| 79 | * @nb: pointer to the entry to remove from the chain |
| 80 | * |
| 81 | * See blocking_notifier_chain_unregister() for return value and further |
| 82 | * observations. |
| 83 | * |
| 84 | * Removes a notifier from the rfkill notifier chain. |
| 85 | */ |
| 86 | int unregister_rfkill_notifier(struct notifier_block *nb) |
| 87 | { |
| 88 | return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb); |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(unregister_rfkill_notifier); |
| 91 | |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 92 | |
| 93 | static void rfkill_led_trigger(struct rfkill *rfkill, |
| 94 | enum rfkill_state state) |
| 95 | { |
| 96 | #ifdef CONFIG_RFKILL_LEDS |
| 97 | struct led_trigger *led = &rfkill->led_trigger; |
| 98 | |
| 99 | if (!led->name) |
| 100 | return; |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 101 | if (state != RFKILL_STATE_UNBLOCKED) |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 102 | led_trigger_event(led, LED_OFF); |
| 103 | else |
| 104 | led_trigger_event(led, LED_FULL); |
| 105 | #endif /* CONFIG_RFKILL_LEDS */ |
| 106 | } |
| 107 | |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 108 | static void notify_rfkill_state_change(struct rfkill *rfkill) |
| 109 | { |
| 110 | blocking_notifier_call_chain(&rfkill_notifier_list, |
| 111 | RFKILL_STATE_CHANGED, |
| 112 | rfkill); |
| 113 | } |
| 114 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 115 | static void update_rfkill_state(struct rfkill *rfkill) |
| 116 | { |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 117 | enum rfkill_state newstate, oldstate; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 118 | |
| 119 | if (rfkill->get_state) { |
| 120 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 121 | if (!rfkill->get_state(rfkill->data, &newstate)) { |
| 122 | oldstate = rfkill->state; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 123 | rfkill->state = newstate; |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 124 | if (oldstate != newstate) |
| 125 | notify_rfkill_state_change(rfkill); |
| 126 | } |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 127 | mutex_unlock(&rfkill->mutex); |
| 128 | } |
| 129 | } |
| 130 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 131 | /** |
| 132 | * rfkill_toggle_radio - wrapper for toggle_radio hook |
Henrique de Moraes Holschuh | 0f687e9 | 2008-07-03 13:14:56 -0300 | [diff] [blame] | 133 | * |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 134 | * @rfkill: the rfkill struct to use |
| 135 | * @force: calls toggle_radio even if cache says it is not needed, |
| 136 | * and also makes sure notifications of the state will be |
| 137 | * sent even if it didn't change |
| 138 | * @state: the new state to call toggle_radio() with |
| 139 | * |
Henrique de Moraes Holschuh | 0f687e9 | 2008-07-03 13:14:56 -0300 | [diff] [blame] | 140 | * Calls rfkill->toggle_radio, enforcing the API for toggle_radio |
| 141 | * calls and handling all the red tape such as issuing notifications |
| 142 | * if the call is successful. |
| 143 | * |
| 144 | * Note that @force cannot override a (possibly cached) state of |
| 145 | * RFKILL_STATE_HARD_BLOCKED. Any device making use of |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 146 | * RFKILL_STATE_HARD_BLOCKED implements either get_state() or |
| 147 | * rfkill_force_state(), so the cache either is bypassed or valid. |
| 148 | * |
| 149 | * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED |
| 150 | * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to |
| 151 | * give the driver a hint that it should double-BLOCK the transmitter. |
| 152 | * |
| 153 | * Caller must have aquired rfkill_mutex. |
| 154 | */ |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 155 | static int rfkill_toggle_radio(struct rfkill *rfkill, |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 156 | enum rfkill_state state, |
| 157 | int force) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 158 | { |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 159 | int retval = 0; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 160 | enum rfkill_state oldstate, newstate; |
| 161 | |
| 162 | oldstate = rfkill->state; |
| 163 | |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 164 | if (rfkill->get_state && !force && |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 165 | !rfkill->get_state(rfkill->data, &newstate)) |
| 166 | rfkill->state = newstate; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 167 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 168 | switch (state) { |
| 169 | case RFKILL_STATE_HARD_BLOCKED: |
| 170 | /* typically happens when refreshing hardware state, |
| 171 | * such as on resume */ |
| 172 | state = RFKILL_STATE_SOFT_BLOCKED; |
| 173 | break; |
| 174 | case RFKILL_STATE_UNBLOCKED: |
| 175 | /* force can't override this, only rfkill_force_state() can */ |
| 176 | if (rfkill->state == RFKILL_STATE_HARD_BLOCKED) |
| 177 | return -EPERM; |
| 178 | break; |
| 179 | case RFKILL_STATE_SOFT_BLOCKED: |
| 180 | /* nothing to do, we want to give drivers the hint to double |
| 181 | * BLOCK even a transmitter that is already in state |
| 182 | * RFKILL_STATE_HARD_BLOCKED */ |
| 183 | break; |
| 184 | } |
| 185 | |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 186 | if (force || state != rfkill->state) { |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 187 | retval = rfkill->toggle_radio(rfkill->data, state); |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 188 | /* never allow a HARD->SOFT downgrade! */ |
| 189 | if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 190 | rfkill->state = state; |
| 191 | } |
| 192 | |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 193 | if (force || rfkill->state != oldstate) { |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 194 | rfkill_led_trigger(rfkill, rfkill->state); |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 195 | notify_rfkill_state_change(rfkill); |
| 196 | } |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 197 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 198 | return retval; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * rfkill_switch_all - Toggle state of all switches of given type |
| 203 | * @type: type of interfaces to be affeceted |
| 204 | * @state: the new state |
| 205 | * |
| 206 | * This function toggles state of all switches of given type unless |
| 207 | * a specific switch is claimed by userspace in which case it is |
| 208 | * left alone. |
| 209 | */ |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 210 | void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state) |
| 211 | { |
| 212 | struct rfkill *rfkill; |
| 213 | |
| 214 | mutex_lock(&rfkill_mutex); |
| 215 | |
| 216 | rfkill_states[type] = state; |
| 217 | |
| 218 | list_for_each_entry(rfkill, &rfkill_list, node) { |
Carlos Corbacho | 89796f6 | 2008-04-12 16:39:47 +0100 | [diff] [blame] | 219 | if ((!rfkill->user_claim) && (rfkill->type == type)) |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 220 | rfkill_toggle_radio(rfkill, state, 0); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | mutex_unlock(&rfkill_mutex); |
| 224 | } |
| 225 | EXPORT_SYMBOL(rfkill_switch_all); |
| 226 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 227 | /** |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 228 | * rfkill_epo - emergency power off all transmitters |
| 229 | * |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 230 | * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 231 | * everything in its path but rfkill_mutex. |
| 232 | */ |
| 233 | void rfkill_epo(void) |
| 234 | { |
| 235 | struct rfkill *rfkill; |
| 236 | |
| 237 | mutex_lock(&rfkill_mutex); |
| 238 | list_for_each_entry(rfkill, &rfkill_list, node) { |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 239 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 240 | } |
| 241 | mutex_unlock(&rfkill_mutex); |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(rfkill_epo); |
| 244 | |
| 245 | /** |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 246 | * rfkill_force_state - Force the internal rfkill radio state |
| 247 | * @rfkill: pointer to the rfkill class to modify. |
| 248 | * @state: the current radio state the class should be forced to. |
| 249 | * |
| 250 | * This function updates the internal state of the radio cached |
| 251 | * by the rfkill class. It should be used when the driver gets |
| 252 | * a notification by the firmware/hardware of the current *real* |
| 253 | * state of the radio rfkill switch. |
| 254 | * |
Henrique de Moraes Holschuh | 2fd9b22 | 2008-07-21 21:18:17 -0300 | [diff] [blame] | 255 | * Devices which are subject to external changes on their rfkill |
| 256 | * state (such as those caused by a hardware rfkill line) MUST |
| 257 | * have their driver arrange to call rfkill_force_state() as soon |
| 258 | * as possible after such a change. |
| 259 | * |
| 260 | * This function may not be called from an atomic context. |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 261 | */ |
| 262 | int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state) |
| 263 | { |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 264 | enum rfkill_state oldstate; |
| 265 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 266 | if (state != RFKILL_STATE_SOFT_BLOCKED && |
| 267 | state != RFKILL_STATE_UNBLOCKED && |
| 268 | state != RFKILL_STATE_HARD_BLOCKED) |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 269 | return -EINVAL; |
| 270 | |
| 271 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 272 | |
| 273 | oldstate = rfkill->state; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 274 | rfkill->state = state; |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 275 | |
| 276 | if (state != oldstate) |
| 277 | notify_rfkill_state_change(rfkill); |
| 278 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 279 | mutex_unlock(&rfkill->mutex); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | EXPORT_SYMBOL(rfkill_force_state); |
| 284 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 285 | static ssize_t rfkill_name_show(struct device *dev, |
| 286 | struct device_attribute *attr, |
| 287 | char *buf) |
| 288 | { |
| 289 | struct rfkill *rfkill = to_rfkill(dev); |
| 290 | |
| 291 | return sprintf(buf, "%s\n", rfkill->name); |
| 292 | } |
| 293 | |
Henrique de Moraes Holschuh | 99c632e | 2008-06-23 17:23:04 -0300 | [diff] [blame] | 294 | static const char *rfkill_get_type_str(enum rfkill_type type) |
| 295 | { |
| 296 | switch (type) { |
| 297 | case RFKILL_TYPE_WLAN: |
| 298 | return "wlan"; |
| 299 | case RFKILL_TYPE_BLUETOOTH: |
| 300 | return "bluetooth"; |
| 301 | case RFKILL_TYPE_UWB: |
| 302 | return "ultrawideband"; |
| 303 | case RFKILL_TYPE_WIMAX: |
| 304 | return "wimax"; |
| 305 | case RFKILL_TYPE_WWAN: |
| 306 | return "wwan"; |
| 307 | default: |
| 308 | BUG(); |
| 309 | } |
| 310 | } |
| 311 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 312 | static ssize_t rfkill_type_show(struct device *dev, |
| 313 | struct device_attribute *attr, |
| 314 | char *buf) |
| 315 | { |
| 316 | struct rfkill *rfkill = to_rfkill(dev); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 317 | |
Henrique de Moraes Holschuh | 99c632e | 2008-06-23 17:23:04 -0300 | [diff] [blame] | 318 | return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type)); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static ssize_t rfkill_state_show(struct device *dev, |
| 322 | struct device_attribute *attr, |
| 323 | char *buf) |
| 324 | { |
| 325 | struct rfkill *rfkill = to_rfkill(dev); |
| 326 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 327 | update_rfkill_state(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 328 | return sprintf(buf, "%d\n", rfkill->state); |
| 329 | } |
| 330 | |
| 331 | static ssize_t rfkill_state_store(struct device *dev, |
| 332 | struct device_attribute *attr, |
| 333 | const char *buf, size_t count) |
| 334 | { |
| 335 | struct rfkill *rfkill = to_rfkill(dev); |
| 336 | unsigned int state = simple_strtoul(buf, NULL, 0); |
| 337 | int error; |
| 338 | |
| 339 | if (!capable(CAP_NET_ADMIN)) |
| 340 | return -EPERM; |
| 341 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 342 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
| 343 | if (state != RFKILL_STATE_UNBLOCKED && |
| 344 | state != RFKILL_STATE_SOFT_BLOCKED) |
| 345 | return -EINVAL; |
| 346 | |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 347 | if (mutex_lock_interruptible(&rfkill->mutex)) |
| 348 | return -ERESTARTSYS; |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 349 | error = rfkill_toggle_radio(rfkill, state, 0); |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 350 | mutex_unlock(&rfkill->mutex); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 351 | |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 352 | return error ? error : count; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static ssize_t rfkill_claim_show(struct device *dev, |
| 356 | struct device_attribute *attr, |
| 357 | char *buf) |
| 358 | { |
| 359 | struct rfkill *rfkill = to_rfkill(dev); |
| 360 | |
| 361 | return sprintf(buf, "%d", rfkill->user_claim); |
| 362 | } |
| 363 | |
| 364 | static ssize_t rfkill_claim_store(struct device *dev, |
| 365 | struct device_attribute *attr, |
| 366 | const char *buf, size_t count) |
| 367 | { |
| 368 | struct rfkill *rfkill = to_rfkill(dev); |
| 369 | bool claim = !!simple_strtoul(buf, NULL, 0); |
| 370 | int error; |
| 371 | |
| 372 | if (!capable(CAP_NET_ADMIN)) |
| 373 | return -EPERM; |
| 374 | |
| 375 | /* |
| 376 | * Take the global lock to make sure the kernel is not in |
| 377 | * the middle of rfkill_switch_all |
| 378 | */ |
| 379 | error = mutex_lock_interruptible(&rfkill_mutex); |
| 380 | if (error) |
| 381 | return error; |
| 382 | |
Michael Buesch | 20405c0 | 2007-09-27 21:34:23 +0200 | [diff] [blame] | 383 | if (rfkill->user_claim_unsupported) { |
| 384 | error = -EOPNOTSUPP; |
| 385 | goto out_unlock; |
| 386 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 387 | if (rfkill->user_claim != claim) { |
| 388 | if (!claim) |
| 389 | rfkill_toggle_radio(rfkill, |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 390 | rfkill_states[rfkill->type], |
| 391 | 0); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 392 | rfkill->user_claim = claim; |
| 393 | } |
| 394 | |
Michael Buesch | 20405c0 | 2007-09-27 21:34:23 +0200 | [diff] [blame] | 395 | out_unlock: |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 396 | mutex_unlock(&rfkill_mutex); |
| 397 | |
Michael Buesch | 20405c0 | 2007-09-27 21:34:23 +0200 | [diff] [blame] | 398 | return error ? error : count; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static struct device_attribute rfkill_dev_attrs[] = { |
| 402 | __ATTR(name, S_IRUGO, rfkill_name_show, NULL), |
| 403 | __ATTR(type, S_IRUGO, rfkill_type_show, NULL), |
Ivo van Doorn | c81de6a | 2007-07-18 15:38:03 -0700 | [diff] [blame] | 404 | __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store), |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 405 | __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store), |
| 406 | __ATTR_NULL |
| 407 | }; |
| 408 | |
| 409 | static void rfkill_release(struct device *dev) |
| 410 | { |
| 411 | struct rfkill *rfkill = to_rfkill(dev); |
| 412 | |
| 413 | kfree(rfkill); |
| 414 | module_put(THIS_MODULE); |
| 415 | } |
| 416 | |
| 417 | #ifdef CONFIG_PM |
| 418 | static int rfkill_suspend(struct device *dev, pm_message_t state) |
| 419 | { |
| 420 | struct rfkill *rfkill = to_rfkill(dev); |
| 421 | |
| 422 | if (dev->power.power_state.event != state.event) { |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 423 | if (state.event & PM_EVENT_SLEEP) { |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 424 | /* Stop transmitter, keep state, no notifies */ |
| 425 | update_rfkill_state(rfkill); |
| 426 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 427 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 428 | rfkill->toggle_radio(rfkill->data, |
| 429 | RFKILL_STATE_SOFT_BLOCKED); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 430 | mutex_unlock(&rfkill->mutex); |
| 431 | } |
| 432 | |
| 433 | dev->power.power_state = state; |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int rfkill_resume(struct device *dev) |
| 440 | { |
| 441 | struct rfkill *rfkill = to_rfkill(dev); |
| 442 | |
| 443 | if (dev->power.power_state.event != PM_EVENT_ON) { |
| 444 | mutex_lock(&rfkill->mutex); |
| 445 | |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 446 | /* restore radio state AND notify everybody */ |
| 447 | rfkill_toggle_radio(rfkill, rfkill->state, 1); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 448 | |
| 449 | mutex_unlock(&rfkill->mutex); |
| 450 | } |
| 451 | |
| 452 | dev->power.power_state = PMSG_ON; |
| 453 | return 0; |
| 454 | } |
| 455 | #else |
| 456 | #define rfkill_suspend NULL |
| 457 | #define rfkill_resume NULL |
| 458 | #endif |
| 459 | |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 460 | static int rfkill_blocking_uevent_notifier(struct notifier_block *nb, |
| 461 | unsigned long eventid, |
| 462 | void *data) |
| 463 | { |
| 464 | struct rfkill *rfkill = (struct rfkill *)data; |
| 465 | |
| 466 | switch (eventid) { |
| 467 | case RFKILL_STATE_CHANGED: |
| 468 | kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE); |
| 469 | break; |
| 470 | default: |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | return NOTIFY_DONE; |
| 475 | } |
| 476 | |
| 477 | static struct notifier_block rfkill_blocking_uevent_nb = { |
| 478 | .notifier_call = rfkill_blocking_uevent_notifier, |
| 479 | .priority = 0, |
| 480 | }; |
| 481 | |
| 482 | static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 483 | { |
| 484 | struct rfkill *rfkill = to_rfkill(dev); |
| 485 | int error; |
| 486 | |
| 487 | error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name); |
| 488 | if (error) |
| 489 | return error; |
| 490 | error = add_uevent_var(env, "RFKILL_TYPE=%s", |
| 491 | rfkill_get_type_str(rfkill->type)); |
| 492 | if (error) |
| 493 | return error; |
| 494 | error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state); |
| 495 | return error; |
| 496 | } |
| 497 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 498 | static struct class rfkill_class = { |
| 499 | .name = "rfkill", |
| 500 | .dev_release = rfkill_release, |
| 501 | .dev_attrs = rfkill_dev_attrs, |
| 502 | .suspend = rfkill_suspend, |
| 503 | .resume = rfkill_resume, |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 504 | .dev_uevent = rfkill_dev_uevent, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 505 | }; |
| 506 | |
| 507 | static int rfkill_add_switch(struct rfkill *rfkill) |
| 508 | { |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame] | 509 | mutex_lock(&rfkill_mutex); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 510 | |
Henrique de Moraes Holschuh | fd4484a | 2008-07-03 13:14:57 -0300 | [diff] [blame] | 511 | rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0); |
| 512 | |
| 513 | list_add_tail(&rfkill->node, &rfkill_list); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 514 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 515 | mutex_unlock(&rfkill_mutex); |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame] | 516 | |
Henrique de Moraes Holschuh | fd4484a | 2008-07-03 13:14:57 -0300 | [diff] [blame] | 517 | return 0; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void rfkill_remove_switch(struct rfkill *rfkill) |
| 521 | { |
| 522 | mutex_lock(&rfkill_mutex); |
| 523 | list_del_init(&rfkill->node); |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 524 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 525 | mutex_unlock(&rfkill_mutex); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * rfkill_allocate - allocate memory for rfkill structure. |
| 530 | * @parent: device that has rf switch on it |
Ivo van Doorn | 234a0ca | 2007-09-13 09:20:42 +0200 | [diff] [blame] | 531 | * @type: type of the switch (RFKILL_TYPE_*) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 532 | * |
| 533 | * This function should be called by the network driver when it needs |
| 534 | * rfkill structure. Once the structure is allocated the driver shoud |
| 535 | * finish its initialization by setting name, private data, enable_radio |
| 536 | * and disable_radio methods and then register it with rfkill_register(). |
| 537 | * NOTE: If registration fails the structure shoudl be freed by calling |
| 538 | * rfkill_free() otherwise rfkill_unregister() should be used. |
| 539 | */ |
| 540 | struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type) |
| 541 | { |
| 542 | struct rfkill *rfkill; |
| 543 | struct device *dev; |
| 544 | |
| 545 | rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL); |
Ivo van Doorn | d007da1 | 2007-05-19 12:24:39 -0700 | [diff] [blame] | 546 | if (!rfkill) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 547 | return NULL; |
| 548 | |
| 549 | mutex_init(&rfkill->mutex); |
| 550 | INIT_LIST_HEAD(&rfkill->node); |
| 551 | rfkill->type = type; |
| 552 | |
| 553 | dev = &rfkill->dev; |
| 554 | dev->class = &rfkill_class; |
| 555 | dev->parent = parent; |
| 556 | device_initialize(dev); |
| 557 | |
| 558 | __module_get(THIS_MODULE); |
| 559 | |
| 560 | return rfkill; |
| 561 | } |
| 562 | EXPORT_SYMBOL(rfkill_allocate); |
| 563 | |
| 564 | /** |
| 565 | * rfkill_free - Mark rfkill structure for deletion |
| 566 | * @rfkill: rfkill structure to be destroyed |
| 567 | * |
Oliver Pinter | 1dbede8 | 2008-02-03 17:55:45 +0200 | [diff] [blame] | 568 | * Decrements reference count of rfkill structure so it is destroyed. |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 569 | * Note that rfkill_free() should _not_ be called after rfkill_unregister(). |
| 570 | */ |
| 571 | void rfkill_free(struct rfkill *rfkill) |
| 572 | { |
| 573 | if (rfkill) |
| 574 | put_device(&rfkill->dev); |
| 575 | } |
| 576 | EXPORT_SYMBOL(rfkill_free); |
| 577 | |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 578 | static void rfkill_led_trigger_register(struct rfkill *rfkill) |
| 579 | { |
| 580 | #ifdef CONFIG_RFKILL_LEDS |
| 581 | int error; |
| 582 | |
| 583 | rfkill->led_trigger.name = rfkill->dev.bus_id; |
| 584 | error = led_trigger_register(&rfkill->led_trigger); |
| 585 | if (error) |
| 586 | rfkill->led_trigger.name = NULL; |
| 587 | #endif /* CONFIG_RFKILL_LEDS */ |
| 588 | } |
| 589 | |
| 590 | static void rfkill_led_trigger_unregister(struct rfkill *rfkill) |
| 591 | { |
| 592 | #ifdef CONFIG_RFKILL_LEDS |
Henrique de Moraes Holschuh | 37f55e9 | 2008-07-21 21:18:18 -0300 | [diff] [blame^] | 593 | if (rfkill->led_trigger.name) { |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 594 | led_trigger_unregister(&rfkill->led_trigger); |
Henrique de Moraes Holschuh | 37f55e9 | 2008-07-21 21:18:18 -0300 | [diff] [blame^] | 595 | rfkill->led_trigger.name = NULL; |
| 596 | } |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 597 | #endif |
| 598 | } |
| 599 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 600 | /** |
| 601 | * rfkill_register - Register a rfkill structure. |
| 602 | * @rfkill: rfkill structure to be registered |
| 603 | * |
| 604 | * This function should be called by the network driver when the rfkill |
| 605 | * structure needs to be registered. Immediately from registration the |
| 606 | * switch driver should be able to service calls to toggle_radio. |
| 607 | */ |
| 608 | int rfkill_register(struct rfkill *rfkill) |
| 609 | { |
| 610 | static atomic_t rfkill_no = ATOMIC_INIT(0); |
| 611 | struct device *dev = &rfkill->dev; |
| 612 | int error; |
| 613 | |
| 614 | if (!rfkill->toggle_radio) |
| 615 | return -EINVAL; |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame] | 616 | if (rfkill->type >= RFKILL_TYPE_MAX) |
| 617 | return -EINVAL; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 618 | |
Michael Buesch | 8a8f1c0 | 2007-10-28 13:07:54 +0100 | [diff] [blame] | 619 | snprintf(dev->bus_id, sizeof(dev->bus_id), |
| 620 | "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1); |
| 621 | |
| 622 | rfkill_led_trigger_register(rfkill); |
| 623 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 624 | error = rfkill_add_switch(rfkill); |
Eric Paris | 632041f | 2008-01-13 16:20:56 -0500 | [diff] [blame] | 625 | if (error) { |
| 626 | rfkill_led_trigger_unregister(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 627 | return error; |
Eric Paris | 632041f | 2008-01-13 16:20:56 -0500 | [diff] [blame] | 628 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 629 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 630 | error = device_add(dev); |
| 631 | if (error) { |
| 632 | rfkill_remove_switch(rfkill); |
Henrique de Moraes Holschuh | 37f55e9 | 2008-07-21 21:18:18 -0300 | [diff] [blame^] | 633 | rfkill_led_trigger_unregister(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 634 | return error; |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | EXPORT_SYMBOL(rfkill_register); |
| 640 | |
| 641 | /** |
Henrique de Moraes Holschuh | c8fcd90 | 2008-06-23 17:22:57 -0300 | [diff] [blame] | 642 | * rfkill_unregister - Unregister a rfkill structure. |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 643 | * @rfkill: rfkill structure to be unregistered |
| 644 | * |
| 645 | * This function should be called by the network driver during device |
| 646 | * teardown to destroy rfkill structure. Note that rfkill_free() should |
| 647 | * _not_ be called after rfkill_unregister(). |
| 648 | */ |
| 649 | void rfkill_unregister(struct rfkill *rfkill) |
| 650 | { |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 651 | device_del(&rfkill->dev); |
| 652 | rfkill_remove_switch(rfkill); |
Michael Buesch | 8a8f1c0 | 2007-10-28 13:07:54 +0100 | [diff] [blame] | 653 | rfkill_led_trigger_unregister(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 654 | put_device(&rfkill->dev); |
| 655 | } |
| 656 | EXPORT_SYMBOL(rfkill_unregister); |
| 657 | |
| 658 | /* |
| 659 | * Rfkill module initialization/deinitialization. |
| 660 | */ |
| 661 | static int __init rfkill_init(void) |
| 662 | { |
| 663 | int error; |
| 664 | int i; |
| 665 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 666 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
| 667 | if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED && |
| 668 | rfkill_default_state != RFKILL_STATE_UNBLOCKED) |
Henrique de Moraes Holschuh | e954b0b | 2008-06-23 17:22:59 -0300 | [diff] [blame] | 669 | return -EINVAL; |
| 670 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 671 | for (i = 0; i < ARRAY_SIZE(rfkill_states); i++) |
Henrique de Moraes Holschuh | e954b0b | 2008-06-23 17:22:59 -0300 | [diff] [blame] | 672 | rfkill_states[i] = rfkill_default_state; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 673 | |
| 674 | error = class_register(&rfkill_class); |
| 675 | if (error) { |
| 676 | printk(KERN_ERR "rfkill: unable to register rfkill class\n"); |
| 677 | return error; |
| 678 | } |
| 679 | |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 680 | register_rfkill_notifier(&rfkill_blocking_uevent_nb); |
| 681 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static void __exit rfkill_exit(void) |
| 686 | { |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 687 | unregister_rfkill_notifier(&rfkill_blocking_uevent_nb); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 688 | class_unregister(&rfkill_class); |
| 689 | } |
| 690 | |
Michael Buesch | 2bf236d | 2007-10-28 14:39:02 +0100 | [diff] [blame] | 691 | subsys_initcall(rfkill_init); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 692 | module_exit(rfkill_exit); |