2 * drivers/input/misc/keychord.c
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
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.
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.
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
24 #include <linux/miscdevice.h>
25 #include <linux/keychord.h>
26 #include <linux/sched.h>
28 #define KEYCHORD_NAME "keychord"
29 #define BUFFER_SIZE 16
31 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
32 MODULE_DESCRIPTION("Key chord input driver");
33 MODULE_SUPPORTED_DEVICE("keychord");
34 MODULE_LICENSE("GPL");
36 #define NEXT_KEYCHORD(kc) ((struct input_keychord *) \
37 ((char *)kc + sizeof(struct input_keychord) + \
38 kc->count * sizeof(kc->keycodes[0])))
40 struct keychord_device {
41 struct input_handler input_handler;
44 /* list of keychords to monitor */
45 struct input_keychord *keychords;
48 /* bitmask of keys contained in our keychords */
49 unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
50 /* current state of the keys */
51 unsigned long keystate[BITS_TO_LONGS(KEY_CNT)];
52 /* number of keys that are currently pressed */
55 /* second input_device_id is needed for null termination */
56 struct input_device_id device_ids[2];
59 wait_queue_head_t waitq;
62 __u16 buff[BUFFER_SIZE];
65 static int check_keychord(struct keychord_device *kdev,
66 struct input_keychord *keychord)
70 if (keychord->count != kdev->key_down)
73 for (i = 0; i < keychord->count; i++) {
74 if (!test_bit(keychord->keycodes[i], kdev->keystate))
82 static void keychord_event(struct input_handle *handle, unsigned int type,
83 unsigned int code, int value)
85 struct keychord_device *kdev = handle->private;
86 struct input_keychord *keychord;
90 if (type != EV_KEY || code >= KEY_MAX)
93 spin_lock_irqsave(&kdev->lock, flags);
94 /* do nothing if key state did not change */
95 if (!test_bit(code, kdev->keystate) == !value)
97 __change_bit(code, kdev->keystate);
103 /* don't notify on key up */
106 /* ignore this event if it is not one of the keys we are monitoring */
107 if (!test_bit(code, kdev->keybit))
110 keychord = kdev->keychords;
114 /* check to see if the keyboard state matches any keychords */
115 for (i = 0; i < kdev->keychord_count; i++) {
116 if (check_keychord(kdev, keychord)) {
117 kdev->buff[kdev->head] = keychord->id;
118 kdev->head = (kdev->head + 1) % BUFFER_SIZE;
122 /* skip to next keychord */
123 keychord = NEXT_KEYCHORD(keychord);
127 spin_unlock_irqrestore(&kdev->lock, flags);
130 wake_up_interruptible(&kdev->waitq);
133 static int keychord_connect(struct input_handler *handler,
134 struct input_dev *dev,
135 const struct input_device_id *id)
138 struct input_handle *handle;
139 struct keychord_device *kdev =
140 container_of(handler, struct keychord_device, input_handler);
143 * ignore this input device if it does not contain any keycodes
144 * that we are monitoring
146 for (i = 0; i < KEY_MAX; i++) {
147 if (test_bit(i, kdev->keybit) && test_bit(i, dev->keybit))
153 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
158 handle->handler = handler;
159 handle->name = KEYCHORD_NAME;
160 handle->private = kdev;
162 ret = input_register_handle(handle);
164 goto err_input_register_handle;
166 ret = input_open_device(handle);
168 goto err_input_open_device;
170 pr_info("keychord: using input dev %s for fevent\n", dev->name);
174 err_input_open_device:
175 input_unregister_handle(handle);
176 err_input_register_handle:
181 static void keychord_disconnect(struct input_handle *handle)
183 input_close_device(handle);
184 input_unregister_handle(handle);
189 * keychord_read is used to read keychord events from the driver
191 static ssize_t keychord_read(struct file *file, char __user *buffer,
192 size_t count, loff_t *ppos)
194 struct keychord_device *kdev = file->private_data;
199 if (count < sizeof(id))
203 if (kdev->head == kdev->tail && (file->f_flags & O_NONBLOCK))
206 retval = wait_event_interruptible(kdev->waitq,
207 kdev->head != kdev->tail);
211 spin_lock_irqsave(&kdev->lock, flags);
212 /* pop a keychord ID off the queue */
213 id = kdev->buff[kdev->tail];
214 kdev->tail = (kdev->tail + 1) % BUFFER_SIZE;
215 spin_unlock_irqrestore(&kdev->lock, flags);
217 if (copy_to_user(buffer, &id, count))
224 * keychord_write is used to configure the driver
226 static ssize_t keychord_write(struct file *file, const char __user *buffer,
227 size_t count, loff_t *ppos)
229 struct keychord_device *kdev = file->private_data;
230 struct input_keychord *keychords = 0;
231 struct input_keychord *keychord, *next, *end;
235 if (count < sizeof(struct input_keychord))
237 keychords = kzalloc(count, GFP_KERNEL);
241 /* read list of keychords from userspace */
242 if (copy_from_user(keychords, buffer, count)) {
247 /* unregister handler before changing configuration */
248 if (kdev->registered) {
249 input_unregister_handler(&kdev->input_handler);
250 kdev->registered = 0;
253 spin_lock_irqsave(&kdev->lock, flags);
254 /* clear any existing configuration */
255 kfree(kdev->keychords);
257 kdev->keychord_count = 0;
259 memset(kdev->keybit, 0, sizeof(kdev->keybit));
260 memset(kdev->keystate, 0, sizeof(kdev->keystate));
261 kdev->head = kdev->tail = 0;
263 keychord = keychords;
264 end = (struct input_keychord *)((char *)keychord + count);
266 while (keychord < end) {
267 next = NEXT_KEYCHORD(keychord);
268 if (keychord->count <= 0 || next > end) {
269 pr_err("keychord: invalid keycode count %d\n",
271 goto err_unlock_return;
273 if (keychord->version != KEYCHORD_VERSION) {
274 pr_err("keychord: unsupported version %d\n",
276 goto err_unlock_return;
279 /* keep track of the keys we are monitoring in keybit */
280 for (i = 0; i < keychord->count; i++) {
281 key = keychord->keycodes[i];
282 if (key < 0 || key >= KEY_CNT) {
283 pr_err("keychord: keycode %d out of range\n",
285 goto err_unlock_return;
287 __set_bit(key, kdev->keybit);
290 kdev->keychord_count++;
294 kdev->keychords = keychords;
295 spin_unlock_irqrestore(&kdev->lock, flags);
297 ret = input_register_handler(&kdev->input_handler);
303 kdev->registered = 1;
308 spin_unlock_irqrestore(&kdev->lock, flags);
313 static unsigned int keychord_poll(struct file *file, poll_table *wait)
315 struct keychord_device *kdev = file->private_data;
317 poll_wait(file, &kdev->waitq, wait);
319 if (kdev->head != kdev->tail)
320 return POLLIN | POLLRDNORM;
325 static int keychord_open(struct inode *inode, struct file *file)
327 struct keychord_device *kdev;
329 kdev = kzalloc(sizeof(struct keychord_device), GFP_KERNEL);
333 spin_lock_init(&kdev->lock);
334 init_waitqueue_head(&kdev->waitq);
336 kdev->input_handler.event = keychord_event;
337 kdev->input_handler.connect = keychord_connect;
338 kdev->input_handler.disconnect = keychord_disconnect;
339 kdev->input_handler.name = KEYCHORD_NAME;
340 kdev->input_handler.id_table = kdev->device_ids;
342 kdev->device_ids[0].flags = INPUT_DEVICE_ID_MATCH_EVBIT;
343 __set_bit(EV_KEY, kdev->device_ids[0].evbit);
345 file->private_data = kdev;
350 static int keychord_release(struct inode *inode, struct file *file)
352 struct keychord_device *kdev = file->private_data;
354 if (kdev->registered)
355 input_unregister_handler(&kdev->input_handler);
361 static const struct file_operations keychord_fops = {
362 .owner = THIS_MODULE,
363 .open = keychord_open,
364 .release = keychord_release,
365 .read = keychord_read,
366 .write = keychord_write,
367 .poll = keychord_poll,
370 static struct miscdevice keychord_misc = {
371 .fops = &keychord_fops,
372 .name = KEYCHORD_NAME,
373 .minor = MISC_DYNAMIC_MINOR,
376 static int __init keychord_init(void)
378 return misc_register(&keychord_misc);
381 static void __exit keychord_exit(void)
383 misc_deregister(&keychord_misc);
386 module_init(keychord_init);
387 module_exit(keychord_exit);