4f5a83183c95cd1de62da70d5ec0d117d641400e
[pandora-kernel.git] / net / rfkill / rfkill.c
1 /*
2  * Copyright (C) 2006 - 2007 Ivo van Doorn
3  * Copyright (C) 2007 Dmitry Torokhov
4  *
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.
9  *
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.
14  *
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.
19  */
20
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>
29
30 /* Get declaration of rfkill_switch_all() to shut up sparse. */
31 #include "rfkill-input.h"
32
33
34 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35 MODULE_VERSION("1.0");
36 MODULE_DESCRIPTION("RF switch support");
37 MODULE_LICENSE("GPL");
38
39 static LIST_HEAD(rfkill_list);  /* list of registered rf switches */
40 static DEFINE_MUTEX(rfkill_global_mutex);
41
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");
46
47 struct rfkill_gsw_state {
48         enum rfkill_state current_state;
49         enum rfkill_state default_state;
50 };
51
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;
55
56
57 #ifdef CONFIG_RFKILL_LEDS
58 static void rfkill_led_trigger(struct rfkill *rfkill,
59                                enum rfkill_state state)
60 {
61         struct led_trigger *led = &rfkill->led_trigger;
62
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);
69 }
70
71 static void rfkill_led_trigger_activate(struct led_classdev *led)
72 {
73         struct rfkill *rfkill = container_of(led->trigger,
74                         struct rfkill, led_trigger);
75
76         rfkill_led_trigger(rfkill, rfkill->state);
77 }
78 #else
79 static inline void rfkill_led_trigger(struct rfkill *rfkill,
80                                         enum rfkill_state state)
81 {
82 }
83 #endif /* CONFIG_RFKILL_LEDS */
84
85 static void rfkill_uevent(struct rfkill *rfkill)
86 {
87         kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
88 }
89
90 static void update_rfkill_state(struct rfkill *rfkill)
91 {
92         enum rfkill_state newstate, oldstate;
93
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);
101                 }
102                 mutex_unlock(&rfkill->mutex);
103         }
104         rfkill_led_trigger(rfkill, rfkill->state);
105 }
106
107 /**
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
114  *
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.
118  *
119  * Suspended devices are not touched at all, and -EAGAIN is returned.
120  *
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.
125  *
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.
129  *
130  * Caller must have acquired rfkill->mutex.
131  */
132 static int rfkill_toggle_radio(struct rfkill *rfkill,
133                                 enum rfkill_state state,
134                                 int force)
135 {
136         int retval = 0;
137         enum rfkill_state oldstate, newstate;
138
139         if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
140                 return -EBUSY;
141
142         oldstate = rfkill->state;
143
144         if (rfkill->get_state && !force &&
145             !rfkill->get_state(rfkill->data, &newstate)) {
146                 rfkill->state = newstate;
147         }
148
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;
170         }
171
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;
177         }
178
179         if (force || rfkill->state != oldstate)
180                 rfkill_uevent(rfkill);
181
182         rfkill_led_trigger(rfkill, rfkill->state);
183         return retval;
184 }
185
186 /**
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
190  *
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.
194  *
195  * Caller must have acquired rfkill_global_mutex.
196  */
197 static void __rfkill_switch_all(const enum rfkill_type type,
198                                 const enum rfkill_state state)
199 {
200         struct rfkill *rfkill;
201
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;
208
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);
216                 }
217         }
218 }
219
220 /**
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
224  *
225  * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
226  * Please refer to __rfkill_switch_all() for details.
227  *
228  * Does nothing if the EPO lock is active.
229  */
230 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
231 {
232         mutex_lock(&rfkill_global_mutex);
233         if (!rfkill_epo_lock_active)
234                 __rfkill_switch_all(type, state);
235         mutex_unlock(&rfkill_global_mutex);
236 }
237 EXPORT_SYMBOL(rfkill_switch_all);
238
239 /**
240  * rfkill_epo - emergency power off all transmitters
241  *
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.
244  *
245  * The global state before the EPO is saved and can be restored later
246  * using rfkill_restore_states().
247  */
248 void rfkill_epo(void)
249 {
250         struct rfkill *rfkill;
251         int i;
252
253         mutex_lock(&rfkill_global_mutex);
254
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);
260         }
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;
266         }
267         mutex_unlock(&rfkill_global_mutex);
268         rfkill_led_trigger(rfkill, rfkill->state);
269 }
270 EXPORT_SYMBOL_GPL(rfkill_epo);
271
272 /**
273  * rfkill_restore_states - restore global states
274  *
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().
278  */
279 void rfkill_restore_states(void)
280 {
281         int i;
282
283         mutex_lock(&rfkill_global_mutex);
284
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);
289 }
290 EXPORT_SYMBOL_GPL(rfkill_restore_states);
291
292 /**
293  * rfkill_remove_epo_lock - unlock state changes
294  *
295  * Used by rfkill-input manually unlock state changes, when
296  * the EPO switch is deactivated.
297  */
298 void rfkill_remove_epo_lock(void)
299 {
300         mutex_lock(&rfkill_global_mutex);
301         rfkill_epo_lock_active = false;
302         mutex_unlock(&rfkill_global_mutex);
303 }
304 EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock);
305
306 /**
307  * rfkill_is_epo_lock_active - returns true EPO is active
308  *
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.
312  *
313  * Can be called in atomic context.
314  */
315 bool rfkill_is_epo_lock_active(void)
316 {
317         return rfkill_epo_lock_active;
318 }
319 EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active);
320
321 /**
322  * rfkill_get_global_state - returns global state for a type
323  * @type: the type to get the global state of
324  *
325  * Returns the current global state for a given wireless
326  * device type.
327  */
328 enum rfkill_state rfkill_get_global_state(const enum rfkill_type type)
329 {
330         return rfkill_global_states[type].current_state;
331 }
332 EXPORT_SYMBOL_GPL(rfkill_get_global_state);
333
334 /**
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.
338  *
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.
343  *
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.
348  *
349  * This function may not be called from an atomic context.
350  */
351 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
352 {
353         enum rfkill_state oldstate;
354
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;
361
362         mutex_lock(&rfkill->mutex);
363
364         oldstate = rfkill->state;
365         rfkill->state = state;
366
367         if (state != oldstate)
368                 rfkill_uevent(rfkill);
369
370         mutex_unlock(&rfkill->mutex);
371         rfkill_led_trigger(rfkill, rfkill->state);
372
373         return 0;
374 }
375 EXPORT_SYMBOL(rfkill_force_state);
376
377 static ssize_t rfkill_name_show(struct device *dev,
378                                 struct device_attribute *attr,
379                                 char *buf)
380 {
381         struct rfkill *rfkill = to_rfkill(dev);
382
383         return sprintf(buf, "%s\n", rfkill->name);
384 }
385
386 static const char *rfkill_get_type_str(enum rfkill_type type)
387 {
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();
401         }
402 }
403
404 static ssize_t rfkill_type_show(struct device *dev,
405                                 struct device_attribute *attr,
406                                 char *buf)
407 {
408         struct rfkill *rfkill = to_rfkill(dev);
409
410         return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
411 }
412
413 static ssize_t rfkill_state_show(struct device *dev,
414                                  struct device_attribute *attr,
415                                  char *buf)
416 {
417         struct rfkill *rfkill = to_rfkill(dev);
418
419         update_rfkill_state(rfkill);
420         return sprintf(buf, "%d\n", rfkill->state);
421 }
422
423 static ssize_t rfkill_state_store(struct device *dev,
424                                   struct device_attribute *attr,
425                                   const char *buf, size_t count)
426 {
427         struct rfkill *rfkill = to_rfkill(dev);
428         unsigned long state;
429         int error;
430
431         if (!capable(CAP_NET_ADMIN))
432                 return -EPERM;
433
434         error = strict_strtoul(buf, 0, &state);
435         if (error)
436                 return error;
437
438         /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
439         if (state != RFKILL_STATE_UNBLOCKED &&
440             state != RFKILL_STATE_SOFT_BLOCKED)
441                 return -EINVAL;
442
443         error = mutex_lock_killable(&rfkill->mutex);
444         if (error)
445                 return error;
446
447         if (!rfkill_epo_lock_active)
448                 error = rfkill_toggle_radio(rfkill, state, 0);
449         else
450                 error = -EPERM;
451
452         mutex_unlock(&rfkill->mutex);
453
454         return error ? error : count;
455 }
456
457 static ssize_t rfkill_claim_show(struct device *dev,
458                                  struct device_attribute *attr,
459                                  char *buf)
460 {
461         return sprintf(buf, "%d\n", 0);
462 }
463
464 static ssize_t rfkill_claim_store(struct device *dev,
465                                   struct device_attribute *attr,
466                                   const char *buf, size_t count)
467 {
468         return -EOPNOTSUPP;
469 }
470
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
477 };
478
479 static void rfkill_release(struct device *dev)
480 {
481         struct rfkill *rfkill = to_rfkill(dev);
482
483         kfree(rfkill);
484         module_put(THIS_MODULE);
485 }
486
487 #ifdef CONFIG_PM
488 static int rfkill_suspend(struct device *dev, pm_message_t state)
489 {
490         struct rfkill *rfkill = to_rfkill(dev);
491
492         /* mark class device as suspended */
493         if (dev->power.power_state.event != state.event)
494                 dev->power.power_state = state;
495
496         /* store state for the resume handler */
497         rfkill->state_for_resume = rfkill->state;
498
499         return 0;
500 }
501
502 static int rfkill_resume(struct device *dev)
503 {
504         struct rfkill *rfkill = to_rfkill(dev);
505         enum rfkill_state newstate;
506
507         if (dev->power.power_state.event != PM_EVENT_ON) {
508                 mutex_lock(&rfkill->mutex);
509
510                 dev->power.power_state.event = PM_EVENT_ON;
511
512                 /*
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.
516                  */
517                 if (rfkill->get_state &&
518                     !rfkill->get_state(rfkill->data, &newstate))
519                         rfkill->state = newstate;
520
521                 /*
522                  * If we are under EPO, kick transmitter offline,
523                  * otherwise restore to pre-suspend state.
524                  *
525                  * Issue a notification in any case
526                  */
527                 rfkill_toggle_radio(rfkill,
528                                 rfkill_epo_lock_active ?
529                                         RFKILL_STATE_SOFT_BLOCKED :
530                                         rfkill->state_for_resume,
531                                 1);
532
533                 mutex_unlock(&rfkill->mutex);
534                 rfkill_led_trigger(rfkill, rfkill->state);
535         }
536
537         return 0;
538 }
539 #else
540 #define rfkill_suspend NULL
541 #define rfkill_resume NULL
542 #endif
543
544 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
545 {
546         struct rfkill *rfkill = to_rfkill(dev);
547         int error;
548
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;
558 }
559
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,
567 };
568
569 static int rfkill_check_duplicity(const struct rfkill *rfkill)
570 {
571         struct rfkill *p;
572         unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
573
574         memset(seen, 0, sizeof(seen));
575
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);
582         }
583
584         /* 0: first switch of its kind */
585         return (test_bit(rfkill->type, seen)) ? 1 : 0;
586 }
587
588 static int rfkill_add_switch(struct rfkill *rfkill)
589 {
590         int error;
591
592         mutex_lock(&rfkill_global_mutex);
593
594         error = rfkill_check_duplicity(rfkill);
595         if (error < 0)
596                 goto unlock_out;
597
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;
603         }
604
605         rfkill_toggle_radio(rfkill,
606                             rfkill_global_states[rfkill->type].current_state,
607                             0);
608
609         list_add_tail(&rfkill->node, &rfkill_list);
610
611         error = 0;
612 unlock_out:
613         mutex_unlock(&rfkill_global_mutex);
614
615         return error;
616 }
617
618 static void rfkill_remove_switch(struct rfkill *rfkill)
619 {
620         mutex_lock(&rfkill_global_mutex);
621         list_del_init(&rfkill->node);
622         mutex_unlock(&rfkill_global_mutex);
623
624         mutex_lock(&rfkill->mutex);
625         rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
626         mutex_unlock(&rfkill->mutex);
627 }
628
629 /**
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_*)
633  *
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().
638  *
639  * NOTE: If registration fails the structure shoudl be freed by calling
640  * rfkill_free() otherwise rfkill_unregister() should be used.
641  */
642 struct rfkill * __must_check rfkill_allocate(struct device *parent,
643                                              enum rfkill_type type)
644 {
645         struct rfkill *rfkill;
646         struct device *dev;
647
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;
653
654         rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
655         if (!rfkill)
656                 return NULL;
657
658         mutex_init(&rfkill->mutex);
659         INIT_LIST_HEAD(&rfkill->node);
660         rfkill->type = type;
661
662         dev = &rfkill->dev;
663         dev->class = &rfkill_class;
664         dev->parent = parent;
665         device_initialize(dev);
666
667         __module_get(THIS_MODULE);
668
669         return rfkill;
670 }
671 EXPORT_SYMBOL(rfkill_allocate);
672
673 /**
674  * rfkill_free - Mark rfkill structure for deletion
675  * @rfkill: rfkill structure to be destroyed
676  *
677  * Decrements reference count of the rfkill structure so it is destroyed.
678  * Note that rfkill_free() should _not_ be called after rfkill_unregister().
679  */
680 void rfkill_free(struct rfkill *rfkill)
681 {
682         if (rfkill)
683                 put_device(&rfkill->dev);
684 }
685 EXPORT_SYMBOL(rfkill_free);
686
687 static void rfkill_led_trigger_register(struct rfkill *rfkill)
688 {
689 #ifdef CONFIG_RFKILL_LEDS
690         int error;
691
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 */
700 }
701
702 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
703 {
704 #ifdef CONFIG_RFKILL_LEDS
705         if (rfkill->led_trigger.name) {
706                 led_trigger_unregister(&rfkill->led_trigger);
707                 rfkill->led_trigger.name = NULL;
708         }
709 #endif
710 }
711
712 /**
713  * rfkill_register - Register a rfkill structure.
714  * @rfkill: rfkill structure to be registered
715  *
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.
719  */
720 int __must_check rfkill_register(struct rfkill *rfkill)
721 {
722         static atomic_t rfkill_no = ATOMIC_INIT(0);
723         struct device *dev = &rfkill->dev;
724         int error;
725
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;
733
734         dev_set_name(dev, "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
735
736         rfkill_led_trigger_register(rfkill);
737
738         error = rfkill_add_switch(rfkill);
739         if (error) {
740                 rfkill_led_trigger_unregister(rfkill);
741                 return error;
742         }
743
744         error = device_add(dev);
745         if (error) {
746                 rfkill_remove_switch(rfkill);
747                 rfkill_led_trigger_unregister(rfkill);
748                 return error;
749         }
750
751         return 0;
752 }
753 EXPORT_SYMBOL(rfkill_register);
754
755 /**
756  * rfkill_unregister - Unregister a rfkill structure.
757  * @rfkill: rfkill structure to be unregistered
758  *
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().
762  */
763 void rfkill_unregister(struct rfkill *rfkill)
764 {
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);
770 }
771 EXPORT_SYMBOL(rfkill_unregister);
772
773 /**
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
777  *
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.
781  *
782  * This function is meant to be used by platform drivers for platforms
783  * that can save switch state across power down/reboot.
784  *
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.
790  *
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.
794  *
795  * Returns 0 if the new default state was set, or an error if it
796  * could not be set.
797  */
798 int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
799 {
800         int error;
801
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;
809
810         mutex_lock(&rfkill_global_mutex);
811
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;
818
819         mutex_unlock(&rfkill_global_mutex);
820         return error;
821 }
822 EXPORT_SYMBOL_GPL(rfkill_set_default);
823
824 /*
825  * Rfkill module initialization/deinitialization.
826  */
827 static int __init rfkill_init(void)
828 {
829         int error;
830         int i;
831
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;
836
837         for (i = 0; i < RFKILL_TYPE_MAX; i++)
838                 rfkill_global_states[i].default_state = rfkill_default_state;
839
840         error = class_register(&rfkill_class);
841         if (error) {
842                 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
843                 return error;
844         }
845
846         return 0;
847 }
848
849 static void __exit rfkill_exit(void)
850 {
851         class_unregister(&rfkill_class);
852 }
853
854 subsys_initcall(rfkill_init);
855 module_exit(rfkill_exit);