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