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 | |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 47 | struct rfkill_gsw_state { |
| 48 | enum rfkill_state current_state; |
| 49 | enum rfkill_state default_state; |
| 50 | }; |
| 51 | |
| 52 | static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX]; |
| 53 | static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 54 | |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 55 | static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list); |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * register_rfkill_notifier - Add notifier to rfkill notifier chain |
| 60 | * @nb: pointer to the new entry to add to the chain |
| 61 | * |
| 62 | * See blocking_notifier_chain_register() for return value and further |
| 63 | * observations. |
| 64 | * |
| 65 | * Adds a notifier to the rfkill notifier chain. The chain will be |
| 66 | * called with a pointer to the relevant rfkill structure as a parameter, |
| 67 | * refer to include/linux/rfkill.h for the possible events. |
| 68 | * |
| 69 | * Notifiers added to this chain are to always return NOTIFY_DONE. This |
| 70 | * chain is a blocking notifier chain: notifiers can sleep. |
| 71 | * |
| 72 | * Calls to this chain may have been done through a workqueue. One must |
| 73 | * assume unordered asynchronous behaviour, there is no way to know if |
| 74 | * actions related to the event that generated the notification have been |
| 75 | * carried out already. |
| 76 | */ |
| 77 | int register_rfkill_notifier(struct notifier_block *nb) |
| 78 | { |
| 79 | return blocking_notifier_chain_register(&rfkill_notifier_list, nb); |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(register_rfkill_notifier); |
| 82 | |
| 83 | /** |
| 84 | * unregister_rfkill_notifier - remove notifier from rfkill notifier chain |
| 85 | * @nb: pointer to the entry to remove from the chain |
| 86 | * |
| 87 | * See blocking_notifier_chain_unregister() for return value and further |
| 88 | * observations. |
| 89 | * |
| 90 | * Removes a notifier from the rfkill notifier chain. |
| 91 | */ |
| 92 | int unregister_rfkill_notifier(struct notifier_block *nb) |
| 93 | { |
| 94 | return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb); |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(unregister_rfkill_notifier); |
| 97 | |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 98 | |
| 99 | static void rfkill_led_trigger(struct rfkill *rfkill, |
| 100 | enum rfkill_state state) |
| 101 | { |
| 102 | #ifdef CONFIG_RFKILL_LEDS |
| 103 | struct led_trigger *led = &rfkill->led_trigger; |
| 104 | |
| 105 | if (!led->name) |
| 106 | return; |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 107 | if (state != RFKILL_STATE_UNBLOCKED) |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 108 | led_trigger_event(led, LED_OFF); |
| 109 | else |
| 110 | led_trigger_event(led, LED_FULL); |
| 111 | #endif /* CONFIG_RFKILL_LEDS */ |
| 112 | } |
| 113 | |
Dmitry Baryshkov | 9618566 | 2008-07-22 14:21:59 +0400 | [diff] [blame] | 114 | #ifdef CONFIG_RFKILL_LEDS |
| 115 | static void rfkill_led_trigger_activate(struct led_classdev *led) |
| 116 | { |
| 117 | struct rfkill *rfkill = container_of(led->trigger, |
| 118 | struct rfkill, led_trigger); |
| 119 | |
| 120 | rfkill_led_trigger(rfkill, rfkill->state); |
| 121 | } |
| 122 | #endif /* CONFIG_RFKILL_LEDS */ |
| 123 | |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 124 | static void notify_rfkill_state_change(struct rfkill *rfkill) |
| 125 | { |
| 126 | blocking_notifier_call_chain(&rfkill_notifier_list, |
| 127 | RFKILL_STATE_CHANGED, |
| 128 | rfkill); |
| 129 | } |
| 130 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 131 | static void update_rfkill_state(struct rfkill *rfkill) |
| 132 | { |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 133 | enum rfkill_state newstate, oldstate; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 134 | |
| 135 | if (rfkill->get_state) { |
| 136 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 137 | if (!rfkill->get_state(rfkill->data, &newstate)) { |
| 138 | oldstate = rfkill->state; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 139 | rfkill->state = newstate; |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 140 | if (oldstate != newstate) |
| 141 | notify_rfkill_state_change(rfkill); |
| 142 | } |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 143 | mutex_unlock(&rfkill->mutex); |
| 144 | } |
| 145 | } |
| 146 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 147 | /** |
| 148 | * rfkill_toggle_radio - wrapper for toggle_radio hook |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 149 | * @rfkill: the rfkill struct to use |
| 150 | * @force: calls toggle_radio even if cache says it is not needed, |
| 151 | * and also makes sure notifications of the state will be |
| 152 | * sent even if it didn't change |
| 153 | * @state: the new state to call toggle_radio() with |
| 154 | * |
Henrique de Moraes Holschuh | 0f687e9 | 2008-07-03 13:14:56 -0300 | [diff] [blame] | 155 | * Calls rfkill->toggle_radio, enforcing the API for toggle_radio |
| 156 | * calls and handling all the red tape such as issuing notifications |
| 157 | * if the call is successful. |
| 158 | * |
Henrique de Moraes Holschuh | e10e0df | 2008-08-02 14:56:25 -0300 | [diff] [blame] | 159 | * Suspended devices are not touched at all, and -EAGAIN is returned. |
| 160 | * |
Henrique de Moraes Holschuh | 435307a | 2008-07-21 21:18:22 -0300 | [diff] [blame] | 161 | * Note that the @force parameter cannot override a (possibly cached) |
| 162 | * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 163 | * RFKILL_STATE_HARD_BLOCKED implements either get_state() or |
| 164 | * rfkill_force_state(), so the cache either is bypassed or valid. |
| 165 | * |
| 166 | * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED |
| 167 | * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to |
| 168 | * give the driver a hint that it should double-BLOCK the transmitter. |
| 169 | * |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 170 | * Caller must have acquired rfkill->mutex. |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 171 | */ |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 172 | static int rfkill_toggle_radio(struct rfkill *rfkill, |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 173 | enum rfkill_state state, |
| 174 | int force) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 175 | { |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 176 | int retval = 0; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 177 | enum rfkill_state oldstate, newstate; |
| 178 | |
Henrique de Moraes Holschuh | e10e0df | 2008-08-02 14:56:25 -0300 | [diff] [blame] | 179 | if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP)) |
| 180 | return -EBUSY; |
| 181 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 182 | oldstate = rfkill->state; |
| 183 | |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 184 | if (rfkill->get_state && !force && |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 185 | !rfkill->get_state(rfkill->data, &newstate)) |
| 186 | rfkill->state = newstate; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 187 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 188 | switch (state) { |
| 189 | case RFKILL_STATE_HARD_BLOCKED: |
| 190 | /* typically happens when refreshing hardware state, |
| 191 | * such as on resume */ |
| 192 | state = RFKILL_STATE_SOFT_BLOCKED; |
| 193 | break; |
| 194 | case RFKILL_STATE_UNBLOCKED: |
| 195 | /* force can't override this, only rfkill_force_state() can */ |
| 196 | if (rfkill->state == RFKILL_STATE_HARD_BLOCKED) |
| 197 | return -EPERM; |
| 198 | break; |
| 199 | case RFKILL_STATE_SOFT_BLOCKED: |
| 200 | /* nothing to do, we want to give drivers the hint to double |
| 201 | * BLOCK even a transmitter that is already in state |
| 202 | * RFKILL_STATE_HARD_BLOCKED */ |
| 203 | break; |
| 204 | } |
| 205 | |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 206 | if (force || state != rfkill->state) { |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 207 | retval = rfkill->toggle_radio(rfkill->data, state); |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 208 | /* never allow a HARD->SOFT downgrade! */ |
| 209 | if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 210 | rfkill->state = state; |
| 211 | } |
| 212 | |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 213 | if (force || rfkill->state != oldstate) { |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 214 | rfkill_led_trigger(rfkill, rfkill->state); |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 215 | notify_rfkill_state_change(rfkill); |
| 216 | } |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 217 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 218 | return retval; |
| 219 | } |
| 220 | |
| 221 | /** |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 222 | * __rfkill_switch_all - Toggle state of all switches of given type |
Henrique de Moraes Holschuh | 435307a | 2008-07-21 21:18:22 -0300 | [diff] [blame] | 223 | * @type: type of interfaces to be affected |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 224 | * @state: the new state |
| 225 | * |
Henrique de Moraes Holschuh | 435307a | 2008-07-21 21:18:22 -0300 | [diff] [blame] | 226 | * This function toggles the state of all switches of given type, |
| 227 | * unless a specific switch is claimed by userspace (in which case, |
Henrique de Moraes Holschuh | e10e0df | 2008-08-02 14:56:25 -0300 | [diff] [blame] | 228 | * that switch is left alone) or suspended. |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 229 | * |
| 230 | * Caller must have acquired rfkill_mutex. |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 231 | */ |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 232 | static void __rfkill_switch_all(const enum rfkill_type type, |
| 233 | const enum rfkill_state state) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 234 | { |
| 235 | struct rfkill *rfkill; |
| 236 | |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 237 | rfkill_global_states[type].current_state = state; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 238 | list_for_each_entry(rfkill, &rfkill_list, node) { |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 239 | if ((!rfkill->user_claim) && (rfkill->type == type)) { |
| 240 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 241 | rfkill_toggle_radio(rfkill, state, 0); |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 242 | mutex_unlock(&rfkill->mutex); |
| 243 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 244 | } |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 245 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 246 | |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 247 | /** |
| 248 | * rfkill_switch_all - Toggle state of all switches of given type |
| 249 | * @type: type of interfaces to be affected |
| 250 | * @state: the new state |
| 251 | * |
| 252 | * Acquires rfkill_mutex and calls __rfkill_switch_all(@type, @state). |
| 253 | * Please refer to __rfkill_switch_all() for details. |
| 254 | */ |
| 255 | void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state) |
| 256 | { |
| 257 | mutex_lock(&rfkill_mutex); |
| 258 | __rfkill_switch_all(type, state); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 259 | mutex_unlock(&rfkill_mutex); |
| 260 | } |
| 261 | EXPORT_SYMBOL(rfkill_switch_all); |
| 262 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 263 | /** |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 264 | * rfkill_epo - emergency power off all transmitters |
| 265 | * |
Henrique de Moraes Holschuh | e10e0df | 2008-08-02 14:56:25 -0300 | [diff] [blame] | 266 | * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED, |
| 267 | * ignoring everything in its path but rfkill_mutex and rfkill->mutex. |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 268 | * |
| 269 | * The global state before the EPO is saved and can be restored later |
| 270 | * using rfkill_restore_states(). |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 271 | */ |
| 272 | void rfkill_epo(void) |
| 273 | { |
| 274 | struct rfkill *rfkill; |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 275 | int i; |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 276 | |
| 277 | mutex_lock(&rfkill_mutex); |
| 278 | list_for_each_entry(rfkill, &rfkill_list, node) { |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 279 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 280 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 281 | mutex_unlock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 282 | } |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 283 | for (i = 0; i < RFKILL_TYPE_MAX; i++) { |
| 284 | rfkill_global_states[i].default_state = |
| 285 | rfkill_global_states[i].current_state; |
| 286 | rfkill_global_states[i].current_state = |
| 287 | RFKILL_STATE_SOFT_BLOCKED; |
| 288 | } |
Henrique de Moraes Holschuh | 4081f00 | 2008-06-23 17:23:07 -0300 | [diff] [blame] | 289 | mutex_unlock(&rfkill_mutex); |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(rfkill_epo); |
| 292 | |
| 293 | /** |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 294 | * rfkill_restore_states - restore global states |
| 295 | * |
| 296 | * Restore (and sync switches to) the global state from the |
| 297 | * states in rfkill_default_states. This can undo the effects of |
| 298 | * a call to rfkill_epo(). |
| 299 | */ |
| 300 | void rfkill_restore_states(void) |
| 301 | { |
| 302 | int i; |
| 303 | |
| 304 | mutex_lock(&rfkill_mutex); |
| 305 | for (i = 0; i < RFKILL_TYPE_MAX; i++) |
| 306 | __rfkill_switch_all(i, rfkill_global_states[i].default_state); |
| 307 | mutex_unlock(&rfkill_mutex); |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(rfkill_restore_states); |
| 310 | |
| 311 | /** |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 312 | * rfkill_force_state - Force the internal rfkill radio state |
| 313 | * @rfkill: pointer to the rfkill class to modify. |
| 314 | * @state: the current radio state the class should be forced to. |
| 315 | * |
| 316 | * This function updates the internal state of the radio cached |
| 317 | * by the rfkill class. It should be used when the driver gets |
| 318 | * a notification by the firmware/hardware of the current *real* |
| 319 | * state of the radio rfkill switch. |
| 320 | * |
Henrique de Moraes Holschuh | 2fd9b22 | 2008-07-21 21:18:17 -0300 | [diff] [blame] | 321 | * Devices which are subject to external changes on their rfkill |
| 322 | * state (such as those caused by a hardware rfkill line) MUST |
| 323 | * have their driver arrange to call rfkill_force_state() as soon |
| 324 | * as possible after such a change. |
| 325 | * |
| 326 | * This function may not be called from an atomic context. |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 327 | */ |
| 328 | int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state) |
| 329 | { |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 330 | enum rfkill_state oldstate; |
| 331 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 332 | if (state != RFKILL_STATE_SOFT_BLOCKED && |
| 333 | state != RFKILL_STATE_UNBLOCKED && |
| 334 | state != RFKILL_STATE_HARD_BLOCKED) |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | |
| 337 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 338 | |
| 339 | oldstate = rfkill->state; |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 340 | rfkill->state = state; |
Henrique de Moraes Holschuh | 79399a8 | 2008-06-23 17:23:03 -0300 | [diff] [blame] | 341 | |
| 342 | if (state != oldstate) |
| 343 | notify_rfkill_state_change(rfkill); |
| 344 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 345 | mutex_unlock(&rfkill->mutex); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | EXPORT_SYMBOL(rfkill_force_state); |
| 350 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 351 | static ssize_t rfkill_name_show(struct device *dev, |
| 352 | struct device_attribute *attr, |
| 353 | char *buf) |
| 354 | { |
| 355 | struct rfkill *rfkill = to_rfkill(dev); |
| 356 | |
| 357 | return sprintf(buf, "%s\n", rfkill->name); |
| 358 | } |
| 359 | |
Henrique de Moraes Holschuh | 99c632e | 2008-06-23 17:23:04 -0300 | [diff] [blame] | 360 | static const char *rfkill_get_type_str(enum rfkill_type type) |
| 361 | { |
| 362 | switch (type) { |
| 363 | case RFKILL_TYPE_WLAN: |
| 364 | return "wlan"; |
| 365 | case RFKILL_TYPE_BLUETOOTH: |
| 366 | return "bluetooth"; |
| 367 | case RFKILL_TYPE_UWB: |
| 368 | return "ultrawideband"; |
| 369 | case RFKILL_TYPE_WIMAX: |
| 370 | return "wimax"; |
| 371 | case RFKILL_TYPE_WWAN: |
| 372 | return "wwan"; |
| 373 | default: |
| 374 | BUG(); |
| 375 | } |
| 376 | } |
| 377 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 378 | static ssize_t rfkill_type_show(struct device *dev, |
| 379 | struct device_attribute *attr, |
| 380 | char *buf) |
| 381 | { |
| 382 | struct rfkill *rfkill = to_rfkill(dev); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 383 | |
Henrique de Moraes Holschuh | 99c632e | 2008-06-23 17:23:04 -0300 | [diff] [blame] | 384 | return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type)); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static ssize_t rfkill_state_show(struct device *dev, |
| 388 | struct device_attribute *attr, |
| 389 | char *buf) |
| 390 | { |
| 391 | struct rfkill *rfkill = to_rfkill(dev); |
| 392 | |
Henrique de Moraes Holschuh | 801e49a | 2008-06-23 17:23:00 -0300 | [diff] [blame] | 393 | update_rfkill_state(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 394 | return sprintf(buf, "%d\n", rfkill->state); |
| 395 | } |
| 396 | |
| 397 | static ssize_t rfkill_state_store(struct device *dev, |
| 398 | struct device_attribute *attr, |
| 399 | const char *buf, size_t count) |
| 400 | { |
| 401 | struct rfkill *rfkill = to_rfkill(dev); |
| 402 | unsigned int state = simple_strtoul(buf, NULL, 0); |
| 403 | int error; |
| 404 | |
| 405 | if (!capable(CAP_NET_ADMIN)) |
| 406 | return -EPERM; |
| 407 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 408 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
| 409 | if (state != RFKILL_STATE_UNBLOCKED && |
| 410 | state != RFKILL_STATE_SOFT_BLOCKED) |
| 411 | return -EINVAL; |
| 412 | |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 413 | if (mutex_lock_interruptible(&rfkill->mutex)) |
| 414 | return -ERESTARTSYS; |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 415 | error = rfkill_toggle_radio(rfkill, state, 0); |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 416 | mutex_unlock(&rfkill->mutex); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 417 | |
Michael Buesch | 7f4c534 | 2007-11-28 17:49:34 +0100 | [diff] [blame] | 418 | return error ? error : count; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static ssize_t rfkill_claim_show(struct device *dev, |
| 422 | struct device_attribute *attr, |
| 423 | char *buf) |
| 424 | { |
| 425 | struct rfkill *rfkill = to_rfkill(dev); |
| 426 | |
| 427 | return sprintf(buf, "%d", rfkill->user_claim); |
| 428 | } |
| 429 | |
| 430 | static ssize_t rfkill_claim_store(struct device *dev, |
| 431 | struct device_attribute *attr, |
| 432 | const char *buf, size_t count) |
| 433 | { |
| 434 | struct rfkill *rfkill = to_rfkill(dev); |
| 435 | bool claim = !!simple_strtoul(buf, NULL, 0); |
| 436 | int error; |
| 437 | |
| 438 | if (!capable(CAP_NET_ADMIN)) |
| 439 | return -EPERM; |
| 440 | |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 441 | if (rfkill->user_claim_unsupported) |
| 442 | return -EOPNOTSUPP; |
| 443 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 444 | /* |
| 445 | * Take the global lock to make sure the kernel is not in |
| 446 | * the middle of rfkill_switch_all |
| 447 | */ |
| 448 | error = mutex_lock_interruptible(&rfkill_mutex); |
| 449 | if (error) |
| 450 | return error; |
| 451 | |
| 452 | if (rfkill->user_claim != claim) { |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 453 | if (!claim) { |
| 454 | mutex_lock(&rfkill->mutex); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 455 | rfkill_toggle_radio(rfkill, |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 456 | rfkill_global_states[rfkill->type].current_state, |
| 457 | 0); |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 458 | mutex_unlock(&rfkill->mutex); |
| 459 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 460 | rfkill->user_claim = claim; |
| 461 | } |
| 462 | |
| 463 | mutex_unlock(&rfkill_mutex); |
| 464 | |
Michael Buesch | 20405c0 | 2007-09-27 21:34:23 +0200 | [diff] [blame] | 465 | return error ? error : count; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static struct device_attribute rfkill_dev_attrs[] = { |
| 469 | __ATTR(name, S_IRUGO, rfkill_name_show, NULL), |
| 470 | __ATTR(type, S_IRUGO, rfkill_type_show, NULL), |
Ivo van Doorn | c81de6a | 2007-07-18 15:38:03 -0700 | [diff] [blame] | 471 | __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] | 472 | __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store), |
| 473 | __ATTR_NULL |
| 474 | }; |
| 475 | |
| 476 | static void rfkill_release(struct device *dev) |
| 477 | { |
| 478 | struct rfkill *rfkill = to_rfkill(dev); |
| 479 | |
| 480 | kfree(rfkill); |
| 481 | module_put(THIS_MODULE); |
| 482 | } |
| 483 | |
| 484 | #ifdef CONFIG_PM |
| 485 | static int rfkill_suspend(struct device *dev, pm_message_t state) |
| 486 | { |
| 487 | struct rfkill *rfkill = to_rfkill(dev); |
| 488 | |
| 489 | if (dev->power.power_state.event != state.event) { |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 490 | if (state.event & PM_EVENT_SLEEP) { |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 491 | /* Stop transmitter, keep state, no notifies */ |
| 492 | update_rfkill_state(rfkill); |
| 493 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 494 | mutex_lock(&rfkill->mutex); |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 495 | rfkill->toggle_radio(rfkill->data, |
| 496 | RFKILL_STATE_SOFT_BLOCKED); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 497 | mutex_unlock(&rfkill->mutex); |
| 498 | } |
| 499 | |
| 500 | dev->power.power_state = state; |
| 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int rfkill_resume(struct device *dev) |
| 507 | { |
| 508 | struct rfkill *rfkill = to_rfkill(dev); |
| 509 | |
| 510 | if (dev->power.power_state.event != PM_EVENT_ON) { |
| 511 | mutex_lock(&rfkill->mutex); |
| 512 | |
Henrique de Moraes Holschuh | e10e0df | 2008-08-02 14:56:25 -0300 | [diff] [blame] | 513 | dev->power.power_state.event = PM_EVENT_ON; |
| 514 | |
Henrique de Moraes Holschuh | 526324b | 2008-06-23 17:23:02 -0300 | [diff] [blame] | 515 | /* restore radio state AND notify everybody */ |
| 516 | rfkill_toggle_radio(rfkill, rfkill->state, 1); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 517 | |
| 518 | mutex_unlock(&rfkill->mutex); |
| 519 | } |
| 520 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | #else |
| 524 | #define rfkill_suspend NULL |
| 525 | #define rfkill_resume NULL |
| 526 | #endif |
| 527 | |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 528 | static int rfkill_blocking_uevent_notifier(struct notifier_block *nb, |
| 529 | unsigned long eventid, |
| 530 | void *data) |
| 531 | { |
| 532 | struct rfkill *rfkill = (struct rfkill *)data; |
| 533 | |
| 534 | switch (eventid) { |
| 535 | case RFKILL_STATE_CHANGED: |
| 536 | kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE); |
| 537 | break; |
| 538 | default: |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | return NOTIFY_DONE; |
| 543 | } |
| 544 | |
| 545 | static struct notifier_block rfkill_blocking_uevent_nb = { |
| 546 | .notifier_call = rfkill_blocking_uevent_notifier, |
| 547 | .priority = 0, |
| 548 | }; |
| 549 | |
| 550 | static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 551 | { |
| 552 | struct rfkill *rfkill = to_rfkill(dev); |
| 553 | int error; |
| 554 | |
| 555 | error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name); |
| 556 | if (error) |
| 557 | return error; |
| 558 | error = add_uevent_var(env, "RFKILL_TYPE=%s", |
| 559 | rfkill_get_type_str(rfkill->type)); |
| 560 | if (error) |
| 561 | return error; |
| 562 | error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state); |
| 563 | return error; |
| 564 | } |
| 565 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 566 | static struct class rfkill_class = { |
| 567 | .name = "rfkill", |
| 568 | .dev_release = rfkill_release, |
| 569 | .dev_attrs = rfkill_dev_attrs, |
| 570 | .suspend = rfkill_suspend, |
| 571 | .resume = rfkill_resume, |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 572 | .dev_uevent = rfkill_dev_uevent, |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 573 | }; |
| 574 | |
Henrique de Moraes Holschuh | 02589f6 | 2008-08-02 15:10:57 -0300 | [diff] [blame] | 575 | static int rfkill_check_duplicity(const struct rfkill *rfkill) |
| 576 | { |
| 577 | struct rfkill *p; |
| 578 | unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; |
| 579 | |
| 580 | memset(seen, 0, sizeof(seen)); |
| 581 | |
| 582 | list_for_each_entry(p, &rfkill_list, node) { |
| 583 | if (p == rfkill) { |
| 584 | WARN_ON(1); |
| 585 | return -EEXIST; |
| 586 | } |
| 587 | set_bit(p->type, seen); |
| 588 | } |
| 589 | |
| 590 | /* 0: first switch of its kind */ |
| 591 | return test_bit(rfkill->type, seen); |
| 592 | } |
| 593 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 594 | static int rfkill_add_switch(struct rfkill *rfkill) |
| 595 | { |
Henrique de Moraes Holschuh | 02589f6 | 2008-08-02 15:10:57 -0300 | [diff] [blame] | 596 | int error; |
| 597 | |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame] | 598 | mutex_lock(&rfkill_mutex); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 599 | |
Henrique de Moraes Holschuh | 02589f6 | 2008-08-02 15:10:57 -0300 | [diff] [blame] | 600 | error = rfkill_check_duplicity(rfkill); |
| 601 | if (error < 0) |
| 602 | goto unlock_out; |
| 603 | |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 604 | if (!error) { |
| 605 | /* lock default after first use */ |
| 606 | set_bit(rfkill->type, rfkill_states_lockdflt); |
| 607 | rfkill_global_states[rfkill->type].current_state = |
| 608 | rfkill_global_states[rfkill->type].default_state; |
| 609 | } |
| 610 | |
| 611 | rfkill_toggle_radio(rfkill, |
| 612 | rfkill_global_states[rfkill->type].current_state, |
| 613 | 0); |
Henrique de Moraes Holschuh | fd4484a | 2008-07-03 13:14:57 -0300 | [diff] [blame] | 614 | |
| 615 | list_add_tail(&rfkill->node, &rfkill_list); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 616 | |
Henrique de Moraes Holschuh | 02589f6 | 2008-08-02 15:10:57 -0300 | [diff] [blame] | 617 | error = 0; |
| 618 | unlock_out: |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 619 | mutex_unlock(&rfkill_mutex); |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame] | 620 | |
Henrique de Moraes Holschuh | 02589f6 | 2008-08-02 15:10:57 -0300 | [diff] [blame] | 621 | return error; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static void rfkill_remove_switch(struct rfkill *rfkill) |
| 625 | { |
| 626 | mutex_lock(&rfkill_mutex); |
| 627 | list_del_init(&rfkill->node); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 628 | mutex_unlock(&rfkill_mutex); |
Henrique de Moraes Holschuh | 064af11 | 2008-07-21 21:18:20 -0300 | [diff] [blame] | 629 | |
| 630 | mutex_lock(&rfkill->mutex); |
| 631 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); |
| 632 | mutex_unlock(&rfkill->mutex); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
| 636 | * rfkill_allocate - allocate memory for rfkill structure. |
| 637 | * @parent: device that has rf switch on it |
Ivo van Doorn | 234a0ca | 2007-09-13 09:20:42 +0200 | [diff] [blame] | 638 | * @type: type of the switch (RFKILL_TYPE_*) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 639 | * |
| 640 | * This function should be called by the network driver when it needs |
Henrique de Moraes Holschuh | 435307a | 2008-07-21 21:18:22 -0300 | [diff] [blame] | 641 | * rfkill structure. Once the structure is allocated the driver should |
| 642 | * finish its initialization by setting the name, private data, enable_radio |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 643 | * and disable_radio methods and then register it with rfkill_register(). |
Henrique de Moraes Holschuh | 435307a | 2008-07-21 21:18:22 -0300 | [diff] [blame] | 644 | * |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 645 | * NOTE: If registration fails the structure shoudl be freed by calling |
| 646 | * rfkill_free() otherwise rfkill_unregister() should be used. |
| 647 | */ |
Henrique de Moraes Holschuh | 77fba13 | 2008-08-02 15:10:59 -0300 | [diff] [blame^] | 648 | struct rfkill * __must_check rfkill_allocate(struct device *parent, |
| 649 | enum rfkill_type type) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 650 | { |
| 651 | struct rfkill *rfkill; |
| 652 | struct device *dev; |
| 653 | |
| 654 | rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL); |
Ivo van Doorn | d007da1 | 2007-05-19 12:24:39 -0700 | [diff] [blame] | 655 | if (!rfkill) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 656 | return NULL; |
| 657 | |
| 658 | mutex_init(&rfkill->mutex); |
| 659 | INIT_LIST_HEAD(&rfkill->node); |
| 660 | rfkill->type = type; |
| 661 | |
| 662 | dev = &rfkill->dev; |
| 663 | dev->class = &rfkill_class; |
| 664 | dev->parent = parent; |
| 665 | device_initialize(dev); |
| 666 | |
| 667 | __module_get(THIS_MODULE); |
| 668 | |
| 669 | return rfkill; |
| 670 | } |
| 671 | EXPORT_SYMBOL(rfkill_allocate); |
| 672 | |
| 673 | /** |
| 674 | * rfkill_free - Mark rfkill structure for deletion |
| 675 | * @rfkill: rfkill structure to be destroyed |
| 676 | * |
Henrique de Moraes Holschuh | 435307a | 2008-07-21 21:18:22 -0300 | [diff] [blame] | 677 | * Decrements reference count of the rfkill structure so it is destroyed. |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 678 | * Note that rfkill_free() should _not_ be called after rfkill_unregister(). |
| 679 | */ |
| 680 | void rfkill_free(struct rfkill *rfkill) |
| 681 | { |
| 682 | if (rfkill) |
| 683 | put_device(&rfkill->dev); |
| 684 | } |
| 685 | EXPORT_SYMBOL(rfkill_free); |
| 686 | |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 687 | static void rfkill_led_trigger_register(struct rfkill *rfkill) |
| 688 | { |
| 689 | #ifdef CONFIG_RFKILL_LEDS |
| 690 | int error; |
| 691 | |
Dmitry Baryshkov | 7c4f457 | 2008-07-22 14:17:37 +0400 | [diff] [blame] | 692 | if (!rfkill->led_trigger.name) |
| 693 | rfkill->led_trigger.name = rfkill->dev.bus_id; |
Dmitry Baryshkov | 9618566 | 2008-07-22 14:21:59 +0400 | [diff] [blame] | 694 | if (!rfkill->led_trigger.activate) |
| 695 | rfkill->led_trigger.activate = rfkill_led_trigger_activate; |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 696 | error = led_trigger_register(&rfkill->led_trigger); |
| 697 | if (error) |
| 698 | rfkill->led_trigger.name = NULL; |
| 699 | #endif /* CONFIG_RFKILL_LEDS */ |
| 700 | } |
| 701 | |
| 702 | static void rfkill_led_trigger_unregister(struct rfkill *rfkill) |
| 703 | { |
| 704 | #ifdef CONFIG_RFKILL_LEDS |
Henrique de Moraes Holschuh | 37f55e9 | 2008-07-21 21:18:18 -0300 | [diff] [blame] | 705 | if (rfkill->led_trigger.name) { |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 706 | led_trigger_unregister(&rfkill->led_trigger); |
Henrique de Moraes Holschuh | 37f55e9 | 2008-07-21 21:18:18 -0300 | [diff] [blame] | 707 | rfkill->led_trigger.name = NULL; |
| 708 | } |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 709 | #endif |
| 710 | } |
| 711 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 712 | /** |
| 713 | * rfkill_register - Register a rfkill structure. |
| 714 | * @rfkill: rfkill structure to be registered |
| 715 | * |
| 716 | * This function should be called by the network driver when the rfkill |
| 717 | * structure needs to be registered. Immediately from registration the |
| 718 | * switch driver should be able to service calls to toggle_radio. |
| 719 | */ |
Henrique de Moraes Holschuh | 77fba13 | 2008-08-02 15:10:59 -0300 | [diff] [blame^] | 720 | int __must_check rfkill_register(struct rfkill *rfkill) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 721 | { |
| 722 | static atomic_t rfkill_no = ATOMIC_INIT(0); |
| 723 | struct device *dev = &rfkill->dev; |
| 724 | int error; |
| 725 | |
| 726 | if (!rfkill->toggle_radio) |
| 727 | return -EINVAL; |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame] | 728 | if (rfkill->type >= RFKILL_TYPE_MAX) |
| 729 | return -EINVAL; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 730 | |
Michael Buesch | 8a8f1c0 | 2007-10-28 13:07:54 +0100 | [diff] [blame] | 731 | snprintf(dev->bus_id, sizeof(dev->bus_id), |
| 732 | "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1); |
| 733 | |
| 734 | rfkill_led_trigger_register(rfkill); |
| 735 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 736 | error = rfkill_add_switch(rfkill); |
Eric Paris | 632041f | 2008-01-13 16:20:56 -0500 | [diff] [blame] | 737 | if (error) { |
| 738 | rfkill_led_trigger_unregister(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 739 | return error; |
Eric Paris | 632041f | 2008-01-13 16:20:56 -0500 | [diff] [blame] | 740 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 741 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 742 | error = device_add(dev); |
| 743 | if (error) { |
| 744 | rfkill_remove_switch(rfkill); |
Henrique de Moraes Holschuh | 37f55e9 | 2008-07-21 21:18:18 -0300 | [diff] [blame] | 745 | rfkill_led_trigger_unregister(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 746 | return error; |
| 747 | } |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | EXPORT_SYMBOL(rfkill_register); |
| 752 | |
| 753 | /** |
Henrique de Moraes Holschuh | c8fcd90 | 2008-06-23 17:22:57 -0300 | [diff] [blame] | 754 | * rfkill_unregister - Unregister a rfkill structure. |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 755 | * @rfkill: rfkill structure to be unregistered |
| 756 | * |
| 757 | * This function should be called by the network driver during device |
| 758 | * teardown to destroy rfkill structure. Note that rfkill_free() should |
| 759 | * _not_ be called after rfkill_unregister(). |
| 760 | */ |
| 761 | void rfkill_unregister(struct rfkill *rfkill) |
| 762 | { |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 763 | device_del(&rfkill->dev); |
| 764 | rfkill_remove_switch(rfkill); |
Michael Buesch | 8a8f1c0 | 2007-10-28 13:07:54 +0100 | [diff] [blame] | 765 | rfkill_led_trigger_unregister(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 766 | put_device(&rfkill->dev); |
| 767 | } |
| 768 | EXPORT_SYMBOL(rfkill_unregister); |
| 769 | |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 770 | /** |
| 771 | * rfkill_set_default - set initial value for a switch type |
| 772 | * @type - the type of switch to set the default state of |
| 773 | * @state - the new default state for that group of switches |
| 774 | * |
| 775 | * Sets the initial state rfkill should use for a given type. |
| 776 | * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED |
| 777 | * and RFKILL_STATE_UNBLOCKED. |
| 778 | * |
| 779 | * This function is meant to be used by platform drivers for platforms |
| 780 | * that can save switch state across power down/reboot. |
| 781 | * |
| 782 | * The default state for each switch type can be changed exactly once. |
| 783 | * After a switch of that type is registered, the default state cannot |
| 784 | * be changed anymore. This guards against multiple drivers it the |
| 785 | * same platform trying to set the initial switch default state, which |
| 786 | * is not allowed. |
| 787 | * |
| 788 | * Returns -EPERM if the state has already been set once or is in use, |
| 789 | * so drivers likely want to either ignore or at most printk(KERN_NOTICE) |
| 790 | * if this function returns -EPERM. |
| 791 | * |
| 792 | * Returns 0 if the new default state was set, or an error if it |
| 793 | * could not be set. |
| 794 | */ |
| 795 | int rfkill_set_default(enum rfkill_type type, enum rfkill_state state) |
| 796 | { |
| 797 | int error; |
| 798 | |
| 799 | if (type >= RFKILL_TYPE_MAX || |
| 800 | (state != RFKILL_STATE_SOFT_BLOCKED && |
| 801 | state != RFKILL_STATE_UNBLOCKED)) |
| 802 | return -EINVAL; |
| 803 | |
| 804 | mutex_lock(&rfkill_mutex); |
| 805 | |
| 806 | if (!test_and_set_bit(type, rfkill_states_lockdflt)) { |
| 807 | rfkill_global_states[type].default_state = state; |
| 808 | error = 0; |
| 809 | } else |
| 810 | error = -EPERM; |
| 811 | |
| 812 | mutex_unlock(&rfkill_mutex); |
| 813 | return error; |
| 814 | } |
| 815 | EXPORT_SYMBOL_GPL(rfkill_set_default); |
| 816 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 817 | /* |
| 818 | * Rfkill module initialization/deinitialization. |
| 819 | */ |
| 820 | static int __init rfkill_init(void) |
| 821 | { |
| 822 | int error; |
| 823 | int i; |
| 824 | |
Henrique de Moraes Holschuh | 5005657 | 2008-06-23 17:46:42 -0300 | [diff] [blame] | 825 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
| 826 | if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED && |
| 827 | rfkill_default_state != RFKILL_STATE_UNBLOCKED) |
Henrique de Moraes Holschuh | e954b0b | 2008-06-23 17:22:59 -0300 | [diff] [blame] | 828 | return -EINVAL; |
| 829 | |
Henrique de Moraes Holschuh | 9961920 | 2008-08-02 15:10:58 -0300 | [diff] [blame] | 830 | for (i = 0; i < RFKILL_TYPE_MAX; i++) |
| 831 | rfkill_global_states[i].default_state = rfkill_default_state; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 832 | |
| 833 | error = class_register(&rfkill_class); |
| 834 | if (error) { |
| 835 | printk(KERN_ERR "rfkill: unable to register rfkill class\n"); |
| 836 | return error; |
| 837 | } |
| 838 | |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 839 | register_rfkill_notifier(&rfkill_blocking_uevent_nb); |
| 840 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | static void __exit rfkill_exit(void) |
| 845 | { |
Henrique de Moraes Holschuh | ffb67c3 | 2008-06-23 17:23:05 -0300 | [diff] [blame] | 846 | unregister_rfkill_notifier(&rfkill_blocking_uevent_nb); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 847 | class_unregister(&rfkill_class); |
| 848 | } |
| 849 | |
Michael Buesch | 2bf236d | 2007-10-28 14:39:02 +0100 | [diff] [blame] | 850 | subsys_initcall(rfkill_init); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 851 | module_exit(rfkill_exit); |