net/rfkill/rfkill.c: fix build with CONFIG_RFKILL_LEDS=n
[linux-2.6/cjktty.git] / net / rfkill / rfkill.c
blob4f5a83183c95cd1de62da70d5ec0d117d641400e
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 #else
79 static inline void rfkill_led_trigger(struct rfkill *rfkill,
80 enum rfkill_state state)
83 #endif /* CONFIG_RFKILL_LEDS */
85 static void rfkill_uevent(struct rfkill *rfkill)
87 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
90 static void update_rfkill_state(struct rfkill *rfkill)
92 enum rfkill_state newstate, oldstate;
94 if (rfkill->get_state) {
95 mutex_lock(&rfkill->mutex);
96 if (!rfkill->get_state(rfkill->data, &newstate)) {
97 oldstate = rfkill->state;
98 rfkill->state = newstate;
99 if (oldstate != newstate)
100 rfkill_uevent(rfkill);
102 mutex_unlock(&rfkill->mutex);
104 rfkill_led_trigger(rfkill, rfkill->state);
108 * rfkill_toggle_radio - wrapper for toggle_radio hook
109 * @rfkill: the rfkill struct to use
110 * @force: calls toggle_radio even if cache says it is not needed,
111 * and also makes sure notifications of the state will be
112 * sent even if it didn't change
113 * @state: the new state to call toggle_radio() with
115 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
116 * calls and handling all the red tape such as issuing notifications
117 * if the call is successful.
119 * Suspended devices are not touched at all, and -EAGAIN is returned.
121 * Note that the @force parameter cannot override a (possibly cached)
122 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
123 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
124 * rfkill_force_state(), so the cache either is bypassed or valid.
126 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
127 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
128 * give the driver a hint that it should double-BLOCK the transmitter.
130 * Caller must have acquired rfkill->mutex.
132 static int rfkill_toggle_radio(struct rfkill *rfkill,
133 enum rfkill_state state,
134 int force)
136 int retval = 0;
137 enum rfkill_state oldstate, newstate;
139 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
140 return -EBUSY;
142 oldstate = rfkill->state;
144 if (rfkill->get_state && !force &&
145 !rfkill->get_state(rfkill->data, &newstate)) {
146 rfkill->state = newstate;
149 switch (state) {
150 case RFKILL_STATE_HARD_BLOCKED:
151 /* typically happens when refreshing hardware state,
152 * such as on resume */
153 state = RFKILL_STATE_SOFT_BLOCKED;
154 break;
155 case RFKILL_STATE_UNBLOCKED:
156 /* force can't override this, only rfkill_force_state() can */
157 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
158 return -EPERM;
159 break;
160 case RFKILL_STATE_SOFT_BLOCKED:
161 /* nothing to do, we want to give drivers the hint to double
162 * BLOCK even a transmitter that is already in state
163 * RFKILL_STATE_HARD_BLOCKED */
164 break;
165 default:
166 WARN(1, KERN_WARNING
167 "rfkill: illegal state %d passed as parameter "
168 "to rfkill_toggle_radio\n", state);
169 return -EINVAL;
172 if (force || state != rfkill->state) {
173 retval = rfkill->toggle_radio(rfkill->data, state);
174 /* never allow a HARD->SOFT downgrade! */
175 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
176 rfkill->state = state;
179 if (force || rfkill->state != oldstate)
180 rfkill_uevent(rfkill);
182 rfkill_led_trigger(rfkill, rfkill->state);
183 return retval;
187 * __rfkill_switch_all - Toggle state of all switches of given type
188 * @type: type of interfaces to be affected
189 * @state: the new state
191 * This function toggles the state of all switches of given type,
192 * unless a specific switch is claimed by userspace (in which case,
193 * that switch is left alone) or suspended.
195 * Caller must have acquired rfkill_global_mutex.
197 static void __rfkill_switch_all(const enum rfkill_type type,
198 const enum rfkill_state state)
200 struct rfkill *rfkill;
202 if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
203 KERN_WARNING
204 "rfkill: illegal state %d or type %d "
205 "passed as parameter to __rfkill_switch_all\n",
206 state, type))
207 return;
209 rfkill_global_states[type].current_state = state;
210 list_for_each_entry(rfkill, &rfkill_list, node) {
211 if (rfkill->type == type) {
212 mutex_lock(&rfkill->mutex);
213 rfkill_toggle_radio(rfkill, state, 0);
214 mutex_unlock(&rfkill->mutex);
215 rfkill_led_trigger(rfkill, rfkill->state);
221 * rfkill_switch_all - Toggle state of all switches of given type
222 * @type: type of interfaces to be affected
223 * @state: the new state
225 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
226 * Please refer to __rfkill_switch_all() for details.
228 * Does nothing if the EPO lock is active.
230 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
232 mutex_lock(&rfkill_global_mutex);
233 if (!rfkill_epo_lock_active)
234 __rfkill_switch_all(type, state);
235 mutex_unlock(&rfkill_global_mutex);
237 EXPORT_SYMBOL(rfkill_switch_all);
240 * rfkill_epo - emergency power off all transmitters
242 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
243 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
245 * The global state before the EPO is saved and can be restored later
246 * using rfkill_restore_states().
248 void rfkill_epo(void)
250 struct rfkill *rfkill;
251 int i;
253 mutex_lock(&rfkill_global_mutex);
255 rfkill_epo_lock_active = true;
256 list_for_each_entry(rfkill, &rfkill_list, node) {
257 mutex_lock(&rfkill->mutex);
258 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
259 mutex_unlock(&rfkill->mutex);
261 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
262 rfkill_global_states[i].default_state =
263 rfkill_global_states[i].current_state;
264 rfkill_global_states[i].current_state =
265 RFKILL_STATE_SOFT_BLOCKED;
267 mutex_unlock(&rfkill_global_mutex);
268 rfkill_led_trigger(rfkill, rfkill->state);
270 EXPORT_SYMBOL_GPL(rfkill_epo);
273 * rfkill_restore_states - restore global states
275 * Restore (and sync switches to) the global state from the
276 * states in rfkill_default_states. This can undo the effects of
277 * a call to rfkill_epo().
279 void rfkill_restore_states(void)
281 int i;
283 mutex_lock(&rfkill_global_mutex);
285 rfkill_epo_lock_active = false;
286 for (i = 0; i < RFKILL_TYPE_MAX; i++)
287 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
288 mutex_unlock(&rfkill_global_mutex);
290 EXPORT_SYMBOL_GPL(rfkill_restore_states);
293 * rfkill_remove_epo_lock - unlock state changes
295 * Used by rfkill-input manually unlock state changes, when
296 * the EPO switch is deactivated.
298 void rfkill_remove_epo_lock(void)
300 mutex_lock(&rfkill_global_mutex);
301 rfkill_epo_lock_active = false;
302 mutex_unlock(&rfkill_global_mutex);
304 EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock);
307 * rfkill_is_epo_lock_active - returns true EPO is active
309 * Returns 0 (false) if there is NOT an active EPO contidion,
310 * and 1 (true) if there is an active EPO contition, which
311 * locks all radios in one of the BLOCKED states.
313 * Can be called in atomic context.
315 bool rfkill_is_epo_lock_active(void)
317 return rfkill_epo_lock_active;
319 EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active);
322 * rfkill_get_global_state - returns global state for a type
323 * @type: the type to get the global state of
325 * Returns the current global state for a given wireless
326 * device type.
328 enum rfkill_state rfkill_get_global_state(const enum rfkill_type type)
330 return rfkill_global_states[type].current_state;
332 EXPORT_SYMBOL_GPL(rfkill_get_global_state);
335 * rfkill_force_state - Force the internal rfkill radio state
336 * @rfkill: pointer to the rfkill class to modify.
337 * @state: the current radio state the class should be forced to.
339 * This function updates the internal state of the radio cached
340 * by the rfkill class. It should be used when the driver gets
341 * a notification by the firmware/hardware of the current *real*
342 * state of the radio rfkill switch.
344 * Devices which are subject to external changes on their rfkill
345 * state (such as those caused by a hardware rfkill line) MUST
346 * have their driver arrange to call rfkill_force_state() as soon
347 * as possible after such a change.
349 * This function may not be called from an atomic context.
351 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
353 enum rfkill_state oldstate;
355 BUG_ON(!rfkill);
356 if (WARN((state >= RFKILL_STATE_MAX),
357 KERN_WARNING
358 "rfkill: illegal state %d passed as parameter "
359 "to rfkill_force_state\n", state))
360 return -EINVAL;
362 mutex_lock(&rfkill->mutex);
364 oldstate = rfkill->state;
365 rfkill->state = state;
367 if (state != oldstate)
368 rfkill_uevent(rfkill);
370 mutex_unlock(&rfkill->mutex);
371 rfkill_led_trigger(rfkill, rfkill->state);
373 return 0;
375 EXPORT_SYMBOL(rfkill_force_state);
377 static ssize_t rfkill_name_show(struct device *dev,
378 struct device_attribute *attr,
379 char *buf)
381 struct rfkill *rfkill = to_rfkill(dev);
383 return sprintf(buf, "%s\n", rfkill->name);
386 static const char *rfkill_get_type_str(enum rfkill_type type)
388 switch (type) {
389 case RFKILL_TYPE_WLAN:
390 return "wlan";
391 case RFKILL_TYPE_BLUETOOTH:
392 return "bluetooth";
393 case RFKILL_TYPE_UWB:
394 return "ultrawideband";
395 case RFKILL_TYPE_WIMAX:
396 return "wimax";
397 case RFKILL_TYPE_WWAN:
398 return "wwan";
399 default:
400 BUG();
404 static ssize_t rfkill_type_show(struct device *dev,
405 struct device_attribute *attr,
406 char *buf)
408 struct rfkill *rfkill = to_rfkill(dev);
410 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
413 static ssize_t rfkill_state_show(struct device *dev,
414 struct device_attribute *attr,
415 char *buf)
417 struct rfkill *rfkill = to_rfkill(dev);
419 update_rfkill_state(rfkill);
420 return sprintf(buf, "%d\n", rfkill->state);
423 static ssize_t rfkill_state_store(struct device *dev,
424 struct device_attribute *attr,
425 const char *buf, size_t count)
427 struct rfkill *rfkill = to_rfkill(dev);
428 unsigned long state;
429 int error;
431 if (!capable(CAP_NET_ADMIN))
432 return -EPERM;
434 error = strict_strtoul(buf, 0, &state);
435 if (error)
436 return error;
438 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
439 if (state != RFKILL_STATE_UNBLOCKED &&
440 state != RFKILL_STATE_SOFT_BLOCKED)
441 return -EINVAL;
443 error = mutex_lock_killable(&rfkill->mutex);
444 if (error)
445 return error;
447 if (!rfkill_epo_lock_active)
448 error = rfkill_toggle_radio(rfkill, state, 0);
449 else
450 error = -EPERM;
452 mutex_unlock(&rfkill->mutex);
454 return error ? error : count;
457 static ssize_t rfkill_claim_show(struct device *dev,
458 struct device_attribute *attr,
459 char *buf)
461 return sprintf(buf, "%d\n", 0);
464 static ssize_t rfkill_claim_store(struct device *dev,
465 struct device_attribute *attr,
466 const char *buf, size_t count)
468 return -EOPNOTSUPP;
471 static struct device_attribute rfkill_dev_attrs[] = {
472 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
473 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
474 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
475 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
476 __ATTR_NULL
479 static void rfkill_release(struct device *dev)
481 struct rfkill *rfkill = to_rfkill(dev);
483 kfree(rfkill);
484 module_put(THIS_MODULE);
487 #ifdef CONFIG_PM
488 static int rfkill_suspend(struct device *dev, pm_message_t state)
490 struct rfkill *rfkill = to_rfkill(dev);
492 /* mark class device as suspended */
493 if (dev->power.power_state.event != state.event)
494 dev->power.power_state = state;
496 /* store state for the resume handler */
497 rfkill->state_for_resume = rfkill->state;
499 return 0;
502 static int rfkill_resume(struct device *dev)
504 struct rfkill *rfkill = to_rfkill(dev);
505 enum rfkill_state newstate;
507 if (dev->power.power_state.event != PM_EVENT_ON) {
508 mutex_lock(&rfkill->mutex);
510 dev->power.power_state.event = PM_EVENT_ON;
513 * rfkill->state could have been modified before we got
514 * called, and won't be updated by rfkill_toggle_radio()
515 * in force mode. Sync it FIRST.
517 if (rfkill->get_state &&
518 !rfkill->get_state(rfkill->data, &newstate))
519 rfkill->state = newstate;
522 * If we are under EPO, kick transmitter offline,
523 * otherwise restore to pre-suspend state.
525 * Issue a notification in any case
527 rfkill_toggle_radio(rfkill,
528 rfkill_epo_lock_active ?
529 RFKILL_STATE_SOFT_BLOCKED :
530 rfkill->state_for_resume,
533 mutex_unlock(&rfkill->mutex);
534 rfkill_led_trigger(rfkill, rfkill->state);
537 return 0;
539 #else
540 #define rfkill_suspend NULL
541 #define rfkill_resume NULL
542 #endif
544 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
546 struct rfkill *rfkill = to_rfkill(dev);
547 int error;
549 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
550 if (error)
551 return error;
552 error = add_uevent_var(env, "RFKILL_TYPE=%s",
553 rfkill_get_type_str(rfkill->type));
554 if (error)
555 return error;
556 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
557 return error;
560 static struct class rfkill_class = {
561 .name = "rfkill",
562 .dev_release = rfkill_release,
563 .dev_attrs = rfkill_dev_attrs,
564 .suspend = rfkill_suspend,
565 .resume = rfkill_resume,
566 .dev_uevent = rfkill_dev_uevent,
569 static int rfkill_check_duplicity(const struct rfkill *rfkill)
571 struct rfkill *p;
572 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
574 memset(seen, 0, sizeof(seen));
576 list_for_each_entry(p, &rfkill_list, node) {
577 if (WARN((p == rfkill), KERN_WARNING
578 "rfkill: illegal attempt to register "
579 "an already registered rfkill struct\n"))
580 return -EEXIST;
581 set_bit(p->type, seen);
584 /* 0: first switch of its kind */
585 return (test_bit(rfkill->type, seen)) ? 1 : 0;
588 static int rfkill_add_switch(struct rfkill *rfkill)
590 int error;
592 mutex_lock(&rfkill_global_mutex);
594 error = rfkill_check_duplicity(rfkill);
595 if (error < 0)
596 goto unlock_out;
598 if (!error) {
599 /* lock default after first use */
600 set_bit(rfkill->type, rfkill_states_lockdflt);
601 rfkill_global_states[rfkill->type].current_state =
602 rfkill_global_states[rfkill->type].default_state;
605 rfkill_toggle_radio(rfkill,
606 rfkill_global_states[rfkill->type].current_state,
609 list_add_tail(&rfkill->node, &rfkill_list);
611 error = 0;
612 unlock_out:
613 mutex_unlock(&rfkill_global_mutex);
615 return error;
618 static void rfkill_remove_switch(struct rfkill *rfkill)
620 mutex_lock(&rfkill_global_mutex);
621 list_del_init(&rfkill->node);
622 mutex_unlock(&rfkill_global_mutex);
624 mutex_lock(&rfkill->mutex);
625 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
626 mutex_unlock(&rfkill->mutex);
630 * rfkill_allocate - allocate memory for rfkill structure.
631 * @parent: device that has rf switch on it
632 * @type: type of the switch (RFKILL_TYPE_*)
634 * This function should be called by the network driver when it needs
635 * rfkill structure. Once the structure is allocated the driver should
636 * finish its initialization by setting the name, private data, enable_radio
637 * and disable_radio methods and then register it with rfkill_register().
639 * NOTE: If registration fails the structure shoudl be freed by calling
640 * rfkill_free() otherwise rfkill_unregister() should be used.
642 struct rfkill * __must_check rfkill_allocate(struct device *parent,
643 enum rfkill_type type)
645 struct rfkill *rfkill;
646 struct device *dev;
648 if (WARN((type >= RFKILL_TYPE_MAX),
649 KERN_WARNING
650 "rfkill: illegal type %d passed as parameter "
651 "to rfkill_allocate\n", type))
652 return NULL;
654 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
655 if (!rfkill)
656 return NULL;
658 mutex_init(&rfkill->mutex);
659 INIT_LIST_HEAD(&rfkill->node);
660 rfkill->type = type;
662 dev = &rfkill->dev;
663 dev->class = &rfkill_class;
664 dev->parent = parent;
665 device_initialize(dev);
667 __module_get(THIS_MODULE);
669 return rfkill;
671 EXPORT_SYMBOL(rfkill_allocate);
674 * rfkill_free - Mark rfkill structure for deletion
675 * @rfkill: rfkill structure to be destroyed
677 * Decrements reference count of the rfkill structure so it is destroyed.
678 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
680 void rfkill_free(struct rfkill *rfkill)
682 if (rfkill)
683 put_device(&rfkill->dev);
685 EXPORT_SYMBOL(rfkill_free);
687 static void rfkill_led_trigger_register(struct rfkill *rfkill)
689 #ifdef CONFIG_RFKILL_LEDS
690 int error;
692 if (!rfkill->led_trigger.name)
693 rfkill->led_trigger.name = dev_name(&rfkill->dev);
694 if (!rfkill->led_trigger.activate)
695 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
696 error = led_trigger_register(&rfkill->led_trigger);
697 if (error)
698 rfkill->led_trigger.name = NULL;
699 #endif /* CONFIG_RFKILL_LEDS */
702 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
704 #ifdef CONFIG_RFKILL_LEDS
705 if (rfkill->led_trigger.name) {
706 led_trigger_unregister(&rfkill->led_trigger);
707 rfkill->led_trigger.name = NULL;
709 #endif
713 * rfkill_register - Register a rfkill structure.
714 * @rfkill: rfkill structure to be registered
716 * This function should be called by the network driver when the rfkill
717 * structure needs to be registered. Immediately from registration the
718 * switch driver should be able to service calls to toggle_radio.
720 int __must_check rfkill_register(struct rfkill *rfkill)
722 static atomic_t rfkill_no = ATOMIC_INIT(0);
723 struct device *dev = &rfkill->dev;
724 int error;
726 if (WARN((!rfkill || !rfkill->toggle_radio ||
727 rfkill->type >= RFKILL_TYPE_MAX ||
728 rfkill->state >= RFKILL_STATE_MAX),
729 KERN_WARNING
730 "rfkill: attempt to register a "
731 "badly initialized rfkill struct\n"))
732 return -EINVAL;
734 dev_set_name(dev, "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
736 rfkill_led_trigger_register(rfkill);
738 error = rfkill_add_switch(rfkill);
739 if (error) {
740 rfkill_led_trigger_unregister(rfkill);
741 return error;
744 error = device_add(dev);
745 if (error) {
746 rfkill_remove_switch(rfkill);
747 rfkill_led_trigger_unregister(rfkill);
748 return error;
751 return 0;
753 EXPORT_SYMBOL(rfkill_register);
756 * rfkill_unregister - Unregister a rfkill structure.
757 * @rfkill: rfkill structure to be unregistered
759 * This function should be called by the network driver during device
760 * teardown to destroy rfkill structure. Note that rfkill_free() should
761 * _not_ be called after rfkill_unregister().
763 void rfkill_unregister(struct rfkill *rfkill)
765 BUG_ON(!rfkill);
766 device_del(&rfkill->dev);
767 rfkill_remove_switch(rfkill);
768 rfkill_led_trigger_unregister(rfkill);
769 put_device(&rfkill->dev);
771 EXPORT_SYMBOL(rfkill_unregister);
774 * rfkill_set_default - set initial value for a switch type
775 * @type - the type of switch to set the default state of
776 * @state - the new default state for that group of switches
778 * Sets the initial state rfkill should use for a given type.
779 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
780 * and RFKILL_STATE_UNBLOCKED.
782 * This function is meant to be used by platform drivers for platforms
783 * that can save switch state across power down/reboot.
785 * The default state for each switch type can be changed exactly once.
786 * After a switch of that type is registered, the default state cannot
787 * be changed anymore. This guards against multiple drivers it the
788 * same platform trying to set the initial switch default state, which
789 * is not allowed.
791 * Returns -EPERM if the state has already been set once or is in use,
792 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
793 * if this function returns -EPERM.
795 * Returns 0 if the new default state was set, or an error if it
796 * could not be set.
798 int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
800 int error;
802 if (WARN((type >= RFKILL_TYPE_MAX ||
803 (state != RFKILL_STATE_SOFT_BLOCKED &&
804 state != RFKILL_STATE_UNBLOCKED)),
805 KERN_WARNING
806 "rfkill: illegal state %d or type %d passed as "
807 "parameter to rfkill_set_default\n", state, type))
808 return -EINVAL;
810 mutex_lock(&rfkill_global_mutex);
812 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
813 rfkill_global_states[type].default_state = state;
814 rfkill_global_states[type].current_state = state;
815 error = 0;
816 } else
817 error = -EPERM;
819 mutex_unlock(&rfkill_global_mutex);
820 return error;
822 EXPORT_SYMBOL_GPL(rfkill_set_default);
825 * Rfkill module initialization/deinitialization.
827 static int __init rfkill_init(void)
829 int error;
830 int i;
832 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
833 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
834 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
835 return -EINVAL;
837 for (i = 0; i < RFKILL_TYPE_MAX; i++)
838 rfkill_global_states[i].default_state = rfkill_default_state;
840 error = class_register(&rfkill_class);
841 if (error) {
842 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
843 return error;
846 return 0;
849 static void __exit rfkill_exit(void)
851 class_unregister(&rfkill_class);
854 subsys_initcall(rfkill_init);
855 module_exit(rfkill_exit);