Merge branches 'fixes' and 'misc' into for-linus
[platform/kernel/linux-starfive.git] / net / rfkill / core.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2006 - 2007 Ivo van Doorn
4  * Copyright (C) 2007 Dmitry Torokhov
5  * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/workqueue.h>
12 #include <linux/capability.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/rfkill.h>
16 #include <linux/sched.h>
17 #include <linux/spinlock.h>
18 #include <linux/device.h>
19 #include <linux/miscdevice.h>
20 #include <linux/wait.h>
21 #include <linux/poll.h>
22 #include <linux/fs.h>
23 #include <linux/slab.h>
24
25 #include "rfkill.h"
26
27 #define POLL_INTERVAL           (5 * HZ)
28
29 #define RFKILL_BLOCK_HW         BIT(0)
30 #define RFKILL_BLOCK_SW         BIT(1)
31 #define RFKILL_BLOCK_SW_PREV    BIT(2)
32 #define RFKILL_BLOCK_ANY        (RFKILL_BLOCK_HW |\
33                                  RFKILL_BLOCK_SW |\
34                                  RFKILL_BLOCK_SW_PREV)
35 #define RFKILL_BLOCK_SW_SETCALL BIT(31)
36
37 struct rfkill {
38         spinlock_t              lock;
39
40         enum rfkill_type        type;
41
42         unsigned long           state;
43         unsigned long           hard_block_reasons;
44
45         u32                     idx;
46
47         bool                    registered;
48         bool                    persistent;
49         bool                    polling_paused;
50         bool                    suspended;
51
52         const struct rfkill_ops *ops;
53         void                    *data;
54
55 #ifdef CONFIG_RFKILL_LEDS
56         struct led_trigger      led_trigger;
57         const char              *ledtrigname;
58 #endif
59
60         struct device           dev;
61         struct list_head        node;
62
63         struct delayed_work     poll_work;
64         struct work_struct      uevent_work;
65         struct work_struct      sync_work;
66         char                    name[];
67 };
68 #define to_rfkill(d)    container_of(d, struct rfkill, dev)
69
70 struct rfkill_int_event {
71         struct list_head        list;
72         struct rfkill_event_ext ev;
73 };
74
75 struct rfkill_data {
76         struct list_head        list;
77         struct list_head        events;
78         struct mutex            mtx;
79         wait_queue_head_t       read_wait;
80         bool                    input_handler;
81 };
82
83
84 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
85 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
86 MODULE_DESCRIPTION("RF switch support");
87 MODULE_LICENSE("GPL");
88
89
90 /*
91  * The locking here should be made much smarter, we currently have
92  * a bit of a stupid situation because drivers might want to register
93  * the rfkill struct under their own lock, and take this lock during
94  * rfkill method calls -- which will cause an AB-BA deadlock situation.
95  *
96  * To fix that, we need to rework this code here to be mostly lock-free
97  * and only use the mutex for list manipulations, not to protect the
98  * various other global variables. Then we can avoid holding the mutex
99  * around driver operations, and all is happy.
100  */
101 static LIST_HEAD(rfkill_list);  /* list of registered rf switches */
102 static DEFINE_MUTEX(rfkill_global_mutex);
103 static LIST_HEAD(rfkill_fds);   /* list of open fds of /dev/rfkill */
104
105 static unsigned int rfkill_default_state = 1;
106 module_param_named(default_state, rfkill_default_state, uint, 0444);
107 MODULE_PARM_DESC(default_state,
108                  "Default initial state for all radio types, 0 = radio off");
109
110 static struct {
111         bool cur, sav;
112 } rfkill_global_states[NUM_RFKILL_TYPES];
113
114 static bool rfkill_epo_lock_active;
115
116
117 #ifdef CONFIG_RFKILL_LEDS
118 static void rfkill_led_trigger_event(struct rfkill *rfkill)
119 {
120         struct led_trigger *trigger;
121
122         if (!rfkill->registered)
123                 return;
124
125         trigger = &rfkill->led_trigger;
126
127         if (rfkill->state & RFKILL_BLOCK_ANY)
128                 led_trigger_event(trigger, LED_OFF);
129         else
130                 led_trigger_event(trigger, LED_FULL);
131 }
132
133 static int rfkill_led_trigger_activate(struct led_classdev *led)
134 {
135         struct rfkill *rfkill;
136
137         rfkill = container_of(led->trigger, struct rfkill, led_trigger);
138
139         rfkill_led_trigger_event(rfkill);
140
141         return 0;
142 }
143
144 const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
145 {
146         return rfkill->led_trigger.name;
147 }
148 EXPORT_SYMBOL(rfkill_get_led_trigger_name);
149
150 void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
151 {
152         BUG_ON(!rfkill);
153
154         rfkill->ledtrigname = name;
155 }
156 EXPORT_SYMBOL(rfkill_set_led_trigger_name);
157
158 static int rfkill_led_trigger_register(struct rfkill *rfkill)
159 {
160         rfkill->led_trigger.name = rfkill->ledtrigname
161                                         ? : dev_name(&rfkill->dev);
162         rfkill->led_trigger.activate = rfkill_led_trigger_activate;
163         return led_trigger_register(&rfkill->led_trigger);
164 }
165
166 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
167 {
168         led_trigger_unregister(&rfkill->led_trigger);
169 }
170
171 static struct led_trigger rfkill_any_led_trigger;
172 static struct led_trigger rfkill_none_led_trigger;
173 static struct work_struct rfkill_global_led_trigger_work;
174
175 static void rfkill_global_led_trigger_worker(struct work_struct *work)
176 {
177         enum led_brightness brightness = LED_OFF;
178         struct rfkill *rfkill;
179
180         mutex_lock(&rfkill_global_mutex);
181         list_for_each_entry(rfkill, &rfkill_list, node) {
182                 if (!(rfkill->state & RFKILL_BLOCK_ANY)) {
183                         brightness = LED_FULL;
184                         break;
185                 }
186         }
187         mutex_unlock(&rfkill_global_mutex);
188
189         led_trigger_event(&rfkill_any_led_trigger, brightness);
190         led_trigger_event(&rfkill_none_led_trigger,
191                           brightness == LED_OFF ? LED_FULL : LED_OFF);
192 }
193
194 static void rfkill_global_led_trigger_event(void)
195 {
196         schedule_work(&rfkill_global_led_trigger_work);
197 }
198
199 static int rfkill_global_led_trigger_register(void)
200 {
201         int ret;
202
203         INIT_WORK(&rfkill_global_led_trigger_work,
204                         rfkill_global_led_trigger_worker);
205
206         rfkill_any_led_trigger.name = "rfkill-any";
207         ret = led_trigger_register(&rfkill_any_led_trigger);
208         if (ret)
209                 return ret;
210
211         rfkill_none_led_trigger.name = "rfkill-none";
212         ret = led_trigger_register(&rfkill_none_led_trigger);
213         if (ret)
214                 led_trigger_unregister(&rfkill_any_led_trigger);
215         else
216                 /* Delay activation until all global triggers are registered */
217                 rfkill_global_led_trigger_event();
218
219         return ret;
220 }
221
222 static void rfkill_global_led_trigger_unregister(void)
223 {
224         led_trigger_unregister(&rfkill_none_led_trigger);
225         led_trigger_unregister(&rfkill_any_led_trigger);
226         cancel_work_sync(&rfkill_global_led_trigger_work);
227 }
228 #else
229 static void rfkill_led_trigger_event(struct rfkill *rfkill)
230 {
231 }
232
233 static inline int rfkill_led_trigger_register(struct rfkill *rfkill)
234 {
235         return 0;
236 }
237
238 static inline void rfkill_led_trigger_unregister(struct rfkill *rfkill)
239 {
240 }
241
242 static void rfkill_global_led_trigger_event(void)
243 {
244 }
245
246 static int rfkill_global_led_trigger_register(void)
247 {
248         return 0;
249 }
250
251 static void rfkill_global_led_trigger_unregister(void)
252 {
253 }
254 #endif /* CONFIG_RFKILL_LEDS */
255
256 static void rfkill_fill_event(struct rfkill_event_ext *ev,
257                               struct rfkill *rfkill,
258                               enum rfkill_operation op)
259 {
260         unsigned long flags;
261
262         ev->idx = rfkill->idx;
263         ev->type = rfkill->type;
264         ev->op = op;
265
266         spin_lock_irqsave(&rfkill->lock, flags);
267         ev->hard = !!(rfkill->state & RFKILL_BLOCK_HW);
268         ev->soft = !!(rfkill->state & (RFKILL_BLOCK_SW |
269                                         RFKILL_BLOCK_SW_PREV));
270         ev->hard_block_reasons = rfkill->hard_block_reasons;
271         spin_unlock_irqrestore(&rfkill->lock, flags);
272 }
273
274 static void rfkill_send_events(struct rfkill *rfkill, enum rfkill_operation op)
275 {
276         struct rfkill_data *data;
277         struct rfkill_int_event *ev;
278
279         list_for_each_entry(data, &rfkill_fds, list) {
280                 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
281                 if (!ev)
282                         continue;
283                 rfkill_fill_event(&ev->ev, rfkill, op);
284                 mutex_lock(&data->mtx);
285                 list_add_tail(&ev->list, &data->events);
286                 mutex_unlock(&data->mtx);
287                 wake_up_interruptible(&data->read_wait);
288         }
289 }
290
291 static void rfkill_event(struct rfkill *rfkill)
292 {
293         if (!rfkill->registered)
294                 return;
295
296         kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
297
298         /* also send event to /dev/rfkill */
299         rfkill_send_events(rfkill, RFKILL_OP_CHANGE);
300 }
301
302 /**
303  * rfkill_set_block - wrapper for set_block method
304  *
305  * @rfkill: the rfkill struct to use
306  * @blocked: the new software state
307  *
308  * Calls the set_block method (when applicable) and handles notifications
309  * etc. as well.
310  */
311 static void rfkill_set_block(struct rfkill *rfkill, bool blocked)
312 {
313         unsigned long flags;
314         bool prev, curr;
315         int err;
316
317         if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
318                 return;
319
320         /*
321          * Some platforms (...!) generate input events which affect the
322          * _hard_ kill state -- whenever something tries to change the
323          * current software state query the hardware state too.
324          */
325         if (rfkill->ops->query)
326                 rfkill->ops->query(rfkill, rfkill->data);
327
328         spin_lock_irqsave(&rfkill->lock, flags);
329         prev = rfkill->state & RFKILL_BLOCK_SW;
330
331         if (prev)
332                 rfkill->state |= RFKILL_BLOCK_SW_PREV;
333         else
334                 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
335
336         if (blocked)
337                 rfkill->state |= RFKILL_BLOCK_SW;
338         else
339                 rfkill->state &= ~RFKILL_BLOCK_SW;
340
341         rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
342         spin_unlock_irqrestore(&rfkill->lock, flags);
343
344         err = rfkill->ops->set_block(rfkill->data, blocked);
345
346         spin_lock_irqsave(&rfkill->lock, flags);
347         if (err) {
348                 /*
349                  * Failed -- reset status to _PREV, which may be different
350                  * from what we have set _PREV to earlier in this function
351                  * if rfkill_set_sw_state was invoked.
352                  */
353                 if (rfkill->state & RFKILL_BLOCK_SW_PREV)
354                         rfkill->state |= RFKILL_BLOCK_SW;
355                 else
356                         rfkill->state &= ~RFKILL_BLOCK_SW;
357         }
358         rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
359         rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
360         curr = rfkill->state & RFKILL_BLOCK_SW;
361         spin_unlock_irqrestore(&rfkill->lock, flags);
362
363         rfkill_led_trigger_event(rfkill);
364         rfkill_global_led_trigger_event();
365
366         if (prev != curr)
367                 rfkill_event(rfkill);
368 }
369
370 static void rfkill_update_global_state(enum rfkill_type type, bool blocked)
371 {
372         int i;
373
374         if (type != RFKILL_TYPE_ALL) {
375                 rfkill_global_states[type].cur = blocked;
376                 return;
377         }
378
379         for (i = 0; i < NUM_RFKILL_TYPES; i++)
380                 rfkill_global_states[i].cur = blocked;
381 }
382
383 #ifdef CONFIG_RFKILL_INPUT
384 static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
385
386 /**
387  * __rfkill_switch_all - Toggle state of all switches of given type
388  * @type: type of interfaces to be affected
389  * @blocked: the new state
390  *
391  * This function sets the state of all switches of given type,
392  * unless a specific switch is suspended.
393  *
394  * Caller must have acquired rfkill_global_mutex.
395  */
396 static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
397 {
398         struct rfkill *rfkill;
399
400         rfkill_update_global_state(type, blocked);
401         list_for_each_entry(rfkill, &rfkill_list, node) {
402                 if (rfkill->type != type && type != RFKILL_TYPE_ALL)
403                         continue;
404
405                 rfkill_set_block(rfkill, blocked);
406         }
407 }
408
409 /**
410  * rfkill_switch_all - Toggle state of all switches of given type
411  * @type: type of interfaces to be affected
412  * @blocked: the new state
413  *
414  * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
415  * Please refer to __rfkill_switch_all() for details.
416  *
417  * Does nothing if the EPO lock is active.
418  */
419 void rfkill_switch_all(enum rfkill_type type, bool blocked)
420 {
421         if (atomic_read(&rfkill_input_disabled))
422                 return;
423
424         mutex_lock(&rfkill_global_mutex);
425
426         if (!rfkill_epo_lock_active)
427                 __rfkill_switch_all(type, blocked);
428
429         mutex_unlock(&rfkill_global_mutex);
430 }
431
432 /**
433  * rfkill_epo - emergency power off all transmitters
434  *
435  * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
436  * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
437  *
438  * The global state before the EPO is saved and can be restored later
439  * using rfkill_restore_states().
440  */
441 void rfkill_epo(void)
442 {
443         struct rfkill *rfkill;
444         int i;
445
446         if (atomic_read(&rfkill_input_disabled))
447                 return;
448
449         mutex_lock(&rfkill_global_mutex);
450
451         rfkill_epo_lock_active = true;
452         list_for_each_entry(rfkill, &rfkill_list, node)
453                 rfkill_set_block(rfkill, true);
454
455         for (i = 0; i < NUM_RFKILL_TYPES; i++) {
456                 rfkill_global_states[i].sav = rfkill_global_states[i].cur;
457                 rfkill_global_states[i].cur = true;
458         }
459
460         mutex_unlock(&rfkill_global_mutex);
461 }
462
463 /**
464  * rfkill_restore_states - restore global states
465  *
466  * Restore (and sync switches to) the global state from the
467  * states in rfkill_default_states.  This can undo the effects of
468  * a call to rfkill_epo().
469  */
470 void rfkill_restore_states(void)
471 {
472         int i;
473
474         if (atomic_read(&rfkill_input_disabled))
475                 return;
476
477         mutex_lock(&rfkill_global_mutex);
478
479         rfkill_epo_lock_active = false;
480         for (i = 0; i < NUM_RFKILL_TYPES; i++)
481                 __rfkill_switch_all(i, rfkill_global_states[i].sav);
482         mutex_unlock(&rfkill_global_mutex);
483 }
484
485 /**
486  * rfkill_remove_epo_lock - unlock state changes
487  *
488  * Used by rfkill-input manually unlock state changes, when
489  * the EPO switch is deactivated.
490  */
491 void rfkill_remove_epo_lock(void)
492 {
493         if (atomic_read(&rfkill_input_disabled))
494                 return;
495
496         mutex_lock(&rfkill_global_mutex);
497         rfkill_epo_lock_active = false;
498         mutex_unlock(&rfkill_global_mutex);
499 }
500
501 /**
502  * rfkill_is_epo_lock_active - returns true EPO is active
503  *
504  * Returns 0 (false) if there is NOT an active EPO condition,
505  * and 1 (true) if there is an active EPO condition, which
506  * locks all radios in one of the BLOCKED states.
507  *
508  * Can be called in atomic context.
509  */
510 bool rfkill_is_epo_lock_active(void)
511 {
512         return rfkill_epo_lock_active;
513 }
514
515 /**
516  * rfkill_get_global_sw_state - returns global state for a type
517  * @type: the type to get the global state of
518  *
519  * Returns the current global state for a given wireless
520  * device type.
521  */
522 bool rfkill_get_global_sw_state(const enum rfkill_type type)
523 {
524         return rfkill_global_states[type].cur;
525 }
526 #endif
527
528 bool rfkill_set_hw_state_reason(struct rfkill *rfkill,
529                                 bool blocked, unsigned long reason)
530 {
531         unsigned long flags;
532         bool ret, prev;
533
534         BUG_ON(!rfkill);
535
536         if (WARN(reason &
537             ~(RFKILL_HARD_BLOCK_SIGNAL | RFKILL_HARD_BLOCK_NOT_OWNER),
538             "hw_state reason not supported: 0x%lx", reason))
539                 return blocked;
540
541         spin_lock_irqsave(&rfkill->lock, flags);
542         prev = !!(rfkill->hard_block_reasons & reason);
543         if (blocked) {
544                 rfkill->state |= RFKILL_BLOCK_HW;
545                 rfkill->hard_block_reasons |= reason;
546         } else {
547                 rfkill->hard_block_reasons &= ~reason;
548                 if (!rfkill->hard_block_reasons)
549                         rfkill->state &= ~RFKILL_BLOCK_HW;
550         }
551         ret = !!(rfkill->state & RFKILL_BLOCK_ANY);
552         spin_unlock_irqrestore(&rfkill->lock, flags);
553
554         rfkill_led_trigger_event(rfkill);
555         rfkill_global_led_trigger_event();
556
557         if (rfkill->registered && prev != blocked)
558                 schedule_work(&rfkill->uevent_work);
559
560         return ret;
561 }
562 EXPORT_SYMBOL(rfkill_set_hw_state_reason);
563
564 static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
565 {
566         u32 bit = RFKILL_BLOCK_SW;
567
568         /* if in a ops->set_block right now, use other bit */
569         if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
570                 bit = RFKILL_BLOCK_SW_PREV;
571
572         if (blocked)
573                 rfkill->state |= bit;
574         else
575                 rfkill->state &= ~bit;
576 }
577
578 bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
579 {
580         unsigned long flags;
581         bool prev, hwblock;
582
583         BUG_ON(!rfkill);
584
585         spin_lock_irqsave(&rfkill->lock, flags);
586         prev = !!(rfkill->state & RFKILL_BLOCK_SW);
587         __rfkill_set_sw_state(rfkill, blocked);
588         hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
589         blocked = blocked || hwblock;
590         spin_unlock_irqrestore(&rfkill->lock, flags);
591
592         if (!rfkill->registered)
593                 return blocked;
594
595         if (prev != blocked && !hwblock)
596                 schedule_work(&rfkill->uevent_work);
597
598         rfkill_led_trigger_event(rfkill);
599         rfkill_global_led_trigger_event();
600
601         return blocked;
602 }
603 EXPORT_SYMBOL(rfkill_set_sw_state);
604
605 void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
606 {
607         unsigned long flags;
608
609         BUG_ON(!rfkill);
610         BUG_ON(rfkill->registered);
611
612         spin_lock_irqsave(&rfkill->lock, flags);
613         __rfkill_set_sw_state(rfkill, blocked);
614         rfkill->persistent = true;
615         spin_unlock_irqrestore(&rfkill->lock, flags);
616 }
617 EXPORT_SYMBOL(rfkill_init_sw_state);
618
619 void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
620 {
621         unsigned long flags;
622         bool swprev, hwprev;
623
624         BUG_ON(!rfkill);
625
626         spin_lock_irqsave(&rfkill->lock, flags);
627
628         /*
629          * No need to care about prev/setblock ... this is for uevent only
630          * and that will get triggered by rfkill_set_block anyway.
631          */
632         swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
633         hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
634         __rfkill_set_sw_state(rfkill, sw);
635         if (hw)
636                 rfkill->state |= RFKILL_BLOCK_HW;
637         else
638                 rfkill->state &= ~RFKILL_BLOCK_HW;
639
640         spin_unlock_irqrestore(&rfkill->lock, flags);
641
642         if (!rfkill->registered) {
643                 rfkill->persistent = true;
644         } else {
645                 if (swprev != sw || hwprev != hw)
646                         schedule_work(&rfkill->uevent_work);
647
648                 rfkill_led_trigger_event(rfkill);
649                 rfkill_global_led_trigger_event();
650         }
651 }
652 EXPORT_SYMBOL(rfkill_set_states);
653
654 static const char * const rfkill_types[] = {
655         NULL, /* RFKILL_TYPE_ALL */
656         "wlan",
657         "bluetooth",
658         "ultrawideband",
659         "wimax",
660         "wwan",
661         "gps",
662         "fm",
663         "nfc",
664 };
665
666 enum rfkill_type rfkill_find_type(const char *name)
667 {
668         int i;
669
670         BUILD_BUG_ON(ARRAY_SIZE(rfkill_types) != NUM_RFKILL_TYPES);
671
672         if (!name)
673                 return RFKILL_TYPE_ALL;
674
675         for (i = 1; i < NUM_RFKILL_TYPES; i++)
676                 if (!strcmp(name, rfkill_types[i]))
677                         return i;
678         return RFKILL_TYPE_ALL;
679 }
680 EXPORT_SYMBOL(rfkill_find_type);
681
682 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
683                          char *buf)
684 {
685         struct rfkill *rfkill = to_rfkill(dev);
686
687         return sprintf(buf, "%s\n", rfkill->name);
688 }
689 static DEVICE_ATTR_RO(name);
690
691 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
692                          char *buf)
693 {
694         struct rfkill *rfkill = to_rfkill(dev);
695
696         return sprintf(buf, "%s\n", rfkill_types[rfkill->type]);
697 }
698 static DEVICE_ATTR_RO(type);
699
700 static ssize_t index_show(struct device *dev, struct device_attribute *attr,
701                           char *buf)
702 {
703         struct rfkill *rfkill = to_rfkill(dev);
704
705         return sprintf(buf, "%d\n", rfkill->idx);
706 }
707 static DEVICE_ATTR_RO(index);
708
709 static ssize_t persistent_show(struct device *dev,
710                                struct device_attribute *attr, char *buf)
711 {
712         struct rfkill *rfkill = to_rfkill(dev);
713
714         return sprintf(buf, "%d\n", rfkill->persistent);
715 }
716 static DEVICE_ATTR_RO(persistent);
717
718 static ssize_t hard_show(struct device *dev, struct device_attribute *attr,
719                          char *buf)
720 {
721         struct rfkill *rfkill = to_rfkill(dev);
722
723         return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 );
724 }
725 static DEVICE_ATTR_RO(hard);
726
727 static ssize_t soft_show(struct device *dev, struct device_attribute *attr,
728                          char *buf)
729 {
730         struct rfkill *rfkill = to_rfkill(dev);
731
732         return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 );
733 }
734
735 static ssize_t soft_store(struct device *dev, struct device_attribute *attr,
736                           const char *buf, size_t count)
737 {
738         struct rfkill *rfkill = to_rfkill(dev);
739         unsigned long state;
740         int err;
741
742         if (!capable(CAP_NET_ADMIN))
743                 return -EPERM;
744
745         err = kstrtoul(buf, 0, &state);
746         if (err)
747                 return err;
748
749         if (state > 1 )
750                 return -EINVAL;
751
752         mutex_lock(&rfkill_global_mutex);
753         rfkill_set_block(rfkill, state);
754         mutex_unlock(&rfkill_global_mutex);
755
756         return count;
757 }
758 static DEVICE_ATTR_RW(soft);
759
760 static ssize_t hard_block_reasons_show(struct device *dev,
761                                        struct device_attribute *attr,
762                                        char *buf)
763 {
764         struct rfkill *rfkill = to_rfkill(dev);
765
766         return sprintf(buf, "0x%lx\n", rfkill->hard_block_reasons);
767 }
768 static DEVICE_ATTR_RO(hard_block_reasons);
769
770 static u8 user_state_from_blocked(unsigned long state)
771 {
772         if (state & RFKILL_BLOCK_HW)
773                 return RFKILL_USER_STATE_HARD_BLOCKED;
774         if (state & RFKILL_BLOCK_SW)
775                 return RFKILL_USER_STATE_SOFT_BLOCKED;
776
777         return RFKILL_USER_STATE_UNBLOCKED;
778 }
779
780 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
781                           char *buf)
782 {
783         struct rfkill *rfkill = to_rfkill(dev);
784
785         return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state));
786 }
787
788 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
789                            const char *buf, size_t count)
790 {
791         struct rfkill *rfkill = to_rfkill(dev);
792         unsigned long state;
793         int err;
794
795         if (!capable(CAP_NET_ADMIN))
796                 return -EPERM;
797
798         err = kstrtoul(buf, 0, &state);
799         if (err)
800                 return err;
801
802         if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
803             state != RFKILL_USER_STATE_UNBLOCKED)
804                 return -EINVAL;
805
806         mutex_lock(&rfkill_global_mutex);
807         rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
808         mutex_unlock(&rfkill_global_mutex);
809
810         return count;
811 }
812 static DEVICE_ATTR_RW(state);
813
814 static struct attribute *rfkill_dev_attrs[] = {
815         &dev_attr_name.attr,
816         &dev_attr_type.attr,
817         &dev_attr_index.attr,
818         &dev_attr_persistent.attr,
819         &dev_attr_state.attr,
820         &dev_attr_soft.attr,
821         &dev_attr_hard.attr,
822         &dev_attr_hard_block_reasons.attr,
823         NULL,
824 };
825 ATTRIBUTE_GROUPS(rfkill_dev);
826
827 static void rfkill_release(struct device *dev)
828 {
829         struct rfkill *rfkill = to_rfkill(dev);
830
831         kfree(rfkill);
832 }
833
834 static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
835 {
836         struct rfkill *rfkill = to_rfkill(dev);
837         unsigned long flags;
838         unsigned long reasons;
839         u32 state;
840         int error;
841
842         error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
843         if (error)
844                 return error;
845         error = add_uevent_var(env, "RFKILL_TYPE=%s",
846                                rfkill_types[rfkill->type]);
847         if (error)
848                 return error;
849         spin_lock_irqsave(&rfkill->lock, flags);
850         state = rfkill->state;
851         reasons = rfkill->hard_block_reasons;
852         spin_unlock_irqrestore(&rfkill->lock, flags);
853         error = add_uevent_var(env, "RFKILL_STATE=%d",
854                                user_state_from_blocked(state));
855         if (error)
856                 return error;
857         return add_uevent_var(env, "RFKILL_HW_BLOCK_REASON=0x%lx", reasons);
858 }
859
860 void rfkill_pause_polling(struct rfkill *rfkill)
861 {
862         BUG_ON(!rfkill);
863
864         if (!rfkill->ops->poll)
865                 return;
866
867         rfkill->polling_paused = true;
868         cancel_delayed_work_sync(&rfkill->poll_work);
869 }
870 EXPORT_SYMBOL(rfkill_pause_polling);
871
872 void rfkill_resume_polling(struct rfkill *rfkill)
873 {
874         BUG_ON(!rfkill);
875
876         if (!rfkill->ops->poll)
877                 return;
878
879         rfkill->polling_paused = false;
880
881         if (rfkill->suspended)
882                 return;
883
884         queue_delayed_work(system_power_efficient_wq,
885                            &rfkill->poll_work, 0);
886 }
887 EXPORT_SYMBOL(rfkill_resume_polling);
888
889 #ifdef CONFIG_PM_SLEEP
890 static int rfkill_suspend(struct device *dev)
891 {
892         struct rfkill *rfkill = to_rfkill(dev);
893
894         rfkill->suspended = true;
895         cancel_delayed_work_sync(&rfkill->poll_work);
896
897         return 0;
898 }
899
900 static int rfkill_resume(struct device *dev)
901 {
902         struct rfkill *rfkill = to_rfkill(dev);
903         bool cur;
904
905         rfkill->suspended = false;
906
907         if (!rfkill->registered)
908                 return 0;
909
910         if (!rfkill->persistent) {
911                 cur = !!(rfkill->state & RFKILL_BLOCK_SW);
912                 rfkill_set_block(rfkill, cur);
913         }
914
915         if (rfkill->ops->poll && !rfkill->polling_paused)
916                 queue_delayed_work(system_power_efficient_wq,
917                                    &rfkill->poll_work, 0);
918
919         return 0;
920 }
921
922 static SIMPLE_DEV_PM_OPS(rfkill_pm_ops, rfkill_suspend, rfkill_resume);
923 #define RFKILL_PM_OPS (&rfkill_pm_ops)
924 #else
925 #define RFKILL_PM_OPS NULL
926 #endif
927
928 static struct class rfkill_class = {
929         .name           = "rfkill",
930         .dev_release    = rfkill_release,
931         .dev_groups     = rfkill_dev_groups,
932         .dev_uevent     = rfkill_dev_uevent,
933         .pm             = RFKILL_PM_OPS,
934 };
935
936 bool rfkill_blocked(struct rfkill *rfkill)
937 {
938         unsigned long flags;
939         u32 state;
940
941         spin_lock_irqsave(&rfkill->lock, flags);
942         state = rfkill->state;
943         spin_unlock_irqrestore(&rfkill->lock, flags);
944
945         return !!(state & RFKILL_BLOCK_ANY);
946 }
947 EXPORT_SYMBOL(rfkill_blocked);
948
949 bool rfkill_soft_blocked(struct rfkill *rfkill)
950 {
951         unsigned long flags;
952         u32 state;
953
954         spin_lock_irqsave(&rfkill->lock, flags);
955         state = rfkill->state;
956         spin_unlock_irqrestore(&rfkill->lock, flags);
957
958         return !!(state & RFKILL_BLOCK_SW);
959 }
960 EXPORT_SYMBOL(rfkill_soft_blocked);
961
962 struct rfkill * __must_check rfkill_alloc(const char *name,
963                                           struct device *parent,
964                                           const enum rfkill_type type,
965                                           const struct rfkill_ops *ops,
966                                           void *ops_data)
967 {
968         struct rfkill *rfkill;
969         struct device *dev;
970
971         if (WARN_ON(!ops))
972                 return NULL;
973
974         if (WARN_ON(!ops->set_block))
975                 return NULL;
976
977         if (WARN_ON(!name))
978                 return NULL;
979
980         if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
981                 return NULL;
982
983         rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
984         if (!rfkill)
985                 return NULL;
986
987         spin_lock_init(&rfkill->lock);
988         INIT_LIST_HEAD(&rfkill->node);
989         rfkill->type = type;
990         strcpy(rfkill->name, name);
991         rfkill->ops = ops;
992         rfkill->data = ops_data;
993
994         dev = &rfkill->dev;
995         dev->class = &rfkill_class;
996         dev->parent = parent;
997         device_initialize(dev);
998
999         return rfkill;
1000 }
1001 EXPORT_SYMBOL(rfkill_alloc);
1002
1003 static void rfkill_poll(struct work_struct *work)
1004 {
1005         struct rfkill *rfkill;
1006
1007         rfkill = container_of(work, struct rfkill, poll_work.work);
1008
1009         /*
1010          * Poll hardware state -- driver will use one of the
1011          * rfkill_set{,_hw,_sw}_state functions and use its
1012          * return value to update the current status.
1013          */
1014         rfkill->ops->poll(rfkill, rfkill->data);
1015
1016         queue_delayed_work(system_power_efficient_wq,
1017                 &rfkill->poll_work,
1018                 round_jiffies_relative(POLL_INTERVAL));
1019 }
1020
1021 static void rfkill_uevent_work(struct work_struct *work)
1022 {
1023         struct rfkill *rfkill;
1024
1025         rfkill = container_of(work, struct rfkill, uevent_work);
1026
1027         mutex_lock(&rfkill_global_mutex);
1028         rfkill_event(rfkill);
1029         mutex_unlock(&rfkill_global_mutex);
1030 }
1031
1032 static void rfkill_sync_work(struct work_struct *work)
1033 {
1034         struct rfkill *rfkill;
1035         bool cur;
1036
1037         rfkill = container_of(work, struct rfkill, sync_work);
1038
1039         mutex_lock(&rfkill_global_mutex);
1040         cur = rfkill_global_states[rfkill->type].cur;
1041         rfkill_set_block(rfkill, cur);
1042         mutex_unlock(&rfkill_global_mutex);
1043 }
1044
1045 int __must_check rfkill_register(struct rfkill *rfkill)
1046 {
1047         static unsigned long rfkill_no;
1048         struct device *dev;
1049         int error;
1050
1051         if (!rfkill)
1052                 return -EINVAL;
1053
1054         dev = &rfkill->dev;
1055
1056         mutex_lock(&rfkill_global_mutex);
1057
1058         if (rfkill->registered) {
1059                 error = -EALREADY;
1060                 goto unlock;
1061         }
1062
1063         rfkill->idx = rfkill_no;
1064         dev_set_name(dev, "rfkill%lu", rfkill_no);
1065         rfkill_no++;
1066
1067         list_add_tail(&rfkill->node, &rfkill_list);
1068
1069         error = device_add(dev);
1070         if (error)
1071                 goto remove;
1072
1073         error = rfkill_led_trigger_register(rfkill);
1074         if (error)
1075                 goto devdel;
1076
1077         rfkill->registered = true;
1078
1079         INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
1080         INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
1081         INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
1082
1083         if (rfkill->ops->poll)
1084                 queue_delayed_work(system_power_efficient_wq,
1085                         &rfkill->poll_work,
1086                         round_jiffies_relative(POLL_INTERVAL));
1087
1088         if (!rfkill->persistent || rfkill_epo_lock_active) {
1089                 schedule_work(&rfkill->sync_work);
1090         } else {
1091 #ifdef CONFIG_RFKILL_INPUT
1092                 bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
1093
1094                 if (!atomic_read(&rfkill_input_disabled))
1095                         __rfkill_switch_all(rfkill->type, soft_blocked);
1096 #endif
1097         }
1098
1099         rfkill_global_led_trigger_event();
1100         rfkill_send_events(rfkill, RFKILL_OP_ADD);
1101
1102         mutex_unlock(&rfkill_global_mutex);
1103         return 0;
1104
1105  devdel:
1106         device_del(&rfkill->dev);
1107  remove:
1108         list_del_init(&rfkill->node);
1109  unlock:
1110         mutex_unlock(&rfkill_global_mutex);
1111         return error;
1112 }
1113 EXPORT_SYMBOL(rfkill_register);
1114
1115 void rfkill_unregister(struct rfkill *rfkill)
1116 {
1117         BUG_ON(!rfkill);
1118
1119         if (rfkill->ops->poll)
1120                 cancel_delayed_work_sync(&rfkill->poll_work);
1121
1122         cancel_work_sync(&rfkill->uevent_work);
1123         cancel_work_sync(&rfkill->sync_work);
1124
1125         rfkill->registered = false;
1126
1127         device_del(&rfkill->dev);
1128
1129         mutex_lock(&rfkill_global_mutex);
1130         rfkill_send_events(rfkill, RFKILL_OP_DEL);
1131         list_del_init(&rfkill->node);
1132         rfkill_global_led_trigger_event();
1133         mutex_unlock(&rfkill_global_mutex);
1134
1135         rfkill_led_trigger_unregister(rfkill);
1136 }
1137 EXPORT_SYMBOL(rfkill_unregister);
1138
1139 void rfkill_destroy(struct rfkill *rfkill)
1140 {
1141         if (rfkill)
1142                 put_device(&rfkill->dev);
1143 }
1144 EXPORT_SYMBOL(rfkill_destroy);
1145
1146 static int rfkill_fop_open(struct inode *inode, struct file *file)
1147 {
1148         struct rfkill_data *data;
1149         struct rfkill *rfkill;
1150         struct rfkill_int_event *ev, *tmp;
1151
1152         data = kzalloc(sizeof(*data), GFP_KERNEL);
1153         if (!data)
1154                 return -ENOMEM;
1155
1156         INIT_LIST_HEAD(&data->events);
1157         mutex_init(&data->mtx);
1158         init_waitqueue_head(&data->read_wait);
1159
1160         mutex_lock(&rfkill_global_mutex);
1161         mutex_lock(&data->mtx);
1162         /*
1163          * start getting events from elsewhere but hold mtx to get
1164          * startup events added first
1165          */
1166
1167         list_for_each_entry(rfkill, &rfkill_list, node) {
1168                 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1169                 if (!ev)
1170                         goto free;
1171                 rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
1172                 list_add_tail(&ev->list, &data->events);
1173         }
1174         list_add(&data->list, &rfkill_fds);
1175         mutex_unlock(&data->mtx);
1176         mutex_unlock(&rfkill_global_mutex);
1177
1178         file->private_data = data;
1179
1180         return stream_open(inode, file);
1181
1182  free:
1183         mutex_unlock(&data->mtx);
1184         mutex_unlock(&rfkill_global_mutex);
1185         mutex_destroy(&data->mtx);
1186         list_for_each_entry_safe(ev, tmp, &data->events, list)
1187                 kfree(ev);
1188         kfree(data);
1189         return -ENOMEM;
1190 }
1191
1192 static __poll_t rfkill_fop_poll(struct file *file, poll_table *wait)
1193 {
1194         struct rfkill_data *data = file->private_data;
1195         __poll_t res = EPOLLOUT | EPOLLWRNORM;
1196
1197         poll_wait(file, &data->read_wait, wait);
1198
1199         mutex_lock(&data->mtx);
1200         if (!list_empty(&data->events))
1201                 res = EPOLLIN | EPOLLRDNORM;
1202         mutex_unlock(&data->mtx);
1203
1204         return res;
1205 }
1206
1207 static ssize_t rfkill_fop_read(struct file *file, char __user *buf,
1208                                size_t count, loff_t *pos)
1209 {
1210         struct rfkill_data *data = file->private_data;
1211         struct rfkill_int_event *ev;
1212         unsigned long sz;
1213         int ret;
1214
1215         mutex_lock(&data->mtx);
1216
1217         while (list_empty(&data->events)) {
1218                 if (file->f_flags & O_NONBLOCK) {
1219                         ret = -EAGAIN;
1220                         goto out;
1221                 }
1222                 mutex_unlock(&data->mtx);
1223                 /* since we re-check and it just compares pointers,
1224                  * using !list_empty() without locking isn't a problem
1225                  */
1226                 ret = wait_event_interruptible(data->read_wait,
1227                                                !list_empty(&data->events));
1228                 mutex_lock(&data->mtx);
1229
1230                 if (ret)
1231                         goto out;
1232         }
1233
1234         ev = list_first_entry(&data->events, struct rfkill_int_event,
1235                                 list);
1236
1237         sz = min_t(unsigned long, sizeof(ev->ev), count);
1238         ret = sz;
1239         if (copy_to_user(buf, &ev->ev, sz))
1240                 ret = -EFAULT;
1241
1242         list_del(&ev->list);
1243         kfree(ev);
1244  out:
1245         mutex_unlock(&data->mtx);
1246         return ret;
1247 }
1248
1249 static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
1250                                 size_t count, loff_t *pos)
1251 {
1252         struct rfkill *rfkill;
1253         struct rfkill_event_ext ev;
1254         int ret;
1255
1256         /* we don't need the 'hard' variable but accept it */
1257         if (count < RFKILL_EVENT_SIZE_V1 - 1)
1258                 return -EINVAL;
1259
1260         /*
1261          * Copy as much data as we can accept into our 'ev' buffer,
1262          * but tell userspace how much we've copied so it can determine
1263          * our API version even in a write() call, if it cares.
1264          */
1265         count = min(count, sizeof(ev));
1266         if (copy_from_user(&ev, buf, count))
1267                 return -EFAULT;
1268
1269         if (ev.type >= NUM_RFKILL_TYPES)
1270                 return -EINVAL;
1271
1272         mutex_lock(&rfkill_global_mutex);
1273
1274         switch (ev.op) {
1275         case RFKILL_OP_CHANGE_ALL:
1276                 rfkill_update_global_state(ev.type, ev.soft);
1277                 list_for_each_entry(rfkill, &rfkill_list, node)
1278                         if (rfkill->type == ev.type ||
1279                             ev.type == RFKILL_TYPE_ALL)
1280                                 rfkill_set_block(rfkill, ev.soft);
1281                 ret = 0;
1282                 break;
1283         case RFKILL_OP_CHANGE:
1284                 list_for_each_entry(rfkill, &rfkill_list, node)
1285                         if (rfkill->idx == ev.idx &&
1286                             (rfkill->type == ev.type ||
1287                              ev.type == RFKILL_TYPE_ALL))
1288                                 rfkill_set_block(rfkill, ev.soft);
1289                 ret = 0;
1290                 break;
1291         default:
1292                 ret = -EINVAL;
1293                 break;
1294         }
1295
1296         mutex_unlock(&rfkill_global_mutex);
1297
1298         return ret ?: count;
1299 }
1300
1301 static int rfkill_fop_release(struct inode *inode, struct file *file)
1302 {
1303         struct rfkill_data *data = file->private_data;
1304         struct rfkill_int_event *ev, *tmp;
1305
1306         mutex_lock(&rfkill_global_mutex);
1307         list_del(&data->list);
1308         mutex_unlock(&rfkill_global_mutex);
1309
1310         mutex_destroy(&data->mtx);
1311         list_for_each_entry_safe(ev, tmp, &data->events, list)
1312                 kfree(ev);
1313
1314 #ifdef CONFIG_RFKILL_INPUT
1315         if (data->input_handler)
1316                 if (atomic_dec_return(&rfkill_input_disabled) == 0)
1317                         printk(KERN_DEBUG "rfkill: input handler enabled\n");
1318 #endif
1319
1320         kfree(data);
1321
1322         return 0;
1323 }
1324
1325 #ifdef CONFIG_RFKILL_INPUT
1326 static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
1327                              unsigned long arg)
1328 {
1329         struct rfkill_data *data = file->private_data;
1330
1331         if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
1332                 return -ENOSYS;
1333
1334         if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
1335                 return -ENOSYS;
1336
1337         mutex_lock(&data->mtx);
1338
1339         if (!data->input_handler) {
1340                 if (atomic_inc_return(&rfkill_input_disabled) == 1)
1341                         printk(KERN_DEBUG "rfkill: input handler disabled\n");
1342                 data->input_handler = true;
1343         }
1344
1345         mutex_unlock(&data->mtx);
1346
1347         return 0;
1348 }
1349 #endif
1350
1351 static const struct file_operations rfkill_fops = {
1352         .owner          = THIS_MODULE,
1353         .open           = rfkill_fop_open,
1354         .read           = rfkill_fop_read,
1355         .write          = rfkill_fop_write,
1356         .poll           = rfkill_fop_poll,
1357         .release        = rfkill_fop_release,
1358 #ifdef CONFIG_RFKILL_INPUT
1359         .unlocked_ioctl = rfkill_fop_ioctl,
1360         .compat_ioctl   = compat_ptr_ioctl,
1361 #endif
1362         .llseek         = no_llseek,
1363 };
1364
1365 #define RFKILL_NAME "rfkill"
1366
1367 static struct miscdevice rfkill_miscdev = {
1368         .fops   = &rfkill_fops,
1369         .name   = RFKILL_NAME,
1370         .minor  = RFKILL_MINOR,
1371 };
1372
1373 static int __init rfkill_init(void)
1374 {
1375         int error;
1376
1377         rfkill_update_global_state(RFKILL_TYPE_ALL, !rfkill_default_state);
1378
1379         error = class_register(&rfkill_class);
1380         if (error)
1381                 goto error_class;
1382
1383         error = misc_register(&rfkill_miscdev);
1384         if (error)
1385                 goto error_misc;
1386
1387         error = rfkill_global_led_trigger_register();
1388         if (error)
1389                 goto error_led_trigger;
1390
1391 #ifdef CONFIG_RFKILL_INPUT
1392         error = rfkill_handler_init();
1393         if (error)
1394                 goto error_input;
1395 #endif
1396
1397         return 0;
1398
1399 #ifdef CONFIG_RFKILL_INPUT
1400 error_input:
1401         rfkill_global_led_trigger_unregister();
1402 #endif
1403 error_led_trigger:
1404         misc_deregister(&rfkill_miscdev);
1405 error_misc:
1406         class_unregister(&rfkill_class);
1407 error_class:
1408         return error;
1409 }
1410 subsys_initcall(rfkill_init);
1411
1412 static void __exit rfkill_exit(void)
1413 {
1414 #ifdef CONFIG_RFKILL_INPUT
1415         rfkill_handler_exit();
1416 #endif
1417         rfkill_global_led_trigger_unregister();
1418         misc_deregister(&rfkill_miscdev);
1419         class_unregister(&rfkill_class);
1420 }
1421 module_exit(rfkill_exit);
1422
1423 MODULE_ALIAS_MISCDEV(RFKILL_MINOR);
1424 MODULE_ALIAS("devname:" RFKILL_NAME);