rfkill: add WARN and BUG_ON paranoia (v2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / rfkill / rfkill.c
blob5422421bb6f5c5752fcb2b2f0da028beef63c11d
1 /*
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 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)];
55 static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
58 /**
59 * register_rfkill_notifier - Add notifier to rfkill notifier chain
60 * @nb: pointer to the new entry to add to the chain
62 * See blocking_notifier_chain_register() for return value and further
63 * observations.
65 * Adds a notifier to the rfkill notifier chain. The chain will be
66 * called with a pointer to the relevant rfkill structure as a parameter,
67 * refer to include/linux/rfkill.h for the possible events.
69 * Notifiers added to this chain are to always return NOTIFY_DONE. This
70 * chain is a blocking notifier chain: notifiers can sleep.
72 * Calls to this chain may have been done through a workqueue. One must
73 * assume unordered asynchronous behaviour, there is no way to know if
74 * actions related to the event that generated the notification have been
75 * carried out already.
77 int register_rfkill_notifier(struct notifier_block *nb)
79 BUG_ON(!nb);
80 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
82 EXPORT_SYMBOL_GPL(register_rfkill_notifier);
84 /**
85 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
86 * @nb: pointer to the entry to remove from the chain
88 * See blocking_notifier_chain_unregister() for return value and further
89 * observations.
91 * Removes a notifier from the rfkill notifier chain.
93 int unregister_rfkill_notifier(struct notifier_block *nb)
95 BUG_ON(!nb);
96 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
98 EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
101 static void rfkill_led_trigger(struct rfkill *rfkill,
102 enum rfkill_state state)
104 #ifdef CONFIG_RFKILL_LEDS
105 struct led_trigger *led = &rfkill->led_trigger;
107 if (!led->name)
108 return;
109 if (state != RFKILL_STATE_UNBLOCKED)
110 led_trigger_event(led, LED_OFF);
111 else
112 led_trigger_event(led, LED_FULL);
113 #endif /* CONFIG_RFKILL_LEDS */
116 #ifdef CONFIG_RFKILL_LEDS
117 static void rfkill_led_trigger_activate(struct led_classdev *led)
119 struct rfkill *rfkill = container_of(led->trigger,
120 struct rfkill, led_trigger);
122 rfkill_led_trigger(rfkill, rfkill->state);
124 #endif /* CONFIG_RFKILL_LEDS */
126 static void notify_rfkill_state_change(struct rfkill *rfkill)
128 rfkill_led_trigger(rfkill, rfkill->state);
129 blocking_notifier_call_chain(&rfkill_notifier_list,
130 RFKILL_STATE_CHANGED,
131 rfkill);
134 static void update_rfkill_state(struct rfkill *rfkill)
136 enum rfkill_state newstate, oldstate;
138 if (rfkill->get_state) {
139 mutex_lock(&rfkill->mutex);
140 if (!rfkill->get_state(rfkill->data, &newstate)) {
141 oldstate = rfkill->state;
142 rfkill->state = newstate;
143 if (oldstate != newstate)
144 notify_rfkill_state_change(rfkill);
146 mutex_unlock(&rfkill->mutex);
151 * rfkill_toggle_radio - wrapper for toggle_radio hook
152 * @rfkill: the rfkill struct to use
153 * @force: calls toggle_radio even if cache says it is not needed,
154 * and also makes sure notifications of the state will be
155 * sent even if it didn't change
156 * @state: the new state to call toggle_radio() with
158 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
159 * calls and handling all the red tape such as issuing notifications
160 * if the call is successful.
162 * Suspended devices are not touched at all, and -EAGAIN is returned.
164 * Note that the @force parameter cannot override a (possibly cached)
165 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
166 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
167 * rfkill_force_state(), so the cache either is bypassed or valid.
169 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
170 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
171 * give the driver a hint that it should double-BLOCK the transmitter.
173 * Caller must have acquired rfkill->mutex.
175 static int rfkill_toggle_radio(struct rfkill *rfkill,
176 enum rfkill_state state,
177 int force)
179 int retval = 0;
180 enum rfkill_state oldstate, newstate;
182 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
183 return -EBUSY;
185 oldstate = rfkill->state;
187 if (rfkill->get_state && !force &&
188 !rfkill->get_state(rfkill->data, &newstate))
189 rfkill->state = newstate;
191 switch (state) {
192 case RFKILL_STATE_HARD_BLOCKED:
193 /* typically happens when refreshing hardware state,
194 * such as on resume */
195 state = RFKILL_STATE_SOFT_BLOCKED;
196 break;
197 case RFKILL_STATE_UNBLOCKED:
198 /* force can't override this, only rfkill_force_state() can */
199 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
200 return -EPERM;
201 break;
202 case RFKILL_STATE_SOFT_BLOCKED:
203 /* nothing to do, we want to give drivers the hint to double
204 * BLOCK even a transmitter that is already in state
205 * RFKILL_STATE_HARD_BLOCKED */
206 break;
207 default:
208 WARN(1, KERN_WARNING
209 "rfkill: illegal state %d passed as parameter "
210 "to rfkill_toggle_radio\n", state);
211 return -EINVAL;
214 if (force || state != rfkill->state) {
215 retval = rfkill->toggle_radio(rfkill->data, state);
216 /* never allow a HARD->SOFT downgrade! */
217 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
218 rfkill->state = state;
221 if (force || rfkill->state != oldstate)
222 notify_rfkill_state_change(rfkill);
224 return retval;
228 * __rfkill_switch_all - Toggle state of all switches of given type
229 * @type: type of interfaces to be affected
230 * @state: the new state
232 * This function toggles the state of all switches of given type,
233 * unless a specific switch is claimed by userspace (in which case,
234 * that switch is left alone) or suspended.
236 * Caller must have acquired rfkill_mutex.
238 static void __rfkill_switch_all(const enum rfkill_type type,
239 const enum rfkill_state state)
241 struct rfkill *rfkill;
243 if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
244 KERN_WARNING
245 "rfkill: illegal state %d or type %d "
246 "passed as parameter to __rfkill_switch_all\n",
247 state, type))
248 return;
250 rfkill_global_states[type].current_state = state;
251 list_for_each_entry(rfkill, &rfkill_list, node) {
252 if ((!rfkill->user_claim) && (rfkill->type == type)) {
253 mutex_lock(&rfkill->mutex);
254 rfkill_toggle_radio(rfkill, state, 0);
255 mutex_unlock(&rfkill->mutex);
261 * rfkill_switch_all - Toggle state of all switches of given type
262 * @type: type of interfaces to be affected
263 * @state: the new state
265 * Acquires rfkill_mutex and calls __rfkill_switch_all(@type, @state).
266 * Please refer to __rfkill_switch_all() for details.
268 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
270 mutex_lock(&rfkill_mutex);
271 __rfkill_switch_all(type, state);
272 mutex_unlock(&rfkill_mutex);
274 EXPORT_SYMBOL(rfkill_switch_all);
277 * rfkill_epo - emergency power off all transmitters
279 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
280 * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
282 * The global state before the EPO is saved and can be restored later
283 * using rfkill_restore_states().
285 void rfkill_epo(void)
287 struct rfkill *rfkill;
288 int i;
290 mutex_lock(&rfkill_mutex);
291 list_for_each_entry(rfkill, &rfkill_list, node) {
292 mutex_lock(&rfkill->mutex);
293 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
294 mutex_unlock(&rfkill->mutex);
296 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
297 rfkill_global_states[i].default_state =
298 rfkill_global_states[i].current_state;
299 rfkill_global_states[i].current_state =
300 RFKILL_STATE_SOFT_BLOCKED;
302 mutex_unlock(&rfkill_mutex);
304 EXPORT_SYMBOL_GPL(rfkill_epo);
307 * rfkill_restore_states - restore global states
309 * Restore (and sync switches to) the global state from the
310 * states in rfkill_default_states. This can undo the effects of
311 * a call to rfkill_epo().
313 void rfkill_restore_states(void)
315 int i;
317 mutex_lock(&rfkill_mutex);
318 for (i = 0; i < RFKILL_TYPE_MAX; i++)
319 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
320 mutex_unlock(&rfkill_mutex);
322 EXPORT_SYMBOL_GPL(rfkill_restore_states);
325 * rfkill_force_state - Force the internal rfkill radio state
326 * @rfkill: pointer to the rfkill class to modify.
327 * @state: the current radio state the class should be forced to.
329 * This function updates the internal state of the radio cached
330 * by the rfkill class. It should be used when the driver gets
331 * a notification by the firmware/hardware of the current *real*
332 * state of the radio rfkill switch.
334 * Devices which are subject to external changes on their rfkill
335 * state (such as those caused by a hardware rfkill line) MUST
336 * have their driver arrange to call rfkill_force_state() as soon
337 * as possible after such a change.
339 * This function may not be called from an atomic context.
341 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
343 enum rfkill_state oldstate;
345 BUG_ON(!rfkill);
346 if (WARN((state >= RFKILL_STATE_MAX),
347 KERN_WARNING
348 "rfkill: illegal state %d passed as parameter "
349 "to rfkill_force_state\n", state))
350 return -EINVAL;
352 mutex_lock(&rfkill->mutex);
354 oldstate = rfkill->state;
355 rfkill->state = state;
357 if (state != oldstate)
358 notify_rfkill_state_change(rfkill);
360 mutex_unlock(&rfkill->mutex);
362 return 0;
364 EXPORT_SYMBOL(rfkill_force_state);
366 static ssize_t rfkill_name_show(struct device *dev,
367 struct device_attribute *attr,
368 char *buf)
370 struct rfkill *rfkill = to_rfkill(dev);
372 return sprintf(buf, "%s\n", rfkill->name);
375 static const char *rfkill_get_type_str(enum rfkill_type type)
377 switch (type) {
378 case RFKILL_TYPE_WLAN:
379 return "wlan";
380 case RFKILL_TYPE_BLUETOOTH:
381 return "bluetooth";
382 case RFKILL_TYPE_UWB:
383 return "ultrawideband";
384 case RFKILL_TYPE_WIMAX:
385 return "wimax";
386 case RFKILL_TYPE_WWAN:
387 return "wwan";
388 default:
389 BUG();
393 static ssize_t rfkill_type_show(struct device *dev,
394 struct device_attribute *attr,
395 char *buf)
397 struct rfkill *rfkill = to_rfkill(dev);
399 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
402 static ssize_t rfkill_state_show(struct device *dev,
403 struct device_attribute *attr,
404 char *buf)
406 struct rfkill *rfkill = to_rfkill(dev);
408 update_rfkill_state(rfkill);
409 return sprintf(buf, "%d\n", rfkill->state);
412 static ssize_t rfkill_state_store(struct device *dev,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
416 struct rfkill *rfkill = to_rfkill(dev);
417 unsigned long state;
418 int error;
420 if (!capable(CAP_NET_ADMIN))
421 return -EPERM;
423 error = strict_strtoul(buf, 0, &state);
424 if (error)
425 return error;
427 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
428 if (state != RFKILL_STATE_UNBLOCKED &&
429 state != RFKILL_STATE_SOFT_BLOCKED)
430 return -EINVAL;
432 if (mutex_lock_interruptible(&rfkill->mutex))
433 return -ERESTARTSYS;
434 error = rfkill_toggle_radio(rfkill, state, 0);
435 mutex_unlock(&rfkill->mutex);
437 return error ? error : count;
440 static ssize_t rfkill_claim_show(struct device *dev,
441 struct device_attribute *attr,
442 char *buf)
444 struct rfkill *rfkill = to_rfkill(dev);
446 return sprintf(buf, "%d\n", rfkill->user_claim);
449 static ssize_t rfkill_claim_store(struct device *dev,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
453 struct rfkill *rfkill = to_rfkill(dev);
454 unsigned long claim_tmp;
455 bool claim;
456 int error;
458 if (!capable(CAP_NET_ADMIN))
459 return -EPERM;
461 if (rfkill->user_claim_unsupported)
462 return -EOPNOTSUPP;
464 error = strict_strtoul(buf, 0, &claim_tmp);
465 if (error)
466 return error;
467 claim = !!claim_tmp;
470 * Take the global lock to make sure the kernel is not in
471 * the middle of rfkill_switch_all
473 error = mutex_lock_interruptible(&rfkill_mutex);
474 if (error)
475 return error;
477 if (rfkill->user_claim != claim) {
478 if (!claim) {
479 mutex_lock(&rfkill->mutex);
480 rfkill_toggle_radio(rfkill,
481 rfkill_global_states[rfkill->type].current_state,
483 mutex_unlock(&rfkill->mutex);
485 rfkill->user_claim = claim;
488 mutex_unlock(&rfkill_mutex);
490 return error ? error : count;
493 static struct device_attribute rfkill_dev_attrs[] = {
494 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
495 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
496 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
497 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
498 __ATTR_NULL
501 static void rfkill_release(struct device *dev)
503 struct rfkill *rfkill = to_rfkill(dev);
505 kfree(rfkill);
506 module_put(THIS_MODULE);
509 #ifdef CONFIG_PM
510 static int rfkill_suspend(struct device *dev, pm_message_t state)
512 struct rfkill *rfkill = to_rfkill(dev);
514 if (dev->power.power_state.event != state.event) {
515 if (state.event & PM_EVENT_SLEEP) {
516 /* Stop transmitter, keep state, no notifies */
517 update_rfkill_state(rfkill);
519 mutex_lock(&rfkill->mutex);
520 rfkill->toggle_radio(rfkill->data,
521 RFKILL_STATE_SOFT_BLOCKED);
522 mutex_unlock(&rfkill->mutex);
525 dev->power.power_state = state;
528 return 0;
531 static int rfkill_resume(struct device *dev)
533 struct rfkill *rfkill = to_rfkill(dev);
535 if (dev->power.power_state.event != PM_EVENT_ON) {
536 mutex_lock(&rfkill->mutex);
538 dev->power.power_state.event = PM_EVENT_ON;
540 /* restore radio state AND notify everybody */
541 rfkill_toggle_radio(rfkill, rfkill->state, 1);
543 mutex_unlock(&rfkill->mutex);
546 return 0;
548 #else
549 #define rfkill_suspend NULL
550 #define rfkill_resume NULL
551 #endif
553 static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
554 unsigned long eventid,
555 void *data)
557 struct rfkill *rfkill = (struct rfkill *)data;
559 switch (eventid) {
560 case RFKILL_STATE_CHANGED:
561 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
562 break;
563 default:
564 break;
567 return NOTIFY_DONE;
570 static struct notifier_block rfkill_blocking_uevent_nb = {
571 .notifier_call = rfkill_blocking_uevent_notifier,
572 .priority = 0,
575 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
577 struct rfkill *rfkill = to_rfkill(dev);
578 int error;
580 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
581 if (error)
582 return error;
583 error = add_uevent_var(env, "RFKILL_TYPE=%s",
584 rfkill_get_type_str(rfkill->type));
585 if (error)
586 return error;
587 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
588 return error;
591 static struct class rfkill_class = {
592 .name = "rfkill",
593 .dev_release = rfkill_release,
594 .dev_attrs = rfkill_dev_attrs,
595 .suspend = rfkill_suspend,
596 .resume = rfkill_resume,
597 .dev_uevent = rfkill_dev_uevent,
600 static int rfkill_check_duplicity(const struct rfkill *rfkill)
602 struct rfkill *p;
603 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
605 memset(seen, 0, sizeof(seen));
607 list_for_each_entry(p, &rfkill_list, node) {
608 if (WARN((p == rfkill), KERN_WARNING
609 "rfkill: illegal attempt to register "
610 "an already registered rfkill struct\n"))
611 return -EEXIST;
612 set_bit(p->type, seen);
615 /* 0: first switch of its kind */
616 return test_bit(rfkill->type, seen);
619 static int rfkill_add_switch(struct rfkill *rfkill)
621 int error;
623 mutex_lock(&rfkill_mutex);
625 error = rfkill_check_duplicity(rfkill);
626 if (error < 0)
627 goto unlock_out;
629 if (!error) {
630 /* lock default after first use */
631 set_bit(rfkill->type, rfkill_states_lockdflt);
632 rfkill_global_states[rfkill->type].current_state =
633 rfkill_global_states[rfkill->type].default_state;
636 rfkill_toggle_radio(rfkill,
637 rfkill_global_states[rfkill->type].current_state,
640 list_add_tail(&rfkill->node, &rfkill_list);
642 error = 0;
643 unlock_out:
644 mutex_unlock(&rfkill_mutex);
646 return error;
649 static void rfkill_remove_switch(struct rfkill *rfkill)
651 mutex_lock(&rfkill_mutex);
652 list_del_init(&rfkill->node);
653 mutex_unlock(&rfkill_mutex);
655 mutex_lock(&rfkill->mutex);
656 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
657 mutex_unlock(&rfkill->mutex);
661 * rfkill_allocate - allocate memory for rfkill structure.
662 * @parent: device that has rf switch on it
663 * @type: type of the switch (RFKILL_TYPE_*)
665 * This function should be called by the network driver when it needs
666 * rfkill structure. Once the structure is allocated the driver should
667 * finish its initialization by setting the name, private data, enable_radio
668 * and disable_radio methods and then register it with rfkill_register().
670 * NOTE: If registration fails the structure shoudl be freed by calling
671 * rfkill_free() otherwise rfkill_unregister() should be used.
673 struct rfkill * __must_check rfkill_allocate(struct device *parent,
674 enum rfkill_type type)
676 struct rfkill *rfkill;
677 struct device *dev;
679 if (WARN((type >= RFKILL_TYPE_MAX),
680 KERN_WARNING
681 "rfkill: illegal type %d passed as parameter "
682 "to rfkill_allocate\n", type))
683 return NULL;
685 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
686 if (!rfkill)
687 return NULL;
689 mutex_init(&rfkill->mutex);
690 INIT_LIST_HEAD(&rfkill->node);
691 rfkill->type = type;
693 dev = &rfkill->dev;
694 dev->class = &rfkill_class;
695 dev->parent = parent;
696 device_initialize(dev);
698 __module_get(THIS_MODULE);
700 return rfkill;
702 EXPORT_SYMBOL(rfkill_allocate);
705 * rfkill_free - Mark rfkill structure for deletion
706 * @rfkill: rfkill structure to be destroyed
708 * Decrements reference count of the rfkill structure so it is destroyed.
709 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
711 void rfkill_free(struct rfkill *rfkill)
713 if (rfkill)
714 put_device(&rfkill->dev);
716 EXPORT_SYMBOL(rfkill_free);
718 static void rfkill_led_trigger_register(struct rfkill *rfkill)
720 #ifdef CONFIG_RFKILL_LEDS
721 int error;
723 if (!rfkill->led_trigger.name)
724 rfkill->led_trigger.name = rfkill->dev.bus_id;
725 if (!rfkill->led_trigger.activate)
726 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
727 error = led_trigger_register(&rfkill->led_trigger);
728 if (error)
729 rfkill->led_trigger.name = NULL;
730 #endif /* CONFIG_RFKILL_LEDS */
733 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
735 #ifdef CONFIG_RFKILL_LEDS
736 if (rfkill->led_trigger.name) {
737 led_trigger_unregister(&rfkill->led_trigger);
738 rfkill->led_trigger.name = NULL;
740 #endif
744 * rfkill_register - Register a rfkill structure.
745 * @rfkill: rfkill structure to be registered
747 * This function should be called by the network driver when the rfkill
748 * structure needs to be registered. Immediately from registration the
749 * switch driver should be able to service calls to toggle_radio.
751 int __must_check rfkill_register(struct rfkill *rfkill)
753 static atomic_t rfkill_no = ATOMIC_INIT(0);
754 struct device *dev = &rfkill->dev;
755 int error;
757 if (WARN((!rfkill || !rfkill->toggle_radio ||
758 rfkill->type >= RFKILL_TYPE_MAX ||
759 rfkill->state >= RFKILL_STATE_MAX),
760 KERN_WARNING
761 "rfkill: attempt to register a "
762 "badly initialized rfkill struct\n"))
763 return -EINVAL;
765 snprintf(dev->bus_id, sizeof(dev->bus_id),
766 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
768 rfkill_led_trigger_register(rfkill);
770 error = rfkill_add_switch(rfkill);
771 if (error) {
772 rfkill_led_trigger_unregister(rfkill);
773 return error;
776 error = device_add(dev);
777 if (error) {
778 rfkill_remove_switch(rfkill);
779 rfkill_led_trigger_unregister(rfkill);
780 return error;
783 return 0;
785 EXPORT_SYMBOL(rfkill_register);
788 * rfkill_unregister - Unregister a rfkill structure.
789 * @rfkill: rfkill structure to be unregistered
791 * This function should be called by the network driver during device
792 * teardown to destroy rfkill structure. Note that rfkill_free() should
793 * _not_ be called after rfkill_unregister().
795 void rfkill_unregister(struct rfkill *rfkill)
797 BUG_ON(!rfkill);
798 device_del(&rfkill->dev);
799 rfkill_remove_switch(rfkill);
800 rfkill_led_trigger_unregister(rfkill);
801 put_device(&rfkill->dev);
803 EXPORT_SYMBOL(rfkill_unregister);
806 * rfkill_set_default - set initial value for a switch type
807 * @type - the type of switch to set the default state of
808 * @state - the new default state for that group of switches
810 * Sets the initial state rfkill should use for a given type.
811 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
812 * and RFKILL_STATE_UNBLOCKED.
814 * This function is meant to be used by platform drivers for platforms
815 * that can save switch state across power down/reboot.
817 * The default state for each switch type can be changed exactly once.
818 * After a switch of that type is registered, the default state cannot
819 * be changed anymore. This guards against multiple drivers it the
820 * same platform trying to set the initial switch default state, which
821 * is not allowed.
823 * Returns -EPERM if the state has already been set once or is in use,
824 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
825 * if this function returns -EPERM.
827 * Returns 0 if the new default state was set, or an error if it
828 * could not be set.
830 int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
832 int error;
834 if (WARN((type >= RFKILL_TYPE_MAX ||
835 (state != RFKILL_STATE_SOFT_BLOCKED &&
836 state != RFKILL_STATE_UNBLOCKED)),
837 KERN_WARNING
838 "rfkill: illegal state %d or type %d passed as "
839 "parameter to rfkill_set_default\n", state, type))
840 return -EINVAL;
842 mutex_lock(&rfkill_mutex);
844 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
845 rfkill_global_states[type].default_state = state;
846 error = 0;
847 } else
848 error = -EPERM;
850 mutex_unlock(&rfkill_mutex);
851 return error;
853 EXPORT_SYMBOL_GPL(rfkill_set_default);
856 * Rfkill module initialization/deinitialization.
858 static int __init rfkill_init(void)
860 int error;
861 int i;
863 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
864 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
865 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
866 return -EINVAL;
868 for (i = 0; i < RFKILL_TYPE_MAX; i++)
869 rfkill_global_states[i].default_state = rfkill_default_state;
871 error = class_register(&rfkill_class);
872 if (error) {
873 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
874 return error;
877 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
879 return 0;
882 static void __exit rfkill_exit(void)
884 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
885 class_unregister(&rfkill_class);
888 subsys_initcall(rfkill_init);
889 module_exit(rfkill_exit);