rfkill: Fix broken rfkill LED in 2.6.30-rc1
[linux-2.6.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 #endif /* CONFIG_RFKILL_LEDS */
79
80 static void rfkill_uevent(struct rfkill *rfkill)
81 {
82         kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
83 }
84
85 static void update_rfkill_state(struct rfkill *rfkill)
86 {
87         enum rfkill_state newstate, oldstate;
88
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);
96                 }
97                 mutex_unlock(&rfkill->mutex);
98         }
99         rfkill_led_trigger(rfkill, rfkill->state);
100 }
101
102 /**
103  * rfkill_toggle_radio - wrapper for toggle_radio hook
104  * @rfkill: the rfkill struct to use
105  * @force: calls toggle_radio even if cache says it is not needed,
106  *      and also makes sure notifications of the state will be
107  *      sent even if it didn't change
108  * @state: the new state to call toggle_radio() with
109  *
110  * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
111  * calls and handling all the red tape such as issuing notifications
112  * if the call is successful.
113  *
114  * Suspended devices are not touched at all, and -EAGAIN is returned.
115  *
116  * Note that the @force parameter cannot override a (possibly cached)
117  * state of RFKILL_STATE_HARD_BLOCKED.  Any device making use of
118  * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
119  * rfkill_force_state(), so the cache either is bypassed or valid.
120  *
121  * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
122  * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
123  * give the driver a hint that it should double-BLOCK the transmitter.
124  *
125  * Caller must have acquired rfkill->mutex.
126  */
127 static int rfkill_toggle_radio(struct rfkill *rfkill,
128                                 enum rfkill_state state,
129                                 int force)
130 {
131         int retval = 0;
132         enum rfkill_state oldstate, newstate;
133
134         if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
135                 return -EBUSY;
136
137         oldstate = rfkill->state;
138
139         if (rfkill->get_state && !force &&
140             !rfkill->get_state(rfkill->data, &newstate)) {
141                 rfkill->state = newstate;
142         }
143
144         switch (state) {
145         case RFKILL_STATE_HARD_BLOCKED:
146                 /* typically happens when refreshing hardware state,
147                  * such as on resume */
148                 state = RFKILL_STATE_SOFT_BLOCKED;
149                 break;
150         case RFKILL_STATE_UNBLOCKED:
151                 /* force can't override this, only rfkill_force_state() can */
152                 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
153                         return -EPERM;
154                 break;
155         case RFKILL_STATE_SOFT_BLOCKED:
156                 /* nothing to do, we want to give drivers the hint to double
157                  * BLOCK even a transmitter that is already in state
158                  * RFKILL_STATE_HARD_BLOCKED */
159                 break;
160         default:
161                 WARN(1, KERN_WARNING
162                         "rfkill: illegal state %d passed as parameter "
163                         "to rfkill_toggle_radio\n", state);
164                 return -EINVAL;
165         }
166
167         if (force || state != rfkill->state) {
168                 retval = rfkill->toggle_radio(rfkill->data, state);
169                 /* never allow a HARD->SOFT downgrade! */
170                 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
171                         rfkill->state = state;
172         }
173
174         if (force || rfkill->state != oldstate)
175                 rfkill_uevent(rfkill);
176
177         rfkill_led_trigger(rfkill, rfkill->state);
178         return retval;
179 }
180
181 /**
182  * __rfkill_switch_all - Toggle state of all switches of given type
183  * @type: type of interfaces to be affected
184  * @state: the new state
185  *
186  * This function toggles the state of all switches of given type,
187  * unless a specific switch is claimed by userspace (in which case,
188  * that switch is left alone) or suspended.
189  *
190  * Caller must have acquired rfkill_global_mutex.
191  */
192 static void __rfkill_switch_all(const enum rfkill_type type,
193                                 const enum rfkill_state state)
194 {
195         struct rfkill *rfkill;
196
197         if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
198                         KERN_WARNING
199                         "rfkill: illegal state %d or type %d "
200                         "passed as parameter to __rfkill_switch_all\n",
201                         state, type))
202                 return;
203
204         rfkill_global_states[type].current_state = state;
205         list_for_each_entry(rfkill, &rfkill_list, node) {
206                 if (rfkill->type == type) {
207                         mutex_lock(&rfkill->mutex);
208                         rfkill_toggle_radio(rfkill, state, 0);
209                         mutex_unlock(&rfkill->mutex);
210                         rfkill_led_trigger(rfkill, rfkill->state);
211                 }
212         }
213 }
214
215 /**
216  * rfkill_switch_all - Toggle state of all switches of given type
217  * @type: type of interfaces to be affected
218  * @state: the new state
219  *
220  * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
221  * Please refer to __rfkill_switch_all() for details.
222  *
223  * Does nothing if the EPO lock is active.
224  */
225 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
226 {
227         mutex_lock(&rfkill_global_mutex);
228         if (!rfkill_epo_lock_active)
229                 __rfkill_switch_all(type, state);
230         mutex_unlock(&rfkill_global_mutex);
231 }
232 EXPORT_SYMBOL(rfkill_switch_all);
233
234 /**
235  * rfkill_epo - emergency power off all transmitters
236  *
237  * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
238  * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
239  *
240  * The global state before the EPO is saved and can be restored later
241  * using rfkill_restore_states().
242  */
243 void rfkill_epo(void)
244 {
245         struct rfkill *rfkill;
246         int i;
247
248         mutex_lock(&rfkill_global_mutex);
249
250         rfkill_epo_lock_active = true;
251         list_for_each_entry(rfkill, &rfkill_list, node) {
252                 mutex_lock(&rfkill->mutex);
253                 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
254                 mutex_unlock(&rfkill->mutex);
255         }
256         for (i = 0; i < RFKILL_TYPE_MAX; i++) {
257                 rfkill_global_states[i].default_state =
258                                 rfkill_global_states[i].current_state;
259                 rfkill_global_states[i].current_state =
260                                 RFKILL_STATE_SOFT_BLOCKED;
261         }
262         mutex_unlock(&rfkill_global_mutex);
263         rfkill_led_trigger(rfkill, rfkill->state);
264 }
265 EXPORT_SYMBOL_GPL(rfkill_epo);
266
267 /**
268  * rfkill_restore_states - restore global states
269  *
270  * Restore (and sync switches to) the global state from the
271  * states in rfkill_default_states.  This can undo the effects of
272  * a call to rfkill_epo().
273  */
274 void rfkill_restore_states(void)
275 {
276         int i;
277
278         mutex_lock(&rfkill_global_mutex);
279
280         rfkill_epo_lock_active = false;
281         for (i = 0; i < RFKILL_TYPE_MAX; i++)
282                 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
283         mutex_unlock(&rfkill_global_mutex);
284 }
285 EXPORT_SYMBOL_GPL(rfkill_restore_states);
286
287 /**
288  * rfkill_remove_epo_lock - unlock state changes
289  *
290  * Used by rfkill-input manually unlock state changes, when
291  * the EPO switch is deactivated.
292  */
293 void rfkill_remove_epo_lock(void)
294 {
295         mutex_lock(&rfkill_global_mutex);
296         rfkill_epo_lock_active = false;
297         mutex_unlock(&rfkill_global_mutex);
298 }
299 EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock);
300
301 /**
302  * rfkill_is_epo_lock_active - returns true EPO is active
303  *
304  * Returns 0 (false) if there is NOT an active EPO contidion,
305  * and 1 (true) if there is an active EPO contition, which
306  * locks all radios in one of the BLOCKED states.
307  *
308  * Can be called in atomic context.
309  */
310 bool rfkill_is_epo_lock_active(void)
311 {
312         return rfkill_epo_lock_active;
313 }
314 EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active);
315
316 /**
317  * rfkill_get_global_state - returns global state for a type
318  * @type: the type to get the global state of
319  *
320  * Returns the current global state for a given wireless
321  * device type.
322  */
323 enum rfkill_state rfkill_get_global_state(const enum rfkill_type type)
324 {
325         return rfkill_global_states[type].current_state;
326 }
327 EXPORT_SYMBOL_GPL(rfkill_get_global_state);
328
329 /**
330  * rfkill_force_state - Force the internal rfkill radio state
331  * @rfkill: pointer to the rfkill class to modify.
332  * @state: the current radio state the class should be forced to.
333  *
334  * This function updates the internal state of the radio cached
335  * by the rfkill class.  It should be used when the driver gets
336  * a notification by the firmware/hardware of the current *real*
337  * state of the radio rfkill switch.
338  *
339  * Devices which are subject to external changes on their rfkill
340  * state (such as those caused by a hardware rfkill line) MUST
341  * have their driver arrange to call rfkill_force_state() as soon
342  * as possible after such a change.
343  *
344  * This function may not be called from an atomic context.
345  */
346 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
347 {
348         enum rfkill_state oldstate;
349
350         BUG_ON(!rfkill);
351         if (WARN((state >= RFKILL_STATE_MAX),
352                         KERN_WARNING
353                         "rfkill: illegal state %d passed as parameter "
354                         "to rfkill_force_state\n", state))
355                 return -EINVAL;
356
357         mutex_lock(&rfkill->mutex);
358
359         oldstate = rfkill->state;
360         rfkill->state = state;
361
362         if (state != oldstate)
363                 rfkill_uevent(rfkill);
364
365         mutex_unlock(&rfkill->mutex);
366         rfkill_led_trigger(rfkill, rfkill->state);
367
368         return 0;
369 }
370 EXPORT_SYMBOL(rfkill_force_state);
371
372 static ssize_t rfkill_name_show(struct device *dev,
373                                 struct device_attribute *attr,
374                                 char *buf)
375 {
376         struct rfkill *rfkill = to_rfkill(dev);
377
378         return sprintf(buf, "%s\n", rfkill->name);
379 }
380
381 static const char *rfkill_get_type_str(enum rfkill_type type)
382 {
383         switch (type) {
384         case RFKILL_TYPE_WLAN:
385                 return "wlan";
386         case RFKILL_TYPE_BLUETOOTH:
387                 return "bluetooth";
388         case RFKILL_TYPE_UWB:
389                 return "ultrawideband";
390         case RFKILL_TYPE_WIMAX:
391                 return "wimax";
392         case RFKILL_TYPE_WWAN:
393                 return "wwan";
394         default:
395                 BUG();
396         }
397 }
398
399 static ssize_t rfkill_type_show(struct device *dev,
400                                 struct device_attribute *attr,
401                                 char *buf)
402 {
403         struct rfkill *rfkill = to_rfkill(dev);
404
405         return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
406 }
407
408 static ssize_t rfkill_state_show(struct device *dev,
409                                  struct device_attribute *attr,
410                                  char *buf)
411 {
412         struct rfkill *rfkill = to_rfkill(dev);
413
414         update_rfkill_state(rfkill);
415         return sprintf(buf, "%d\n", rfkill->state);
416 }
417
418 static ssize_t rfkill_state_store(struct device *dev,
419                                   struct device_attribute *attr,
420                                   const char *buf, size_t count)
421 {
422         struct rfkill *rfkill = to_rfkill(dev);
423         unsigned long state;
424         int error;
425
426         if (!capable(CAP_NET_ADMIN))
427                 return -EPERM;
428
429         error = strict_strtoul(buf, 0, &state);
430         if (error)
431                 return error;
432
433         /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
434         if (state != RFKILL_STATE_UNBLOCKED &&
435             state != RFKILL_STATE_SOFT_BLOCKED)
436                 return -EINVAL;
437
438         error = mutex_lock_killable(&rfkill->mutex);
439         if (error)
440                 return error;
441
442         if (!rfkill_epo_lock_active)
443                 error = rfkill_toggle_radio(rfkill, state, 0);
444         else
445                 error = -EPERM;
446
447         mutex_unlock(&rfkill->mutex);
448
449         return error ? error : count;
450 }
451
452 static ssize_t rfkill_claim_show(struct device *dev,
453                                  struct device_attribute *attr,
454                                  char *buf)
455 {
456         return sprintf(buf, "%d\n", 0);
457 }
458
459 static ssize_t rfkill_claim_store(struct device *dev,
460                                   struct device_attribute *attr,
461                                   const char *buf, size_t count)
462 {
463         return -EOPNOTSUPP;
464 }
465
466 static struct device_attribute rfkill_dev_attrs[] = {
467         __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
468         __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
469         __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
470         __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
471         __ATTR_NULL
472 };
473
474 static void rfkill_release(struct device *dev)
475 {
476         struct rfkill *rfkill = to_rfkill(dev);
477
478         kfree(rfkill);
479         module_put(THIS_MODULE);
480 }
481
482 #ifdef CONFIG_PM
483 static int rfkill_suspend(struct device *dev, pm_message_t state)
484 {
485         struct rfkill *rfkill = to_rfkill(dev);
486
487         /* mark class device as suspended */
488         if (dev->power.power_state.event != state.event)
489                 dev->power.power_state = state;
490
491         /* store state for the resume handler */
492         rfkill->state_for_resume = rfkill->state;
493
494         return 0;
495 }
496
497 static int rfkill_resume(struct device *dev)
498 {
499         struct rfkill *rfkill = to_rfkill(dev);
500         enum rfkill_state newstate;
501
502         if (dev->power.power_state.event != PM_EVENT_ON) {
503                 mutex_lock(&rfkill->mutex);
504
505                 dev->power.power_state.event = PM_EVENT_ON;
506
507                 /*
508                  * rfkill->state could have been modified before we got
509                  * called, and won't be updated by rfkill_toggle_radio()
510                  * in force mode.  Sync it FIRST.
511                  */
512                 if (rfkill->get_state &&
513                     !rfkill->get_state(rfkill->data, &newstate))
514                         rfkill->state = newstate;
515
516                 /*
517                  * If we are under EPO, kick transmitter offline,
518                  * otherwise restore to pre-suspend state.
519                  *
520                  * Issue a notification in any case
521                  */
522                 rfkill_toggle_radio(rfkill,
523                                 rfkill_epo_lock_active ?
524                                         RFKILL_STATE_SOFT_BLOCKED :
525                                         rfkill->state_for_resume,
526                                 1);
527
528                 mutex_unlock(&rfkill->mutex);
529                 rfkill_led_trigger(rfkill, rfkill->state);
530         }
531
532         return 0;
533 }
534 #else
535 #define rfkill_suspend NULL
536 #define rfkill_resume NULL
537 #endif
538
539 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
540 {
541         struct rfkill *rfkill = to_rfkill(dev);
542         int error;
543
544         error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
545         if (error)
546                 return error;
547         error = add_uevent_var(env, "RFKILL_TYPE=%s",
548                                 rfkill_get_type_str(rfkill->type));
549         if (error)
550                 return error;
551         error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
552         return error;
553 }
554
555 static struct class rfkill_class = {
556         .name           = "rfkill",
557         .dev_release    = rfkill_release,
558         .dev_attrs      = rfkill_dev_attrs,
559         .suspend        = rfkill_suspend,
560         .resume         = rfkill_resume,
561         .dev_uevent     = rfkill_dev_uevent,
562 };
563
564 static int rfkill_check_duplicity(const struct rfkill *rfkill)
565 {
566         struct rfkill *p;
567         unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
568
569         memset(seen, 0, sizeof(seen));
570
571         list_for_each_entry(p, &rfkill_list, node) {
572                 if (WARN((p == rfkill), KERN_WARNING
573                                 "rfkill: illegal attempt to register "
574                                 "an already registered rfkill struct\n"))
575                         return -EEXIST;
576                 set_bit(p->type, seen);
577         }
578
579         /* 0: first switch of its kind */
580         return (test_bit(rfkill->type, seen)) ? 1 : 0;
581 }
582
583 static int rfkill_add_switch(struct rfkill *rfkill)
584 {
585         int error;
586
587         mutex_lock(&rfkill_global_mutex);
588
589         error = rfkill_check_duplicity(rfkill);
590         if (error < 0)
591                 goto unlock_out;
592
593         if (!error) {
594                 /* lock default after first use */
595                 set_bit(rfkill->type, rfkill_states_lockdflt);
596                 rfkill_global_states[rfkill->type].current_state =
597                         rfkill_global_states[rfkill->type].default_state;
598         }
599
600         rfkill_toggle_radio(rfkill,
601                             rfkill_global_states[rfkill->type].current_state,
602                             0);
603
604         list_add_tail(&rfkill->node, &rfkill_list);
605
606         error = 0;
607 unlock_out:
608         mutex_unlock(&rfkill_global_mutex);
609
610         return error;
611 }
612
613 static void rfkill_remove_switch(struct rfkill *rfkill)
614 {
615         mutex_lock(&rfkill_global_mutex);
616         list_del_init(&rfkill->node);
617         mutex_unlock(&rfkill_global_mutex);
618
619         mutex_lock(&rfkill->mutex);
620         rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
621         mutex_unlock(&rfkill->mutex);
622 }
623
624 /**
625  * rfkill_allocate - allocate memory for rfkill structure.
626  * @parent: device that has rf switch on it
627  * @type: type of the switch (RFKILL_TYPE_*)
628  *
629  * This function should be called by the network driver when it needs
630  * rfkill structure.  Once the structure is allocated the driver should
631  * finish its initialization by setting the name, private data, enable_radio
632  * and disable_radio methods and then register it with rfkill_register().
633  *
634  * NOTE: If registration fails the structure shoudl be freed by calling
635  * rfkill_free() otherwise rfkill_unregister() should be used.
636  */
637 struct rfkill * __must_check rfkill_allocate(struct device *parent,
638                                              enum rfkill_type type)
639 {
640         struct rfkill *rfkill;
641         struct device *dev;
642
643         if (WARN((type >= RFKILL_TYPE_MAX),
644                         KERN_WARNING
645                         "rfkill: illegal type %d passed as parameter "
646                         "to rfkill_allocate\n", type))
647                 return NULL;
648
649         rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
650         if (!rfkill)
651                 return NULL;
652
653         mutex_init(&rfkill->mutex);
654         INIT_LIST_HEAD(&rfkill->node);
655         rfkill->type = type;
656
657         dev = &rfkill->dev;
658         dev->class = &rfkill_class;
659         dev->parent = parent;
660         device_initialize(dev);
661
662         __module_get(THIS_MODULE);
663
664         return rfkill;
665 }
666 EXPORT_SYMBOL(rfkill_allocate);
667
668 /**
669  * rfkill_free - Mark rfkill structure for deletion
670  * @rfkill: rfkill structure to be destroyed
671  *
672  * Decrements reference count of the rfkill structure so it is destroyed.
673  * Note that rfkill_free() should _not_ be called after rfkill_unregister().
674  */
675 void rfkill_free(struct rfkill *rfkill)
676 {
677         if (rfkill)
678                 put_device(&rfkill->dev);
679 }
680 EXPORT_SYMBOL(rfkill_free);
681
682 static void rfkill_led_trigger_register(struct rfkill *rfkill)
683 {
684 #ifdef CONFIG_RFKILL_LEDS
685         int error;
686
687         if (!rfkill->led_trigger.name)
688                 rfkill->led_trigger.name = dev_name(&rfkill->dev);
689         if (!rfkill->led_trigger.activate)
690                 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
691         error = led_trigger_register(&rfkill->led_trigger);
692         if (error)
693                 rfkill->led_trigger.name = NULL;
694 #endif /* CONFIG_RFKILL_LEDS */
695 }
696
697 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
698 {
699 #ifdef CONFIG_RFKILL_LEDS
700         if (rfkill->led_trigger.name) {
701                 led_trigger_unregister(&rfkill->led_trigger);
702                 rfkill->led_trigger.name = NULL;
703         }
704 #endif
705 }
706
707 /**
708  * rfkill_register - Register a rfkill structure.
709  * @rfkill: rfkill structure to be registered
710  *
711  * This function should be called by the network driver when the rfkill
712  * structure needs to be registered. Immediately from registration the
713  * switch driver should be able to service calls to toggle_radio.
714  */
715 int __must_check rfkill_register(struct rfkill *rfkill)
716 {
717         static atomic_t rfkill_no = ATOMIC_INIT(0);
718         struct device *dev = &rfkill->dev;
719         int error;
720
721         if (WARN((!rfkill || !rfkill->toggle_radio ||
722                         rfkill->type >= RFKILL_TYPE_MAX ||
723                         rfkill->state >= RFKILL_STATE_MAX),
724                         KERN_WARNING
725                         "rfkill: attempt to register a "
726                         "badly initialized rfkill struct\n"))
727                 return -EINVAL;
728
729         dev_set_name(dev, "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
730
731         rfkill_led_trigger_register(rfkill);
732
733         error = rfkill_add_switch(rfkill);
734         if (error) {
735                 rfkill_led_trigger_unregister(rfkill);
736                 return error;
737         }
738
739         error = device_add(dev);
740         if (error) {
741                 rfkill_remove_switch(rfkill);
742                 rfkill_led_trigger_unregister(rfkill);
743                 return error;
744         }
745
746         return 0;
747 }
748 EXPORT_SYMBOL(rfkill_register);
749
750 /**
751  * rfkill_unregister - Unregister a rfkill structure.
752  * @rfkill: rfkill structure to be unregistered
753  *
754  * This function should be called by the network driver during device
755  * teardown to destroy rfkill structure. Note that rfkill_free() should
756  * _not_ be called after rfkill_unregister().
757  */
758 void rfkill_unregister(struct rfkill *rfkill)
759 {
760         BUG_ON(!rfkill);
761         device_del(&rfkill->dev);
762         rfkill_remove_switch(rfkill);
763         rfkill_led_trigger_unregister(rfkill);
764         put_device(&rfkill->dev);
765 }
766 EXPORT_SYMBOL(rfkill_unregister);
767
768 /**
769  * rfkill_set_default - set initial value for a switch type
770  * @type - the type of switch to set the default state of
771  * @state - the new default state for that group of switches
772  *
773  * Sets the initial state rfkill should use for a given type.
774  * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
775  * and RFKILL_STATE_UNBLOCKED.
776  *
777  * This function is meant to be used by platform drivers for platforms
778  * that can save switch state across power down/reboot.
779  *
780  * The default state for each switch type can be changed exactly once.
781  * After a switch of that type is registered, the default state cannot
782  * be changed anymore.  This guards against multiple drivers it the
783  * same platform trying to set the initial switch default state, which
784  * is not allowed.
785  *
786  * Returns -EPERM if the state has already been set once or is in use,
787  * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
788  * if this function returns -EPERM.
789  *
790  * Returns 0 if the new default state was set, or an error if it
791  * could not be set.
792  */
793 int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
794 {
795         int error;
796
797         if (WARN((type >= RFKILL_TYPE_MAX ||
798                         (state != RFKILL_STATE_SOFT_BLOCKED &&
799                          state != RFKILL_STATE_UNBLOCKED)),
800                         KERN_WARNING
801                         "rfkill: illegal state %d or type %d passed as "
802                         "parameter to rfkill_set_default\n", state, type))
803                 return -EINVAL;
804
805         mutex_lock(&rfkill_global_mutex);
806
807         if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
808                 rfkill_global_states[type].default_state = state;
809                 rfkill_global_states[type].current_state = state;
810                 error = 0;
811         } else
812                 error = -EPERM;
813
814         mutex_unlock(&rfkill_global_mutex);
815         return error;
816 }
817 EXPORT_SYMBOL_GPL(rfkill_set_default);
818
819 /*
820  * Rfkill module initialization/deinitialization.
821  */
822 static int __init rfkill_init(void)
823 {
824         int error;
825         int i;
826
827         /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
828         if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
829             rfkill_default_state != RFKILL_STATE_UNBLOCKED)
830                 return -EINVAL;
831
832         for (i = 0; i < RFKILL_TYPE_MAX; i++)
833                 rfkill_global_states[i].default_state = rfkill_default_state;
834
835         error = class_register(&rfkill_class);
836         if (error) {
837                 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
838                 return error;
839         }
840
841         return 0;
842 }
843
844 static void __exit rfkill_exit(void)
845 {
846         class_unregister(&rfkill_class);
847 }
848
849 subsys_initcall(rfkill_init);
850 module_exit(rfkill_exit);