blob: 166439995f34d2cb3944f2475e02a6774abd0bdb [file] [log] [blame]
Johannes Berg19d337d2009-06-02 13:01:37 +02001/*
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
Jeff Kirsher8232f1f2013-12-06 03:32:14 -080017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Johannes Berg19d337d2009-06-02 13:01:37 +020018 */
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>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040028#include <linux/sched.h>
Johannes Berg19d337d2009-06-02 13:01:37 +020029#include <linux/spinlock.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050030#include <linux/device.h>
Johannes Bergc64fb012009-06-02 13:01:38 +020031#include <linux/miscdevice.h>
32#include <linux/wait.h>
33#include <linux/poll.h>
34#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Johannes Berg19d337d2009-06-02 13:01:37 +020036
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
Johannes Berg19d337d2009-06-02 13:01:37 +020052 enum rfkill_type type;
53
54 unsigned long state;
55
Johannes Bergc64fb012009-06-02 13:01:38 +020056 u32 idx;
57
Johannes Berg19d337d2009-06-02 13:01:37 +020058 bool registered;
Alan Jenkinsb3fa1322009-06-08 13:27:27 +010059 bool persistent;
Johannes Bergdd21dfc2016-01-20 10:39:23 +010060 bool polling_paused;
61 bool suspended;
Johannes Berg19d337d2009-06-02 13:01:37 +020062
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;
Johannes Bergb7bb1102015-12-10 10:37:51 +010077 char name[];
Johannes Berg19d337d2009-06-02 13:01:37 +020078};
79#define to_rfkill(d) container_of(d, struct rfkill, dev)
80
Johannes Bergc64fb012009-06-02 13:01:38 +020081struct 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};
Johannes Berg19d337d2009-06-02 13:01:37 +020093
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);
Johannes Bergc64fb012009-06-02 13:01:38 +0200114static LIST_HEAD(rfkill_fds); /* list of open fds of /dev/rfkill */
Johannes Berg19d337d2009-06-02 13:01:37 +0200115
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 {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100122 bool cur, sav;
Johannes Berg19d337d2009-06-02 13:01:37 +0200123} rfkill_global_states[NUM_RFKILL_TYPES];
124
Johannes Berg19d337d2009-06-02 13:01:37 +0200125static 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
AceLan Kao06d7de82012-07-26 09:51:08 +0800153const 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
Johannes Berg19d337d2009-06-02 13:01:37 +0200167static 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
Johannes Bergc64fb012009-06-02 13:01:38 +0200194static 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)
Johannes Berg19d337d2009-06-02 13:01:37 +0200228{
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100229 if (!rfkill->registered)
Johannes Berg19d337d2009-06-02 13:01:37 +0200230 return;
231
232 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
Johannes Bergc64fb012009-06-02 13:01:38 +0200233
234 /* also send event to /dev/rfkill */
235 rfkill_send_events(rfkill, RFKILL_OP_CHANGE);
Johannes Berg19d337d2009-06-02 13:01:37 +0200236}
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;
Mohammed Shafi Shajakhan6be19cc2011-09-16 19:03:40 +0530253 any = !!(rfkill->state & RFKILL_BLOCK_ANY);
Johannes Berg19d337d2009-06-02 13:01:37 +0200254 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;
Vitaly Wooleab48342012-09-06 16:06:52 +0200273 bool prev, curr;
Johannes Berg19d337d2009-06-02 13:01:37 +0200274 int err;
275
Alan Jenkins7fa20a72009-06-16 14:53:24 +0100276 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
277 return;
278
Johannes Berg19d337d2009-06-02 13:01:37 +0200279 /*
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);
Vitaly Wooleab48342012-09-06 16:06:52 +0200288 prev = rfkill->state & RFKILL_BLOCK_SW;
289
Johannes Berg19d337d2009-06-02 13:01:37 +0200290 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
Johannes Berg19d337d2009-06-02 13:01:37 +0200303 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;
Vitaly Wooleab48342012-09-06 16:06:52 +0200319 curr = rfkill->state & RFKILL_BLOCK_SW;
Johannes Berg19d337d2009-06-02 13:01:37 +0200320 spin_unlock_irqrestore(&rfkill->lock, flags);
321
322 rfkill_led_trigger_event(rfkill);
Vitaly Wooleab48342012-09-06 16:06:52 +0200323
324 if (prev != curr)
325 rfkill_event(rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200326}
327
Johannes Bergc64fb012009-06-02 13:01:38 +0200328#ifdef CONFIG_RFKILL_INPUT
329static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
330
Johannes Berg19d337d2009-06-02 13:01:37 +0200331/**
332 * __rfkill_switch_all - Toggle state of all switches of given type
333 * @type: type of interfaces to be affected
Fabian Frederick2f29fed2014-10-07 22:20:23 +0200334 * @blocked: the new state
Johannes Berg19d337d2009-06-02 13:01:37 +0200335 *
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
João Paulo Rechi Vita4c077892015-08-25 08:56:43 -0400346 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
Johannes Berg19d337d2009-06-02 13:01:37 +0200355 list_for_each_entry(rfkill, &rfkill_list, node) {
Alex Hung27e49ca2012-05-23 14:11:52 +0800356 if (rfkill->type != type && type != RFKILL_TYPE_ALL)
Johannes Berg19d337d2009-06-02 13:01:37 +0200357 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
Fabian Frederick2f29fed2014-10-07 22:20:23 +0200366 * @blocked: the new state
Johannes Berg19d337d2009-06-02 13:01:37 +0200367 *
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{
Johannes Bergc64fb012009-06-02 13:01:38 +0200375 if (atomic_read(&rfkill_input_disabled))
376 return;
377
Johannes Berg19d337d2009-06-02 13:01:37 +0200378 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
Johannes Bergc64fb012009-06-02 13:01:38 +0200400 if (atomic_read(&rfkill_input_disabled))
401 return;
402
Johannes Berg19d337d2009-06-02 13:01:37 +0200403 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++) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100410 rfkill_global_states[i].sav = rfkill_global_states[i].cur;
Johannes Berg19d337d2009-06-02 13:01:37 +0200411 rfkill_global_states[i].cur = true;
412 }
Johannes Bergc64fb012009-06-02 13:01:38 +0200413
Johannes Berg19d337d2009-06-02 13:01:37 +0200414 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
Johannes Bergc64fb012009-06-02 13:01:38 +0200428 if (atomic_read(&rfkill_input_disabled))
429 return;
430
Johannes Berg19d337d2009-06-02 13:01:37 +0200431 mutex_lock(&rfkill_global_mutex);
432
433 rfkill_epo_lock_active = false;
434 for (i = 0; i < NUM_RFKILL_TYPES; i++)
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100435 __rfkill_switch_all(i, rfkill_global_states[i].sav);
Johannes Berg19d337d2009-06-02 13:01:37 +0200436 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{
Johannes Bergc64fb012009-06-02 13:01:38 +0200447 if (atomic_read(&rfkill_input_disabled))
448 return;
449
Johannes Berg19d337d2009-06-02 13:01:37 +0200450 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}
Johannes Bergc64fb012009-06-02 13:01:38 +0200480#endif
Johannes Berg19d337d2009-06-02 13:01:37 +0200481
Johannes Berg19d337d2009-06-02 13:01:37 +0200482
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
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100527 if (!rfkill->registered)
528 return blocked;
Johannes Berg19d337d2009-06-02 13:01:37 +0200529
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100530 if (prev != blocked && !hwblock)
531 schedule_work(&rfkill->uevent_work);
532
533 rfkill_led_trigger_event(rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200534
535 return blocked;
536}
537EXPORT_SYMBOL(rfkill_set_sw_state);
538
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100539void 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
Johannes Berg19d337d2009-06-02 13:01:37 +0200553void 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);
Alan Jenkins48ab3572009-07-12 17:03:13 +0100569 if (hw)
570 rfkill->state |= RFKILL_BLOCK_HW;
571 else
572 rfkill->state &= ~RFKILL_BLOCK_HW;
Johannes Berg19d337d2009-06-02 13:01:37 +0200573
574 spin_unlock_irqrestore(&rfkill->lock, flags);
575
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100576 if (!rfkill->registered) {
577 rfkill->persistent = true;
578 } else {
579 if (swprev != sw || hwprev != hw)
580 schedule_work(&rfkill->uevent_work);
Johannes Berg19d337d2009-06-02 13:01:37 +0200581
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100582 rfkill_led_trigger_event(rfkill);
583 }
Johannes Berg19d337d2009-06-02 13:01:37 +0200584}
585EXPORT_SYMBOL(rfkill_set_states);
586
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700587static ssize_t name_show(struct device *dev, struct device_attribute *attr,
588 char *buf)
Johannes Berg19d337d2009-06-02 13:01:37 +0200589{
590 struct rfkill *rfkill = to_rfkill(dev);
591
592 return sprintf(buf, "%s\n", rfkill->name);
593}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700594static DEVICE_ATTR_RO(name);
Johannes Berg19d337d2009-06-02 13:01:37 +0200595
596static const char *rfkill_get_type_str(enum rfkill_type type)
597{
Samuel Ortiz44b3dec2013-04-11 11:51:36 +0200598 BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_NFC + 1);
Andrew Morton02f7f172009-12-03 20:45:07 -0800599
Johannes Berg19d337d2009-06-02 13:01:37 +0200600 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";
Tomas Winkler3ad20142009-08-02 02:36:49 +0300611 case RFKILL_TYPE_GPS:
612 return "gps";
Marcel Holtmann875405a2009-11-18 16:48:01 +0100613 case RFKILL_TYPE_FM:
614 return "fm";
Samuel Ortiz44b3dec2013-04-11 11:51:36 +0200615 case RFKILL_TYPE_NFC:
616 return "nfc";
Johannes Berg19d337d2009-06-02 13:01:37 +0200617 default:
618 BUG();
619 }
Johannes Berg19d337d2009-06-02 13:01:37 +0200620}
621
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700622static ssize_t type_show(struct device *dev, struct device_attribute *attr,
623 char *buf)
Johannes Berg19d337d2009-06-02 13:01:37 +0200624{
625 struct rfkill *rfkill = to_rfkill(dev);
626
627 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
628}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700629static DEVICE_ATTR_RO(type);
Johannes Berg19d337d2009-06-02 13:01:37 +0200630
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700631static ssize_t index_show(struct device *dev, struct device_attribute *attr,
632 char *buf)
Johannes Bergc64fb012009-06-02 13:01:38 +0200633{
634 struct rfkill *rfkill = to_rfkill(dev);
635
636 return sprintf(buf, "%d\n", rfkill->idx);
637}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700638static DEVICE_ATTR_RO(index);
Johannes Bergc64fb012009-06-02 13:01:38 +0200639
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700640static ssize_t persistent_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
Alan Jenkins464902e2009-06-16 14:54:04 +0100642{
643 struct rfkill *rfkill = to_rfkill(dev);
644
645 return sprintf(buf, "%d\n", rfkill->persistent);
646}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700647static DEVICE_ATTR_RO(persistent);
Alan Jenkins464902e2009-06-16 14:54:04 +0100648
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700649static ssize_t hard_show(struct device *dev, struct device_attribute *attr,
650 char *buf)
florian@mickler.org6c263612010-02-26 12:01:34 +0100651{
652 struct rfkill *rfkill = to_rfkill(dev);
florian@mickler.org6c263612010-02-26 12:01:34 +0100653
florian@mickler.org819bfec2010-03-13 13:31:05 +0100654 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 );
florian@mickler.org6c263612010-02-26 12:01:34 +0100655}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700656static DEVICE_ATTR_RO(hard);
florian@mickler.org6c263612010-02-26 12:01:34 +0100657
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700658static ssize_t soft_show(struct device *dev, struct device_attribute *attr,
659 char *buf)
florian@mickler.org6c263612010-02-26 12:01:34 +0100660{
661 struct rfkill *rfkill = to_rfkill(dev);
florian@mickler.org6c263612010-02-26 12:01:34 +0100662
florian@mickler.org819bfec2010-03-13 13:31:05 +0100663 return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 );
florian@mickler.org6c263612010-02-26 12:01:34 +0100664}
665
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700666static ssize_t soft_store(struct device *dev, struct device_attribute *attr,
667 const char *buf, size_t count)
florian@mickler.org6c263612010-02-26 12:01:34 +0100668{
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
Julia Lawall1bac92c2011-11-06 14:26:49 +0100676 err = kstrtoul(buf, 0, &state);
florian@mickler.org6c263612010-02-26 12:01:34 +0100677 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
Alan Cox6f7c9622012-10-25 15:18:34 +0100687 return count;
florian@mickler.org6c263612010-02-26 12:01:34 +0100688}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700689static DEVICE_ATTR_RW(soft);
florian@mickler.org6c263612010-02-26 12:01:34 +0100690
Johannes Berg19d337d2009-06-02 13:01:37 +0200691static 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
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700701static ssize_t state_show(struct device *dev, struct device_attribute *attr,
702 char *buf)
Johannes Berg19d337d2009-06-02 13:01:37 +0200703{
704 struct rfkill *rfkill = to_rfkill(dev);
Johannes Berg19d337d2009-06-02 13:01:37 +0200705
florian@mickler.org819bfec2010-03-13 13:31:05 +0100706 return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state));
Johannes Berg19d337d2009-06-02 13:01:37 +0200707}
708
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700709static ssize_t state_store(struct device *dev, struct device_attribute *attr,
710 const char *buf, size_t count)
Johannes Berg19d337d2009-06-02 13:01:37 +0200711{
Johannes Bergf54c1422009-07-10 21:41:39 +0200712 struct rfkill *rfkill = to_rfkill(dev);
713 unsigned long state;
714 int err;
Johannes Berg19d337d2009-06-02 13:01:37 +0200715
Johannes Bergf54c1422009-07-10 21:41:39 +0200716 if (!capable(CAP_NET_ADMIN))
717 return -EPERM;
718
Julia Lawall1bac92c2011-11-06 14:26:49 +0100719 err = kstrtoul(buf, 0, &state);
Johannes Bergf54c1422009-07-10 21:41:39 +0200720 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);
730
Alan Cox6f7c9622012-10-25 15:18:34 +0100731 return count;
Johannes Berg19d337d2009-06-02 13:01:37 +0200732}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700733static DEVICE_ATTR_RW(state);
Johannes Berg19d337d2009-06-02 13:01:37 +0200734
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700735static ssize_t claim_show(struct device *dev, struct device_attribute *attr,
736 char *buf)
Johannes Berg19d337d2009-06-02 13:01:37 +0200737{
738 return sprintf(buf, "%d\n", 0);
739}
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700740static DEVICE_ATTR_RO(claim);
Johannes Berg19d337d2009-06-02 13:01:37 +0200741
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700742static 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,
Johannes Berg19d337d2009-06-02 13:01:37 +0200752};
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700753ATTRIBUTE_GROUPS(rfkill_dev);
Johannes Berg19d337d2009-06-02 13:01:37 +0200754
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
Johannes Bergdd21dfc2016-01-20 10:39:23 +0100791 rfkill->polling_paused = true;
Johannes Berg19d337d2009-06-02 13:01:37 +0200792 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
Johannes Bergdd21dfc2016-01-20 10:39:23 +0100803 rfkill->polling_paused = false;
804
805 if (rfkill->suspended)
806 return;
807
Shaibal Dutta67235cb2014-01-30 14:43:34 -0800808 queue_delayed_work(system_power_efficient_wq,
809 &rfkill->poll_work, 0);
Johannes Berg19d337d2009-06-02 13:01:37 +0200810}
811EXPORT_SYMBOL(rfkill_resume_polling);
812
Lars-Peter Clausen28f297a2015-05-16 14:23:55 +0200813#ifdef CONFIG_PM_SLEEP
814static int rfkill_suspend(struct device *dev)
Johannes Berg19d337d2009-06-02 13:01:37 +0200815{
816 struct rfkill *rfkill = to_rfkill(dev);
817
Johannes Bergdd21dfc2016-01-20 10:39:23 +0100818 rfkill->suspended = true;
819 cancel_delayed_work_sync(&rfkill->poll_work);
Johannes Berg19d337d2009-06-02 13:01:37 +0200820
Johannes Berg19d337d2009-06-02 13:01:37 +0200821 return 0;
822}
823
824static int rfkill_resume(struct device *dev)
825{
826 struct rfkill *rfkill = to_rfkill(dev);
827 bool cur;
828
Johannes Bergdd21dfc2016-01-20 10:39:23 +0100829 rfkill->suspended = false;
830
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100831 if (!rfkill->persistent) {
832 cur = !!(rfkill->state & RFKILL_BLOCK_SW);
833 rfkill_set_block(rfkill, cur);
834 }
Johannes Berg19d337d2009-06-02 13:01:37 +0200835
Johannes Bergdd21dfc2016-01-20 10:39:23 +0100836 if (rfkill->ops->poll && !rfkill->polling_paused)
837 queue_delayed_work(system_power_efficient_wq,
838 &rfkill->poll_work, 0);
Johannes Berg19d337d2009-06-02 13:01:37 +0200839
840 return 0;
841}
842
Lars-Peter Clausen28f297a2015-05-16 14:23:55 +0200843static 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
Johannes Berg19d337d2009-06-02 13:01:37 +0200849static struct class rfkill_class = {
850 .name = "rfkill",
851 .dev_release = rfkill_release,
Greg Kroah-Hartmane49df672013-07-24 15:05:36 -0700852 .dev_groups = rfkill_dev_groups,
Johannes Berg19d337d2009-06-02 13:01:37 +0200853 .dev_uevent = rfkill_dev_uevent,
Lars-Peter Clausen28f297a2015-05-16 14:23:55 +0200854 .pm = RFKILL_PM_OPS,
Johannes Berg19d337d2009-06-02 13:01:37 +0200855};
856
Johannes Berg60811622009-06-02 13:01:40 +0200857bool 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
Johannes Berg19d337d2009-06-02 13:01:37 +0200870
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
Johannes Bergc64fb012009-06-02 13:01:38 +0200889 if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
Johannes Berg19d337d2009-06-02 13:01:37 +0200890 return NULL;
891
Johannes Bergb7bb1102015-12-10 10:37:51 +0100892 rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
Johannes Berg19d337d2009-06-02 13:01:37 +0200893 if (!rfkill)
894 return NULL;
895
896 spin_lock_init(&rfkill->lock);
897 INIT_LIST_HEAD(&rfkill->node);
898 rfkill->type = type;
Johannes Bergb7bb1102015-12-10 10:37:51 +0100899 strcpy(rfkill->name, name);
Johannes Berg19d337d2009-06-02 13:01:37 +0200900 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
Shaibal Dutta67235cb2014-01-30 14:43:34 -0800925 queue_delayed_work(system_power_efficient_wq,
926 &rfkill->poll_work,
Johannes Berg19d337d2009-06-02 13:01:37 +0200927 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
Johannes Bergc64fb012009-06-02 13:01:38 +0200936 mutex_lock(&rfkill_global_mutex);
937 rfkill_event(rfkill);
938 mutex_unlock(&rfkill_global_mutex);
Johannes Berg19d337d2009-06-02 13:01:37 +0200939}
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
Johannes Bergc64fb012009-06-02 13:01:38 +0200969 rfkill->idx = rfkill_no;
Johannes Berg19d337d2009-06-02 13:01:37 +0200970 dev_set_name(dev, "rfkill%lu", rfkill_no);
971 rfkill_no++;
972
Johannes Berg19d337d2009-06-02 13:01:37 +0200973 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
Johannes Berg2ec2c682009-06-03 09:55:29 +0200985 INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
986 INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
987 INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
988
989 if (rfkill->ops->poll)
Shaibal Dutta67235cb2014-01-30 14:43:34 -0800990 queue_delayed_work(system_power_efficient_wq,
991 &rfkill->poll_work,
Johannes Berg19d337d2009-06-02 13:01:37 +0200992 round_jiffies_relative(POLL_INTERVAL));
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100993
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 }
Johannes Berg2ec2c682009-06-03 09:55:29 +02001004
Johannes Bergc64fb012009-06-02 13:01:38 +02001005 rfkill_send_events(rfkill, RFKILL_OP_ADD);
Johannes Berg19d337d2009-06-02 13:01:37 +02001006
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);
Johannes Bergc64fb012009-06-02 13:01:38 +02001035 rfkill_send_events(rfkill, RFKILL_OP_DEL);
Johannes Berg19d337d2009-06-02 13:01:37 +02001036 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
Johannes Bergc64fb012009-06-02 13:01:38 +02001050static 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 */
Johannes Bergc64fb012009-06-02 13:01:38 +02001070
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 }
Julia Lawallbd2281b2011-05-13 15:52:10 +02001078 list_add(&data->list, &rfkill_fds);
Johannes Bergc64fb012009-06-02 13:01:38 +02001079 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 */
Johannes Berg1be491f2009-07-05 14:51:06 +02001168 if (count < RFKILL_EVENT_SIZE_V1 - 1)
Johannes Bergc64fb012009-06-02 13:01:38 +02001169 return -EINVAL;
1170
Johannes Berg1be491f2009-07-05 14:51:06 +02001171 /*
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))
Johannes Bergc64fb012009-06-02 13:01:38 +02001178 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)
Johannes Berg207ee162009-06-07 12:26:52 +02001227 if (atomic_dec_return(&rfkill_input_disabled) == 0)
1228 printk(KERN_DEBUG "rfkill: input handler enabled\n");
Johannes Bergc64fb012009-06-02 13:01:38 +02001229#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) {
Johannes Berg207ee162009-06-07 12:26:52 +02001251 if (atomic_inc_return(&rfkill_input_disabled) == 1)
1252 printk(KERN_DEBUG "rfkill: input handler disabled\n");
Johannes Bergc64fb012009-06-02 13:01:38 +02001253 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 = {
Johannes Berg45ba5642009-11-23 11:27:30 +01001263 .owner = THIS_MODULE,
Johannes Bergc64fb012009-06-02 13:01:38 +02001264 .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
Arnd Bergmann6038f372010-08-15 18:52:59 +02001273 .llseek = no_llseek,
Johannes Bergc64fb012009-06-02 13:01:38 +02001274};
1275
1276static struct miscdevice rfkill_miscdev = {
1277 .name = "rfkill",
1278 .fops = &rfkill_fops,
1279 .minor = MISC_DYNAMIC_MINOR,
1280};
Johannes Berg19d337d2009-06-02 13:01:37 +02001281
1282static int __init rfkill_init(void)
1283{
1284 int error;
1285 int i;
1286
1287 for (i = 0; i < NUM_RFKILL_TYPES; i++)
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001288 rfkill_global_states[i].cur = !rfkill_default_state;
Johannes Berg19d337d2009-06-02 13:01:37 +02001289
1290 error = class_register(&rfkill_class);
1291 if (error)
1292 goto out;
1293
Johannes Bergc64fb012009-06-02 13:01:38 +02001294 error = misc_register(&rfkill_miscdev);
1295 if (error) {
1296 class_unregister(&rfkill_class);
1297 goto out;
1298 }
1299
Johannes Berg19d337d2009-06-02 13:01:37 +02001300#ifdef CONFIG_RFKILL_INPUT
1301 error = rfkill_handler_init();
Johannes Bergc64fb012009-06-02 13:01:38 +02001302 if (error) {
1303 misc_deregister(&rfkill_miscdev);
Johannes Berg19d337d2009-06-02 13:01:37 +02001304 class_unregister(&rfkill_class);
Johannes Bergc64fb012009-06-02 13:01:38 +02001305 goto out;
1306 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001307#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
Johannes Bergc64fb012009-06-02 13:01:38 +02001319 misc_deregister(&rfkill_miscdev);
Johannes Berg19d337d2009-06-02 13:01:37 +02001320 class_unregister(&rfkill_class);
1321}
1322module_exit(rfkill_exit);