162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2006 - 2007 Ivo van Doorn
462306a36Sopenharmony_ci * Copyright (C) 2007 Dmitry Torokhov
562306a36Sopenharmony_ci * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/kernel.h>
962306a36Sopenharmony_ci#include <linux/module.h>
1062306a36Sopenharmony_ci#include <linux/init.h>
1162306a36Sopenharmony_ci#include <linux/workqueue.h>
1262306a36Sopenharmony_ci#include <linux/capability.h>
1362306a36Sopenharmony_ci#include <linux/list.h>
1462306a36Sopenharmony_ci#include <linux/mutex.h>
1562306a36Sopenharmony_ci#include <linux/rfkill.h>
1662306a36Sopenharmony_ci#include <linux/sched.h>
1762306a36Sopenharmony_ci#include <linux/spinlock.h>
1862306a36Sopenharmony_ci#include <linux/device.h>
1962306a36Sopenharmony_ci#include <linux/miscdevice.h>
2062306a36Sopenharmony_ci#include <linux/wait.h>
2162306a36Sopenharmony_ci#include <linux/poll.h>
2262306a36Sopenharmony_ci#include <linux/fs.h>
2362306a36Sopenharmony_ci#include <linux/slab.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include "rfkill.h"
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define POLL_INTERVAL		(5 * HZ)
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define RFKILL_BLOCK_HW		BIT(0)
3062306a36Sopenharmony_ci#define RFKILL_BLOCK_SW		BIT(1)
3162306a36Sopenharmony_ci#define RFKILL_BLOCK_SW_PREV	BIT(2)
3262306a36Sopenharmony_ci#define RFKILL_BLOCK_ANY	(RFKILL_BLOCK_HW |\
3362306a36Sopenharmony_ci				 RFKILL_BLOCK_SW |\
3462306a36Sopenharmony_ci				 RFKILL_BLOCK_SW_PREV)
3562306a36Sopenharmony_ci#define RFKILL_BLOCK_SW_SETCALL	BIT(31)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct rfkill {
3862306a36Sopenharmony_ci	spinlock_t		lock;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	enum rfkill_type	type;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	unsigned long		state;
4362306a36Sopenharmony_ci	unsigned long		hard_block_reasons;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	u32			idx;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	bool			registered;
4862306a36Sopenharmony_ci	bool			persistent;
4962306a36Sopenharmony_ci	bool			polling_paused;
5062306a36Sopenharmony_ci	bool			suspended;
5162306a36Sopenharmony_ci	bool			need_sync;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	const struct rfkill_ops	*ops;
5462306a36Sopenharmony_ci	void			*data;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_LEDS
5762306a36Sopenharmony_ci	struct led_trigger	led_trigger;
5862306a36Sopenharmony_ci	const char		*ledtrigname;
5962306a36Sopenharmony_ci#endif
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	struct device		dev;
6262306a36Sopenharmony_ci	struct list_head	node;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	struct delayed_work	poll_work;
6562306a36Sopenharmony_ci	struct work_struct	uevent_work;
6662306a36Sopenharmony_ci	struct work_struct	sync_work;
6762306a36Sopenharmony_ci	char			name[];
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ci#define to_rfkill(d)	container_of(d, struct rfkill, dev)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistruct rfkill_int_event {
7262306a36Sopenharmony_ci	struct list_head	list;
7362306a36Sopenharmony_ci	struct rfkill_event_ext	ev;
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistruct rfkill_data {
7762306a36Sopenharmony_ci	struct list_head	list;
7862306a36Sopenharmony_ci	struct list_head	events;
7962306a36Sopenharmony_ci	struct mutex		mtx;
8062306a36Sopenharmony_ci	wait_queue_head_t	read_wait;
8162306a36Sopenharmony_ci	bool			input_handler;
8262306a36Sopenharmony_ci	u8			max_size;
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ciMODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
8762306a36Sopenharmony_ciMODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
8862306a36Sopenharmony_ciMODULE_DESCRIPTION("RF switch support");
8962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/*
9362306a36Sopenharmony_ci * The locking here should be made much smarter, we currently have
9462306a36Sopenharmony_ci * a bit of a stupid situation because drivers might want to register
9562306a36Sopenharmony_ci * the rfkill struct under their own lock, and take this lock during
9662306a36Sopenharmony_ci * rfkill method calls -- which will cause an AB-BA deadlock situation.
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci * To fix that, we need to rework this code here to be mostly lock-free
9962306a36Sopenharmony_ci * and only use the mutex for list manipulations, not to protect the
10062306a36Sopenharmony_ci * various other global variables. Then we can avoid holding the mutex
10162306a36Sopenharmony_ci * around driver operations, and all is happy.
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_cistatic LIST_HEAD(rfkill_list);	/* list of registered rf switches */
10462306a36Sopenharmony_cistatic DEFINE_MUTEX(rfkill_global_mutex);
10562306a36Sopenharmony_cistatic LIST_HEAD(rfkill_fds);	/* list of open fds of /dev/rfkill */
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic unsigned int rfkill_default_state = 1;
10862306a36Sopenharmony_cimodule_param_named(default_state, rfkill_default_state, uint, 0444);
10962306a36Sopenharmony_ciMODULE_PARM_DESC(default_state,
11062306a36Sopenharmony_ci		 "Default initial state for all radio types, 0 = radio off");
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_cistatic struct {
11362306a36Sopenharmony_ci	bool cur, sav;
11462306a36Sopenharmony_ci} rfkill_global_states[NUM_RFKILL_TYPES];
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic bool rfkill_epo_lock_active;
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_LEDS
12062306a36Sopenharmony_cistatic void rfkill_led_trigger_event(struct rfkill *rfkill)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	struct led_trigger *trigger;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	if (!rfkill->registered)
12562306a36Sopenharmony_ci		return;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	trigger = &rfkill->led_trigger;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	if (rfkill->state & RFKILL_BLOCK_ANY)
13062306a36Sopenharmony_ci		led_trigger_event(trigger, LED_OFF);
13162306a36Sopenharmony_ci	else
13262306a36Sopenharmony_ci		led_trigger_event(trigger, LED_FULL);
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic int rfkill_led_trigger_activate(struct led_classdev *led)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	struct rfkill *rfkill;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	rfkill = container_of(led->trigger, struct rfkill, led_trigger);
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	rfkill_led_trigger_event(rfkill);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	return 0;
14462306a36Sopenharmony_ci}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ciconst char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	return rfkill->led_trigger.name;
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_get_led_trigger_name);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_civoid rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
15362306a36Sopenharmony_ci{
15462306a36Sopenharmony_ci	BUG_ON(!rfkill);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	rfkill->ledtrigname = name;
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_set_led_trigger_name);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic int rfkill_led_trigger_register(struct rfkill *rfkill)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	rfkill->led_trigger.name = rfkill->ledtrigname
16362306a36Sopenharmony_ci					? : dev_name(&rfkill->dev);
16462306a36Sopenharmony_ci	rfkill->led_trigger.activate = rfkill_led_trigger_activate;
16562306a36Sopenharmony_ci	return led_trigger_register(&rfkill->led_trigger);
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic void rfkill_led_trigger_unregister(struct rfkill *rfkill)
16962306a36Sopenharmony_ci{
17062306a36Sopenharmony_ci	led_trigger_unregister(&rfkill->led_trigger);
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic struct led_trigger rfkill_any_led_trigger;
17462306a36Sopenharmony_cistatic struct led_trigger rfkill_none_led_trigger;
17562306a36Sopenharmony_cistatic struct work_struct rfkill_global_led_trigger_work;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cistatic void rfkill_global_led_trigger_worker(struct work_struct *work)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci	enum led_brightness brightness = LED_OFF;
18062306a36Sopenharmony_ci	struct rfkill *rfkill;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
18362306a36Sopenharmony_ci	list_for_each_entry(rfkill, &rfkill_list, node) {
18462306a36Sopenharmony_ci		if (!(rfkill->state & RFKILL_BLOCK_ANY)) {
18562306a36Sopenharmony_ci			brightness = LED_FULL;
18662306a36Sopenharmony_ci			break;
18762306a36Sopenharmony_ci		}
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	led_trigger_event(&rfkill_any_led_trigger, brightness);
19262306a36Sopenharmony_ci	led_trigger_event(&rfkill_none_led_trigger,
19362306a36Sopenharmony_ci			  brightness == LED_OFF ? LED_FULL : LED_OFF);
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistatic void rfkill_global_led_trigger_event(void)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	schedule_work(&rfkill_global_led_trigger_work);
19962306a36Sopenharmony_ci}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistatic int rfkill_global_led_trigger_register(void)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	int ret;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	INIT_WORK(&rfkill_global_led_trigger_work,
20662306a36Sopenharmony_ci			rfkill_global_led_trigger_worker);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	rfkill_any_led_trigger.name = "rfkill-any";
20962306a36Sopenharmony_ci	ret = led_trigger_register(&rfkill_any_led_trigger);
21062306a36Sopenharmony_ci	if (ret)
21162306a36Sopenharmony_ci		return ret;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	rfkill_none_led_trigger.name = "rfkill-none";
21462306a36Sopenharmony_ci	ret = led_trigger_register(&rfkill_none_led_trigger);
21562306a36Sopenharmony_ci	if (ret)
21662306a36Sopenharmony_ci		led_trigger_unregister(&rfkill_any_led_trigger);
21762306a36Sopenharmony_ci	else
21862306a36Sopenharmony_ci		/* Delay activation until all global triggers are registered */
21962306a36Sopenharmony_ci		rfkill_global_led_trigger_event();
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	return ret;
22262306a36Sopenharmony_ci}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_cistatic void rfkill_global_led_trigger_unregister(void)
22562306a36Sopenharmony_ci{
22662306a36Sopenharmony_ci	led_trigger_unregister(&rfkill_none_led_trigger);
22762306a36Sopenharmony_ci	led_trigger_unregister(&rfkill_any_led_trigger);
22862306a36Sopenharmony_ci	cancel_work_sync(&rfkill_global_led_trigger_work);
22962306a36Sopenharmony_ci}
23062306a36Sopenharmony_ci#else
23162306a36Sopenharmony_cistatic void rfkill_led_trigger_event(struct rfkill *rfkill)
23262306a36Sopenharmony_ci{
23362306a36Sopenharmony_ci}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cistatic inline int rfkill_led_trigger_register(struct rfkill *rfkill)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	return 0;
23862306a36Sopenharmony_ci}
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cistatic inline void rfkill_led_trigger_unregister(struct rfkill *rfkill)
24162306a36Sopenharmony_ci{
24262306a36Sopenharmony_ci}
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_cistatic void rfkill_global_led_trigger_event(void)
24562306a36Sopenharmony_ci{
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_cistatic int rfkill_global_led_trigger_register(void)
24962306a36Sopenharmony_ci{
25062306a36Sopenharmony_ci	return 0;
25162306a36Sopenharmony_ci}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_cistatic void rfkill_global_led_trigger_unregister(void)
25462306a36Sopenharmony_ci{
25562306a36Sopenharmony_ci}
25662306a36Sopenharmony_ci#endif /* CONFIG_RFKILL_LEDS */
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_cistatic void rfkill_fill_event(struct rfkill_event_ext *ev,
25962306a36Sopenharmony_ci			      struct rfkill *rfkill,
26062306a36Sopenharmony_ci			      enum rfkill_operation op)
26162306a36Sopenharmony_ci{
26262306a36Sopenharmony_ci	unsigned long flags;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	ev->idx = rfkill->idx;
26562306a36Sopenharmony_ci	ev->type = rfkill->type;
26662306a36Sopenharmony_ci	ev->op = op;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
26962306a36Sopenharmony_ci	ev->hard = !!(rfkill->state & RFKILL_BLOCK_HW);
27062306a36Sopenharmony_ci	ev->soft = !!(rfkill->state & (RFKILL_BLOCK_SW |
27162306a36Sopenharmony_ci					RFKILL_BLOCK_SW_PREV));
27262306a36Sopenharmony_ci	ev->hard_block_reasons = rfkill->hard_block_reasons;
27362306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_cistatic void rfkill_send_events(struct rfkill *rfkill, enum rfkill_operation op)
27762306a36Sopenharmony_ci{
27862306a36Sopenharmony_ci	struct rfkill_data *data;
27962306a36Sopenharmony_ci	struct rfkill_int_event *ev;
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci	list_for_each_entry(data, &rfkill_fds, list) {
28262306a36Sopenharmony_ci		ev = kzalloc(sizeof(*ev), GFP_KERNEL);
28362306a36Sopenharmony_ci		if (!ev)
28462306a36Sopenharmony_ci			continue;
28562306a36Sopenharmony_ci		rfkill_fill_event(&ev->ev, rfkill, op);
28662306a36Sopenharmony_ci		mutex_lock(&data->mtx);
28762306a36Sopenharmony_ci		list_add_tail(&ev->list, &data->events);
28862306a36Sopenharmony_ci		mutex_unlock(&data->mtx);
28962306a36Sopenharmony_ci		wake_up_interruptible(&data->read_wait);
29062306a36Sopenharmony_ci	}
29162306a36Sopenharmony_ci}
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_cistatic void rfkill_event(struct rfkill *rfkill)
29462306a36Sopenharmony_ci{
29562306a36Sopenharmony_ci	if (!rfkill->registered)
29662306a36Sopenharmony_ci		return;
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci	/* also send event to /dev/rfkill */
30162306a36Sopenharmony_ci	rfkill_send_events(rfkill, RFKILL_OP_CHANGE);
30262306a36Sopenharmony_ci}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/**
30562306a36Sopenharmony_ci * rfkill_set_block - wrapper for set_block method
30662306a36Sopenharmony_ci *
30762306a36Sopenharmony_ci * @rfkill: the rfkill struct to use
30862306a36Sopenharmony_ci * @blocked: the new software state
30962306a36Sopenharmony_ci *
31062306a36Sopenharmony_ci * Calls the set_block method (when applicable) and handles notifications
31162306a36Sopenharmony_ci * etc. as well.
31262306a36Sopenharmony_ci */
31362306a36Sopenharmony_cistatic void rfkill_set_block(struct rfkill *rfkill, bool blocked)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	unsigned long flags;
31662306a36Sopenharmony_ci	bool prev, curr;
31762306a36Sopenharmony_ci	int err;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
32062306a36Sopenharmony_ci		return;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	/*
32362306a36Sopenharmony_ci	 * Some platforms (...!) generate input events which affect the
32462306a36Sopenharmony_ci	 * _hard_ kill state -- whenever something tries to change the
32562306a36Sopenharmony_ci	 * current software state query the hardware state too.
32662306a36Sopenharmony_ci	 */
32762306a36Sopenharmony_ci	if (rfkill->ops->query)
32862306a36Sopenharmony_ci		rfkill->ops->query(rfkill, rfkill->data);
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
33162306a36Sopenharmony_ci	prev = rfkill->state & RFKILL_BLOCK_SW;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	if (prev)
33462306a36Sopenharmony_ci		rfkill->state |= RFKILL_BLOCK_SW_PREV;
33562306a36Sopenharmony_ci	else
33662306a36Sopenharmony_ci		rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci	if (blocked)
33962306a36Sopenharmony_ci		rfkill->state |= RFKILL_BLOCK_SW;
34062306a36Sopenharmony_ci	else
34162306a36Sopenharmony_ci		rfkill->state &= ~RFKILL_BLOCK_SW;
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
34462306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	err = rfkill->ops->set_block(rfkill->data, blocked);
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
34962306a36Sopenharmony_ci	if (err) {
35062306a36Sopenharmony_ci		/*
35162306a36Sopenharmony_ci		 * Failed -- reset status to _PREV, which may be different
35262306a36Sopenharmony_ci		 * from what we have set _PREV to earlier in this function
35362306a36Sopenharmony_ci		 * if rfkill_set_sw_state was invoked.
35462306a36Sopenharmony_ci		 */
35562306a36Sopenharmony_ci		if (rfkill->state & RFKILL_BLOCK_SW_PREV)
35662306a36Sopenharmony_ci			rfkill->state |= RFKILL_BLOCK_SW;
35762306a36Sopenharmony_ci		else
35862306a36Sopenharmony_ci			rfkill->state &= ~RFKILL_BLOCK_SW;
35962306a36Sopenharmony_ci	}
36062306a36Sopenharmony_ci	rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
36162306a36Sopenharmony_ci	rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
36262306a36Sopenharmony_ci	curr = rfkill->state & RFKILL_BLOCK_SW;
36362306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	rfkill_led_trigger_event(rfkill);
36662306a36Sopenharmony_ci	rfkill_global_led_trigger_event();
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	if (prev != curr)
36962306a36Sopenharmony_ci		rfkill_event(rfkill);
37062306a36Sopenharmony_ci}
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_cistatic void rfkill_sync(struct rfkill *rfkill)
37362306a36Sopenharmony_ci{
37462306a36Sopenharmony_ci	lockdep_assert_held(&rfkill_global_mutex);
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	if (!rfkill->need_sync)
37762306a36Sopenharmony_ci		return;
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	rfkill_set_block(rfkill, rfkill_global_states[rfkill->type].cur);
38062306a36Sopenharmony_ci	rfkill->need_sync = false;
38162306a36Sopenharmony_ci}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_cistatic void rfkill_update_global_state(enum rfkill_type type, bool blocked)
38462306a36Sopenharmony_ci{
38562306a36Sopenharmony_ci	int i;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	if (type != RFKILL_TYPE_ALL) {
38862306a36Sopenharmony_ci		rfkill_global_states[type].cur = blocked;
38962306a36Sopenharmony_ci		return;
39062306a36Sopenharmony_ci	}
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	for (i = 0; i < NUM_RFKILL_TYPES; i++)
39362306a36Sopenharmony_ci		rfkill_global_states[i].cur = blocked;
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_INPUT
39762306a36Sopenharmony_cistatic atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci/**
40062306a36Sopenharmony_ci * __rfkill_switch_all - Toggle state of all switches of given type
40162306a36Sopenharmony_ci * @type: type of interfaces to be affected
40262306a36Sopenharmony_ci * @blocked: the new state
40362306a36Sopenharmony_ci *
40462306a36Sopenharmony_ci * This function sets the state of all switches of given type,
40562306a36Sopenharmony_ci * unless a specific switch is suspended.
40662306a36Sopenharmony_ci *
40762306a36Sopenharmony_ci * Caller must have acquired rfkill_global_mutex.
40862306a36Sopenharmony_ci */
40962306a36Sopenharmony_cistatic void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
41062306a36Sopenharmony_ci{
41162306a36Sopenharmony_ci	struct rfkill *rfkill;
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci	rfkill_update_global_state(type, blocked);
41462306a36Sopenharmony_ci	list_for_each_entry(rfkill, &rfkill_list, node) {
41562306a36Sopenharmony_ci		if (rfkill->type != type && type != RFKILL_TYPE_ALL)
41662306a36Sopenharmony_ci			continue;
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci		rfkill_set_block(rfkill, blocked);
41962306a36Sopenharmony_ci	}
42062306a36Sopenharmony_ci}
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci/**
42362306a36Sopenharmony_ci * rfkill_switch_all - Toggle state of all switches of given type
42462306a36Sopenharmony_ci * @type: type of interfaces to be affected
42562306a36Sopenharmony_ci * @blocked: the new state
42662306a36Sopenharmony_ci *
42762306a36Sopenharmony_ci * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
42862306a36Sopenharmony_ci * Please refer to __rfkill_switch_all() for details.
42962306a36Sopenharmony_ci *
43062306a36Sopenharmony_ci * Does nothing if the EPO lock is active.
43162306a36Sopenharmony_ci */
43262306a36Sopenharmony_civoid rfkill_switch_all(enum rfkill_type type, bool blocked)
43362306a36Sopenharmony_ci{
43462306a36Sopenharmony_ci	if (atomic_read(&rfkill_input_disabled))
43562306a36Sopenharmony_ci		return;
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	if (!rfkill_epo_lock_active)
44062306a36Sopenharmony_ci		__rfkill_switch_all(type, blocked);
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
44362306a36Sopenharmony_ci}
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci/**
44662306a36Sopenharmony_ci * rfkill_epo - emergency power off all transmitters
44762306a36Sopenharmony_ci *
44862306a36Sopenharmony_ci * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
44962306a36Sopenharmony_ci * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
45062306a36Sopenharmony_ci *
45162306a36Sopenharmony_ci * The global state before the EPO is saved and can be restored later
45262306a36Sopenharmony_ci * using rfkill_restore_states().
45362306a36Sopenharmony_ci */
45462306a36Sopenharmony_civoid rfkill_epo(void)
45562306a36Sopenharmony_ci{
45662306a36Sopenharmony_ci	struct rfkill *rfkill;
45762306a36Sopenharmony_ci	int i;
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	if (atomic_read(&rfkill_input_disabled))
46062306a36Sopenharmony_ci		return;
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	rfkill_epo_lock_active = true;
46562306a36Sopenharmony_ci	list_for_each_entry(rfkill, &rfkill_list, node)
46662306a36Sopenharmony_ci		rfkill_set_block(rfkill, true);
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	for (i = 0; i < NUM_RFKILL_TYPES; i++) {
46962306a36Sopenharmony_ci		rfkill_global_states[i].sav = rfkill_global_states[i].cur;
47062306a36Sopenharmony_ci		rfkill_global_states[i].cur = true;
47162306a36Sopenharmony_ci	}
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
47462306a36Sopenharmony_ci}
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci/**
47762306a36Sopenharmony_ci * rfkill_restore_states - restore global states
47862306a36Sopenharmony_ci *
47962306a36Sopenharmony_ci * Restore (and sync switches to) the global state from the
48062306a36Sopenharmony_ci * states in rfkill_default_states.  This can undo the effects of
48162306a36Sopenharmony_ci * a call to rfkill_epo().
48262306a36Sopenharmony_ci */
48362306a36Sopenharmony_civoid rfkill_restore_states(void)
48462306a36Sopenharmony_ci{
48562306a36Sopenharmony_ci	int i;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	if (atomic_read(&rfkill_input_disabled))
48862306a36Sopenharmony_ci		return;
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	rfkill_epo_lock_active = false;
49362306a36Sopenharmony_ci	for (i = 0; i < NUM_RFKILL_TYPES; i++)
49462306a36Sopenharmony_ci		__rfkill_switch_all(i, rfkill_global_states[i].sav);
49562306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
49662306a36Sopenharmony_ci}
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci/**
49962306a36Sopenharmony_ci * rfkill_remove_epo_lock - unlock state changes
50062306a36Sopenharmony_ci *
50162306a36Sopenharmony_ci * Used by rfkill-input manually unlock state changes, when
50262306a36Sopenharmony_ci * the EPO switch is deactivated.
50362306a36Sopenharmony_ci */
50462306a36Sopenharmony_civoid rfkill_remove_epo_lock(void)
50562306a36Sopenharmony_ci{
50662306a36Sopenharmony_ci	if (atomic_read(&rfkill_input_disabled))
50762306a36Sopenharmony_ci		return;
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
51062306a36Sopenharmony_ci	rfkill_epo_lock_active = false;
51162306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
51262306a36Sopenharmony_ci}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci/**
51562306a36Sopenharmony_ci * rfkill_is_epo_lock_active - returns true EPO is active
51662306a36Sopenharmony_ci *
51762306a36Sopenharmony_ci * Returns 0 (false) if there is NOT an active EPO condition,
51862306a36Sopenharmony_ci * and 1 (true) if there is an active EPO condition, which
51962306a36Sopenharmony_ci * locks all radios in one of the BLOCKED states.
52062306a36Sopenharmony_ci *
52162306a36Sopenharmony_ci * Can be called in atomic context.
52262306a36Sopenharmony_ci */
52362306a36Sopenharmony_cibool rfkill_is_epo_lock_active(void)
52462306a36Sopenharmony_ci{
52562306a36Sopenharmony_ci	return rfkill_epo_lock_active;
52662306a36Sopenharmony_ci}
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci/**
52962306a36Sopenharmony_ci * rfkill_get_global_sw_state - returns global state for a type
53062306a36Sopenharmony_ci * @type: the type to get the global state of
53162306a36Sopenharmony_ci *
53262306a36Sopenharmony_ci * Returns the current global state for a given wireless
53362306a36Sopenharmony_ci * device type.
53462306a36Sopenharmony_ci */
53562306a36Sopenharmony_cibool rfkill_get_global_sw_state(const enum rfkill_type type)
53662306a36Sopenharmony_ci{
53762306a36Sopenharmony_ci	return rfkill_global_states[type].cur;
53862306a36Sopenharmony_ci}
53962306a36Sopenharmony_ci#endif
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_cibool rfkill_set_hw_state_reason(struct rfkill *rfkill,
54262306a36Sopenharmony_ci				bool blocked, unsigned long reason)
54362306a36Sopenharmony_ci{
54462306a36Sopenharmony_ci	unsigned long flags;
54562306a36Sopenharmony_ci	bool ret, prev;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	BUG_ON(!rfkill);
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci	if (WARN(reason &
55062306a36Sopenharmony_ci	    ~(RFKILL_HARD_BLOCK_SIGNAL | RFKILL_HARD_BLOCK_NOT_OWNER),
55162306a36Sopenharmony_ci	    "hw_state reason not supported: 0x%lx", reason))
55262306a36Sopenharmony_ci		return blocked;
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
55562306a36Sopenharmony_ci	prev = !!(rfkill->hard_block_reasons & reason);
55662306a36Sopenharmony_ci	if (blocked) {
55762306a36Sopenharmony_ci		rfkill->state |= RFKILL_BLOCK_HW;
55862306a36Sopenharmony_ci		rfkill->hard_block_reasons |= reason;
55962306a36Sopenharmony_ci	} else {
56062306a36Sopenharmony_ci		rfkill->hard_block_reasons &= ~reason;
56162306a36Sopenharmony_ci		if (!rfkill->hard_block_reasons)
56262306a36Sopenharmony_ci			rfkill->state &= ~RFKILL_BLOCK_HW;
56362306a36Sopenharmony_ci	}
56462306a36Sopenharmony_ci	ret = !!(rfkill->state & RFKILL_BLOCK_ANY);
56562306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	rfkill_led_trigger_event(rfkill);
56862306a36Sopenharmony_ci	rfkill_global_led_trigger_event();
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci	if (rfkill->registered && prev != blocked)
57162306a36Sopenharmony_ci		schedule_work(&rfkill->uevent_work);
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	return ret;
57462306a36Sopenharmony_ci}
57562306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_set_hw_state_reason);
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_cistatic void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
57862306a36Sopenharmony_ci{
57962306a36Sopenharmony_ci	u32 bit = RFKILL_BLOCK_SW;
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci	/* if in a ops->set_block right now, use other bit */
58262306a36Sopenharmony_ci	if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
58362306a36Sopenharmony_ci		bit = RFKILL_BLOCK_SW_PREV;
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci	if (blocked)
58662306a36Sopenharmony_ci		rfkill->state |= bit;
58762306a36Sopenharmony_ci	else
58862306a36Sopenharmony_ci		rfkill->state &= ~bit;
58962306a36Sopenharmony_ci}
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_cibool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
59262306a36Sopenharmony_ci{
59362306a36Sopenharmony_ci	unsigned long flags;
59462306a36Sopenharmony_ci	bool prev, hwblock;
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	BUG_ON(!rfkill);
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
59962306a36Sopenharmony_ci	prev = !!(rfkill->state & RFKILL_BLOCK_SW);
60062306a36Sopenharmony_ci	__rfkill_set_sw_state(rfkill, blocked);
60162306a36Sopenharmony_ci	hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
60262306a36Sopenharmony_ci	blocked = blocked || hwblock;
60362306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	if (!rfkill->registered)
60662306a36Sopenharmony_ci		return blocked;
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_ci	if (prev != blocked && !hwblock)
60962306a36Sopenharmony_ci		schedule_work(&rfkill->uevent_work);
61062306a36Sopenharmony_ci
61162306a36Sopenharmony_ci	rfkill_led_trigger_event(rfkill);
61262306a36Sopenharmony_ci	rfkill_global_led_trigger_event();
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	return blocked;
61562306a36Sopenharmony_ci}
61662306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_set_sw_state);
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_civoid rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
61962306a36Sopenharmony_ci{
62062306a36Sopenharmony_ci	unsigned long flags;
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci	BUG_ON(!rfkill);
62362306a36Sopenharmony_ci	BUG_ON(rfkill->registered);
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
62662306a36Sopenharmony_ci	__rfkill_set_sw_state(rfkill, blocked);
62762306a36Sopenharmony_ci	rfkill->persistent = true;
62862306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
62962306a36Sopenharmony_ci}
63062306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_init_sw_state);
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_civoid rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
63362306a36Sopenharmony_ci{
63462306a36Sopenharmony_ci	unsigned long flags;
63562306a36Sopenharmony_ci	bool swprev, hwprev;
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci	BUG_ON(!rfkill);
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci	/*
64262306a36Sopenharmony_ci	 * No need to care about prev/setblock ... this is for uevent only
64362306a36Sopenharmony_ci	 * and that will get triggered by rfkill_set_block anyway.
64462306a36Sopenharmony_ci	 */
64562306a36Sopenharmony_ci	swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
64662306a36Sopenharmony_ci	hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
64762306a36Sopenharmony_ci	__rfkill_set_sw_state(rfkill, sw);
64862306a36Sopenharmony_ci	if (hw)
64962306a36Sopenharmony_ci		rfkill->state |= RFKILL_BLOCK_HW;
65062306a36Sopenharmony_ci	else
65162306a36Sopenharmony_ci		rfkill->state &= ~RFKILL_BLOCK_HW;
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
65462306a36Sopenharmony_ci
65562306a36Sopenharmony_ci	if (!rfkill->registered) {
65662306a36Sopenharmony_ci		rfkill->persistent = true;
65762306a36Sopenharmony_ci	} else {
65862306a36Sopenharmony_ci		if (swprev != sw || hwprev != hw)
65962306a36Sopenharmony_ci			schedule_work(&rfkill->uevent_work);
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci		rfkill_led_trigger_event(rfkill);
66262306a36Sopenharmony_ci		rfkill_global_led_trigger_event();
66362306a36Sopenharmony_ci	}
66462306a36Sopenharmony_ci}
66562306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_set_states);
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_cistatic const char * const rfkill_types[] = {
66862306a36Sopenharmony_ci	NULL, /* RFKILL_TYPE_ALL */
66962306a36Sopenharmony_ci	"wlan",
67062306a36Sopenharmony_ci	"bluetooth",
67162306a36Sopenharmony_ci	"ultrawideband",
67262306a36Sopenharmony_ci	"wimax",
67362306a36Sopenharmony_ci	"wwan",
67462306a36Sopenharmony_ci	"gps",
67562306a36Sopenharmony_ci	"fm",
67662306a36Sopenharmony_ci	"nfc",
67762306a36Sopenharmony_ci};
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_cienum rfkill_type rfkill_find_type(const char *name)
68062306a36Sopenharmony_ci{
68162306a36Sopenharmony_ci	int i;
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(rfkill_types) != NUM_RFKILL_TYPES);
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci	if (!name)
68662306a36Sopenharmony_ci		return RFKILL_TYPE_ALL;
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_ci	for (i = 1; i < NUM_RFKILL_TYPES; i++)
68962306a36Sopenharmony_ci		if (!strcmp(name, rfkill_types[i]))
69062306a36Sopenharmony_ci			return i;
69162306a36Sopenharmony_ci	return RFKILL_TYPE_ALL;
69262306a36Sopenharmony_ci}
69362306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_find_type);
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_cistatic ssize_t name_show(struct device *dev, struct device_attribute *attr,
69662306a36Sopenharmony_ci			 char *buf)
69762306a36Sopenharmony_ci{
69862306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci	return sysfs_emit(buf, "%s\n", rfkill->name);
70162306a36Sopenharmony_ci}
70262306a36Sopenharmony_cistatic DEVICE_ATTR_RO(name);
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_cistatic ssize_t type_show(struct device *dev, struct device_attribute *attr,
70562306a36Sopenharmony_ci			 char *buf)
70662306a36Sopenharmony_ci{
70762306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	return sysfs_emit(buf, "%s\n", rfkill_types[rfkill->type]);
71062306a36Sopenharmony_ci}
71162306a36Sopenharmony_cistatic DEVICE_ATTR_RO(type);
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_cistatic ssize_t index_show(struct device *dev, struct device_attribute *attr,
71462306a36Sopenharmony_ci			  char *buf)
71562306a36Sopenharmony_ci{
71662306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", rfkill->idx);
71962306a36Sopenharmony_ci}
72062306a36Sopenharmony_cistatic DEVICE_ATTR_RO(index);
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_cistatic ssize_t persistent_show(struct device *dev,
72362306a36Sopenharmony_ci			       struct device_attribute *attr, char *buf)
72462306a36Sopenharmony_ci{
72562306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", rfkill->persistent);
72862306a36Sopenharmony_ci}
72962306a36Sopenharmony_cistatic DEVICE_ATTR_RO(persistent);
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_cistatic ssize_t hard_show(struct device *dev, struct device_attribute *attr,
73262306a36Sopenharmony_ci			 char *buf)
73362306a36Sopenharmony_ci{
73462306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
73562306a36Sopenharmony_ci
73662306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0);
73762306a36Sopenharmony_ci}
73862306a36Sopenharmony_cistatic DEVICE_ATTR_RO(hard);
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_cistatic ssize_t soft_show(struct device *dev, struct device_attribute *attr,
74162306a36Sopenharmony_ci			 char *buf)
74262306a36Sopenharmony_ci{
74362306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
74662306a36Sopenharmony_ci	rfkill_sync(rfkill);
74762306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
74862306a36Sopenharmony_ci
74962306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0);
75062306a36Sopenharmony_ci}
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_cistatic ssize_t soft_store(struct device *dev, struct device_attribute *attr,
75362306a36Sopenharmony_ci			  const char *buf, size_t count)
75462306a36Sopenharmony_ci{
75562306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
75662306a36Sopenharmony_ci	unsigned long state;
75762306a36Sopenharmony_ci	int err;
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci	if (!capable(CAP_NET_ADMIN))
76062306a36Sopenharmony_ci		return -EPERM;
76162306a36Sopenharmony_ci
76262306a36Sopenharmony_ci	err = kstrtoul(buf, 0, &state);
76362306a36Sopenharmony_ci	if (err)
76462306a36Sopenharmony_ci		return err;
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ci	if (state > 1 )
76762306a36Sopenharmony_ci		return -EINVAL;
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
77062306a36Sopenharmony_ci	rfkill_sync(rfkill);
77162306a36Sopenharmony_ci	rfkill_set_block(rfkill, state);
77262306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci	return count;
77562306a36Sopenharmony_ci}
77662306a36Sopenharmony_cistatic DEVICE_ATTR_RW(soft);
77762306a36Sopenharmony_ci
77862306a36Sopenharmony_cistatic ssize_t hard_block_reasons_show(struct device *dev,
77962306a36Sopenharmony_ci				       struct device_attribute *attr,
78062306a36Sopenharmony_ci				       char *buf)
78162306a36Sopenharmony_ci{
78262306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ci	return sysfs_emit(buf, "0x%lx\n", rfkill->hard_block_reasons);
78562306a36Sopenharmony_ci}
78662306a36Sopenharmony_cistatic DEVICE_ATTR_RO(hard_block_reasons);
78762306a36Sopenharmony_ci
78862306a36Sopenharmony_cistatic u8 user_state_from_blocked(unsigned long state)
78962306a36Sopenharmony_ci{
79062306a36Sopenharmony_ci	if (state & RFKILL_BLOCK_HW)
79162306a36Sopenharmony_ci		return RFKILL_USER_STATE_HARD_BLOCKED;
79262306a36Sopenharmony_ci	if (state & RFKILL_BLOCK_SW)
79362306a36Sopenharmony_ci		return RFKILL_USER_STATE_SOFT_BLOCKED;
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci	return RFKILL_USER_STATE_UNBLOCKED;
79662306a36Sopenharmony_ci}
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_cistatic ssize_t state_show(struct device *dev, struct device_attribute *attr,
79962306a36Sopenharmony_ci			  char *buf)
80062306a36Sopenharmony_ci{
80162306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
80462306a36Sopenharmony_ci	rfkill_sync(rfkill);
80562306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", user_state_from_blocked(rfkill->state));
80862306a36Sopenharmony_ci}
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_cistatic ssize_t state_store(struct device *dev, struct device_attribute *attr,
81162306a36Sopenharmony_ci			   const char *buf, size_t count)
81262306a36Sopenharmony_ci{
81362306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
81462306a36Sopenharmony_ci	unsigned long state;
81562306a36Sopenharmony_ci	int err;
81662306a36Sopenharmony_ci
81762306a36Sopenharmony_ci	if (!capable(CAP_NET_ADMIN))
81862306a36Sopenharmony_ci		return -EPERM;
81962306a36Sopenharmony_ci
82062306a36Sopenharmony_ci	err = kstrtoul(buf, 0, &state);
82162306a36Sopenharmony_ci	if (err)
82262306a36Sopenharmony_ci		return err;
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ci	if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
82562306a36Sopenharmony_ci	    state != RFKILL_USER_STATE_UNBLOCKED)
82662306a36Sopenharmony_ci		return -EINVAL;
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
82962306a36Sopenharmony_ci	rfkill_sync(rfkill);
83062306a36Sopenharmony_ci	rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
83162306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci	return count;
83462306a36Sopenharmony_ci}
83562306a36Sopenharmony_cistatic DEVICE_ATTR_RW(state);
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_cistatic struct attribute *rfkill_dev_attrs[] = {
83862306a36Sopenharmony_ci	&dev_attr_name.attr,
83962306a36Sopenharmony_ci	&dev_attr_type.attr,
84062306a36Sopenharmony_ci	&dev_attr_index.attr,
84162306a36Sopenharmony_ci	&dev_attr_persistent.attr,
84262306a36Sopenharmony_ci	&dev_attr_state.attr,
84362306a36Sopenharmony_ci	&dev_attr_soft.attr,
84462306a36Sopenharmony_ci	&dev_attr_hard.attr,
84562306a36Sopenharmony_ci	&dev_attr_hard_block_reasons.attr,
84662306a36Sopenharmony_ci	NULL,
84762306a36Sopenharmony_ci};
84862306a36Sopenharmony_ciATTRIBUTE_GROUPS(rfkill_dev);
84962306a36Sopenharmony_ci
85062306a36Sopenharmony_cistatic void rfkill_release(struct device *dev)
85162306a36Sopenharmony_ci{
85262306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
85362306a36Sopenharmony_ci
85462306a36Sopenharmony_ci	kfree(rfkill);
85562306a36Sopenharmony_ci}
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_cistatic int rfkill_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
85862306a36Sopenharmony_ci{
85962306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
86062306a36Sopenharmony_ci	unsigned long flags;
86162306a36Sopenharmony_ci	unsigned long reasons;
86262306a36Sopenharmony_ci	u32 state;
86362306a36Sopenharmony_ci	int error;
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci	error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
86662306a36Sopenharmony_ci	if (error)
86762306a36Sopenharmony_ci		return error;
86862306a36Sopenharmony_ci	error = add_uevent_var(env, "RFKILL_TYPE=%s",
86962306a36Sopenharmony_ci			       rfkill_types[rfkill->type]);
87062306a36Sopenharmony_ci	if (error)
87162306a36Sopenharmony_ci		return error;
87262306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
87362306a36Sopenharmony_ci	state = rfkill->state;
87462306a36Sopenharmony_ci	reasons = rfkill->hard_block_reasons;
87562306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
87662306a36Sopenharmony_ci	error = add_uevent_var(env, "RFKILL_STATE=%d",
87762306a36Sopenharmony_ci			       user_state_from_blocked(state));
87862306a36Sopenharmony_ci	if (error)
87962306a36Sopenharmony_ci		return error;
88062306a36Sopenharmony_ci	return add_uevent_var(env, "RFKILL_HW_BLOCK_REASON=0x%lx", reasons);
88162306a36Sopenharmony_ci}
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_civoid rfkill_pause_polling(struct rfkill *rfkill)
88462306a36Sopenharmony_ci{
88562306a36Sopenharmony_ci	BUG_ON(!rfkill);
88662306a36Sopenharmony_ci
88762306a36Sopenharmony_ci	if (!rfkill->ops->poll)
88862306a36Sopenharmony_ci		return;
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci	rfkill->polling_paused = true;
89162306a36Sopenharmony_ci	cancel_delayed_work_sync(&rfkill->poll_work);
89262306a36Sopenharmony_ci}
89362306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_pause_polling);
89462306a36Sopenharmony_ci
89562306a36Sopenharmony_civoid rfkill_resume_polling(struct rfkill *rfkill)
89662306a36Sopenharmony_ci{
89762306a36Sopenharmony_ci	BUG_ON(!rfkill);
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci	if (!rfkill->ops->poll)
90062306a36Sopenharmony_ci		return;
90162306a36Sopenharmony_ci
90262306a36Sopenharmony_ci	rfkill->polling_paused = false;
90362306a36Sopenharmony_ci
90462306a36Sopenharmony_ci	if (rfkill->suspended)
90562306a36Sopenharmony_ci		return;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	queue_delayed_work(system_power_efficient_wq,
90862306a36Sopenharmony_ci			   &rfkill->poll_work, 0);
90962306a36Sopenharmony_ci}
91062306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_resume_polling);
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
91362306a36Sopenharmony_cistatic int rfkill_suspend(struct device *dev)
91462306a36Sopenharmony_ci{
91562306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
91662306a36Sopenharmony_ci
91762306a36Sopenharmony_ci	rfkill->suspended = true;
91862306a36Sopenharmony_ci	cancel_delayed_work_sync(&rfkill->poll_work);
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_ci	return 0;
92162306a36Sopenharmony_ci}
92262306a36Sopenharmony_ci
92362306a36Sopenharmony_cistatic int rfkill_resume(struct device *dev)
92462306a36Sopenharmony_ci{
92562306a36Sopenharmony_ci	struct rfkill *rfkill = to_rfkill(dev);
92662306a36Sopenharmony_ci	bool cur;
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci	rfkill->suspended = false;
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci	if (!rfkill->registered)
93162306a36Sopenharmony_ci		return 0;
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci	if (!rfkill->persistent) {
93462306a36Sopenharmony_ci		cur = !!(rfkill->state & RFKILL_BLOCK_SW);
93562306a36Sopenharmony_ci		rfkill_set_block(rfkill, cur);
93662306a36Sopenharmony_ci	}
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ci	if (rfkill->ops->poll && !rfkill->polling_paused)
93962306a36Sopenharmony_ci		queue_delayed_work(system_power_efficient_wq,
94062306a36Sopenharmony_ci				   &rfkill->poll_work, 0);
94162306a36Sopenharmony_ci
94262306a36Sopenharmony_ci	return 0;
94362306a36Sopenharmony_ci}
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(rfkill_pm_ops, rfkill_suspend, rfkill_resume);
94662306a36Sopenharmony_ci#define RFKILL_PM_OPS (&rfkill_pm_ops)
94762306a36Sopenharmony_ci#else
94862306a36Sopenharmony_ci#define RFKILL_PM_OPS NULL
94962306a36Sopenharmony_ci#endif
95062306a36Sopenharmony_ci
95162306a36Sopenharmony_cistatic struct class rfkill_class = {
95262306a36Sopenharmony_ci	.name		= "rfkill",
95362306a36Sopenharmony_ci	.dev_release	= rfkill_release,
95462306a36Sopenharmony_ci	.dev_groups	= rfkill_dev_groups,
95562306a36Sopenharmony_ci	.dev_uevent	= rfkill_dev_uevent,
95662306a36Sopenharmony_ci	.pm		= RFKILL_PM_OPS,
95762306a36Sopenharmony_ci};
95862306a36Sopenharmony_ci
95962306a36Sopenharmony_cibool rfkill_blocked(struct rfkill *rfkill)
96062306a36Sopenharmony_ci{
96162306a36Sopenharmony_ci	unsigned long flags;
96262306a36Sopenharmony_ci	u32 state;
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
96562306a36Sopenharmony_ci	state = rfkill->state;
96662306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci	return !!(state & RFKILL_BLOCK_ANY);
96962306a36Sopenharmony_ci}
97062306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_blocked);
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_cibool rfkill_soft_blocked(struct rfkill *rfkill)
97362306a36Sopenharmony_ci{
97462306a36Sopenharmony_ci	unsigned long flags;
97562306a36Sopenharmony_ci	u32 state;
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci	spin_lock_irqsave(&rfkill->lock, flags);
97862306a36Sopenharmony_ci	state = rfkill->state;
97962306a36Sopenharmony_ci	spin_unlock_irqrestore(&rfkill->lock, flags);
98062306a36Sopenharmony_ci
98162306a36Sopenharmony_ci	return !!(state & RFKILL_BLOCK_SW);
98262306a36Sopenharmony_ci}
98362306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_soft_blocked);
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_cistruct rfkill * __must_check rfkill_alloc(const char *name,
98662306a36Sopenharmony_ci					  struct device *parent,
98762306a36Sopenharmony_ci					  const enum rfkill_type type,
98862306a36Sopenharmony_ci					  const struct rfkill_ops *ops,
98962306a36Sopenharmony_ci					  void *ops_data)
99062306a36Sopenharmony_ci{
99162306a36Sopenharmony_ci	struct rfkill *rfkill;
99262306a36Sopenharmony_ci	struct device *dev;
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci	if (WARN_ON(!ops))
99562306a36Sopenharmony_ci		return NULL;
99662306a36Sopenharmony_ci
99762306a36Sopenharmony_ci	if (WARN_ON(!ops->set_block))
99862306a36Sopenharmony_ci		return NULL;
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ci	if (WARN_ON(!name))
100162306a36Sopenharmony_ci		return NULL;
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci	if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
100462306a36Sopenharmony_ci		return NULL;
100562306a36Sopenharmony_ci
100662306a36Sopenharmony_ci	rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
100762306a36Sopenharmony_ci	if (!rfkill)
100862306a36Sopenharmony_ci		return NULL;
100962306a36Sopenharmony_ci
101062306a36Sopenharmony_ci	spin_lock_init(&rfkill->lock);
101162306a36Sopenharmony_ci	INIT_LIST_HEAD(&rfkill->node);
101262306a36Sopenharmony_ci	rfkill->type = type;
101362306a36Sopenharmony_ci	strcpy(rfkill->name, name);
101462306a36Sopenharmony_ci	rfkill->ops = ops;
101562306a36Sopenharmony_ci	rfkill->data = ops_data;
101662306a36Sopenharmony_ci
101762306a36Sopenharmony_ci	dev = &rfkill->dev;
101862306a36Sopenharmony_ci	dev->class = &rfkill_class;
101962306a36Sopenharmony_ci	dev->parent = parent;
102062306a36Sopenharmony_ci	device_initialize(dev);
102162306a36Sopenharmony_ci
102262306a36Sopenharmony_ci	return rfkill;
102362306a36Sopenharmony_ci}
102462306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_alloc);
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_cistatic void rfkill_poll(struct work_struct *work)
102762306a36Sopenharmony_ci{
102862306a36Sopenharmony_ci	struct rfkill *rfkill;
102962306a36Sopenharmony_ci
103062306a36Sopenharmony_ci	rfkill = container_of(work, struct rfkill, poll_work.work);
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci	/*
103362306a36Sopenharmony_ci	 * Poll hardware state -- driver will use one of the
103462306a36Sopenharmony_ci	 * rfkill_set{,_hw,_sw}_state functions and use its
103562306a36Sopenharmony_ci	 * return value to update the current status.
103662306a36Sopenharmony_ci	 */
103762306a36Sopenharmony_ci	rfkill->ops->poll(rfkill, rfkill->data);
103862306a36Sopenharmony_ci
103962306a36Sopenharmony_ci	queue_delayed_work(system_power_efficient_wq,
104062306a36Sopenharmony_ci		&rfkill->poll_work,
104162306a36Sopenharmony_ci		round_jiffies_relative(POLL_INTERVAL));
104262306a36Sopenharmony_ci}
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_cistatic void rfkill_uevent_work(struct work_struct *work)
104562306a36Sopenharmony_ci{
104662306a36Sopenharmony_ci	struct rfkill *rfkill;
104762306a36Sopenharmony_ci
104862306a36Sopenharmony_ci	rfkill = container_of(work, struct rfkill, uevent_work);
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
105162306a36Sopenharmony_ci	rfkill_event(rfkill);
105262306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
105362306a36Sopenharmony_ci}
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_cistatic void rfkill_sync_work(struct work_struct *work)
105662306a36Sopenharmony_ci{
105762306a36Sopenharmony_ci	struct rfkill *rfkill = container_of(work, struct rfkill, sync_work);
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
106062306a36Sopenharmony_ci	rfkill_sync(rfkill);
106162306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
106262306a36Sopenharmony_ci}
106362306a36Sopenharmony_ci
106462306a36Sopenharmony_ciint __must_check rfkill_register(struct rfkill *rfkill)
106562306a36Sopenharmony_ci{
106662306a36Sopenharmony_ci	static unsigned long rfkill_no;
106762306a36Sopenharmony_ci	struct device *dev;
106862306a36Sopenharmony_ci	int error;
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_ci	if (!rfkill)
107162306a36Sopenharmony_ci		return -EINVAL;
107262306a36Sopenharmony_ci
107362306a36Sopenharmony_ci	dev = &rfkill->dev;
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
107662306a36Sopenharmony_ci
107762306a36Sopenharmony_ci	if (rfkill->registered) {
107862306a36Sopenharmony_ci		error = -EALREADY;
107962306a36Sopenharmony_ci		goto unlock;
108062306a36Sopenharmony_ci	}
108162306a36Sopenharmony_ci
108262306a36Sopenharmony_ci	rfkill->idx = rfkill_no;
108362306a36Sopenharmony_ci	dev_set_name(dev, "rfkill%lu", rfkill_no);
108462306a36Sopenharmony_ci	rfkill_no++;
108562306a36Sopenharmony_ci
108662306a36Sopenharmony_ci	list_add_tail(&rfkill->node, &rfkill_list);
108762306a36Sopenharmony_ci
108862306a36Sopenharmony_ci	error = device_add(dev);
108962306a36Sopenharmony_ci	if (error)
109062306a36Sopenharmony_ci		goto remove;
109162306a36Sopenharmony_ci
109262306a36Sopenharmony_ci	error = rfkill_led_trigger_register(rfkill);
109362306a36Sopenharmony_ci	if (error)
109462306a36Sopenharmony_ci		goto devdel;
109562306a36Sopenharmony_ci
109662306a36Sopenharmony_ci	rfkill->registered = true;
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_ci	INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
109962306a36Sopenharmony_ci	INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
110062306a36Sopenharmony_ci	INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
110162306a36Sopenharmony_ci
110262306a36Sopenharmony_ci	if (rfkill->ops->poll)
110362306a36Sopenharmony_ci		queue_delayed_work(system_power_efficient_wq,
110462306a36Sopenharmony_ci			&rfkill->poll_work,
110562306a36Sopenharmony_ci			round_jiffies_relative(POLL_INTERVAL));
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_ci	if (!rfkill->persistent || rfkill_epo_lock_active) {
110862306a36Sopenharmony_ci		rfkill->need_sync = true;
110962306a36Sopenharmony_ci		schedule_work(&rfkill->sync_work);
111062306a36Sopenharmony_ci	} else {
111162306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_INPUT
111262306a36Sopenharmony_ci		bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
111362306a36Sopenharmony_ci
111462306a36Sopenharmony_ci		if (!atomic_read(&rfkill_input_disabled))
111562306a36Sopenharmony_ci			__rfkill_switch_all(rfkill->type, soft_blocked);
111662306a36Sopenharmony_ci#endif
111762306a36Sopenharmony_ci	}
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_ci	rfkill_global_led_trigger_event();
112062306a36Sopenharmony_ci	rfkill_send_events(rfkill, RFKILL_OP_ADD);
112162306a36Sopenharmony_ci
112262306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
112362306a36Sopenharmony_ci	return 0;
112462306a36Sopenharmony_ci
112562306a36Sopenharmony_ci devdel:
112662306a36Sopenharmony_ci	device_del(&rfkill->dev);
112762306a36Sopenharmony_ci remove:
112862306a36Sopenharmony_ci	list_del_init(&rfkill->node);
112962306a36Sopenharmony_ci unlock:
113062306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
113162306a36Sopenharmony_ci	return error;
113262306a36Sopenharmony_ci}
113362306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_register);
113462306a36Sopenharmony_ci
113562306a36Sopenharmony_civoid rfkill_unregister(struct rfkill *rfkill)
113662306a36Sopenharmony_ci{
113762306a36Sopenharmony_ci	BUG_ON(!rfkill);
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci	if (rfkill->ops->poll)
114062306a36Sopenharmony_ci		cancel_delayed_work_sync(&rfkill->poll_work);
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci	cancel_work_sync(&rfkill->uevent_work);
114362306a36Sopenharmony_ci	cancel_work_sync(&rfkill->sync_work);
114462306a36Sopenharmony_ci
114562306a36Sopenharmony_ci	rfkill->registered = false;
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_ci	device_del(&rfkill->dev);
114862306a36Sopenharmony_ci
114962306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
115062306a36Sopenharmony_ci	rfkill_send_events(rfkill, RFKILL_OP_DEL);
115162306a36Sopenharmony_ci	list_del_init(&rfkill->node);
115262306a36Sopenharmony_ci	rfkill_global_led_trigger_event();
115362306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
115462306a36Sopenharmony_ci
115562306a36Sopenharmony_ci	rfkill_led_trigger_unregister(rfkill);
115662306a36Sopenharmony_ci}
115762306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_unregister);
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_civoid rfkill_destroy(struct rfkill *rfkill)
116062306a36Sopenharmony_ci{
116162306a36Sopenharmony_ci	if (rfkill)
116262306a36Sopenharmony_ci		put_device(&rfkill->dev);
116362306a36Sopenharmony_ci}
116462306a36Sopenharmony_ciEXPORT_SYMBOL(rfkill_destroy);
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_cistatic int rfkill_fop_open(struct inode *inode, struct file *file)
116762306a36Sopenharmony_ci{
116862306a36Sopenharmony_ci	struct rfkill_data *data;
116962306a36Sopenharmony_ci	struct rfkill *rfkill;
117062306a36Sopenharmony_ci	struct rfkill_int_event *ev, *tmp;
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci	data = kzalloc(sizeof(*data), GFP_KERNEL);
117362306a36Sopenharmony_ci	if (!data)
117462306a36Sopenharmony_ci		return -ENOMEM;
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_ci	data->max_size = RFKILL_EVENT_SIZE_V1;
117762306a36Sopenharmony_ci
117862306a36Sopenharmony_ci	INIT_LIST_HEAD(&data->events);
117962306a36Sopenharmony_ci	mutex_init(&data->mtx);
118062306a36Sopenharmony_ci	init_waitqueue_head(&data->read_wait);
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
118362306a36Sopenharmony_ci	/*
118462306a36Sopenharmony_ci	 * start getting events from elsewhere but hold mtx to get
118562306a36Sopenharmony_ci	 * startup events added first
118662306a36Sopenharmony_ci	 */
118762306a36Sopenharmony_ci
118862306a36Sopenharmony_ci	list_for_each_entry(rfkill, &rfkill_list, node) {
118962306a36Sopenharmony_ci		ev = kzalloc(sizeof(*ev), GFP_KERNEL);
119062306a36Sopenharmony_ci		if (!ev)
119162306a36Sopenharmony_ci			goto free;
119262306a36Sopenharmony_ci		rfkill_sync(rfkill);
119362306a36Sopenharmony_ci		rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
119462306a36Sopenharmony_ci		mutex_lock(&data->mtx);
119562306a36Sopenharmony_ci		list_add_tail(&ev->list, &data->events);
119662306a36Sopenharmony_ci		mutex_unlock(&data->mtx);
119762306a36Sopenharmony_ci	}
119862306a36Sopenharmony_ci	list_add(&data->list, &rfkill_fds);
119962306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci	file->private_data = data;
120262306a36Sopenharmony_ci
120362306a36Sopenharmony_ci	return stream_open(inode, file);
120462306a36Sopenharmony_ci
120562306a36Sopenharmony_ci free:
120662306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
120762306a36Sopenharmony_ci	mutex_destroy(&data->mtx);
120862306a36Sopenharmony_ci	list_for_each_entry_safe(ev, tmp, &data->events, list)
120962306a36Sopenharmony_ci		kfree(ev);
121062306a36Sopenharmony_ci	kfree(data);
121162306a36Sopenharmony_ci	return -ENOMEM;
121262306a36Sopenharmony_ci}
121362306a36Sopenharmony_ci
121462306a36Sopenharmony_cistatic __poll_t rfkill_fop_poll(struct file *file, poll_table *wait)
121562306a36Sopenharmony_ci{
121662306a36Sopenharmony_ci	struct rfkill_data *data = file->private_data;
121762306a36Sopenharmony_ci	__poll_t res = EPOLLOUT | EPOLLWRNORM;
121862306a36Sopenharmony_ci
121962306a36Sopenharmony_ci	poll_wait(file, &data->read_wait, wait);
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci	mutex_lock(&data->mtx);
122262306a36Sopenharmony_ci	if (!list_empty(&data->events))
122362306a36Sopenharmony_ci		res = EPOLLIN | EPOLLRDNORM;
122462306a36Sopenharmony_ci	mutex_unlock(&data->mtx);
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	return res;
122762306a36Sopenharmony_ci}
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_cistatic ssize_t rfkill_fop_read(struct file *file, char __user *buf,
123062306a36Sopenharmony_ci			       size_t count, loff_t *pos)
123162306a36Sopenharmony_ci{
123262306a36Sopenharmony_ci	struct rfkill_data *data = file->private_data;
123362306a36Sopenharmony_ci	struct rfkill_int_event *ev;
123462306a36Sopenharmony_ci	unsigned long sz;
123562306a36Sopenharmony_ci	int ret;
123662306a36Sopenharmony_ci
123762306a36Sopenharmony_ci	mutex_lock(&data->mtx);
123862306a36Sopenharmony_ci
123962306a36Sopenharmony_ci	while (list_empty(&data->events)) {
124062306a36Sopenharmony_ci		if (file->f_flags & O_NONBLOCK) {
124162306a36Sopenharmony_ci			ret = -EAGAIN;
124262306a36Sopenharmony_ci			goto out;
124362306a36Sopenharmony_ci		}
124462306a36Sopenharmony_ci		mutex_unlock(&data->mtx);
124562306a36Sopenharmony_ci		/* since we re-check and it just compares pointers,
124662306a36Sopenharmony_ci		 * using !list_empty() without locking isn't a problem
124762306a36Sopenharmony_ci		 */
124862306a36Sopenharmony_ci		ret = wait_event_interruptible(data->read_wait,
124962306a36Sopenharmony_ci					       !list_empty(&data->events));
125062306a36Sopenharmony_ci		mutex_lock(&data->mtx);
125162306a36Sopenharmony_ci
125262306a36Sopenharmony_ci		if (ret)
125362306a36Sopenharmony_ci			goto out;
125462306a36Sopenharmony_ci	}
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci	ev = list_first_entry(&data->events, struct rfkill_int_event,
125762306a36Sopenharmony_ci				list);
125862306a36Sopenharmony_ci
125962306a36Sopenharmony_ci	sz = min_t(unsigned long, sizeof(ev->ev), count);
126062306a36Sopenharmony_ci	sz = min_t(unsigned long, sz, data->max_size);
126162306a36Sopenharmony_ci	ret = sz;
126262306a36Sopenharmony_ci	if (copy_to_user(buf, &ev->ev, sz))
126362306a36Sopenharmony_ci		ret = -EFAULT;
126462306a36Sopenharmony_ci
126562306a36Sopenharmony_ci	list_del(&ev->list);
126662306a36Sopenharmony_ci	kfree(ev);
126762306a36Sopenharmony_ci out:
126862306a36Sopenharmony_ci	mutex_unlock(&data->mtx);
126962306a36Sopenharmony_ci	return ret;
127062306a36Sopenharmony_ci}
127162306a36Sopenharmony_ci
127262306a36Sopenharmony_cistatic ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
127362306a36Sopenharmony_ci				size_t count, loff_t *pos)
127462306a36Sopenharmony_ci{
127562306a36Sopenharmony_ci	struct rfkill_data *data = file->private_data;
127662306a36Sopenharmony_ci	struct rfkill *rfkill;
127762306a36Sopenharmony_ci	struct rfkill_event_ext ev;
127862306a36Sopenharmony_ci	int ret;
127962306a36Sopenharmony_ci
128062306a36Sopenharmony_ci	/* we don't need the 'hard' variable but accept it */
128162306a36Sopenharmony_ci	if (count < RFKILL_EVENT_SIZE_V1 - 1)
128262306a36Sopenharmony_ci		return -EINVAL;
128362306a36Sopenharmony_ci
128462306a36Sopenharmony_ci	/*
128562306a36Sopenharmony_ci	 * Copy as much data as we can accept into our 'ev' buffer,
128662306a36Sopenharmony_ci	 * but tell userspace how much we've copied so it can determine
128762306a36Sopenharmony_ci	 * our API version even in a write() call, if it cares.
128862306a36Sopenharmony_ci	 */
128962306a36Sopenharmony_ci	count = min(count, sizeof(ev));
129062306a36Sopenharmony_ci	count = min_t(size_t, count, data->max_size);
129162306a36Sopenharmony_ci	if (copy_from_user(&ev, buf, count))
129262306a36Sopenharmony_ci		return -EFAULT;
129362306a36Sopenharmony_ci
129462306a36Sopenharmony_ci	if (ev.type >= NUM_RFKILL_TYPES)
129562306a36Sopenharmony_ci		return -EINVAL;
129662306a36Sopenharmony_ci
129762306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
129862306a36Sopenharmony_ci
129962306a36Sopenharmony_ci	switch (ev.op) {
130062306a36Sopenharmony_ci	case RFKILL_OP_CHANGE_ALL:
130162306a36Sopenharmony_ci		rfkill_update_global_state(ev.type, ev.soft);
130262306a36Sopenharmony_ci		list_for_each_entry(rfkill, &rfkill_list, node)
130362306a36Sopenharmony_ci			if (rfkill->type == ev.type ||
130462306a36Sopenharmony_ci			    ev.type == RFKILL_TYPE_ALL)
130562306a36Sopenharmony_ci				rfkill_set_block(rfkill, ev.soft);
130662306a36Sopenharmony_ci		ret = 0;
130762306a36Sopenharmony_ci		break;
130862306a36Sopenharmony_ci	case RFKILL_OP_CHANGE:
130962306a36Sopenharmony_ci		list_for_each_entry(rfkill, &rfkill_list, node)
131062306a36Sopenharmony_ci			if (rfkill->idx == ev.idx &&
131162306a36Sopenharmony_ci			    (rfkill->type == ev.type ||
131262306a36Sopenharmony_ci			     ev.type == RFKILL_TYPE_ALL))
131362306a36Sopenharmony_ci				rfkill_set_block(rfkill, ev.soft);
131462306a36Sopenharmony_ci		ret = 0;
131562306a36Sopenharmony_ci		break;
131662306a36Sopenharmony_ci	default:
131762306a36Sopenharmony_ci		ret = -EINVAL;
131862306a36Sopenharmony_ci		break;
131962306a36Sopenharmony_ci	}
132062306a36Sopenharmony_ci
132162306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
132262306a36Sopenharmony_ci
132362306a36Sopenharmony_ci	return ret ?: count;
132462306a36Sopenharmony_ci}
132562306a36Sopenharmony_ci
132662306a36Sopenharmony_cistatic int rfkill_fop_release(struct inode *inode, struct file *file)
132762306a36Sopenharmony_ci{
132862306a36Sopenharmony_ci	struct rfkill_data *data = file->private_data;
132962306a36Sopenharmony_ci	struct rfkill_int_event *ev, *tmp;
133062306a36Sopenharmony_ci
133162306a36Sopenharmony_ci	mutex_lock(&rfkill_global_mutex);
133262306a36Sopenharmony_ci	list_del(&data->list);
133362306a36Sopenharmony_ci	mutex_unlock(&rfkill_global_mutex);
133462306a36Sopenharmony_ci
133562306a36Sopenharmony_ci	mutex_destroy(&data->mtx);
133662306a36Sopenharmony_ci	list_for_each_entry_safe(ev, tmp, &data->events, list)
133762306a36Sopenharmony_ci		kfree(ev);
133862306a36Sopenharmony_ci
133962306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_INPUT
134062306a36Sopenharmony_ci	if (data->input_handler)
134162306a36Sopenharmony_ci		if (atomic_dec_return(&rfkill_input_disabled) == 0)
134262306a36Sopenharmony_ci			printk(KERN_DEBUG "rfkill: input handler enabled\n");
134362306a36Sopenharmony_ci#endif
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_ci	kfree(data);
134662306a36Sopenharmony_ci
134762306a36Sopenharmony_ci	return 0;
134862306a36Sopenharmony_ci}
134962306a36Sopenharmony_ci
135062306a36Sopenharmony_cistatic long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
135162306a36Sopenharmony_ci			     unsigned long arg)
135262306a36Sopenharmony_ci{
135362306a36Sopenharmony_ci	struct rfkill_data *data = file->private_data;
135462306a36Sopenharmony_ci	int ret = -ENOSYS;
135562306a36Sopenharmony_ci	u32 size;
135662306a36Sopenharmony_ci
135762306a36Sopenharmony_ci	if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
135862306a36Sopenharmony_ci		return -ENOSYS;
135962306a36Sopenharmony_ci
136062306a36Sopenharmony_ci	mutex_lock(&data->mtx);
136162306a36Sopenharmony_ci	switch (_IOC_NR(cmd)) {
136262306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_INPUT
136362306a36Sopenharmony_ci	case RFKILL_IOC_NOINPUT:
136462306a36Sopenharmony_ci		if (!data->input_handler) {
136562306a36Sopenharmony_ci			if (atomic_inc_return(&rfkill_input_disabled) == 1)
136662306a36Sopenharmony_ci				printk(KERN_DEBUG "rfkill: input handler disabled\n");
136762306a36Sopenharmony_ci			data->input_handler = true;
136862306a36Sopenharmony_ci		}
136962306a36Sopenharmony_ci		ret = 0;
137062306a36Sopenharmony_ci		break;
137162306a36Sopenharmony_ci#endif
137262306a36Sopenharmony_ci	case RFKILL_IOC_MAX_SIZE:
137362306a36Sopenharmony_ci		if (get_user(size, (__u32 __user *)arg)) {
137462306a36Sopenharmony_ci			ret = -EFAULT;
137562306a36Sopenharmony_ci			break;
137662306a36Sopenharmony_ci		}
137762306a36Sopenharmony_ci		if (size < RFKILL_EVENT_SIZE_V1 || size > U8_MAX) {
137862306a36Sopenharmony_ci			ret = -EINVAL;
137962306a36Sopenharmony_ci			break;
138062306a36Sopenharmony_ci		}
138162306a36Sopenharmony_ci		data->max_size = size;
138262306a36Sopenharmony_ci		ret = 0;
138362306a36Sopenharmony_ci		break;
138462306a36Sopenharmony_ci	default:
138562306a36Sopenharmony_ci		break;
138662306a36Sopenharmony_ci	}
138762306a36Sopenharmony_ci	mutex_unlock(&data->mtx);
138862306a36Sopenharmony_ci
138962306a36Sopenharmony_ci	return ret;
139062306a36Sopenharmony_ci}
139162306a36Sopenharmony_ci
139262306a36Sopenharmony_cistatic const struct file_operations rfkill_fops = {
139362306a36Sopenharmony_ci	.owner		= THIS_MODULE,
139462306a36Sopenharmony_ci	.open		= rfkill_fop_open,
139562306a36Sopenharmony_ci	.read		= rfkill_fop_read,
139662306a36Sopenharmony_ci	.write		= rfkill_fop_write,
139762306a36Sopenharmony_ci	.poll		= rfkill_fop_poll,
139862306a36Sopenharmony_ci	.release	= rfkill_fop_release,
139962306a36Sopenharmony_ci	.unlocked_ioctl	= rfkill_fop_ioctl,
140062306a36Sopenharmony_ci	.compat_ioctl	= compat_ptr_ioctl,
140162306a36Sopenharmony_ci	.llseek		= no_llseek,
140262306a36Sopenharmony_ci};
140362306a36Sopenharmony_ci
140462306a36Sopenharmony_ci#define RFKILL_NAME "rfkill"
140562306a36Sopenharmony_ci
140662306a36Sopenharmony_cistatic struct miscdevice rfkill_miscdev = {
140762306a36Sopenharmony_ci	.fops	= &rfkill_fops,
140862306a36Sopenharmony_ci	.name	= RFKILL_NAME,
140962306a36Sopenharmony_ci	.minor	= RFKILL_MINOR,
141062306a36Sopenharmony_ci};
141162306a36Sopenharmony_ci
141262306a36Sopenharmony_cistatic int __init rfkill_init(void)
141362306a36Sopenharmony_ci{
141462306a36Sopenharmony_ci	int error;
141562306a36Sopenharmony_ci
141662306a36Sopenharmony_ci	rfkill_update_global_state(RFKILL_TYPE_ALL, !rfkill_default_state);
141762306a36Sopenharmony_ci
141862306a36Sopenharmony_ci	error = class_register(&rfkill_class);
141962306a36Sopenharmony_ci	if (error)
142062306a36Sopenharmony_ci		goto error_class;
142162306a36Sopenharmony_ci
142262306a36Sopenharmony_ci	error = misc_register(&rfkill_miscdev);
142362306a36Sopenharmony_ci	if (error)
142462306a36Sopenharmony_ci		goto error_misc;
142562306a36Sopenharmony_ci
142662306a36Sopenharmony_ci	error = rfkill_global_led_trigger_register();
142762306a36Sopenharmony_ci	if (error)
142862306a36Sopenharmony_ci		goto error_led_trigger;
142962306a36Sopenharmony_ci
143062306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_INPUT
143162306a36Sopenharmony_ci	error = rfkill_handler_init();
143262306a36Sopenharmony_ci	if (error)
143362306a36Sopenharmony_ci		goto error_input;
143462306a36Sopenharmony_ci#endif
143562306a36Sopenharmony_ci
143662306a36Sopenharmony_ci	return 0;
143762306a36Sopenharmony_ci
143862306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_INPUT
143962306a36Sopenharmony_cierror_input:
144062306a36Sopenharmony_ci	rfkill_global_led_trigger_unregister();
144162306a36Sopenharmony_ci#endif
144262306a36Sopenharmony_cierror_led_trigger:
144362306a36Sopenharmony_ci	misc_deregister(&rfkill_miscdev);
144462306a36Sopenharmony_cierror_misc:
144562306a36Sopenharmony_ci	class_unregister(&rfkill_class);
144662306a36Sopenharmony_cierror_class:
144762306a36Sopenharmony_ci	return error;
144862306a36Sopenharmony_ci}
144962306a36Sopenharmony_cisubsys_initcall(rfkill_init);
145062306a36Sopenharmony_ci
145162306a36Sopenharmony_cistatic void __exit rfkill_exit(void)
145262306a36Sopenharmony_ci{
145362306a36Sopenharmony_ci#ifdef CONFIG_RFKILL_INPUT
145462306a36Sopenharmony_ci	rfkill_handler_exit();
145562306a36Sopenharmony_ci#endif
145662306a36Sopenharmony_ci	rfkill_global_led_trigger_unregister();
145762306a36Sopenharmony_ci	misc_deregister(&rfkill_miscdev);
145862306a36Sopenharmony_ci	class_unregister(&rfkill_class);
145962306a36Sopenharmony_ci}
146062306a36Sopenharmony_cimodule_exit(rfkill_exit);
146162306a36Sopenharmony_ci
146262306a36Sopenharmony_ciMODULE_ALIAS_MISCDEV(RFKILL_MINOR);
146362306a36Sopenharmony_ciMODULE_ALIAS("devname:" RFKILL_NAME);
1464