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_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 static enum rfkill_state rfkill_states
[RFKILL_TYPE_MAX
];
49 static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list
);
53 * register_rfkill_notifier - Add notifier to rfkill notifier chain
54 * @nb: pointer to the new entry to add to the chain
56 * See blocking_notifier_chain_register() for return value and further
59 * Adds a notifier to the rfkill notifier chain. The chain will be
60 * called with a pointer to the relevant rfkill structure as a parameter,
61 * refer to include/linux/rfkill.h for the possible events.
63 * Notifiers added to this chain are to always return NOTIFY_DONE. This
64 * chain is a blocking notifier chain: notifiers can sleep.
66 * Calls to this chain may have been done through a workqueue. One must
67 * assume unordered asynchronous behaviour, there is no way to know if
68 * actions related to the event that generated the notification have been
69 * carried out already.
71 int register_rfkill_notifier(struct notifier_block
*nb
)
73 return blocking_notifier_chain_register(&rfkill_notifier_list
, nb
);
75 EXPORT_SYMBOL_GPL(register_rfkill_notifier
);
78 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
79 * @nb: pointer to the entry to remove from the chain
81 * See blocking_notifier_chain_unregister() for return value and further
84 * Removes a notifier from the rfkill notifier chain.
86 int unregister_rfkill_notifier(struct notifier_block
*nb
)
88 return blocking_notifier_chain_unregister(&rfkill_notifier_list
, nb
);
90 EXPORT_SYMBOL_GPL(unregister_rfkill_notifier
);
93 static void rfkill_led_trigger(struct rfkill
*rfkill
,
94 enum rfkill_state state
)
96 #ifdef CONFIG_RFKILL_LEDS
97 struct led_trigger
*led
= &rfkill
->led_trigger
;
101 if (state
!= RFKILL_STATE_UNBLOCKED
)
102 led_trigger_event(led
, LED_OFF
);
104 led_trigger_event(led
, LED_FULL
);
105 #endif /* CONFIG_RFKILL_LEDS */
108 #ifdef CONFIG_RFKILL_LEDS
109 static void rfkill_led_trigger_activate(struct led_classdev
*led
)
111 struct rfkill
*rfkill
= container_of(led
->trigger
,
112 struct rfkill
, led_trigger
);
114 rfkill_led_trigger(rfkill
, rfkill
->state
);
116 #endif /* CONFIG_RFKILL_LEDS */
118 static void notify_rfkill_state_change(struct rfkill
*rfkill
)
120 blocking_notifier_call_chain(&rfkill_notifier_list
,
121 RFKILL_STATE_CHANGED
,
125 static void update_rfkill_state(struct rfkill
*rfkill
)
127 enum rfkill_state newstate
, oldstate
;
129 if (rfkill
->get_state
) {
130 mutex_lock(&rfkill
->mutex
);
131 if (!rfkill
->get_state(rfkill
->data
, &newstate
)) {
132 oldstate
= rfkill
->state
;
133 rfkill
->state
= newstate
;
134 if (oldstate
!= newstate
)
135 notify_rfkill_state_change(rfkill
);
137 mutex_unlock(&rfkill
->mutex
);
142 * rfkill_toggle_radio - wrapper for toggle_radio hook
143 * @rfkill: the rfkill struct to use
144 * @force: calls toggle_radio even if cache says it is not needed,
145 * and also makes sure notifications of the state will be
146 * sent even if it didn't change
147 * @state: the new state to call toggle_radio() with
149 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
150 * calls and handling all the red tape such as issuing notifications
151 * if the call is successful.
153 * Note that the @force parameter cannot override a (possibly cached)
154 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
155 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
156 * rfkill_force_state(), so the cache either is bypassed or valid.
158 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
159 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
160 * give the driver a hint that it should double-BLOCK the transmitter.
162 * Caller must have acquired rfkill->mutex.
164 static int rfkill_toggle_radio(struct rfkill
*rfkill
,
165 enum rfkill_state state
,
169 enum rfkill_state oldstate
, newstate
;
171 oldstate
= rfkill
->state
;
173 if (rfkill
->get_state
&& !force
&&
174 !rfkill
->get_state(rfkill
->data
, &newstate
))
175 rfkill
->state
= newstate
;
178 case RFKILL_STATE_HARD_BLOCKED
:
179 /* typically happens when refreshing hardware state,
180 * such as on resume */
181 state
= RFKILL_STATE_SOFT_BLOCKED
;
183 case RFKILL_STATE_UNBLOCKED
:
184 /* force can't override this, only rfkill_force_state() can */
185 if (rfkill
->state
== RFKILL_STATE_HARD_BLOCKED
)
188 case RFKILL_STATE_SOFT_BLOCKED
:
189 /* nothing to do, we want to give drivers the hint to double
190 * BLOCK even a transmitter that is already in state
191 * RFKILL_STATE_HARD_BLOCKED */
195 if (force
|| state
!= rfkill
->state
) {
196 retval
= rfkill
->toggle_radio(rfkill
->data
, state
);
197 /* never allow a HARD->SOFT downgrade! */
198 if (!retval
&& rfkill
->state
!= RFKILL_STATE_HARD_BLOCKED
)
199 rfkill
->state
= state
;
202 if (force
|| rfkill
->state
!= oldstate
) {
203 rfkill_led_trigger(rfkill
, rfkill
->state
);
204 notify_rfkill_state_change(rfkill
);
211 * rfkill_switch_all - Toggle state of all switches of given type
212 * @type: type of interfaces to be affected
213 * @state: the new state
215 * This function toggles the state of all switches of given type,
216 * unless a specific switch is claimed by userspace (in which case,
217 * that switch is left alone).
219 void rfkill_switch_all(enum rfkill_type type
, enum rfkill_state state
)
221 struct rfkill
*rfkill
;
223 mutex_lock(&rfkill_mutex
);
225 rfkill_states
[type
] = state
;
227 list_for_each_entry(rfkill
, &rfkill_list
, node
) {
228 if ((!rfkill
->user_claim
) && (rfkill
->type
== type
)) {
229 mutex_lock(&rfkill
->mutex
);
230 rfkill_toggle_radio(rfkill
, state
, 0);
231 mutex_unlock(&rfkill
->mutex
);
235 mutex_unlock(&rfkill_mutex
);
237 EXPORT_SYMBOL(rfkill_switch_all
);
240 * rfkill_epo - emergency power off all transmitters
242 * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
243 * everything in its path but rfkill_mutex and rfkill->mutex.
245 void rfkill_epo(void)
247 struct rfkill
*rfkill
;
249 mutex_lock(&rfkill_mutex
);
250 list_for_each_entry(rfkill
, &rfkill_list
, node
) {
251 mutex_lock(&rfkill
->mutex
);
252 rfkill_toggle_radio(rfkill
, RFKILL_STATE_SOFT_BLOCKED
, 1);
253 mutex_unlock(&rfkill
->mutex
);
255 mutex_unlock(&rfkill_mutex
);
257 EXPORT_SYMBOL_GPL(rfkill_epo
);
260 * rfkill_force_state - Force the internal rfkill radio state
261 * @rfkill: pointer to the rfkill class to modify.
262 * @state: the current radio state the class should be forced to.
264 * This function updates the internal state of the radio cached
265 * by the rfkill class. It should be used when the driver gets
266 * a notification by the firmware/hardware of the current *real*
267 * state of the radio rfkill switch.
269 * Devices which are subject to external changes on their rfkill
270 * state (such as those caused by a hardware rfkill line) MUST
271 * have their driver arrange to call rfkill_force_state() as soon
272 * as possible after such a change.
274 * This function may not be called from an atomic context.
276 int rfkill_force_state(struct rfkill
*rfkill
, enum rfkill_state state
)
278 enum rfkill_state oldstate
;
280 if (state
!= RFKILL_STATE_SOFT_BLOCKED
&&
281 state
!= RFKILL_STATE_UNBLOCKED
&&
282 state
!= RFKILL_STATE_HARD_BLOCKED
)
285 mutex_lock(&rfkill
->mutex
);
287 oldstate
= rfkill
->state
;
288 rfkill
->state
= state
;
290 if (state
!= oldstate
)
291 notify_rfkill_state_change(rfkill
);
293 mutex_unlock(&rfkill
->mutex
);
297 EXPORT_SYMBOL(rfkill_force_state
);
299 static ssize_t
rfkill_name_show(struct device
*dev
,
300 struct device_attribute
*attr
,
303 struct rfkill
*rfkill
= to_rfkill(dev
);
305 return sprintf(buf
, "%s\n", rfkill
->name
);
308 static const char *rfkill_get_type_str(enum rfkill_type type
)
311 case RFKILL_TYPE_WLAN
:
313 case RFKILL_TYPE_BLUETOOTH
:
315 case RFKILL_TYPE_UWB
:
316 return "ultrawideband";
317 case RFKILL_TYPE_WIMAX
:
319 case RFKILL_TYPE_WWAN
:
326 static ssize_t
rfkill_type_show(struct device
*dev
,
327 struct device_attribute
*attr
,
330 struct rfkill
*rfkill
= to_rfkill(dev
);
332 return sprintf(buf
, "%s\n", rfkill_get_type_str(rfkill
->type
));
335 static ssize_t
rfkill_state_show(struct device
*dev
,
336 struct device_attribute
*attr
,
339 struct rfkill
*rfkill
= to_rfkill(dev
);
341 update_rfkill_state(rfkill
);
342 return sprintf(buf
, "%d\n", rfkill
->state
);
345 static ssize_t
rfkill_state_store(struct device
*dev
,
346 struct device_attribute
*attr
,
347 const char *buf
, size_t count
)
349 struct rfkill
*rfkill
= to_rfkill(dev
);
350 unsigned int state
= simple_strtoul(buf
, NULL
, 0);
353 if (!capable(CAP_NET_ADMIN
))
356 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
357 if (state
!= RFKILL_STATE_UNBLOCKED
&&
358 state
!= RFKILL_STATE_SOFT_BLOCKED
)
361 if (mutex_lock_interruptible(&rfkill
->mutex
))
363 error
= rfkill_toggle_radio(rfkill
, state
, 0);
364 mutex_unlock(&rfkill
->mutex
);
366 return error
? error
: count
;
369 static ssize_t
rfkill_claim_show(struct device
*dev
,
370 struct device_attribute
*attr
,
373 struct rfkill
*rfkill
= to_rfkill(dev
);
375 return sprintf(buf
, "%d", rfkill
->user_claim
);
378 static ssize_t
rfkill_claim_store(struct device
*dev
,
379 struct device_attribute
*attr
,
380 const char *buf
, size_t count
)
382 struct rfkill
*rfkill
= to_rfkill(dev
);
383 bool claim
= !!simple_strtoul(buf
, NULL
, 0);
386 if (!capable(CAP_NET_ADMIN
))
389 if (rfkill
->user_claim_unsupported
)
393 * Take the global lock to make sure the kernel is not in
394 * the middle of rfkill_switch_all
396 error
= mutex_lock_interruptible(&rfkill_mutex
);
400 if (rfkill
->user_claim
!= claim
) {
402 mutex_lock(&rfkill
->mutex
);
403 rfkill_toggle_radio(rfkill
,
404 rfkill_states
[rfkill
->type
],
406 mutex_unlock(&rfkill
->mutex
);
408 rfkill
->user_claim
= claim
;
411 mutex_unlock(&rfkill_mutex
);
413 return error
? error
: count
;
416 static struct device_attribute rfkill_dev_attrs
[] = {
417 __ATTR(name
, S_IRUGO
, rfkill_name_show
, NULL
),
418 __ATTR(type
, S_IRUGO
, rfkill_type_show
, NULL
),
419 __ATTR(state
, S_IRUGO
|S_IWUSR
, rfkill_state_show
, rfkill_state_store
),
420 __ATTR(claim
, S_IRUGO
|S_IWUSR
, rfkill_claim_show
, rfkill_claim_store
),
424 static void rfkill_release(struct device
*dev
)
426 struct rfkill
*rfkill
= to_rfkill(dev
);
429 module_put(THIS_MODULE
);
433 static int rfkill_suspend(struct device
*dev
, pm_message_t state
)
435 struct rfkill
*rfkill
= to_rfkill(dev
);
437 if (dev
->power
.power_state
.event
!= state
.event
) {
438 if (state
.event
& PM_EVENT_SLEEP
) {
439 /* Stop transmitter, keep state, no notifies */
440 update_rfkill_state(rfkill
);
442 mutex_lock(&rfkill
->mutex
);
443 rfkill
->toggle_radio(rfkill
->data
,
444 RFKILL_STATE_SOFT_BLOCKED
);
445 mutex_unlock(&rfkill
->mutex
);
448 dev
->power
.power_state
= state
;
454 static int rfkill_resume(struct device
*dev
)
456 struct rfkill
*rfkill
= to_rfkill(dev
);
458 if (dev
->power
.power_state
.event
!= PM_EVENT_ON
) {
459 mutex_lock(&rfkill
->mutex
);
461 /* restore radio state AND notify everybody */
462 rfkill_toggle_radio(rfkill
, rfkill
->state
, 1);
464 mutex_unlock(&rfkill
->mutex
);
467 dev
->power
.power_state
= PMSG_ON
;
471 #define rfkill_suspend NULL
472 #define rfkill_resume NULL
475 static int rfkill_blocking_uevent_notifier(struct notifier_block
*nb
,
476 unsigned long eventid
,
479 struct rfkill
*rfkill
= (struct rfkill
*)data
;
482 case RFKILL_STATE_CHANGED
:
483 kobject_uevent(&rfkill
->dev
.kobj
, KOBJ_CHANGE
);
492 static struct notifier_block rfkill_blocking_uevent_nb
= {
493 .notifier_call
= rfkill_blocking_uevent_notifier
,
497 static int rfkill_dev_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
499 struct rfkill
*rfkill
= to_rfkill(dev
);
502 error
= add_uevent_var(env
, "RFKILL_NAME=%s", rfkill
->name
);
505 error
= add_uevent_var(env
, "RFKILL_TYPE=%s",
506 rfkill_get_type_str(rfkill
->type
));
509 error
= add_uevent_var(env
, "RFKILL_STATE=%d", rfkill
->state
);
513 static struct class rfkill_class
= {
515 .dev_release
= rfkill_release
,
516 .dev_attrs
= rfkill_dev_attrs
,
517 .suspend
= rfkill_suspend
,
518 .resume
= rfkill_resume
,
519 .dev_uevent
= rfkill_dev_uevent
,
522 static int rfkill_add_switch(struct rfkill
*rfkill
)
524 mutex_lock(&rfkill_mutex
);
526 rfkill_toggle_radio(rfkill
, rfkill_states
[rfkill
->type
], 0);
528 list_add_tail(&rfkill
->node
, &rfkill_list
);
530 mutex_unlock(&rfkill_mutex
);
535 static void rfkill_remove_switch(struct rfkill
*rfkill
)
537 mutex_lock(&rfkill_mutex
);
538 list_del_init(&rfkill
->node
);
539 mutex_unlock(&rfkill_mutex
);
541 mutex_lock(&rfkill
->mutex
);
542 rfkill_toggle_radio(rfkill
, RFKILL_STATE_SOFT_BLOCKED
, 1);
543 mutex_unlock(&rfkill
->mutex
);
547 * rfkill_allocate - allocate memory for rfkill structure.
548 * @parent: device that has rf switch on it
549 * @type: type of the switch (RFKILL_TYPE_*)
551 * This function should be called by the network driver when it needs
552 * rfkill structure. Once the structure is allocated the driver should
553 * finish its initialization by setting the name, private data, enable_radio
554 * and disable_radio methods and then register it with rfkill_register().
556 * NOTE: If registration fails the structure shoudl be freed by calling
557 * rfkill_free() otherwise rfkill_unregister() should be used.
559 struct rfkill
*rfkill_allocate(struct device
*parent
, enum rfkill_type type
)
561 struct rfkill
*rfkill
;
564 rfkill
= kzalloc(sizeof(struct rfkill
), GFP_KERNEL
);
568 mutex_init(&rfkill
->mutex
);
569 INIT_LIST_HEAD(&rfkill
->node
);
573 dev
->class = &rfkill_class
;
574 dev
->parent
= parent
;
575 device_initialize(dev
);
577 __module_get(THIS_MODULE
);
581 EXPORT_SYMBOL(rfkill_allocate
);
584 * rfkill_free - Mark rfkill structure for deletion
585 * @rfkill: rfkill structure to be destroyed
587 * Decrements reference count of the rfkill structure so it is destroyed.
588 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
590 void rfkill_free(struct rfkill
*rfkill
)
593 put_device(&rfkill
->dev
);
595 EXPORT_SYMBOL(rfkill_free
);
597 static void rfkill_led_trigger_register(struct rfkill
*rfkill
)
599 #ifdef CONFIG_RFKILL_LEDS
602 if (!rfkill
->led_trigger
.name
)
603 rfkill
->led_trigger
.name
= rfkill
->dev
.bus_id
;
604 if (!rfkill
->led_trigger
.activate
)
605 rfkill
->led_trigger
.activate
= rfkill_led_trigger_activate
;
606 error
= led_trigger_register(&rfkill
->led_trigger
);
608 rfkill
->led_trigger
.name
= NULL
;
609 #endif /* CONFIG_RFKILL_LEDS */
612 static void rfkill_led_trigger_unregister(struct rfkill
*rfkill
)
614 #ifdef CONFIG_RFKILL_LEDS
615 if (rfkill
->led_trigger
.name
) {
616 led_trigger_unregister(&rfkill
->led_trigger
);
617 rfkill
->led_trigger
.name
= NULL
;
623 * rfkill_register - Register a rfkill structure.
624 * @rfkill: rfkill structure to be registered
626 * This function should be called by the network driver when the rfkill
627 * structure needs to be registered. Immediately from registration the
628 * switch driver should be able to service calls to toggle_radio.
630 int rfkill_register(struct rfkill
*rfkill
)
632 static atomic_t rfkill_no
= ATOMIC_INIT(0);
633 struct device
*dev
= &rfkill
->dev
;
636 if (!rfkill
->toggle_radio
)
638 if (rfkill
->type
>= RFKILL_TYPE_MAX
)
641 snprintf(dev
->bus_id
, sizeof(dev
->bus_id
),
642 "rfkill%ld", (long)atomic_inc_return(&rfkill_no
) - 1);
644 rfkill_led_trigger_register(rfkill
);
646 error
= rfkill_add_switch(rfkill
);
648 rfkill_led_trigger_unregister(rfkill
);
652 error
= device_add(dev
);
654 rfkill_remove_switch(rfkill
);
655 rfkill_led_trigger_unregister(rfkill
);
661 EXPORT_SYMBOL(rfkill_register
);
664 * rfkill_unregister - Unregister a rfkill structure.
665 * @rfkill: rfkill structure to be unregistered
667 * This function should be called by the network driver during device
668 * teardown to destroy rfkill structure. Note that rfkill_free() should
669 * _not_ be called after rfkill_unregister().
671 void rfkill_unregister(struct rfkill
*rfkill
)
673 device_del(&rfkill
->dev
);
674 rfkill_remove_switch(rfkill
);
675 rfkill_led_trigger_unregister(rfkill
);
676 put_device(&rfkill
->dev
);
678 EXPORT_SYMBOL(rfkill_unregister
);
681 * Rfkill module initialization/deinitialization.
683 static int __init
rfkill_init(void)
688 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
689 if (rfkill_default_state
!= RFKILL_STATE_SOFT_BLOCKED
&&
690 rfkill_default_state
!= RFKILL_STATE_UNBLOCKED
)
693 for (i
= 0; i
< ARRAY_SIZE(rfkill_states
); i
++)
694 rfkill_states
[i
] = rfkill_default_state
;
696 error
= class_register(&rfkill_class
);
698 printk(KERN_ERR
"rfkill: unable to register rfkill class\n");
702 register_rfkill_notifier(&rfkill_blocking_uevent_nb
);
707 static void __exit
rfkill_exit(void)
709 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb
);
710 class_unregister(&rfkill_class
);
713 subsys_initcall(rfkill_init
);
714 module_exit(rfkill_exit
);