cfg80211: Fix some linguistics in Kconfig
[deliverable/linux.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
238static bool __rfkill_set_hw_state(struct rfkill *rfkill,
239 bool blocked, bool *change)
240{
241 unsigned long flags;
242 bool prev, any;
243
244 BUG_ON(!rfkill);
245
246 spin_lock_irqsave(&rfkill->lock, flags);
247 prev = !!(rfkill->state & RFKILL_BLOCK_HW);
248 if (blocked)
249 rfkill->state |= RFKILL_BLOCK_HW;
250 else
251 rfkill->state &= ~RFKILL_BLOCK_HW;
252 *change = prev != blocked;
6be19ccd 253 any = !!(rfkill->state & RFKILL_BLOCK_ANY);
19d337df
JB
254 spin_unlock_irqrestore(&rfkill->lock, flags);
255
256 rfkill_led_trigger_event(rfkill);
257
258 return any;
259}
260
261/**
262 * rfkill_set_block - wrapper for set_block method
263 *
264 * @rfkill: the rfkill struct to use
265 * @blocked: the new software state
266 *
267 * Calls the set_block method (when applicable) and handles notifications
268 * etc. as well.
269 */
270static void rfkill_set_block(struct rfkill *rfkill, bool blocked)
271{
272 unsigned long flags;
eab48345 273 bool prev, curr;
19d337df
JB
274 int err;
275
7fa20a7f
AJ
276 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
277 return;
278
19d337df
JB
279 /*
280 * Some platforms (...!) generate input events which affect the
281 * _hard_ kill state -- whenever something tries to change the
282 * current software state query the hardware state too.
283 */
284 if (rfkill->ops->query)
285 rfkill->ops->query(rfkill, rfkill->data);
286
287 spin_lock_irqsave(&rfkill->lock, flags);
eab48345
VW
288 prev = rfkill->state & RFKILL_BLOCK_SW;
289
19d337df
JB
290 if (rfkill->state & RFKILL_BLOCK_SW)
291 rfkill->state |= RFKILL_BLOCK_SW_PREV;
292 else
293 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
294
295 if (blocked)
296 rfkill->state |= RFKILL_BLOCK_SW;
297 else
298 rfkill->state &= ~RFKILL_BLOCK_SW;
299
300 rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
301 spin_unlock_irqrestore(&rfkill->lock, flags);
302
19d337df
JB
303 err = rfkill->ops->set_block(rfkill->data, blocked);
304
305 spin_lock_irqsave(&rfkill->lock, flags);
306 if (err) {
307 /*
308 * Failed -- reset status to _prev, this may be different
309 * from what set set _PREV to earlier in this function
310 * if rfkill_set_sw_state was invoked.
311 */
312 if (rfkill->state & RFKILL_BLOCK_SW_PREV)
313 rfkill->state |= RFKILL_BLOCK_SW;
314 else
315 rfkill->state &= ~RFKILL_BLOCK_SW;
316 }
317 rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
318 rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
eab48345 319 curr = rfkill->state & RFKILL_BLOCK_SW;
19d337df
JB
320 spin_unlock_irqrestore(&rfkill->lock, flags);
321
322 rfkill_led_trigger_event(rfkill);
eab48345
VW
323
324 if (prev != curr)
325 rfkill_event(rfkill);
19d337df
JB
326}
327
c64fb016
JB
328#ifdef CONFIG_RFKILL_INPUT
329static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
330
19d337df
JB
331/**
332 * __rfkill_switch_all - Toggle state of all switches of given type
333 * @type: type of interfaces to be affected
2f29fed3 334 * @blocked: the new state
19d337df
JB
335 *
336 * This function sets the state of all switches of given type,
337 * unless a specific switch is claimed by userspace (in which case,
338 * that switch is left alone) or suspended.
339 *
340 * Caller must have acquired rfkill_global_mutex.
341 */
342static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
343{
344 struct rfkill *rfkill;
345
4c077893
JPRV
346 if (type == RFKILL_TYPE_ALL) {
347 int i;
348
349 for (i = 0; i < NUM_RFKILL_TYPES; i++)
350 rfkill_global_states[i].cur = blocked;
351 } else {
352 rfkill_global_states[type].cur = blocked;
353 }
354
19d337df 355 list_for_each_entry(rfkill, &rfkill_list, node) {
27e49ca9 356 if (rfkill->type != type && type != RFKILL_TYPE_ALL)
19d337df
JB
357 continue;
358
359 rfkill_set_block(rfkill, blocked);
360 }
361}
362
363/**
364 * rfkill_switch_all - Toggle state of all switches of given type
365 * @type: type of interfaces to be affected
2f29fed3 366 * @blocked: the new state
19d337df
JB
367 *
368 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
369 * Please refer to __rfkill_switch_all() for details.
370 *
371 * Does nothing if the EPO lock is active.
372 */
373void rfkill_switch_all(enum rfkill_type type, bool blocked)
374{
c64fb016
JB
375 if (atomic_read(&rfkill_input_disabled))
376 return;
377
19d337df
JB
378 mutex_lock(&rfkill_global_mutex);
379
380 if (!rfkill_epo_lock_active)
381 __rfkill_switch_all(type, blocked);
382
383 mutex_unlock(&rfkill_global_mutex);
384}
385
386/**
387 * rfkill_epo - emergency power off all transmitters
388 *
389 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
390 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
391 *
392 * The global state before the EPO is saved and can be restored later
393 * using rfkill_restore_states().
394 */
395void rfkill_epo(void)
396{
397 struct rfkill *rfkill;
398 int i;
399
c64fb016
JB
400 if (atomic_read(&rfkill_input_disabled))
401 return;
402
19d337df
JB
403 mutex_lock(&rfkill_global_mutex);
404
405 rfkill_epo_lock_active = true;
406 list_for_each_entry(rfkill, &rfkill_list, node)
407 rfkill_set_block(rfkill, true);
408
409 for (i = 0; i < NUM_RFKILL_TYPES; i++) {
b3fa1329 410 rfkill_global_states[i].sav = rfkill_global_states[i].cur;
19d337df
JB
411 rfkill_global_states[i].cur = true;
412 }
c64fb016 413
19d337df
JB
414 mutex_unlock(&rfkill_global_mutex);
415}
416
417/**
418 * rfkill_restore_states - restore global states
419 *
420 * Restore (and sync switches to) the global state from the
421 * states in rfkill_default_states. This can undo the effects of
422 * a call to rfkill_epo().
423 */
424void rfkill_restore_states(void)
425{
426 int i;
427
c64fb016
JB
428 if (atomic_read(&rfkill_input_disabled))
429 return;
430
19d337df
JB
431 mutex_lock(&rfkill_global_mutex);
432
433 rfkill_epo_lock_active = false;
434 for (i = 0; i < NUM_RFKILL_TYPES; i++)
b3fa1329 435 __rfkill_switch_all(i, rfkill_global_states[i].sav);
19d337df
JB
436 mutex_unlock(&rfkill_global_mutex);
437}
438
439/**
440 * rfkill_remove_epo_lock - unlock state changes
441 *
442 * Used by rfkill-input manually unlock state changes, when
443 * the EPO switch is deactivated.
444 */
445void rfkill_remove_epo_lock(void)
446{
c64fb016
JB
447 if (atomic_read(&rfkill_input_disabled))
448 return;
449
19d337df
JB
450 mutex_lock(&rfkill_global_mutex);
451 rfkill_epo_lock_active = false;
452 mutex_unlock(&rfkill_global_mutex);
453}
454
455/**
456 * rfkill_is_epo_lock_active - returns true EPO is active
457 *
458 * Returns 0 (false) if there is NOT an active EPO contidion,
459 * and 1 (true) if there is an active EPO contition, which
460 * locks all radios in one of the BLOCKED states.
461 *
462 * Can be called in atomic context.
463 */
464bool rfkill_is_epo_lock_active(void)
465{
466 return rfkill_epo_lock_active;
467}
468
469/**
470 * rfkill_get_global_sw_state - returns global state for a type
471 * @type: the type to get the global state of
472 *
473 * Returns the current global state for a given wireless
474 * device type.
475 */
476bool rfkill_get_global_sw_state(const enum rfkill_type type)
477{
478 return rfkill_global_states[type].cur;
479}
c64fb016 480#endif
19d337df 481
19d337df
JB
482
483bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
484{
485 bool ret, change;
486
487 ret = __rfkill_set_hw_state(rfkill, blocked, &change);
488
489 if (!rfkill->registered)
490 return ret;
491
492 if (change)
493 schedule_work(&rfkill->uevent_work);
494
495 return ret;
496}
497EXPORT_SYMBOL(rfkill_set_hw_state);
498
499static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
500{
501 u32 bit = RFKILL_BLOCK_SW;
502
503 /* if in a ops->set_block right now, use other bit */
504 if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
505 bit = RFKILL_BLOCK_SW_PREV;
506
507 if (blocked)
508 rfkill->state |= bit;
509 else
510 rfkill->state &= ~bit;
511}
512
513bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
514{
515 unsigned long flags;
516 bool prev, hwblock;
517
518 BUG_ON(!rfkill);
519
520 spin_lock_irqsave(&rfkill->lock, flags);
521 prev = !!(rfkill->state & RFKILL_BLOCK_SW);
522 __rfkill_set_sw_state(rfkill, blocked);
523 hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
524 blocked = blocked || hwblock;
525 spin_unlock_irqrestore(&rfkill->lock, flags);
526
06d5caf4
AJ
527 if (!rfkill->registered)
528 return blocked;
19d337df 529
06d5caf4
AJ
530 if (prev != blocked && !hwblock)
531 schedule_work(&rfkill->uevent_work);
532
533 rfkill_led_trigger_event(rfkill);
19d337df
JB
534
535 return blocked;
536}
537EXPORT_SYMBOL(rfkill_set_sw_state);
538
06d5caf4
AJ
539void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
540{
541 unsigned long flags;
542
543 BUG_ON(!rfkill);
544 BUG_ON(rfkill->registered);
545
546 spin_lock_irqsave(&rfkill->lock, flags);
547 __rfkill_set_sw_state(rfkill, blocked);
548 rfkill->persistent = true;
549 spin_unlock_irqrestore(&rfkill->lock, flags);
550}
551EXPORT_SYMBOL(rfkill_init_sw_state);
552
19d337df
JB
553void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
554{
555 unsigned long flags;
556 bool swprev, hwprev;
557
558 BUG_ON(!rfkill);
559
560 spin_lock_irqsave(&rfkill->lock, flags);
561
562 /*
563 * No need to care about prev/setblock ... this is for uevent only
564 * and that will get triggered by rfkill_set_block anyway.
565 */
566 swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
567 hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
568 __rfkill_set_sw_state(rfkill, sw);
48ab3578
AJ
569 if (hw)
570 rfkill->state |= RFKILL_BLOCK_HW;
571 else
572 rfkill->state &= ~RFKILL_BLOCK_HW;
19d337df
JB
573
574 spin_unlock_irqrestore(&rfkill->lock, flags);
575
b3fa1329
AJ
576 if (!rfkill->registered) {
577 rfkill->persistent = true;
578 } else {
579 if (swprev != sw || hwprev != hw)
580 schedule_work(&rfkill->uevent_work);
19d337df 581
b3fa1329
AJ
582 rfkill_led_trigger_event(rfkill);
583 }
19d337df
JB
584}
585EXPORT_SYMBOL(rfkill_set_states);
586
e49df67d
GKH
587static ssize_t name_show(struct device *dev, struct device_attribute *attr,
588 char *buf)
19d337df
JB
589{
590 struct rfkill *rfkill = to_rfkill(dev);
591
592 return sprintf(buf, "%s\n", rfkill->name);
593}
e49df67d 594static DEVICE_ATTR_RO(name);
19d337df
JB
595
596static const char *rfkill_get_type_str(enum rfkill_type type)
597{
44b3decb 598 BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_NFC + 1);
02f7f179 599
19d337df
JB
600 switch (type) {
601 case RFKILL_TYPE_WLAN:
602 return "wlan";
603 case RFKILL_TYPE_BLUETOOTH:
604 return "bluetooth";
605 case RFKILL_TYPE_UWB:
606 return "ultrawideband";
607 case RFKILL_TYPE_WIMAX:
608 return "wimax";
609 case RFKILL_TYPE_WWAN:
610 return "wwan";
3ad20149
TW
611 case RFKILL_TYPE_GPS:
612 return "gps";
875405a7
MH
613 case RFKILL_TYPE_FM:
614 return "fm";
44b3decb
SO
615 case RFKILL_TYPE_NFC:
616 return "nfc";
19d337df
JB
617 default:
618 BUG();
619 }
19d337df
JB
620}
621
e49df67d
GKH
622static ssize_t type_show(struct device *dev, struct device_attribute *attr,
623 char *buf)
19d337df
JB
624{
625 struct rfkill *rfkill = to_rfkill(dev);
626
627 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
628}
e49df67d 629static DEVICE_ATTR_RO(type);
19d337df 630
e49df67d
GKH
631static ssize_t index_show(struct device *dev, struct device_attribute *attr,
632 char *buf)
c64fb016
JB
633{
634 struct rfkill *rfkill = to_rfkill(dev);
635
636 return sprintf(buf, "%d\n", rfkill->idx);
637}
e49df67d 638static DEVICE_ATTR_RO(index);
c64fb016 639
e49df67d
GKH
640static ssize_t persistent_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
464902e8
AJ
642{
643 struct rfkill *rfkill = to_rfkill(dev);
644
645 return sprintf(buf, "%d\n", rfkill->persistent);
646}
e49df67d 647static DEVICE_ATTR_RO(persistent);
464902e8 648
e49df67d
GKH
649static ssize_t hard_show(struct device *dev, struct device_attribute *attr,
650 char *buf)
6c26361e 651{
652 struct rfkill *rfkill = to_rfkill(dev);
6c26361e 653
819bfecc 654 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 );
6c26361e 655}
e49df67d 656static DEVICE_ATTR_RO(hard);
6c26361e 657
e49df67d
GKH
658static ssize_t soft_show(struct device *dev, struct device_attribute *attr,
659 char *buf)
6c26361e 660{
661 struct rfkill *rfkill = to_rfkill(dev);
6c26361e 662
819bfecc 663 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 );
6c26361e 664}
665
e49df67d
GKH
666static ssize_t soft_store(struct device *dev, struct device_attribute *attr,
667 const char *buf, size_t count)
6c26361e 668{
669 struct rfkill *rfkill = to_rfkill(dev);
670 unsigned long state;
671 int err;
672
673 if (!capable(CAP_NET_ADMIN))
674 return -EPERM;
675
1bac92ca 676 err = kstrtoul(buf, 0, &state);
6c26361e 677 if (err)
678 return err;
679
680 if (state > 1 )
681 return -EINVAL;
682
683 mutex_lock(&rfkill_global_mutex);
684 rfkill_set_block(rfkill, state);
685 mutex_unlock(&rfkill_global_mutex);
686
6f7c962c 687 return count;
6c26361e 688}
e49df67d 689static DEVICE_ATTR_RW(soft);
6c26361e 690
19d337df
JB
691static u8 user_state_from_blocked(unsigned long state)
692{
693 if (state & RFKILL_BLOCK_HW)
694 return RFKILL_USER_STATE_HARD_BLOCKED;
695 if (state & RFKILL_BLOCK_SW)
696 return RFKILL_USER_STATE_SOFT_BLOCKED;
697
698 return RFKILL_USER_STATE_UNBLOCKED;
699}
700
e49df67d
GKH
701static ssize_t state_show(struct device *dev, struct device_attribute *attr,
702 char *buf)
19d337df
JB
703{
704 struct rfkill *rfkill = to_rfkill(dev);
19d337df 705
819bfecc 706 return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state));
19d337df
JB
707}
708
e49df67d
GKH
709static ssize_t state_store(struct device *dev, struct device_attribute *attr,
710 const char *buf, size_t count)
19d337df 711{
f54c1427
JB
712 struct rfkill *rfkill = to_rfkill(dev);
713 unsigned long state;
714 int err;
715
716 if (!capable(CAP_NET_ADMIN))
717 return -EPERM;
718
1bac92ca 719 err = kstrtoul(buf, 0, &state);
f54c1427
JB
720 if (err)
721 return err;
722
723 if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
724 state != RFKILL_USER_STATE_UNBLOCKED)
725 return -EINVAL;
726
727 mutex_lock(&rfkill_global_mutex);
728 rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
729 mutex_unlock(&rfkill_global_mutex);
19d337df 730
6f7c962c 731 return count;
19d337df 732}
e49df67d 733static DEVICE_ATTR_RW(state);
19d337df 734
e49df67d
GKH
735static ssize_t claim_show(struct device *dev, struct device_attribute *attr,
736 char *buf)
19d337df
JB
737{
738 return sprintf(buf, "%d\n", 0);
739}
e49df67d
GKH
740static DEVICE_ATTR_RO(claim);
741
742static struct attribute *rfkill_dev_attrs[] = {
743 &dev_attr_name.attr,
744 &dev_attr_type.attr,
745 &dev_attr_index.attr,
746 &dev_attr_persistent.attr,
747 &dev_attr_state.attr,
748 &dev_attr_claim.attr,
749 &dev_attr_soft.attr,
750 &dev_attr_hard.attr,
751 NULL,
19d337df 752};
e49df67d 753ATTRIBUTE_GROUPS(rfkill_dev);
19d337df
JB
754
755static void rfkill_release(struct device *dev)
756{
757 struct rfkill *rfkill = to_rfkill(dev);
758
759 kfree(rfkill);
760}
761
762static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
763{
764 struct rfkill *rfkill = to_rfkill(dev);
765 unsigned long flags;
766 u32 state;
767 int error;
768
769 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
770 if (error)
771 return error;
772 error = add_uevent_var(env, "RFKILL_TYPE=%s",
773 rfkill_get_type_str(rfkill->type));
774 if (error)
775 return error;
776 spin_lock_irqsave(&rfkill->lock, flags);
777 state = rfkill->state;
778 spin_unlock_irqrestore(&rfkill->lock, flags);
779 error = add_uevent_var(env, "RFKILL_STATE=%d",
780 user_state_from_blocked(state));
781 return error;
782}
783
784void rfkill_pause_polling(struct rfkill *rfkill)
785{
786 BUG_ON(!rfkill);
787
788 if (!rfkill->ops->poll)
789 return;
790
dd21dfc6 791 rfkill->polling_paused = true;
19d337df
JB
792 cancel_delayed_work_sync(&rfkill->poll_work);
793}
794EXPORT_SYMBOL(rfkill_pause_polling);
795
796void rfkill_resume_polling(struct rfkill *rfkill)
797{
798 BUG_ON(!rfkill);
799
800 if (!rfkill->ops->poll)
801 return;
802
dd21dfc6
JB
803 rfkill->polling_paused = false;
804
805 if (rfkill->suspended)
806 return;
807
67235cbc
SD
808 queue_delayed_work(system_power_efficient_wq,
809 &rfkill->poll_work, 0);
19d337df
JB
810}
811EXPORT_SYMBOL(rfkill_resume_polling);
812
28f297a7
LPC
813#ifdef CONFIG_PM_SLEEP
814static int rfkill_suspend(struct device *dev)
19d337df
JB
815{
816 struct rfkill *rfkill = to_rfkill(dev);
817
dd21dfc6
JB
818 rfkill->suspended = true;
819 cancel_delayed_work_sync(&rfkill->poll_work);
19d337df 820
19d337df
JB
821 return 0;
822}
823
824static int rfkill_resume(struct device *dev)
825{
826 struct rfkill *rfkill = to_rfkill(dev);
827 bool cur;
828
dd21dfc6
JB
829 rfkill->suspended = false;
830
06d5caf4
AJ
831 if (!rfkill->persistent) {
832 cur = !!(rfkill->state & RFKILL_BLOCK_SW);
833 rfkill_set_block(rfkill, cur);
834 }
19d337df 835
dd21dfc6
JB
836 if (rfkill->ops->poll && !rfkill->polling_paused)
837 queue_delayed_work(system_power_efficient_wq,
838 &rfkill->poll_work, 0);
19d337df
JB
839
840 return 0;
841}
842
28f297a7
LPC
843static SIMPLE_DEV_PM_OPS(rfkill_pm_ops, rfkill_suspend, rfkill_resume);
844#define RFKILL_PM_OPS (&rfkill_pm_ops)
845#else
846#define RFKILL_PM_OPS NULL
847#endif
848
19d337df
JB
849static struct class rfkill_class = {
850 .name = "rfkill",
851 .dev_release = rfkill_release,
e49df67d 852 .dev_groups = rfkill_dev_groups,
19d337df 853 .dev_uevent = rfkill_dev_uevent,
28f297a7 854 .pm = RFKILL_PM_OPS,
19d337df
JB
855};
856
6081162e
JB
857bool rfkill_blocked(struct rfkill *rfkill)
858{
859 unsigned long flags;
860 u32 state;
861
862 spin_lock_irqsave(&rfkill->lock, flags);
863 state = rfkill->state;
864 spin_unlock_irqrestore(&rfkill->lock, flags);
865
866 return !!(state & RFKILL_BLOCK_ANY);
867}
868EXPORT_SYMBOL(rfkill_blocked);
869
19d337df
JB
870
871struct rfkill * __must_check rfkill_alloc(const char *name,
872 struct device *parent,
873 const enum rfkill_type type,
874 const struct rfkill_ops *ops,
875 void *ops_data)
876{
877 struct rfkill *rfkill;
878 struct device *dev;
879
880 if (WARN_ON(!ops))
881 return NULL;
882
883 if (WARN_ON(!ops->set_block))
884 return NULL;
885
886 if (WARN_ON(!name))
887 return NULL;
888
c64fb016 889 if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
19d337df
JB
890 return NULL;
891
b7bb1100 892 rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
19d337df
JB
893 if (!rfkill)
894 return NULL;
895
896 spin_lock_init(&rfkill->lock);
897 INIT_LIST_HEAD(&rfkill->node);
898 rfkill->type = type;
b7bb1100 899 strcpy(rfkill->name, name);
19d337df
JB
900 rfkill->ops = ops;
901 rfkill->data = ops_data;
902
903 dev = &rfkill->dev;
904 dev->class = &rfkill_class;
905 dev->parent = parent;
906 device_initialize(dev);
907
908 return rfkill;
909}
910EXPORT_SYMBOL(rfkill_alloc);
911
912static void rfkill_poll(struct work_struct *work)
913{
914 struct rfkill *rfkill;
915
916 rfkill = container_of(work, struct rfkill, poll_work.work);
917
918 /*
919 * Poll hardware state -- driver will use one of the
920 * rfkill_set{,_hw,_sw}_state functions and use its
921 * return value to update the current status.
922 */
923 rfkill->ops->poll(rfkill, rfkill->data);
924
67235cbc
SD
925 queue_delayed_work(system_power_efficient_wq,
926 &rfkill->poll_work,
19d337df
JB
927 round_jiffies_relative(POLL_INTERVAL));
928}
929
930static void rfkill_uevent_work(struct work_struct *work)
931{
932 struct rfkill *rfkill;
933
934 rfkill = container_of(work, struct rfkill, uevent_work);
935
c64fb016
JB
936 mutex_lock(&rfkill_global_mutex);
937 rfkill_event(rfkill);
938 mutex_unlock(&rfkill_global_mutex);
19d337df
JB
939}
940
941static void rfkill_sync_work(struct work_struct *work)
942{
943 struct rfkill *rfkill;
944 bool cur;
945
946 rfkill = container_of(work, struct rfkill, sync_work);
947
948 mutex_lock(&rfkill_global_mutex);
949 cur = rfkill_global_states[rfkill->type].cur;
950 rfkill_set_block(rfkill, cur);
951 mutex_unlock(&rfkill_global_mutex);
952}
953
954int __must_check rfkill_register(struct rfkill *rfkill)
955{
956 static unsigned long rfkill_no;
957 struct device *dev = &rfkill->dev;
958 int error;
959
960 BUG_ON(!rfkill);
961
962 mutex_lock(&rfkill_global_mutex);
963
964 if (rfkill->registered) {
965 error = -EALREADY;
966 goto unlock;
967 }
968
c64fb016 969 rfkill->idx = rfkill_no;
19d337df
JB
970 dev_set_name(dev, "rfkill%lu", rfkill_no);
971 rfkill_no++;
972
19d337df
JB
973 list_add_tail(&rfkill->node, &rfkill_list);
974
975 error = device_add(dev);
976 if (error)
977 goto remove;
978
979 error = rfkill_led_trigger_register(rfkill);
980 if (error)
981 goto devdel;
982
983 rfkill->registered = true;
984
2ec2c68c 985 INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
19d337df 986 INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
19d337df 987 INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
2ec2c68c
JB
988
989 if (rfkill->ops->poll)
67235cbc
SD
990 queue_delayed_work(system_power_efficient_wq,
991 &rfkill->poll_work,
2ec2c68c 992 round_jiffies_relative(POLL_INTERVAL));
b3fa1329
AJ
993
994 if (!rfkill->persistent || rfkill_epo_lock_active) {
995 schedule_work(&rfkill->sync_work);
996 } else {
997#ifdef CONFIG_RFKILL_INPUT
998 bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
999
1000 if (!atomic_read(&rfkill_input_disabled))
1001 __rfkill_switch_all(rfkill->type, soft_blocked);
1002#endif
1003 }
2ec2c68c 1004
c64fb016 1005 rfkill_send_events(rfkill, RFKILL_OP_ADD);
19d337df
JB
1006
1007 mutex_unlock(&rfkill_global_mutex);
1008 return 0;
1009
1010 devdel:
1011 device_del(&rfkill->dev);
1012 remove:
1013 list_del_init(&rfkill->node);
1014 unlock:
1015 mutex_unlock(&rfkill_global_mutex);
1016 return error;
1017}
1018EXPORT_SYMBOL(rfkill_register);
1019
1020void rfkill_unregister(struct rfkill *rfkill)
1021{
1022 BUG_ON(!rfkill);
1023
1024 if (rfkill->ops->poll)
1025 cancel_delayed_work_sync(&rfkill->poll_work);
1026
1027 cancel_work_sync(&rfkill->uevent_work);
1028 cancel_work_sync(&rfkill->sync_work);
1029
1030 rfkill->registered = false;
1031
1032 device_del(&rfkill->dev);
1033
1034 mutex_lock(&rfkill_global_mutex);
c64fb016 1035 rfkill_send_events(rfkill, RFKILL_OP_DEL);
19d337df
JB
1036 list_del_init(&rfkill->node);
1037 mutex_unlock(&rfkill_global_mutex);
1038
1039 rfkill_led_trigger_unregister(rfkill);
1040}
1041EXPORT_SYMBOL(rfkill_unregister);
1042
1043void rfkill_destroy(struct rfkill *rfkill)
1044{
1045 if (rfkill)
1046 put_device(&rfkill->dev);
1047}
1048EXPORT_SYMBOL(rfkill_destroy);
1049
c64fb016
JB
1050static int rfkill_fop_open(struct inode *inode, struct file *file)
1051{
1052 struct rfkill_data *data;
1053 struct rfkill *rfkill;
1054 struct rfkill_int_event *ev, *tmp;
1055
1056 data = kzalloc(sizeof(*data), GFP_KERNEL);
1057 if (!data)
1058 return -ENOMEM;
1059
1060 INIT_LIST_HEAD(&data->events);
1061 mutex_init(&data->mtx);
1062 init_waitqueue_head(&data->read_wait);
1063
1064 mutex_lock(&rfkill_global_mutex);
1065 mutex_lock(&data->mtx);
1066 /*
1067 * start getting events from elsewhere but hold mtx to get
1068 * startup events added first
1069 */
c64fb016
JB
1070
1071 list_for_each_entry(rfkill, &rfkill_list, node) {
1072 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1073 if (!ev)
1074 goto free;
1075 rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
1076 list_add_tail(&ev->list, &data->events);
1077 }
bd2281b8 1078 list_add(&data->list, &rfkill_fds);
c64fb016
JB
1079 mutex_unlock(&data->mtx);
1080 mutex_unlock(&rfkill_global_mutex);
1081
1082 file->private_data = data;
1083
1084 return nonseekable_open(inode, file);
1085
1086 free:
1087 mutex_unlock(&data->mtx);
1088 mutex_unlock(&rfkill_global_mutex);
1089 mutex_destroy(&data->mtx);
1090 list_for_each_entry_safe(ev, tmp, &data->events, list)
1091 kfree(ev);
1092 kfree(data);
1093 return -ENOMEM;
1094}
1095
1096static unsigned int rfkill_fop_poll(struct file *file, poll_table *wait)
1097{
1098 struct rfkill_data *data = file->private_data;
1099 unsigned int res = POLLOUT | POLLWRNORM;
1100
1101 poll_wait(file, &data->read_wait, wait);
1102
1103 mutex_lock(&data->mtx);
1104 if (!list_empty(&data->events))
1105 res = POLLIN | POLLRDNORM;
1106 mutex_unlock(&data->mtx);
1107
1108 return res;
1109}
1110
1111static bool rfkill_readable(struct rfkill_data *data)
1112{
1113 bool r;
1114
1115 mutex_lock(&data->mtx);
1116 r = !list_empty(&data->events);
1117 mutex_unlock(&data->mtx);
1118
1119 return r;
1120}
1121
1122static ssize_t rfkill_fop_read(struct file *file, char __user *buf,
1123 size_t count, loff_t *pos)
1124{
1125 struct rfkill_data *data = file->private_data;
1126 struct rfkill_int_event *ev;
1127 unsigned long sz;
1128 int ret;
1129
1130 mutex_lock(&data->mtx);
1131
1132 while (list_empty(&data->events)) {
1133 if (file->f_flags & O_NONBLOCK) {
1134 ret = -EAGAIN;
1135 goto out;
1136 }
1137 mutex_unlock(&data->mtx);
1138 ret = wait_event_interruptible(data->read_wait,
1139 rfkill_readable(data));
1140 mutex_lock(&data->mtx);
1141
1142 if (ret)
1143 goto out;
1144 }
1145
1146 ev = list_first_entry(&data->events, struct rfkill_int_event,
1147 list);
1148
1149 sz = min_t(unsigned long, sizeof(ev->ev), count);
1150 ret = sz;
1151 if (copy_to_user(buf, &ev->ev, sz))
1152 ret = -EFAULT;
1153
1154 list_del(&ev->list);
1155 kfree(ev);
1156 out:
1157 mutex_unlock(&data->mtx);
1158 return ret;
1159}
1160
1161static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
1162 size_t count, loff_t *pos)
1163{
1164 struct rfkill *rfkill;
1165 struct rfkill_event ev;
1166
1167 /* we don't need the 'hard' variable but accept it */
1be491fc 1168 if (count < RFKILL_EVENT_SIZE_V1 - 1)
c64fb016
JB
1169 return -EINVAL;
1170
1be491fc
JB
1171 /*
1172 * Copy as much data as we can accept into our 'ev' buffer,
1173 * but tell userspace how much we've copied so it can determine
1174 * our API version even in a write() call, if it cares.
1175 */
1176 count = min(count, sizeof(ev));
1177 if (copy_from_user(&ev, buf, count))
c64fb016
JB
1178 return -EFAULT;
1179
1180 if (ev.op != RFKILL_OP_CHANGE && ev.op != RFKILL_OP_CHANGE_ALL)
1181 return -EINVAL;
1182
1183 if (ev.type >= NUM_RFKILL_TYPES)
1184 return -EINVAL;
1185
1186 mutex_lock(&rfkill_global_mutex);
1187
1188 if (ev.op == RFKILL_OP_CHANGE_ALL) {
1189 if (ev.type == RFKILL_TYPE_ALL) {
1190 enum rfkill_type i;
1191 for (i = 0; i < NUM_RFKILL_TYPES; i++)
1192 rfkill_global_states[i].cur = ev.soft;
1193 } else {
1194 rfkill_global_states[ev.type].cur = ev.soft;
1195 }
1196 }
1197
1198 list_for_each_entry(rfkill, &rfkill_list, node) {
1199 if (rfkill->idx != ev.idx && ev.op != RFKILL_OP_CHANGE_ALL)
1200 continue;
1201
1202 if (rfkill->type != ev.type && ev.type != RFKILL_TYPE_ALL)
1203 continue;
1204
1205 rfkill_set_block(rfkill, ev.soft);
1206 }
1207 mutex_unlock(&rfkill_global_mutex);
1208
1209 return count;
1210}
1211
1212static int rfkill_fop_release(struct inode *inode, struct file *file)
1213{
1214 struct rfkill_data *data = file->private_data;
1215 struct rfkill_int_event *ev, *tmp;
1216
1217 mutex_lock(&rfkill_global_mutex);
1218 list_del(&data->list);
1219 mutex_unlock(&rfkill_global_mutex);
1220
1221 mutex_destroy(&data->mtx);
1222 list_for_each_entry_safe(ev, tmp, &data->events, list)
1223 kfree(ev);
1224
1225#ifdef CONFIG_RFKILL_INPUT
1226 if (data->input_handler)
207ee162
JB
1227 if (atomic_dec_return(&rfkill_input_disabled) == 0)
1228 printk(KERN_DEBUG "rfkill: input handler enabled\n");
c64fb016
JB
1229#endif
1230
1231 kfree(data);
1232
1233 return 0;
1234}
1235
1236#ifdef CONFIG_RFKILL_INPUT
1237static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
1238 unsigned long arg)
1239{
1240 struct rfkill_data *data = file->private_data;
1241
1242 if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
1243 return -ENOSYS;
1244
1245 if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
1246 return -ENOSYS;
1247
1248 mutex_lock(&data->mtx);
1249
1250 if (!data->input_handler) {
207ee162
JB
1251 if (atomic_inc_return(&rfkill_input_disabled) == 1)
1252 printk(KERN_DEBUG "rfkill: input handler disabled\n");
c64fb016
JB
1253 data->input_handler = true;
1254 }
1255
1256 mutex_unlock(&data->mtx);
1257
1258 return 0;
1259}
1260#endif
1261
1262static const struct file_operations rfkill_fops = {
45ba564d 1263 .owner = THIS_MODULE,
c64fb016
JB
1264 .open = rfkill_fop_open,
1265 .read = rfkill_fop_read,
1266 .write = rfkill_fop_write,
1267 .poll = rfkill_fop_poll,
1268 .release = rfkill_fop_release,
1269#ifdef CONFIG_RFKILL_INPUT
1270 .unlocked_ioctl = rfkill_fop_ioctl,
1271 .compat_ioctl = rfkill_fop_ioctl,
1272#endif
6038f373 1273 .llseek = no_llseek,
c64fb016
JB
1274};
1275
1276static struct miscdevice rfkill_miscdev = {
1277 .name = "rfkill",
1278 .fops = &rfkill_fops,
1279 .minor = MISC_DYNAMIC_MINOR,
1280};
19d337df
JB
1281
1282static int __init rfkill_init(void)
1283{
1284 int error;
1285 int i;
1286
1287 for (i = 0; i < NUM_RFKILL_TYPES; i++)
b3fa1329 1288 rfkill_global_states[i].cur = !rfkill_default_state;
19d337df
JB
1289
1290 error = class_register(&rfkill_class);
1291 if (error)
1292 goto out;
1293
c64fb016
JB
1294 error = misc_register(&rfkill_miscdev);
1295 if (error) {
1296 class_unregister(&rfkill_class);
1297 goto out;
1298 }
1299
19d337df
JB
1300#ifdef CONFIG_RFKILL_INPUT
1301 error = rfkill_handler_init();
c64fb016
JB
1302 if (error) {
1303 misc_deregister(&rfkill_miscdev);
19d337df 1304 class_unregister(&rfkill_class);
c64fb016
JB
1305 goto out;
1306 }
19d337df
JB
1307#endif
1308
1309 out:
1310 return error;
1311}
1312subsys_initcall(rfkill_init);
1313
1314static void __exit rfkill_exit(void)
1315{
1316#ifdef CONFIG_RFKILL_INPUT
1317 rfkill_handler_exit();
1318#endif
c64fb016 1319 misc_deregister(&rfkill_miscdev);
19d337df
JB
1320 class_unregister(&rfkill_class);
1321}
1322module_exit(rfkill_exit);
This page took 0.420415 seconds and 5 git commands to generate.