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