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