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 pr_info("keychord: got keychord id %d. Any tasks: %d\n",
132 !list_empty_careful(&kdev->waitq.task_list));
133 wake_up_interruptible(&kdev->waitq);
137 static int keychord_connect(struct input_handler *handler,
138 struct input_dev *dev,
139 const struct input_device_id *id)
142 struct input_handle *handle;
143 struct keychord_device *kdev =
144 container_of(handler, struct keychord_device, input_handler);
147 * ignore this input device if it does not contain any keycodes
148 * that we are monitoring
150 for (i = 0; i < KEY_MAX; i++) {
151 if (test_bit(i, kdev->keybit) && test_bit(i, dev->keybit))
157 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
162 handle->handler = handler;
163 handle->name = KEYCHORD_NAME;
164 handle->private = kdev;
166 ret = input_register_handle(handle);
168 goto err_input_register_handle;
170 ret = input_open_device(handle);
172 goto err_input_open_device;
174 pr_info("keychord: using input dev %s for fevent\n", dev->name);
178 err_input_open_device:
179 input_unregister_handle(handle);
180 err_input_register_handle:
185 static void keychord_disconnect(struct input_handle *handle)
187 input_close_device(handle);
188 input_unregister_handle(handle);
193 * keychord_read is used to read keychord events from the driver
195 static ssize_t keychord_read(struct file *file, char __user *buffer,
196 size_t count, loff_t *ppos)
198 struct keychord_device *kdev = file->private_data;
203 if (count < sizeof(id))
207 if (kdev->head == kdev->tail && (file->f_flags & O_NONBLOCK))
210 retval = wait_event_interruptible(kdev->waitq,
211 kdev->head != kdev->tail);
215 spin_lock_irqsave(&kdev->lock, flags);
216 /* pop a keychord ID off the queue */
217 id = kdev->buff[kdev->tail];
218 kdev->tail = (kdev->tail + 1) % BUFFER_SIZE;
219 spin_unlock_irqrestore(&kdev->lock, flags);
221 if (copy_to_user(buffer, &id, count))
228 * keychord_write is used to configure the driver
230 static ssize_t keychord_write(struct file *file, const char __user *buffer,
231 size_t count, loff_t *ppos)
233 struct keychord_device *kdev = file->private_data;
234 struct input_keychord *keychords = 0;
235 struct input_keychord *keychord, *next, *end;
239 if (count < sizeof(struct input_keychord))
241 keychords = kzalloc(count, GFP_KERNEL);
245 /* read list of keychords from userspace */
246 if (copy_from_user(keychords, buffer, count)) {
251 /* unregister handler before changing configuration */
252 if (kdev->registered) {
253 input_unregister_handler(&kdev->input_handler);
254 kdev->registered = 0;
257 spin_lock_irqsave(&kdev->lock, flags);
258 /* clear any existing configuration */
259 kfree(kdev->keychords);
261 kdev->keychord_count = 0;
263 memset(kdev->keybit, 0, sizeof(kdev->keybit));
264 memset(kdev->keystate, 0, sizeof(kdev->keystate));
265 kdev->head = kdev->tail = 0;
267 keychord = keychords;
268 end = (struct input_keychord *)((char *)keychord + count);
270 while (keychord < end) {
271 next = NEXT_KEYCHORD(keychord);
272 if (keychord->count <= 0 || next > end) {
273 pr_err("keychord: invalid keycode count %d\n",
275 goto err_unlock_return;
277 if (keychord->version != KEYCHORD_VERSION) {
278 pr_err("keychord: unsupported version %d\n",
280 goto err_unlock_return;
283 /* keep track of the keys we are monitoring in keybit */
284 for (i = 0; i < keychord->count; i++) {
285 key = keychord->keycodes[i];
286 if (key < 0 || key >= KEY_CNT) {
287 pr_err("keychord: keycode %d out of range\n",
289 goto err_unlock_return;
291 __set_bit(key, kdev->keybit);
294 kdev->keychord_count++;
298 kdev->keychords = keychords;
299 spin_unlock_irqrestore(&kdev->lock, flags);
301 ret = input_register_handler(&kdev->input_handler);
307 kdev->registered = 1;
312 spin_unlock_irqrestore(&kdev->lock, flags);
317 static unsigned int keychord_poll(struct file *file, poll_table *wait)
319 struct keychord_device *kdev = file->private_data;
321 poll_wait(file, &kdev->waitq, wait);
323 if (kdev->head != kdev->tail)
324 return POLLIN | POLLRDNORM;
329 static int keychord_open(struct inode *inode, struct file *file)
331 struct keychord_device *kdev;
333 kdev = kzalloc(sizeof(struct keychord_device), GFP_KERNEL);
337 spin_lock_init(&kdev->lock);
338 init_waitqueue_head(&kdev->waitq);
340 kdev->input_handler.event = keychord_event;
341 kdev->input_handler.connect = keychord_connect;
342 kdev->input_handler.disconnect = keychord_disconnect;
343 kdev->input_handler.name = KEYCHORD_NAME;
344 kdev->input_handler.id_table = kdev->device_ids;
346 kdev->device_ids[0].flags = INPUT_DEVICE_ID_MATCH_EVBIT;
347 __set_bit(EV_KEY, kdev->device_ids[0].evbit);
349 file->private_data = kdev;
354 static int keychord_release(struct inode *inode, struct file *file)
356 struct keychord_device *kdev = file->private_data;
358 if (kdev->registered)
359 input_unregister_handler(&kdev->input_handler);
365 static const struct file_operations keychord_fops = {
366 .owner = THIS_MODULE,
367 .open = keychord_open,
368 .release = keychord_release,
369 .read = keychord_read,
370 .write = keychord_write,
371 .poll = keychord_poll,
374 static struct miscdevice keychord_misc = {
375 .fops = &keychord_fops,
376 .name = KEYCHORD_NAME,
377 .minor = MISC_DYNAMIC_MINOR,
380 static int __init keychord_init(void)
382 return misc_register(&keychord_misc);
385 static void __exit keychord_exit(void)
387 misc_deregister(&keychord_misc);
390 module_init(keychord_init);
391 module_exit(keychord_exit);