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