rfkill-input.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Input layer to RF Kill interface connector
 *
 * Copyright (c) 2007 Dmitry Torokhov
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/init.h>
#include <linux/rfkill.h>
19
#include <linux/sched.h>
20

21 22
#include "rfkill-input.h"

23 24 25 26
MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
MODULE_DESCRIPTION("Input layer to RF switch connector");
MODULE_LICENSE("GPL");

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
enum rfkill_input_master_mode {
	RFKILL_INPUT_MASTER_DONOTHING = 0,
	RFKILL_INPUT_MASTER_RESTORE = 1,
	RFKILL_INPUT_MASTER_UNBLOCKALL = 2,
	RFKILL_INPUT_MASTER_MAX,	/* marker */
};

static enum rfkill_input_master_mode rfkill_master_switch_mode =
					RFKILL_INPUT_MASTER_UNBLOCKALL;
module_param_named(master_switch_mode, rfkill_master_switch_mode, uint, 0);
MODULE_PARM_DESC(master_switch_mode,
	"SW_RFKILL_ALL ON should: 0=do nothing; 1=restore; 2=unblock all");

enum rfkill_global_sched_op {
	RFKILL_GLOBAL_OP_EPO = 0,
	RFKILL_GLOBAL_OP_RESTORE,
	RFKILL_GLOBAL_OP_UNLOCK,
	RFKILL_GLOBAL_OP_UNBLOCK,
};

/*
 * Currently, the code marked with RFKILL_NEED_SWSET is inactive.
 * If handling of EV_SW SW_WLAN/WWAN/BLUETOOTH/etc is needed in the
 * future, when such events are added, that code will be necessary.
 */

53 54
struct rfkill_task {
	struct work_struct work;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

	/* ensures that task is serialized */
	struct mutex mutex;

	/* protects everything below */
	spinlock_t lock;

	/* pending regular switch operations (1=pending) */
	unsigned long sw_pending[BITS_TO_LONGS(RFKILL_TYPE_MAX)];

#ifdef RFKILL_NEED_SWSET
	/* set operation pending (1=pending) */
	unsigned long sw_setpending[BITS_TO_LONGS(RFKILL_TYPE_MAX)];

