Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Input layer to RF Kill interface connector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2007 Dmitry Torokhov
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * If you ever run into a situation in which you have a SW_ type rfkill
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * input device, then you can revive code that was removed in the patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * "rfkill-input: remove unused code".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/rfkill.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "rfkill.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) enum rfkill_input_master_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	RFKILL_INPUT_MASTER_UNLOCK = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	RFKILL_INPUT_MASTER_RESTORE = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	RFKILL_INPUT_MASTER_UNBLOCKALL = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	NUM_RFKILL_INPUT_MASTER_MODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /* Delay (in ms) between consecutive switch ops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define RFKILL_OPS_DELAY 200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static enum rfkill_input_master_mode rfkill_master_switch_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 					RFKILL_INPUT_MASTER_UNBLOCKALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) module_param_named(master_switch_mode, rfkill_master_switch_mode, uint, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) MODULE_PARM_DESC(master_switch_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	"SW_RFKILL_ALL ON should: 0=do nothing (only unlock); 1=restore; 2=unblock all");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static spinlock_t rfkill_op_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static bool rfkill_op_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static unsigned long rfkill_sw_pending[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static unsigned long rfkill_sw_state[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) enum rfkill_sched_op {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	RFKILL_GLOBAL_OP_EPO = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	RFKILL_GLOBAL_OP_RESTORE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	RFKILL_GLOBAL_OP_UNLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	RFKILL_GLOBAL_OP_UNBLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static enum rfkill_sched_op rfkill_master_switch_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static enum rfkill_sched_op rfkill_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static void __rfkill_handle_global_op(enum rfkill_sched_op op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	switch (op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	case RFKILL_GLOBAL_OP_EPO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		rfkill_epo();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	case RFKILL_GLOBAL_OP_RESTORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		rfkill_restore_states();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	case RFKILL_GLOBAL_OP_UNLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		rfkill_remove_epo_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	case RFKILL_GLOBAL_OP_UNBLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		rfkill_remove_epo_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		for (i = 0; i < NUM_RFKILL_TYPES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			rfkill_switch_all(i, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		/* memory corruption or bug, fail safely */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		rfkill_epo();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		WARN(1, "Unknown requested operation %d! "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			"rfkill Emergency Power Off activated\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static void __rfkill_handle_normal_op(const enum rfkill_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				      const bool complement)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	bool blocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	blocked = rfkill_get_global_sw_state(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (complement)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		blocked = !blocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	rfkill_switch_all(type, blocked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static void rfkill_op_handler(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	bool c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	spin_lock_irq(&rfkill_op_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		if (rfkill_op_pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			enum rfkill_sched_op op = rfkill_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			rfkill_op_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			memset(rfkill_sw_pending, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				sizeof(rfkill_sw_pending));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			spin_unlock_irq(&rfkill_op_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			__rfkill_handle_global_op(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			spin_lock_irq(&rfkill_op_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			 * handle global ops first -- during unlocked period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			 * we might have gotten a new global op.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			if (rfkill_op_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		if (rfkill_is_epo_lock_active())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		for (i = 0; i < NUM_RFKILL_TYPES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			if (__test_and_clear_bit(i, rfkill_sw_pending)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				c = __test_and_clear_bit(i, rfkill_sw_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				spin_unlock_irq(&rfkill_op_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				__rfkill_handle_normal_op(i, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				spin_lock_irq(&rfkill_op_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	} while (rfkill_op_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	spin_unlock_irq(&rfkill_op_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static DECLARE_DELAYED_WORK(rfkill_op_work, rfkill_op_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static unsigned long rfkill_last_scheduled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static unsigned long rfkill_ratelimit(const unsigned long last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return time_after(jiffies, last + delay) ? 0 : delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static void rfkill_schedule_ratelimited(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (schedule_delayed_work(&rfkill_op_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				  rfkill_ratelimit(rfkill_last_scheduled)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		rfkill_last_scheduled = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void rfkill_schedule_global_op(enum rfkill_sched_op op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	spin_lock_irqsave(&rfkill_op_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	rfkill_op = op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	rfkill_op_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		/* bypass the limiter for EPO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		mod_delayed_work(system_wq, &rfkill_op_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		rfkill_last_scheduled = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		rfkill_schedule_ratelimited();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	spin_unlock_irqrestore(&rfkill_op_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static void rfkill_schedule_toggle(enum rfkill_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (rfkill_is_epo_lock_active())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	spin_lock_irqsave(&rfkill_op_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!rfkill_op_pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		__set_bit(type, rfkill_sw_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		__change_bit(type, rfkill_sw_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		rfkill_schedule_ratelimited();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	spin_unlock_irqrestore(&rfkill_op_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static void rfkill_schedule_evsw_rfkillall(int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		rfkill_schedule_global_op(rfkill_master_switch_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static void rfkill_event(struct input_handle *handle, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			unsigned int code, int data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (type == EV_KEY && data == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		case KEY_WLAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			rfkill_schedule_toggle(RFKILL_TYPE_WLAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		case KEY_BLUETOOTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			rfkill_schedule_toggle(RFKILL_TYPE_BLUETOOTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		case KEY_UWB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			rfkill_schedule_toggle(RFKILL_TYPE_UWB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		case KEY_WIMAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			rfkill_schedule_toggle(RFKILL_TYPE_WIMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		case KEY_RFKILL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			rfkill_schedule_toggle(RFKILL_TYPE_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	} else if (type == EV_SW && code == SW_RFKILL_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		rfkill_schedule_evsw_rfkillall(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			  const struct input_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct input_handle *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	handle->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	handle->handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	handle->name = "rfkill";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* causes rfkill_start() to be called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	error = input_register_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		goto err_free_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	error = input_open_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		goto err_unregister_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  err_unregister_handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	input_unregister_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  err_free_handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	kfree(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static void rfkill_start(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	 * Take event_lock to guard against configuration changes, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	 * should be able to deal with concurrency with rfkill_event()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	 * just fine (which event_lock will also avoid).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	spin_lock_irq(&handle->dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (test_bit(EV_SW, handle->dev->evbit) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	    test_bit(SW_RFKILL_ALL, handle->dev->swbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 							handle->dev->sw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	spin_unlock_irq(&handle->dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static void rfkill_disconnect(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	input_close_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	input_unregister_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	kfree(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static const struct input_device_id rfkill_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		.evbit = { BIT_MASK(EV_KEY) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		.keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		.evbit = { BIT_MASK(EV_KEY) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		.keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		.evbit = { BIT_MASK(EV_KEY) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		.keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		.evbit = { BIT_MASK(EV_KEY) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		.keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		.evbit = { BIT_MASK(EV_KEY) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		.keybit = { [BIT_WORD(KEY_RFKILL)] = BIT_MASK(KEY_RFKILL) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		.evbit = { BIT(EV_SW) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		.swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static struct input_handler rfkill_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	.name =	"rfkill",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	.event = rfkill_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	.connect = rfkill_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	.start = rfkill_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	.disconnect = rfkill_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	.id_table = rfkill_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int __init rfkill_handler_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	switch (rfkill_master_switch_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	case RFKILL_INPUT_MASTER_UNBLOCKALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	case RFKILL_INPUT_MASTER_RESTORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		rfkill_master_switch_op = RFKILL_GLOBAL_OP_RESTORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	case RFKILL_INPUT_MASTER_UNLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	spin_lock_init(&rfkill_op_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	/* Avoid delay at first schedule */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	rfkill_last_scheduled =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return input_register_handler(&rfkill_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) void __exit rfkill_handler_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	input_unregister_handler(&rfkill_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	cancel_delayed_work_sync(&rfkill_op_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }