1 /*
2 * drivers/input/misc/keychord.c
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/smp_lock.h>
23 #include <linux/fs.h>
24 #include <linux/miscdevice.h>
25 #include <linux/keychord.h>
26
27 #define KEYCHORD_NAME "keychord"
28 #define BUFFER_SIZE 16
29
30 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
31 MODULE_DESCRIPTION("Key chord input driver");
32 MODULE_SUPPORTED_DEVICE("keychord");
33 MODULE_LICENSE("GPL");
34
35 #define NEXT_KEYCHORD(kc) ((struct input_keychord *) \
36 ((char *)kc + sizeof(struct input_keychord) + \
37 kc->count * sizeof(kc->keycodes[0])))
38
39 struct keychord_device {
40 struct input_handler input_handler;
41 int registered;
42
43 /* list of keychords to monitor */
44 struct input_keychord *keychords;
45 int keychord_count;
46
47 /* bitmask of keys contained in our keychords */
48 unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
49 /* current state of the keys */
50 unsigned long keystate[BITS_TO_LONGS(KEY_CNT)];
51 /* number of keys that are currently pressed */
52 int key_down;
53
54 /* second input_device_id is needed for null termination */
55 struct input_device_id device_ids[2];
56
57 spinlock_t lock;
58 wait_queue_head_t waitq;
59 unsigned char head;
60 unsigned char tail;
61 __u16 buff[BUFFER_SIZE];
62 };
63
check_keychord(struct keychord_device * kdev,struct input_keychord * keychord)64 static int check_keychord(struct keychord_device *kdev,
65 struct input_keychord *keychord)
66 {
67 int i;
68
69 if (keychord->count != kdev->key_down)
70 return 0;
71
72 for (i = 0; i < keychord->count; i++) {
73 if (!test_bit(keychord->keycodes[i], kdev->keystate))
74 return 0;
75 }
76
77 /* we have a match */
78 return 1;
79 }
80
keychord_event(struct input_handle * handle,unsigned int type,unsigned int code,int value)81 static void keychord_event(struct input_handle *handle, unsigned int type,
82 unsigned int code, int value)
83 {
84 struct keychord_device *kdev = handle->private;
85 struct input_keychord *keychord;
86 unsigned long flags;
87 int i, got_chord = 0;
88
89 if (type != EV_KEY || code >= KEY_MAX)
90 return;
91
92 spin_lock_irqsave(&kdev->lock, flags);
93 /* do nothing if key state did not change */
94 if (!test_bit(code, kdev->keystate) == !value)
95 goto done;
96 __change_bit(code, kdev->keystate);
97 if (value)
98 kdev->key_down++;
99 else
100 kdev->key_down--;
101
102 /* don't notify on key up */
103 if (!value)
104 goto done;
105 /* ignore this event if it is not one of the keys we are monitoring */
106 if (!test_bit(code, kdev->keybit))
107 goto done;
108
109 keychord = kdev->keychords;
110 if (!keychord)
111 goto done;
112
113 /* check to see if the keyboard state matches any keychords */
114 for (i = 0; i < kdev->keychord_count; i++) {
115 if (check_keychord(kdev, keychord)) {
116 kdev->buff[kdev->head] = keychord->id;
117 kdev->head = (kdev->head + 1) % BUFFER_SIZE;
118 got_chord = 1;
119 break;
120 }
121 /* skip to next keychord */
122 keychord = NEXT_KEYCHORD(keychord);
123 }
124
125 done:
126 spin_unlock_irqrestore(&kdev->lock, flags);
127
128 if (got_chord)
129 wake_up_interruptible(&kdev->waitq);
130 }
131
keychord_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)132 static int keychord_connect(struct input_handler *handler,
133 struct input_dev *dev,
134 const struct input_device_id *id)
135 {
136 int i, ret;
137 struct input_handle *handle;
138 struct keychord_device *kdev =
139 container_of(handler, struct keychord_device, input_handler);
140
141 /*
142 * ignore this input device if it does not contain any keycodes
143 * that we are monitoring
144 */
145 for (i = 0; i < KEY_MAX; i++) {
146 if (test_bit(i, kdev->keybit) && test_bit(i, dev->keybit))
147 break;
148 }
149 if (i == KEY_MAX)
150 return -ENODEV;
151
152 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
153 if (!handle)
154 return -ENOMEM;
155
156 handle->dev = dev;
157 handle->handler = handler;
158 handle->name = KEYCHORD_NAME;
159 handle->private = kdev;
160
161 ret = input_register_handle(handle);
162 if (ret)
163 goto err_input_register_handle;
164
165 ret = input_open_device(handle);
166 if (ret)
167 goto err_input_open_device;
168
169 pr_info("keychord: using input dev %s for fevent\n", dev->name);
170
171 return 0;
172
173 err_input_open_device:
174 input_unregister_handle(handle);
175 err_input_register_handle:
176 kfree(handle);
177 return ret;
178 }
179
keychord_disconnect(struct input_handle * handle)180 static void keychord_disconnect(struct input_handle *handle)
181 {
182 input_close_device(handle);
183 input_unregister_handle(handle);
184 kfree(handle);
185 }
186
187 /*
188 * keychord_read is used to read keychord events from the driver
189 */
keychord_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)190 static ssize_t keychord_read(struct file *file, char __user *buffer,
191 size_t count, loff_t *ppos)
192 {
193 struct keychord_device *kdev = file->private_data;
194 __u16 id;
195 int retval;
196 unsigned long flags;
197
198 if (count < sizeof(id))
199 return -EINVAL;
200 count = sizeof(id);
201
202 if (kdev->head == kdev->tail && (file->f_flags & O_NONBLOCK))
203 return -EAGAIN;
204
205 retval = wait_event_interruptible(kdev->waitq,
206 kdev->head != kdev->tail);
207 if (retval)
208 return retval;
209
210 spin_lock_irqsave(&kdev->lock, flags);
211 /* pop a keychord ID off the queue */
212 id = kdev->buff[kdev->tail];
213 kdev->tail = (kdev->tail + 1) % BUFFER_SIZE;
214 spin_unlock_irqrestore(&kdev->lock, flags);
215
216 if (copy_to_user(buffer, &id, count))
217 return -EFAULT;
218
219 return count;
220 }
221
222 /*
223 * keychord_write is used to configure the driver
224 */
keychord_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)225 static ssize_t keychord_write(struct file *file, const char __user *buffer,
226 size_t count, loff_t *ppos)
227 {
228 struct keychord_device *kdev = file->private_data;
229 struct input_keychord *keychords = 0;
230 struct input_keychord *keychord, *next, *end;
231 int ret, i, key;
232 unsigned long flags;
233
234 if (count < sizeof(struct input_keychord))
235 return -EINVAL;
236 keychords = kzalloc(count, GFP_KERNEL);
237 if (!keychords)
238 return -ENOMEM;
239
240 /* read list of keychords from userspace */
241 if (copy_from_user(keychords, buffer, count)) {
242 kfree(keychords);
243 return -EFAULT;
244 }
245
246 /* unregister handler before changing configuration */
247 if (kdev->registered) {
248 input_unregister_handler(&kdev->input_handler);
249 kdev->registered = 0;
250 }
251
252 spin_lock_irqsave(&kdev->lock, flags);
253 /* clear any existing configuration */
254 kfree(kdev->keychords);
255 kdev->keychords = 0;
256 kdev->keychord_count = 0;
257 kdev->key_down = 0;
258 memset(kdev->keybit, 0, sizeof(kdev->keybit));
259 memset(kdev->keystate, 0, sizeof(kdev->keystate));
260 kdev->head = kdev->tail = 0;
261
262 keychord = keychords;
263 end = (struct input_keychord *)((char *)keychord + count);
264
265 while (keychord < end) {
266 next = NEXT_KEYCHORD(keychord);
267 if (keychord->count <= 0 || next > end) {
268 pr_err("keychord: invalid keycode count %d\n",
269 keychord->count);
270 goto err_unlock_return;
271 }
272 if (keychord->version != KEYCHORD_VERSION) {
273 pr_err("keychord: unsupported version %d\n",
274 keychord->version);
275 goto err_unlock_return;
276 }
277
278 /* keep track of the keys we are monitoring in keybit */
279 for (i = 0; i < keychord->count; i++) {
280 key = keychord->keycodes[i];
281 if (key < 0 || key >= KEY_CNT) {
282 pr_err("keychord: keycode %d out of range\n",
283 key);
284 goto err_unlock_return;
285 }
286 __set_bit(key, kdev->keybit);
287 }
288
289 kdev->keychord_count++;
290 keychord = next;
291 }
292
293 kdev->keychords = keychords;
294 spin_unlock_irqrestore(&kdev->lock, flags);
295
296 ret = input_register_handler(&kdev->input_handler);
297 if (ret) {
298 kfree(keychords);
299 kdev->keychords = 0;
300 return ret;
301 }
302 kdev->registered = 1;
303
304 return count;
305
306 err_unlock_return:
307 spin_unlock_irqrestore(&kdev->lock, flags);
308 kfree(keychords);
309 return -EINVAL;
310 }
311
keychord_poll(struct file * file,poll_table * wait)312 static unsigned int keychord_poll(struct file *file, poll_table *wait)
313 {
314 struct keychord_device *kdev = file->private_data;
315
316 poll_wait(file, &kdev->waitq, wait);
317
318 if (kdev->head != kdev->tail)
319 return POLLIN | POLLRDNORM;
320
321 return 0;
322 }
323
keychord_open(struct inode * inode,struct file * file)324 static int keychord_open(struct inode *inode, struct file *file)
325 {
326 struct keychord_device *kdev;
327
328 kdev = kzalloc(sizeof(struct keychord_device), GFP_KERNEL);
329 if (!kdev)
330 return -ENOMEM;
331
332 spin_lock_init(&kdev->lock);
333 init_waitqueue_head(&kdev->waitq);
334
335 kdev->input_handler.event = keychord_event;
336 kdev->input_handler.connect = keychord_connect;
337 kdev->input_handler.disconnect = keychord_disconnect;
338 kdev->input_handler.name = KEYCHORD_NAME;
339 kdev->input_handler.id_table = kdev->device_ids;
340
341 kdev->device_ids[0].flags = INPUT_DEVICE_ID_MATCH_EVBIT;
342 __set_bit(EV_KEY, kdev->device_ids[0].evbit);
343
344 file->private_data = kdev;
345
346 return 0;
347 }
348
keychord_release(struct inode * inode,struct file * file)349 static int keychord_release(struct inode *inode, struct file *file)
350 {
351 struct keychord_device *kdev = file->private_data;
352
353 if (kdev->registered)
354 input_unregister_handler(&kdev->input_handler);
355 kfree(kdev);
356
357 return 0;
358 }
359
360 static const struct file_operations keychord_fops = {
361 .owner = THIS_MODULE,
362 .open = keychord_open,
363 .release = keychord_release,
364 .read = keychord_read,
365 .write = keychord_write,
366 .poll = keychord_poll,
367 };
368
369 static struct miscdevice keychord_misc = {
370 .fops = &keychord_fops,
371 .name = KEYCHORD_NAME,
372 .minor = MISC_DYNAMIC_MINOR,
373 };
374
keychord_init(void)375 static int __init keychord_init(void)
376 {
377 return misc_register(&keychord_misc);
378 }
379
keychord_exit(void)380 static void __exit keychord_exit(void)
381 {
382 misc_deregister(&keychord_misc);
383 }
384
385 module_init(keychord_init);
386 module_exit(keychord_exit);
387