2 * Copyright (C) 2006 - 2007 Ivo van Doorn
3 * Copyright (C) 2007 Dmitry Torokhov
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/workqueue.h>
25 #include <linux/capability.h>
26 #include <linux/list.h>
27 #include <linux/mutex.h>
28 #include <linux/rfkill.h>
30 /* Get declaration of rfkill_switch_all() to shut up sparse. */
31 #include "rfkill-input.h"
34 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35 MODULE_VERSION("1.0");
36 MODULE_DESCRIPTION("RF switch support");
37 MODULE_LICENSE("GPL");
39 static LIST_HEAD(rfkill_list
); /* list of registered rf switches */
40 static DEFINE_MUTEX(rfkill_global_mutex
);
42 static unsigned int rfkill_default_state
= RFKILL_STATE_UNBLOCKED
;
43 module_param_named(default_state
, rfkill_default_state
, uint
, 0444);
44 MODULE_PARM_DESC(default_state
,
45 "Default initial state for all radio types, 0 = radio off");
47 struct rfkill_gsw_state
{
48 enum rfkill_state current_state
;
49 enum rfkill_state default_state
;
52 static struct rfkill_gsw_state rfkill_global_states
[RFKILL_TYPE_MAX
];
53 static unsigned long rfkill_states_lockdflt
[BITS_TO_LONGS(RFKILL_TYPE_MAX
)];
54 static bool rfkill_epo_lock_active
;
56 static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list
);
60 * register_rfkill_notifier - Add notifier to rfkill notifier chain
61 * @nb: pointer to the new entry to add to the chain
63 * See blocking_notifier_chain_register() for return value and further
66 * Adds a notifier to the rfkill notifier chain. The chain will be
67 * called with a pointer to the relevant rfkill structure as a parameter,
68 * refer to include/linux/rfkill.h for the possible events.
70 * Notifiers added to this chain are to always return NOTIFY_DONE. This
71 * chain is a blocking notifier chain: notifiers can sleep.
73 * Calls to this chain may have been done through a workqueue. One must
74 * assume unordered asynchronous behaviour, there is no way to know if
75 * actions related to the event that generated the notification have been
76 * carried out already.
78 int register_rfkill_notifier(struct notifier_block
*nb
)
81 return blocking_notifier_chain_register(&rfkill_notifier_list
, nb
);
83 EXPORT_SYMBOL_GPL(register_rfkill_notifier
);
86 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
87 * @nb: pointer to the entry to remove from the chain
89 * See blocking_notifier_chain_unregister() for return value and further
92 * Removes a notifier from the rfkill notifier chain.
94 int unregister_rfkill_notifier(struct notifier_block
*nb
)
97 return blocking_notifier_chain_unregister(&rfkill_notifier_list
, nb
);
99 EXPORT_SYMBOL_GPL(unregister_rfkill_notifier
);
102 static void rfkill_led_trigger(struct rfkill
*rfkill
,
103 enum rfkill_state state
)
105 #ifdef CONFIG_RFKILL_LEDS
106 struct led_trigger
*led
= &rfkill
->led_trigger
;
110 if (state
!= RFKILL_STATE_UNBLOCKED
)
111 led_trigger_event(led
, LED_OFF
);
113 led_trigger_event(led
, LED_FULL
);
114 #endif /* CONFIG_RFKILL_LEDS */
117 #ifdef CONFIG_RFKILL_LEDS
118 static void rfkill_led_trigger_activate(struct led_classdev
*led
)
120 struct rfkill
*rfkill
= container_of(led
->trigger
,
121 struct rfkill
, led_trigger
);
123 rfkill_led_trigger(rfkill
, rfkill
->state
);
125 #endif /* CONFIG_RFKILL_LEDS */
127 static void notify_rfkill_state_change(struct rfkill
*rfkill
)
129 rfkill_led_trigger(rfkill
, rfkill
->state
);
130 blocking_notifier_call_chain(&rfkill_notifier_list
,
131 RFKILL_STATE_CHANGED
,
135 static void update_rfkill_state(struct rfkill
*rfkill
)
137 enum rfkill_state newstate
, oldstate
;
139 if (rfkill
->get_state
) {
140 mutex_lock(&rfkill
->mutex
);
141 if (!rfkill
->get_state(rfkill
->data
, &newstate
)) {
142 oldstate
= rfkill
->state
;
143 rfkill
->state
= newstate
;
144 if (oldstate
!= newstate
)
145 notify_rfkill_state_change(rfkill
);
147 mutex_unlock(&rfkill
->mutex
);
152 * rfkill_toggle_radio - wrapper for toggle_radio hook
153 * @rfkill: the rfkill struct to use
154 * @force: calls toggle_radio even if cache says it is not needed,
155 * and also makes sure notifications of the state will be
156 * sent even if it didn't change
157 * @state: the new state to call toggle_radio() with
159 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
160 * calls and handling all the red tape such as issuing notifications
161 * if the call is successful.
163 * Suspended devices are not touched at all, and -EAGAIN is returned.
165 * Note that the @force parameter cannot override a (possibly cached)
166 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
167 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
168 * rfkill_force_state(), so the cache either is bypassed or valid.
170 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
171 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
172 * give the driver a hint that it should double-BLOCK the transmitter.
174 * Caller must have acquired rfkill->mutex.
176 static int rfkill_toggle_radio(struct rfkill
*rfkill
,
177 enum rfkill_state state
,
181 enum rfkill_state oldstate
, newstate
;
183 if (unlikely(rfkill
->dev
.power
.power_state
.event
& PM_EVENT_SLEEP
))
186 oldstate
= rfkill
->state
;
188 if (rfkill
->get_state
&& !force
&&
189 !rfkill
->get_state(rfkill
->data
, &newstate
))
190 rfkill
->state
= newstate
;
193 case RFKILL_STATE_HARD_BLOCKED
:
194 /* typically happens when refreshing hardware state,
195 * such as on resume */
196 state
= RFKILL_STATE_SOFT_BLOCKED
;
198 case RFKILL_STATE_UNBLOCKED
:
199 /* force can't override this, only rfkill_force_state() can */
200 if (rfkill
->state
== RFKILL_STATE_HARD_BLOCKED
)
203 case RFKILL_STATE_SOFT_BLOCKED
:
204 /* nothing to do, we want to give drivers the hint to double
205 * BLOCK even a transmitter that is already in state
206 * RFKILL_STATE_HARD_BLOCKED */
210 "rfkill: illegal state %d passed as parameter "
211 "to rfkill_toggle_radio\n", state
);
215 if (force
|| state
!= rfkill
->state
) {
216 retval
= rfkill
->toggle_radio(rfkill
->data
, state
);
217 /* never allow a HARD->SOFT downgrade! */
218 if (!retval
&& rfkill
->state
!= RFKILL_STATE_HARD_BLOCKED
)
219 rfkill
->state
= state
;
222 if (force
|| rfkill
->state
!= oldstate
)
223 notify_rfkill_state_change(rfkill
);
229 * __rfkill_switch_all - Toggle state of all switches of given type
230 * @type: type of interfaces to be affected
231 * @state: the new state
233 * This function toggles the state of all switches of given type,
234 * unless a specific switch is claimed by userspace (in which case,
235 * that switch is left alone) or suspended.
237 * Caller must have acquired rfkill_global_mutex.
239 static void __rfkill_switch_all(const enum rfkill_type type
,
240 const enum rfkill_state state
)
242 struct rfkill
*rfkill
;
244 if (WARN((state
>= RFKILL_STATE_MAX
|| type
>= RFKILL_TYPE_MAX
),
246 "rfkill: illegal state %d or type %d "
247 "passed as parameter to __rfkill_switch_all\n",
251 rfkill_global_states
[type
].current_state
= state
;
252 list_for_each_entry(rfkill
, &rfkill_list
, node
) {
253 if ((!rfkill
->user_claim
) && (rfkill
->type
== type
)) {
254 mutex_lock(&rfkill
->mutex
);
255 rfkill_toggle_radio(rfkill
, state
, 0);
256 mutex_unlock(&rfkill
->mutex
);
262 * rfkill_switch_all - Toggle state of all switches of given type
263 * @type: type of interfaces to be affected
264 * @state: the new state
266 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
267 * Please refer to __rfkill_switch_all() for details.
269 * Does nothing if the EPO lock is active.
271 void rfkill_switch_all(enum rfkill_type type
, enum rfkill_state state
)
273 mutex_lock(&rfkill_global_mutex
);
274 if (!rfkill_epo_lock_active
)
275 __rfkill_switch_all(type
, state
);
276 mutex_unlock(&rfkill_global_mutex
);
278 EXPORT_SYMBOL(rfkill_switch_all
);
281 * rfkill_epo - emergency power off all transmitters
283 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
284 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
286 * The global state before the EPO is saved and can be restored later
287 * using rfkill_restore_states().
289 void rfkill_epo(void)
291 struct rfkill
*rfkill
;
294 mutex_lock(&rfkill_global_mutex
);
296 rfkill_epo_lock_active
= true;
297 list_for_each_entry(rfkill
, &rfkill_list
, node
) {
298 mutex_lock(&rfkill
->mutex
);
299 rfkill_toggle_radio(rfkill
, RFKILL_STATE_SOFT_BLOCKED
, 1);
300 mutex_unlock(&rfkill
->mutex
);
302 for (i
= 0; i
< RFKILL_TYPE_MAX
; i
++) {
303 rfkill_global_states
[i
].default_state
=
304 rfkill_global_states
[i
].current_state
;
305 rfkill_global_states
[i
].current_state
=
306 RFKILL_STATE_SOFT_BLOCKED
;
308 mutex_unlock(&rfkill_global_mutex
);
310 EXPORT_SYMBOL_GPL(rfkill_epo
);
313 * rfkill_restore_states - restore global states
315 * Restore (and sync switches to) the global state from the
316 * states in rfkill_default_states. This can undo the effects of
317 * a call to rfkill_epo().
319 void rfkill_restore_states(void)
323 mutex_lock(&rfkill_global_mutex
);
325 rfkill_epo_lock_active
= false;
326 for (i
= 0; i
< RFKILL_TYPE_MAX
; i
++)
327 __rfkill_switch_all(i
, rfkill_global_states
[i
].default_state
);
328 mutex_unlock(&rfkill_global_mutex
);
330 EXPORT_SYMBOL_GPL(rfkill_restore_states
);
333 * rfkill_remove_epo_lock - unlock state changes
335 * Used by rfkill-input manually unlock state changes, when
336 * the EPO switch is deactivated.
338 void rfkill_remove_epo_lock(void)
340 mutex_lock(&rfkill_global_mutex
);
341 rfkill_epo_lock_active
= false;
342 mutex_unlock(&rfkill_global_mutex
);
344 EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock
);
347 * rfkill_is_epo_lock_active - returns true EPO is active
349 * Returns 0 (false) if there is NOT an active EPO contidion,
350 * and 1 (true) if there is an active EPO contition, which
351 * locks all radios in one of the BLOCKED states.
353 * Can be called in atomic context.
355 bool rfkill_is_epo_lock_active(void)
357 return rfkill_epo_lock_active
;
359 EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active
);
362 * rfkill_get_global_state - returns global state for a type
363 * @type: the type to get the global state of
365 * Returns the current global state for a given wireless
368 enum rfkill_state
rfkill_get_global_state(const enum rfkill_type type
)
370 return rfkill_global_states
[type
].current_state
;
372 EXPORT_SYMBOL_GPL(rfkill_get_global_state
);
375 * rfkill_force_state - Force the internal rfkill radio state
376 * @rfkill: pointer to the rfkill class to modify.
377 * @state: the current radio state the class should be forced to.
379 * This function updates the internal state of the radio cached
380 * by the rfkill class. It should be used when the driver gets
381 * a notification by the firmware/hardware of the current *real*
382 * state of the radio rfkill switch.
384 * Devices which are subject to external changes on their rfkill
385 * state (such as those caused by a hardware rfkill line) MUST
386 * have their driver arrange to call rfkill_force_state() as soon
387 * as possible after such a change.
389 * This function may not be called from an atomic context.
391 int rfkill_force_state(struct rfkill
*rfkill
, enum rfkill_state state
)
393 enum rfkill_state oldstate
;
396 if (WARN((state
>= RFKILL_STATE_MAX
),
398 "rfkill: illegal state %d passed as parameter "
399 "to rfkill_force_state\n", state
))
402 mutex_lock(&rfkill
->mutex
);
404 oldstate
= rfkill
->state
;
405 rfkill
->state
= state
;
407 if (state
!= oldstate
)
408 notify_rfkill_state_change(rfkill
);
410 mutex_unlock(&rfkill
->mutex
);
414 EXPORT_SYMBOL(rfkill_force_state
);
416 static ssize_t
rfkill_name_show(struct device
*dev
,
417 struct device_attribute
*attr
,
420 struct rfkill
*rfkill
= to_rfkill(dev
);
422 return sprintf(buf
, "%s\n", rfkill
->name
);
425 static const char *rfkill_get_type_str(enum rfkill_type type
)
428 case RFKILL_TYPE_WLAN
:
430 case RFKILL_TYPE_BLUETOOTH
:
432 case RFKILL_TYPE_UWB
:
433 return "ultrawideband";
434 case RFKILL_TYPE_WIMAX
:
436 case RFKILL_TYPE_WWAN
:
443 static ssize_t
rfkill_type_show(struct device
*dev
,
444 struct device_attribute
*attr
,
447 struct rfkill
*rfkill
= to_rfkill(dev
);
449 return sprintf(buf
, "%s\n", rfkill_get_type_str(rfkill
->type
));
452 static ssize_t
rfkill_state_show(struct device
*dev
,
453 struct device_attribute
*attr
,
456 struct rfkill
*rfkill
= to_rfkill(dev
);
458 update_rfkill_state(rfkill
);
459 return sprintf(buf
, "%d\n", rfkill
->state
);
462 static ssize_t
rfkill_state_store(struct device
*dev
,
463 struct device_attribute
*attr
,
464 const char *buf
, size_t count
)
466 struct rfkill
*rfkill
= to_rfkill(dev
);
470 if (!capable(CAP_NET_ADMIN
))
473 error
= strict_strtoul(buf
, 0, &state
);
477 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
478 if (state
!= RFKILL_STATE_UNBLOCKED
&&
479 state
!= RFKILL_STATE_SOFT_BLOCKED
)
482 error
= mutex_lock_killable(&rfkill
->mutex
);
486 if (!rfkill_epo_lock_active
)
487 error
= rfkill_toggle_radio(rfkill
, state
, 0);
491 mutex_unlock(&rfkill
->mutex
);
493 return error
? error
: count
;
496 static ssize_t
rfkill_claim_show(struct device
*dev
,
497 struct device_attribute
*attr
,
500 struct rfkill
*rfkill
= to_rfkill(dev
);
502 return sprintf(buf
, "%d\n", rfkill
->user_claim
);
505 static ssize_t
rfkill_claim_store(struct device
*dev
,
506 struct device_attribute
*attr
,
507 const char *buf
, size_t count
)
509 struct rfkill
*rfkill
= to_rfkill(dev
);
510 unsigned long claim_tmp
;
514 if (!capable(CAP_NET_ADMIN
))
517 if (rfkill
->user_claim_unsupported
)
520 error
= strict_strtoul(buf
, 0, &claim_tmp
);
526 * Take the global lock to make sure the kernel is not in
527 * the middle of rfkill_switch_all
529 error
= mutex_lock_killable(&rfkill_global_mutex
);
533 if (rfkill
->user_claim
!= claim
) {
534 if (!claim
&& !rfkill_epo_lock_active
) {
535 mutex_lock(&rfkill
->mutex
);
536 rfkill_toggle_radio(rfkill
,
537 rfkill_global_states
[rfkill
->type
].current_state
,
539 mutex_unlock(&rfkill
->mutex
);
541 rfkill
->user_claim
= claim
;
544 mutex_unlock(&rfkill_global_mutex
);
546 return error
? error
: count
;
549 static struct device_attribute rfkill_dev_attrs
[] = {
550 __ATTR(name
, S_IRUGO
, rfkill_name_show
, NULL
),
551 __ATTR(type
, S_IRUGO
, rfkill_type_show
, NULL
),
552 __ATTR(state
, S_IRUGO
|S_IWUSR
, rfkill_state_show
, rfkill_state_store
),
553 __ATTR(claim
, S_IRUGO
|S_IWUSR
, rfkill_claim_show
, rfkill_claim_store
),
557 static void rfkill_release(struct device
*dev
)
559 struct rfkill
*rfkill
= to_rfkill(dev
);
562 module_put(THIS_MODULE
);
566 static int rfkill_suspend(struct device
*dev
, pm_message_t state
)
568 struct rfkill
*rfkill
= to_rfkill(dev
);
570 /* mark class device as suspended */
571 if (dev
->power
.power_state
.event
!= state
.event
)
572 dev
->power
.power_state
= state
;
574 /* store state for the resume handler */
575 rfkill
->state_for_resume
= rfkill
->state
;
580 static int rfkill_resume(struct device
*dev
)
582 struct rfkill
*rfkill
= to_rfkill(dev
);
583 enum rfkill_state newstate
;
585 if (dev
->power
.power_state
.event
!= PM_EVENT_ON
) {
586 mutex_lock(&rfkill
->mutex
);
588 dev
->power
.power_state
.event
= PM_EVENT_ON
;
591 * rfkill->state could have been modified before we got
592 * called, and won't be updated by rfkill_toggle_radio()
593 * in force mode. Sync it FIRST.
595 if (rfkill
->get_state
&&
596 !rfkill
->get_state(rfkill
->data
, &newstate
))
597 rfkill
->state
= newstate
;
600 * If we are under EPO, kick transmitter offline,
601 * otherwise restore to pre-suspend state.
603 * Issue a notification in any case
605 rfkill_toggle_radio(rfkill
,
606 rfkill_epo_lock_active
?
607 RFKILL_STATE_SOFT_BLOCKED
:
608 rfkill
->state_for_resume
,
611 mutex_unlock(&rfkill
->mutex
);
617 #define rfkill_suspend NULL
618 #define rfkill_resume NULL
621 static int rfkill_blocking_uevent_notifier(struct notifier_block
*nb
,
622 unsigned long eventid
,
625 struct rfkill
*rfkill
= (struct rfkill
*)data
;
628 case RFKILL_STATE_CHANGED
:
629 kobject_uevent(&rfkill
->dev
.kobj
, KOBJ_CHANGE
);
638 static struct notifier_block rfkill_blocking_uevent_nb
= {
639 .notifier_call
= rfkill_blocking_uevent_notifier
,
643 static int rfkill_dev_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
645 struct rfkill
*rfkill
= to_rfkill(dev
);
648 error
= add_uevent_var(env
, "RFKILL_NAME=%s", rfkill
->name
);
651 error
= add_uevent_var(env
, "RFKILL_TYPE=%s",
652 rfkill_get_type_str(rfkill
->type
));
655 error
= add_uevent_var(env
, "RFKILL_STATE=%d", rfkill
->state
);
659 static struct class rfkill_class
= {
661 .dev_release
= rfkill_release
,
662 .dev_attrs
= rfkill_dev_attrs
,
663 .suspend
= rfkill_suspend
,
664 .resume
= rfkill_resume
,
665 .dev_uevent
= rfkill_dev_uevent
,
668 static int rfkill_check_duplicity(const struct rfkill
*rfkill
)
671 unsigned long seen
[BITS_TO_LONGS(RFKILL_TYPE_MAX
)];
673 memset(seen
, 0, sizeof(seen
));
675 list_for_each_entry(p
, &rfkill_list
, node
) {
676 if (WARN((p
== rfkill
), KERN_WARNING
677 "rfkill: illegal attempt to register "
678 "an already registered rfkill struct\n"))
680 set_bit(p
->type
, seen
);
683 /* 0: first switch of its kind */
684 return (test_bit(rfkill
->type
, seen
)) ? 1 : 0;
687 static int rfkill_add_switch(struct rfkill
*rfkill
)
691 mutex_lock(&rfkill_global_mutex
);
693 error
= rfkill_check_duplicity(rfkill
);
698 /* lock default after first use */
699 set_bit(rfkill
->type
, rfkill_states_lockdflt
);
700 rfkill_global_states
[rfkill
->type
].current_state
=
701 rfkill_global_states
[rfkill
->type
].default_state
;
704 rfkill_toggle_radio(rfkill
,
705 rfkill_global_states
[rfkill
->type
].current_state
,
708 list_add_tail(&rfkill
->node
, &rfkill_list
);
712 mutex_unlock(&rfkill_global_mutex
);
717 static void rfkill_remove_switch(struct rfkill
*rfkill
)
719 mutex_lock(&rfkill_global_mutex
);
720 list_del_init(&rfkill
->node
);
721 mutex_unlock(&rfkill_global_mutex
);
723 mutex_lock(&rfkill
->mutex
);
724 rfkill_toggle_radio(rfkill
, RFKILL_STATE_SOFT_BLOCKED
, 1);
725 mutex_unlock(&rfkill
->mutex
);
729 * rfkill_allocate - allocate memory for rfkill structure.
730 * @parent: device that has rf switch on it
731 * @type: type of the switch (RFKILL_TYPE_*)
733 * This function should be called by the network driver when it needs
734 * rfkill structure. Once the structure is allocated the driver should
735 * finish its initialization by setting the name, private data, enable_radio
736 * and disable_radio methods and then register it with rfkill_register().
738 * NOTE: If registration fails the structure shoudl be freed by calling
739 * rfkill_free() otherwise rfkill_unregister() should be used.
741 struct rfkill
* __must_check
rfkill_allocate(struct device
*parent
,
742 enum rfkill_type type
)
744 struct rfkill
*rfkill
;
747 if (WARN((type
>= RFKILL_TYPE_MAX
),
749 "rfkill: illegal type %d passed as parameter "
750 "to rfkill_allocate\n", type
))
753 rfkill
= kzalloc(sizeof(struct rfkill
), GFP_KERNEL
);
757 mutex_init(&rfkill
->mutex
);
758 INIT_LIST_HEAD(&rfkill
->node
);
762 dev
->class = &rfkill_class
;
763 dev
->parent
= parent
;
764 device_initialize(dev
);
766 __module_get(THIS_MODULE
);
770 EXPORT_SYMBOL(rfkill_allocate
);
773 * rfkill_free - Mark rfkill structure for deletion
774 * @rfkill: rfkill structure to be destroyed
776 * Decrements reference count of the rfkill structure so it is destroyed.
777 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
779 void rfkill_free(struct rfkill
*rfkill
)
782 put_device(&rfkill
->dev
);
784 EXPORT_SYMBOL(rfkill_free
);
786 static void rfkill_led_trigger_register(struct rfkill
*rfkill
)
788 #ifdef CONFIG_RFKILL_LEDS
791 if (!rfkill
->led_trigger
.name
)
792 rfkill
->led_trigger
.name
= dev_name(&rfkill
->dev
);
793 if (!rfkill
->led_trigger
.activate
)
794 rfkill
->led_trigger
.activate
= rfkill_led_trigger_activate
;
795 error
= led_trigger_register(&rfkill
->led_trigger
);
797 rfkill
->led_trigger
.name
= NULL
;
798 #endif /* CONFIG_RFKILL_LEDS */
801 static void rfkill_led_trigger_unregister(struct rfkill
*rfkill
)
803 #ifdef CONFIG_RFKILL_LEDS
804 if (rfkill
->led_trigger
.name
) {
805 led_trigger_unregister(&rfkill
->led_trigger
);
806 rfkill
->led_trigger
.name
= NULL
;
812 * rfkill_register - Register a rfkill structure.
813 * @rfkill: rfkill structure to be registered
815 * This function should be called by the network driver when the rfkill
816 * structure needs to be registered. Immediately from registration the
817 * switch driver should be able to service calls to toggle_radio.
819 int __must_check
rfkill_register(struct rfkill
*rfkill
)
821 static atomic_t rfkill_no
= ATOMIC_INIT(0);
822 struct device
*dev
= &rfkill
->dev
;
825 if (WARN((!rfkill
|| !rfkill
->toggle_radio
||
826 rfkill
->type
>= RFKILL_TYPE_MAX
||
827 rfkill
->state
>= RFKILL_STATE_MAX
),
829 "rfkill: attempt to register a "
830 "badly initialized rfkill struct\n"))
833 dev_set_name(dev
, "rfkill%ld", (long)atomic_inc_return(&rfkill_no
) - 1);
835 rfkill_led_trigger_register(rfkill
);
837 error
= rfkill_add_switch(rfkill
);
839 rfkill_led_trigger_unregister(rfkill
);
843 error
= device_add(dev
);
845 rfkill_remove_switch(rfkill
);
846 rfkill_led_trigger_unregister(rfkill
);
852 EXPORT_SYMBOL(rfkill_register
);
855 * rfkill_unregister - Unregister a rfkill structure.
856 * @rfkill: rfkill structure to be unregistered
858 * This function should be called by the network driver during device
859 * teardown to destroy rfkill structure. Note that rfkill_free() should
860 * _not_ be called after rfkill_unregister().
862 void rfkill_unregister(struct rfkill
*rfkill
)
865 device_del(&rfkill
->dev
);
866 rfkill_remove_switch(rfkill
);
867 rfkill_led_trigger_unregister(rfkill
);
868 put_device(&rfkill
->dev
);
870 EXPORT_SYMBOL(rfkill_unregister
);
873 * rfkill_set_default - set initial value for a switch type
874 * @type - the type of switch to set the default state of
875 * @state - the new default state for that group of switches
877 * Sets the initial state rfkill should use for a given type.
878 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
879 * and RFKILL_STATE_UNBLOCKED.
881 * This function is meant to be used by platform drivers for platforms
882 * that can save switch state across power down/reboot.
884 * The default state for each switch type can be changed exactly once.
885 * After a switch of that type is registered, the default state cannot
886 * be changed anymore. This guards against multiple drivers it the
887 * same platform trying to set the initial switch default state, which
890 * Returns -EPERM if the state has already been set once or is in use,
891 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
892 * if this function returns -EPERM.
894 * Returns 0 if the new default state was set, or an error if it
897 int rfkill_set_default(enum rfkill_type type
, enum rfkill_state state
)
901 if (WARN((type
>= RFKILL_TYPE_MAX
||
902 (state
!= RFKILL_STATE_SOFT_BLOCKED
&&
903 state
!= RFKILL_STATE_UNBLOCKED
)),
905 "rfkill: illegal state %d or type %d passed as "
906 "parameter to rfkill_set_default\n", state
, type
))
909 mutex_lock(&rfkill_global_mutex
);
911 if (!test_and_set_bit(type
, rfkill_states_lockdflt
)) {
912 rfkill_global_states
[type
].default_state
= state
;
913 rfkill_global_states
[type
].current_state
= state
;
918 mutex_unlock(&rfkill_global_mutex
);
921 EXPORT_SYMBOL_GPL(rfkill_set_default
);
924 * Rfkill module initialization/deinitialization.
926 static int __init
rfkill_init(void)
931 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
932 if (rfkill_default_state
!= RFKILL_STATE_SOFT_BLOCKED
&&
933 rfkill_default_state
!= RFKILL_STATE_UNBLOCKED
)
936 for (i
= 0; i
< RFKILL_TYPE_MAX
; i
++)
937 rfkill_global_states
[i
].default_state
= rfkill_default_state
;
939 error
= class_register(&rfkill_class
);
941 printk(KERN_ERR
"rfkill: unable to register rfkill class\n");
945 register_rfkill_notifier(&rfkill_blocking_uevent_nb
);
950 static void __exit
rfkill_exit(void)
952 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb
);
953 class_unregister(&rfkill_class
);
956 subsys_initcall(rfkill_init
);
957 module_exit(rfkill_exit
);