2 * Copyright (C) 2006 - 2007 Ivo van Doorn
3 * Copyright (C) 2007 Dmitry Torokhov
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/workqueue.h>
26 #include <linux/capability.h>
27 #include <linux/list.h>
28 #include <linux/mutex.h>
29 #include <linux/rfkill.h>
30 #include <linux/spinlock.h>
34 #define POLL_INTERVAL (5 * HZ)
36 #define RFKILL_BLOCK_HW BIT(0)
37 #define RFKILL_BLOCK_SW BIT(1)
38 #define RFKILL_BLOCK_SW_PREV BIT(2)
39 #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
42 #define RFKILL_BLOCK_SW_SETCALL BIT(31)
48 enum rfkill_type type;
55 const struct rfkill_ops *ops;
58 #ifdef CONFIG_RFKILL_LEDS
59 struct led_trigger led_trigger;
60 const char *ledtrigname;
64 struct list_head node;
66 struct delayed_work poll_work;
67 struct work_struct uevent_work;
68 struct work_struct sync_work;
70 #define to_rfkill(d) container_of(d, struct rfkill, dev)
74 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
75 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
76 MODULE_DESCRIPTION("RF switch support");
77 MODULE_LICENSE("GPL");
81 * The locking here should be made much smarter, we currently have
82 * a bit of a stupid situation because drivers might want to register
83 * the rfkill struct under their own lock, and take this lock during
84 * rfkill method calls -- which will cause an AB-BA deadlock situation.
86 * To fix that, we need to rework this code here to be mostly lock-free
87 * and only use the mutex for list manipulations, not to protect the
88 * various other global variables. Then we can avoid holding the mutex
89 * around driver operations, and all is happy.
91 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
92 static DEFINE_MUTEX(rfkill_global_mutex);
94 static unsigned int rfkill_default_state = 1;
95 module_param_named(default_state, rfkill_default_state, uint, 0444);
96 MODULE_PARM_DESC(default_state,
97 "Default initial state for all radio types, 0 = radio off");
101 } rfkill_global_states[NUM_RFKILL_TYPES];
103 static unsigned long rfkill_states_default_locked;
105 static bool rfkill_epo_lock_active;
108 #ifdef CONFIG_RFKILL_LEDS
109 static void rfkill_led_trigger_event(struct rfkill *rfkill)
111 struct led_trigger *trigger;
113 if (!rfkill->registered)
116 trigger = &rfkill->led_trigger;
118 if (rfkill->state & RFKILL_BLOCK_ANY)
119 led_trigger_event(trigger, LED_OFF);
121 led_trigger_event(trigger, LED_FULL);
124 static void rfkill_led_trigger_activate(struct led_classdev *led)
126 struct rfkill *rfkill;
128 rfkill = container_of(led->trigger, struct rfkill, led_trigger);
130 rfkill_led_trigger_event(rfkill);
133 const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
135 return rfkill->led_trigger.name;
137 EXPORT_SYMBOL(rfkill_get_led_trigger_name);
139 void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
143 rfkill->ledtrigname = name;
145 EXPORT_SYMBOL(rfkill_set_led_trigger_name);
147 static int rfkill_led_trigger_register(struct rfkill *rfkill)
149 rfkill->led_trigger.name = rfkill->ledtrigname
150 ? : dev_name(&rfkill->dev);
151 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
152 return led_trigger_register(&rfkill->led_trigger);
155 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
157 led_trigger_unregister(&rfkill->led_trigger);
160 static void rfkill_led_trigger_event(struct rfkill *rfkill)
164 static inline int rfkill_led_trigger_register(struct rfkill *rfkill)
169 static inline void rfkill_led_trigger_unregister(struct rfkill *rfkill)
172 #endif /* CONFIG_RFKILL_LEDS */
174 static void rfkill_uevent(struct rfkill *rfkill)
176 if (!rfkill->registered || rfkill->suspended)
179 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
182 static bool __rfkill_set_hw_state(struct rfkill *rfkill,
183 bool blocked, bool *change)
190 spin_lock_irqsave(&rfkill->lock, flags);
191 prev = !!(rfkill->state & RFKILL_BLOCK_HW);
193 rfkill->state |= RFKILL_BLOCK_HW;
195 rfkill->state &= ~RFKILL_BLOCK_HW;
196 *change = prev != blocked;
197 any = rfkill->state & RFKILL_BLOCK_ANY;
198 spin_unlock_irqrestore(&rfkill->lock, flags);
200 rfkill_led_trigger_event(rfkill);
206 * rfkill_set_block - wrapper for set_block method
208 * @rfkill: the rfkill struct to use
209 * @blocked: the new software state
211 * Calls the set_block method (when applicable) and handles notifications
214 static void rfkill_set_block(struct rfkill *rfkill, bool blocked)
220 * Some platforms (...!) generate input events which affect the
221 * _hard_ kill state -- whenever something tries to change the
222 * current software state query the hardware state too.
224 if (rfkill->ops->query)
225 rfkill->ops->query(rfkill, rfkill->data);
227 spin_lock_irqsave(&rfkill->lock, flags);
228 if (rfkill->state & RFKILL_BLOCK_SW)
229 rfkill->state |= RFKILL_BLOCK_SW_PREV;
231 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
234 rfkill->state |= RFKILL_BLOCK_SW;
236 rfkill->state &= ~RFKILL_BLOCK_SW;
238 rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
239 spin_unlock_irqrestore(&rfkill->lock, flags);
241 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
244 err = rfkill->ops->set_block(rfkill->data, blocked);
246 spin_lock_irqsave(&rfkill->lock, flags);
249 * Failed -- reset status to _prev, this may be different
250 * from what set set _PREV to earlier in this function
251 * if rfkill_set_sw_state was invoked.
253 if (rfkill->state & RFKILL_BLOCK_SW_PREV)
254 rfkill->state |= RFKILL_BLOCK_SW;
256 rfkill->state &= ~RFKILL_BLOCK_SW;
258 rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
259 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
260 spin_unlock_irqrestore(&rfkill->lock, flags);
262 rfkill_led_trigger_event(rfkill);
263 rfkill_uevent(rfkill);
267 * __rfkill_switch_all - Toggle state of all switches of given type
268 * @type: type of interfaces to be affected
269 * @state: the new state
271 * This function sets the state of all switches of given type,
272 * unless a specific switch is claimed by userspace (in which case,
273 * that switch is left alone) or suspended.
275 * Caller must have acquired rfkill_global_mutex.
277 static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
279 struct rfkill *rfkill;
281 rfkill_global_states[type].cur = blocked;
282 list_for_each_entry(rfkill, &rfkill_list, node) {
283 if (rfkill->type != type)
286 rfkill_set_block(rfkill, blocked);
291 * rfkill_switch_all - Toggle state of all switches of given type
292 * @type: type of interfaces to be affected
293 * @state: the new state
295 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
296 * Please refer to __rfkill_switch_all() for details.
298 * Does nothing if the EPO lock is active.
300 void rfkill_switch_all(enum rfkill_type type, bool blocked)
302 mutex_lock(&rfkill_global_mutex);
304 if (!rfkill_epo_lock_active)
305 __rfkill_switch_all(type, blocked);
307 mutex_unlock(&rfkill_global_mutex);
311 * rfkill_epo - emergency power off all transmitters
313 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
314 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
316 * The global state before the EPO is saved and can be restored later
317 * using rfkill_restore_states().
319 void rfkill_epo(void)
321 struct rfkill *rfkill;
324 mutex_lock(&rfkill_global_mutex);
326 rfkill_epo_lock_active = true;
327 list_for_each_entry(rfkill, &rfkill_list, node)
328 rfkill_set_block(rfkill, true);
330 for (i = 0; i < NUM_RFKILL_TYPES; i++) {
331 rfkill_global_states[i].def = rfkill_global_states[i].cur;
332 rfkill_global_states[i].cur = true;
334 mutex_unlock(&rfkill_global_mutex);
338 * rfkill_restore_states - restore global states
340 * Restore (and sync switches to) the global state from the
341 * states in rfkill_default_states. This can undo the effects of
342 * a call to rfkill_epo().
344 void rfkill_restore_states(void)
348 mutex_lock(&rfkill_global_mutex);
350 rfkill_epo_lock_active = false;
351 for (i = 0; i < NUM_RFKILL_TYPES; i++)
352 __rfkill_switch_all(i, rfkill_global_states[i].def);
353 mutex_unlock(&rfkill_global_mutex);
357 * rfkill_remove_epo_lock - unlock state changes
359 * Used by rfkill-input manually unlock state changes, when
360 * the EPO switch is deactivated.
362 void rfkill_remove_epo_lock(void)
364 mutex_lock(&rfkill_global_mutex);
365 rfkill_epo_lock_active = false;
366 mutex_unlock(&rfkill_global_mutex);
370 * rfkill_is_epo_lock_active - returns true EPO is active
372 * Returns 0 (false) if there is NOT an active EPO contidion,
373 * and 1 (true) if there is an active EPO contition, which
374 * locks all radios in one of the BLOCKED states.
376 * Can be called in atomic context.
378 bool rfkill_is_epo_lock_active(void)
380 return rfkill_epo_lock_active;
384 * rfkill_get_global_sw_state - returns global state for a type
385 * @type: the type to get the global state of
387 * Returns the current global state for a given wireless
390 bool rfkill_get_global_sw_state(const enum rfkill_type type)
392 return rfkill_global_states[type].cur;
395 void rfkill_set_global_sw_state(const enum rfkill_type type, bool blocked)
397 mutex_lock(&rfkill_global_mutex);
399 /* don't allow unblock when epo */
400 if (rfkill_epo_lock_active && !blocked)
404 if (rfkill_states_default_locked & BIT(type))
407 rfkill_states_default_locked |= BIT(type);
409 rfkill_global_states[type].cur = blocked;
410 rfkill_global_states[type].def = blocked;
412 mutex_unlock(&rfkill_global_mutex);
414 EXPORT_SYMBOL(rfkill_set_global_sw_state);
417 bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
421 ret = __rfkill_set_hw_state(rfkill, blocked, &change);
423 if (!rfkill->registered)
427 schedule_work(&rfkill->uevent_work);
431 EXPORT_SYMBOL(rfkill_set_hw_state);
433 static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
435 u32 bit = RFKILL_BLOCK_SW;
437 /* if in a ops->set_block right now, use other bit */
438 if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
439 bit = RFKILL_BLOCK_SW_PREV;
442 rfkill->state |= bit;
444 rfkill->state &= ~bit;
447 bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
454 spin_lock_irqsave(&rfkill->lock, flags);
455 prev = !!(rfkill->state & RFKILL_BLOCK_SW);
456 __rfkill_set_sw_state(rfkill, blocked);
457 hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
458 blocked = blocked || hwblock;
459 spin_unlock_irqrestore(&rfkill->lock, flags);
461 if (!rfkill->registered)
464 if (prev != blocked && !hwblock)
465 schedule_work(&rfkill->uevent_work);
467 rfkill_led_trigger_event(rfkill);
471 EXPORT_SYMBOL(rfkill_set_sw_state);
473 void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
480 spin_lock_irqsave(&rfkill->lock, flags);
483 * No need to care about prev/setblock ... this is for uevent only
484 * and that will get triggered by rfkill_set_block anyway.
486 swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
487 hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
488 __rfkill_set_sw_state(rfkill, sw);
490 spin_unlock_irqrestore(&rfkill->lock, flags);
492 if (!rfkill->registered)
495 if (swprev != sw || hwprev != hw)
496 schedule_work(&rfkill->uevent_work);
498 rfkill_led_trigger_event(rfkill);
500 EXPORT_SYMBOL(rfkill_set_states);
502 static ssize_t rfkill_name_show(struct device *dev,
503 struct device_attribute *attr,
506 struct rfkill *rfkill = to_rfkill(dev);
508 return sprintf(buf, "%s\n", rfkill->name);
511 static const char *rfkill_get_type_str(enum rfkill_type type)
514 case RFKILL_TYPE_WLAN:
516 case RFKILL_TYPE_BLUETOOTH:
518 case RFKILL_TYPE_UWB:
519 return "ultrawideband";
520 case RFKILL_TYPE_WIMAX:
522 case RFKILL_TYPE_WWAN:
528 BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_WWAN + 1);
531 static ssize_t rfkill_type_show(struct device *dev,
532 struct device_attribute *attr,
535 struct rfkill *rfkill = to_rfkill(dev);
537 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
540 static u8 user_state_from_blocked(unsigned long state)
542 if (state & RFKILL_BLOCK_HW)
543 return RFKILL_USER_STATE_HARD_BLOCKED;
544 if (state & RFKILL_BLOCK_SW)
545 return RFKILL_USER_STATE_SOFT_BLOCKED;
547 return RFKILL_USER_STATE_UNBLOCKED;
550 static ssize_t rfkill_state_show(struct device *dev,
551 struct device_attribute *attr,
554 struct rfkill *rfkill = to_rfkill(dev);
558 spin_lock_irqsave(&rfkill->lock, flags);
559 state = rfkill->state;
560 spin_unlock_irqrestore(&rfkill->lock, flags);
562 return sprintf(buf, "%d\n", user_state_from_blocked(state));
565 static ssize_t rfkill_state_store(struct device *dev,
566 struct device_attribute *attr,
567 const char *buf, size_t count)
570 * The intention was that userspace can only take control over
571 * a given device when/if rfkill-input doesn't control it due
572 * to user_claim. Since user_claim is currently unsupported,
573 * we never support changing the state from userspace -- this
574 * can be implemented again later.
580 static ssize_t rfkill_claim_show(struct device *dev,
581 struct device_attribute *attr,
584 return sprintf(buf, "%d\n", 0);
587 static ssize_t rfkill_claim_store(struct device *dev,
588 struct device_attribute *attr,
589 const char *buf, size_t count)
594 static struct device_attribute rfkill_dev_attrs[] = {
595 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
596 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
597 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
598 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
602 static void rfkill_release(struct device *dev)
604 struct rfkill *rfkill = to_rfkill(dev);
609 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
611 struct rfkill *rfkill = to_rfkill(dev);
616 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
619 error = add_uevent_var(env, "RFKILL_TYPE=%s",
620 rfkill_get_type_str(rfkill->type));
623 spin_lock_irqsave(&rfkill->lock, flags);
624 state = rfkill->state;
625 spin_unlock_irqrestore(&rfkill->lock, flags);
626 error = add_uevent_var(env, "RFKILL_STATE=%d",
627 user_state_from_blocked(state));
631 void rfkill_pause_polling(struct rfkill *rfkill)
635 if (!rfkill->ops->poll)
638 cancel_delayed_work_sync(&rfkill->poll_work);
640 EXPORT_SYMBOL(rfkill_pause_polling);
642 void rfkill_resume_polling(struct rfkill *rfkill)
646 if (!rfkill->ops->poll)
649 schedule_work(&rfkill->poll_work.work);
651 EXPORT_SYMBOL(rfkill_resume_polling);
653 static int rfkill_suspend(struct device *dev, pm_message_t state)
655 struct rfkill *rfkill = to_rfkill(dev);
657 rfkill_pause_polling(rfkill);
659 rfkill->suspended = true;
664 static int rfkill_resume(struct device *dev)
666 struct rfkill *rfkill = to_rfkill(dev);
669 mutex_lock(&rfkill_global_mutex);
670 cur = rfkill_global_states[rfkill->type].cur;
671 rfkill_set_block(rfkill, cur);
672 mutex_unlock(&rfkill_global_mutex);
674 rfkill->suspended = false;
676 schedule_work(&rfkill->uevent_work);
678 rfkill_resume_polling(rfkill);
683 static struct class rfkill_class = {
685 .dev_release = rfkill_release,
686 .dev_attrs = rfkill_dev_attrs,
687 .dev_uevent = rfkill_dev_uevent,
688 .suspend = rfkill_suspend,
689 .resume = rfkill_resume,
693 struct rfkill * __must_check rfkill_alloc(const char *name,
694 struct device *parent,
695 const enum rfkill_type type,
696 const struct rfkill_ops *ops,
699 struct rfkill *rfkill;
705 if (WARN_ON(!ops->set_block))
711 if (WARN_ON(type >= NUM_RFKILL_TYPES))
714 rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
718 spin_lock_init(&rfkill->lock);
719 INIT_LIST_HEAD(&rfkill->node);
723 rfkill->data = ops_data;
726 dev->class = &rfkill_class;
727 dev->parent = parent;
728 device_initialize(dev);
732 EXPORT_SYMBOL(rfkill_alloc);
734 static void rfkill_poll(struct work_struct *work)
736 struct rfkill *rfkill;
738 rfkill = container_of(work, struct rfkill, poll_work.work);
741 * Poll hardware state -- driver will use one of the
742 * rfkill_set{,_hw,_sw}_state functions and use its
743 * return value to update the current status.
745 rfkill->ops->poll(rfkill, rfkill->data);
747 schedule_delayed_work(&rfkill->poll_work,
748 round_jiffies_relative(POLL_INTERVAL));
751 static void rfkill_uevent_work(struct work_struct *work)
753 struct rfkill *rfkill;
755 rfkill = container_of(work, struct rfkill, uevent_work);
757 rfkill_uevent(rfkill);
760 static void rfkill_sync_work(struct work_struct *work)
762 struct rfkill *rfkill;
765 rfkill = container_of(work, struct rfkill, sync_work);
767 mutex_lock(&rfkill_global_mutex);
768 cur = rfkill_global_states[rfkill->type].cur;
769 rfkill_set_block(rfkill, cur);
770 mutex_unlock(&rfkill_global_mutex);
773 int __must_check rfkill_register(struct rfkill *rfkill)
775 static unsigned long rfkill_no;
776 struct device *dev = &rfkill->dev;
781 mutex_lock(&rfkill_global_mutex);
783 if (rfkill->registered) {
788 dev_set_name(dev, "rfkill%lu", rfkill_no);
791 if (!(rfkill_states_default_locked & BIT(rfkill->type))) {
792 /* first of its kind */
793 BUILD_BUG_ON(NUM_RFKILL_TYPES >
794 sizeof(rfkill_states_default_locked) * 8);
795 rfkill_states_default_locked |= BIT(rfkill->type);
796 rfkill_global_states[rfkill->type].cur =
797 rfkill_global_states[rfkill->type].def;
800 list_add_tail(&rfkill->node, &rfkill_list);
802 error = device_add(dev);
806 error = rfkill_led_trigger_register(rfkill);
810 rfkill->registered = true;
812 if (rfkill->ops->poll) {
813 INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
814 schedule_delayed_work(&rfkill->poll_work,
815 round_jiffies_relative(POLL_INTERVAL));
818 INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
820 INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
821 schedule_work(&rfkill->sync_work);
823 mutex_unlock(&rfkill_global_mutex);
827 device_del(&rfkill->dev);
829 list_del_init(&rfkill->node);
831 mutex_unlock(&rfkill_global_mutex);
834 EXPORT_SYMBOL(rfkill_register);
836 void rfkill_unregister(struct rfkill *rfkill)
840 if (rfkill->ops->poll)
841 cancel_delayed_work_sync(&rfkill->poll_work);
843 cancel_work_sync(&rfkill->uevent_work);
844 cancel_work_sync(&rfkill->sync_work);
846 rfkill->registered = false;
848 device_del(&rfkill->dev);
850 mutex_lock(&rfkill_global_mutex);
851 list_del_init(&rfkill->node);
852 mutex_unlock(&rfkill_global_mutex);
854 rfkill_led_trigger_unregister(rfkill);
856 EXPORT_SYMBOL(rfkill_unregister);
858 void rfkill_destroy(struct rfkill *rfkill)
861 put_device(&rfkill->dev);
863 EXPORT_SYMBOL(rfkill_destroy);
866 static int __init rfkill_init(void)
871 for (i = 0; i < NUM_RFKILL_TYPES; i++)
872 rfkill_global_states[i].def = !rfkill_default_state;
874 error = class_register(&rfkill_class);
878 #ifdef CONFIG_RFKILL_INPUT
879 error = rfkill_handler_init();
881 class_unregister(&rfkill_class);
887 subsys_initcall(rfkill_init);
889 static void __exit rfkill_exit(void)
891 #ifdef CONFIG_RFKILL_INPUT
892 rfkill_handler_exit();
894 class_unregister(&rfkill_class);
896 module_exit(rfkill_exit);