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 rfkill_led_trigger(rfkill
, rfkill
->state
);
121 blocking_notifier_call_chain(&rfkill_notifier_list
,
122 RFKILL_STATE_CHANGED
,
126 static void update_rfkill_state(struct rfkill
*rfkill
)
128 enum rfkill_state newstate
, oldstate
;
130 if (rfkill
->get_state
) {
131 mutex_lock(&rfkill
->mutex
);
132 if (!rfkill
->get_state(rfkill
->data
, &newstate
)) {
133 oldstate
= rfkill
->state
;
134 rfkill
->state
= newstate
;
135 if (oldstate
!= newstate
)
136 notify_rfkill_state_change(rfkill
);
138 mutex_unlock(&rfkill
->mutex
);
143 * rfkill_toggle_radio - wrapper for toggle_radio hook
144 * @rfkill: the rfkill struct to use
145 * @force: calls toggle_radio even if cache says it is not needed,
146 * and also makes sure notifications of the state will be
147 * sent even if it didn't change
148 * @state: the new state to call toggle_radio() with
150 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
151 * calls and handling all the red tape such as issuing notifications
152 * if the call is successful.
154 * Suspended devices are not touched at all, and -EAGAIN is returned.
156 * Note that the @force parameter cannot override a (possibly cached)
157 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
158 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
159 * rfkill_force_state(), so the cache either is bypassed or valid.
161 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
162 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
163 * give the driver a hint that it should double-BLOCK the transmitter.
165 * Caller must have acquired rfkill->mutex.
167 static int rfkill_toggle_radio(struct rfkill
*rfkill
,
168 enum rfkill_state state
,
172 enum rfkill_state oldstate
, newstate
;
174 if (unlikely(rfkill
->dev
.power
.power_state
.event
& PM_EVENT_SLEEP
))
177 oldstate
= rfkill
->state
;
179 if (rfkill
->get_state
&& !force
&&
180 !rfkill
->get_state(rfkill
->data
, &newstate
))
181 rfkill
->state
= newstate
;
184 case RFKILL_STATE_HARD_BLOCKED
:
185 /* typically happens when refreshing hardware state,
186 * such as on resume */
187 state
= RFKILL_STATE_SOFT_BLOCKED
;
189 case RFKILL_STATE_UNBLOCKED
:
190 /* force can't override this, only rfkill_force_state() can */
191 if (rfkill
->state
== RFKILL_STATE_HARD_BLOCKED
)
194 case RFKILL_STATE_SOFT_BLOCKED
:
195 /* nothing to do, we want to give drivers the hint to double
196 * BLOCK even a transmitter that is already in state
197 * RFKILL_STATE_HARD_BLOCKED */
201 if (force
|| state
!= rfkill
->state
) {
202 retval
= rfkill
->toggle_radio(rfkill
->data
, state
);
203 /* never allow a HARD->SOFT downgrade! */
204 if (!retval
&& rfkill
->state
!= RFKILL_STATE_HARD_BLOCKED
)
205 rfkill
->state
= state
;
208 if (force
|| rfkill
->state
!= oldstate
)
209 notify_rfkill_state_change(rfkill
);
215 * rfkill_switch_all - Toggle state of all switches of given type
216 * @type: type of interfaces to be affected
217 * @state: the new state
219 * This function toggles the state of all switches of given type,
220 * unless a specific switch is claimed by userspace (in which case,
221 * that switch is left alone) or suspended.
223 void rfkill_switch_all(enum rfkill_type type
, enum rfkill_state state
)
225 struct rfkill
*rfkill
;
227 mutex_lock(&rfkill_mutex
);
229 rfkill_states
[type
] = state
;
231 list_for_each_entry(rfkill
, &rfkill_list
, node
) {
232 if ((!rfkill
->user_claim
) && (rfkill
->type
== type
)) {
233 mutex_lock(&rfkill
->mutex
);
234 rfkill_toggle_radio(rfkill
, state
, 0);
235 mutex_unlock(&rfkill
->mutex
);
239 mutex_unlock(&rfkill_mutex
);
241 EXPORT_SYMBOL(rfkill_switch_all
);
244 * rfkill_epo - emergency power off all transmitters
246 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
247 * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
249 void rfkill_epo(void)
251 struct rfkill
*rfkill
;
253 mutex_lock(&rfkill_mutex
);
254 list_for_each_entry(rfkill
, &rfkill_list
, node
) {
255 mutex_lock(&rfkill
->mutex
);
256 rfkill_toggle_radio(rfkill
, RFKILL_STATE_SOFT_BLOCKED
, 1);
257 mutex_unlock(&rfkill
->mutex
);
259 mutex_unlock(&rfkill_mutex
);
261 EXPORT_SYMBOL_GPL(rfkill_epo
);
264 * rfkill_force_state - Force the internal rfkill radio state
265 * @rfkill: pointer to the rfkill class to modify.
266 * @state: the current radio state the class should be forced to.
268 * This function updates the internal state of the radio cached
269 * by the rfkill class. It should be used when the driver gets
270 * a notification by the firmware/hardware of the current *real*
271 * state of the radio rfkill switch.
273 * Devices which are subject to external changes on their rfkill
274 * state (such as those caused by a hardware rfkill line) MUST
275 * have their driver arrange to call rfkill_force_state() as soon
276 * as possible after such a change.
278 * This function may not be called from an atomic context.
280 int rfkill_force_state(struct rfkill
*rfkill
, enum rfkill_state state
)
282 enum rfkill_state oldstate
;
284 if (state
!= RFKILL_STATE_SOFT_BLOCKED
&&
285 state
!= RFKILL_STATE_UNBLOCKED
&&
286 state
!= RFKILL_STATE_HARD_BLOCKED
)
289 mutex_lock(&rfkill
->mutex
);
291 oldstate
= rfkill
->state
;
292 rfkill
->state
= state
;
294 if (state
!= oldstate
)
295 notify_rfkill_state_change(rfkill
);
297 mutex_unlock(&rfkill
->mutex
);
301 EXPORT_SYMBOL(rfkill_force_state
);
303 static ssize_t
rfkill_name_show(struct device
*dev
,
304 struct device_attribute
*attr
,
307 struct rfkill
*rfkill
= to_rfkill(dev
);
309 return sprintf(buf
, "%s\n", rfkill
->name
);
312 static const char *rfkill_get_type_str(enum rfkill_type type
)
315 case RFKILL_TYPE_WLAN
:
317 case RFKILL_TYPE_BLUETOOTH
:
319 case RFKILL_TYPE_UWB
:
320 return "ultrawideband";
321 case RFKILL_TYPE_WIMAX
:
323 case RFKILL_TYPE_WWAN
:
330 static ssize_t
rfkill_type_show(struct device
*dev
,
331 struct device_attribute
*attr
,
334 struct rfkill
*rfkill
= to_rfkill(dev
);
336 return sprintf(buf
, "%s\n", rfkill_get_type_str(rfkill
->type
));
339 static ssize_t
rfkill_state_show(struct device
*dev
,
340 struct device_attribute
*attr
,
343 struct rfkill
*rfkill
= to_rfkill(dev
);
345 update_rfkill_state(rfkill
);
346 return sprintf(buf
, "%d\n", rfkill
->state
);
349 static ssize_t
rfkill_state_store(struct device
*dev
,
350 struct device_attribute
*attr
,
351 const char *buf
, size_t count
)
353 struct rfkill
*rfkill
= to_rfkill(dev
);
354 unsigned int state
= simple_strtoul(buf
, NULL
, 0);
357 if (!capable(CAP_NET_ADMIN
))
360 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
361 if (state
!= RFKILL_STATE_UNBLOCKED
&&
362 state
!= RFKILL_STATE_SOFT_BLOCKED
)
365 if (mutex_lock_interruptible(&rfkill
->mutex
))
367 error
= rfkill_toggle_radio(rfkill
, state
, 0);
368 mutex_unlock(&rfkill
->mutex
);
370 return error
? error
: count
;
373 static ssize_t
rfkill_claim_show(struct device
*dev
,
374 struct device_attribute
*attr
,
377 struct rfkill
*rfkill
= to_rfkill(dev
);
379 return sprintf(buf
, "%d\n", rfkill
->user_claim
);
382 static ssize_t
rfkill_claim_store(struct device
*dev
,
383 struct device_attribute
*attr
,
384 const char *buf
, size_t count
)
386 struct rfkill
*rfkill
= to_rfkill(dev
);
387 bool claim
= !!simple_strtoul(buf
, NULL
, 0);
390 if (!capable(CAP_NET_ADMIN
))
393 if (rfkill
->user_claim_unsupported
)
397 * Take the global lock to make sure the kernel is not in
398 * the middle of rfkill_switch_all
400 error
= mutex_lock_interruptible(&rfkill_mutex
);
404 if (rfkill
->user_claim
!= claim
) {
406 mutex_lock(&rfkill
->mutex
);
407 rfkill_toggle_radio(rfkill
,
408 rfkill_states
[rfkill
->type
],
410 mutex_unlock(&rfkill
->mutex
);
412 rfkill
->user_claim
= claim
;
415 mutex_unlock(&rfkill_mutex
);
417 return error
? error
: count
;
420 static struct device_attribute rfkill_dev_attrs
[] = {
421 __ATTR(name
, S_IRUGO
, rfkill_name_show
, NULL
),
422 __ATTR(type
, S_IRUGO
, rfkill_type_show
, NULL
),
423 __ATTR(state
, S_IRUGO
|S_IWUSR
, rfkill_state_show
, rfkill_state_store
),
424 __ATTR(claim
, S_IRUGO
|S_IWUSR
, rfkill_claim_show
, rfkill_claim_store
),
428 static void rfkill_release(struct device
*dev
)
430 struct rfkill
*rfkill
= to_rfkill(dev
);
433 module_put(THIS_MODULE
);
437 static int rfkill_suspend(struct device
*dev
, pm_message_t state
)
439 struct rfkill
*rfkill
= to_rfkill(dev
);
441 if (dev
->power
.power_state
.event
!= state
.event
) {
442 if (state
.event
& PM_EVENT_SLEEP
) {
443 /* Stop transmitter, keep state, no notifies */
444 update_rfkill_state(rfkill
);
446 mutex_lock(&rfkill
->mutex
);
447 rfkill
->toggle_radio(rfkill
->data
,
448 RFKILL_STATE_SOFT_BLOCKED
);
449 mutex_unlock(&rfkill
->mutex
);
452 dev
->power
.power_state
= state
;
458 static int rfkill_resume(struct device
*dev
)
460 struct rfkill
*rfkill
= to_rfkill(dev
);
462 if (dev
->power
.power_state
.event
!= PM_EVENT_ON
) {
463 mutex_lock(&rfkill
->mutex
);
465 dev
->power
.power_state
.event
= PM_EVENT_ON
;
467 /* restore radio state AND notify everybody */
468 rfkill_toggle_radio(rfkill
, rfkill
->state
, 1);
470 mutex_unlock(&rfkill
->mutex
);
476 #define rfkill_suspend NULL
477 #define rfkill_resume NULL
480 static int rfkill_blocking_uevent_notifier(struct notifier_block
*nb
,
481 unsigned long eventid
,
484 struct rfkill
*rfkill
= (struct rfkill
*)data
;
487 case RFKILL_STATE_CHANGED
:
488 kobject_uevent(&rfkill
->dev
.kobj
, KOBJ_CHANGE
);
497 static struct notifier_block rfkill_blocking_uevent_nb
= {
498 .notifier_call
= rfkill_blocking_uevent_notifier
,
502 static int rfkill_dev_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
504 struct rfkill
*rfkill
= to_rfkill(dev
);
507 error
= add_uevent_var(env
, "RFKILL_NAME=%s", rfkill
->name
);
510 error
= add_uevent_var(env
, "RFKILL_TYPE=%s",
511 rfkill_get_type_str(rfkill
->type
));
514 error
= add_uevent_var(env
, "RFKILL_STATE=%d", rfkill
->state
);
518 static struct class rfkill_class
= {
520 .dev_release
= rfkill_release
,
521 .dev_attrs
= rfkill_dev_attrs
,
522 .suspend
= rfkill_suspend
,
523 .resume
= rfkill_resume
,
524 .dev_uevent
= rfkill_dev_uevent
,
527 static int rfkill_add_switch(struct rfkill
*rfkill
)
529 mutex_lock(&rfkill_mutex
);
531 rfkill_toggle_radio(rfkill
, rfkill_states
[rfkill
->type
], 0);
533 list_add_tail(&rfkill
->node
, &rfkill_list
);
535 mutex_unlock(&rfkill_mutex
);
540 static void rfkill_remove_switch(struct rfkill
*rfkill
)
542 mutex_lock(&rfkill_mutex
);
543 list_del_init(&rfkill
->node
);
544 mutex_unlock(&rfkill_mutex
);
546 mutex_lock(&rfkill
->mutex
);
547 rfkill_toggle_radio(rfkill
, RFKILL_STATE_SOFT_BLOCKED
, 1);
548 mutex_unlock(&rfkill
->mutex
);
552 * rfkill_allocate - allocate memory for rfkill structure.
553 * @parent: device that has rf switch on it
554 * @type: type of the switch (RFKILL_TYPE_*)
556 * This function should be called by the network driver when it needs
557 * rfkill structure. Once the structure is allocated the driver should
558 * finish its initialization by setting the name, private data, enable_radio
559 * and disable_radio methods and then register it with rfkill_register().
561 * NOTE: If registration fails the structure shoudl be freed by calling
562 * rfkill_free() otherwise rfkill_unregister() should be used.
564 struct rfkill
*rfkill_allocate(struct device
*parent
, enum rfkill_type type
)
566 struct rfkill
*rfkill
;
569 rfkill
= kzalloc(sizeof(struct rfkill
), GFP_KERNEL
);
573 mutex_init(&rfkill
->mutex
);
574 INIT_LIST_HEAD(&rfkill
->node
);
578 dev
->class = &rfkill_class
;
579 dev
->parent
= parent
;
580 device_initialize(dev
);
582 __module_get(THIS_MODULE
);
586 EXPORT_SYMBOL(rfkill_allocate
);
589 * rfkill_free - Mark rfkill structure for deletion
590 * @rfkill: rfkill structure to be destroyed
592 * Decrements reference count of the rfkill structure so it is destroyed.
593 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
595 void rfkill_free(struct rfkill
*rfkill
)
598 put_device(&rfkill
->dev
);
600 EXPORT_SYMBOL(rfkill_free
);
602 static void rfkill_led_trigger_register(struct rfkill
*rfkill
)
604 #ifdef CONFIG_RFKILL_LEDS
607 if (!rfkill
->led_trigger
.name
)
608 rfkill
->led_trigger
.name
= rfkill
->dev
.bus_id
;
609 if (!rfkill
->led_trigger
.activate
)
610 rfkill
->led_trigger
.activate
= rfkill_led_trigger_activate
;
611 error
= led_trigger_register(&rfkill
->led_trigger
);
613 rfkill
->led_trigger
.name
= NULL
;
614 #endif /* CONFIG_RFKILL_LEDS */
617 static void rfkill_led_trigger_unregister(struct rfkill
*rfkill
)
619 #ifdef CONFIG_RFKILL_LEDS
620 if (rfkill
->led_trigger
.name
) {
621 led_trigger_unregister(&rfkill
->led_trigger
);
622 rfkill
->led_trigger
.name
= NULL
;
628 * rfkill_register - Register a rfkill structure.
629 * @rfkill: rfkill structure to be registered
631 * This function should be called by the network driver when the rfkill
632 * structure needs to be registered. Immediately from registration the
633 * switch driver should be able to service calls to toggle_radio.
635 int rfkill_register(struct rfkill
*rfkill
)
637 static atomic_t rfkill_no
= ATOMIC_INIT(0);
638 struct device
*dev
= &rfkill
->dev
;
641 if (!rfkill
->toggle_radio
)
643 if (rfkill
->type
>= RFKILL_TYPE_MAX
)
646 snprintf(dev
->bus_id
, sizeof(dev
->bus_id
),
647 "rfkill%ld", (long)atomic_inc_return(&rfkill_no
) - 1);
649 rfkill_led_trigger_register(rfkill
);
651 error
= rfkill_add_switch(rfkill
);
653 rfkill_led_trigger_unregister(rfkill
);
657 error
= device_add(dev
);
659 rfkill_remove_switch(rfkill
);
660 rfkill_led_trigger_unregister(rfkill
);
666 EXPORT_SYMBOL(rfkill_register
);
669 * rfkill_unregister - Unregister a rfkill structure.
670 * @rfkill: rfkill structure to be unregistered
672 * This function should be called by the network driver during device
673 * teardown to destroy rfkill structure. Note that rfkill_free() should
674 * _not_ be called after rfkill_unregister().
676 void rfkill_unregister(struct rfkill
*rfkill
)
678 device_del(&rfkill
->dev
);
679 rfkill_remove_switch(rfkill
);
680 rfkill_led_trigger_unregister(rfkill
);
681 put_device(&rfkill
->dev
);
683 EXPORT_SYMBOL(rfkill_unregister
);
686 * Rfkill module initialization/deinitialization.
688 static int __init
rfkill_init(void)
693 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
694 if (rfkill_default_state
!= RFKILL_STATE_SOFT_BLOCKED
&&
695 rfkill_default_state
!= RFKILL_STATE_UNBLOCKED
)
698 for (i
= 0; i
< ARRAY_SIZE(rfkill_states
); i
++)
699 rfkill_states
[i
] = rfkill_default_state
;
701 error
= class_register(&rfkill_class
);
703 printk(KERN_ERR
"rfkill: unable to register rfkill class\n");
707 register_rfkill_notifier(&rfkill_blocking_uevent_nb
);
712 static void __exit
rfkill_exit(void)
714 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb
);
715 class_unregister(&rfkill_class
);
718 subsys_initcall(rfkill_init
);
719 module_exit(rfkill_exit
);