	/* desired state for pending set operation (1=unblock) */
	unsigned long sw_newstate[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
#endif

	/* should the state be complemented (1=yes) */
	unsigned long sw_togglestate[BITS_TO_LONGS(RFKILL_TYPE_MAX)];

	bool global_op_pending;
	enum rfkill_global_sched_op op;
78 79
};

80
static void __rfkill_handle_global_op(enum rfkill_global_sched_op op)
81
{
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	unsigned int i;

	switch (op) {
	case RFKILL_GLOBAL_OP_EPO:
		rfkill_epo();
		break;
	case RFKILL_GLOBAL_OP_RESTORE:
		rfkill_restore_states();
		break;
	case RFKILL_GLOBAL_OP_UNLOCK:
		rfkill_remove_epo_lock();
		break;
	case RFKILL_GLOBAL_OP_UNBLOCK:
		rfkill_remove_epo_lock();
		for (i = 0; i < RFKILL_TYPE_MAX; i++)
			rfkill_switch_all(i, RFKILL_STATE_UNBLOCKED);
		break;
	default:
		/* memory corruption or bug, fail safely */
		rfkill_epo();
		WARN(1, "Unknown requested operation %d! "
			"rfkill Emergency Power Off activated\n",
			op);
	}
}
107

108 109 110 111 112
#ifdef RFKILL_NEED_SWSET
static void __rfkill_handle_normal_op(const enum rfkill_type type,
			const bool sp, const bool s, const bool c)
{
	enum rfkill_state state;
113

114 115 116 117 118
	if (sp)
		state = (s) ? RFKILL_STATE_UNBLOCKED :
			      RFKILL_STATE_SOFT_BLOCKED;
	else
		state = rfkill_get_global_state(type);
119

120 121 122 123
	if (c)
		state = rfkill_state_complement(state);

	rfkill_switch_all(type, state);
124
}
125 126 127 128 129 130 131 132 133 134 135 136 137
#else
static void __rfkill_handle_normal_op(const enum rfkill_type type,
			const bool c)
{
	enum rfkill_state state;

	state = rfkill_get_global_state(type);
	if (c)
		state = rfkill_state_complement(state);

	rfkill_switch_all(type, state);
}
#endif
138

139
static void rfkill_task_handler(struct work_struct *work)
140
{
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	struct rfkill_task *task =
			container_of(work, struct rfkill_task, work);
	bool doit = true;

	mutex_lock(&task->mutex);

	spin_lock_irq(&task->lock);
	while (doit) {
		if (task->global_op_pending) {
			enum rfkill_global_sched_op op = task->op;
			task->global_op_pending = false;
			memset(task->sw_pending, 0, sizeof(task->sw_pending));
			spin_unlock_irq(&task->lock);

			__rfkill_handle_global_op(op);

			/* make sure we do at least one pass with
			 * !task->global_op_pending */
			spin_lock_irq(&task->lock);
			continue;
		} else if (!rfkill_is_epo_lock_active()) {
			unsigned int i = 0;

			while (!task->global_op_pending &&
						i < RFKILL_TYPE_MAX) {
				if (test_and_clear_bit(i, task->sw_pending)) {
					bool c;
#ifdef RFKILL_NEED_SWSET
					bool sp, s;
					sp = test_and_clear_bit(i,
							task->sw_setpending);
					s = test_bit(i, task->sw_newstate);
#endif
					c = test_and_clear_bit(i,
							task->sw_togglestate);
					spin_unlock_irq(&task->lock);

#ifdef RFKILL_NEED_SWSET
					__rfkill_handle_normal_op(i, sp, s, c);
#else
					__rfkill_handle_normal_op(i, c);
#endif

					spin_lock_irq(&task->lock);
				}
				i++;
			}
		}
		doit = task->global_op_pending;
	}
	spin_unlock_irq(&task->lock);

	mutex_unlock(&task->mutex);
194 195
}

196 197 198 199 200 201
static struct rfkill_task rfkill_task = {
	.work = __WORK_INITIALIZER(rfkill_task.work,
				rfkill_task_handler),
	.mutex = __MUTEX_INITIALIZER(rfkill_task.mutex),
	.lock = __SPIN_LOCK_UNLOCKED(rfkill_task.lock),
};
202

203
static void rfkill_schedule_global_op(enum rfkill_global_sched_op op)
204
{
205 206 207 208 209 210 211
	unsigned long flags;

	spin_lock_irqsave(&rfkill_task.lock, flags);
	rfkill_task.op = op;
	rfkill_task.global_op_pending = true;
	schedule_work(&rfkill_task.work);
	spin_unlock_irqrestore(&rfkill_task.lock, flags);
212 213
}

214 215 216 217
#ifdef RFKILL_NEED_SWSET
/* Use this if you need to add EV_SW SW_WLAN/WWAN/BLUETOOTH/etc handling */

static void rfkill_schedule_set(enum rfkill_type type,
218 219 220 221
				enum rfkill_state desired_state)
{
	unsigned long flags;

222
	if (rfkill_is_epo_lock_active())
223 224
		return;

225 226 227 228 229 230 231 232 233 234
	spin_lock_irqsave(&rfkill_task.lock, flags);
	if (!rfkill_task.global_op_pending) {
		set_bit(type, rfkill_task.sw_pending);
		set_bit(type, rfkill_task.sw_setpending);
		clear_bit(type, rfkill_task.sw_togglestate);
		if (desired_state)
			set_bit(type,  rfkill_task.sw_newstate);
		else
			clear_bit(type, rfkill_task.sw_newstate);
		schedule_work(&rfkill_task.work);
235
	}
236
	spin_unlock_irqrestore(&rfkill_task.lock, flags);
237
}
238
#endif
239

240
static void rfkill_schedule_toggle(enum rfkill_type type)
241
{
242
	unsigned long flags;
243

244
	if (rfkill_is_epo_lock_active())
245 246
		return;

247 248 249 250 251
	spin_lock_irqsave(&rfkill_task.lock, flags);
	if (!rfkill_task.global_op_pending) {
		set_bit(type, rfkill_task.sw_pending);
		change_bit(type, rfkill_task.sw_togglestate);
		schedule_work(&rfkill_task.work);
252
	}
253
	spin_unlock_irqrestore(&rfkill_task.lock, flags);
254 255
}

256 257 258
static void rfkill_schedule_evsw_rfkillall(int state)
{
	if (state) {
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
		switch (rfkill_master_switch_mode) {
		case RFKILL_INPUT_MASTER_UNBLOCKALL:
			rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNBLOCK);
			break;
		case RFKILL_INPUT_MASTER_RESTORE:
			rfkill_schedule_global_op(RFKILL_GLOBAL_OP_RESTORE);
			break;
		case RFKILL_INPUT_MASTER_DONOTHING:
			rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNLOCK);
			break;
		default:
			/* memory corruption or driver bug! fail safely */
			rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
			WARN(1, "Unknown rfkill_master_switch_mode (%d), "
				"driver bug or memory corruption detected!\n",
				rfkill_master_switch_mode);
			break;
		}
277
	} else
