ps3: R.I.P.
[openwrt.git] / target / linux / goldfish / patches-2.6.30 / 0090-input-Add-keyreset-driver.patch
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.
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=utf-8
7 Content-Transfer-Encoding: 8bit
8
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
13 this to work.
14
15 Signed-off-by: Arve Hjønnevåg <arve@android.com>
16 ---
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
24
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.
30  
31 +config INPUT_KEYRESET
32 +       tristate "Reset key"
33 +       depends on INPUT
34 +       ---help---
35 +         Say Y here if you want to reboot when some keys are pressed;
36 +
37 +         To compile this driver as a module, choose M here: the
38 +         module will be called keyreset.
39 +
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/
47  
48  obj-$(CONFIG_INPUT_APMPOWER)   += apm-power.o
49 +obj-$(CONFIG_INPUT_KEYRESET)   += keyreset.o
50  
51  obj-$(CONFIG_XEN_KBDDEV_FRONTEND)      += xen-kbdfront.o
52 --- /dev/null
53 +++ b/drivers/input/keyreset.c
54 @@ -0,0 +1,229 @@
55 +/* drivers/input/keyreset.c
56 + *
57 + * Copyright (C) 2008 Google, Inc.
58 + *
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.
62 + *
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.
67 + *
68 + */
69 +
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>
77 +
78 +
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)];
84 +       spinlock_t lock;
85 +       int key_down_target;
86 +       int key_down;
87 +       int key_up;
88 +       int restart_disabled;
89 +};
90 +
91 +int restart_requested;
92 +static void deferred_restart(struct work_struct *dummy)
93 +{
94 +       restart_requested = 2;
95 +       sys_sync();
96 +       restart_requested = 3;
97 +       kernel_restart(NULL);
98 +}
99 +static DECLARE_WORK(restart_work, deferred_restart);
100 +
101 +static void keyreset_event(struct input_handle *handle, unsigned int type,
102 +                          unsigned int code, int value)
103 +{
104 +       unsigned long flags;
105 +       struct keyreset_state *state = handle->private;
106 +
107 +       if (type != EV_KEY)
108 +               return;
109 +
110 +       if (code >= KEY_MAX)
111 +               return;
112 +
113 +       if (!test_bit(code, state->keybit))
114 +               return;
115 +
116 +       spin_lock_irqsave(&state->lock, flags);
117 +       if (!test_bit(code, state->key) == !value)
118 +               goto done;
119 +       __change_bit(code, state->key);
120 +       if (test_bit(code, state->upbit)) {
121 +               if (value) {
122 +                       state->restart_disabled = 1;
123 +                       state->key_up++;
124 +               } else
125 +                       state->key_up--;
126 +       } else {
127 +               if (value)
128 +                       state->key_down++;
129 +               else
130 +                       state->key_down--;
131 +       }
132 +       if (state->key_down == 0 && state->key_up == 0)
133 +               state->restart_disabled = 0;
134 +
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);
137 +
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;
146 +       }
147 +done:
148 +       spin_unlock_irqrestore(&state->lock, flags);
149 +}
150 +
151 +static int keyreset_connect(struct input_handler *handler,
152 +                                         struct input_dev *dev,
153 +                                         const struct input_device_id *id)
154 +{
155 +       int i;
156 +       int ret;
157 +       struct input_handle *handle;
158 +       struct keyreset_state *state =
159 +               container_of(handler, struct keyreset_state, input_handler);
160 +
161 +       for (i = 0; i < KEY_MAX; i++) {
162 +               if (test_bit(i, state->keybit) && test_bit(i, dev->keybit))
163 +                       break;
164 +       }
165 +       if (i == KEY_MAX)
166 +               return -ENODEV;
167 +
168 +       handle = kzalloc(sizeof(*handle), GFP_KERNEL);
169 +       if (!handle)
170 +               return -ENOMEM;
171 +
172 +       handle->dev = dev;
173 +       handle->handler = handler;
174 +       handle->name = "keyreset";
175 +       handle->private = state;
176 +
177 +       ret = input_register_handle(handle);
178 +       if (ret)
179 +               goto err_input_register_handle;
180 +
181 +       ret = input_open_device(handle);
182 +       if (ret)
183 +               goto err_input_open_device;
184 +
185 +       pr_info("using input dev %s for key reset\n", dev->name);
186 +
187 +       return 0;
188 +
189 +err_input_open_device:
190 +       input_unregister_handle(handle);
191 +err_input_register_handle:
192 +       kfree(handle);
193 +       return ret;
194 +}
195 +
196 +static void keyreset_disconnect(struct input_handle *handle)
197 +{
198 +       input_close_device(handle);
199 +       input_unregister_handle(handle);
200 +       kfree(handle);
201 +}
202 +
203 +static const struct input_device_id keyreset_ids[] = {
204 +       {
205 +               .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
206 +               .evbit = { BIT_MASK(EV_KEY) },
207 +       },
208 +       { },
209 +};
210 +MODULE_DEVICE_TABLE(input, keyreset_ids);
211 +
212 +static int keyreset_probe(struct platform_device *pdev)
213 +{
214 +       int ret;
215 +       int key, *keyp;
216 +       struct keyreset_state *state;
217 +       struct keyreset_platform_data *pdata = pdev->dev.platform_data;
218 +
219 +       if (!pdata)
220 +               return -EINVAL;
221 +
222 +       state = kzalloc(sizeof(*state), GFP_KERNEL);
223 +       if (!state)
224 +               return -ENOMEM;
225 +
226 +       spin_lock_init(&state->lock);
227 +       keyp = pdata->keys_down;
228 +       while ((key = *keyp++)) {
229 +               if (key >= KEY_MAX)
230 +                       continue;
231 +               state->key_down_target++;
232 +               __set_bit(key, state->keybit);
233 +       }
234 +       if (pdata->keys_up) {
235 +               keyp = pdata->keys_up;
236 +               while ((key = *keyp++)) {
237 +                       if (key >= KEY_MAX)
238 +                               continue;
239 +                       __set_bit(key, state->keybit);
240 +                       __set_bit(key, state->upbit);
241 +               }
242 +       }
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);
249 +       if (ret) {
250 +               kfree(state);
251 +               return ret;
252 +       }
253 +       platform_set_drvdata(pdev, state);
254 +       return 0;
255 +}
256 +
257 +int keyreset_remove(struct platform_device *pdev)
258 +{
259 +       struct keyreset_state *state = platform_get_drvdata(pdev);
260 +       input_unregister_handler(&state->input_handler);
261 +       kfree(state);
262 +       return 0;
263 +}
264 +
265 +
266 +struct platform_driver keyreset_driver = {
267 +       .driver.name = KEYRESET_NAME,
268 +       .probe = keyreset_probe,
269 +       .remove = keyreset_remove,
270 +};
271 +
272 +static int __init keyreset_init(void)
273 +{
274 +       return platform_driver_register(&keyreset_driver);
275 +}
276 +
277 +static void __exit keyreset_exit(void)
278 +{
279 +       return platform_driver_unregister(&keyreset_driver);
280 +}
281 +
282 +module_init(keyreset_init);
283 +module_exit(keyreset_exit);
284 --- /dev/null
285 +++ b/include/linux/keyreset.h
286 @@ -0,0 +1,27 @@
287 +/*
288 + * include/linux/keyreset.h - platform data structure for resetkeys driver
289 + *
290 + * Copyright (C) 2008 Google, Inc.
291 + *
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.
295 + *
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.
300 + *
301 + */
302 +
303 +#ifndef _LINUX_KEYRESET_H
304 +#define _LINUX_KEYRESET_H
305 +
306 +#define KEYRESET_NAME "keyreset"
307 +
308 +struct keyreset_platform_data {
309 +       int *keys_up;
310 +       int keys_down[]; /* 0 terminated */
311 +};
312 +
313 +#endif /* _LINUX_KEYRESET_H */