rfkill: detect bogus double-registering (v2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / rfkill / rfkill.c
blob2fae1609eef0b44ecce22cd35957548b7540b589
1 /*
2 * Copyright (C) 2006 - 2007 Ivo van Doorn
3 * Copyright (C) 2007 Dmitry Torokhov
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.
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.
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.
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>
30 /* Get declaration of rfkill_switch_all() to shut up sparse. */
31 #include "rfkill-input.h"
34 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35 MODULE_VERSION("1.0");
36 MODULE_DESCRIPTION("RF switch support");
37 MODULE_LICENSE("GPL");
39 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
40 static DEFINE_MUTEX(rfkill_mutex);
42 static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
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");
47 static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
49 static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
52 /**
53 * register_rfkill_notifier - Add notifier to rfkill notifier chain
54 * @nb: pointer to the new entry to add to the chain
56 * See blocking_notifier_chain_register() for return value and further
57 * observations.
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.
63 * Notifiers added to this chain are to always return NOTIFY_DONE. This
64 * chain is a blocking notifier chain: notifiers can sleep.
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.
71 int register_rfkill_notifier(struct notifier_block *nb)
73 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
75 EXPORT_SYMBOL_GPL(register_rfkill_notifier);
77 /**
78 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
79 * @nb: pointer to the entry to remove from the chain
81 * See blocking_notifier_chain_unregister() for return value and further
82 * observations.
84 * Removes a notifier from the rfkill notifier chain.
86 int unregister_rfkill_notifier(struct notifier_block *nb)
88 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
90 EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
93 static void rfkill_led_trigger(struct rfkill *rfkill,
94 enum rfkill_state state)
96 #ifdef CONFIG_RFKILL_LEDS
97 struct led_trigger *led = &rfkill->led_trigger;
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 */
108 #ifdef CONFIG_RFKILL_LEDS
109 static void rfkill_led_trigger_activate(struct led_classdev *led)
111 struct rfkill *rfkill = container_of(led->trigger,
112 struct rfkill, led_trigger);
114 rfkill_led_trigger(rfkill, rfkill->state);
116 #endif /* CONFIG_RFKILL_LEDS */
118 static void notify_rfkill_state_change(struct rfkill *rfkill)
120 blocking_notifier_call_chain(&rfkill_notifier_list,
121 RFKILL_STATE_CHANGED,
122 rfkill);
125 static void update_rfkill_state(struct rfkill *rfkill)
127 enum rfkill_state newstate, oldstate;
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);
137 mutex_unlock(&rfkill->mutex);
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
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.
153 * Suspended devices are not touched at all, and -EAGAIN is returned.
155 * Note that the @force parameter cannot override a (possibly cached)
156 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
157 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
158 * rfkill_force_state(), so the cache either is bypassed or valid.
160 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
161 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
162 * give the driver a hint that it should double-BLOCK the transmitter.
164 * Caller must have acquired rfkill->mutex.
166 static int rfkill_toggle_radio(struct rfkill *rfkill,
167 enum rfkill_state state,
168 int force)
170 int retval = 0;
171 enum rfkill_state oldstate, newstate;
173 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
174 return -EBUSY;
176 oldstate = rfkill->state;
178 if (rfkill->get_state && !force &&
179 !rfkill->get_state(rfkill->data, &newstate))
180 rfkill->state = newstate;
182 switch (state) {
183 case RFKILL_STATE_HARD_BLOCKED:
184 /* typically happens when refreshing hardware state,
185 * such as on resume */
186 state = RFKILL_STATE_SOFT_BLOCKED;
187 break;
188 case RFKILL_STATE_UNBLOCKED:
189 /* force can't override this, only rfkill_force_state() can */
190 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
191 return -EPERM;
192 break;
193 case RFKILL_STATE_SOFT_BLOCKED:
194 /* nothing to do, we want to give drivers the hint to double
195 * BLOCK even a transmitter that is already in state
196 * RFKILL_STATE_HARD_BLOCKED */
197 break;
200 if (force || state != rfkill->state) {
201 retval = rfkill->toggle_radio(rfkill->data, state);
202 /* never allow a HARD->SOFT downgrade! */
203 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
204 rfkill->state = state;
207 if (force || rfkill->state != oldstate) {
208 rfkill_led_trigger(rfkill, rfkill->state);
209 notify_rfkill_state_change(rfkill);
212 return retval;
216 * rfkill_switch_all - Toggle state of all switches of given type
217 * @type: type of interfaces to be affected
218 * @state: the new state
220 * This function toggles the state of all switches of given type,
221 * unless a specific switch is claimed by userspace (in which case,
222 * that switch is left alone) or suspended.
224 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
226 struct rfkill *rfkill;
228 mutex_lock(&rfkill_mutex);
230 rfkill_states[type] = state;
232 list_for_each_entry(rfkill, &rfkill_list, node) {
233 if ((!rfkill->user_claim) && (rfkill->type == type)) {
234 mutex_lock(&rfkill->mutex);
235 rfkill_toggle_radio(rfkill, state, 0);
236 mutex_unlock(&rfkill->mutex);
240 mutex_unlock(&rfkill_mutex);
242 EXPORT_SYMBOL(rfkill_switch_all);
245 * rfkill_epo - emergency power off all transmitters
247 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
248 * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
250 void rfkill_epo(void)
252 struct rfkill *rfkill;
254 mutex_lock(&rfkill_mutex);
255 list_for_each_entry(rfkill, &rfkill_list, node) {
256 mutex_lock(&rfkill->mutex);
257 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
258 mutex_unlock(&rfkill->mutex);
260 mutex_unlock(&rfkill_mutex);
262 EXPORT_SYMBOL_GPL(rfkill_epo);
265 * rfkill_force_state - Force the internal rfkill radio state
266 * @rfkill: pointer to the rfkill class to modify.
267 * @state: the current radio state the class should be forced to.
269 * This function updates the internal state of the radio cached
270 * by the rfkill class. It should be used when the driver gets
271 * a notification by the firmware/hardware of the current *real*
272 * state of the radio rfkill switch.
274 * Devices which are subject to external changes on their rfkill
275 * state (such as those caused by a hardware rfkill line) MUST
276 * have their driver arrange to call rfkill_force_state() as soon
277 * as possible after such a change.
279 * This function may not be called from an atomic context.
281 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
283 enum rfkill_state oldstate;
285 if (state != RFKILL_STATE_SOFT_BLOCKED &&
286 state != RFKILL_STATE_UNBLOCKED &&
287 state != RFKILL_STATE_HARD_BLOCKED)
288 return -EINVAL;
290 mutex_lock(&rfkill->mutex);
292 oldstate = rfkill->state;
293 rfkill->state = state;
295 if (state != oldstate)
296 notify_rfkill_state_change(rfkill);
298 mutex_unlock(&rfkill->mutex);
300 return 0;
302 EXPORT_SYMBOL(rfkill_force_state);
304 static ssize_t rfkill_name_show(struct device *dev,
305 struct device_attribute *attr,
306 char *buf)
308 struct rfkill *rfkill = to_rfkill(dev);
310 return sprintf(buf, "%s\n", rfkill->name);
313 static const char *rfkill_get_type_str(enum rfkill_type type)
315 switch (type) {
316 case RFKILL_TYPE_WLAN:
317 return "wlan";
318 case RFKILL_TYPE_BLUETOOTH:
319 return "bluetooth";
320 case RFKILL_TYPE_UWB:
321 return "ultrawideband";
322 case RFKILL_TYPE_WIMAX:
323 return "wimax";
324 case RFKILL_TYPE_WWAN:
325 return "wwan";
326 default:
327 BUG();
331 static ssize_t rfkill_type_show(struct device *dev,
332 struct device_attribute *attr,
333 char *buf)
335 struct rfkill *rfkill = to_rfkill(dev);
337 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
340 static ssize_t rfkill_state_show(struct device *dev,
341 struct device_attribute *attr,
342 char *buf)
344 struct rfkill *rfkill = to_rfkill(dev);
346 update_rfkill_state(rfkill);
347 return sprintf(buf, "%d\n", rfkill->state);
350 static ssize_t rfkill_state_store(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
354 struct rfkill *rfkill = to_rfkill(dev);
355 unsigned int state = simple_strtoul(buf, NULL, 0);
356 int error;
358 if (!capable(CAP_NET_ADMIN))
359 return -EPERM;
361 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
362 if (state != RFKILL_STATE_UNBLOCKED &&
363 state != RFKILL_STATE_SOFT_BLOCKED)
364 return -EINVAL;
366 if (mutex_lock_interruptible(&rfkill->mutex))
367 return -ERESTARTSYS;
368 error = rfkill_toggle_radio(rfkill, state, 0);
369 mutex_unlock(&rfkill->mutex);
371 return error ? error : count;
374 static ssize_t rfkill_claim_show(struct device *dev,
375 struct device_attribute *attr,
376 char *buf)
378 struct rfkill *rfkill = to_rfkill(dev);
380 return sprintf(buf, "%d\n", rfkill->user_claim);
383 static ssize_t rfkill_claim_store(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
387 struct rfkill *rfkill = to_rfkill(dev);
388 bool claim = !!simple_strtoul(buf, NULL, 0);
389 int error;
391 if (!capable(CAP_NET_ADMIN))
392 return -EPERM;
394 if (rfkill->user_claim_unsupported)
395 return -EOPNOTSUPP;
398 * Take the global lock to make sure the kernel is not in
399 * the middle of rfkill_switch_all
401 error = mutex_lock_interruptible(&rfkill_mutex);
402 if (error)
403 return error;
405 if (rfkill->user_claim != claim) {
406 if (!claim) {
407 mutex_lock(&rfkill->mutex);
408 rfkill_toggle_radio(rfkill,
409 rfkill_states[rfkill->type],
411 mutex_unlock(&rfkill->mutex);
413 rfkill->user_claim = claim;
416 mutex_unlock(&rfkill_mutex);
418 return error ? error : count;
421 static struct device_attribute rfkill_dev_attrs[] = {
422 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
423 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
424 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
425 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
426 __ATTR_NULL
429 static void rfkill_release(struct device *dev)
431 struct rfkill *rfkill = to_rfkill(dev);
433 kfree(rfkill);
434 module_put(THIS_MODULE);
437 #ifdef CONFIG_PM
438 static int rfkill_suspend(struct device *dev, pm_message_t state)
440 struct rfkill *rfkill = to_rfkill(dev);
442 if (dev->power.power_state.event != state.event) {
443 if (state.event & PM_EVENT_SLEEP) {
444 /* Stop transmitter, keep state, no notifies */
445 update_rfkill_state(rfkill);
447 mutex_lock(&rfkill->mutex);
448 rfkill->toggle_radio(rfkill->data,
449 RFKILL_STATE_SOFT_BLOCKED);
450 mutex_unlock(&rfkill->mutex);
453 dev->power.power_state = state;
456 return 0;
459 static int rfkill_resume(struct device *dev)
461 struct rfkill *rfkill = to_rfkill(dev);
463 if (dev->power.power_state.event != PM_EVENT_ON) {
464 mutex_lock(&rfkill->mutex);
466 dev->power.power_state.event = PM_EVENT_ON;
468 /* restore radio state AND notify everybody */
469 rfkill_toggle_radio(rfkill, rfkill->state, 1);
471 mutex_unlock(&rfkill->mutex);
474 return 0;
476 #else
477 #define rfkill_suspend NULL
478 #define rfkill_resume NULL
479 #endif
481 static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
482 unsigned long eventid,
483 void *data)
485 struct rfkill *rfkill = (struct rfkill *)data;
487 switch (eventid) {
488 case RFKILL_STATE_CHANGED:
489 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
490 break;
491 default:
492 break;
495 return NOTIFY_DONE;
498 static struct notifier_block rfkill_blocking_uevent_nb = {
499 .notifier_call = rfkill_blocking_uevent_notifier,
500 .priority = 0,
503 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
505 struct rfkill *rfkill = to_rfkill(dev);
506 int error;
508 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
509 if (error)
510 return error;
511 error = add_uevent_var(env, "RFKILL_TYPE=%s",
512 rfkill_get_type_str(rfkill->type));
513 if (error)
514 return error;
515 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
516 return error;
519 static struct class rfkill_class = {
520 .name = "rfkill",
521 .dev_release = rfkill_release,
522 .dev_attrs = rfkill_dev_attrs,
523 .suspend = rfkill_suspend,
524 .resume = rfkill_resume,
525 .dev_uevent = rfkill_dev_uevent,
528 static int rfkill_check_duplicity(const struct rfkill *rfkill)
530 struct rfkill *p;
531 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
533 memset(seen, 0, sizeof(seen));
535 list_for_each_entry(p, &rfkill_list, node) {
536 if (p == rfkill) {
537 WARN_ON(1);
538 return -EEXIST;
540 set_bit(p->type, seen);
543 /* 0: first switch of its kind */
544 return test_bit(rfkill->type, seen);
547 static int rfkill_add_switch(struct rfkill *rfkill)
549 int error;
551 mutex_lock(&rfkill_mutex);
553 error = rfkill_check_duplicity(rfkill);
554 if (error < 0)
555 goto unlock_out;
557 rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
559 list_add_tail(&rfkill->node, &rfkill_list);
561 error = 0;
562 unlock_out:
563 mutex_unlock(&rfkill_mutex);
565 return error;
568 static void rfkill_remove_switch(struct rfkill *rfkill)
570 mutex_lock(&rfkill_mutex);
571 list_del_init(&rfkill->node);
572 mutex_unlock(&rfkill_mutex);
574 mutex_lock(&rfkill->mutex);
575 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
576 mutex_unlock(&rfkill->mutex);
580 * rfkill_allocate - allocate memory for rfkill structure.
581 * @parent: device that has rf switch on it
582 * @type: type of the switch (RFKILL_TYPE_*)
584 * This function should be called by the network driver when it needs
585 * rfkill structure. Once the structure is allocated the driver should
586 * finish its initialization by setting the name, private data, enable_radio
587 * and disable_radio methods and then register it with rfkill_register().
589 * NOTE: If registration fails the structure shoudl be freed by calling
590 * rfkill_free() otherwise rfkill_unregister() should be used.
592 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
594 struct rfkill *rfkill;
595 struct device *dev;
597 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
598 if (!rfkill)
599 return NULL;
601 mutex_init(&rfkill->mutex);
602 INIT_LIST_HEAD(&rfkill->node);
603 rfkill->type = type;
605 dev = &rfkill->dev;
606 dev->class = &rfkill_class;
607 dev->parent = parent;
608 device_initialize(dev);
610 __module_get(THIS_MODULE);
612 return rfkill;
614 EXPORT_SYMBOL(rfkill_allocate);
617 * rfkill_free - Mark rfkill structure for deletion
618 * @rfkill: rfkill structure to be destroyed
620 * Decrements reference count of the rfkill structure so it is destroyed.
621 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
623 void rfkill_free(struct rfkill *rfkill)
625 if (rfkill)
626 put_device(&rfkill->dev);
628 EXPORT_SYMBOL(rfkill_free);
630 static void rfkill_led_trigger_register(struct rfkill *rfkill)
632 #ifdef CONFIG_RFKILL_LEDS
633 int error;
635 if (!rfkill->led_trigger.name)
636 rfkill->led_trigger.name = rfkill->dev.bus_id;
637 if (!rfkill->led_trigger.activate)
638 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
639 error = led_trigger_register(&rfkill->led_trigger);
640 if (error)
641 rfkill->led_trigger.name = NULL;
642 #endif /* CONFIG_RFKILL_LEDS */
645 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
647 #ifdef CONFIG_RFKILL_LEDS
648 if (rfkill->led_trigger.name) {
649 led_trigger_unregister(&rfkill->led_trigger);
650 rfkill->led_trigger.name = NULL;
652 #endif
656 * rfkill_register - Register a rfkill structure.
657 * @rfkill: rfkill structure to be registered
659 * This function should be called by the network driver when the rfkill
660 * structure needs to be registered. Immediately from registration the
661 * switch driver should be able to service calls to toggle_radio.
663 int rfkill_register(struct rfkill *rfkill)
665 static atomic_t rfkill_no = ATOMIC_INIT(0);
666 struct device *dev = &rfkill->dev;
667 int error;
669 if (!rfkill->toggle_radio)
670 return -EINVAL;
671 if (rfkill->type >= RFKILL_TYPE_MAX)
672 return -EINVAL;
674 snprintf(dev->bus_id, sizeof(dev->bus_id),
675 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
677 rfkill_led_trigger_register(rfkill);
679 error = rfkill_add_switch(rfkill);
680 if (error) {
681 rfkill_led_trigger_unregister(rfkill);
682 return error;
685 error = device_add(dev);
686 if (error) {
687 rfkill_remove_switch(rfkill);
688 rfkill_led_trigger_unregister(rfkill);
689 return error;
692 return 0;
694 EXPORT_SYMBOL(rfkill_register);
697 * rfkill_unregister - Unregister a rfkill structure.
698 * @rfkill: rfkill structure to be unregistered
700 * This function should be called by the network driver during device
701 * teardown to destroy rfkill structure. Note that rfkill_free() should
702 * _not_ be called after rfkill_unregister().
704 void rfkill_unregister(struct rfkill *rfkill)
706 device_del(&rfkill->dev);
707 rfkill_remove_switch(rfkill);
708 rfkill_led_trigger_unregister(rfkill);
709 put_device(&rfkill->dev);
711 EXPORT_SYMBOL(rfkill_unregister);
714 * Rfkill module initialization/deinitialization.
716 static int __init rfkill_init(void)
718 int error;
719 int i;
721 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
722 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
723 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
724 return -EINVAL;
726 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
727 rfkill_states[i] = rfkill_default_state;
729 error = class_register(&rfkill_class);
730 if (error) {
731 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
732 return error;
735 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
737 return 0;
740 static void __exit rfkill_exit(void)
742 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
743 class_unregister(&rfkill_class);
746 subsys_initcall(rfkill_init);
747 module_exit(rfkill_exit);