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