1 From eb92150c047e76960ecf8989b9037fd34b43a4af Mon Sep 17 00:00:00 2001
2 From: =?utf-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= <arve@android.com>
3 Date: Fri, 21 Nov 2008 21:47:23 -0800
4 Subject: [PATCH 090/134] input: Add keyreset driver.
6 Content-Type: text/plain; charset=utf-8
7 Content-Transfer-Encoding: 8bit
9 Add a platform device in the board file to specify a reset key-combo.
10 The first time the key-combo is detected a work function that syncs
11 the filesystems is scheduled. If all the keys are released and then
12 pressed again, it calls panic. Reboot on panic should be set for
15 Signed-off-by: Arve Hjønnevåg <arve@android.com>
17 drivers/input/Kconfig | 9 ++
18 drivers/input/Makefile | 1 +
19 drivers/input/keyreset.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++
20 include/linux/keyreset.h | 27 ++++++
21 4 files changed, 266 insertions(+), 0 deletions(-)
22 create mode 100644 drivers/input/keyreset.c
23 create mode 100644 include/linux/keyreset.h
25 --- a/drivers/input/Kconfig
26 +++ b/drivers/input/Kconfig
27 @@ -149,6 +149,15 @@ config INPUT_APMPOWER
28 To compile this driver as a module, choose M here: the
29 module will be called apm-power.
31 +config INPUT_KEYRESET
32 + tristate "Reset key"
35 + Say Y here if you want to reboot when some keys are pressed;
37 + To compile this driver as a module, choose M here: the
38 + module will be called keyreset.
40 config XEN_KBDDEV_FRONTEND
41 tristate "Xen virtual keyboard and mouse support"
42 depends on XEN_FBDEV_FRONTEND
43 --- a/drivers/input/Makefile
44 +++ b/drivers/input/Makefile
45 @@ -23,5 +23,6 @@ obj-$(CONFIG_INPUT_TOUCHSCREEN) += touch
46 obj-$(CONFIG_INPUT_MISC) += misc/
48 obj-$(CONFIG_INPUT_APMPOWER) += apm-power.o
49 +obj-$(CONFIG_INPUT_KEYRESET) += keyreset.o
51 obj-$(CONFIG_XEN_KBDDEV_FRONTEND) += xen-kbdfront.o
53 +++ b/drivers/input/keyreset.c
55 +/* drivers/input/keyreset.c
57 + * Copyright (C) 2008 Google, Inc.
59 + * This software is licensed under the terms of the GNU General Public
60 + * License version 2, as published by the Free Software Foundation, and
61 + * may be copied, distributed, and modified under those terms.
63 + * This program is distributed in the hope that it will be useful,
64 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 + * GNU General Public License for more details.
70 +#include <linux/input.h>
71 +#include <linux/keyreset.h>
72 +#include <linux/module.h>
73 +#include <linux/platform_device.h>
74 +#include <linux/reboot.h>
75 +#include <linux/sched.h>
76 +#include <linux/syscalls.h>
79 +struct keyreset_state {
80 + struct input_handler input_handler;
81 + unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
82 + unsigned long upbit[BITS_TO_LONGS(KEY_CNT)];
83 + unsigned long key[BITS_TO_LONGS(KEY_CNT)];
85 + int key_down_target;
88 + int restart_disabled;
91 +int restart_requested;
92 +static void deferred_restart(struct work_struct *dummy)
94 + restart_requested = 2;
96 + restart_requested = 3;
97 + kernel_restart(NULL);
99 +static DECLARE_WORK(restart_work, deferred_restart);
101 +static void keyreset_event(struct input_handle *handle, unsigned int type,
102 + unsigned int code, int value)
104 + unsigned long flags;
105 + struct keyreset_state *state = handle->private;
107 + if (type != EV_KEY)
110 + if (code >= KEY_MAX)
113 + if (!test_bit(code, state->keybit))
116 + spin_lock_irqsave(&state->lock, flags);
117 + if (!test_bit(code, state->key) == !value)
119 + __change_bit(code, state->key);
120 + if (test_bit(code, state->upbit)) {
122 + state->restart_disabled = 1;
132 + if (state->key_down == 0 && state->key_up == 0)
133 + state->restart_disabled = 0;
135 + pr_debug("reset key changed %d %d new state %d-%d-%d\n", code, value,
136 + state->key_down, state->key_up, state->restart_disabled);
138 + if (value && !state->restart_disabled &&
139 + state->key_down == state->key_down_target) {
140 + state->restart_disabled = 1;
141 + if (restart_requested)
142 + panic("keyboard reset failed, %d", restart_requested);
143 + pr_info("keyboard reset\n");
144 + schedule_work(&restart_work);
145 + restart_requested = 1;
148 + spin_unlock_irqrestore(&state->lock, flags);
151 +static int keyreset_connect(struct input_handler *handler,
152 + struct input_dev *dev,
153 + const struct input_device_id *id)
157 + struct input_handle *handle;
158 + struct keyreset_state *state =
159 + container_of(handler, struct keyreset_state, input_handler);
161 + for (i = 0; i < KEY_MAX; i++) {
162 + if (test_bit(i, state->keybit) && test_bit(i, dev->keybit))
168 + handle = kzalloc(sizeof(*handle), GFP_KERNEL);
173 + handle->handler = handler;
174 + handle->name = "keyreset";
175 + handle->private = state;
177 + ret = input_register_handle(handle);
179 + goto err_input_register_handle;
181 + ret = input_open_device(handle);
183 + goto err_input_open_device;
185 + pr_info("using input dev %s for key reset\n", dev->name);
189 +err_input_open_device:
190 + input_unregister_handle(handle);
191 +err_input_register_handle:
196 +static void keyreset_disconnect(struct input_handle *handle)
198 + input_close_device(handle);
199 + input_unregister_handle(handle);
203 +static const struct input_device_id keyreset_ids[] = {
205 + .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
206 + .evbit = { BIT_MASK(EV_KEY) },
210 +MODULE_DEVICE_TABLE(input, keyreset_ids);
212 +static int keyreset_probe(struct platform_device *pdev)
216 + struct keyreset_state *state;
217 + struct keyreset_platform_data *pdata = pdev->dev.platform_data;
222 + state = kzalloc(sizeof(*state), GFP_KERNEL);
226 + spin_lock_init(&state->lock);
227 + keyp = pdata->keys_down;
228 + while ((key = *keyp++)) {
229 + if (key >= KEY_MAX)
231 + state->key_down_target++;
232 + __set_bit(key, state->keybit);
234 + if (pdata->keys_up) {
235 + keyp = pdata->keys_up;
236 + while ((key = *keyp++)) {
237 + if (key >= KEY_MAX)
239 + __set_bit(key, state->keybit);
240 + __set_bit(key, state->upbit);
243 + state->input_handler.event = keyreset_event;
244 + state->input_handler.connect = keyreset_connect;
245 + state->input_handler.disconnect = keyreset_disconnect;
246 + state->input_handler.name = KEYRESET_NAME;
247 + state->input_handler.id_table = keyreset_ids;
248 + ret = input_register_handler(&state->input_handler);
253 + platform_set_drvdata(pdev, state);
257 +int keyreset_remove(struct platform_device *pdev)
259 + struct keyreset_state *state = platform_get_drvdata(pdev);
260 + input_unregister_handler(&state->input_handler);
266 +struct platform_driver keyreset_driver = {
267 + .driver.name = KEYRESET_NAME,
268 + .probe = keyreset_probe,
269 + .remove = keyreset_remove,
272 +static int __init keyreset_init(void)
274 + return platform_driver_register(&keyreset_driver);
277 +static void __exit keyreset_exit(void)
279 + return platform_driver_unregister(&keyreset_driver);
282 +module_init(keyreset_init);
283 +module_exit(keyreset_exit);
285 +++ b/include/linux/keyreset.h
288 + * include/linux/keyreset.h - platform data structure for resetkeys driver
290 + * Copyright (C) 2008 Google, Inc.
292 + * This software is licensed under the terms of the GNU General Public
293 + * License version 2, as published by the Free Software Foundation, and
294 + * may be copied, distributed, and modified under those terms.
296 + * This program is distributed in the hope that it will be useful,
297 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
298 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
299 + * GNU General Public License for more details.
303 +#ifndef _LINUX_KEYRESET_H
304 +#define _LINUX_KEYRESET_H
306 +#define KEYRESET_NAME "keyreset"
308 +struct keyreset_platform_data {
310 + int keys_down[]; /* 0 terminated */
313 +#endif /* _LINUX_KEYRESET_H */