Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / net / rfkill / core.c
blob1355f5ca8d2273e992f57c8a6e7c811c53072b2e
1 /*
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, see <http://www.gnu.org/licenses/>.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/workqueue.h>
24 #include <linux/capability.h>
25 #include <linux/list.h>
26 #include <linux/mutex.h>
27 #include <linux/rfkill.h>
28 #include <linux/sched.h>
29 #include <linux/spinlock.h>
30 #include <linux/device.h>
31 #include <linux/miscdevice.h>
32 #include <linux/wait.h>
33 #include <linux/poll.h>
34 #include <linux/fs.h>
35 #include <linux/slab.h>
37 #include "rfkill.h"
39 #define POLL_INTERVAL (5 * HZ)
41 #define RFKILL_BLOCK_HW BIT(0)
42 #define RFKILL_BLOCK_SW BIT(1)
43 #define RFKILL_BLOCK_SW_PREV BIT(2)
44 #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
45 RFKILL_BLOCK_SW |\
46 RFKILL_BLOCK_SW_PREV)
47 #define RFKILL_BLOCK_SW_SETCALL BIT(31)
49 struct rfkill {
50 spinlock_t lock;
52 enum rfkill_type type;
54 unsigned long state;
56 u32 idx;
58 bool registered;
59 bool persistent;
60 bool polling_paused;
61 bool suspended;
63 const struct rfkill_ops *ops;
64 void *data;
66 #ifdef CONFIG_RFKILL_LEDS
67 struct led_trigger led_trigger;
68 const char *ledtrigname;
69 #endif
71 struct device dev;
72 struct list_head node;
74 struct delayed_work poll_work;
75 struct work_struct uevent_work;
76 struct work_struct sync_work;
77 char name[];
79 #define to_rfkill(d) container_of(d, struct rfkill, dev)
81 struct rfkill_int_event {
82 struct list_head list;
83 struct rfkill_event ev;
86 struct rfkill_data {
87 struct list_head list;
88 struct list_head events;
89 struct mutex mtx;
90 wait_queue_head_t read_wait;
91 bool input_handler;
95 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
96 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
97 MODULE_DESCRIPTION("RF switch support");
98 MODULE_LICENSE("GPL");
102 * The locking here should be made much smarter, we currently have
103 * a bit of a stupid situation because drivers might want to register
104 * the rfkill struct under their own lock, and take this lock during
105 * rfkill method calls -- which will cause an AB-BA deadlock situation.
107 * To fix that, we need to rework this code here to be mostly lock-free
108 * and only use the mutex for list manipulations, not to protect the
109 * various other global variables. Then we can avoid holding the mutex
110 * around driver operations, and all is happy.
112 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
113 static DEFINE_MUTEX(rfkill_global_mutex);
114 static LIST_HEAD(rfkill_fds); /* list of open fds of /dev/rfkill */
116 static unsigned int rfkill_default_state = 1;
117 module_param_named(default_state, rfkill_default_state, uint, 0444);
118 MODULE_PARM_DESC(default_state,
119 "Default initial state for all radio types, 0 = radio off");
121 static struct {
122 bool cur, sav;
123 } rfkill_global_states[NUM_RFKILL_TYPES];
125 static bool rfkill_epo_lock_active;
128 #ifdef CONFIG_RFKILL_LEDS
129 static void rfkill_led_trigger_event(struct rfkill *rfkill)
131 struct led_trigger *trigger;
133 if (!rfkill->registered)
134 return;
136 trigger = &rfkill->led_trigger;
138 if (rfkill->state & RFKILL_BLOCK_ANY)
139 led_trigger_event(trigger, LED_OFF);
140 else
141 led_trigger_event(trigger, LED_FULL);
144 static int rfkill_led_trigger_activate(struct led_classdev *led)
146 struct rfkill *rfkill;
148 rfkill = container_of(led->trigger, struct rfkill, led_trigger);
150 rfkill_led_trigger_event(rfkill);
152 return 0;
155 const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
157 return rfkill->led_trigger.name;
159 EXPORT_SYMBOL(rfkill_get_led_trigger_name);
161 void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
163 BUG_ON(!rfkill);
165 rfkill->ledtrigname = name;
167 EXPORT_SYMBOL(rfkill_set_led_trigger_name);
169 static int rfkill_led_trigger_register(struct rfkill *rfkill)
171 rfkill->led_trigger.name = rfkill->ledtrigname
172 ? : dev_name(&rfkill->dev);
173 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
174 return led_trigger_register(&rfkill->led_trigger);
177 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
179 led_trigger_unregister(&rfkill->led_trigger);
182 static struct led_trigger rfkill_any_led_trigger;
183 static struct led_trigger rfkill_none_led_trigger;
184 static struct work_struct rfkill_global_led_trigger_work;
186 static void rfkill_global_led_trigger_worker(struct work_struct *work)
188 enum led_brightness brightness = LED_OFF;
189 struct rfkill *rfkill;
191 mutex_lock(&rfkill_global_mutex);
192 list_for_each_entry(rfkill, &rfkill_list, node) {
193 if (!(rfkill->state & RFKILL_BLOCK_ANY)) {
194 brightness = LED_FULL;
195 break;
198 mutex_unlock(&rfkill_global_mutex);
200 led_trigger_event(&rfkill_any_led_trigger, brightness);
201 led_trigger_event(&rfkill_none_led_trigger,
202 brightness == LED_OFF ? LED_FULL : LED_OFF);
205 static void rfkill_global_led_trigger_event(void)
207 schedule_work(&rfkill_global_led_trigger_work);
210 static int rfkill_global_led_trigger_register(void)
212 int ret;
214 INIT_WORK(&rfkill_global_led_trigger_work,
215 rfkill_global_led_trigger_worker);
217 rfkill_any_led_trigger.name = "rfkill-any";
218 ret = led_trigger_register(&rfkill_any_led_trigger);
219 if (ret)
220 return ret;
222 rfkill_none_led_trigger.name = "rfkill-none";
223 ret = led_trigger_register(&rfkill_none_led_trigger);
224 if (ret)
225 led_trigger_unregister(&rfkill_any_led_trigger);
226 else
227 /* Delay activation until all global triggers are registered */
228 rfkill_global_led_trigger_event();
230 return ret;
233 static void rfkill_global_led_trigger_unregister(void)
235 led_trigger_unregister(&rfkill_none_led_trigger);
236 led_trigger_unregister(&rfkill_any_led_trigger);
237 cancel_work_sync(&rfkill_global_led_trigger_work);
239 #else
240 static void rfkill_led_trigger_event(struct rfkill *rfkill)
244 static inline int rfkill_led_trigger_register(struct rfkill *rfkill)
246 return 0;
249 static inline void rfkill_led_trigger_unregister(struct rfkill *rfkill)
253 static void rfkill_global_led_trigger_event(void)
257 static int rfkill_global_led_trigger_register(void)
259 return 0;
262 static void rfkill_global_led_trigger_unregister(void)
265 #endif /* CONFIG_RFKILL_LEDS */
267 static void rfkill_fill_event(struct rfkill_event *ev, struct rfkill *rfkill,
268 enum rfkill_operation op)
270 unsigned long flags;
272 ev->idx = rfkill->idx;
273 ev->type = rfkill->type;
274 ev->op = op;
276 spin_lock_irqsave(&rfkill->lock, flags);
277 ev->hard = !!(rfkill->state & RFKILL_BLOCK_HW);
278 ev->soft = !!(rfkill->state & (RFKILL_BLOCK_SW |
279 RFKILL_BLOCK_SW_PREV));
280 spin_unlock_irqrestore(&rfkill->lock, flags);
283 static void rfkill_send_events(struct rfkill *rfkill, enum rfkill_operation op)
285 struct rfkill_data *data;
286 struct rfkill_int_event *ev;
288 list_for_each_entry(data, &rfkill_fds, list) {
289 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
290 if (!ev)
291 continue;
292 rfkill_fill_event(&ev->ev, rfkill, op);
293 mutex_lock(&data->mtx);
294 list_add_tail(&ev->list, &data->events);
295 mutex_unlock(&data->mtx);
296 wake_up_interruptible(&data->read_wait);
300 static void rfkill_event(struct rfkill *rfkill)
302 if (!rfkill->registered)
303 return;
305 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
307 /* also send event to /dev/rfkill */
308 rfkill_send_events(rfkill, RFKILL_OP_CHANGE);
312 * rfkill_set_block - wrapper for set_block method
314 * @rfkill: the rfkill struct to use
315 * @blocked: the new software state
317 * Calls the set_block method (when applicable) and handles notifications
318 * etc. as well.
320 static void rfkill_set_block(struct rfkill *rfkill, bool blocked)
322 unsigned long flags;
323 bool prev, curr;
324 int err;
326 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
327 return;
330 * Some platforms (...!) generate input events which affect the
331 * _hard_ kill state -- whenever something tries to change the
332 * current software state query the hardware state too.
334 if (rfkill->ops->query)
335 rfkill->ops->query(rfkill, rfkill->data);
337 spin_lock_irqsave(&rfkill->lock, flags);
338 prev = rfkill->state & RFKILL_BLOCK_SW;
340 if (prev)
341 rfkill->state |= RFKILL_BLOCK_SW_PREV;
342 else
343 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
345 if (blocked)
346 rfkill->state |= RFKILL_BLOCK_SW;
347 else
348 rfkill->state &= ~RFKILL_BLOCK_SW;
350 rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
351 spin_unlock_irqrestore(&rfkill->lock, flags);
353 err = rfkill->ops->set_block(rfkill->data, blocked);
355 spin_lock_irqsave(&rfkill->lock, flags);
356 if (err) {
358 * Failed -- reset status to _PREV, which may be different
359 * from what we have set _PREV to earlier in this function
360 * if rfkill_set_sw_state was invoked.
362 if (rfkill->state & RFKILL_BLOCK_SW_PREV)
363 rfkill->state |= RFKILL_BLOCK_SW;
364 else
365 rfkill->state &= ~RFKILL_BLOCK_SW;
367 rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
368 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
369 curr = rfkill->state & RFKILL_BLOCK_SW;
370 spin_unlock_irqrestore(&rfkill->lock, flags);
372 rfkill_led_trigger_event(rfkill);
373 rfkill_global_led_trigger_event();
375 if (prev != curr)
376 rfkill_event(rfkill);
379 static void rfkill_update_global_state(enum rfkill_type type, bool blocked)
381 int i;
383 if (type != RFKILL_TYPE_ALL) {
384 rfkill_global_states[type].cur = blocked;
385 return;
388 for (i = 0; i < NUM_RFKILL_TYPES; i++)
389 rfkill_global_states[i].cur = blocked;
392 #ifdef CONFIG_RFKILL_INPUT
393 static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
396 * __rfkill_switch_all - Toggle state of all switches of given type
397 * @type: type of interfaces to be affected
398 * @blocked: the new state
400 * This function sets the state of all switches of given type,
401 * unless a specific switch is suspended.
403 * Caller must have acquired rfkill_global_mutex.
405 static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
407 struct rfkill *rfkill;
409 rfkill_update_global_state(type, blocked);
410 list_for_each_entry(rfkill, &rfkill_list, node) {
411 if (rfkill->type != type && type != RFKILL_TYPE_ALL)
412 continue;
414 rfkill_set_block(rfkill, blocked);
419 * rfkill_switch_all - Toggle state of all switches of given type
420 * @type: type of interfaces to be affected
421 * @blocked: the new state
423 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
424 * Please refer to __rfkill_switch_all() for details.
426 * Does nothing if the EPO lock is active.
428 void rfkill_switch_all(enum rfkill_type type, bool blocked)
430 if (atomic_read(&rfkill_input_disabled))
431 return;
433 mutex_lock(&rfkill_global_mutex);
435 if (!rfkill_epo_lock_active)
436 __rfkill_switch_all(type, blocked);
438 mutex_unlock(&rfkill_global_mutex);
442 * rfkill_epo - emergency power off all transmitters
444 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
445 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
447 * The global state before the EPO is saved and can be restored later
448 * using rfkill_restore_states().
450 void rfkill_epo(void)
452 struct rfkill *rfkill;
453 int i;
455 if (atomic_read(&rfkill_input_disabled))
456 return;
458 mutex_lock(&rfkill_global_mutex);
460 rfkill_epo_lock_active = true;
461 list_for_each_entry(rfkill, &rfkill_list, node)
462 rfkill_set_block(rfkill, true);
464 for (i = 0; i < NUM_RFKILL_TYPES; i++) {
465 rfkill_global_states[i].sav = rfkill_global_states[i].cur;
466 rfkill_global_states[i].cur = true;
469 mutex_unlock(&rfkill_global_mutex);
473 * rfkill_restore_states - restore global states
475 * Restore (and sync switches to) the global state from the
476 * states in rfkill_default_states. This can undo the effects of
477 * a call to rfkill_epo().
479 void rfkill_restore_states(void)
481 int i;
483 if (atomic_read(&rfkill_input_disabled))
484 return;
486 mutex_lock(&rfkill_global_mutex);
488 rfkill_epo_lock_active = false;
489 for (i = 0; i < NUM_RFKILL_TYPES; i++)
490 __rfkill_switch_all(i, rfkill_global_states[i].sav);
491 mutex_unlock(&rfkill_global_mutex);
495 * rfkill_remove_epo_lock - unlock state changes
497 * Used by rfkill-input manually unlock state changes, when
498 * the EPO switch is deactivated.
500 void rfkill_remove_epo_lock(void)
502 if (atomic_read(&rfkill_input_disabled))
503 return;
505 mutex_lock(&rfkill_global_mutex);
506 rfkill_epo_lock_active = false;
507 mutex_unlock(&rfkill_global_mutex);
511 * rfkill_is_epo_lock_active - returns true EPO is active
513 * Returns 0 (false) if there is NOT an active EPO contidion,
514 * and 1 (true) if there is an active EPO contition, which
515 * locks all radios in one of the BLOCKED states.
517 * Can be called in atomic context.
519 bool rfkill_is_epo_lock_active(void)
521 return rfkill_epo_lock_active;
525 * rfkill_get_global_sw_state - returns global state for a type
526 * @type: the type to get the global state of
528 * Returns the current global state for a given wireless
529 * device type.
531 bool rfkill_get_global_sw_state(const enum rfkill_type type)
533 return rfkill_global_states[type].cur;
535 #endif
537 bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
539 unsigned long flags;
540 bool ret, prev;
542 BUG_ON(!rfkill);
544 spin_lock_irqsave(&rfkill->lock, flags);
545 prev = !!(rfkill->state & RFKILL_BLOCK_HW);
546 if (blocked)
547 rfkill->state |= RFKILL_BLOCK_HW;
548 else
549 rfkill->state &= ~RFKILL_BLOCK_HW;
550 ret = !!(rfkill->state & RFKILL_BLOCK_ANY);
551 spin_unlock_irqrestore(&rfkill->lock, flags);
553 rfkill_led_trigger_event(rfkill);
554 rfkill_global_led_trigger_event();
556 if (rfkill->registered && prev != blocked)
557 schedule_work(&rfkill->uevent_work);
559 return ret;
561 EXPORT_SYMBOL(rfkill_set_hw_state);
563 static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
565 u32 bit = RFKILL_BLOCK_SW;
567 /* if in a ops->set_block right now, use other bit */
568 if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
569 bit = RFKILL_BLOCK_SW_PREV;
571 if (blocked)
572 rfkill->state |= bit;
573 else
574 rfkill->state &= ~bit;
577 bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
579 unsigned long flags;
580 bool prev, hwblock;
582 BUG_ON(!rfkill);
584 spin_lock_irqsave(&rfkill->lock, flags);
585 prev = !!(rfkill->state & RFKILL_BLOCK_SW);
586 __rfkill_set_sw_state(rfkill, blocked);
587 hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
588 blocked = blocked || hwblock;
589 spin_unlock_irqrestore(&rfkill->lock, flags);
591 if (!rfkill->registered)
592 return blocked;
594 if (prev != blocked && !hwblock)
595 schedule_work(&rfkill->uevent_work);
597 rfkill_led_trigger_event(rfkill);
598 rfkill_global_led_trigger_event();
600 return blocked;
602 EXPORT_SYMBOL(rfkill_set_sw_state);
604 void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
606 unsigned long flags;
608 BUG_ON(!rfkill);
609 BUG_ON(rfkill->registered);
611 spin_lock_irqsave(&rfkill->lock, flags);
612 __rfkill_set_sw_state(rfkill, blocked);
613 rfkill->persistent = true;
614 spin_unlock_irqrestore(&rfkill->lock, flags);
616 EXPORT_SYMBOL(rfkill_init_sw_state);
618 void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
620 unsigned long flags;
621 bool swprev, hwprev;
623 BUG_ON(!rfkill);
625 spin_lock_irqsave(&rfkill->lock, flags);
628 * No need to care about prev/setblock ... this is for uevent only
629 * and that will get triggered by rfkill_set_block anyway.
631 swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
632 hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
633 __rfkill_set_sw_state(rfkill, sw);
634 if (hw)
635 rfkill->state |= RFKILL_BLOCK_HW;
636 else
637 rfkill->state &= ~RFKILL_BLOCK_HW;
639 spin_unlock_irqrestore(&rfkill->lock, flags);
641 if (!rfkill->registered) {
642 rfkill->persistent = true;
643 } else {
644 if (swprev != sw || hwprev != hw)
645 schedule_work(&rfkill->uevent_work);
647 rfkill_led_trigger_event(rfkill);
648 rfkill_global_led_trigger_event();
651 EXPORT_SYMBOL(rfkill_set_states);
653 static const char * const rfkill_types[] = {
654 NULL, /* RFKILL_TYPE_ALL */
655 "wlan",
656 "bluetooth",
657 "ultrawideband",
658 "wimax",
659 "wwan",
660 "gps",
661 "fm",
662 "nfc",
665 enum rfkill_type rfkill_find_type(const char *name)
667 int i;
669 BUILD_BUG_ON(ARRAY_SIZE(rfkill_types) != NUM_RFKILL_TYPES);
671 if (!name)
672 return RFKILL_TYPE_ALL;
674 for (i = 1; i < NUM_RFKILL_TYPES; i++)
675 if (!strcmp(name, rfkill_types[i]))
676 return i;
677 return RFKILL_TYPE_ALL;
679 EXPORT_SYMBOL(rfkill_find_type);
681 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
682 char *buf)
684 struct rfkill *rfkill = to_rfkill(dev);
686 return sprintf(buf, "%s\n", rfkill->name);
688 static DEVICE_ATTR_RO(name);
690 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
691 char *buf)
693 struct rfkill *rfkill = to_rfkill(dev);
695 return sprintf(buf, "%s\n", rfkill_types[rfkill->type]);
697 static DEVICE_ATTR_RO(type);
699 static ssize_t index_show(struct device *dev, struct device_attribute *attr,
700 char *buf)
702 struct rfkill *rfkill = to_rfkill(dev);
704 return sprintf(buf, "%d\n", rfkill->idx);
706 static DEVICE_ATTR_RO(index);
708 static ssize_t persistent_show(struct device *dev,
709 struct device_attribute *attr, char *buf)
711 struct rfkill *rfkill = to_rfkill(dev);
713 return sprintf(buf, "%d\n", rfkill->persistent);
715 static DEVICE_ATTR_RO(persistent);
717 static ssize_t hard_show(struct device *dev, struct device_attribute *attr,
718 char *buf)
720 struct rfkill *rfkill = to_rfkill(dev);
722 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 );
724 static DEVICE_ATTR_RO(hard);
726 static ssize_t soft_show(struct device *dev, struct device_attribute *attr,
727 char *buf)
729 struct rfkill *rfkill = to_rfkill(dev);
731 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 );
734 static ssize_t soft_store(struct device *dev, struct device_attribute *attr,
735 const char *buf, size_t count)
737 struct rfkill *rfkill = to_rfkill(dev);
738 unsigned long state;
739 int err;
741 if (!capable(CAP_NET_ADMIN))
742 return -EPERM;
744 err = kstrtoul(buf, 0, &state);
745 if (err)
746 return err;
748 if (state > 1 )
749 return -EINVAL;
751 mutex_lock(&rfkill_global_mutex);
752 rfkill_set_block(rfkill, state);
753 mutex_unlock(&rfkill_global_mutex);
755 return count;
757 static DEVICE_ATTR_RW(soft);
759 static u8 user_state_from_blocked(unsigned long state)
761 if (state & RFKILL_BLOCK_HW)
762 return RFKILL_USER_STATE_HARD_BLOCKED;
763 if (state & RFKILL_BLOCK_SW)
764 return RFKILL_USER_STATE_SOFT_BLOCKED;
766 return RFKILL_USER_STATE_UNBLOCKED;
769 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
770 char *buf)
772 struct rfkill *rfkill = to_rfkill(dev);
774 return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state));
777 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
780 struct rfkill *rfkill = to_rfkill(dev);
781 unsigned long state;
782 int err;
784 if (!capable(CAP_NET_ADMIN))
785 return -EPERM;
787 err = kstrtoul(buf, 0, &state);
788 if (err)
789 return err;
791 if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
792 state != RFKILL_USER_STATE_UNBLOCKED)
793 return -EINVAL;
795 mutex_lock(&rfkill_global_mutex);
796 rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
797 mutex_unlock(&rfkill_global_mutex);
799 return count;
801 static DEVICE_ATTR_RW(state);
803 static struct attribute *rfkill_dev_attrs[] = {
804 &dev_attr_name.attr,
805 &dev_attr_type.attr,
806 &dev_attr_index.attr,
807 &dev_attr_persistent.attr,
808 &dev_attr_state.attr,
809 &dev_attr_soft.attr,
810 &dev_attr_hard.attr,
811 NULL,
813 ATTRIBUTE_GROUPS(rfkill_dev);
815 static void rfkill_release(struct device *dev)
817 struct rfkill *rfkill = to_rfkill(dev);
819 kfree(rfkill);
822 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
824 struct rfkill *rfkill = to_rfkill(dev);
825 unsigned long flags;
826 u32 state;
827 int error;
829 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
830 if (error)
831 return error;
832 error = add_uevent_var(env, "RFKILL_TYPE=%s",
833 rfkill_types[rfkill->type]);
834 if (error)
835 return error;
836 spin_lock_irqsave(&rfkill->lock, flags);
837 state = rfkill->state;
838 spin_unlock_irqrestore(&rfkill->lock, flags);
839 error = add_uevent_var(env, "RFKILL_STATE=%d",
840 user_state_from_blocked(state));
841 return error;
844 void rfkill_pause_polling(struct rfkill *rfkill)
846 BUG_ON(!rfkill);
848 if (!rfkill->ops->poll)
849 return;
851 rfkill->polling_paused = true;
852 cancel_delayed_work_sync(&rfkill->poll_work);
854 EXPORT_SYMBOL(rfkill_pause_polling);
856 void rfkill_resume_polling(struct rfkill *rfkill)
858 BUG_ON(!rfkill);
860 if (!rfkill->ops->poll)
861 return;
863 rfkill->polling_paused = false;
865 if (rfkill->suspended)
866 return;
868 queue_delayed_work(system_power_efficient_wq,
869 &rfkill->poll_work, 0);
871 EXPORT_SYMBOL(rfkill_resume_polling);
873 #ifdef CONFIG_PM_SLEEP
874 static int rfkill_suspend(struct device *dev)
876 struct rfkill *rfkill = to_rfkill(dev);
878 rfkill->suspended = true;
879 cancel_delayed_work_sync(&rfkill->poll_work);
881 return 0;
884 static int rfkill_resume(struct device *dev)
886 struct rfkill *rfkill = to_rfkill(dev);
887 bool cur;
889 rfkill->suspended = false;
891 if (!rfkill->persistent) {
892 cur = !!(rfkill->state & RFKILL_BLOCK_SW);
893 rfkill_set_block(rfkill, cur);
896 if (rfkill->ops->poll && !rfkill->polling_paused)
897 queue_delayed_work(system_power_efficient_wq,
898 &rfkill->poll_work, 0);
900 return 0;
903 static SIMPLE_DEV_PM_OPS(rfkill_pm_ops, rfkill_suspend, rfkill_resume);
904 #define RFKILL_PM_OPS (&rfkill_pm_ops)
905 #else
906 #define RFKILL_PM_OPS NULL
907 #endif
909 static struct class rfkill_class = {
910 .name = "rfkill",
911 .dev_release = rfkill_release,
912 .dev_groups = rfkill_dev_groups,
913 .dev_uevent = rfkill_dev_uevent,
914 .pm = RFKILL_PM_OPS,
917 bool rfkill_blocked(struct rfkill *rfkill)
919 unsigned long flags;
920 u32 state;
922 spin_lock_irqsave(&rfkill->lock, flags);
923 state = rfkill->state;
924 spin_unlock_irqrestore(&rfkill->lock, flags);
926 return !!(state & RFKILL_BLOCK_ANY);
928 EXPORT_SYMBOL(rfkill_blocked);
931 struct rfkill * __must_check rfkill_alloc(const char *name,
932 struct device *parent,
933 const enum rfkill_type type,
934 const struct rfkill_ops *ops,
935 void *ops_data)
937 struct rfkill *rfkill;
938 struct device *dev;
940 if (WARN_ON(!ops))
941 return NULL;
943 if (WARN_ON(!ops->set_block))
944 return NULL;
946 if (WARN_ON(!name))
947 return NULL;
949 if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
950 return NULL;
952 rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
953 if (!rfkill)
954 return NULL;
956 spin_lock_init(&rfkill->lock);
957 INIT_LIST_HEAD(&rfkill->node);
958 rfkill->type = type;
959 strcpy(rfkill->name, name);
960 rfkill->ops = ops;
961 rfkill->data = ops_data;
963 dev = &rfkill->dev;
964 dev->class = &rfkill_class;
965 dev->parent = parent;
966 device_initialize(dev);
968 return rfkill;
970 EXPORT_SYMBOL(rfkill_alloc);
972 static void rfkill_poll(struct work_struct *work)
974 struct rfkill *rfkill;
976 rfkill = container_of(work, struct rfkill, poll_work.work);
979 * Poll hardware state -- driver will use one of the
980 * rfkill_set{,_hw,_sw}_state functions and use its
981 * return value to update the current status.
983 rfkill->ops->poll(rfkill, rfkill->data);
985 queue_delayed_work(system_power_efficient_wq,
986 &rfkill->poll_work,
987 round_jiffies_relative(POLL_INTERVAL));
990 static void rfkill_uevent_work(struct work_struct *work)
992 struct rfkill *rfkill;
994 rfkill = container_of(work, struct rfkill, uevent_work);
996 mutex_lock(&rfkill_global_mutex);
997 rfkill_event(rfkill);
998 mutex_unlock(&rfkill_global_mutex);
1001 static void rfkill_sync_work(struct work_struct *work)
1003 struct rfkill *rfkill;
1004 bool cur;
1006 rfkill = container_of(work, struct rfkill, sync_work);
1008 mutex_lock(&rfkill_global_mutex);
1009 cur = rfkill_global_states[rfkill->type].cur;
1010 rfkill_set_block(rfkill, cur);
1011 mutex_unlock(&rfkill_global_mutex);
1014 int __must_check rfkill_register(struct rfkill *rfkill)
1016 static unsigned long rfkill_no;
1017 struct device *dev = &rfkill->dev;
1018 int error;
1020 BUG_ON(!rfkill);
1022 mutex_lock(&rfkill_global_mutex);
1024 if (rfkill->registered) {
1025 error = -EALREADY;
1026 goto unlock;
1029 rfkill->idx = rfkill_no;
1030 dev_set_name(dev, "rfkill%lu", rfkill_no);
1031 rfkill_no++;
1033 list_add_tail(&rfkill->node, &rfkill_list);
1035 error = device_add(dev);
1036 if (error)
1037 goto remove;
1039 error = rfkill_led_trigger_register(rfkill);
1040 if (error)
1041 goto devdel;
1043 rfkill->registered = true;
1045 INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
1046 INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
1047 INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
1049 if (rfkill->ops->poll)
1050 queue_delayed_work(system_power_efficient_wq,
1051 &rfkill->poll_work,
1052 round_jiffies_relative(POLL_INTERVAL));
1054 if (!rfkill->persistent || rfkill_epo_lock_active) {
1055 schedule_work(&rfkill->sync_work);
1056 } else {
1057 #ifdef CONFIG_RFKILL_INPUT
1058 bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
1060 if (!atomic_read(&rfkill_input_disabled))
1061 __rfkill_switch_all(rfkill->type, soft_blocked);
1062 #endif
1065 rfkill_global_led_trigger_event();
1066 rfkill_send_events(rfkill, RFKILL_OP_ADD);
1068 mutex_unlock(&rfkill_global_mutex);
1069 return 0;
1071 devdel:
1072 device_del(&rfkill->dev);
1073 remove:
1074 list_del_init(&rfkill->node);
1075 unlock:
1076 mutex_unlock(&rfkill_global_mutex);
1077 return error;
1079 EXPORT_SYMBOL(rfkill_register);
1081 void rfkill_unregister(struct rfkill *rfkill)
1083 BUG_ON(!rfkill);
1085 if (rfkill->ops->poll)
1086 cancel_delayed_work_sync(&rfkill->poll_work);
1088 cancel_work_sync(&rfkill->uevent_work);
1089 cancel_work_sync(&rfkill->sync_work);
1091 rfkill->registered = false;
1093 device_del(&rfkill->dev);
1095 mutex_lock(&rfkill_global_mutex);
1096 rfkill_send_events(rfkill, RFKILL_OP_DEL);
1097 list_del_init(&rfkill->node);
1098 rfkill_global_led_trigger_event();
1099 mutex_unlock(&rfkill_global_mutex);
1101 rfkill_led_trigger_unregister(rfkill);
1103 EXPORT_SYMBOL(rfkill_unregister);
1105 void rfkill_destroy(struct rfkill *rfkill)
1107 if (rfkill)
1108 put_device(&rfkill->dev);
1110 EXPORT_SYMBOL(rfkill_destroy);
1112 static int rfkill_fop_open(struct inode *inode, struct file *file)
1114 struct rfkill_data *data;
1115 struct rfkill *rfkill;
1116 struct rfkill_int_event *ev, *tmp;
1118 data = kzalloc(sizeof(*data), GFP_KERNEL);
1119 if (!data)
1120 return -ENOMEM;
1122 INIT_LIST_HEAD(&data->events);
1123 mutex_init(&data->mtx);
1124 init_waitqueue_head(&data->read_wait);
1126 mutex_lock(&rfkill_global_mutex);
1127 mutex_lock(&data->mtx);
1129 * start getting events from elsewhere but hold mtx to get
1130 * startup events added first
1133 list_for_each_entry(rfkill, &rfkill_list, node) {
1134 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1135 if (!ev)
1136 goto free;
1137 rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
1138 list_add_tail(&ev->list, &data->events);
1140 list_add(&data->list, &rfkill_fds);
1141 mutex_unlock(&data->mtx);
1142 mutex_unlock(&rfkill_global_mutex);
1144 file->private_data = data;
1146 return nonseekable_open(inode, file);
1148 free:
1149 mutex_unlock(&data->mtx);
1150 mutex_unlock(&rfkill_global_mutex);
1151 mutex_destroy(&data->mtx);
1152 list_for_each_entry_safe(ev, tmp, &data->events, list)
1153 kfree(ev);
1154 kfree(data);
1155 return -ENOMEM;
1158 static __poll_t rfkill_fop_poll(struct file *file, poll_table *wait)
1160 struct rfkill_data *data = file->private_data;
1161 __poll_t res = EPOLLOUT | EPOLLWRNORM;
1163 poll_wait(file, &data->read_wait, wait);
1165 mutex_lock(&data->mtx);
1166 if (!list_empty(&data->events))
1167 res = EPOLLIN | EPOLLRDNORM;
1168 mutex_unlock(&data->mtx);
1170 return res;
1173 static ssize_t rfkill_fop_read(struct file *file, char __user *buf,
1174 size_t count, loff_t *pos)
1176 struct rfkill_data *data = file->private_data;
1177 struct rfkill_int_event *ev;
1178 unsigned long sz;
1179 int ret;
1181 mutex_lock(&data->mtx);
1183 while (list_empty(&data->events)) {
1184 if (file->f_flags & O_NONBLOCK) {
1185 ret = -EAGAIN;
1186 goto out;
1188 mutex_unlock(&data->mtx);
1189 /* since we re-check and it just compares pointers,
1190 * using !list_empty() without locking isn't a problem
1192 ret = wait_event_interruptible(data->read_wait,
1193 !list_empty(&data->events));
1194 mutex_lock(&data->mtx);
1196 if (ret)
1197 goto out;
1200 ev = list_first_entry(&data->events, struct rfkill_int_event,
1201 list);
1203 sz = min_t(unsigned long, sizeof(ev->ev), count);
1204 ret = sz;
1205 if (copy_to_user(buf, &ev->ev, sz))
1206 ret = -EFAULT;
1208 list_del(&ev->list);
1209 kfree(ev);
1210 out:
1211 mutex_unlock(&data->mtx);
1212 return ret;
1215 static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
1216 size_t count, loff_t *pos)
1218 struct rfkill *rfkill;
1219 struct rfkill_event ev;
1220 int ret;
1222 /* we don't need the 'hard' variable but accept it */
1223 if (count < RFKILL_EVENT_SIZE_V1 - 1)
1224 return -EINVAL;
1227 * Copy as much data as we can accept into our 'ev' buffer,
1228 * but tell userspace how much we've copied so it can determine
1229 * our API version even in a write() call, if it cares.
1231 count = min(count, sizeof(ev));
1232 if (copy_from_user(&ev, buf, count))
1233 return -EFAULT;
1235 if (ev.type >= NUM_RFKILL_TYPES)
1236 return -EINVAL;
1238 mutex_lock(&rfkill_global_mutex);
1240 switch (ev.op) {
1241 case RFKILL_OP_CHANGE_ALL:
1242 rfkill_update_global_state(ev.type, ev.soft);
1243 list_for_each_entry(rfkill, &rfkill_list, node)
1244 if (rfkill->type == ev.type ||
1245 ev.type == RFKILL_TYPE_ALL)
1246 rfkill_set_block(rfkill, ev.soft);
1247 ret = 0;
1248 break;
1249 case RFKILL_OP_CHANGE:
1250 list_for_each_entry(rfkill, &rfkill_list, node)
1251 if (rfkill->idx == ev.idx &&
1252 (rfkill->type == ev.type ||
1253 ev.type == RFKILL_TYPE_ALL))
1254 rfkill_set_block(rfkill, ev.soft);
1255 ret = 0;
1256 break;
1257 default:
1258 ret = -EINVAL;
1259 break;
1262 mutex_unlock(&rfkill_global_mutex);
1264 return ret ?: count;
1267 static int rfkill_fop_release(struct inode *inode, struct file *file)
1269 struct rfkill_data *data = file->private_data;
1270 struct rfkill_int_event *ev, *tmp;
1272 mutex_lock(&rfkill_global_mutex);
1273 list_del(&data->list);
1274 mutex_unlock(&rfkill_global_mutex);
1276 mutex_destroy(&data->mtx);
1277 list_for_each_entry_safe(ev, tmp, &data->events, list)
1278 kfree(ev);
1280 #ifdef CONFIG_RFKILL_INPUT
1281 if (data->input_handler)
1282 if (atomic_dec_return(&rfkill_input_disabled) == 0)
1283 printk(KERN_DEBUG "rfkill: input handler enabled\n");
1284 #endif
1286 kfree(data);
1288 return 0;
1291 #ifdef CONFIG_RFKILL_INPUT
1292 static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
1293 unsigned long arg)
1295 struct rfkill_data *data = file->private_data;
1297 if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
1298 return -ENOSYS;
1300 if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
1301 return -ENOSYS;
1303 mutex_lock(&data->mtx);
1305 if (!data->input_handler) {
1306 if (atomic_inc_return(&rfkill_input_disabled) == 1)
1307 printk(KERN_DEBUG "rfkill: input handler disabled\n");
1308 data->input_handler = true;
1311 mutex_unlock(&data->mtx);
1313 return 0;
1315 #endif
1317 static const struct file_operations rfkill_fops = {
1318 .owner = THIS_MODULE,
1319 .open = rfkill_fop_open,
1320 .read = rfkill_fop_read,
1321 .write = rfkill_fop_write,
1322 .poll = rfkill_fop_poll,
1323 .release = rfkill_fop_release,
1324 #ifdef CONFIG_RFKILL_INPUT
1325 .unlocked_ioctl = rfkill_fop_ioctl,
1326 .compat_ioctl = rfkill_fop_ioctl,
1327 #endif
1328 .llseek = no_llseek,
1331 static struct miscdevice rfkill_miscdev = {
1332 .name = "rfkill",
1333 .fops = &rfkill_fops,
1334 .minor = MISC_DYNAMIC_MINOR,
1337 static int __init rfkill_init(void)
1339 int error;
1341 rfkill_update_global_state(RFKILL_TYPE_ALL, !rfkill_default_state);
1343 error = class_register(&rfkill_class);
1344 if (error)
1345 goto error_class;
1347 error = misc_register(&rfkill_miscdev);
1348 if (error)
1349 goto error_misc;
1351 error = rfkill_global_led_trigger_register();
1352 if (error)
1353 goto error_led_trigger;
1355 #ifdef CONFIG_RFKILL_INPUT
1356 error = rfkill_handler_init();
1357 if (error)
1358 goto error_input;
1359 #endif
1361 return 0;
1363 #ifdef CONFIG_RFKILL_INPUT
1364 error_input:
1365 rfkill_global_led_trigger_unregister();
1366 #endif
1367 error_led_trigger:
1368 misc_deregister(&rfkill_miscdev);
1369 error_misc:
1370 class_unregister(&rfkill_class);
1371 error_class:
1372 return error;
1374 subsys_initcall(rfkill_init);
1376 static void __exit rfkill_exit(void)
1378 #ifdef CONFIG_RFKILL_INPUT
1379 rfkill_handler_exit();
1380 #endif
1381 rfkill_global_led_trigger_unregister();
1382 misc_deregister(&rfkill_miscdev);
1383 class_unregister(&rfkill_class);
1385 module_exit(rfkill_exit);