blob: 3385dd03abfc17be819ce992eb8bc314eb64abc5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The input core
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 */
12
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/smp_lock.h>
16#include <linux/input.h>
17#include <linux/module.h>
18#include <linux/random.h>
19#include <linux/major.h>
20#include <linux/proc_fs.h>
21#include <linux/kobject_uevent.h>
22#include <linux/interrupt.h>
23#include <linux/poll.h>
24#include <linux/device.h>
25#include <linux/devfs_fs_kernel.h>
26
27MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28MODULE_DESCRIPTION("Input core");
29MODULE_LICENSE("GPL");
30
31EXPORT_SYMBOL(input_register_device);
32EXPORT_SYMBOL(input_unregister_device);
33EXPORT_SYMBOL(input_register_handler);
34EXPORT_SYMBOL(input_unregister_handler);
35EXPORT_SYMBOL(input_grab_device);
36EXPORT_SYMBOL(input_release_device);
37EXPORT_SYMBOL(input_open_device);
38EXPORT_SYMBOL(input_close_device);
39EXPORT_SYMBOL(input_accept_process);
40EXPORT_SYMBOL(input_flush_device);
41EXPORT_SYMBOL(input_event);
42EXPORT_SYMBOL(input_class);
43
44#define INPUT_DEVICES 256
45
46static LIST_HEAD(input_dev_list);
47static LIST_HEAD(input_handler_list);
48
49static struct input_handler *input_table[8];
50
51#ifdef CONFIG_PROC_FS
52static struct proc_dir_entry *proc_bus_input_dir;
53static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
54static int input_devices_state;
55#endif
56
57void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
58{
59 struct input_handle *handle;
60
61 if (type > EV_MAX || !test_bit(type, dev->evbit))
62 return;
63
64 add_input_randomness(type, code, value);
65
66 switch (type) {
67
68 case EV_SYN:
69 switch (code) {
70 case SYN_CONFIG:
71 if (dev->event) dev->event(dev, type, code, value);
72 break;
73
74 case SYN_REPORT:
75 if (dev->sync) return;
76 dev->sync = 1;
77 break;
78 }
79 break;
80
81 case EV_KEY:
82
83 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
84 return;
85
86 if (value == 2)
87 break;
88
89 change_bit(code, dev->key);
90
91 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
92 dev->repeat_key = code;
93 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
94 }
95
96 break;
97
98 case EV_ABS:
99
100 if (code > ABS_MAX || !test_bit(code, dev->absbit))
101 return;
102
103 if (dev->absfuzz[code]) {
104 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
106 return;
107
108 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
109 (value < dev->abs[code] + dev->absfuzz[code]))
110 value = (dev->abs[code] * 3 + value) >> 2;
111
112 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
113 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
114 value = (dev->abs[code] + value) >> 1;
115 }
116
117 if (dev->abs[code] == value)
118 return;
119
120 dev->abs[code] = value;
121 break;
122
123 case EV_REL:
124
125 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
126 return;
127
128 break;
129
130 case EV_MSC:
131
132 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
133 return;
134
135 if (dev->event) dev->event(dev, type, code, value);
136
137 break;
138
139 case EV_LED:
140
141 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
142 return;
143
144 change_bit(code, dev->led);
145 if (dev->event) dev->event(dev, type, code, value);
146
147 break;
148
149 case EV_SND:
150
151 if (code > SND_MAX || !test_bit(code, dev->sndbit))
152 return;
153
154 if (dev->event) dev->event(dev, type, code, value);
155
156 break;
157
158 case EV_REP:
159
160 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
161
162 dev->rep[code] = value;
163 if (dev->event) dev->event(dev, type, code, value);
164
165 break;
166
167 case EV_FF:
168 if (dev->event) dev->event(dev, type, code, value);
169 break;
170 }
171
172 if (type != EV_SYN)
173 dev->sync = 0;
174
175 if (dev->grab)
176 dev->grab->handler->event(dev->grab, type, code, value);
177 else
178 list_for_each_entry(handle, &dev->h_list, d_node)
179 if (handle->open)
180 handle->handler->event(handle, type, code, value);
181}
182
183static void input_repeat_key(unsigned long data)
184{
185 struct input_dev *dev = (void *) data;
186
187 if (!test_bit(dev->repeat_key, dev->key))
188 return;
189
190 input_event(dev, EV_KEY, dev->repeat_key, 2);
191 input_sync(dev);
192
193 if (dev->rep[REP_PERIOD])
194 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
195}
196
197int input_accept_process(struct input_handle *handle, struct file *file)
198{
199 if (handle->dev->accept)
200 return handle->dev->accept(handle->dev, file);
201
202 return 0;
203}
204
205int input_grab_device(struct input_handle *handle)
206{
207 if (handle->dev->grab)
208 return -EBUSY;
209
210 handle->dev->grab = handle;
211 return 0;
212}
213
214void input_release_device(struct input_handle *handle)
215{
216 if (handle->dev->grab == handle)
217 handle->dev->grab = NULL;
218}
219
220int input_open_device(struct input_handle *handle)
221{
222 handle->open++;
223 if (handle->dev->open)
224 return handle->dev->open(handle->dev);
225 return 0;
226}
227
228int input_flush_device(struct input_handle* handle, struct file* file)
229{
230 if (handle->dev->flush)
231 return handle->dev->flush(handle->dev, file);
232
233 return 0;
234}
235
236void input_close_device(struct input_handle *handle)
237{
238 input_release_device(handle);
239 if (handle->dev->close)
240 handle->dev->close(handle->dev);
241 handle->open--;
242}
243
244static void input_link_handle(struct input_handle *handle)
245{
246 list_add_tail(&handle->d_node, &handle->dev->h_list);
247 list_add_tail(&handle->h_node, &handle->handler->h_list);
248}
249
250#define MATCH_BIT(bit, max) \
251 for (i = 0; i < NBITS(max); i++) \
252 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
253 break; \
254 if (i != NBITS(max)) \
255 continue;
256
257static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
258{
259 int i;
260
261 for (; id->flags || id->driver_info; id++) {
262
263 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
264 if (id->id.bustype != dev->id.bustype)
265 continue;
266
267 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
268 if (id->id.vendor != dev->id.vendor)
269 continue;
270
271 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
272 if (id->id.product != dev->id.product)
273 continue;
274
275 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
276 if (id->id.version != dev->id.version)
277 continue;
278
279 MATCH_BIT(evbit, EV_MAX);
280 MATCH_BIT(keybit, KEY_MAX);
281 MATCH_BIT(relbit, REL_MAX);
282 MATCH_BIT(absbit, ABS_MAX);
283 MATCH_BIT(mscbit, MSC_MAX);
284 MATCH_BIT(ledbit, LED_MAX);
285 MATCH_BIT(sndbit, SND_MAX);
286 MATCH_BIT(ffbit, FF_MAX);
287
288 return id;
289 }
290
291 return NULL;
292}
293
294/*
295 * Input hotplugging interface - loading event handlers based on
296 * device bitfields.
297 */
298
299#ifdef CONFIG_HOTPLUG
300
301/*
302 * Input hotplugging invokes what /proc/sys/kernel/hotplug says
303 * (normally /sbin/hotplug) when input devices get added or removed.
304 *
305 * This invokes a user mode policy agent, typically helping to load driver
306 * or other modules, configure the device, and more. Drivers can provide
307 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
308 *
309 */
310
311#define SPRINTF_BIT_A(bit, name, max) \
312 do { \
313 envp[i++] = scratch; \
314 scratch += sprintf(scratch, name); \
315 for (j = NBITS(max) - 1; j >= 0; j--) \
316 if (dev->bit[j]) break; \
317 for (; j >= 0; j--) \
318 scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
319 scratch++; \
320 } while (0)
321
322#define SPRINTF_BIT_A2(bit, name, max, ev) \
323 do { \
324 if (test_bit(ev, dev->evbit)) \
325 SPRINTF_BIT_A(bit, name, max); \
326 } while (0)
327
328static void input_call_hotplug(char *verb, struct input_dev *dev)
329{
330 char *argv[3], **envp, *buf, *scratch;
331 int i = 0, j, value;
332
333 if (!hotplug_path[0]) {
334 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
335 return;
336 }
337 if (in_interrupt()) {
338 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
339 return;
340 }
341 if (!current->fs->root) {
342 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
343 return;
344 }
345 if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
346 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
347 return;
348 }
349 if (!(buf = kmalloc(1024, GFP_KERNEL))) {
350 kfree (envp);
351 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
352 return;
353 }
354
355 argv[0] = hotplug_path;
356 argv[1] = "input";
357 argv[2] = NULL;
358
359 envp[i++] = "HOME=/";
360 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
361
362 scratch = buf;
363
364 envp[i++] = scratch;
365 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
366
367 envp[i++] = scratch;
368 scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
369 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
370
371 if (dev->name) {
372 envp[i++] = scratch;
373 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
374 }
375
376 if (dev->phys) {
377 envp[i++] = scratch;
378 scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
379 }
380
381 SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
382 SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
383 SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
384 SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
385 SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
386 SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
387 SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
388 SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF);
389
390 envp[i++] = NULL;
391
392#ifdef INPUT_DEBUG
393 printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
394 argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
395#endif
396
397 value = call_usermodehelper(argv [0], argv, envp, 0);
398
399 kfree(buf);
400 kfree(envp);
401
402#ifdef INPUT_DEBUG
403 if (value != 0)
404 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
405#endif
406}
407
408#endif
409
410void input_register_device(struct input_dev *dev)
411{
412 struct input_handle *handle;
413 struct input_handler *handler;
414 struct input_device_id *id;
415
416 set_bit(EV_SYN, dev->evbit);
417
418 /*
419 * If delay and period are pre-set by the driver, then autorepeating
420 * is handled by the driver itself and we don't do it in input.c.
421 */
422
423 init_timer(&dev->timer);
424 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
425 dev->timer.data = (long) dev;
426 dev->timer.function = input_repeat_key;
427 dev->rep[REP_DELAY] = 250;
428 dev->rep[REP_PERIOD] = 33;
429 }
430
431 INIT_LIST_HEAD(&dev->h_list);
432 list_add_tail(&dev->node, &input_dev_list);
433
434 list_for_each_entry(handler, &input_handler_list, node)
435 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
436 if ((id = input_match_device(handler->id_table, dev)))
437 if ((handle = handler->connect(handler, dev, id)))
438 input_link_handle(handle);
439
440#ifdef CONFIG_HOTPLUG
441 input_call_hotplug("add", dev);
442#endif
443
444#ifdef CONFIG_PROC_FS
445 input_devices_state++;
446 wake_up(&input_devices_poll_wait);
447#endif
448}
449
450void input_unregister_device(struct input_dev *dev)
451{
452 struct list_head * node, * next;
453
454 if (!dev) return;
455
456 del_timer_sync(&dev->timer);
457
458 list_for_each_safe(node, next, &dev->h_list) {
459 struct input_handle * handle = to_handle(node);
460 list_del_init(&handle->d_node);
461 list_del_init(&handle->h_node);
462 handle->handler->disconnect(handle);
463 }
464
465#ifdef CONFIG_HOTPLUG
466 input_call_hotplug("remove", dev);
467#endif
468
469 list_del_init(&dev->node);
470
471#ifdef CONFIG_PROC_FS
472 input_devices_state++;
473 wake_up(&input_devices_poll_wait);
474#endif
475}
476
477void input_register_handler(struct input_handler *handler)
478{
479 struct input_dev *dev;
480 struct input_handle *handle;
481 struct input_device_id *id;
482
483 if (!handler) return;
484
485 INIT_LIST_HEAD(&handler->h_list);
486
487 if (handler->fops != NULL)
488 input_table[handler->minor >> 5] = handler;
489
490 list_add_tail(&handler->node, &input_handler_list);
491
492 list_for_each_entry(dev, &input_dev_list, node)
493 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
494 if ((id = input_match_device(handler->id_table, dev)))
495 if ((handle = handler->connect(handler, dev, id)))
496 input_link_handle(handle);
497
498#ifdef CONFIG_PROC_FS
499 input_devices_state++;
500 wake_up(&input_devices_poll_wait);
501#endif
502}
503
504void input_unregister_handler(struct input_handler *handler)
505{
506 struct list_head * node, * next;
507
508 list_for_each_safe(node, next, &handler->h_list) {
509 struct input_handle * handle = to_handle_h(node);
510 list_del_init(&handle->h_node);
511 list_del_init(&handle->d_node);
512 handler->disconnect(handle);
513 }
514
515 list_del_init(&handler->node);
516
517 if (handler->fops != NULL)
518 input_table[handler->minor >> 5] = NULL;
519
520#ifdef CONFIG_PROC_FS
521 input_devices_state++;
522 wake_up(&input_devices_poll_wait);
523#endif
524}
525
526static int input_open_file(struct inode *inode, struct file *file)
527{
528 struct input_handler *handler = input_table[iminor(inode) >> 5];
529 struct file_operations *old_fops, *new_fops = NULL;
530 int err;
531
532 /* No load-on-demand here? */
533 if (!handler || !(new_fops = fops_get(handler->fops)))
534 return -ENODEV;
535
536 /*
537 * That's _really_ odd. Usually NULL ->open means "nothing special",
538 * not "no device". Oh, well...
539 */
540 if (!new_fops->open) {
541 fops_put(new_fops);
542 return -ENODEV;
543 }
544 old_fops = file->f_op;
545 file->f_op = new_fops;
546
547 err = new_fops->open(inode, file);
548
549 if (err) {
550 fops_put(file->f_op);
551 file->f_op = fops_get(old_fops);
552 }
553 fops_put(old_fops);
554 return err;
555}
556
557static struct file_operations input_fops = {
558 .owner = THIS_MODULE,
559 .open = input_open_file,
560};
561
562#ifdef CONFIG_PROC_FS
563
564#define SPRINTF_BIT_B(bit, name, max) \
565 do { \
566 len += sprintf(buf + len, "B: %s", name); \
567 for (i = NBITS(max) - 1; i >= 0; i--) \
568 if (dev->bit[i]) break; \
569 for (; i >= 0; i--) \
570 len += sprintf(buf + len, "%lx ", dev->bit[i]); \
571 len += sprintf(buf + len, "\n"); \
572 } while (0)
573
574#define SPRINTF_BIT_B2(bit, name, max, ev) \
575 do { \
576 if (test_bit(ev, dev->evbit)) \
577 SPRINTF_BIT_B(bit, name, max); \
578 } while (0)
579
580
581static unsigned int input_devices_poll(struct file *file, poll_table *wait)
582{
583 int state = input_devices_state;
584 poll_wait(file, &input_devices_poll_wait, wait);
585 if (state != input_devices_state)
586 return POLLIN | POLLRDNORM;
587 return 0;
588}
589
590static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
591{
592 struct input_dev *dev;
593 struct input_handle *handle;
594
595 off_t at = 0;
596 int i, len, cnt = 0;
597
598 list_for_each_entry(dev, &input_dev_list, node) {
599
600 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
601 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
602
603 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
604 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
605 len += sprintf(buf + len, "H: Handlers=");
606
607 list_for_each_entry(handle, &dev->h_list, d_node)
608 len += sprintf(buf + len, "%s ", handle->name);
609
610 len += sprintf(buf + len, "\n");
611
612 SPRINTF_BIT_B(evbit, "EV=", EV_MAX);
613 SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY);
614 SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL);
615 SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS);
616 SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC);
617 SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED);
618 SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND);
619 SPRINTF_BIT_B2(ffbit, "FF=", FF_MAX, EV_FF);
620
621 len += sprintf(buf + len, "\n");
622
623 at += len;
624
625 if (at >= pos) {
626 if (!*start) {
627 *start = buf + (pos - (at - len));
628 cnt = at - pos;
629 } else cnt += len;
630 buf += len;
631 if (cnt >= count)
632 break;
633 }
634 }
635
636 if (&dev->node == &input_dev_list)
637 *eof = 1;
638
639 return (count > cnt) ? cnt : count;
640}
641
642static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
643{
644 struct input_handler *handler;
645
646 off_t at = 0;
647 int len = 0, cnt = 0;
648 int i = 0;
649
650 list_for_each_entry(handler, &input_handler_list, node) {
651
652 if (handler->fops)
653 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
654 i++, handler->name, handler->minor);
655 else
656 len = sprintf(buf, "N: Number=%d Name=%s\n",
657 i++, handler->name);
658
659 at += len;
660
661 if (at >= pos) {
662 if (!*start) {
663 *start = buf + (pos - (at - len));
664 cnt = at - pos;
665 } else cnt += len;
666 buf += len;
667 if (cnt >= count)
668 break;
669 }
670 }
671 if (&handler->node == &input_handler_list)
672 *eof = 1;
673
674 return (count > cnt) ? cnt : count;
675}
676
677static int __init input_proc_init(void)
678{
679 struct proc_dir_entry *entry;
680
681 proc_bus_input_dir = proc_mkdir("input", proc_bus);
682 if (proc_bus_input_dir == NULL)
683 return -ENOMEM;
684 proc_bus_input_dir->owner = THIS_MODULE;
685 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
686 if (entry == NULL) {
687 remove_proc_entry("input", proc_bus);
688 return -ENOMEM;
689 }
690 entry->owner = THIS_MODULE;
691 entry->proc_fops->poll = input_devices_poll;
692 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
693 if (entry == NULL) {
694 remove_proc_entry("devices", proc_bus_input_dir);
695 remove_proc_entry("input", proc_bus);
696 return -ENOMEM;
697 }
698 entry->owner = THIS_MODULE;
699 return 0;
700}
701#else /* !CONFIG_PROC_FS */
702static inline int input_proc_init(void) { return 0; }
703#endif
704
705struct class_simple *input_class;
706
707static int __init input_init(void)
708{
709 int retval = -ENOMEM;
710
711 input_class = class_simple_create(THIS_MODULE, "input");
712 if (IS_ERR(input_class))
713 return PTR_ERR(input_class);
714 input_proc_init();
715 retval = register_chrdev(INPUT_MAJOR, "input", &input_fops);
716 if (retval) {
717 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
718 remove_proc_entry("devices", proc_bus_input_dir);
719 remove_proc_entry("handlers", proc_bus_input_dir);
720 remove_proc_entry("input", proc_bus);
721 class_simple_destroy(input_class);
722 return retval;
723 }
724
725 retval = devfs_mk_dir("input");
726 if (retval) {
727 remove_proc_entry("devices", proc_bus_input_dir);
728 remove_proc_entry("handlers", proc_bus_input_dir);
729 remove_proc_entry("input", proc_bus);
730 unregister_chrdev(INPUT_MAJOR, "input");
731 class_simple_destroy(input_class);
732 }
733 return retval;
734}
735
736static void __exit input_exit(void)
737{
738 remove_proc_entry("devices", proc_bus_input_dir);
739 remove_proc_entry("handlers", proc_bus_input_dir);
740 remove_proc_entry("input", proc_bus);
741
742 devfs_remove("input");
743 unregister_chrdev(INPUT_MAJOR, "input");
744 class_simple_destroy(input_class);
745}
746
747subsys_initcall(input_init);
748module_exit(input_exit);