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>
35 #include <linux/slab.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 |\
47 #define RFKILL_BLOCK_SW_SETCALL BIT(31)
52 enum rfkill_type type
;
63 const struct rfkill_ops
*ops
;
66 #ifdef CONFIG_RFKILL_LEDS
67 struct led_trigger led_trigger
;
68 const char *ledtrigname
;
72 struct list_head node
;
74 struct delayed_work poll_work
;
75 struct work_struct uevent_work
;
76 struct work_struct sync_work
;
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
;
87 struct list_head list
;
88 struct list_head events
;
90 wait_queue_head_t read_wait
;
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");
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
)
136 trigger
= &rfkill
->led_trigger
;
138 if (rfkill
->state
& RFKILL_BLOCK_ANY
)
139 led_trigger_event(trigger
, LED_OFF
);
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
);
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
)
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
;
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)
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
);
222 rfkill_none_led_trigger
.name
= "rfkill-none";
223 ret
= led_trigger_register(&rfkill_none_led_trigger
);
225 led_trigger_unregister(&rfkill_any_led_trigger
);
227 /* Delay activation until all global triggers are registered */
228 rfkill_global_led_trigger_event();
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
);
240 static void rfkill_led_trigger_event(struct rfkill
*rfkill
)
244 static inline int rfkill_led_trigger_register(struct rfkill
*rfkill
)
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)
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
)
272 ev
->idx
= rfkill
->idx
;
273 ev
->type
= rfkill
->type
;
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
);
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
)
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
320 static void rfkill_set_block(struct rfkill
*rfkill
, bool blocked
)
326 if (unlikely(rfkill
->dev
.power
.power_state
.event
& PM_EVENT_SLEEP
))
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
;
341 rfkill
->state
|= RFKILL_BLOCK_SW_PREV
;
343 rfkill
->state
&= ~RFKILL_BLOCK_SW_PREV
;
346 rfkill
->state
|= RFKILL_BLOCK_SW
;
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
);
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
;
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();
376 rfkill_event(rfkill
);
379 static void rfkill_update_global_state(enum rfkill_type type
, bool blocked
)
383 if (type
!= RFKILL_TYPE_ALL
) {
384 rfkill_global_states
[type
].cur
= blocked
;
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
)
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
))
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
;
455 if (atomic_read(&rfkill_input_disabled
))
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)
483 if (atomic_read(&rfkill_input_disabled
))
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
))
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
531 bool rfkill_get_global_sw_state(const enum rfkill_type type
)
533 return rfkill_global_states
[type
].cur
;
537 bool rfkill_set_hw_state(struct rfkill
*rfkill
, bool blocked
)
544 spin_lock_irqsave(&rfkill
->lock
, flags
);
545 prev
= !!(rfkill
->state
& RFKILL_BLOCK_HW
);
547 rfkill
->state
|= RFKILL_BLOCK_HW
;
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
);
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
;
572 rfkill
->state
|= bit
;
574 rfkill
->state
&= ~bit
;
577 bool rfkill_set_sw_state(struct rfkill
*rfkill
, bool blocked
)
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
)
594 if (prev
!= blocked
&& !hwblock
)
595 schedule_work(&rfkill
->uevent_work
);
597 rfkill_led_trigger_event(rfkill
);
598 rfkill_global_led_trigger_event();
602 EXPORT_SYMBOL(rfkill_set_sw_state
);
604 void rfkill_init_sw_state(struct rfkill
*rfkill
, bool blocked
)
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
)
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
);
635 rfkill
->state
|= RFKILL_BLOCK_HW
;
637 rfkill
->state
&= ~RFKILL_BLOCK_HW
;
639 spin_unlock_irqrestore(&rfkill
->lock
, flags
);
641 if (!rfkill
->registered
) {
642 rfkill
->persistent
= true;
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 */
665 enum rfkill_type
rfkill_find_type(const char *name
)
669 BUILD_BUG_ON(ARRAY_SIZE(rfkill_types
) != NUM_RFKILL_TYPES
);
672 return RFKILL_TYPE_ALL
;
674 for (i
= 1; i
< NUM_RFKILL_TYPES
; i
++)
675 if (!strcmp(name
, rfkill_types
[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
,
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
,
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
,
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
,
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
,
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
);
741 if (!capable(CAP_NET_ADMIN
))
744 err
= kstrtoul(buf
, 0, &state
);
751 mutex_lock(&rfkill_global_mutex
);
752 rfkill_set_block(rfkill
, state
);
753 mutex_unlock(&rfkill_global_mutex
);
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
,
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
);
784 if (!capable(CAP_NET_ADMIN
))
787 err
= kstrtoul(buf
, 0, &state
);
791 if (state
!= RFKILL_USER_STATE_SOFT_BLOCKED
&&
792 state
!= RFKILL_USER_STATE_UNBLOCKED
)
795 mutex_lock(&rfkill_global_mutex
);
796 rfkill_set_block(rfkill
, state
== RFKILL_USER_STATE_SOFT_BLOCKED
);
797 mutex_unlock(&rfkill_global_mutex
);
801 static DEVICE_ATTR_RW(state
);
803 static struct attribute
*rfkill_dev_attrs
[] = {
806 &dev_attr_index
.attr
,
807 &dev_attr_persistent
.attr
,
808 &dev_attr_state
.attr
,
813 ATTRIBUTE_GROUPS(rfkill_dev
);
815 static void rfkill_release(struct device
*dev
)
817 struct rfkill
*rfkill
= to_rfkill(dev
);
822 static int rfkill_dev_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
824 struct rfkill
*rfkill
= to_rfkill(dev
);
829 error
= add_uevent_var(env
, "RFKILL_NAME=%s", rfkill
->name
);
832 error
= add_uevent_var(env
, "RFKILL_TYPE=%s",
833 rfkill_types
[rfkill
->type
]);
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
));
844 void rfkill_pause_polling(struct rfkill
*rfkill
)
848 if (!rfkill
->ops
->poll
)
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
)
860 if (!rfkill
->ops
->poll
)
863 rfkill
->polling_paused
= false;
865 if (rfkill
->suspended
)
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
);
884 static int rfkill_resume(struct device
*dev
)
886 struct rfkill
*rfkill
= to_rfkill(dev
);
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);
903 static SIMPLE_DEV_PM_OPS(rfkill_pm_ops
, rfkill_suspend
, rfkill_resume
);
904 #define RFKILL_PM_OPS (&rfkill_pm_ops)
906 #define RFKILL_PM_OPS NULL
909 static struct class rfkill_class
= {
911 .dev_release
= rfkill_release
,
912 .dev_groups
= rfkill_dev_groups
,
913 .dev_uevent
= rfkill_dev_uevent
,
917 bool rfkill_blocked(struct rfkill
*rfkill
)
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
,
937 struct rfkill
*rfkill
;
943 if (WARN_ON(!ops
->set_block
))
949 if (WARN_ON(type
== RFKILL_TYPE_ALL
|| type
>= NUM_RFKILL_TYPES
))
952 rfkill
= kzalloc(sizeof(*rfkill
) + strlen(name
) + 1, GFP_KERNEL
);
956 spin_lock_init(&rfkill
->lock
);
957 INIT_LIST_HEAD(&rfkill
->node
);
959 strcpy(rfkill
->name
, name
);
961 rfkill
->data
= ops_data
;
964 dev
->class = &rfkill_class
;
965 dev
->parent
= parent
;
966 device_initialize(dev
);
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
,
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
;
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
;
1025 mutex_lock(&rfkill_global_mutex
);
1027 if (rfkill
->registered
) {
1032 rfkill
->idx
= rfkill_no
;
1033 dev_set_name(dev
, "rfkill%lu", rfkill_no
);
1036 list_add_tail(&rfkill
->node
, &rfkill_list
);
1038 error
= device_add(dev
);
1042 error
= rfkill_led_trigger_register(rfkill
);
1046 rfkill
->registered
= true;
1048 INIT_DELAYED_WORK(&rfkill
->poll_work
, rfkill_poll
);
1049 INIT_WORK(&rfkill
->uevent_work
, rfkill_uevent_work
);
1050 INIT_WORK(&rfkill
->sync_work
, rfkill_sync_work
);
1052 if (rfkill
->ops
->poll
)
1053 queue_delayed_work(system_power_efficient_wq
,
1055 round_jiffies_relative(POLL_INTERVAL
));
1057 if (!rfkill
->persistent
|| rfkill_epo_lock_active
) {
1058 schedule_work(&rfkill
->sync_work
);
1060 #ifdef CONFIG_RFKILL_INPUT
1061 bool soft_blocked
= !!(rfkill
->state
& RFKILL_BLOCK_SW
);
1063 if (!atomic_read(&rfkill_input_disabled
))
1064 __rfkill_switch_all(rfkill
->type
, soft_blocked
);
1068 rfkill_global_led_trigger_event();
1069 rfkill_send_events(rfkill
, RFKILL_OP_ADD
);
1071 mutex_unlock(&rfkill_global_mutex
);
1075 device_del(&rfkill
->dev
);
1077 list_del_init(&rfkill
->node
);
1079 mutex_unlock(&rfkill_global_mutex
);
1082 EXPORT_SYMBOL(rfkill_register
);
1084 void rfkill_unregister(struct rfkill
*rfkill
)
1088 if (rfkill
->ops
->poll
)
1089 cancel_delayed_work_sync(&rfkill
->poll_work
);
1091 cancel_work_sync(&rfkill
->uevent_work
);
1092 cancel_work_sync(&rfkill
->sync_work
);
1094 rfkill
->registered
= false;
1096 device_del(&rfkill
->dev
);
1098 mutex_lock(&rfkill_global_mutex
);
1099 rfkill_send_events(rfkill
, RFKILL_OP_DEL
);
1100 list_del_init(&rfkill
->node
);
1101 rfkill_global_led_trigger_event();
1102 mutex_unlock(&rfkill_global_mutex
);
1104 rfkill_led_trigger_unregister(rfkill
);
1106 EXPORT_SYMBOL(rfkill_unregister
);
1108 void rfkill_destroy(struct rfkill
*rfkill
)
1111 put_device(&rfkill
->dev
);
1113 EXPORT_SYMBOL(rfkill_destroy
);
1115 static int rfkill_fop_open(struct inode
*inode
, struct file
*file
)
1117 struct rfkill_data
*data
;
1118 struct rfkill
*rfkill
;
1119 struct rfkill_int_event
*ev
, *tmp
;
1121 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1125 INIT_LIST_HEAD(&data
->events
);
1126 mutex_init(&data
->mtx
);
1127 init_waitqueue_head(&data
->read_wait
);
1129 mutex_lock(&rfkill_global_mutex
);
1130 mutex_lock(&data
->mtx
);
1132 * start getting events from elsewhere but hold mtx to get
1133 * startup events added first
1136 list_for_each_entry(rfkill
, &rfkill_list
, node
) {
1137 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
1140 rfkill_fill_event(&ev
->ev
, rfkill
, RFKILL_OP_ADD
);
1141 list_add_tail(&ev
->list
, &data
->events
);
1143 list_add(&data
->list
, &rfkill_fds
);
1144 mutex_unlock(&data
->mtx
);
1145 mutex_unlock(&rfkill_global_mutex
);
1147 file
->private_data
= data
;
1149 return nonseekable_open(inode
, file
);
1152 mutex_unlock(&data
->mtx
);
1153 mutex_unlock(&rfkill_global_mutex
);
1154 mutex_destroy(&data
->mtx
);
1155 list_for_each_entry_safe(ev
, tmp
, &data
->events
, list
)
1161 static __poll_t
rfkill_fop_poll(struct file
*file
, poll_table
*wait
)
1163 struct rfkill_data
*data
= file
->private_data
;
1164 __poll_t res
= EPOLLOUT
| EPOLLWRNORM
;
1166 poll_wait(file
, &data
->read_wait
, wait
);
1168 mutex_lock(&data
->mtx
);
1169 if (!list_empty(&data
->events
))
1170 res
= EPOLLIN
| EPOLLRDNORM
;
1171 mutex_unlock(&data
->mtx
);
1176 static ssize_t
rfkill_fop_read(struct file
*file
, char __user
*buf
,
1177 size_t count
, loff_t
*pos
)
1179 struct rfkill_data
*data
= file
->private_data
;
1180 struct rfkill_int_event
*ev
;
1184 mutex_lock(&data
->mtx
);
1186 while (list_empty(&data
->events
)) {
1187 if (file
->f_flags
& O_NONBLOCK
) {
1191 mutex_unlock(&data
->mtx
);
1192 /* since we re-check and it just compares pointers,
1193 * using !list_empty() without locking isn't a problem
1195 ret
= wait_event_interruptible(data
->read_wait
,
1196 !list_empty(&data
->events
));
1197 mutex_lock(&data
->mtx
);
1203 ev
= list_first_entry(&data
->events
, struct rfkill_int_event
,
1206 sz
= min_t(unsigned long, sizeof(ev
->ev
), count
);
1208 if (copy_to_user(buf
, &ev
->ev
, sz
))
1211 list_del(&ev
->list
);
1214 mutex_unlock(&data
->mtx
);
1218 static ssize_t
rfkill_fop_write(struct file
*file
, const char __user
*buf
,
1219 size_t count
, loff_t
*pos
)
1221 struct rfkill
*rfkill
;
1222 struct rfkill_event ev
;
1225 /* we don't need the 'hard' variable but accept it */
1226 if (count
< RFKILL_EVENT_SIZE_V1
- 1)
1230 * Copy as much data as we can accept into our 'ev' buffer,
1231 * but tell userspace how much we've copied so it can determine
1232 * our API version even in a write() call, if it cares.
1234 count
= min(count
, sizeof(ev
));
1235 if (copy_from_user(&ev
, buf
, count
))
1238 if (ev
.type
>= NUM_RFKILL_TYPES
)
1241 mutex_lock(&rfkill_global_mutex
);
1244 case RFKILL_OP_CHANGE_ALL
:
1245 rfkill_update_global_state(ev
.type
, ev
.soft
);
1246 list_for_each_entry(rfkill
, &rfkill_list
, node
)
1247 if (rfkill
->type
== ev
.type
||
1248 ev
.type
== RFKILL_TYPE_ALL
)
1249 rfkill_set_block(rfkill
, ev
.soft
);
1252 case RFKILL_OP_CHANGE
:
1253 list_for_each_entry(rfkill
, &rfkill_list
, node
)
1254 if (rfkill
->idx
== ev
.idx
&&
1255 (rfkill
->type
== ev
.type
||
1256 ev
.type
== RFKILL_TYPE_ALL
))
1257 rfkill_set_block(rfkill
, ev
.soft
);
1265 mutex_unlock(&rfkill_global_mutex
);
1267 return ret
?: count
;
1270 static int rfkill_fop_release(struct inode
*inode
, struct file
*file
)
1272 struct rfkill_data
*data
= file
->private_data
;
1273 struct rfkill_int_event
*ev
, *tmp
;
1275 mutex_lock(&rfkill_global_mutex
);
1276 list_del(&data
->list
);
1277 mutex_unlock(&rfkill_global_mutex
);
1279 mutex_destroy(&data
->mtx
);
1280 list_for_each_entry_safe(ev
, tmp
, &data
->events
, list
)
1283 #ifdef CONFIG_RFKILL_INPUT
1284 if (data
->input_handler
)
1285 if (atomic_dec_return(&rfkill_input_disabled
) == 0)
1286 printk(KERN_DEBUG
"rfkill: input handler enabled\n");
1294 #ifdef CONFIG_RFKILL_INPUT
1295 static long rfkill_fop_ioctl(struct file
*file
, unsigned int cmd
,
1298 struct rfkill_data
*data
= file
->private_data
;
1300 if (_IOC_TYPE(cmd
) != RFKILL_IOC_MAGIC
)
1303 if (_IOC_NR(cmd
) != RFKILL_IOC_NOINPUT
)
1306 mutex_lock(&data
->mtx
);
1308 if (!data
->input_handler
) {
1309 if (atomic_inc_return(&rfkill_input_disabled
) == 1)
1310 printk(KERN_DEBUG
"rfkill: input handler disabled\n");
1311 data
->input_handler
= true;
1314 mutex_unlock(&data
->mtx
);
1320 static const struct file_operations rfkill_fops
= {
1321 .owner
= THIS_MODULE
,
1322 .open
= rfkill_fop_open
,
1323 .read
= rfkill_fop_read
,
1324 .write
= rfkill_fop_write
,
1325 .poll
= rfkill_fop_poll
,
1326 .release
= rfkill_fop_release
,
1327 #ifdef CONFIG_RFKILL_INPUT
1328 .unlocked_ioctl
= rfkill_fop_ioctl
,
1329 .compat_ioctl
= rfkill_fop_ioctl
,
1331 .llseek
= no_llseek
,
1334 #define RFKILL_NAME "rfkill"
1336 static struct miscdevice rfkill_miscdev
= {
1337 .fops
= &rfkill_fops
,
1338 .name
= RFKILL_NAME
,
1339 .minor
= RFKILL_MINOR
,
1342 static int __init
rfkill_init(void)
1346 rfkill_update_global_state(RFKILL_TYPE_ALL
, !rfkill_default_state
);
1348 error
= class_register(&rfkill_class
);
1352 error
= misc_register(&rfkill_miscdev
);
1356 error
= rfkill_global_led_trigger_register();
1358 goto error_led_trigger
;
1360 #ifdef CONFIG_RFKILL_INPUT
1361 error
= rfkill_handler_init();
1368 #ifdef CONFIG_RFKILL_INPUT
1370 rfkill_global_led_trigger_unregister();
1373 misc_deregister(&rfkill_miscdev
);
1375 class_unregister(&rfkill_class
);
1379 subsys_initcall(rfkill_init
);
1381 static void __exit
rfkill_exit(void)
1383 #ifdef CONFIG_RFKILL_INPUT
1384 rfkill_handler_exit();
1386 rfkill_global_led_trigger_unregister();
1387 misc_deregister(&rfkill_miscdev
);
1388 class_unregister(&rfkill_class
);
1390 module_exit(rfkill_exit
);
1392 MODULE_ALIAS_MISCDEV(RFKILL_MINOR
);
1393 MODULE_ALIAS("devname:" RFKILL_NAME
);