278
		rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
279 280
}

281
static void rfkill_event(struct input_handle *handle, unsigned int type,
282
			unsigned int code, int data)
283
{
284
	if (type == EV_KEY && data == 1) {
285 286
		enum rfkill_type t;

287 288
		switch (code) {
		case KEY_WLAN:
289
			t = RFKILL_TYPE_WLAN;
290 291
			break;
		case KEY_BLUETOOTH:
292
			t = RFKILL_TYPE_BLUETOOTH;
293
			break;
294
		case KEY_UWB:
295
			t = RFKILL_TYPE_UWB;
296
			break;
297
		case KEY_WIMAX:
298
			t = RFKILL_TYPE_WIMAX;
299
			break;
300
		default:
301
			return;
302
		}
303 304
		rfkill_schedule_toggle(t);
		return;
305 306 307
	} else if (type == EV_SW) {
		switch (code) {
		case SW_RFKILL_ALL:
308
			rfkill_schedule_evsw_rfkillall(data);
309
			return;
310
		default:
311
			return;
312
		}
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	}
}

static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
			  const struct input_device_id *id)
{
	struct input_handle *handle;
	int error;

	handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
	if (!handle)
		return -ENOMEM;

	handle->dev = dev;
	handle->handler = handler;
	handle->name = "rfkill";

330
	/* causes rfkill_start() to be called */
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	error = input_register_handle(handle);
	if (error)
		goto err_free_handle;

	error = input_open_device(handle);
	if (error)
		goto err_unregister_handle;

	return 0;

 err_unregister_handle:
	input_unregister_handle(handle);
 err_free_handle:
	kfree(handle);
	return error;
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
static void rfkill_start(struct input_handle *handle)
{
	/* Take event_lock to guard against configuration changes, we
	 * should be able to deal with concurrency with rfkill_event()
	 * just fine (which event_lock will also avoid). */
	spin_lock_irq(&handle->dev->event_lock);

	if (test_bit(EV_SW, handle->dev->evbit)) {
		if (test_bit(SW_RFKILL_ALL, handle->dev->swbit))
			rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
							handle->dev->sw));
		/* add resync for further EV_SW events here */
	}

	spin_unlock_irq(&handle->dev->event_lock);
}

365 366 367 368 369 370 371 372 373 374
static void rfkill_disconnect(struct input_handle *handle)
{
	input_close_device(handle);
	input_unregister_handle(handle);
	kfree(handle);
}

static const struct input_device_id rfkill_ids[] = {
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
375 376
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
377 378 379
	},
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
380 381
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
382
	},
383 384
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
385 386
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
387
	},
388 389 390 391 392
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
	},
393 394 395 396 397
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
		.evbit = { BIT(EV_SW) },
		.swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
	},
398 399 400 401 402 403 404
	{ }
};

static struct input_handler rfkill_handler = {
	.event =	rfkill_event,
	.connect =	rfkill_connect,
	.disconnect =	rfkill_disconnect,
405
	.start =	rfkill_start,
406 407 408 409 410 411
	.name =		"rfkill",
	.id_table =	rfkill_ids,
};

static int __init rfkill_handler_init(void)
{
412 413 414
	if (rfkill_master_switch_mode >= RFKILL_INPUT_MASTER_MAX)
		return -EINVAL;

415 416 417 418 419 420 421
	return input_register_handler(&rfkill_handler);
}

static void __exit rfkill_handler_exit(void)
{
	input_unregister_handler(&rfkill_handler);
	flush_scheduled_work();
422
	rfkill_remove_epo_lock();
423 424 425 426
}

module_init(rfkill_handler_init);
module_exit(rfkill_handler_exit);