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 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
)
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
;
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)
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
);
220 rfkill_none_led_trigger
.name
= "rfkill-none";
221 ret
= led_trigger_register(&rfkill_none_led_trigger
);
223 led_trigger_unregister(&rfkill_any_led_trigger
);
225 /* Delay activation until all global triggers are registered */
226 rfkill_global_led_trigger_event();
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
);
238 static void rfkill_led_trigger_event(struct rfkill
*rfkill
)
242 static inline int rfkill_led_trigger_register(struct rfkill
*rfkill
)
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)
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
)
270 ev
->idx
= rfkill
->idx
;
271 ev
->type
= rfkill
->type
;
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
);
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
)
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
318 static void rfkill_set_block(struct rfkill
*rfkill
, bool blocked
)
324 if (unlikely(rfkill
->dev
.power
.power_state
.event
& PM_EVENT_SLEEP
))
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
;
339 rfkill
->state
|= RFKILL_BLOCK_SW_PREV
;
341 rfkill
->state
&= ~RFKILL_BLOCK_SW_PREV
;
344 rfkill
->state
|= RFKILL_BLOCK_SW
;
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
);
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
;
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();
374 rfkill_event(rfkill
);
377 static void rfkill_update_global_state(enum rfkill_type type
, bool blocked
)
381 if (type
!= RFKILL_TYPE_ALL
) {
382 rfkill_global_states
[type
].cur
= blocked
;
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
)
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
))
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
;
453 if (atomic_read(&rfkill_input_disabled
))
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)
481 if (atomic_read(&rfkill_input_disabled
))
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
))
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
529 bool rfkill_get_global_sw_state(const enum rfkill_type type
)
531 return rfkill_global_states
[type
].cur
;
535 bool rfkill_set_hw_state(struct rfkill
*rfkill
, bool blocked
)
542 spin_lock_irqsave(&rfkill
->lock
, flags
);
543 prev
= !!(rfkill
->state
& RFKILL_BLOCK_HW
);
545 rfkill
->state
|= RFKILL_BLOCK_HW
;
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
);
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
;
570 rfkill
->state
|= bit
;
572 rfkill
->state
&= ~bit
;
575 bool rfkill_set_sw_state(struct rfkill
*rfkill
, bool blocked
)
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
)
592 if (prev
!= blocked
&& !hwblock
)
593 schedule_work(&rfkill
->uevent_work
);
595 rfkill_led_trigger_event(rfkill
);
596 rfkill_global_led_trigger_event();
600 EXPORT_SYMBOL(rfkill_set_sw_state
);
602 void rfkill_init_sw_state(struct rfkill
*rfkill
, bool blocked
)
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
)
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
);
633 rfkill
->state
|= RFKILL_BLOCK_HW
;
635 rfkill
->state
&= ~RFKILL_BLOCK_HW
;
637 spin_unlock_irqrestore(&rfkill
->lock
, flags
);
639 if (!rfkill
->registered
) {
640 rfkill
->persistent
= true;
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 */
663 enum rfkill_type
rfkill_find_type(const char *name
)
667 BUILD_BUG_ON(ARRAY_SIZE(rfkill_types
) != NUM_RFKILL_TYPES
);
670 return RFKILL_TYPE_ALL
;
672 for (i
= 1; i
< NUM_RFKILL_TYPES
; i
++)
673 if (!strcmp(name
, rfkill_types
[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
,
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
,
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
,
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
,
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
,
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
);
739 if (!capable(CAP_NET_ADMIN
))
742 err
= kstrtoul(buf
, 0, &state
);
749 mutex_lock(&rfkill_global_mutex
);
750 rfkill_set_block(rfkill
, state
);
751 mutex_unlock(&rfkill_global_mutex
);
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
,
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
);
782 if (!capable(CAP_NET_ADMIN
))
785 err
= kstrtoul(buf
, 0, &state
);
789 if (state
!= RFKILL_USER_STATE_SOFT_BLOCKED
&&
790 state
!= RFKILL_USER_STATE_UNBLOCKED
)
793 mutex_lock(&rfkill_global_mutex
);
794 rfkill_set_block(rfkill
, state
== RFKILL_USER_STATE_SOFT_BLOCKED
);
795 mutex_unlock(&rfkill_global_mutex
);
799 static DEVICE_ATTR_RW(state
);
801 static struct attribute
*rfkill_dev_attrs
[] = {
804 &dev_attr_index
.attr
,
805 &dev_attr_persistent
.attr
,
806 &dev_attr_state
.attr
,
811 ATTRIBUTE_GROUPS(rfkill_dev
);
813 static void rfkill_release(struct device
*dev
)
815 struct rfkill
*rfkill
= to_rfkill(dev
);
820 static int rfkill_dev_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
822 struct rfkill
*rfkill
= to_rfkill(dev
);
827 error
= add_uevent_var(env
, "RFKILL_NAME=%s", rfkill
->name
);
830 error
= add_uevent_var(env
, "RFKILL_TYPE=%s",
831 rfkill_types
[rfkill
->type
]);
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
));
842 void rfkill_pause_polling(struct rfkill
*rfkill
)
846 if (!rfkill
->ops
->poll
)
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
)
858 if (!rfkill
->ops
->poll
)
861 rfkill
->polling_paused
= false;
863 if (rfkill
->suspended
)
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
);
882 static int rfkill_resume(struct device
*dev
)
884 struct rfkill
*rfkill
= to_rfkill(dev
);
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);
901 static SIMPLE_DEV_PM_OPS(rfkill_pm_ops
, rfkill_suspend
, rfkill_resume
);
902 #define RFKILL_PM_OPS (&rfkill_pm_ops)
904 #define RFKILL_PM_OPS NULL
907 static struct class rfkill_class
= {
909 .dev_release
= rfkill_release
,
910 .dev_groups
= rfkill_dev_groups
,
911 .dev_uevent
= rfkill_dev_uevent
,
915 bool rfkill_blocked(struct rfkill
*rfkill
)
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
,
935 struct rfkill
*rfkill
;
941 if (WARN_ON(!ops
->set_block
))
947 if (WARN_ON(type
== RFKILL_TYPE_ALL
|| type
>= NUM_RFKILL_TYPES
))
950 rfkill
= kzalloc(sizeof(*rfkill
) + strlen(name
) + 1, GFP_KERNEL
);
954 spin_lock_init(&rfkill
->lock
);
955 INIT_LIST_HEAD(&rfkill
->node
);
957 strcpy(rfkill
->name
, name
);
959 rfkill
->data
= ops_data
;
962 dev
->class = &rfkill_class
;
963 dev
->parent
= parent
;
964 device_initialize(dev
);
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
,
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
;
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
;
1020 mutex_lock(&rfkill_global_mutex
);
1022 if (rfkill
->registered
) {
1027 rfkill
->idx
= rfkill_no
;
1028 dev_set_name(dev
, "rfkill%lu", rfkill_no
);
1031 list_add_tail(&rfkill
->node
, &rfkill_list
);
1033 error
= device_add(dev
);
1037 error
= rfkill_led_trigger_register(rfkill
);
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
,
1050 round_jiffies_relative(POLL_INTERVAL
));
1052 if (!rfkill
->persistent
|| rfkill_epo_lock_active
) {
1053 schedule_work(&rfkill
->sync_work
);
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
);
1063 rfkill_global_led_trigger_event();
1064 rfkill_send_events(rfkill
, RFKILL_OP_ADD
);
1066 mutex_unlock(&rfkill_global_mutex
);
1070 device_del(&rfkill
->dev
);
1072 list_del_init(&rfkill
->node
);
1074 mutex_unlock(&rfkill_global_mutex
);
1077 EXPORT_SYMBOL(rfkill_register
);
1079 void rfkill_unregister(struct rfkill
*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
)
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
);
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
);
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
);
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
)
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
);
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
;
1179 mutex_lock(&data
->mtx
);
1181 while (list_empty(&data
->events
)) {
1182 if (file
->f_flags
& O_NONBLOCK
) {
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
);
1198 ev
= list_first_entry(&data
->events
, struct rfkill_int_event
,
1201 sz
= min_t(unsigned long, sizeof(ev
->ev
), count
);
1203 if (copy_to_user(buf
, &ev
->ev
, sz
))
1206 list_del(&ev
->list
);
1209 mutex_unlock(&data
->mtx
);
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
;
1220 /* we don't need the 'hard' variable but accept it */
1221 if (count
< RFKILL_EVENT_SIZE_V1
- 1)
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
))
1233 if (ev
.type
>= NUM_RFKILL_TYPES
)
1236 mutex_lock(&rfkill_global_mutex
);
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
);
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
);
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
)
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");
1289 #ifdef CONFIG_RFKILL_INPUT
1290 static long rfkill_fop_ioctl(struct file
*file
, unsigned int cmd
,
1293 struct rfkill_data
*data
= file
->private_data
;
1295 if (_IOC_TYPE(cmd
) != RFKILL_IOC_MAGIC
)
1298 if (_IOC_NR(cmd
) != RFKILL_IOC_NOINPUT
)
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
);
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
,
1326 .llseek
= no_llseek
,
1329 static struct miscdevice rfkill_miscdev
= {
1331 .fops
= &rfkill_fops
,
1332 .minor
= MISC_DYNAMIC_MINOR
,
1335 static int __init
rfkill_init(void)
1339 rfkill_update_global_state(RFKILL_TYPE_ALL
, !rfkill_default_state
);
1341 error
= class_register(&rfkill_class
);
1345 error
= misc_register(&rfkill_miscdev
);
1349 error
= rfkill_global_led_trigger_register();
1351 goto error_led_trigger
;
1353 #ifdef CONFIG_RFKILL_INPUT
1354 error
= rfkill_handler_init();
1361 #ifdef CONFIG_RFKILL_INPUT
1363 rfkill_global_led_trigger_unregister();
1366 misc_deregister(&rfkill_miscdev
);
1368 class_unregister(&rfkill_class
);
1372 subsys_initcall(rfkill_init
);
1374 static void __exit
rfkill_exit(void)
1376 #ifdef CONFIG_RFKILL_INPUT
1377 rfkill_handler_exit();
1379 rfkill_global_led_trigger_unregister();
1380 misc_deregister(&rfkill_miscdev
);
1381 class_unregister(&rfkill_class
);
1383 module_exit(rfkill_exit
);