rfkill-input.c 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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>

20 21
#include "rfkill-input.h"

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
MODULE_DESCRIPTION("Input layer to RF switch connector");
MODULE_LICENSE("GPL");

struct rfkill_task {
	struct work_struct work;
	enum rfkill_type type;
	struct mutex mutex; /* ensures that task is serialized */
	spinlock_t lock; /* for accessing last and desired state */
	unsigned long last; /* last schedule */
	enum rfkill_state desired_state; /* on/off */
};

static void rfkill_task_handler(struct work_struct *work)
{
	struct rfkill_task *task = container_of(work, struct rfkill_task, work);

	mutex_lock(&task->mutex);

41
	rfkill_switch_all(task->type, task->desired_state);
42 43 44 45

	mutex_unlock(&task->mutex);
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static void rfkill_schedule_set(struct rfkill_task *task,
				enum rfkill_state desired_state)
{
	unsigned long flags;

	spin_lock_irqsave(&task->lock, flags);

	if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
		task->desired_state = desired_state;
		task->last = jiffies;
		schedule_work(&task->work);
	}

	spin_unlock_irqrestore(&task->lock, flags);
}

62 63
static void rfkill_schedule_toggle(struct rfkill_task *task)
{
64
	unsigned long flags;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

	spin_lock_irqsave(&task->lock, flags);

	if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
		task->desired_state = !task->desired_state;
		task->last = jiffies;
		schedule_work(&task->work);
	}

	spin_unlock_irqrestore(&task->lock, flags);
}

#define DEFINE_RFKILL_TASK(n, t)			\
	struct rfkill_task n = {			\
		.work = __WORK_INITIALIZER(n.work,	\
				rfkill_task_handler),	\
		.type = t,				\
		.mutex = __MUTEX_INITIALIZER(n.mutex),	\
		.lock = __SPIN_LOCK_UNLOCKED(n.lock),	\
		.desired_state = RFKILL_STATE_ON,	\
	}

static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
89
static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
90
static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX);
91
static DEFINE_RFKILL_TASK(rfkill_wwan, RFKILL_TYPE_WWAN);
92 93

static void rfkill_event(struct input_handle *handle, unsigned int type,
94
			unsigned int code, int data)
95
{
96
	if (type == EV_KEY && data == 1) {
97 98 99 100 101 102 103
		switch (code) {
		case KEY_WLAN:
			rfkill_schedule_toggle(&rfkill_wlan);
			break;
		case KEY_BLUETOOTH:
			rfkill_schedule_toggle(&rfkill_bt);
			break;
104 105 106
		case KEY_UWB:
			rfkill_schedule_toggle(&rfkill_uwb);
			break;
107 108 109
		case KEY_WIMAX:
			rfkill_schedule_toggle(&rfkill_wimax);
			break;
110 111 112
		default:
			break;
		}
113 114 115 116
	} else if (type == EV_SW) {
		switch (code) {
		case SW_RFKILL_ALL:
			/* EVERY radio type. data != 0 means radios ON */
117 118 119
			rfkill_schedule_set(&rfkill_wwan,
					    (data)? RFKILL_STATE_ON:
						    RFKILL_STATE_OFF);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
			rfkill_schedule_set(&rfkill_wimax,
					    (data)? RFKILL_STATE_ON:
						    RFKILL_STATE_OFF);
			rfkill_schedule_set(&rfkill_uwb,
					    (data)? RFKILL_STATE_ON:
						    RFKILL_STATE_OFF);
			rfkill_schedule_set(&rfkill_bt,
					    (data)? RFKILL_STATE_ON:
						    RFKILL_STATE_OFF);
			rfkill_schedule_set(&rfkill_wlan,
					    (data)? RFKILL_STATE_ON:
						    RFKILL_STATE_OFF);
			break;
		default:
			break;
		}
136 137 138 139 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
	}
}

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";

	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;
}

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,
180 181
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
182 183 184
	},
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
185 186
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
187
	},
188 189
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
190 191
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
192
	},
193 194 195 196 197
	{
		.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) },
	},
198 199 200 201 202
	{
		.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) },
	},
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	{ }
};

static struct input_handler rfkill_handler = {
	.event =	rfkill_event,
	.connect =	rfkill_connect,
	.disconnect =	rfkill_disconnect,
	.name =		"rfkill",
	.id_table =	rfkill_ids,
};

static int __init rfkill_handler_init(void)
{
	return input_register_handler(&rfkill_handler);
}

static void __exit rfkill_handler_exit(void)
{
	input_unregister_handler(&rfkill_handler);
	flush_scheduled_work();
}

module_init(rfkill_handler_init);
module_exit(rfkill_handler_exit);