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 static void notify_rfkill_state_change(struct rfkill *rfkill)
110 blocking_notifier_call_chain(&rfkill_notifier_list,
111 RFKILL_STATE_CHANGED,
115 static void update_rfkill_state(struct rfkill *rfkill)
117 enum rfkill_state newstate, oldstate;
119 if (rfkill->get_state) {
120 mutex_lock(&rfkill->mutex);
121 if (!rfkill->get_state(rfkill->data, &newstate)) {
122 oldstate = rfkill->state;
123 rfkill->state = newstate;
124 if (oldstate != newstate)
125 notify_rfkill_state_change(rfkill);
127 mutex_unlock(&rfkill->mutex);
132 * rfkill_toggle_radio - wrapper for toggle_radio hook
134 * @rfkill: the rfkill struct to use
135 * @force: calls toggle_radio even if cache says it is not needed,
136 * and also makes sure notifications of the state will be
137 * sent even if it didn't change
138 * @state: the new state to call toggle_radio() with
140 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
141 * calls and handling all the red tape such as issuing notifications
142 * if the call is successful.
144 * Note that @force cannot override a (possibly cached) state of
145 * RFKILL_STATE_HARD_BLOCKED. Any device making use of
146 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
147 * rfkill_force_state(), so the cache either is bypassed or valid.
149 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
150 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
151 * give the driver a hint that it should double-BLOCK the transmitter.
153 * Caller must have aquired rfkill_mutex.
155 static int rfkill_toggle_radio(struct rfkill *rfkill,
156 enum rfkill_state state,
160 enum rfkill_state oldstate, newstate;
162 oldstate = rfkill->state;
164 if (rfkill->get_state && !force &&
165 !rfkill->get_state(rfkill->data, &newstate))
166 rfkill->state = newstate;
169 case RFKILL_STATE_HARD_BLOCKED:
170 /* typically happens when refreshing hardware state,
171 * such as on resume */
172 state = RFKILL_STATE_SOFT_BLOCKED;
174 case RFKILL_STATE_UNBLOCKED:
175 /* force can't override this, only rfkill_force_state() can */
176 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
179 case RFKILL_STATE_SOFT_BLOCKED:
180 /* nothing to do, we want to give drivers the hint to double
181 * BLOCK even a transmitter that is already in state
182 * RFKILL_STATE_HARD_BLOCKED */
186 if (force || state != rfkill->state) {
187 retval = rfkill->toggle_radio(rfkill->data, state);
188 /* never allow a HARD->SOFT downgrade! */
189 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
190 rfkill->state = state;
193 if (force || rfkill->state != oldstate) {
194 rfkill_led_trigger(rfkill, rfkill->state);
195 notify_rfkill_state_change(rfkill);
202 * rfkill_switch_all - Toggle state of all switches of given type
203 * @type: type of interfaces to be affeceted
204 * @state: the new state
206 * This function toggles state of all switches of given type unless
207 * a specific switch is claimed by userspace in which case it is
210 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
212 struct rfkill *rfkill;
214 mutex_lock(&rfkill_mutex);
216 rfkill_states[type] = state;
218 list_for_each_entry(rfkill, &rfkill_list, node) {
219 if ((!rfkill->user_claim) && (rfkill->type == type))
220 rfkill_toggle_radio(rfkill, state, 0);
223 mutex_unlock(&rfkill_mutex);
225 EXPORT_SYMBOL(rfkill_switch_all);
228 * rfkill_epo - emergency power off all transmitters
230 * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
231 * everything in its path but rfkill_mutex.
233 void rfkill_epo(void)
235 struct rfkill *rfkill;
237 mutex_lock(&rfkill_mutex);
238 list_for_each_entry(rfkill, &rfkill_list, node) {
239 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
241 mutex_unlock(&rfkill_mutex);
243 EXPORT_SYMBOL_GPL(rfkill_epo);
246 * rfkill_force_state - Force the internal rfkill radio state
247 * @rfkill: pointer to the rfkill class to modify.
248 * @state: the current radio state the class should be forced to.
250 * This function updates the internal state of the radio cached
251 * by the rfkill class. It should be used when the driver gets
252 * a notification by the firmware/hardware of the current *real*
253 * state of the radio rfkill switch.
255 * Devices which are subject to external changes on their rfkill
256 * state (such as those caused by a hardware rfkill line) MUST
257 * have their driver arrange to call rfkill_force_state() as soon
258 * as possible after such a change.
260 * This function may not be called from an atomic context.
262 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
264 enum rfkill_state oldstate;
266 if (state != RFKILL_STATE_SOFT_BLOCKED &&
267 state != RFKILL_STATE_UNBLOCKED &&
268 state != RFKILL_STATE_HARD_BLOCKED)
271 mutex_lock(&rfkill->mutex);
273 oldstate = rfkill->state;
274 rfkill->state = state;
276 if (state != oldstate)
277 notify_rfkill_state_change(rfkill);
279 mutex_unlock(&rfkill->mutex);
283 EXPORT_SYMBOL(rfkill_force_state);
285 static ssize_t rfkill_name_show(struct device *dev,
286 struct device_attribute *attr,
289 struct rfkill *rfkill = to_rfkill(dev);
291 return sprintf(buf, "%s\n", rfkill->name);
294 static const char *rfkill_get_type_str(enum rfkill_type type)
297 case RFKILL_TYPE_WLAN:
299 case RFKILL_TYPE_BLUETOOTH:
301 case RFKILL_TYPE_UWB:
302 return "ultrawideband";
303 case RFKILL_TYPE_WIMAX:
305 case RFKILL_TYPE_WWAN:
312 static ssize_t rfkill_type_show(struct device *dev,
313 struct device_attribute *attr,
316 struct rfkill *rfkill = to_rfkill(dev);
318 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
321 static ssize_t rfkill_state_show(struct device *dev,
322 struct device_attribute *attr,
325 struct rfkill *rfkill = to_rfkill(dev);
327 update_rfkill_state(rfkill);
328 return sprintf(buf, "%d\n", rfkill->state);
331 static ssize_t rfkill_state_store(struct device *dev,
332 struct device_attribute *attr,
333 const char *buf, size_t count)
335 struct rfkill *rfkill = to_rfkill(dev);
336 unsigned int state = simple_strtoul(buf, NULL, 0);
339 if (!capable(CAP_NET_ADMIN))
342 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
343 if (state != RFKILL_STATE_UNBLOCKED &&
344 state != RFKILL_STATE_SOFT_BLOCKED)
347 if (mutex_lock_interruptible(&rfkill->mutex))
349 error = rfkill_toggle_radio(rfkill, state, 0);
350 mutex_unlock(&rfkill->mutex);
352 return error ? error : count;
355 static ssize_t rfkill_claim_show(struct device *dev,
356 struct device_attribute *attr,
359 struct rfkill *rfkill = to_rfkill(dev);
361 return sprintf(buf, "%d", rfkill->user_claim);
364 static ssize_t rfkill_claim_store(struct device *dev,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
368 struct rfkill *rfkill = to_rfkill(dev);
369 bool claim = !!simple_strtoul(buf, NULL, 0);
372 if (!capable(CAP_NET_ADMIN))
376 * Take the global lock to make sure the kernel is not in
377 * the middle of rfkill_switch_all
379 error = mutex_lock_interruptible(&rfkill_mutex);
383 if (rfkill->user_claim_unsupported) {
387 if (rfkill->user_claim != claim) {
389 rfkill_toggle_radio(rfkill,
390 rfkill_states[rfkill->type],
392 rfkill->user_claim = claim;
396 mutex_unlock(&rfkill_mutex);
398 return error ? error : count;
401 static struct device_attribute rfkill_dev_attrs[] = {
402 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
403 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
404 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
405 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
409 static void rfkill_release(struct device *dev)
411 struct rfkill *rfkill = to_rfkill(dev);
414 module_put(THIS_MODULE);
418 static int rfkill_suspend(struct device *dev, pm_message_t state)
420 struct rfkill *rfkill = to_rfkill(dev);
422 if (dev->power.power_state.event != state.event) {
423 if (state.event & PM_EVENT_SLEEP) {
424 /* Stop transmitter, keep state, no notifies */
425 update_rfkill_state(rfkill);
427 mutex_lock(&rfkill->mutex);
428 rfkill->toggle_radio(rfkill->data,
429 RFKILL_STATE_SOFT_BLOCKED);
430 mutex_unlock(&rfkill->mutex);
433 dev->power.power_state = state;
439 static int rfkill_resume(struct device *dev)
441 struct rfkill *rfkill = to_rfkill(dev);
443 if (dev->power.power_state.event != PM_EVENT_ON) {
444 mutex_lock(&rfkill->mutex);
446 /* restore radio state AND notify everybody */
447 rfkill_toggle_radio(rfkill, rfkill->state, 1);
449 mutex_unlock(&rfkill->mutex);
452 dev->power.power_state = PMSG_ON;
456 #define rfkill_suspend NULL
457 #define rfkill_resume NULL
460 static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
461 unsigned long eventid,
464 struct rfkill *rfkill = (struct rfkill *)data;
467 case RFKILL_STATE_CHANGED:
468 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
477 static struct notifier_block rfkill_blocking_uevent_nb = {
478 .notifier_call = rfkill_blocking_uevent_notifier,
482 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
484 struct rfkill *rfkill = to_rfkill(dev);
487 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
490 error = add_uevent_var(env, "RFKILL_TYPE=%s",
491 rfkill_get_type_str(rfkill->type));
494 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
498 static struct class rfkill_class = {
500 .dev_release = rfkill_release,
501 .dev_attrs = rfkill_dev_attrs,
502 .suspend = rfkill_suspend,
503 .resume = rfkill_resume,
504 .dev_uevent = rfkill_dev_uevent,
507 static int rfkill_add_switch(struct rfkill *rfkill)
509 mutex_lock(&rfkill_mutex);
511 rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
513 list_add_tail(&rfkill->node, &rfkill_list);
515 mutex_unlock(&rfkill_mutex);
520 static void rfkill_remove_switch(struct rfkill *rfkill)
522 mutex_lock(&rfkill_mutex);
523 list_del_init(&rfkill->node);
524 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
525 mutex_unlock(&rfkill_mutex);
529 * rfkill_allocate - allocate memory for rfkill structure.
530 * @parent: device that has rf switch on it
531 * @type: type of the switch (RFKILL_TYPE_*)
533 * This function should be called by the network driver when it needs
534 * rfkill structure. Once the structure is allocated the driver shoud
535 * finish its initialization by setting name, private data, enable_radio
536 * and disable_radio methods and then register it with rfkill_register().
537 * NOTE: If registration fails the structure shoudl be freed by calling
538 * rfkill_free() otherwise rfkill_unregister() should be used.
540 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
542 struct rfkill *rfkill;
545 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
549 mutex_init(&rfkill->mutex);
550 INIT_LIST_HEAD(&rfkill->node);
554 dev->class = &rfkill_class;
555 dev->parent = parent;
556 device_initialize(dev);
558 __module_get(THIS_MODULE);
562 EXPORT_SYMBOL(rfkill_allocate);
565 * rfkill_free - Mark rfkill structure for deletion
566 * @rfkill: rfkill structure to be destroyed
568 * Decrements reference count of rfkill structure so it is destroyed.
569 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
571 void rfkill_free(struct rfkill *rfkill)
574 put_device(&rfkill->dev);
576 EXPORT_SYMBOL(rfkill_free);
578 static void rfkill_led_trigger_register(struct rfkill *rfkill)
580 #ifdef CONFIG_RFKILL_LEDS
583 rfkill->led_trigger.name = rfkill->dev.bus_id;
584 error = led_trigger_register(&rfkill->led_trigger);
586 rfkill->led_trigger.name = NULL;
587 #endif /* CONFIG_RFKILL_LEDS */
590 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
592 #ifdef CONFIG_RFKILL_LEDS
593 if (rfkill->led_trigger.name)
594 led_trigger_unregister(&rfkill->led_trigger);
599 * rfkill_register - Register a rfkill structure.
600 * @rfkill: rfkill structure to be registered
602 * This function should be called by the network driver when the rfkill
603 * structure needs to be registered. Immediately from registration the
604 * switch driver should be able to service calls to toggle_radio.
606 int rfkill_register(struct rfkill *rfkill)
608 static atomic_t rfkill_no = ATOMIC_INIT(0);
609 struct device *dev = &rfkill->dev;
612 if (!rfkill->toggle_radio)
614 if (rfkill->type >= RFKILL_TYPE_MAX)
617 snprintf(dev->bus_id, sizeof(dev->bus_id),
618 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
620 rfkill_led_trigger_register(rfkill);
622 error = rfkill_add_switch(rfkill);
624 rfkill_led_trigger_unregister(rfkill);
628 error = device_add(dev);
630 rfkill_led_trigger_unregister(rfkill);
631 rfkill_remove_switch(rfkill);
637 EXPORT_SYMBOL(rfkill_register);
640 * rfkill_unregister - Unregister a rfkill structure.
641 * @rfkill: rfkill structure to be unregistered
643 * This function should be called by the network driver during device
644 * teardown to destroy rfkill structure. Note that rfkill_free() should
645 * _not_ be called after rfkill_unregister().
647 void rfkill_unregister(struct rfkill *rfkill)
649 device_del(&rfkill->dev);
650 rfkill_remove_switch(rfkill);
651 rfkill_led_trigger_unregister(rfkill);
652 put_device(&rfkill->dev);
654 EXPORT_SYMBOL(rfkill_unregister);
657 * Rfkill module initialization/deinitialization.
659 static int __init rfkill_init(void)
664 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
665 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
666 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
669 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
670 rfkill_states[i] = rfkill_default_state;
672 error = class_register(&rfkill_class);
674 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
678 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
683 static void __exit rfkill_exit(void)
685 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
686 class_unregister(&rfkill_class);
689 subsys_initcall(rfkill_init);
690 module_exit(rfkill_exit);