rfkill: introduce RFKILL_STATE_MAX
[linux-2.6/mini2440.git] / net / rfkill / rfkill.c
blob47e0b2d232e30c120121e85e10e88902f86f4e52
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 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
81 EXPORT_SYMBOL_GPL(register_rfkill_notifier);
83 /**
84 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
85 * @nb: pointer to the entry to remove from the chain
87 * See blocking_notifier_chain_unregister() for return value and further
88 * observations.
90 * Removes a notifier from the rfkill notifier chain.
92 int unregister_rfkill_notifier(struct notifier_block *nb)
94 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
96 EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
99 static void rfkill_led_trigger(struct rfkill *rfkill,
100 enum rfkill_state state)
102 #ifdef CONFIG_RFKILL_LEDS
103 struct led_trigger *led = &rfkill->led_trigger;
105 if (!led->name)
106 return;
107 if (state != RFKILL_STATE_UNBLOCKED)
108 led_trigger_event(led, LED_OFF);
109 else
110 led_trigger_event(led, LED_FULL);
111 #endif /* CONFIG_RFKILL_LEDS */
114 #ifdef CONFIG_RFKILL_LEDS
115 static void rfkill_led_trigger_activate(struct led_classdev *led)
117 struct rfkill *rfkill = container_of(led->trigger,
118 struct rfkill, led_trigger);
120 rfkill_led_trigger(rfkill, rfkill->state);
122 #endif /* CONFIG_RFKILL_LEDS */
124 static void notify_rfkill_state_change(struct rfkill *rfkill)
126 blocking_notifier_call_chain(&rfkill_notifier_list,
127 RFKILL_STATE_CHANGED,
128 rfkill);
131 static void update_rfkill_state(struct rfkill *rfkill)
133 enum rfkill_state newstate, oldstate;
135 if (rfkill->get_state) {
136 mutex_lock(&rfkill->mutex);
137 if (!rfkill->get_state(rfkill->data, &newstate)) {
138 oldstate = rfkill->state;
139 rfkill->state = newstate;
140 if (oldstate != newstate)
141 notify_rfkill_state_change(rfkill);
143 mutex_unlock(&rfkill->mutex);
148 * rfkill_toggle_radio - wrapper for toggle_radio hook
149 * @rfkill: the rfkill struct to use
150 * @force: calls toggle_radio even if cache says it is not needed,
151 * and also makes sure notifications of the state will be
152 * sent even if it didn't change
153 * @state: the new state to call toggle_radio() with
155 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
156 * calls and handling all the red tape such as issuing notifications
157 * if the call is successful.
159 * Suspended devices are not touched at all, and -EAGAIN is returned.
161 * Note that the @force parameter cannot override a (possibly cached)
162 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
163 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
164 * rfkill_force_state(), so the cache either is bypassed or valid.
166 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
167 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
168 * give the driver a hint that it should double-BLOCK the transmitter.
170 * Caller must have acquired rfkill->mutex.
172 static int rfkill_toggle_radio(struct rfkill *rfkill,
173 enum rfkill_state state,
174 int force)
176 int retval = 0;
177 enum rfkill_state oldstate, newstate;
179 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
180 return -EBUSY;
182 oldstate = rfkill->state;
184 if (rfkill->get_state && !force &&
185 !rfkill->get_state(rfkill->data, &newstate))
186 rfkill->state = newstate;
188 switch (state) {
189 case RFKILL_STATE_HARD_BLOCKED:
190 /* typically happens when refreshing hardware state,
191 * such as on resume */
192 state = RFKILL_STATE_SOFT_BLOCKED;
193 break;
194 case RFKILL_STATE_UNBLOCKED:
195 /* force can't override this, only rfkill_force_state() can */
196 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
197 return -EPERM;
198 break;
199 case RFKILL_STATE_SOFT_BLOCKED:
200 /* nothing to do, we want to give drivers the hint to double
201 * BLOCK even a transmitter that is already in state
202 * RFKILL_STATE_HARD_BLOCKED */
203 break;
204 default:
205 return -EINVAL;
208 if (force || state != rfkill->state) {
209 retval = rfkill->toggle_radio(rfkill->data, state);
210 /* never allow a HARD->SOFT downgrade! */
211 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
212 rfkill->state = state;
215 if (force || rfkill->state != oldstate) {
216 rfkill_led_trigger(rfkill, rfkill->state);
217 notify_rfkill_state_change(rfkill);
220 return retval;
224 * __rfkill_switch_all - Toggle state of all switches of given type
225 * @type: type of interfaces to be affected
226 * @state: the new state
228 * This function toggles the state of all switches of given type,
229 * unless a specific switch is claimed by userspace (in which case,
230 * that switch is left alone) or suspended.
232 * Caller must have acquired rfkill_mutex.
234 static void __rfkill_switch_all(const enum rfkill_type type,
235 const enum rfkill_state state)
237 struct rfkill *rfkill;
239 if (unlikely(state >= RFKILL_STATE_MAX))
240 return;
242 rfkill_global_states[type].current_state = state;
243 list_for_each_entry(rfkill, &rfkill_list, node) {
244 if ((!rfkill->user_claim) && (rfkill->type == type)) {
245 mutex_lock(&rfkill->mutex);
246 rfkill_toggle_radio(rfkill, state, 0);
247 mutex_unlock(&rfkill->mutex);
253 * rfkill_switch_all - Toggle state of all switches of given type
254 * @type: type of interfaces to be affected
255 * @state: the new state
257 * Acquires rfkill_mutex and calls __rfkill_switch_all(@type, @state).
258 * Please refer to __rfkill_switch_all() for details.
260 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
262 mutex_lock(&rfkill_mutex);
263 __rfkill_switch_all(type, state);
264 mutex_unlock(&rfkill_mutex);
266 EXPORT_SYMBOL(rfkill_switch_all);
269 * rfkill_epo - emergency power off all transmitters
271 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
272 * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
274 * The global state before the EPO is saved and can be restored later
275 * using rfkill_restore_states().
277 void rfkill_epo(void)
279 struct rfkill *rfkill;
280 int i;
282 mutex_lock(&rfkill_mutex);
283 list_for_each_entry(rfkill, &rfkill_list, node) {
284 mutex_lock(&rfkill->mutex);
285 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
286 mutex_unlock(&rfkill->mutex);
288 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
289 rfkill_global_states[i].default_state =
290 rfkill_global_states[i].current_state;
291 rfkill_global_states[i].current_state =
292 RFKILL_STATE_SOFT_BLOCKED;
294 mutex_unlock(&rfkill_mutex);
296 EXPORT_SYMBOL_GPL(rfkill_epo);
299 * rfkill_restore_states - restore global states
301 * Restore (and sync switches to) the global state from the
302 * states in rfkill_default_states. This can undo the effects of
303 * a call to rfkill_epo().
305 void rfkill_restore_states(void)
307 int i;
309 mutex_lock(&rfkill_mutex);
310 for (i = 0; i < RFKILL_TYPE_MAX; i++)
311 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
312 mutex_unlock(&rfkill_mutex);
314 EXPORT_SYMBOL_GPL(rfkill_restore_states);
317 * rfkill_force_state - Force the internal rfkill radio state
318 * @rfkill: pointer to the rfkill class to modify.
319 * @state: the current radio state the class should be forced to.
321 * This function updates the internal state of the radio cached
322 * by the rfkill class. It should be used when the driver gets
323 * a notification by the firmware/hardware of the current *real*
324 * state of the radio rfkill switch.
326 * Devices which are subject to external changes on their rfkill
327 * state (such as those caused by a hardware rfkill line) MUST
328 * have their driver arrange to call rfkill_force_state() as soon
329 * as possible after such a change.
331 * This function may not be called from an atomic context.
333 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
335 enum rfkill_state oldstate;
337 if (unlikely(state >= RFKILL_STATE_MAX))
338 return -EINVAL;
340 mutex_lock(&rfkill->mutex);
342 oldstate = rfkill->state;
343 rfkill->state = state;
345 if (state != oldstate)
346 notify_rfkill_state_change(rfkill);
348 mutex_unlock(&rfkill->mutex);
350 return 0;
352 EXPORT_SYMBOL(rfkill_force_state);
354 static ssize_t rfkill_name_show(struct device *dev,
355 struct device_attribute *attr,
356 char *buf)
358 struct rfkill *rfkill = to_rfkill(dev);
360 return sprintf(buf, "%s\n", rfkill->name);
363 static const char *rfkill_get_type_str(enum rfkill_type type)
365 switch (type) {
366 case RFKILL_TYPE_WLAN:
367 return "wlan";
368 case RFKILL_TYPE_BLUETOOTH:
369 return "bluetooth";
370 case RFKILL_TYPE_UWB:
371 return "ultrawideband";
372 case RFKILL_TYPE_WIMAX:
373 return "wimax";
374 case RFKILL_TYPE_WWAN:
375 return "wwan";
376 default:
377 BUG();
381 static ssize_t rfkill_type_show(struct device *dev,
382 struct device_attribute *attr,
383 char *buf)
385 struct rfkill *rfkill = to_rfkill(dev);
387 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
390 static ssize_t rfkill_state_show(struct device *dev,
391 struct device_attribute *attr,
392 char *buf)
394 struct rfkill *rfkill = to_rfkill(dev);
396 update_rfkill_state(rfkill);
397 return sprintf(buf, "%d\n", rfkill->state);
400 static ssize_t rfkill_state_store(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
404 struct rfkill *rfkill = to_rfkill(dev);
405 unsigned int state = simple_strtoul(buf, NULL, 0);
406 int error;
408 if (!capable(CAP_NET_ADMIN))
409 return -EPERM;
411 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
412 if (state != RFKILL_STATE_UNBLOCKED &&
413 state != RFKILL_STATE_SOFT_BLOCKED)
414 return -EINVAL;
416 if (mutex_lock_interruptible(&rfkill->mutex))
417 return -ERESTARTSYS;
418 error = rfkill_toggle_radio(rfkill, state, 0);
419 mutex_unlock(&rfkill->mutex);
421 return error ? error : count;
424 static ssize_t rfkill_claim_show(struct device *dev,
425 struct device_attribute *attr,
426 char *buf)
428 struct rfkill *rfkill = to_rfkill(dev);
430 return sprintf(buf, "%d", rfkill->user_claim);
433 static ssize_t rfkill_claim_store(struct device *dev,
434 struct device_attribute *attr,
435 const char *buf, size_t count)
437 struct rfkill *rfkill = to_rfkill(dev);
438 bool claim = !!simple_strtoul(buf, NULL, 0);
439 int error;
441 if (!capable(CAP_NET_ADMIN))
442 return -EPERM;
444 if (rfkill->user_claim_unsupported)
445 return -EOPNOTSUPP;
448 * Take the global lock to make sure the kernel is not in
449 * the middle of rfkill_switch_all
451 error = mutex_lock_interruptible(&rfkill_mutex);
452 if (error)
453 return error;
455 if (rfkill->user_claim != claim) {
456 if (!claim) {
457 mutex_lock(&rfkill->mutex);
458 rfkill_toggle_radio(rfkill,
459 rfkill_global_states[rfkill->type].current_state,
461 mutex_unlock(&rfkill->mutex);
463 rfkill->user_claim = claim;
466 mutex_unlock(&rfkill_mutex);
468 return error ? error : count;
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 if (dev->power.power_state.event != state.event) {
493 if (state.event & PM_EVENT_SLEEP) {
494 /* Stop transmitter, keep state, no notifies */
495 update_rfkill_state(rfkill);
497 mutex_lock(&rfkill->mutex);
498 rfkill->toggle_radio(rfkill->data,
499 RFKILL_STATE_SOFT_BLOCKED);
500 mutex_unlock(&rfkill->mutex);
503 dev->power.power_state = state;
506 return 0;
509 static int rfkill_resume(struct device *dev)
511 struct rfkill *rfkill = to_rfkill(dev);
513 if (dev->power.power_state.event != PM_EVENT_ON) {
514 mutex_lock(&rfkill->mutex);
516 dev->power.power_state.event = PM_EVENT_ON;
518 /* restore radio state AND notify everybody */
519 rfkill_toggle_radio(rfkill, rfkill->state, 1);
521 mutex_unlock(&rfkill->mutex);
524 return 0;
526 #else
527 #define rfkill_suspend NULL
528 #define rfkill_resume NULL
529 #endif
531 static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
532 unsigned long eventid,
533 void *data)
535 struct rfkill *rfkill = (struct rfkill *)data;
537 switch (eventid) {
538 case RFKILL_STATE_CHANGED:
539 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
540 break;
541 default:
542 break;
545 return NOTIFY_DONE;
548 static struct notifier_block rfkill_blocking_uevent_nb = {
549 .notifier_call = rfkill_blocking_uevent_notifier,
550 .priority = 0,
553 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
555 struct rfkill *rfkill = to_rfkill(dev);
556 int error;
558 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
559 if (error)
560 return error;
561 error = add_uevent_var(env, "RFKILL_TYPE=%s",
562 rfkill_get_type_str(rfkill->type));
563 if (error)
564 return error;
565 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
566 return error;
569 static struct class rfkill_class = {
570 .name = "rfkill",
571 .dev_release = rfkill_release,
572 .dev_attrs = rfkill_dev_attrs,
573 .suspend = rfkill_suspend,
574 .resume = rfkill_resume,
575 .dev_uevent = rfkill_dev_uevent,
578 static int rfkill_check_duplicity(const struct rfkill *rfkill)
580 struct rfkill *p;
581 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
583 memset(seen, 0, sizeof(seen));
585 list_for_each_entry(p, &rfkill_list, node) {
586 if (p == rfkill) {
587 WARN_ON(1);
588 return -EEXIST;
590 set_bit(p->type, seen);
593 /* 0: first switch of its kind */
594 return test_bit(rfkill->type, seen);
597 static int rfkill_add_switch(struct rfkill *rfkill)
599 int error;
601 mutex_lock(&rfkill_mutex);
603 error = rfkill_check_duplicity(rfkill);
604 if (error < 0)
605 goto unlock_out;
607 if (!error) {
608 /* lock default after first use */
609 set_bit(rfkill->type, rfkill_states_lockdflt);
610 rfkill_global_states[rfkill->type].current_state =
611 rfkill_global_states[rfkill->type].default_state;
614 rfkill_toggle_radio(rfkill,
615 rfkill_global_states[rfkill->type].current_state,
618 list_add_tail(&rfkill->node, &rfkill_list);
620 error = 0;
621 unlock_out:
622 mutex_unlock(&rfkill_mutex);
624 return error;
627 static void rfkill_remove_switch(struct rfkill *rfkill)
629 mutex_lock(&rfkill_mutex);
630 list_del_init(&rfkill->node);
631 mutex_unlock(&rfkill_mutex);
633 mutex_lock(&rfkill->mutex);
634 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
635 mutex_unlock(&rfkill->mutex);
639 * rfkill_allocate - allocate memory for rfkill structure.
640 * @parent: device that has rf switch on it
641 * @type: type of the switch (RFKILL_TYPE_*)
643 * This function should be called by the network driver when it needs
644 * rfkill structure. Once the structure is allocated the driver should
645 * finish its initialization by setting the name, private data, enable_radio
646 * and disable_radio methods and then register it with rfkill_register().
648 * NOTE: If registration fails the structure shoudl be freed by calling
649 * rfkill_free() otherwise rfkill_unregister() should be used.
651 struct rfkill * __must_check rfkill_allocate(struct device *parent,
652 enum rfkill_type type)
654 struct rfkill *rfkill;
655 struct device *dev;
657 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
658 if (!rfkill)
659 return NULL;
661 mutex_init(&rfkill->mutex);
662 INIT_LIST_HEAD(&rfkill->node);
663 rfkill->type = type;
665 dev = &rfkill->dev;
666 dev->class = &rfkill_class;
667 dev->parent = parent;
668 device_initialize(dev);
670 __module_get(THIS_MODULE);
672 return rfkill;
674 EXPORT_SYMBOL(rfkill_allocate);
677 * rfkill_free - Mark rfkill structure for deletion
678 * @rfkill: rfkill structure to be destroyed
680 * Decrements reference count of the rfkill structure so it is destroyed.
681 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
683 void rfkill_free(struct rfkill *rfkill)
685 if (rfkill)
686 put_device(&rfkill->dev);
688 EXPORT_SYMBOL(rfkill_free);
690 static void rfkill_led_trigger_register(struct rfkill *rfkill)
692 #ifdef CONFIG_RFKILL_LEDS
693 int error;
695 if (!rfkill->led_trigger.name)
696 rfkill->led_trigger.name = rfkill->dev.bus_id;
697 if (!rfkill->led_trigger.activate)
698 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
699 error = led_trigger_register(&rfkill->led_trigger);
700 if (error)
701 rfkill->led_trigger.name = NULL;
702 #endif /* CONFIG_RFKILL_LEDS */
705 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
707 #ifdef CONFIG_RFKILL_LEDS
708 if (rfkill->led_trigger.name) {
709 led_trigger_unregister(&rfkill->led_trigger);
710 rfkill->led_trigger.name = NULL;
712 #endif
716 * rfkill_register - Register a rfkill structure.
717 * @rfkill: rfkill structure to be registered
719 * This function should be called by the network driver when the rfkill
720 * structure needs to be registered. Immediately from registration the
721 * switch driver should be able to service calls to toggle_radio.
723 int __must_check rfkill_register(struct rfkill *rfkill)
725 static atomic_t rfkill_no = ATOMIC_INIT(0);
726 struct device *dev = &rfkill->dev;
727 int error;
729 if (!rfkill->toggle_radio)
730 return -EINVAL;
731 if (rfkill->type >= RFKILL_TYPE_MAX)
732 return -EINVAL;
733 if (rfkill->state >= RFKILL_STATE_MAX)
734 return -EINVAL;
736 snprintf(dev->bus_id, sizeof(dev->bus_id),
737 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
739 rfkill_led_trigger_register(rfkill);
741 error = rfkill_add_switch(rfkill);
742 if (error) {
743 rfkill_led_trigger_unregister(rfkill);
744 return error;
747 error = device_add(dev);
748 if (error) {
749 rfkill_remove_switch(rfkill);
750 rfkill_led_trigger_unregister(rfkill);
751 return error;
754 return 0;
756 EXPORT_SYMBOL(rfkill_register);
759 * rfkill_unregister - Unregister a rfkill structure.
760 * @rfkill: rfkill structure to be unregistered
762 * This function should be called by the network driver during device
763 * teardown to destroy rfkill structure. Note that rfkill_free() should
764 * _not_ be called after rfkill_unregister().
766 void rfkill_unregister(struct rfkill *rfkill)
768 device_del(&rfkill->dev);
769 rfkill_remove_switch(rfkill);
770 rfkill_led_trigger_unregister(rfkill);
771 put_device(&rfkill->dev);
773 EXPORT_SYMBOL(rfkill_unregister);
776 * rfkill_set_default - set initial value for a switch type
777 * @type - the type of switch to set the default state of
778 * @state - the new default state for that group of switches
780 * Sets the initial state rfkill should use for a given type.
781 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
782 * and RFKILL_STATE_UNBLOCKED.
784 * This function is meant to be used by platform drivers for platforms
785 * that can save switch state across power down/reboot.
787 * The default state for each switch type can be changed exactly once.
788 * After a switch of that type is registered, the default state cannot
789 * be changed anymore. This guards against multiple drivers it the
790 * same platform trying to set the initial switch default state, which
791 * is not allowed.
793 * Returns -EPERM if the state has already been set once or is in use,
794 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
795 * if this function returns -EPERM.
797 * Returns 0 if the new default state was set, or an error if it
798 * could not be set.
800 int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
802 int error;
804 if (type >= RFKILL_TYPE_MAX ||
805 (state != RFKILL_STATE_SOFT_BLOCKED &&
806 state != RFKILL_STATE_UNBLOCKED))
807 return -EINVAL;
809 mutex_lock(&rfkill_mutex);
811 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
812 rfkill_global_states[type].default_state = state;
813 error = 0;
814 } else
815 error = -EPERM;
817 mutex_unlock(&rfkill_mutex);
818 return error;
820 EXPORT_SYMBOL_GPL(rfkill_set_default);
823 * Rfkill module initialization/deinitialization.
825 static int __init rfkill_init(void)
827 int error;
828 int i;
830 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
831 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
832 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
833 return -EINVAL;
835 for (i = 0; i < RFKILL_TYPE_MAX; i++)
836 rfkill_global_states[i].default_state = rfkill_default_state;
838 error = class_register(&rfkill_class);
839 if (error) {
840 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
841 return error;
844 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
846 return 0;
849 static void __exit rfkill_exit(void)
851 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
852 class_unregister(&rfkill_class);
855 subsys_initcall(rfkill_init);
856 module_exit(rfkill_exit);