added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / net / rfkill / rfkill.c
blob6b26af1d165575e636187db093a691d2fbfb92ae
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_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;
57 #ifdef CONFIG_RFKILL_LEDS
58 static void rfkill_led_trigger(struct rfkill *rfkill,
59 enum rfkill_state state)
61 struct led_trigger *led = &rfkill->led_trigger;
63 if (!led->name)
64 return;
65 if (state != RFKILL_STATE_UNBLOCKED)
66 led_trigger_event(led, LED_OFF);
67 else
68 led_trigger_event(led, LED_FULL);
71 static void rfkill_led_trigger_activate(struct led_classdev *led)
73 struct rfkill *rfkill = container_of(led->trigger,
74 struct rfkill, led_trigger);
76 rfkill_led_trigger(rfkill, rfkill->state);
78 #endif /* CONFIG_RFKILL_LEDS */
80 static void rfkill_uevent(struct rfkill *rfkill)
82 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
85 static void update_rfkill_state(struct rfkill *rfkill)
87 enum rfkill_state newstate, oldstate;
89 if (rfkill->get_state) {
90 mutex_lock(&rfkill->mutex);
91 if (!rfkill->get_state(rfkill->data, &newstate)) {
92 oldstate = rfkill->state;
93 rfkill->state = newstate;
94 if (oldstate != newstate)
95 rfkill_uevent(rfkill);
97 mutex_unlock(&rfkill->mutex);
102 * rfkill_toggle_radio - wrapper for toggle_radio hook
103 * @rfkill: the rfkill struct to use
104 * @force: calls toggle_radio even if cache says it is not needed,
105 * and also makes sure notifications of the state will be
106 * sent even if it didn't change
107 * @state: the new state to call toggle_radio() with
109 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
110 * calls and handling all the red tape such as issuing notifications
111 * if the call is successful.
113 * Suspended devices are not touched at all, and -EAGAIN is returned.
115 * Note that the @force parameter cannot override a (possibly cached)
116 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
117 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
118 * rfkill_force_state(), so the cache either is bypassed or valid.
120 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
121 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
122 * give the driver a hint that it should double-BLOCK the transmitter.
124 * Caller must have acquired rfkill->mutex.
126 static int rfkill_toggle_radio(struct rfkill *rfkill,
127 enum rfkill_state state,
128 int force)
130 int retval = 0;
131 enum rfkill_state oldstate, newstate;
133 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
134 return -EBUSY;
136 oldstate = rfkill->state;
138 if (rfkill->get_state && !force &&
139 !rfkill->get_state(rfkill->data, &newstate))
140 rfkill->state = newstate;
142 switch (state) {
143 case RFKILL_STATE_HARD_BLOCKED:
144 /* typically happens when refreshing hardware state,
145 * such as on resume */
146 state = RFKILL_STATE_SOFT_BLOCKED;
147 break;
148 case RFKILL_STATE_UNBLOCKED:
149 /* force can't override this, only rfkill_force_state() can */
150 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
151 return -EPERM;
152 break;
153 case RFKILL_STATE_SOFT_BLOCKED:
154 /* nothing to do, we want to give drivers the hint to double
155 * BLOCK even a transmitter that is already in state
156 * RFKILL_STATE_HARD_BLOCKED */
157 break;
158 default:
159 WARN(1, KERN_WARNING
160 "rfkill: illegal state %d passed as parameter "
161 "to rfkill_toggle_radio\n", state);
162 return -EINVAL;
165 if (force || state != rfkill->state) {
166 retval = rfkill->toggle_radio(rfkill->data, state);
167 /* never allow a HARD->SOFT downgrade! */
168 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
169 rfkill->state = state;
172 if (force || rfkill->state != oldstate)
173 rfkill_uevent(rfkill);
175 return retval;
179 * __rfkill_switch_all - Toggle state of all switches of given type
180 * @type: type of interfaces to be affected
181 * @state: the new state
183 * This function toggles the state of all switches of given type,
184 * unless a specific switch is claimed by userspace (in which case,
185 * that switch is left alone) or suspended.
187 * Caller must have acquired rfkill_global_mutex.
189 static void __rfkill_switch_all(const enum rfkill_type type,
190 const enum rfkill_state state)
192 struct rfkill *rfkill;
194 if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
195 KERN_WARNING
196 "rfkill: illegal state %d or type %d "
197 "passed as parameter to __rfkill_switch_all\n",
198 state, type))
199 return;
201 rfkill_global_states[type].current_state = state;
202 list_for_each_entry(rfkill, &rfkill_list, node) {
203 if ((!rfkill->user_claim) && (rfkill->type == type)) {
204 mutex_lock(&rfkill->mutex);
205 rfkill_toggle_radio(rfkill, state, 0);
206 mutex_unlock(&rfkill->mutex);
212 * rfkill_switch_all - Toggle state of all switches of given type
213 * @type: type of interfaces to be affected
214 * @state: the new state
216 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
217 * Please refer to __rfkill_switch_all() for details.
219 * Does nothing if the EPO lock is active.
221 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
223 mutex_lock(&rfkill_global_mutex);
224 if (!rfkill_epo_lock_active)
225 __rfkill_switch_all(type, state);
226 mutex_unlock(&rfkill_global_mutex);
228 EXPORT_SYMBOL(rfkill_switch_all);
231 * rfkill_epo - emergency power off all transmitters
233 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
234 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
236 * The global state before the EPO is saved and can be restored later
237 * using rfkill_restore_states().
239 void rfkill_epo(void)
241 struct rfkill *rfkill;
242 int i;
244 mutex_lock(&rfkill_global_mutex);
246 rfkill_epo_lock_active = true;
247 list_for_each_entry(rfkill, &rfkill_list, node) {
248 mutex_lock(&rfkill->mutex);
249 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
250 mutex_unlock(&rfkill->mutex);
252 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
253 rfkill_global_states[i].default_state =
254 rfkill_global_states[i].current_state;
255 rfkill_global_states[i].current_state =
256 RFKILL_STATE_SOFT_BLOCKED;
258 mutex_unlock(&rfkill_global_mutex);
260 EXPORT_SYMBOL_GPL(rfkill_epo);
263 * rfkill_restore_states - restore global states
265 * Restore (and sync switches to) the global state from the
266 * states in rfkill_default_states. This can undo the effects of
267 * a call to rfkill_epo().
269 void rfkill_restore_states(void)
271 int i;
273 mutex_lock(&rfkill_global_mutex);
275 rfkill_epo_lock_active = false;
276 for (i = 0; i < RFKILL_TYPE_MAX; i++)
277 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
278 mutex_unlock(&rfkill_global_mutex);
280 EXPORT_SYMBOL_GPL(rfkill_restore_states);
283 * rfkill_remove_epo_lock - unlock state changes
285 * Used by rfkill-input manually unlock state changes, when
286 * the EPO switch is deactivated.
288 void rfkill_remove_epo_lock(void)
290 mutex_lock(&rfkill_global_mutex);
291 rfkill_epo_lock_active = false;
292 mutex_unlock(&rfkill_global_mutex);
294 EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock);
297 * rfkill_is_epo_lock_active - returns true EPO is active
299 * Returns 0 (false) if there is NOT an active EPO contidion,
300 * and 1 (true) if there is an active EPO contition, which
301 * locks all radios in one of the BLOCKED states.
303 * Can be called in atomic context.
305 bool rfkill_is_epo_lock_active(void)
307 return rfkill_epo_lock_active;
309 EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active);
312 * rfkill_get_global_state - returns global state for a type
313 * @type: the type to get the global state of
315 * Returns the current global state for a given wireless
316 * device type.
318 enum rfkill_state rfkill_get_global_state(const enum rfkill_type type)
320 return rfkill_global_states[type].current_state;
322 EXPORT_SYMBOL_GPL(rfkill_get_global_state);
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 rfkill_uevent(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();
390 return NULL;
394 static ssize_t rfkill_type_show(struct device *dev,
395 struct device_attribute *attr,
396 char *buf)
398 struct rfkill *rfkill = to_rfkill(dev);
400 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
403 static ssize_t rfkill_state_show(struct device *dev,
404 struct device_attribute *attr,
405 char *buf)
407 struct rfkill *rfkill = to_rfkill(dev);
409 update_rfkill_state(rfkill);
410 return sprintf(buf, "%d\n", rfkill->state);
413 static ssize_t rfkill_state_store(struct device *dev,
414 struct device_attribute *attr,
415 const char *buf, size_t count)
417 struct rfkill *rfkill = to_rfkill(dev);
418 unsigned long state;
419 int error;
421 if (!capable(CAP_NET_ADMIN))
422 return -EPERM;
424 error = strict_strtoul(buf, 0, &state);
425 if (error)
426 return error;
428 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
429 if (state != RFKILL_STATE_UNBLOCKED &&
430 state != RFKILL_STATE_SOFT_BLOCKED)
431 return -EINVAL;
433 error = mutex_lock_killable(&rfkill->mutex);
434 if (error)
435 return error;
437 if (!rfkill_epo_lock_active)
438 error = rfkill_toggle_radio(rfkill, state, 0);
439 else
440 error = -EPERM;
442 mutex_unlock(&rfkill->mutex);
444 return error ? error : count;
447 static ssize_t rfkill_claim_show(struct device *dev,
448 struct device_attribute *attr,
449 char *buf)
451 struct rfkill *rfkill = to_rfkill(dev);
453 return sprintf(buf, "%d\n", rfkill->user_claim);
456 static ssize_t rfkill_claim_store(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf, size_t count)
460 struct rfkill *rfkill = to_rfkill(dev);
461 unsigned long claim_tmp;
462 bool claim;
463 int error;
465 if (!capable(CAP_NET_ADMIN))
466 return -EPERM;
468 if (rfkill->user_claim_unsupported)
469 return -EOPNOTSUPP;
471 error = strict_strtoul(buf, 0, &claim_tmp);
472 if (error)
473 return error;
474 claim = !!claim_tmp;
477 * Take the global lock to make sure the kernel is not in
478 * the middle of rfkill_switch_all
480 error = mutex_lock_killable(&rfkill_global_mutex);
481 if (error)
482 return error;
484 if (rfkill->user_claim != claim) {
485 if (!claim && !rfkill_epo_lock_active) {
486 mutex_lock(&rfkill->mutex);
487 rfkill_toggle_radio(rfkill,
488 rfkill_global_states[rfkill->type].current_state,
490 mutex_unlock(&rfkill->mutex);
492 rfkill->user_claim = claim;
495 mutex_unlock(&rfkill_global_mutex);
497 return error ? error : count;
500 static struct device_attribute rfkill_dev_attrs[] = {
501 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
502 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
503 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
504 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
505 __ATTR_NULL
508 static void rfkill_release(struct device *dev)
510 struct rfkill *rfkill = to_rfkill(dev);
512 kfree(rfkill);
513 module_put(THIS_MODULE);
516 #ifdef CONFIG_PM
517 static int rfkill_suspend(struct device *dev, pm_message_t state)
519 struct rfkill *rfkill = to_rfkill(dev);
521 /* mark class device as suspended */
522 if (dev->power.power_state.event != state.event)
523 dev->power.power_state = state;
525 /* store state for the resume handler */
526 rfkill->state_for_resume = rfkill->state;
528 return 0;
531 static int rfkill_resume(struct device *dev)
533 struct rfkill *rfkill = to_rfkill(dev);
534 enum rfkill_state newstate;
536 if (dev->power.power_state.event != PM_EVENT_ON) {
537 mutex_lock(&rfkill->mutex);
539 dev->power.power_state.event = PM_EVENT_ON;
542 * rfkill->state could have been modified before we got
543 * called, and won't be updated by rfkill_toggle_radio()
544 * in force mode. Sync it FIRST.
546 if (rfkill->get_state &&
547 !rfkill->get_state(rfkill->data, &newstate))
548 rfkill->state = newstate;
551 * If we are under EPO, kick transmitter offline,
552 * otherwise restore to pre-suspend state.
554 * Issue a notification in any case
556 rfkill_toggle_radio(rfkill,
557 rfkill_epo_lock_active ?
558 RFKILL_STATE_SOFT_BLOCKED :
559 rfkill->state_for_resume,
562 mutex_unlock(&rfkill->mutex);
565 return 0;
567 #else
568 #define rfkill_suspend NULL
569 #define rfkill_resume NULL
570 #endif
572 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
574 struct rfkill *rfkill = to_rfkill(dev);
575 int error;
577 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
578 if (error)
579 return error;
580 error = add_uevent_var(env, "RFKILL_TYPE=%s",
581 rfkill_get_type_str(rfkill->type));
582 if (error)
583 return error;
584 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
585 return error;
588 static struct class rfkill_class = {
589 .name = "rfkill",
590 .dev_release = rfkill_release,
591 .dev_attrs = rfkill_dev_attrs,
592 .suspend = rfkill_suspend,
593 .resume = rfkill_resume,
594 .dev_uevent = rfkill_dev_uevent,
597 static int rfkill_check_duplicity(const struct rfkill *rfkill)
599 struct rfkill *p;
600 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
602 memset(seen, 0, sizeof(seen));
604 list_for_each_entry(p, &rfkill_list, node) {
605 if (WARN((p == rfkill), KERN_WARNING
606 "rfkill: illegal attempt to register "
607 "an already registered rfkill struct\n"))
608 return -EEXIST;
609 set_bit(p->type, seen);
612 /* 0: first switch of its kind */
613 return (test_bit(rfkill->type, seen)) ? 1 : 0;
616 static int rfkill_add_switch(struct rfkill *rfkill)
618 int error;
620 mutex_lock(&rfkill_global_mutex);
622 error = rfkill_check_duplicity(rfkill);
623 if (error < 0)
624 goto unlock_out;
626 if (!error) {
627 /* lock default after first use */
628 set_bit(rfkill->type, rfkill_states_lockdflt);
629 rfkill_global_states[rfkill->type].current_state =
630 rfkill_global_states[rfkill->type].default_state;
633 rfkill_toggle_radio(rfkill,
634 rfkill_global_states[rfkill->type].current_state,
637 list_add_tail(&rfkill->node, &rfkill_list);
639 error = 0;
640 unlock_out:
641 mutex_unlock(&rfkill_global_mutex);
643 return error;
646 static void rfkill_remove_switch(struct rfkill *rfkill)
648 mutex_lock(&rfkill_global_mutex);
649 list_del_init(&rfkill->node);
650 mutex_unlock(&rfkill_global_mutex);
652 mutex_lock(&rfkill->mutex);
653 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
654 mutex_unlock(&rfkill->mutex);
658 * rfkill_allocate - allocate memory for rfkill structure.
659 * @parent: device that has rf switch on it
660 * @type: type of the switch (RFKILL_TYPE_*)
662 * This function should be called by the network driver when it needs
663 * rfkill structure. Once the structure is allocated the driver should
664 * finish its initialization by setting the name, private data, enable_radio
665 * and disable_radio methods and then register it with rfkill_register().
667 * NOTE: If registration fails the structure shoudl be freed by calling
668 * rfkill_free() otherwise rfkill_unregister() should be used.
670 struct rfkill * __must_check rfkill_allocate(struct device *parent,
671 enum rfkill_type type)
673 struct rfkill *rfkill;
674 struct device *dev;
676 if (WARN((type >= RFKILL_TYPE_MAX),
677 KERN_WARNING
678 "rfkill: illegal type %d passed as parameter "
679 "to rfkill_allocate\n", type))
680 return NULL;
682 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
683 if (!rfkill)
684 return NULL;
686 mutex_init(&rfkill->mutex);
687 INIT_LIST_HEAD(&rfkill->node);
688 rfkill->type = type;
690 dev = &rfkill->dev;
691 dev->class = &rfkill_class;
692 dev->parent = parent;
693 device_initialize(dev);
695 __module_get(THIS_MODULE);
697 return rfkill;
699 EXPORT_SYMBOL(rfkill_allocate);
702 * rfkill_free - Mark rfkill structure for deletion
703 * @rfkill: rfkill structure to be destroyed
705 * Decrements reference count of the rfkill structure so it is destroyed.
706 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
708 void rfkill_free(struct rfkill *rfkill)
710 if (rfkill)
711 put_device(&rfkill->dev);
713 EXPORT_SYMBOL(rfkill_free);
715 static void rfkill_led_trigger_register(struct rfkill *rfkill)
717 #ifdef CONFIG_RFKILL_LEDS
718 int error;
720 if (!rfkill->led_trigger.name)
721 rfkill->led_trigger.name = dev_name(&rfkill->dev);
722 if (!rfkill->led_trigger.activate)
723 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
724 error = led_trigger_register(&rfkill->led_trigger);
725 if (error)
726 rfkill->led_trigger.name = NULL;
727 #endif /* CONFIG_RFKILL_LEDS */
730 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
732 #ifdef CONFIG_RFKILL_LEDS
733 if (rfkill->led_trigger.name) {
734 led_trigger_unregister(&rfkill->led_trigger);
735 rfkill->led_trigger.name = NULL;
737 #endif
741 * rfkill_register - Register a rfkill structure.
742 * @rfkill: rfkill structure to be registered
744 * This function should be called by the network driver when the rfkill
745 * structure needs to be registered. Immediately from registration the
746 * switch driver should be able to service calls to toggle_radio.
748 int __must_check rfkill_register(struct rfkill *rfkill)
750 static atomic_t rfkill_no = ATOMIC_INIT(0);
751 struct device *dev = &rfkill->dev;
752 int error;
754 if (WARN((!rfkill || !rfkill->toggle_radio ||
755 rfkill->type >= RFKILL_TYPE_MAX ||
756 rfkill->state >= RFKILL_STATE_MAX),
757 KERN_WARNING
758 "rfkill: attempt to register a "
759 "badly initialized rfkill struct\n"))
760 return -EINVAL;
762 dev_set_name(dev, "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
764 rfkill_led_trigger_register(rfkill);
766 error = rfkill_add_switch(rfkill);
767 if (error) {
768 rfkill_led_trigger_unregister(rfkill);
769 return error;
772 error = device_add(dev);
773 if (error) {
774 rfkill_remove_switch(rfkill);
775 rfkill_led_trigger_unregister(rfkill);
776 return error;
779 return 0;
781 EXPORT_SYMBOL(rfkill_register);
784 * rfkill_unregister - Unregister a rfkill structure.
785 * @rfkill: rfkill structure to be unregistered
787 * This function should be called by the network driver during device
788 * teardown to destroy rfkill structure. Note that rfkill_free() should
789 * _not_ be called after rfkill_unregister().
791 void rfkill_unregister(struct rfkill *rfkill)
793 BUG_ON(!rfkill);
794 device_del(&rfkill->dev);
795 rfkill_remove_switch(rfkill);
796 rfkill_led_trigger_unregister(rfkill);
797 put_device(&rfkill->dev);
799 EXPORT_SYMBOL(rfkill_unregister);
802 * rfkill_set_default - set initial value for a switch type
803 * @type - the type of switch to set the default state of
804 * @state - the new default state for that group of switches
806 * Sets the initial state rfkill should use for a given type.
807 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
808 * and RFKILL_STATE_UNBLOCKED.
810 * This function is meant to be used by platform drivers for platforms
811 * that can save switch state across power down/reboot.
813 * The default state for each switch type can be changed exactly once.
814 * After a switch of that type is registered, the default state cannot
815 * be changed anymore. This guards against multiple drivers it the
816 * same platform trying to set the initial switch default state, which
817 * is not allowed.
819 * Returns -EPERM if the state has already been set once or is in use,
820 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
821 * if this function returns -EPERM.
823 * Returns 0 if the new default state was set, or an error if it
824 * could not be set.
826 int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
828 int error;
830 if (WARN((type >= RFKILL_TYPE_MAX ||
831 (state != RFKILL_STATE_SOFT_BLOCKED &&
832 state != RFKILL_STATE_UNBLOCKED)),
833 KERN_WARNING
834 "rfkill: illegal state %d or type %d passed as "
835 "parameter to rfkill_set_default\n", state, type))
836 return -EINVAL;
838 mutex_lock(&rfkill_global_mutex);
840 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
841 rfkill_global_states[type].default_state = state;
842 rfkill_global_states[type].current_state = state;
843 error = 0;
844 } else
845 error = -EPERM;
847 mutex_unlock(&rfkill_global_mutex);
848 return error;
850 EXPORT_SYMBOL_GPL(rfkill_set_default);
853 * Rfkill module initialization/deinitialization.
855 static int __init rfkill_init(void)
857 int error;
858 int i;
860 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
861 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
862 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
863 return -EINVAL;
865 for (i = 0; i < RFKILL_TYPE_MAX; i++)
866 rfkill_global_states[i].default_state = rfkill_default_state;
868 error = class_register(&rfkill_class);
869 if (error) {
870 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
871 return error;
874 return 0;
877 static void __exit rfkill_exit(void)
879 class_unregister(&rfkill_class);
882 subsys_initcall(rfkill_init);
883 module_exit(rfkill_exit);