1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
31#include "rfkill-input.h"
32
33
34MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35MODULE_VERSION("1.0");
36MODULE_DESCRIPTION("RF switch support");
37MODULE_LICENSE("GPL");
38
39static LIST_HEAD(rfkill_list);
40static DEFINE_MUTEX(rfkill_global_mutex);
41
42static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
43module_param_named(default_state, rfkill_default_state, uint, 0444);
44MODULE_PARM_DESC(default_state,
45 "Default initial state for all radio types, 0 = radio off");
46
47struct rfkill_gsw_state {
48 enum rfkill_state current_state;
49 enum rfkill_state default_state;
50};
51
52static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX];
53static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
54
55static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77int register_rfkill_notifier(struct notifier_block *nb)
78{
79 BUG_ON(!nb);
80 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
81}
82EXPORT_SYMBOL_GPL(register_rfkill_notifier);
83
84
85
86
87
88
89
90
91
92
93int unregister_rfkill_notifier(struct notifier_block *nb)
94{
95 BUG_ON(!nb);
96 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
97}
98EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
99
100
101static void rfkill_led_trigger(struct rfkill *rfkill,
102 enum rfkill_state state)
103{
104#ifdef CONFIG_RFKILL_LEDS
105 struct led_trigger *led = &rfkill->led_trigger;
106
107 if (!led->name)
108 return;
109 if (state != RFKILL_STATE_UNBLOCKED)
110 led_trigger_event(led, LED_OFF);
111 else
112 led_trigger_event(led, LED_FULL);
113#endif
114}
115
116#ifdef CONFIG_RFKILL_LEDS
117static void rfkill_led_trigger_activate(struct led_classdev *led)
118{
119 struct rfkill *rfkill = container_of(led->trigger,
120 struct rfkill, led_trigger);
121
122 rfkill_led_trigger(rfkill, rfkill->state);
123}
124#endif
125
126static void notify_rfkill_state_change(struct rfkill *rfkill)
127{
128 rfkill_led_trigger(rfkill, rfkill->state);
129 blocking_notifier_call_chain(&rfkill_notifier_list,
130 RFKILL_STATE_CHANGED,
131 rfkill);
132}
133
134static void update_rfkill_state(struct rfkill *rfkill)
135{
136 enum rfkill_state newstate, oldstate;
137
138 if (rfkill->get_state) {
139 mutex_lock(&rfkill->mutex);
140 if (!rfkill->get_state(rfkill->data, &newstate)) {
141 oldstate = rfkill->state;
142 rfkill->state = newstate;
143 if (oldstate != newstate)
144 notify_rfkill_state_change(rfkill);
145 }
146 mutex_unlock(&rfkill->mutex);
147 }
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175static int rfkill_toggle_radio(struct rfkill *rfkill,
176 enum rfkill_state state,
177 int force)
178{
179 int retval = 0;
180 enum rfkill_state oldstate, newstate;
181
182 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
183 return -EBUSY;
184
185 oldstate = rfkill->state;
186
187 if (rfkill->get_state && !force &&
188 !rfkill->get_state(rfkill->data, &newstate))
189 rfkill->state = newstate;
190
191 switch (state) {
192 case RFKILL_STATE_HARD_BLOCKED:
193
194
195 state = RFKILL_STATE_SOFT_BLOCKED;
196 break;
197 case RFKILL_STATE_UNBLOCKED:
198
199 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
200 return -EPERM;
201 break;
202 case RFKILL_STATE_SOFT_BLOCKED:
203
204
205
206 break;
207 default:
208 WARN(1, KERN_WARNING
209 "rfkill: illegal state %d passed as parameter "
210 "to rfkill_toggle_radio\n", state);
211 return -EINVAL;
212 }
213
214 if (force || state != rfkill->state) {
215 retval = rfkill->toggle_radio(rfkill->data, state);
216
217 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
218 rfkill->state = state;
219 }
220
221 if (force || rfkill->state != oldstate)
222 notify_rfkill_state_change(rfkill);
223
224 return retval;
225}
226
227
228
229
230
231
232
233
234
235
236
237
238static void __rfkill_switch_all(const enum rfkill_type type,
239 const enum rfkill_state state)
240{
241 struct rfkill *rfkill;
242
243 if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
244 KERN_WARNING
245 "rfkill: illegal state %d or type %d "
246 "passed as parameter to __rfkill_switch_all\n",
247 state, type))
248 return;
249
250 rfkill_global_states[type].current_state = state;
251 list_for_each_entry(rfkill, &rfkill_list, node) {
252 if ((!rfkill->user_claim) && (rfkill->type == type)) {
253 mutex_lock(&rfkill->mutex);
254 rfkill_toggle_radio(rfkill, state, 0);
255 mutex_unlock(&rfkill->mutex);
256 }
257 }
258}
259
260
261
262
263
264
265
266
267
268void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
269{
270 mutex_lock(&rfkill_global_mutex);
271 __rfkill_switch_all(type, state);
272 mutex_unlock(&rfkill_global_mutex);
273}
274EXPORT_SYMBOL(rfkill_switch_all);
275
276
277
278
279
280
281
282
283
284
285void rfkill_epo(void)
286{
287 struct rfkill *rfkill;
288 int i;
289
290 mutex_lock(&rfkill_global_mutex);
291
292 list_for_each_entry(rfkill, &rfkill_list, node) {
293 mutex_lock(&rfkill->mutex);
294 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
295 mutex_unlock(&rfkill->mutex);
296 }
297 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
298 rfkill_global_states[i].default_state =
299 rfkill_global_states[i].current_state;
300 rfkill_global_states[i].current_state =
301 RFKILL_STATE_SOFT_BLOCKED;
302 }
303 mutex_unlock(&rfkill_global_mutex);
304}
305EXPORT_SYMBOL_GPL(rfkill_epo);
306
307
308
309
310
311
312
313
314void rfkill_restore_states(void)
315{
316 int i;
317
318 mutex_lock(&rfkill_global_mutex);
319
320 for (i = 0; i < RFKILL_TYPE_MAX; i++)
321 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
322 mutex_unlock(&rfkill_global_mutex);
323}
324EXPORT_SYMBOL_GPL(rfkill_restore_states);
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
344{
345 enum rfkill_state oldstate;
346
347 BUG_ON(!rfkill);
348 if (WARN((state >= RFKILL_STATE_MAX),
349 KERN_WARNING
350 "rfkill: illegal state %d passed as parameter "
351 "to rfkill_force_state\n", state))
352 return -EINVAL;
353
354 mutex_lock(&rfkill->mutex);
355
356 oldstate = rfkill->state;
357 rfkill->state = state;
358
359 if (state != oldstate)
360 notify_rfkill_state_change(rfkill);
361
362 mutex_unlock(&rfkill->mutex);
363
364 return 0;
365}
366EXPORT_SYMBOL(rfkill_force_state);
367
368static ssize_t rfkill_name_show(struct device *dev,
369 struct device_attribute *attr,
370 char *buf)
371{
372 struct rfkill *rfkill = to_rfkill(dev);
373
374 return sprintf(buf, "%s\n", rfkill->name);
375}
376
377static const char *rfkill_get_type_str(enum rfkill_type type)
378{
379 switch (type) {
380 case RFKILL_TYPE_WLAN:
381 return "wlan";
382 case RFKILL_TYPE_BLUETOOTH:
383 return "bluetooth";
384 case RFKILL_TYPE_UWB:
385 return "ultrawideband";
386 case RFKILL_TYPE_WIMAX:
387 return "wimax";
388 case RFKILL_TYPE_WWAN:
389 return "wwan";
390 default:
391 BUG();
392 }
393}
394
395static ssize_t rfkill_type_show(struct device *dev,
396 struct device_attribute *attr,
397 char *buf)
398{
399 struct rfkill *rfkill = to_rfkill(dev);
400
401 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
402}
403
404static ssize_t rfkill_state_show(struct device *dev,
405 struct device_attribute *attr,
406 char *buf)
407{
408 struct rfkill *rfkill = to_rfkill(dev);
409
410 update_rfkill_state(rfkill);
411 return sprintf(buf, "%d\n", rfkill->state);
412}
413
414static ssize_t rfkill_state_store(struct device *dev,
415 struct device_attribute *attr,
416 const char *buf, size_t count)
417{
418 struct rfkill *rfkill = to_rfkill(dev);
419 unsigned long state;
420 int error;
421
422 if (!capable(CAP_NET_ADMIN))
423 return -EPERM;
424
425 error = strict_strtoul(buf, 0, &state);
426 if (error)
427 return error;
428
429
430 if (state != RFKILL_STATE_UNBLOCKED &&
431 state != RFKILL_STATE_SOFT_BLOCKED)
432 return -EINVAL;
433
434 if (mutex_lock_interruptible(&rfkill->mutex))
435 return -ERESTARTSYS;
436 error = rfkill_toggle_radio(rfkill, state, 0);
437 mutex_unlock(&rfkill->mutex);
438
439 return error ? error : count;
440}
441
442static ssize_t rfkill_claim_show(struct device *dev,
443 struct device_attribute *attr,
444 char *buf)
445{
446 struct rfkill *rfkill = to_rfkill(dev);
447
448 return sprintf(buf, "%d\n", rfkill->user_claim);
449}
450
451static ssize_t rfkill_claim_store(struct device *dev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
454{
455 struct rfkill *rfkill = to_rfkill(dev);
456 unsigned long claim_tmp;
457 bool claim;
458 int error;
459
460 if (!capable(CAP_NET_ADMIN))
461 return -EPERM;
462
463 if (rfkill->user_claim_unsupported)
464 return -EOPNOTSUPP;
465
466 error = strict_strtoul(buf, 0, &claim_tmp);
467 if (error)
468 return error;
469 claim = !!claim_tmp;
470
471
472
473
474
475 error = mutex_lock_interruptible(&rfkill_global_mutex);
476 if (error)
477 return error;
478
479 if (rfkill->user_claim != claim) {
480 if (!claim) {
481 mutex_lock(&rfkill->mutex);
482 rfkill_toggle_radio(rfkill,
483 rfkill_global_states[rfkill->type].current_state,
484 0);
485 mutex_unlock(&rfkill->mutex);
486 }
487 rfkill->user_claim = claim;
488 }
489
490 mutex_unlock(&rfkill_global_mutex);
491
492 return error ? error : count;
493}
494
495static struct device_attribute rfkill_dev_attrs[] = {
496 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
497 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
498 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
499 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
500 __ATTR_NULL
501};
502
503static void rfkill_release(struct device *dev)
504{
505 struct rfkill *rfkill = to_rfkill(dev);
506
507 kfree(rfkill);
508 module_put(THIS_MODULE);
509}
510
511#ifdef CONFIG_PM
512static int rfkill_suspend(struct device *dev, pm_message_t state)
513{
514
515 if (dev->power.power_state.event != state.event)
516 dev->power.power_state = state;
517
518 return 0;
519}
520
521static int rfkill_resume(struct device *dev)
522{
523 struct rfkill *rfkill = to_rfkill(dev);
524
525 if (dev->power.power_state.event != PM_EVENT_ON) {
526 mutex_lock(&rfkill->mutex);
527
528 dev->power.power_state.event = PM_EVENT_ON;
529
530
531 rfkill_toggle_radio(rfkill, rfkill->state, 1);
532
533 mutex_unlock(&rfkill->mutex);
534 }
535
536 return 0;
537}
538#else
539#define rfkill_suspend NULL
540#define rfkill_resume NULL
541#endif
542
543static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
544 unsigned long eventid,
545 void *data)
546{
547 struct rfkill *rfkill = (struct rfkill *)data;
548
549 switch (eventid) {
550 case RFKILL_STATE_CHANGED:
551 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
552 break;
553 default:
554 break;
555 }
556
557 return NOTIFY_DONE;
558}
559
560static struct notifier_block rfkill_blocking_uevent_nb = {
561 .notifier_call = rfkill_blocking_uevent_notifier,
562 .priority = 0,
563};
564
565static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
566{
567 struct rfkill *rfkill = to_rfkill(dev);
568 int error;
569
570 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
571 if (error)
572 return error;
573 error = add_uevent_var(env, "RFKILL_TYPE=%s",
574 rfkill_get_type_str(rfkill->type));
575 if (error)
576 return error;
577 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
578 return error;
579}
580
581static struct class rfkill_class = {
582 .name = "rfkill",
583 .dev_release = rfkill_release,
584 .dev_attrs = rfkill_dev_attrs,
585 .suspend = rfkill_suspend,
586 .resume = rfkill_resume,
587 .dev_uevent = rfkill_dev_uevent,
588};
589
590static int rfkill_check_duplicity(const struct rfkill *rfkill)
591{
592 struct rfkill *p;
593 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
594
595 memset(seen, 0, sizeof(seen));
596
597 list_for_each_entry(p, &rfkill_list, node) {
598 if (WARN((p == rfkill), KERN_WARNING
599 "rfkill: illegal attempt to register "
600 "an already registered rfkill struct\n"))
601 return -EEXIST;
602 set_bit(p->type, seen);
603 }
604
605
606 return (test_bit(rfkill->type, seen)) ? 1 : 0;
607}
608
609static int rfkill_add_switch(struct rfkill *rfkill)
610{
611 int error;
612
613 mutex_lock(&rfkill_global_mutex);
614
615 error = rfkill_check_duplicity(rfkill);
616 if (error < 0)
617 goto unlock_out;
618
619 if (!error) {
620
621 set_bit(rfkill->type, rfkill_states_lockdflt);
622 rfkill_global_states[rfkill->type].current_state =
623 rfkill_global_states[rfkill->type].default_state;
624 }
625
626 rfkill_toggle_radio(rfkill,
627 rfkill_global_states[rfkill->type].current_state,
628 0);
629
630 list_add_tail(&rfkill->node, &rfkill_list);
631
632 error = 0;
633unlock_out:
634 mutex_unlock(&rfkill_global_mutex);
635
636 return error;
637}
638
639static void rfkill_remove_switch(struct rfkill *rfkill)
640{
641 mutex_lock(&rfkill_global_mutex);
642 list_del_init(&rfkill->node);
643 mutex_unlock(&rfkill_global_mutex);
644
645 mutex_lock(&rfkill->mutex);
646 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
647 mutex_unlock(&rfkill->mutex);
648}
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663struct rfkill * __must_check rfkill_allocate(struct device *parent,
664 enum rfkill_type type)
665{
666 struct rfkill *rfkill;
667 struct device *dev;
668
669 if (WARN((type >= RFKILL_TYPE_MAX),
670 KERN_WARNING
671 "rfkill: illegal type %d passed as parameter "
672 "to rfkill_allocate\n", type))
673 return NULL;
674
675 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
676 if (!rfkill)
677 return NULL;
678
679 mutex_init(&rfkill->mutex);
680 INIT_LIST_HEAD(&rfkill->node);
681 rfkill->type = type;
682
683 dev = &rfkill->dev;
684 dev->class = &rfkill_class;
685 dev->parent = parent;
686 device_initialize(dev);
687
688 __module_get(THIS_MODULE);
689
690 return rfkill;
691}
692EXPORT_SYMBOL(rfkill_allocate);
693
694
695
696
697
698
699
700
701void rfkill_free(struct rfkill *rfkill)
702{
703 if (rfkill)
704 put_device(&rfkill->dev);
705}
706EXPORT_SYMBOL(rfkill_free);
707
708static void rfkill_led_trigger_register(struct rfkill *rfkill)
709{
710#ifdef CONFIG_RFKILL_LEDS
711 int error;
712
713 if (!rfkill->led_trigger.name)
714 rfkill->led_trigger.name = rfkill->dev.bus_id;
715 if (!rfkill->led_trigger.activate)
716 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
717 error = led_trigger_register(&rfkill->led_trigger);
718 if (error)
719 rfkill->led_trigger.name = NULL;
720#endif
721}
722
723static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
724{
725#ifdef CONFIG_RFKILL_LEDS
726 if (rfkill->led_trigger.name) {
727 led_trigger_unregister(&rfkill->led_trigger);
728 rfkill->led_trigger.name = NULL;
729 }
730#endif
731}
732
733
734
735
736
737
738
739
740
741int __must_check rfkill_register(struct rfkill *rfkill)
742{
743 static atomic_t rfkill_no = ATOMIC_INIT(0);
744 struct device *dev = &rfkill->dev;
745 int error;
746
747 if (WARN((!rfkill || !rfkill->toggle_radio ||
748 rfkill->type >= RFKILL_TYPE_MAX ||
749 rfkill->state >= RFKILL_STATE_MAX),
750 KERN_WARNING
751 "rfkill: attempt to register a "
752 "badly initialized rfkill struct\n"))
753 return -EINVAL;
754
755 snprintf(dev->bus_id, sizeof(dev->bus_id),
756 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
757
758 rfkill_led_trigger_register(rfkill);
759
760 error = rfkill_add_switch(rfkill);
761 if (error) {
762 rfkill_led_trigger_unregister(rfkill);
763 return error;
764 }
765
766 error = device_add(dev);
767 if (error) {
768 rfkill_remove_switch(rfkill);
769 rfkill_led_trigger_unregister(rfkill);
770 return error;
771 }
772
773 return 0;
774}
775EXPORT_SYMBOL(rfkill_register);
776
777
778
779
780
781
782
783
784
785void rfkill_unregister(struct rfkill *rfkill)
786{
787 BUG_ON(!rfkill);
788 device_del(&rfkill->dev);
789 rfkill_remove_switch(rfkill);
790 rfkill_led_trigger_unregister(rfkill);
791 put_device(&rfkill->dev);
792}
793EXPORT_SYMBOL(rfkill_unregister);
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
821{
822 int error;
823
824 if (WARN((type >= RFKILL_TYPE_MAX ||
825 (state != RFKILL_STATE_SOFT_BLOCKED &&
826 state != RFKILL_STATE_UNBLOCKED)),
827 KERN_WARNING
828 "rfkill: illegal state %d or type %d passed as "
829 "parameter to rfkill_set_default\n", state, type))
830 return -EINVAL;
831
832 mutex_lock(&rfkill_global_mutex);
833
834 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
835 rfkill_global_states[type].default_state = state;
836 error = 0;
837 } else
838 error = -EPERM;
839
840 mutex_unlock(&rfkill_global_mutex);
841 return error;
842}
843EXPORT_SYMBOL_GPL(rfkill_set_default);
844
845
846
847
848static int __init rfkill_init(void)
849{
850 int error;
851 int i;
852
853
854 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
855 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
856 return -EINVAL;
857
858 for (i = 0; i < RFKILL_TYPE_MAX; i++)
859 rfkill_global_states[i].default_state = rfkill_default_state;
860
861 error = class_register(&rfkill_class);
862 if (error) {
863 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
864 return error;
865 }
866
867 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
868
869 return 0;
870}
871
872static void __exit rfkill_exit(void)
873{
874 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
875 class_unregister(&rfkill_class);
876}
877
878subsys_initcall(rfkill_init);
879module_exit(rfkill_exit);
880