i2c: designware-master: use core to detect 'no zero length' quirk
[linux/fpc-iii.git] / net / rfkill / core.c
bloba7a4e6ff9be2941b86ce983cd4db42e861016563
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 void 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);
153 const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
155 return rfkill->led_trigger.name;
157 EXPORT_SYMBOL(rfkill_get_led_trigger_name);
159 void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
161 BUG_ON(!rfkill);
163 rfkill->ledtrigname = name;
165 EXPORT_SYMBOL(rfkill_set_led_trigger_name);
167 static int rfkill_led_trigger_register(struct rfkill *rfkill)
169 rfkill->led_trigger.name = rfkill->ledtrigname
170 ? : dev_name(&rfkill->dev);
171 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
172 return led_trigger_register(&rfkill->led_trigger);
175 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
177 led_trigger_unregister(&rfkill->led_trigger);
180 static struct led_trigger rfkill_any_led_trigger;
181 static struct led_trigger rfkill_none_led_trigger;
182 static struct work_struct rfkill_global_led_trigger_work;
184 static void rfkill_global_led_trigger_worker(struct work_struct *work)
186 enum led_brightness brightness = LED_OFF;
187 struct rfkill *rfkill;
189 mutex_lock(&rfkill_global_mutex);
190 list_for_each_entry(rfkill, &rfkill_list, node) {
191 if (!(rfkill->state & RFKILL_BLOCK_ANY)) {
192 brightness = LED_FULL;
193 break;
196 mutex_unlock(&rfkill_global_mutex);
198 led_trigger_event(&rfkill_any_led_trigger, brightness);
199 led_trigger_event(&rfkill_none_led_trigger,
200 brightness == LED_OFF ? LED_FULL : LED_OFF);
203 static void rfkill_global_led_trigger_event(void)
205 schedule_work(&rfkill_global_led_trigger_work);
208 static int rfkill_global_led_trigger_register(void)
210 int ret;
212 INIT_WORK(&rfkill_global_led_trigger_work,
213 rfkill_global_led_trigger_worker);
215 rfkill_any_led_trigger.name = "rfkill-any";
216 ret = led_trigger_register(&rfkill_any_led_trigger);
217 if (ret)
218 return ret;
220 rfkill_none_led_trigger.name = "rfkill-none";
221 ret = led_trigger_register(&rfkill_none_led_trigger);
222 if (ret)
223 led_trigger_unregister(&rfkill_any_led_trigger);
224 else
225 /* Delay activation until all global triggers are registered */
226 rfkill_global_led_trigger_event();
228 return ret;
231 static void rfkill_global_led_trigger_unregister(void)
233 led_trigger_unregister(&rfkill_none_led_trigger);
234 led_trigger_unregister(&rfkill_any_led_trigger);
235 cancel_work_sync(&rfkill_global_led_trigger_work);
237 #else
238 static void rfkill_led_trigger_event(struct rfkill *rfkill)
242 static inline int rfkill_led_trigger_register(struct rfkill *rfkill)
244 return 0;
247 static inline void rfkill_led_trigger_unregister(struct rfkill *rfkill)
251 static void rfkill_global_led_trigger_event(void)
255 static int rfkill_global_led_trigger_register(void)
257 return 0;
260 static void rfkill_global_led_trigger_unregister(void)
263 #endif /* CONFIG_RFKILL_LEDS */
265 static void rfkill_fill_event(struct rfkill_event *ev, struct rfkill *rfkill,
266 enum rfkill_operation op)
268 unsigned long flags;
270 ev->idx = rfkill->idx;
271 ev->type = rfkill->type;
272 ev->op = op;
274 spin_lock_irqsave(&rfkill->lock, flags);
275 ev->hard = !!(rfkill->state & RFKILL_BLOCK_HW);
276 ev->soft = !!(rfkill->state & (RFKILL_BLOCK_SW |
277 RFKILL_BLOCK_SW_PREV));
278 spin_unlock_irqrestore(&rfkill->lock, flags);
281 static void rfkill_send_events(struct rfkill *rfkill, enum rfkill_operation op)
283 struct rfkill_data *data;
284 struct rfkill_int_event *ev;
286 list_for_each_entry(data, &rfkill_fds, list) {
287 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
288 if (!ev)
289 continue;
290 rfkill_fill_event(&ev->ev, rfkill, op);
291 mutex_lock(&data->mtx);
292 list_add_tail(&ev->list, &data->events);
293 mutex_unlock(&data->mtx);
294 wake_up_interruptible(&data->read_wait);
298 static void rfkill_event(struct rfkill *rfkill)
300 if (!rfkill->registered)
301 return;
303 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
305 /* also send event to /dev/rfkill */
306 rfkill_send_events(rfkill, RFKILL_OP_CHANGE);
310 * rfkill_set_block - wrapper for set_block method
312 * @rfkill: the rfkill struct to use
313 * @blocked: the new software state
315 * Calls the set_block method (when applicable) and handles notifications
316 * etc. as well.
318 static void rfkill_set_block(struct rfkill *rfkill, bool blocked)
320 unsigned long flags;
321 bool prev, curr;
322 int err;
324 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
325 return;
328 * Some platforms (...!) generate input events which affect the
329 * _hard_ kill state -- whenever something tries to change the
330 * current software state query the hardware state too.
332 if (rfkill->ops->query)
333 rfkill->ops->query(rfkill, rfkill->data);
335 spin_lock_irqsave(&rfkill->lock, flags);
336 prev = rfkill->state & RFKILL_BLOCK_SW;
338 if (prev)
339 rfkill->state |= RFKILL_BLOCK_SW_PREV;
340 else
341 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
343 if (blocked)
344 rfkill->state |= RFKILL_BLOCK_SW;
345 else
346 rfkill->state &= ~RFKILL_BLOCK_SW;
348 rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
349 spin_unlock_irqrestore(&rfkill->lock, flags);
351 err = rfkill->ops->set_block(rfkill->data, blocked);
353 spin_lock_irqsave(&rfkill->lock, flags);
354 if (err) {
356 * Failed -- reset status to _PREV, which may be different
357 * from what we have set _PREV to earlier in this function
358 * if rfkill_set_sw_state was invoked.
360 if (rfkill->state & RFKILL_BLOCK_SW_PREV)
361 rfkill->state |= RFKILL_BLOCK_SW;
362 else
363 rfkill->state &= ~RFKILL_BLOCK_SW;
365 rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
366 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
367 curr = rfkill->state & RFKILL_BLOCK_SW;
368 spin_unlock_irqrestore(&rfkill->lock, flags);
370 rfkill_led_trigger_event(rfkill);
371 rfkill_global_led_trigger_event();
373 if (prev != curr)
374 rfkill_event(rfkill);
377 static void rfkill_update_global_state(enum rfkill_type type, bool blocked)
379 int i;
381 if (type != RFKILL_TYPE_ALL) {
382 rfkill_global_states[type].cur = blocked;
383 return;
386 for (i = 0; i < NUM_RFKILL_TYPES; i++)
387 rfkill_global_states[i].cur = blocked;
390 #ifdef CONFIG_RFKILL_INPUT
391 static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
394 * __rfkill_switch_all - Toggle state of all switches of given type
395 * @type: type of interfaces to be affected
396 * @blocked: the new state
398 * This function sets the state of all switches of given type,
399 * unless a specific switch is suspended.
401 * Caller must have acquired rfkill_global_mutex.
403 static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
405 struct rfkill *rfkill;
407 rfkill_update_global_state(type, blocked);
408 list_for_each_entry(rfkill, &rfkill_list, node) {
409 if (rfkill->type != type && type != RFKILL_TYPE_ALL)
410 continue;
412 rfkill_set_block(rfkill, blocked);
417 * rfkill_switch_all - Toggle state of all switches of given type
418 * @type: type of interfaces to be affected
419 * @blocked: the new state
421 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
422 * Please refer to __rfkill_switch_all() for details.
424 * Does nothing if the EPO lock is active.
426 void rfkill_switch_all(enum rfkill_type type, bool blocked)
428 if (atomic_read(&rfkill_input_disabled))
429 return;
431 mutex_lock(&rfkill_global_mutex);
433 if (!rfkill_epo_lock_active)
434 __rfkill_switch_all(type, blocked);
436 mutex_unlock(&rfkill_global_mutex);
440 * rfkill_epo - emergency power off all transmitters
442 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
443 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
445 * The global state before the EPO is saved and can be restored later
446 * using rfkill_restore_states().
448 void rfkill_epo(void)
450 struct rfkill *rfkill;
451 int i;
453 if (atomic_read(&rfkill_input_disabled))
454 return;
456 mutex_lock(&rfkill_global_mutex);
458 rfkill_epo_lock_active = true;
459 list_for_each_entry(rfkill, &rfkill_list, node)
460 rfkill_set_block(rfkill, true);
462 for (i = 0; i < NUM_RFKILL_TYPES; i++) {
463 rfkill_global_states[i].sav = rfkill_global_states[i].cur;
464 rfkill_global_states[i].cur = true;
467 mutex_unlock(&rfkill_global_mutex);
471 * rfkill_restore_states - restore global states
473 * Restore (and sync switches to) the global state from the
474 * states in rfkill_default_states. This can undo the effects of
475 * a call to rfkill_epo().
477 void rfkill_restore_states(void)
479 int i;
481 if (atomic_read(&rfkill_input_disabled))
482 return;
484 mutex_lock(&rfkill_global_mutex);
486 rfkill_epo_lock_active = false;
487 for (i = 0; i < NUM_RFKILL_TYPES; i++)
488 __rfkill_switch_all(i, rfkill_global_states[i].sav);
489 mutex_unlock(&rfkill_global_mutex);
493 * rfkill_remove_epo_lock - unlock state changes
495 * Used by rfkill-input manually unlock state changes, when
496 * the EPO switch is deactivated.
498 void rfkill_remove_epo_lock(void)
500 if (atomic_read(&rfkill_input_disabled))
501 return;
503 mutex_lock(&rfkill_global_mutex);
504 rfkill_epo_lock_active = false;
505 mutex_unlock(&rfkill_global_mutex);
509 * rfkill_is_epo_lock_active - returns true EPO is active
511 * Returns 0 (false) if there is NOT an active EPO contidion,
512 * and 1 (true) if there is an active EPO contition, which
513 * locks all radios in one of the BLOCKED states.
515 * Can be called in atomic context.
517 bool rfkill_is_epo_lock_active(void)
519 return rfkill_epo_lock_active;
523 * rfkill_get_global_sw_state - returns global state for a type
524 * @type: the type to get the global state of
526 * Returns the current global state for a given wireless
527 * device type.
529 bool rfkill_get_global_sw_state(const enum rfkill_type type)
531 return rfkill_global_states[type].cur;
533 #endif
535 bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
537 unsigned long flags;
538 bool ret, prev;
540 BUG_ON(!rfkill);
542 spin_lock_irqsave(&rfkill->lock, flags);
543 prev = !!(rfkill->state & RFKILL_BLOCK_HW);
544 if (blocked)
545 rfkill->state |= RFKILL_BLOCK_HW;
546 else
547 rfkill->state &= ~RFKILL_BLOCK_HW;
548 ret = !!(rfkill->state & RFKILL_BLOCK_ANY);
549 spin_unlock_irqrestore(&rfkill->lock, flags);
551 rfkill_led_trigger_event(rfkill);
552 rfkill_global_led_trigger_event();
554 if (rfkill->registered && prev != blocked)
555 schedule_work(&rfkill->uevent_work);
557 return ret;
559 EXPORT_SYMBOL(rfkill_set_hw_state);
561 static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
563 u32 bit = RFKILL_BLOCK_SW;
565 /* if in a ops->set_block right now, use other bit */
566 if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
567 bit = RFKILL_BLOCK_SW_PREV;
569 if (blocked)
570 rfkill->state |= bit;
571 else
572 rfkill->state &= ~bit;
575 bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
577 unsigned long flags;
578 bool prev, hwblock;
580 BUG_ON(!rfkill);
582 spin_lock_irqsave(&rfkill->lock, flags);
583 prev = !!(rfkill->state & RFKILL_BLOCK_SW);
584 __rfkill_set_sw_state(rfkill, blocked);
585 hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
586 blocked = blocked || hwblock;
587 spin_unlock_irqrestore(&rfkill->lock, flags);
589 if (!rfkill->registered)
590 return blocked;
592 if (prev != blocked && !hwblock)
593 schedule_work(&rfkill->uevent_work);
595 rfkill_led_trigger_event(rfkill);
596 rfkill_global_led_trigger_event();
598 return blocked;
600 EXPORT_SYMBOL(rfkill_set_sw_state);
602 void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
604 unsigned long flags;
606 BUG_ON(!rfkill);
607 BUG_ON(rfkill->registered);
609 spin_lock_irqsave(&rfkill->lock, flags);
610 __rfkill_set_sw_state(rfkill, blocked);
611 rfkill->persistent = true;
612 spin_unlock_irqrestore(&rfkill->lock, flags);
614 EXPORT_SYMBOL(rfkill_init_sw_state);
616 void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
618 unsigned long flags;
619 bool swprev, hwprev;
621 BUG_ON(!rfkill);
623 spin_lock_irqsave(&rfkill->lock, flags);
626 * No need to care about prev/setblock ... this is for uevent only
627 * and that will get triggered by rfkill_set_block anyway.
629 swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
630 hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
631 __rfkill_set_sw_state(rfkill, sw);
632 if (hw)
633 rfkill->state |= RFKILL_BLOCK_HW;
634 else
635 rfkill->state &= ~RFKILL_BLOCK_HW;
637 spin_unlock_irqrestore(&rfkill->lock, flags);
639 if (!rfkill->registered) {
640 rfkill->persistent = true;
641 } else {
642 if (swprev != sw || hwprev != hw)
643 schedule_work(&rfkill->uevent_work);
645 rfkill_led_trigger_event(rfkill);
646 rfkill_global_led_trigger_event();
649 EXPORT_SYMBOL(rfkill_set_states);
651 static const char * const rfkill_types[] = {
652 NULL, /* RFKILL_TYPE_ALL */
653 "wlan",
654 "bluetooth",
655 "ultrawideband",
656 "wimax",
657 "wwan",
658 "gps",
659 "fm",
660 "nfc",
663 enum rfkill_type rfkill_find_type(const char *name)
665 int i;
667 BUILD_BUG_ON(ARRAY_SIZE(rfkill_types) != NUM_RFKILL_TYPES);
669 if (!name)
670 return RFKILL_TYPE_ALL;
672 for (i = 1; i < NUM_RFKILL_TYPES; i++)
673 if (!strcmp(name, rfkill_types[i]))
674 return i;
675 return RFKILL_TYPE_ALL;
677 EXPORT_SYMBOL(rfkill_find_type);
679 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
680 char *buf)
682 struct rfkill *rfkill = to_rfkill(dev);
684 return sprintf(buf, "%s\n", rfkill->name);
686 static DEVICE_ATTR_RO(name);
688 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
689 char *buf)
691 struct rfkill *rfkill = to_rfkill(dev);
693 return sprintf(buf, "%s\n", rfkill_types[rfkill->type]);
695 static DEVICE_ATTR_RO(type);
697 static ssize_t index_show(struct device *dev, struct device_attribute *attr,
698 char *buf)
700 struct rfkill *rfkill = to_rfkill(dev);
702 return sprintf(buf, "%d\n", rfkill->idx);
704 static DEVICE_ATTR_RO(index);
706 static ssize_t persistent_show(struct device *dev,
707 struct device_attribute *attr, char *buf)
709 struct rfkill *rfkill = to_rfkill(dev);
711 return sprintf(buf, "%d\n", rfkill->persistent);
713 static DEVICE_ATTR_RO(persistent);
715 static ssize_t hard_show(struct device *dev, struct device_attribute *attr,
716 char *buf)
718 struct rfkill *rfkill = to_rfkill(dev);
720 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 );
722 static DEVICE_ATTR_RO(hard);
724 static ssize_t soft_show(struct device *dev, struct device_attribute *attr,
725 char *buf)
727 struct rfkill *rfkill = to_rfkill(dev);
729 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 );
732 static ssize_t soft_store(struct device *dev, struct device_attribute *attr,
733 const char *buf, size_t count)
735 struct rfkill *rfkill = to_rfkill(dev);
736 unsigned long state;
737 int err;
739 if (!capable(CAP_NET_ADMIN))
740 return -EPERM;
742 err = kstrtoul(buf, 0, &state);
743 if (err)
744 return err;
746 if (state > 1 )
747 return -EINVAL;
749 mutex_lock(&rfkill_global_mutex);
750 rfkill_set_block(rfkill, state);
751 mutex_unlock(&rfkill_global_mutex);
753 return count;
755 static DEVICE_ATTR_RW(soft);
757 static u8 user_state_from_blocked(unsigned long state)
759 if (state & RFKILL_BLOCK_HW)
760 return RFKILL_USER_STATE_HARD_BLOCKED;
761 if (state & RFKILL_BLOCK_SW)
762 return RFKILL_USER_STATE_SOFT_BLOCKED;
764 return RFKILL_USER_STATE_UNBLOCKED;
767 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
768 char *buf)
770 struct rfkill *rfkill = to_rfkill(dev);
772 return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state));
775 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
778 struct rfkill *rfkill = to_rfkill(dev);
779 unsigned long state;
780 int err;
782 if (!capable(CAP_NET_ADMIN))
783 return -EPERM;
785 err = kstrtoul(buf, 0, &state);
786 if (err)
787 return err;
789 if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
790 state != RFKILL_USER_STATE_UNBLOCKED)
791 return -EINVAL;
793 mutex_lock(&rfkill_global_mutex);
794 rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
795 mutex_unlock(&rfkill_global_mutex);
797 return count;
799 static DEVICE_ATTR_RW(state);
801 static struct attribute *rfkill_dev_attrs[] = {
802 &dev_attr_name.attr,
803 &dev_attr_type.attr,
804 &dev_attr_index.attr,
805 &dev_attr_persistent.attr,
806 &dev_attr_state.attr,
807 &dev_attr_soft.attr,
808 &dev_attr_hard.attr,
809 NULL,
811 ATTRIBUTE_GROUPS(rfkill_dev);
813 static void rfkill_release(struct device *dev)
815 struct rfkill *rfkill = to_rfkill(dev);
817 kfree(rfkill);
820 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
822 struct rfkill *rfkill = to_rfkill(dev);
823 unsigned long flags;
824 u32 state;
825 int error;
827 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
828 if (error)
829 return error;
830 error = add_uevent_var(env, "RFKILL_TYPE=%s",
831 rfkill_types[rfkill->type]);
832 if (error)
833 return error;
834 spin_lock_irqsave(&rfkill->lock, flags);
835 state = rfkill->state;
836 spin_unlock_irqrestore(&rfkill->lock, flags);
837 error = add_uevent_var(env, "RFKILL_STATE=%d",
838 user_state_from_blocked(state));
839 return error;
842 void rfkill_pause_polling(struct rfkill *rfkill)
844 BUG_ON(!rfkill);
846 if (!rfkill->ops->poll)
847 return;
849 rfkill->polling_paused = true;
850 cancel_delayed_work_sync(&rfkill->poll_work);
852 EXPORT_SYMBOL(rfkill_pause_polling);
854 void rfkill_resume_polling(struct rfkill *rfkill)
856 BUG_ON(!rfkill);
858 if (!rfkill->ops->poll)
859 return;
861 rfkill->polling_paused = false;
863 if (rfkill->suspended)
864 return;
866 queue_delayed_work(system_power_efficient_wq,
867 &rfkill->poll_work, 0);
869 EXPORT_SYMBOL(rfkill_resume_polling);
871 #ifdef CONFIG_PM_SLEEP
872 static int rfkill_suspend(struct device *dev)
874 struct rfkill *rfkill = to_rfkill(dev);
876 rfkill->suspended = true;
877 cancel_delayed_work_sync(&rfkill->poll_work);
879 return 0;
882 static int rfkill_resume(struct device *dev)
884 struct rfkill *rfkill = to_rfkill(dev);
885 bool cur;
887 rfkill->suspended = false;
889 if (!rfkill->persistent) {
890 cur = !!(rfkill->state & RFKILL_BLOCK_SW);
891 rfkill_set_block(rfkill, cur);
894 if (rfkill->ops->poll && !rfkill->polling_paused)
895 queue_delayed_work(system_power_efficient_wq,
896 &rfkill->poll_work, 0);
898 return 0;
901 static SIMPLE_DEV_PM_OPS(rfkill_pm_ops, rfkill_suspend, rfkill_resume);
902 #define RFKILL_PM_OPS (&rfkill_pm_ops)
903 #else
904 #define RFKILL_PM_OPS NULL
905 #endif
907 static struct class rfkill_class = {
908 .name = "rfkill",
909 .dev_release = rfkill_release,
910 .dev_groups = rfkill_dev_groups,
911 .dev_uevent = rfkill_dev_uevent,
912 .pm = RFKILL_PM_OPS,
915 bool rfkill_blocked(struct rfkill *rfkill)
917 unsigned long flags;
918 u32 state;
920 spin_lock_irqsave(&rfkill->lock, flags);
921 state = rfkill->state;
922 spin_unlock_irqrestore(&rfkill->lock, flags);
924 return !!(state & RFKILL_BLOCK_ANY);
926 EXPORT_SYMBOL(rfkill_blocked);
929 struct rfkill * __must_check rfkill_alloc(const char *name,
930 struct device *parent,
931 const enum rfkill_type type,
932 const struct rfkill_ops *ops,
933 void *ops_data)
935 struct rfkill *rfkill;
936 struct device *dev;
938 if (WARN_ON(!ops))
939 return NULL;
941 if (WARN_ON(!ops->set_block))
942 return NULL;
944 if (WARN_ON(!name))
945 return NULL;
947 if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
948 return NULL;
950 rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
951 if (!rfkill)
952 return NULL;
954 spin_lock_init(&rfkill->lock);
955 INIT_LIST_HEAD(&rfkill->node);
956 rfkill->type = type;
957 strcpy(rfkill->name, name);
958 rfkill->ops = ops;
959 rfkill->data = ops_data;
961 dev = &rfkill->dev;
962 dev->class = &rfkill_class;
963 dev->parent = parent;
964 device_initialize(dev);
966 return rfkill;
968 EXPORT_SYMBOL(rfkill_alloc);
970 static void rfkill_poll(struct work_struct *work)
972 struct rfkill *rfkill;
974 rfkill = container_of(work, struct rfkill, poll_work.work);
977 * Poll hardware state -- driver will use one of the
978 * rfkill_set{,_hw,_sw}_state functions and use its
979 * return value to update the current status.
981 rfkill->ops->poll(rfkill, rfkill->data);
983 queue_delayed_work(system_power_efficient_wq,
984 &rfkill->poll_work,
985 round_jiffies_relative(POLL_INTERVAL));
988 static void rfkill_uevent_work(struct work_struct *work)
990 struct rfkill *rfkill;
992 rfkill = container_of(work, struct rfkill, uevent_work);
994 mutex_lock(&rfkill_global_mutex);
995 rfkill_event(rfkill);
996 mutex_unlock(&rfkill_global_mutex);
999 static void rfkill_sync_work(struct work_struct *work)
1001 struct rfkill *rfkill;
1002 bool cur;
1004 rfkill = container_of(work, struct rfkill, sync_work);
1006 mutex_lock(&rfkill_global_mutex);
1007 cur = rfkill_global_states[rfkill->type].cur;
1008 rfkill_set_block(rfkill, cur);
1009 mutex_unlock(&rfkill_global_mutex);
1012 int __must_check rfkill_register(struct rfkill *rfkill)
1014 static unsigned long rfkill_no;
1015 struct device *dev = &rfkill->dev;
1016 int error;
1018 BUG_ON(!rfkill);
1020 mutex_lock(&rfkill_global_mutex);
1022 if (rfkill->registered) {
1023 error = -EALREADY;
1024 goto unlock;
1027 rfkill->idx = rfkill_no;
1028 dev_set_name(dev, "rfkill%lu", rfkill_no);
1029 rfkill_no++;
1031 list_add_tail(&rfkill->node, &rfkill_list);
1033 error = device_add(dev);
1034 if (error)
1035 goto remove;
1037 error = rfkill_led_trigger_register(rfkill);
1038 if (error)
1039 goto devdel;
1041 rfkill->registered = true;
1043 INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
1044 INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
1045 INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
1047 if (rfkill->ops->poll)
1048 queue_delayed_work(system_power_efficient_wq,
1049 &rfkill->poll_work,
1050 round_jiffies_relative(POLL_INTERVAL));
1052 if (!rfkill->persistent || rfkill_epo_lock_active) {
1053 schedule_work(&rfkill->sync_work);
1054 } else {
1055 #ifdef CONFIG_RFKILL_INPUT
1056 bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
1058 if (!atomic_read(&rfkill_input_disabled))
1059 __rfkill_switch_all(rfkill->type, soft_blocked);
1060 #endif
1063 rfkill_global_led_trigger_event();
1064 rfkill_send_events(rfkill, RFKILL_OP_ADD);
1066 mutex_unlock(&rfkill_global_mutex);
1067 return 0;
1069 devdel:
1070 device_del(&rfkill->dev);
1071 remove:
1072 list_del_init(&rfkill->node);
1073 unlock:
1074 mutex_unlock(&rfkill_global_mutex);
1075 return error;
1077 EXPORT_SYMBOL(rfkill_register);
1079 void rfkill_unregister(struct rfkill *rfkill)
1081 BUG_ON(!rfkill);
1083 if (rfkill->ops->poll)
1084 cancel_delayed_work_sync(&rfkill->poll_work);
1086 cancel_work_sync(&rfkill->uevent_work);
1087 cancel_work_sync(&rfkill->sync_work);
1089 rfkill->registered = false;
1091 device_del(&rfkill->dev);
1093 mutex_lock(&rfkill_global_mutex);
1094 rfkill_send_events(rfkill, RFKILL_OP_DEL);
1095 list_del_init(&rfkill->node);
1096 rfkill_global_led_trigger_event();
1097 mutex_unlock(&rfkill_global_mutex);
1099 rfkill_led_trigger_unregister(rfkill);
1101 EXPORT_SYMBOL(rfkill_unregister);
1103 void rfkill_destroy(struct rfkill *rfkill)
1105 if (rfkill)
1106 put_device(&rfkill->dev);
1108 EXPORT_SYMBOL(rfkill_destroy);
1110 static int rfkill_fop_open(struct inode *inode, struct file *file)
1112 struct rfkill_data *data;
1113 struct rfkill *rfkill;
1114 struct rfkill_int_event *ev, *tmp;
1116 data = kzalloc(sizeof(*data), GFP_KERNEL);
1117 if (!data)
1118 return -ENOMEM;
1120 INIT_LIST_HEAD(&data->events);
1121 mutex_init(&data->mtx);
1122 init_waitqueue_head(&data->read_wait);
1124 mutex_lock(&rfkill_global_mutex);
1125 mutex_lock(&data->mtx);
1127 * start getting events from elsewhere but hold mtx to get
1128 * startup events added first
1131 list_for_each_entry(rfkill, &rfkill_list, node) {
1132 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1133 if (!ev)
1134 goto free;
1135 rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
1136 list_add_tail(&ev->list, &data->events);
1138 list_add(&data->list, &rfkill_fds);
1139 mutex_unlock(&data->mtx);
1140 mutex_unlock(&rfkill_global_mutex);
1142 file->private_data = data;
1144 return nonseekable_open(inode, file);
1146 free:
1147 mutex_unlock(&data->mtx);
1148 mutex_unlock(&rfkill_global_mutex);
1149 mutex_destroy(&data->mtx);
1150 list_for_each_entry_safe(ev, tmp, &data->events, list)
1151 kfree(ev);
1152 kfree(data);
1153 return -ENOMEM;
1156 static __poll_t rfkill_fop_poll(struct file *file, poll_table *wait)
1158 struct rfkill_data *data = file->private_data;
1159 __poll_t res = EPOLLOUT | EPOLLWRNORM;
1161 poll_wait(file, &data->read_wait, wait);
1163 mutex_lock(&data->mtx);
1164 if (!list_empty(&data->events))
1165 res = EPOLLIN | EPOLLRDNORM;
1166 mutex_unlock(&data->mtx);
1168 return res;
1171 static ssize_t rfkill_fop_read(struct file *file, char __user *buf,
1172 size_t count, loff_t *pos)
1174 struct rfkill_data *data = file->private_data;
1175 struct rfkill_int_event *ev;
1176 unsigned long sz;
1177 int ret;
1179 mutex_lock(&data->mtx);
1181 while (list_empty(&data->events)) {
1182 if (file->f_flags & O_NONBLOCK) {
1183 ret = -EAGAIN;
1184 goto out;
1186 mutex_unlock(&data->mtx);
1187 /* since we re-check and it just compares pointers,
1188 * using !list_empty() without locking isn't a problem
1190 ret = wait_event_interruptible(data->read_wait,
1191 !list_empty(&data->events));
1192 mutex_lock(&data->mtx);
1194 if (ret)
1195 goto out;
1198 ev = list_first_entry(&data->events, struct rfkill_int_event,
1199 list);
1201 sz = min_t(unsigned long, sizeof(ev->ev), count);
1202 ret = sz;
1203 if (copy_to_user(buf, &ev->ev, sz))
1204 ret = -EFAULT;
1206 list_del(&ev->list);
1207 kfree(ev);
1208 out:
1209 mutex_unlock(&data->mtx);
1210 return ret;
1213 static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
1214 size_t count, loff_t *pos)
1216 struct rfkill *rfkill;
1217 struct rfkill_event ev;
1218 int ret;
1220 /* we don't need the 'hard' variable but accept it */
1221 if (count < RFKILL_EVENT_SIZE_V1 - 1)
1222 return -EINVAL;
1225 * Copy as much data as we can accept into our 'ev' buffer,
1226 * but tell userspace how much we've copied so it can determine
1227 * our API version even in a write() call, if it cares.
1229 count = min(count, sizeof(ev));
1230 if (copy_from_user(&ev, buf, count))
1231 return -EFAULT;
1233 if (ev.type >= NUM_RFKILL_TYPES)
1234 return -EINVAL;
1236 mutex_lock(&rfkill_global_mutex);
1238 switch (ev.op) {
1239 case RFKILL_OP_CHANGE_ALL:
1240 rfkill_update_global_state(ev.type, ev.soft);
1241 list_for_each_entry(rfkill, &rfkill_list, node)
1242 if (rfkill->type == ev.type ||
1243 ev.type == RFKILL_TYPE_ALL)
1244 rfkill_set_block(rfkill, ev.soft);
1245 ret = 0;
1246 break;
1247 case RFKILL_OP_CHANGE:
1248 list_for_each_entry(rfkill, &rfkill_list, node)
1249 if (rfkill->idx == ev.idx &&
1250 (rfkill->type == ev.type ||
1251 ev.type == RFKILL_TYPE_ALL))
1252 rfkill_set_block(rfkill, ev.soft);
1253 ret = 0;
1254 break;
1255 default:
1256 ret = -EINVAL;
1257 break;
1260 mutex_unlock(&rfkill_global_mutex);
1262 return ret ?: count;
1265 static int rfkill_fop_release(struct inode *inode, struct file *file)
1267 struct rfkill_data *data = file->private_data;
1268 struct rfkill_int_event *ev, *tmp;
1270 mutex_lock(&rfkill_global_mutex);
1271 list_del(&data->list);
1272 mutex_unlock(&rfkill_global_mutex);
1274 mutex_destroy(&data->mtx);
1275 list_for_each_entry_safe(ev, tmp, &data->events, list)
1276 kfree(ev);
1278 #ifdef CONFIG_RFKILL_INPUT
1279 if (data->input_handler)
1280 if (atomic_dec_return(&rfkill_input_disabled) == 0)
1281 printk(KERN_DEBUG "rfkill: input handler enabled\n");
1282 #endif
1284 kfree(data);
1286 return 0;
1289 #ifdef CONFIG_RFKILL_INPUT
1290 static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
1291 unsigned long arg)
1293 struct rfkill_data *data = file->private_data;
1295 if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
1296 return -ENOSYS;
1298 if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
1299 return -ENOSYS;
1301 mutex_lock(&data->mtx);
1303 if (!data->input_handler) {
1304 if (atomic_inc_return(&rfkill_input_disabled) == 1)
1305 printk(KERN_DEBUG "rfkill: input handler disabled\n");
1306 data->input_handler = true;
1309 mutex_unlock(&data->mtx);
1311 return 0;
1313 #endif
1315 static const struct file_operations rfkill_fops = {
1316 .owner = THIS_MODULE,
1317 .open = rfkill_fop_open,
1318 .read = rfkill_fop_read,
1319 .write = rfkill_fop_write,
1320 .poll = rfkill_fop_poll,
1321 .release = rfkill_fop_release,
1322 #ifdef CONFIG_RFKILL_INPUT
1323 .unlocked_ioctl = rfkill_fop_ioctl,
1324 .compat_ioctl = rfkill_fop_ioctl,
1325 #endif
1326 .llseek = no_llseek,
1329 static struct miscdevice rfkill_miscdev = {
1330 .name = "rfkill",
1331 .fops = &rfkill_fops,
1332 .minor = MISC_DYNAMIC_MINOR,
1335 static int __init rfkill_init(void)
1337 int error;
1339 rfkill_update_global_state(RFKILL_TYPE_ALL, !rfkill_default_state);
1341 error = class_register(&rfkill_class);
1342 if (error)
1343 goto error_class;
1345 error = misc_register(&rfkill_miscdev);
1346 if (error)
1347 goto error_misc;
1349 error = rfkill_global_led_trigger_register();
1350 if (error)
1351 goto error_led_trigger;
1353 #ifdef CONFIG_RFKILL_INPUT
1354 error = rfkill_handler_init();
1355 if (error)
1356 goto error_input;
1357 #endif
1359 return 0;
1361 #ifdef CONFIG_RFKILL_INPUT
1362 error_input:
1363 rfkill_global_led_trigger_unregister();
1364 #endif
1365 error_led_trigger:
1366 misc_deregister(&rfkill_miscdev);
1367 error_misc:
1368 class_unregister(&rfkill_class);
1369 error_class:
1370 return error;
1372 subsys_initcall(rfkill_init);
1374 static void __exit rfkill_exit(void)
1376 #ifdef CONFIG_RFKILL_INPUT
1377 rfkill_handler_exit();
1378 #endif
1379 rfkill_global_led_trigger_unregister();
1380 misc_deregister(&rfkill_miscdev);
1381 class_unregister(&rfkill_class);
1383 module_exit(rfkill_exit);