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