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 | |
| 30 | MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>"); |
| 31 | MODULE_VERSION("1.0"); |
| 32 | MODULE_DESCRIPTION("RF switch support"); |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | |
| 35 | static LIST_HEAD(rfkill_list); /* list of registered rf switches */ |
| 36 | static DEFINE_MUTEX(rfkill_mutex); |
| 37 | |
| 38 | static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX]; |
| 39 | |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 40 | |
| 41 | static void rfkill_led_trigger(struct rfkill *rfkill, |
| 42 | enum rfkill_state state) |
| 43 | { |
| 44 | #ifdef CONFIG_RFKILL_LEDS |
| 45 | struct led_trigger *led = &rfkill->led_trigger; |
| 46 | |
| 47 | if (!led->name) |
| 48 | return; |
| 49 | if (state == RFKILL_STATE_OFF) |
| 50 | led_trigger_event(led, LED_OFF); |
| 51 | else |
| 52 | led_trigger_event(led, LED_FULL); |
| 53 | #endif /* CONFIG_RFKILL_LEDS */ |
| 54 | } |
| 55 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 56 | static int rfkill_toggle_radio(struct rfkill *rfkill, |
| 57 | enum rfkill_state state) |
| 58 | { |
| 59 | int retval; |
| 60 | |
| 61 | retval = mutex_lock_interruptible(&rfkill->mutex); |
| 62 | if (retval) |
| 63 | return retval; |
| 64 | |
| 65 | if (state != rfkill->state) { |
| 66 | retval = rfkill->toggle_radio(rfkill->data, state); |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 67 | if (!retval) { |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 68 | rfkill->state = state; |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 69 | rfkill_led_trigger(rfkill, state); |
| 70 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | mutex_unlock(&rfkill->mutex); |
| 74 | return retval; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * rfkill_switch_all - Toggle state of all switches of given type |
| 79 | * @type: type of interfaces to be affeceted |
| 80 | * @state: the new state |
| 81 | * |
| 82 | * This function toggles state of all switches of given type unless |
| 83 | * a specific switch is claimed by userspace in which case it is |
| 84 | * left alone. |
| 85 | */ |
| 86 | |
| 87 | void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state) |
| 88 | { |
| 89 | struct rfkill *rfkill; |
| 90 | |
| 91 | mutex_lock(&rfkill_mutex); |
| 92 | |
| 93 | rfkill_states[type] = state; |
| 94 | |
| 95 | list_for_each_entry(rfkill, &rfkill_list, node) { |
| 96 | if (!rfkill->user_claim) |
| 97 | rfkill_toggle_radio(rfkill, state); |
| 98 | } |
| 99 | |
| 100 | mutex_unlock(&rfkill_mutex); |
| 101 | } |
| 102 | EXPORT_SYMBOL(rfkill_switch_all); |
| 103 | |
| 104 | static ssize_t rfkill_name_show(struct device *dev, |
| 105 | struct device_attribute *attr, |
| 106 | char *buf) |
| 107 | { |
| 108 | struct rfkill *rfkill = to_rfkill(dev); |
| 109 | |
| 110 | return sprintf(buf, "%s\n", rfkill->name); |
| 111 | } |
| 112 | |
| 113 | static ssize_t rfkill_type_show(struct device *dev, |
| 114 | struct device_attribute *attr, |
| 115 | char *buf) |
| 116 | { |
| 117 | struct rfkill *rfkill = to_rfkill(dev); |
| 118 | const char *type; |
| 119 | |
| 120 | switch (rfkill->type) { |
| 121 | case RFKILL_TYPE_WLAN: |
| 122 | type = "wlan"; |
| 123 | break; |
| 124 | case RFKILL_TYPE_BLUETOOTH: |
| 125 | type = "bluetooth"; |
| 126 | break; |
Ivo van Doorn | e0665486 | 2007-09-13 09:21:31 +0200 | [diff] [blame] | 127 | case RFKILL_TYPE_UWB: |
| 128 | type = "ultrawideband"; |
| 129 | break; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 130 | default: |
| 131 | BUG(); |
| 132 | } |
| 133 | |
| 134 | return sprintf(buf, "%s\n", type); |
| 135 | } |
| 136 | |
| 137 | static ssize_t rfkill_state_show(struct device *dev, |
| 138 | struct device_attribute *attr, |
| 139 | char *buf) |
| 140 | { |
| 141 | struct rfkill *rfkill = to_rfkill(dev); |
| 142 | |
| 143 | return sprintf(buf, "%d\n", rfkill->state); |
| 144 | } |
| 145 | |
| 146 | static ssize_t rfkill_state_store(struct device *dev, |
| 147 | struct device_attribute *attr, |
| 148 | const char *buf, size_t count) |
| 149 | { |
| 150 | struct rfkill *rfkill = to_rfkill(dev); |
| 151 | unsigned int state = simple_strtoul(buf, NULL, 0); |
| 152 | int error; |
| 153 | |
| 154 | if (!capable(CAP_NET_ADMIN)) |
| 155 | return -EPERM; |
| 156 | |
| 157 | error = rfkill_toggle_radio(rfkill, |
| 158 | state ? RFKILL_STATE_ON : RFKILL_STATE_OFF); |
| 159 | if (error) |
| 160 | return error; |
| 161 | |
| 162 | return count; |
| 163 | } |
| 164 | |
| 165 | static ssize_t rfkill_claim_show(struct device *dev, |
| 166 | struct device_attribute *attr, |
| 167 | char *buf) |
| 168 | { |
| 169 | struct rfkill *rfkill = to_rfkill(dev); |
| 170 | |
| 171 | return sprintf(buf, "%d", rfkill->user_claim); |
| 172 | } |
| 173 | |
| 174 | static ssize_t rfkill_claim_store(struct device *dev, |
| 175 | struct device_attribute *attr, |
| 176 | const char *buf, size_t count) |
| 177 | { |
| 178 | struct rfkill *rfkill = to_rfkill(dev); |
| 179 | bool claim = !!simple_strtoul(buf, NULL, 0); |
| 180 | int error; |
| 181 | |
| 182 | if (!capable(CAP_NET_ADMIN)) |
| 183 | return -EPERM; |
| 184 | |
| 185 | /* |
| 186 | * Take the global lock to make sure the kernel is not in |
| 187 | * the middle of rfkill_switch_all |
| 188 | */ |
| 189 | error = mutex_lock_interruptible(&rfkill_mutex); |
| 190 | if (error) |
| 191 | return error; |
| 192 | |
Michael Buesch | 20405c0 | 2007-09-27 21:34:23 +0200 | [diff] [blame] | 193 | if (rfkill->user_claim_unsupported) { |
| 194 | error = -EOPNOTSUPP; |
| 195 | goto out_unlock; |
| 196 | } |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 197 | if (rfkill->user_claim != claim) { |
| 198 | if (!claim) |
| 199 | rfkill_toggle_radio(rfkill, |
| 200 | rfkill_states[rfkill->type]); |
| 201 | rfkill->user_claim = claim; |
| 202 | } |
| 203 | |
Michael Buesch | 20405c0 | 2007-09-27 21:34:23 +0200 | [diff] [blame] | 204 | out_unlock: |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 205 | mutex_unlock(&rfkill_mutex); |
| 206 | |
Michael Buesch | 20405c0 | 2007-09-27 21:34:23 +0200 | [diff] [blame] | 207 | return error ? error : count; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static struct device_attribute rfkill_dev_attrs[] = { |
| 211 | __ATTR(name, S_IRUGO, rfkill_name_show, NULL), |
| 212 | __ATTR(type, S_IRUGO, rfkill_type_show, NULL), |
Ivo van Doorn | c81de6a | 2007-07-18 15:38:03 -0700 | [diff] [blame] | 213 | __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] | 214 | __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store), |
| 215 | __ATTR_NULL |
| 216 | }; |
| 217 | |
| 218 | static void rfkill_release(struct device *dev) |
| 219 | { |
| 220 | struct rfkill *rfkill = to_rfkill(dev); |
| 221 | |
| 222 | kfree(rfkill); |
| 223 | module_put(THIS_MODULE); |
| 224 | } |
| 225 | |
| 226 | #ifdef CONFIG_PM |
| 227 | static int rfkill_suspend(struct device *dev, pm_message_t state) |
| 228 | { |
| 229 | struct rfkill *rfkill = to_rfkill(dev); |
| 230 | |
| 231 | if (dev->power.power_state.event != state.event) { |
| 232 | if (state.event == PM_EVENT_SUSPEND) { |
| 233 | mutex_lock(&rfkill->mutex); |
| 234 | |
| 235 | if (rfkill->state == RFKILL_STATE_ON) |
| 236 | rfkill->toggle_radio(rfkill->data, |
| 237 | RFKILL_STATE_OFF); |
| 238 | |
| 239 | mutex_unlock(&rfkill->mutex); |
| 240 | } |
| 241 | |
| 242 | dev->power.power_state = state; |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int rfkill_resume(struct device *dev) |
| 249 | { |
| 250 | struct rfkill *rfkill = to_rfkill(dev); |
| 251 | |
| 252 | if (dev->power.power_state.event != PM_EVENT_ON) { |
| 253 | mutex_lock(&rfkill->mutex); |
| 254 | |
| 255 | if (rfkill->state == RFKILL_STATE_ON) |
| 256 | rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON); |
| 257 | |
| 258 | mutex_unlock(&rfkill->mutex); |
| 259 | } |
| 260 | |
| 261 | dev->power.power_state = PMSG_ON; |
| 262 | return 0; |
| 263 | } |
| 264 | #else |
| 265 | #define rfkill_suspend NULL |
| 266 | #define rfkill_resume NULL |
| 267 | #endif |
| 268 | |
| 269 | static struct class rfkill_class = { |
| 270 | .name = "rfkill", |
| 271 | .dev_release = rfkill_release, |
| 272 | .dev_attrs = rfkill_dev_attrs, |
| 273 | .suspend = rfkill_suspend, |
| 274 | .resume = rfkill_resume, |
| 275 | }; |
| 276 | |
| 277 | static int rfkill_add_switch(struct rfkill *rfkill) |
| 278 | { |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame^] | 279 | int error; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 280 | |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame^] | 281 | mutex_lock(&rfkill_mutex); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 282 | |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame^] | 283 | error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]); |
| 284 | if (!error) |
| 285 | list_add_tail(&rfkill->node, &rfkill_list); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 286 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 287 | mutex_unlock(&rfkill_mutex); |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame^] | 288 | |
| 289 | return error; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void rfkill_remove_switch(struct rfkill *rfkill) |
| 293 | { |
| 294 | mutex_lock(&rfkill_mutex); |
| 295 | list_del_init(&rfkill->node); |
| 296 | rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF); |
| 297 | mutex_unlock(&rfkill_mutex); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * rfkill_allocate - allocate memory for rfkill structure. |
| 302 | * @parent: device that has rf switch on it |
Ivo van Doorn | 234a0ca | 2007-09-13 09:20:42 +0200 | [diff] [blame] | 303 | * @type: type of the switch (RFKILL_TYPE_*) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 304 | * |
| 305 | * This function should be called by the network driver when it needs |
| 306 | * rfkill structure. Once the structure is allocated the driver shoud |
| 307 | * finish its initialization by setting name, private data, enable_radio |
| 308 | * and disable_radio methods and then register it with rfkill_register(). |
| 309 | * NOTE: If registration fails the structure shoudl be freed by calling |
| 310 | * rfkill_free() otherwise rfkill_unregister() should be used. |
| 311 | */ |
| 312 | struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type) |
| 313 | { |
| 314 | struct rfkill *rfkill; |
| 315 | struct device *dev; |
| 316 | |
| 317 | rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL); |
Ivo van Doorn | d007da1 | 2007-05-19 12:24:39 -0700 | [diff] [blame] | 318 | if (!rfkill) |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 319 | return NULL; |
| 320 | |
| 321 | mutex_init(&rfkill->mutex); |
| 322 | INIT_LIST_HEAD(&rfkill->node); |
| 323 | rfkill->type = type; |
| 324 | |
| 325 | dev = &rfkill->dev; |
| 326 | dev->class = &rfkill_class; |
| 327 | dev->parent = parent; |
| 328 | device_initialize(dev); |
| 329 | |
| 330 | __module_get(THIS_MODULE); |
| 331 | |
| 332 | return rfkill; |
| 333 | } |
| 334 | EXPORT_SYMBOL(rfkill_allocate); |
| 335 | |
| 336 | /** |
| 337 | * rfkill_free - Mark rfkill structure for deletion |
| 338 | * @rfkill: rfkill structure to be destroyed |
| 339 | * |
| 340 | * Decrements reference count of rfkill structure so it is destoryed. |
| 341 | * Note that rfkill_free() should _not_ be called after rfkill_unregister(). |
| 342 | */ |
| 343 | void rfkill_free(struct rfkill *rfkill) |
| 344 | { |
| 345 | if (rfkill) |
| 346 | put_device(&rfkill->dev); |
| 347 | } |
| 348 | EXPORT_SYMBOL(rfkill_free); |
| 349 | |
Michael Buesch | 135900c | 2007-09-27 21:33:12 +0200 | [diff] [blame] | 350 | static void rfkill_led_trigger_register(struct rfkill *rfkill) |
| 351 | { |
| 352 | #ifdef CONFIG_RFKILL_LEDS |
| 353 | int error; |
| 354 | |
| 355 | rfkill->led_trigger.name = rfkill->dev.bus_id; |
| 356 | error = led_trigger_register(&rfkill->led_trigger); |
| 357 | if (error) |
| 358 | rfkill->led_trigger.name = NULL; |
| 359 | #endif /* CONFIG_RFKILL_LEDS */ |
| 360 | } |
| 361 | |
| 362 | static void rfkill_led_trigger_unregister(struct rfkill *rfkill) |
| 363 | { |
| 364 | #ifdef CONFIG_RFKILL_LEDS |
| 365 | if (rfkill->led_trigger.name) |
| 366 | led_trigger_unregister(&rfkill->led_trigger); |
| 367 | #endif |
| 368 | } |
| 369 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 370 | /** |
| 371 | * rfkill_register - Register a rfkill structure. |
| 372 | * @rfkill: rfkill structure to be registered |
| 373 | * |
| 374 | * This function should be called by the network driver when the rfkill |
| 375 | * structure needs to be registered. Immediately from registration the |
| 376 | * switch driver should be able to service calls to toggle_radio. |
| 377 | */ |
| 378 | int rfkill_register(struct rfkill *rfkill) |
| 379 | { |
| 380 | static atomic_t rfkill_no = ATOMIC_INIT(0); |
| 381 | struct device *dev = &rfkill->dev; |
| 382 | int error; |
| 383 | |
| 384 | if (!rfkill->toggle_radio) |
| 385 | return -EINVAL; |
Michael Buesch | 7319f1e | 2007-10-28 15:16:50 +0100 | [diff] [blame^] | 386 | if (rfkill->type >= RFKILL_TYPE_MAX) |
| 387 | return -EINVAL; |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 388 | |
Michael Buesch | 8a8f1c0 | 2007-10-28 13:07:54 +0100 | [diff] [blame] | 389 | snprintf(dev->bus_id, sizeof(dev->bus_id), |
| 390 | "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1); |
| 391 | |
| 392 | rfkill_led_trigger_register(rfkill); |
| 393 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 394 | error = rfkill_add_switch(rfkill); |
| 395 | if (error) |
| 396 | return error; |
| 397 | |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 398 | error = device_add(dev); |
| 399 | if (error) { |
| 400 | rfkill_remove_switch(rfkill); |
| 401 | return error; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | EXPORT_SYMBOL(rfkill_register); |
| 407 | |
| 408 | /** |
| 409 | * rfkill_unregister - Uegister a rfkill structure. |
| 410 | * @rfkill: rfkill structure to be unregistered |
| 411 | * |
| 412 | * This function should be called by the network driver during device |
| 413 | * teardown to destroy rfkill structure. Note that rfkill_free() should |
| 414 | * _not_ be called after rfkill_unregister(). |
| 415 | */ |
| 416 | void rfkill_unregister(struct rfkill *rfkill) |
| 417 | { |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 418 | device_del(&rfkill->dev); |
| 419 | rfkill_remove_switch(rfkill); |
Michael Buesch | 8a8f1c0 | 2007-10-28 13:07:54 +0100 | [diff] [blame] | 420 | rfkill_led_trigger_unregister(rfkill); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 421 | put_device(&rfkill->dev); |
| 422 | } |
| 423 | EXPORT_SYMBOL(rfkill_unregister); |
| 424 | |
| 425 | /* |
| 426 | * Rfkill module initialization/deinitialization. |
| 427 | */ |
| 428 | static int __init rfkill_init(void) |
| 429 | { |
| 430 | int error; |
| 431 | int i; |
| 432 | |
| 433 | for (i = 0; i < ARRAY_SIZE(rfkill_states); i++) |
| 434 | rfkill_states[i] = RFKILL_STATE_ON; |
| 435 | |
| 436 | error = class_register(&rfkill_class); |
| 437 | if (error) { |
| 438 | printk(KERN_ERR "rfkill: unable to register rfkill class\n"); |
| 439 | return error; |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static void __exit rfkill_exit(void) |
| 446 | { |
| 447 | class_unregister(&rfkill_class); |
| 448 | } |
| 449 | |
Michael Buesch | 2bf236d | 2007-10-28 14:39:02 +0100 | [diff] [blame] | 450 | subsys_initcall(rfkill_init); |
Ivo van Doorn | cf4328c | 2007-05-07 00:34:20 -0700 | [diff] [blame] | 451 | module_exit(rfkill_exit); |