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