• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* drivers/input/keyreset.c
2  *
3  * Copyright (C) 2008 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 
16 #include <linux/input.h>
17 #include <linux/keyreset.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/reboot.h>
21 #include <linux/sched.h>
22 #include <linux/syscalls.h>
23 
24 
25 struct keyreset_state {
26 	struct input_handler input_handler;
27 	unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
28 	unsigned long upbit[BITS_TO_LONGS(KEY_CNT)];
29 	unsigned long key[BITS_TO_LONGS(KEY_CNT)];
30 	spinlock_t lock;
31 	int key_down_target;
32 	int key_down;
33 	int key_up;
34 	int restart_disabled;
35 };
36 
37 int restart_requested;
deferred_restart(struct work_struct * dummy)38 static void deferred_restart(struct work_struct *dummy)
39 {
40 	restart_requested = 2;
41 	sys_sync();
42 	restart_requested = 3;
43 	kernel_restart(NULL);
44 }
45 static DECLARE_WORK(restart_work, deferred_restart);
46 
keyreset_event(struct input_handle * handle,unsigned int type,unsigned int code,int value)47 static void keyreset_event(struct input_handle *handle, unsigned int type,
48 			   unsigned int code, int value)
49 {
50 	unsigned long flags;
51 	struct keyreset_state *state = handle->private;
52 
53 	if (type != EV_KEY)
54 		return;
55 
56 	if (code >= KEY_MAX)
57 		return;
58 
59 	if (!test_bit(code, state->keybit))
60 		return;
61 
62 	spin_lock_irqsave(&state->lock, flags);
63 	if (!test_bit(code, state->key) == !value)
64 		goto done;
65 	__change_bit(code, state->key);
66 	if (test_bit(code, state->upbit)) {
67 		if (value) {
68 			state->restart_disabled = 1;
69 			state->key_up++;
70 		} else
71 			state->key_up--;
72 	} else {
73 		if (value)
74 			state->key_down++;
75 		else
76 			state->key_down--;
77 	}
78 	if (state->key_down == 0 && state->key_up == 0)
79 		state->restart_disabled = 0;
80 
81 	pr_debug("reset key changed %d %d new state %d-%d-%d\n", code, value,
82 		 state->key_down, state->key_up, state->restart_disabled);
83 
84 	if (value && !state->restart_disabled &&
85 	    state->key_down == state->key_down_target) {
86 		state->restart_disabled = 1;
87 		if (restart_requested)
88 			panic("keyboard reset failed, %d", restart_requested);
89 		pr_info("keyboard reset\n");
90 		schedule_work(&restart_work);
91 		restart_requested = 1;
92 	}
93 done:
94 	spin_unlock_irqrestore(&state->lock, flags);
95 }
96 
keyreset_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)97 static int keyreset_connect(struct input_handler *handler,
98 					  struct input_dev *dev,
99 					  const struct input_device_id *id)
100 {
101 	int i;
102 	int ret;
103 	struct input_handle *handle;
104 	struct keyreset_state *state =
105 		container_of(handler, struct keyreset_state, input_handler);
106 
107 	for (i = 0; i < KEY_MAX; i++) {
108 		if (test_bit(i, state->keybit) && test_bit(i, dev->keybit))
109 			break;
110 	}
111 	if (i == KEY_MAX)
112 		return -ENODEV;
113 
114 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
115 	if (!handle)
116 		return -ENOMEM;
117 
118 	handle->dev = dev;
119 	handle->handler = handler;
120 	handle->name = "keyreset";
121 	handle->private = state;
122 
123 	ret = input_register_handle(handle);
124 	if (ret)
125 		goto err_input_register_handle;
126 
127 	ret = input_open_device(handle);
128 	if (ret)
129 		goto err_input_open_device;
130 
131 	pr_info("using input dev %s for key reset\n", dev->name);
132 
133 	return 0;
134 
135 err_input_open_device:
136 	input_unregister_handle(handle);
137 err_input_register_handle:
138 	kfree(handle);
139 	return ret;
140 }
141 
keyreset_disconnect(struct input_handle * handle)142 static void keyreset_disconnect(struct input_handle *handle)
143 {
144 	input_close_device(handle);
145 	input_unregister_handle(handle);
146 	kfree(handle);
147 }
148 
149 static const struct input_device_id keyreset_ids[] = {
150 	{
151 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
152 		.evbit = { BIT_MASK(EV_KEY) },
153 	},
154 	{ },
155 };
156 MODULE_DEVICE_TABLE(input, keyreset_ids);
157 
keyreset_probe(struct platform_device * pdev)158 static int keyreset_probe(struct platform_device *pdev)
159 {
160 	int ret;
161 	int key, *keyp;
162 	struct keyreset_state *state;
163 	struct keyreset_platform_data *pdata = pdev->dev.platform_data;
164 
165 	if (!pdata)
166 		return -EINVAL;
167 
168 	state = kzalloc(sizeof(*state), GFP_KERNEL);
169 	if (!state)
170 		return -ENOMEM;
171 
172 	spin_lock_init(&state->lock);
173 	keyp = pdata->keys_down;
174 	while ((key = *keyp++)) {
175 		if (key >= KEY_MAX)
176 			continue;
177 		state->key_down_target++;
178 		__set_bit(key, state->keybit);
179 	}
180 	if (pdata->keys_up) {
181 		keyp = pdata->keys_up;
182 		while ((key = *keyp++)) {
183 			if (key >= KEY_MAX)
184 				continue;
185 			__set_bit(key, state->keybit);
186 			__set_bit(key, state->upbit);
187 		}
188 	}
189 	state->input_handler.event = keyreset_event;
190 	state->input_handler.connect = keyreset_connect;
191 	state->input_handler.disconnect = keyreset_disconnect;
192 	state->input_handler.name = KEYRESET_NAME;
193 	state->input_handler.id_table = keyreset_ids;
194 	ret = input_register_handler(&state->input_handler);
195 	if (ret) {
196 		kfree(state);
197 		return ret;
198 	}
199 	platform_set_drvdata(pdev, state);
200 	return 0;
201 }
202 
keyreset_remove(struct platform_device * pdev)203 int keyreset_remove(struct platform_device *pdev)
204 {
205 	struct keyreset_state *state = platform_get_drvdata(pdev);
206 	input_unregister_handler(&state->input_handler);
207 	kfree(state);
208 	return 0;
209 }
210 
211 
212 struct platform_driver keyreset_driver = {
213 	.driver.name = KEYRESET_NAME,
214 	.probe = keyreset_probe,
215 	.remove = keyreset_remove,
216 };
217 
keyreset_init(void)218 static int __init keyreset_init(void)
219 {
220 	return platform_driver_register(&keyreset_driver);
221 }
222 
keyreset_exit(void)223 static void __exit keyreset_exit(void)
224 {
225 	return platform_driver_unregister(&keyreset_driver);
226 }
227 
228 module_init(keyreset_init);
229 module_exit(keyreset_exit);
230