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