at v2.6.27-rc2 714 lines 19 kB view raw
1/* 2 * Copyright (C) 2006 - 2007 Ivo van Doorn 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/* Get declaration of rfkill_switch_all() to shut up sparse. */ 31#include "rfkill-input.h" 32 33 34MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>"); 35MODULE_VERSION("1.0"); 36MODULE_DESCRIPTION("RF switch support"); 37MODULE_LICENSE("GPL"); 38 39static LIST_HEAD(rfkill_list); /* list of registered rf switches */ 40static DEFINE_MUTEX(rfkill_mutex); 41 42static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED; 43module_param_named(default_state, rfkill_default_state, uint, 0444); 44MODULE_PARM_DESC(default_state, 45 "Default initial state for all radio types, 0 = radio off"); 46 47static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX]; 48 49static 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 */ 71int register_rfkill_notifier(struct notifier_block *nb) 72{ 73 return blocking_notifier_chain_register(&rfkill_notifier_list, nb); 74} 75EXPORT_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 */ 86int unregister_rfkill_notifier(struct notifier_block *nb) 87{ 88 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb); 89} 90EXPORT_SYMBOL_GPL(unregister_rfkill_notifier); 91 92 93static 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_UNBLOCKED) 102 led_trigger_event(led, LED_OFF); 103 else 104 led_trigger_event(led, LED_FULL); 105#endif /* CONFIG_RFKILL_LEDS */ 106} 107 108#ifdef CONFIG_RFKILL_LEDS 109static void rfkill_led_trigger_activate(struct led_classdev *led) 110{ 111 struct rfkill *rfkill = container_of(led->trigger, 112 struct rfkill, led_trigger); 113 114 rfkill_led_trigger(rfkill, rfkill->state); 115} 116#endif /* CONFIG_RFKILL_LEDS */ 117 118static void notify_rfkill_state_change(struct rfkill *rfkill) 119{ 120 blocking_notifier_call_chain(&rfkill_notifier_list, 121 RFKILL_STATE_CHANGED, 122 rfkill); 123} 124 125static void update_rfkill_state(struct rfkill *rfkill) 126{ 127 enum rfkill_state newstate, oldstate; 128 129 if (rfkill->get_state) { 130 mutex_lock(&rfkill->mutex); 131 if (!rfkill->get_state(rfkill->data, &newstate)) { 132 oldstate = rfkill->state; 133 rfkill->state = newstate; 134 if (oldstate != newstate) 135 notify_rfkill_state_change(rfkill); 136 } 137 mutex_unlock(&rfkill->mutex); 138 } 139} 140 141/** 142 * rfkill_toggle_radio - wrapper for toggle_radio hook 143 * @rfkill: the rfkill struct to use 144 * @force: calls toggle_radio even if cache says it is not needed, 145 * and also makes sure notifications of the state will be 146 * sent even if it didn't change 147 * @state: the new state to call toggle_radio() with 148 * 149 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio 150 * calls and handling all the red tape such as issuing notifications 151 * if the call is successful. 152 * 153 * Note that the @force parameter cannot override a (possibly cached) 154 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of 155 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or 156 * rfkill_force_state(), so the cache either is bypassed or valid. 157 * 158 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED 159 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to 160 * give the driver a hint that it should double-BLOCK the transmitter. 161 * 162 * Caller must have acquired rfkill->mutex. 163 */ 164static int rfkill_toggle_radio(struct rfkill *rfkill, 165 enum rfkill_state state, 166 int force) 167{ 168 int retval = 0; 169 enum rfkill_state oldstate, newstate; 170 171 oldstate = rfkill->state; 172 173 if (rfkill->get_state && !force && 174 !rfkill->get_state(rfkill->data, &newstate)) 175 rfkill->state = newstate; 176 177 switch (state) { 178 case RFKILL_STATE_HARD_BLOCKED: 179 /* typically happens when refreshing hardware state, 180 * such as on resume */ 181 state = RFKILL_STATE_SOFT_BLOCKED; 182 break; 183 case RFKILL_STATE_UNBLOCKED: 184 /* force can't override this, only rfkill_force_state() can */ 185 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED) 186 return -EPERM; 187 break; 188 case RFKILL_STATE_SOFT_BLOCKED: 189 /* nothing to do, we want to give drivers the hint to double 190 * BLOCK even a transmitter that is already in state 191 * RFKILL_STATE_HARD_BLOCKED */ 192 break; 193 } 194 195 if (force || state != rfkill->state) { 196 retval = rfkill->toggle_radio(rfkill->data, state); 197 /* never allow a HARD->SOFT downgrade! */ 198 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED) 199 rfkill->state = state; 200 } 201 202 if (force || rfkill->state != oldstate) { 203 rfkill_led_trigger(rfkill, rfkill->state); 204 notify_rfkill_state_change(rfkill); 205 } 206 207 return retval; 208} 209 210/** 211 * rfkill_switch_all - Toggle state of all switches of given type 212 * @type: type of interfaces to be affected 213 * @state: the new state 214 * 215 * This function toggles the state of all switches of given type, 216 * unless a specific switch is claimed by userspace (in which case, 217 * that switch is left alone). 218 */ 219void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state) 220{ 221 struct rfkill *rfkill; 222 223 mutex_lock(&rfkill_mutex); 224 225 rfkill_states[type] = state; 226 227 list_for_each_entry(rfkill, &rfkill_list, node) { 228 if ((!rfkill->user_claim) && (rfkill->type == type)) { 229 mutex_lock(&rfkill->mutex); 230 rfkill_toggle_radio(rfkill, state, 0); 231 mutex_unlock(&rfkill->mutex); 232 } 233 } 234 235 mutex_unlock(&rfkill_mutex); 236} 237EXPORT_SYMBOL(rfkill_switch_all); 238 239/** 240 * rfkill_epo - emergency power off all transmitters 241 * 242 * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring 243 * everything in its path but rfkill_mutex and rfkill->mutex. 244 */ 245void rfkill_epo(void) 246{ 247 struct rfkill *rfkill; 248 249 mutex_lock(&rfkill_mutex); 250 list_for_each_entry(rfkill, &rfkill_list, node) { 251 mutex_lock(&rfkill->mutex); 252 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); 253 mutex_unlock(&rfkill->mutex); 254 } 255 mutex_unlock(&rfkill_mutex); 256} 257EXPORT_SYMBOL_GPL(rfkill_epo); 258 259/** 260 * rfkill_force_state - Force the internal rfkill radio state 261 * @rfkill: pointer to the rfkill class to modify. 262 * @state: the current radio state the class should be forced to. 263 * 264 * This function updates the internal state of the radio cached 265 * by the rfkill class. It should be used when the driver gets 266 * a notification by the firmware/hardware of the current *real* 267 * state of the radio rfkill switch. 268 * 269 * Devices which are subject to external changes on their rfkill 270 * state (such as those caused by a hardware rfkill line) MUST 271 * have their driver arrange to call rfkill_force_state() as soon 272 * as possible after such a change. 273 * 274 * This function may not be called from an atomic context. 275 */ 276int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state) 277{ 278 enum rfkill_state oldstate; 279 280 if (state != RFKILL_STATE_SOFT_BLOCKED && 281 state != RFKILL_STATE_UNBLOCKED && 282 state != RFKILL_STATE_HARD_BLOCKED) 283 return -EINVAL; 284 285 mutex_lock(&rfkill->mutex); 286 287 oldstate = rfkill->state; 288 rfkill->state = state; 289 290 if (state != oldstate) 291 notify_rfkill_state_change(rfkill); 292 293 mutex_unlock(&rfkill->mutex); 294 295 return 0; 296} 297EXPORT_SYMBOL(rfkill_force_state); 298 299static ssize_t rfkill_name_show(struct device *dev, 300 struct device_attribute *attr, 301 char *buf) 302{ 303 struct rfkill *rfkill = to_rfkill(dev); 304 305 return sprintf(buf, "%s\n", rfkill->name); 306} 307 308static const char *rfkill_get_type_str(enum rfkill_type type) 309{ 310 switch (type) { 311 case RFKILL_TYPE_WLAN: 312 return "wlan"; 313 case RFKILL_TYPE_BLUETOOTH: 314 return "bluetooth"; 315 case RFKILL_TYPE_UWB: 316 return "ultrawideband"; 317 case RFKILL_TYPE_WIMAX: 318 return "wimax"; 319 case RFKILL_TYPE_WWAN: 320 return "wwan"; 321 default: 322 BUG(); 323 } 324} 325 326static ssize_t rfkill_type_show(struct device *dev, 327 struct device_attribute *attr, 328 char *buf) 329{ 330 struct rfkill *rfkill = to_rfkill(dev); 331 332 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type)); 333} 334 335static ssize_t rfkill_state_show(struct device *dev, 336 struct device_attribute *attr, 337 char *buf) 338{ 339 struct rfkill *rfkill = to_rfkill(dev); 340 341 update_rfkill_state(rfkill); 342 return sprintf(buf, "%d\n", rfkill->state); 343} 344 345static ssize_t rfkill_state_store(struct device *dev, 346 struct device_attribute *attr, 347 const char *buf, size_t count) 348{ 349 struct rfkill *rfkill = to_rfkill(dev); 350 unsigned int state = simple_strtoul(buf, NULL, 0); 351 int error; 352 353 if (!capable(CAP_NET_ADMIN)) 354 return -EPERM; 355 356 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ 357 if (state != RFKILL_STATE_UNBLOCKED && 358 state != RFKILL_STATE_SOFT_BLOCKED) 359 return -EINVAL; 360 361 if (mutex_lock_interruptible(&rfkill->mutex)) 362 return -ERESTARTSYS; 363 error = rfkill_toggle_radio(rfkill, state, 0); 364 mutex_unlock(&rfkill->mutex); 365 366 return error ? error : count; 367} 368 369static ssize_t rfkill_claim_show(struct device *dev, 370 struct device_attribute *attr, 371 char *buf) 372{ 373 struct rfkill *rfkill = to_rfkill(dev); 374 375 return sprintf(buf, "%d", rfkill->user_claim); 376} 377 378static ssize_t rfkill_claim_store(struct device *dev, 379 struct device_attribute *attr, 380 const char *buf, size_t count) 381{ 382 struct rfkill *rfkill = to_rfkill(dev); 383 bool claim = !!simple_strtoul(buf, NULL, 0); 384 int error; 385 386 if (!capable(CAP_NET_ADMIN)) 387 return -EPERM; 388 389 if (rfkill->user_claim_unsupported) 390 return -EOPNOTSUPP; 391 392 /* 393 * Take the global lock to make sure the kernel is not in 394 * the middle of rfkill_switch_all 395 */ 396 error = mutex_lock_interruptible(&rfkill_mutex); 397 if (error) 398 return error; 399 400 if (rfkill->user_claim != claim) { 401 if (!claim) { 402 mutex_lock(&rfkill->mutex); 403 rfkill_toggle_radio(rfkill, 404 rfkill_states[rfkill->type], 405 0); 406 mutex_unlock(&rfkill->mutex); 407 } 408 rfkill->user_claim = claim; 409 } 410 411 mutex_unlock(&rfkill_mutex); 412 413 return error ? error : count; 414} 415 416static struct device_attribute rfkill_dev_attrs[] = { 417 __ATTR(name, S_IRUGO, rfkill_name_show, NULL), 418 __ATTR(type, S_IRUGO, rfkill_type_show, NULL), 419 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store), 420 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store), 421 __ATTR_NULL 422}; 423 424static void rfkill_release(struct device *dev) 425{ 426 struct rfkill *rfkill = to_rfkill(dev); 427 428 kfree(rfkill); 429 module_put(THIS_MODULE); 430} 431 432#ifdef CONFIG_PM 433static int rfkill_suspend(struct device *dev, pm_message_t state) 434{ 435 struct rfkill *rfkill = to_rfkill(dev); 436 437 if (dev->power.power_state.event != state.event) { 438 if (state.event & PM_EVENT_SLEEP) { 439 /* Stop transmitter, keep state, no notifies */ 440 update_rfkill_state(rfkill); 441 442 mutex_lock(&rfkill->mutex); 443 rfkill->toggle_radio(rfkill->data, 444 RFKILL_STATE_SOFT_BLOCKED); 445 mutex_unlock(&rfkill->mutex); 446 } 447 448 dev->power.power_state = state; 449 } 450 451 return 0; 452} 453 454static int rfkill_resume(struct device *dev) 455{ 456 struct rfkill *rfkill = to_rfkill(dev); 457 458 if (dev->power.power_state.event != PM_EVENT_ON) { 459 mutex_lock(&rfkill->mutex); 460 461 /* restore radio state AND notify everybody */ 462 rfkill_toggle_radio(rfkill, rfkill->state, 1); 463 464 mutex_unlock(&rfkill->mutex); 465 } 466 467 dev->power.power_state = PMSG_ON; 468 return 0; 469} 470#else 471#define rfkill_suspend NULL 472#define rfkill_resume NULL 473#endif 474 475static int rfkill_blocking_uevent_notifier(struct notifier_block *nb, 476 unsigned long eventid, 477 void *data) 478{ 479 struct rfkill *rfkill = (struct rfkill *)data; 480 481 switch (eventid) { 482 case RFKILL_STATE_CHANGED: 483 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE); 484 break; 485 default: 486 break; 487 } 488 489 return NOTIFY_DONE; 490} 491 492static struct notifier_block rfkill_blocking_uevent_nb = { 493 .notifier_call = rfkill_blocking_uevent_notifier, 494 .priority = 0, 495}; 496 497static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env) 498{ 499 struct rfkill *rfkill = to_rfkill(dev); 500 int error; 501 502 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name); 503 if (error) 504 return error; 505 error = add_uevent_var(env, "RFKILL_TYPE=%s", 506 rfkill_get_type_str(rfkill->type)); 507 if (error) 508 return error; 509 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state); 510 return error; 511} 512 513static struct class rfkill_class = { 514 .name = "rfkill", 515 .dev_release = rfkill_release, 516 .dev_attrs = rfkill_dev_attrs, 517 .suspend = rfkill_suspend, 518 .resume = rfkill_resume, 519 .dev_uevent = rfkill_dev_uevent, 520}; 521 522static int rfkill_add_switch(struct rfkill *rfkill) 523{ 524 mutex_lock(&rfkill_mutex); 525 526 rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0); 527 528 list_add_tail(&rfkill->node, &rfkill_list); 529 530 mutex_unlock(&rfkill_mutex); 531 532 return 0; 533} 534 535static void rfkill_remove_switch(struct rfkill *rfkill) 536{ 537 mutex_lock(&rfkill_mutex); 538 list_del_init(&rfkill->node); 539 mutex_unlock(&rfkill_mutex); 540 541 mutex_lock(&rfkill->mutex); 542 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); 543 mutex_unlock(&rfkill->mutex); 544} 545 546/** 547 * rfkill_allocate - allocate memory for rfkill structure. 548 * @parent: device that has rf switch on it 549 * @type: type of the switch (RFKILL_TYPE_*) 550 * 551 * This function should be called by the network driver when it needs 552 * rfkill structure. Once the structure is allocated the driver should 553 * finish its initialization by setting the name, private data, enable_radio 554 * and disable_radio methods and then register it with rfkill_register(). 555 * 556 * NOTE: If registration fails the structure shoudl be freed by calling 557 * rfkill_free() otherwise rfkill_unregister() should be used. 558 */ 559struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type) 560{ 561 struct rfkill *rfkill; 562 struct device *dev; 563 564 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL); 565 if (!rfkill) 566 return NULL; 567 568 mutex_init(&rfkill->mutex); 569 INIT_LIST_HEAD(&rfkill->node); 570 rfkill->type = type; 571 572 dev = &rfkill->dev; 573 dev->class = &rfkill_class; 574 dev->parent = parent; 575 device_initialize(dev); 576 577 __module_get(THIS_MODULE); 578 579 return rfkill; 580} 581EXPORT_SYMBOL(rfkill_allocate); 582 583/** 584 * rfkill_free - Mark rfkill structure for deletion 585 * @rfkill: rfkill structure to be destroyed 586 * 587 * Decrements reference count of the rfkill structure so it is destroyed. 588 * Note that rfkill_free() should _not_ be called after rfkill_unregister(). 589 */ 590void rfkill_free(struct rfkill *rfkill) 591{ 592 if (rfkill) 593 put_device(&rfkill->dev); 594} 595EXPORT_SYMBOL(rfkill_free); 596 597static void rfkill_led_trigger_register(struct rfkill *rfkill) 598{ 599#ifdef CONFIG_RFKILL_LEDS 600 int error; 601 602 if (!rfkill->led_trigger.name) 603 rfkill->led_trigger.name = rfkill->dev.bus_id; 604 if (!rfkill->led_trigger.activate) 605 rfkill->led_trigger.activate = rfkill_led_trigger_activate; 606 error = led_trigger_register(&rfkill->led_trigger); 607 if (error) 608 rfkill->led_trigger.name = NULL; 609#endif /* CONFIG_RFKILL_LEDS */ 610} 611 612static void rfkill_led_trigger_unregister(struct rfkill *rfkill) 613{ 614#ifdef CONFIG_RFKILL_LEDS 615 if (rfkill->led_trigger.name) { 616 led_trigger_unregister(&rfkill->led_trigger); 617 rfkill->led_trigger.name = NULL; 618 } 619#endif 620} 621 622/** 623 * rfkill_register - Register a rfkill structure. 624 * @rfkill: rfkill structure to be registered 625 * 626 * This function should be called by the network driver when the rfkill 627 * structure needs to be registered. Immediately from registration the 628 * switch driver should be able to service calls to toggle_radio. 629 */ 630int rfkill_register(struct rfkill *rfkill) 631{ 632 static atomic_t rfkill_no = ATOMIC_INIT(0); 633 struct device *dev = &rfkill->dev; 634 int error; 635 636 if (!rfkill->toggle_radio) 637 return -EINVAL; 638 if (rfkill->type >= RFKILL_TYPE_MAX) 639 return -EINVAL; 640 641 snprintf(dev->bus_id, sizeof(dev->bus_id), 642 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1); 643 644 rfkill_led_trigger_register(rfkill); 645 646 error = rfkill_add_switch(rfkill); 647 if (error) { 648 rfkill_led_trigger_unregister(rfkill); 649 return error; 650 } 651 652 error = device_add(dev); 653 if (error) { 654 rfkill_remove_switch(rfkill); 655 rfkill_led_trigger_unregister(rfkill); 656 return error; 657 } 658 659 return 0; 660} 661EXPORT_SYMBOL(rfkill_register); 662 663/** 664 * rfkill_unregister - Unregister a rfkill structure. 665 * @rfkill: rfkill structure to be unregistered 666 * 667 * This function should be called by the network driver during device 668 * teardown to destroy rfkill structure. Note that rfkill_free() should 669 * _not_ be called after rfkill_unregister(). 670 */ 671void rfkill_unregister(struct rfkill *rfkill) 672{ 673 device_del(&rfkill->dev); 674 rfkill_remove_switch(rfkill); 675 rfkill_led_trigger_unregister(rfkill); 676 put_device(&rfkill->dev); 677} 678EXPORT_SYMBOL(rfkill_unregister); 679 680/* 681 * Rfkill module initialization/deinitialization. 682 */ 683static int __init rfkill_init(void) 684{ 685 int error; 686 int i; 687 688 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ 689 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED && 690 rfkill_default_state != RFKILL_STATE_UNBLOCKED) 691 return -EINVAL; 692 693 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++) 694 rfkill_states[i] = rfkill_default_state; 695 696 error = class_register(&rfkill_class); 697 if (error) { 698 printk(KERN_ERR "rfkill: unable to register rfkill class\n"); 699 return error; 700 } 701 702 register_rfkill_notifier(&rfkill_blocking_uevent_nb); 703 704 return 0; 705} 706 707static void __exit rfkill_exit(void) 708{ 709 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb); 710 class_unregister(&rfkill_class); 711} 712 713subsys_initcall(rfkill_init); 714module_exit(rfkill_exit);