1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/init.h>
14#include <linux/types.h>
15#include <linux/input.h>
16#include <linux/module.h>
17#include <linux/random.h>
18#include <linux/major.h>
19#include <linux/proc_fs.h>
20#include <linux/sched.h>
21#include <linux/seq_file.h>
22#include <linux/poll.h>
23#include <linux/device.h>
24#include <linux/mutex.h>
25#include <linux/rcupdate.h>
26#include <linux/smp_lock.h>
27
28MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29MODULE_DESCRIPTION("Input core");
30MODULE_LICENSE("GPL");
31
32#define INPUT_DEVICES 256
33
34
35
36
37static unsigned int input_abs_bypass_init_data[] __initdata = {
38 ABS_MT_TOUCH_MAJOR,
39 ABS_MT_TOUCH_MINOR,
40 ABS_MT_WIDTH_MAJOR,
41 ABS_MT_WIDTH_MINOR,
42 ABS_MT_ORIENTATION,
43 ABS_MT_POSITION_X,
44 ABS_MT_POSITION_Y,
45 ABS_MT_TOOL_TYPE,
46 ABS_MT_BLOB_ID,
47 ABS_MT_TRACKING_ID,
48 0
49};
50static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
51
52static LIST_HEAD(input_dev_list);
53static LIST_HEAD(input_handler_list);
54
55
56
57
58
59
60
61static DEFINE_MUTEX(input_mutex);
62
63static struct input_handler *input_table[8];
64
65static inline int is_event_supported(unsigned int code,
66 unsigned long *bm, unsigned int max)
67{
68 return code <= max && test_bit(code, bm);
69}
70
71static int input_defuzz_abs_event(int value, int old_val, int fuzz)
72{
73 if (fuzz) {
74 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
75 return old_val;
76
77 if (value > old_val - fuzz && value < old_val + fuzz)
78 return (old_val * 3 + value) / 4;
79
80 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
81 return (old_val + value) / 2;
82 }
83
84 return value;
85}
86
87
88
89
90
91static void input_pass_event(struct input_dev *dev,
92 unsigned int type, unsigned int code, int value)
93{
94 struct input_handle *handle;
95
96 rcu_read_lock();
97
98 handle = rcu_dereference(dev->grab);
99 if (handle)
100 handle->handler->event(handle, type, code, value);
101 else
102 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
103 if (handle->open)
104 handle->handler->event(handle,
105 type, code, value);
106 rcu_read_unlock();
107}
108
109
110
111
112
113
114static void input_repeat_key(unsigned long data)
115{
116 struct input_dev *dev = (void *) data;
117 unsigned long flags;
118
119 spin_lock_irqsave(&dev->event_lock, flags);
120
121 if (test_bit(dev->repeat_key, dev->key) &&
122 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
123
124 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
125
126 if (dev->sync) {
127
128
129
130
131
132
133 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
134 }
135
136 if (dev->rep[REP_PERIOD])
137 mod_timer(&dev->timer, jiffies +
138 msecs_to_jiffies(dev->rep[REP_PERIOD]));
139 }
140
141 spin_unlock_irqrestore(&dev->event_lock, flags);
142}
143
144static void input_start_autorepeat(struct input_dev *dev, int code)
145{
146 if (test_bit(EV_REP, dev->evbit) &&
147 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
148 dev->timer.data) {
149 dev->repeat_key = code;
150 mod_timer(&dev->timer,
151 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
152 }
153}
154
155static void input_stop_autorepeat(struct input_dev *dev)
156{
157 del_timer(&dev->timer);
158}
159
160#define INPUT_IGNORE_EVENT 0
161#define INPUT_PASS_TO_HANDLERS 1
162#define INPUT_PASS_TO_DEVICE 2
163#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
164
165static void input_handle_event(struct input_dev *dev,
166 unsigned int type, unsigned int code, int value)
167{
168 int disposition = INPUT_IGNORE_EVENT;
169
170 switch (type) {
171
172 case EV_SYN:
173 switch (code) {
174 case SYN_CONFIG:
175 disposition = INPUT_PASS_TO_ALL;
176 break;
177
178 case SYN_REPORT:
179 if (!dev->sync) {
180 dev->sync = 1;
181 disposition = INPUT_PASS_TO_HANDLERS;
182 }
183 break;
184 case SYN_MT_REPORT:
185 dev->sync = 0;
186 disposition = INPUT_PASS_TO_HANDLERS;
187 break;
188 }
189 break;
190
191 case EV_KEY:
192 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
193 !!test_bit(code, dev->key) != value) {
194
195 if (value != 2) {
196 __change_bit(code, dev->key);
197 if (value)
198 input_start_autorepeat(dev, code);
199 else
200 input_stop_autorepeat(dev);
201 }
202
203 disposition = INPUT_PASS_TO_HANDLERS;
204 }
205 break;
206
207 case EV_SW:
208 if (is_event_supported(code, dev->swbit, SW_MAX) &&
209 !!test_bit(code, dev->sw) != value) {
210
211 __change_bit(code, dev->sw);
212 disposition = INPUT_PASS_TO_HANDLERS;
213 }
214 break;
215
216 case EV_ABS:
217 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
218
219 if (test_bit(code, input_abs_bypass)) {
220 disposition = INPUT_PASS_TO_HANDLERS;
221 break;
222 }
223
224 value = input_defuzz_abs_event(value,
225 dev->abs[code], dev->absfuzz[code]);
226
227 if (dev->abs[code] != value) {
228 dev->abs[code] = value;
229 disposition = INPUT_PASS_TO_HANDLERS;
230 }
231 }
232 break;
233
234 case EV_REL:
235 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
236 disposition = INPUT_PASS_TO_HANDLERS;
237
238 break;
239
240 case EV_MSC:
241 if (is_event_supported(code, dev->mscbit, MSC_MAX))
242 disposition = INPUT_PASS_TO_ALL;
243
244 break;
245
246 case EV_LED:
247 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
248 !!test_bit(code, dev->led) != value) {
249
250 __change_bit(code, dev->led);
251 disposition = INPUT_PASS_TO_ALL;
252 }
253 break;
254
255 case EV_SND:
256 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
257
258 if (!!test_bit(code, dev->snd) != !!value)
259 __change_bit(code, dev->snd);
260 disposition = INPUT_PASS_TO_ALL;
261 }
262 break;
263
264 case EV_REP:
265 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
266 dev->rep[code] = value;
267 disposition = INPUT_PASS_TO_ALL;
268 }
269 break;
270
271 case EV_FF:
272 if (value >= 0)
273 disposition = INPUT_PASS_TO_ALL;
274 break;
275
276 case EV_PWR:
277 disposition = INPUT_PASS_TO_ALL;
278 break;
279 }
280
281 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
282 dev->sync = 0;
283
284 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
285 dev->event(dev, type, code, value);
286
287 if (disposition & INPUT_PASS_TO_HANDLERS)
288 input_pass_event(dev, type, code, value);
289}
290
291
292
293
294
295
296
297
298
299
300
301
302void input_event(struct input_dev *dev,
303 unsigned int type, unsigned int code, int value)
304{
305 unsigned long flags;
306
307 if (is_event_supported(type, dev->evbit, EV_MAX)) {
308
309 spin_lock_irqsave(&dev->event_lock, flags);
310 add_input_randomness(type, code, value);
311 input_handle_event(dev, type, code, value);
312 spin_unlock_irqrestore(&dev->event_lock, flags);
313 }
314}
315EXPORT_SYMBOL(input_event);
316
317
318
319
320
321
322
323
324
325
326
327
328void input_inject_event(struct input_handle *handle,
329 unsigned int type, unsigned int code, int value)
330{
331 struct input_dev *dev = handle->dev;
332 struct input_handle *grab;
333 unsigned long flags;
334
335 if (is_event_supported(type, dev->evbit, EV_MAX)) {
336 spin_lock_irqsave(&dev->event_lock, flags);
337
338 rcu_read_lock();
339 grab = rcu_dereference(dev->grab);
340 if (!grab || grab == handle)
341 input_handle_event(dev, type, code, value);
342 rcu_read_unlock();
343
344 spin_unlock_irqrestore(&dev->event_lock, flags);
345 }
346}
347EXPORT_SYMBOL(input_inject_event);
348
349
350
351
352
353
354
355
356
357int input_grab_device(struct input_handle *handle)
358{
359 struct input_dev *dev = handle->dev;
360 int retval;
361
362 retval = mutex_lock_interruptible(&dev->mutex);
363 if (retval)
364 return retval;
365
366 if (dev->grab) {
367 retval = -EBUSY;
368 goto out;
369 }
370
371 rcu_assign_pointer(dev->grab, handle);
372 synchronize_rcu();
373
374 out:
375 mutex_unlock(&dev->mutex);
376 return retval;
377}
378EXPORT_SYMBOL(input_grab_device);
379
380static void __input_release_device(struct input_handle *handle)
381{
382 struct input_dev *dev = handle->dev;
383
384 if (dev->grab == handle) {
385 rcu_assign_pointer(dev->grab, NULL);
386
387 synchronize_rcu();
388
389 list_for_each_entry(handle, &dev->h_list, d_node)
390 if (handle->open && handle->handler->start)
391 handle->handler->start(handle);
392 }
393}
394
395
396
397
398
399
400
401
402
403
404void input_release_device(struct input_handle *handle)
405{
406 struct input_dev *dev = handle->dev;
407
408 mutex_lock(&dev->mutex);
409 __input_release_device(handle);
410 mutex_unlock(&dev->mutex);
411}
412EXPORT_SYMBOL(input_release_device);
413
414
415
416
417
418
419
420
421int input_open_device(struct input_handle *handle)
422{
423 struct input_dev *dev = handle->dev;
424 int retval;
425
426 retval = mutex_lock_interruptible(&dev->mutex);
427 if (retval)
428 return retval;
429
430 if (dev->going_away) {
431 retval = -ENODEV;
432 goto out;
433 }
434
435 handle->open++;
436
437 if (!dev->users++ && dev->open)
438 retval = dev->open(dev);
439
440 if (retval) {
441 dev->users--;
442 if (!--handle->open) {
443
444
445
446
447 synchronize_rcu();
448 }
449 }
450
451 out:
452 mutex_unlock(&dev->mutex);
453 return retval;
454}
455EXPORT_SYMBOL(input_open_device);
456
457int input_flush_device(struct input_handle *handle, struct file *file)
458{
459 struct input_dev *dev = handle->dev;
460 int retval;
461
462 retval = mutex_lock_interruptible(&dev->mutex);
463 if (retval)
464 return retval;
465
466 if (dev->flush)
467 retval = dev->flush(dev, file);
468
469 mutex_unlock(&dev->mutex);
470 return retval;
471}
472EXPORT_SYMBOL(input_flush_device);
473
474
475
476
477
478
479
480
481void input_close_device(struct input_handle *handle)
482{
483 struct input_dev *dev = handle->dev;
484
485 mutex_lock(&dev->mutex);
486
487 __input_release_device(handle);
488
489 if (!--dev->users && dev->close)
490 dev->close(dev);
491
492 if (!--handle->open) {
493
494
495
496
497
498 synchronize_rcu();
499 }
500
501 mutex_unlock(&dev->mutex);
502}
503EXPORT_SYMBOL(input_close_device);
504
505
506
507
508static void input_disconnect_device(struct input_dev *dev)
509{
510 struct input_handle *handle;
511 int code;
512
513
514
515
516
517
518 mutex_lock(&dev->mutex);
519 dev->going_away = true;
520 mutex_unlock(&dev->mutex);
521
522 spin_lock_irq(&dev->event_lock);
523
524
525
526
527
528
529
530 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
531 for (code = 0; code <= KEY_MAX; code++) {
532 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
533 __test_and_clear_bit(code, dev->key)) {
534 input_pass_event(dev, EV_KEY, code, 0);
535 }
536 }
537 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
538 }
539
540 list_for_each_entry(handle, &dev->h_list, d_node)
541 handle->open = 0;
542
543 spin_unlock_irq(&dev->event_lock);
544}
545
546static int input_fetch_keycode(struct input_dev *dev, int scancode)
547{
548 switch (dev->keycodesize) {
549 case 1:
550 return ((u8 *)dev->keycode)[scancode];
551
552 case 2:
553 return ((u16 *)dev->keycode)[scancode];
554
555 default:
556 return ((u32 *)dev->keycode)[scancode];
557 }
558}
559
560static int input_default_getkeycode(struct input_dev *dev,
561 int scancode, int *keycode)
562{
563 if (!dev->keycodesize)
564 return -EINVAL;
565
566 if (scancode >= dev->keycodemax)
567 return -EINVAL;
568
569 *keycode = input_fetch_keycode(dev, scancode);
570
571 return 0;
572}
573
574static int input_default_setkeycode(struct input_dev *dev,
575 int scancode, int keycode)
576{
577 int old_keycode;
578 int i;
579
580 if (scancode >= dev->keycodemax)
581 return -EINVAL;
582
583 if (!dev->keycodesize)
584 return -EINVAL;
585
586 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
587 return -EINVAL;
588
589 switch (dev->keycodesize) {
590 case 1: {
591 u8 *k = (u8 *)dev->keycode;
592 old_keycode = k[scancode];
593 k[scancode] = keycode;
594 break;
595 }
596 case 2: {
597 u16 *k = (u16 *)dev->keycode;
598 old_keycode = k[scancode];
599 k[scancode] = keycode;
600 break;
601 }
602 default: {
603 u32 *k = (u32 *)dev->keycode;
604 old_keycode = k[scancode];
605 k[scancode] = keycode;
606 break;
607 }
608 }
609
610 clear_bit(old_keycode, dev->keybit);
611 set_bit(keycode, dev->keybit);
612
613 for (i = 0; i < dev->keycodemax; i++) {
614 if (input_fetch_keycode(dev, i) == old_keycode) {
615 set_bit(old_keycode, dev->keybit);
616 break;
617 }
618 }
619
620 return 0;
621}
622
623
624
625
626
627
628
629
630
631
632
633int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
634{
635 if (scancode < 0)
636 return -EINVAL;
637
638 return dev->getkeycode(dev, scancode, keycode);
639}
640EXPORT_SYMBOL(input_get_keycode);
641
642
643
644
645
646
647
648
649
650
651int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
652{
653 unsigned long flags;
654 int old_keycode;
655 int retval;
656
657 if (scancode < 0)
658 return -EINVAL;
659
660 if (keycode < 0 || keycode > KEY_MAX)
661 return -EINVAL;
662
663 spin_lock_irqsave(&dev->event_lock, flags);
664
665 retval = dev->getkeycode(dev, scancode, &old_keycode);
666 if (retval)
667 goto out;
668
669 retval = dev->setkeycode(dev, scancode, keycode);
670 if (retval)
671 goto out;
672
673
674
675
676
677 if (test_bit(EV_KEY, dev->evbit) &&
678 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
679 __test_and_clear_bit(old_keycode, dev->key)) {
680
681 input_pass_event(dev, EV_KEY, old_keycode, 0);
682 if (dev->sync)
683 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
684 }
685
686 out:
687 spin_unlock_irqrestore(&dev->event_lock, flags);
688
689 return retval;
690}
691EXPORT_SYMBOL(input_set_keycode);
692
693#define MATCH_BIT(bit, max) \
694 for (i = 0; i < BITS_TO_LONGS(max); i++) \
695 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
696 break; \
697 if (i != BITS_TO_LONGS(max)) \
698 continue;
699
700static const struct input_device_id *input_match_device(const struct input_device_id *id,
701 struct input_dev *dev)
702{
703 int i;
704
705 for (; id->flags || id->driver_info; id++) {
706
707 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
708 if (id->bustype != dev->id.bustype)
709 continue;
710
711 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
712 if (id->vendor != dev->id.vendor)
713 continue;
714
715 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
716 if (id->product != dev->id.product)
717 continue;
718
719 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
720 if (id->version != dev->id.version)
721 continue;
722
723 MATCH_BIT(evbit, EV_MAX);
724 MATCH_BIT(keybit, KEY_MAX);
725 MATCH_BIT(relbit, REL_MAX);
726 MATCH_BIT(absbit, ABS_MAX);
727 MATCH_BIT(mscbit, MSC_MAX);
728 MATCH_BIT(ledbit, LED_MAX);
729 MATCH_BIT(sndbit, SND_MAX);
730 MATCH_BIT(ffbit, FF_MAX);
731 MATCH_BIT(swbit, SW_MAX);
732
733 return id;
734 }
735
736 return NULL;
737}
738
739static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
740{
741 const struct input_device_id *id;
742 int error;
743
744 if (handler->blacklist && input_match_device(handler->blacklist, dev))
745 return -ENODEV;
746
747 id = input_match_device(handler->id_table, dev);
748 if (!id)
749 return -ENODEV;
750
751 error = handler->connect(handler, dev, id);
752 if (error && error != -ENODEV)
753 printk(KERN_ERR
754 "input: failed to attach handler %s to device %s, "
755 "error: %d\n",
756 handler->name, kobject_name(&dev->dev.kobj), error);
757
758 return error;
759}
760
761
762#ifdef CONFIG_PROC_FS
763
764static struct proc_dir_entry *proc_bus_input_dir;
765static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
766static int input_devices_state;
767
768static inline void input_wakeup_procfs_readers(void)
769{
770 input_devices_state++;
771 wake_up(&input_devices_poll_wait);
772}
773
774static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
775{
776 poll_wait(file, &input_devices_poll_wait, wait);
777 if (file->f_version != input_devices_state) {
778 file->f_version = input_devices_state;
779 return POLLIN | POLLRDNORM;
780 }
781
782 return 0;
783}
784
785union input_seq_state {
786 struct {
787 unsigned short pos;
788 bool mutex_acquired;
789 };
790 void *p;
791};
792
793static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
794{
795 union input_seq_state *state = (union input_seq_state *)&seq->private;
796 int error;
797
798
799 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
800
801 error = mutex_lock_interruptible(&input_mutex);
802 if (error) {
803 state->mutex_acquired = false;
804 return ERR_PTR(error);
805 }
806
807 state->mutex_acquired = true;
808
809 return seq_list_start(&input_dev_list, *pos);
810}
811
812static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
813{
814 return seq_list_next(v, &input_dev_list, pos);
815}
816
817static void input_seq_stop(struct seq_file *seq, void *v)
818{
819 union input_seq_state *state = (union input_seq_state *)&seq->private;
820
821 if (state->mutex_acquired)
822 mutex_unlock(&input_mutex);
823}
824
825static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
826 unsigned long *bitmap, int max)
827{
828 int i;
829
830 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
831 if (bitmap[i])
832 break;
833
834 seq_printf(seq, "B: %s=", name);
835 for (; i >= 0; i--)
836 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
837 seq_putc(seq, '\n');
838}
839
840static int input_devices_seq_show(struct seq_file *seq, void *v)
841{
842 struct input_dev *dev = container_of(v, struct input_dev, node);
843 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
844 struct input_handle *handle;
845
846 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
847 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
848
849 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
850 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
851 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
852 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
853 seq_printf(seq, "H: Handlers=");
854
855 list_for_each_entry(handle, &dev->h_list, d_node)
856 seq_printf(seq, "%s ", handle->name);
857 seq_putc(seq, '\n');
858
859 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
860 if (test_bit(EV_KEY, dev->evbit))
861 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
862 if (test_bit(EV_REL, dev->evbit))
863 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
864 if (test_bit(EV_ABS, dev->evbit))
865 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
866 if (test_bit(EV_MSC, dev->evbit))
867 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
868 if (test_bit(EV_LED, dev->evbit))
869 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
870 if (test_bit(EV_SND, dev->evbit))
871 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
872 if (test_bit(EV_FF, dev->evbit))
873 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
874 if (test_bit(EV_SW, dev->evbit))
875 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
876
877 seq_putc(seq, '\n');
878
879 kfree(path);
880 return 0;
881}
882
883static const struct seq_operations input_devices_seq_ops = {
884 .start = input_devices_seq_start,
885 .next = input_devices_seq_next,
886 .stop = input_seq_stop,
887 .show = input_devices_seq_show,
888};
889
890static int input_proc_devices_open(struct inode *inode, struct file *file)
891{
892 return seq_open(file, &input_devices_seq_ops);
893}
894
895static const struct file_operations input_devices_fileops = {
896 .owner = THIS_MODULE,
897 .open = input_proc_devices_open,
898 .poll = input_proc_devices_poll,
899 .read = seq_read,
900 .llseek = seq_lseek,
901 .release = seq_release,
902};
903
904static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
905{
906 union input_seq_state *state = (union input_seq_state *)&seq->private;
907 int error;
908
909
910 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
911
912 error = mutex_lock_interruptible(&input_mutex);
913 if (error) {
914 state->mutex_acquired = false;
915 return ERR_PTR(error);
916 }
917
918 state->mutex_acquired = true;
919 state->pos = *pos;
920
921 return seq_list_start(&input_handler_list, *pos);
922}
923
924static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
925{
926 union input_seq_state *state = (union input_seq_state *)&seq->private;
927
928 state->pos = *pos + 1;
929 return seq_list_next(v, &input_handler_list, pos);
930}
931
932static int input_handlers_seq_show(struct seq_file *seq, void *v)
933{
934 struct input_handler *handler = container_of(v, struct input_handler, node);
935 union input_seq_state *state = (union input_seq_state *)&seq->private;
936
937 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
938 if (handler->fops)
939 seq_printf(seq, " Minor=%d", handler->minor);
940 seq_putc(seq, '\n');
941
942 return 0;
943}
944
945static const struct seq_operations input_handlers_seq_ops = {
946 .start = input_handlers_seq_start,
947 .next = input_handlers_seq_next,
948 .stop = input_seq_stop,
949 .show = input_handlers_seq_show,
950};
951
952static int input_proc_handlers_open(struct inode *inode, struct file *file)
953{
954 return seq_open(file, &input_handlers_seq_ops);
955}
956
957static const struct file_operations input_handlers_fileops = {
958 .owner = THIS_MODULE,
959 .open = input_proc_handlers_open,
960 .read = seq_read,
961 .llseek = seq_lseek,
962 .release = seq_release,
963};
964
965static int __init input_proc_init(void)
966{
967 struct proc_dir_entry *entry;
968
969 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
970 if (!proc_bus_input_dir)
971 return -ENOMEM;
972
973 entry = proc_create("devices", 0, proc_bus_input_dir,
974 &input_devices_fileops);
975 if (!entry)
976 goto fail1;
977
978 entry = proc_create("handlers", 0, proc_bus_input_dir,
979 &input_handlers_fileops);
980 if (!entry)
981 goto fail2;
982
983 return 0;
984
985 fail2: remove_proc_entry("devices", proc_bus_input_dir);
986 fail1: remove_proc_entry("bus/input", NULL);
987 return -ENOMEM;
988}
989
990static void input_proc_exit(void)
991{
992 remove_proc_entry("devices", proc_bus_input_dir);
993 remove_proc_entry("handlers", proc_bus_input_dir);
994 remove_proc_entry("bus/input", NULL);
995}
996
997#else
998static inline void input_wakeup_procfs_readers(void) { }
999static inline int input_proc_init(void) { return 0; }
1000static inline void input_proc_exit(void) { }
1001#endif
1002
1003#define INPUT_DEV_STRING_ATTR_SHOW(name) \
1004static ssize_t input_dev_show_##name(struct device *dev, \
1005 struct device_attribute *attr, \
1006 char *buf) \
1007{ \
1008 struct input_dev *input_dev = to_input_dev(dev); \
1009 \
1010 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1011 input_dev->name ? input_dev->name : ""); \
1012} \
1013static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
1014
1015INPUT_DEV_STRING_ATTR_SHOW(name);
1016INPUT_DEV_STRING_ATTR_SHOW(phys);
1017INPUT_DEV_STRING_ATTR_SHOW(uniq);
1018
1019static int input_print_modalias_bits(char *buf, int size,
1020 char name, unsigned long *bm,
1021 unsigned int min_bit, unsigned int max_bit)
1022{
1023 int len = 0, i;
1024
1025 len += snprintf(buf, max(size, 0), "%c", name);
1026 for (i = min_bit; i < max_bit; i++)
1027 if (bm[BIT_WORD(i)] & BIT_MASK(i))
1028 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1029 return len;
1030}
1031
1032static int input_print_modalias(char *buf, int size, struct input_dev *id,
1033 int add_cr)
1034{
1035 int len;
1036
1037 len = snprintf(buf, max(size, 0),
1038 "input:b%04Xv%04Xp%04Xe%04X-",
1039 id->id.bustype, id->id.vendor,
1040 id->id.product, id->id.version);
1041
1042 len += input_print_modalias_bits(buf + len, size - len,
1043 'e', id->evbit, 0, EV_MAX);
1044 len += input_print_modalias_bits(buf + len, size - len,
1045 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1046 len += input_print_modalias_bits(buf + len, size - len,
1047 'r', id->relbit, 0, REL_MAX);
1048 len += input_print_modalias_bits(buf + len, size - len,
1049 'a', id->absbit, 0, ABS_MAX);
1050 len += input_print_modalias_bits(buf + len, size - len,
1051 'm', id->mscbit, 0, MSC_MAX);
1052 len += input_print_modalias_bits(buf + len, size - len,
1053 'l', id->ledbit, 0, LED_MAX);
1054 len += input_print_modalias_bits(buf + len, size - len,
1055 's', id->sndbit, 0, SND_MAX);
1056 len += input_print_modalias_bits(buf + len, size - len,
1057 'f', id->ffbit, 0, FF_MAX);
1058 len += input_print_modalias_bits(buf + len, size - len,
1059 'w', id->swbit, 0, SW_MAX);
1060
1061 if (add_cr)
1062 len += snprintf(buf + len, max(size - len, 0), "\n");
1063
1064 return len;
1065}
1066
1067static ssize_t input_dev_show_modalias(struct device *dev,
1068 struct device_attribute *attr,
1069 char *buf)
1070{
1071 struct input_dev *id = to_input_dev(dev);
1072 ssize_t len;
1073
1074 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1075
1076 return min_t(int, len, PAGE_SIZE);
1077}
1078static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1079
1080static struct attribute *input_dev_attrs[] = {
1081 &dev_attr_name.attr,
1082 &dev_attr_phys.attr,
1083 &dev_attr_uniq.attr,
1084 &dev_attr_modalias.attr,
1085 NULL
1086};
1087
1088static struct attribute_group input_dev_attr_group = {
1089 .attrs = input_dev_attrs,
1090};
1091
1092#define INPUT_DEV_ID_ATTR(name) \
1093static ssize_t input_dev_show_id_##name(struct device *dev, \
1094 struct device_attribute *attr, \
1095 char *buf) \
1096{ \
1097 struct input_dev *input_dev = to_input_dev(dev); \
1098 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1099} \
1100static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1101
1102INPUT_DEV_ID_ATTR(bustype);
1103INPUT_DEV_ID_ATTR(vendor);
1104INPUT_DEV_ID_ATTR(product);
1105INPUT_DEV_ID_ATTR(version);
1106
1107static struct attribute *input_dev_id_attrs[] = {
1108 &dev_attr_bustype.attr,
1109 &dev_attr_vendor.attr,
1110 &dev_attr_product.attr,
1111 &dev_attr_version.attr,
1112 NULL
1113};
1114
1115static struct attribute_group input_dev_id_attr_group = {
1116 .name = "id",
1117 .attrs = input_dev_id_attrs,
1118};
1119
1120static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1121 int max, int add_cr)
1122{
1123 int i;
1124 int len = 0;
1125
1126 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
1127 if (bitmap[i])
1128 break;
1129
1130 for (; i >= 0; i--)
1131 len += snprintf(buf + len, max(buf_size - len, 0),
1132 "%lx%s", bitmap[i], i > 0 ? " " : "");
1133
1134 if (add_cr)
1135 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1136
1137 return len;
1138}
1139
1140#define INPUT_DEV_CAP_ATTR(ev, bm) \
1141static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1142 struct device_attribute *attr, \
1143 char *buf) \
1144{ \
1145 struct input_dev *input_dev = to_input_dev(dev); \
1146 int len = input_print_bitmap(buf, PAGE_SIZE, \
1147 input_dev->bm##bit, ev##_MAX, 1); \
1148 return min_t(int, len, PAGE_SIZE); \
1149} \
1150static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1151
1152INPUT_DEV_CAP_ATTR(EV, ev);
1153INPUT_DEV_CAP_ATTR(KEY, key);
1154INPUT_DEV_CAP_ATTR(REL, rel);
1155INPUT_DEV_CAP_ATTR(ABS, abs);
1156INPUT_DEV_CAP_ATTR(MSC, msc);
1157INPUT_DEV_CAP_ATTR(LED, led);
1158INPUT_DEV_CAP_ATTR(SND, snd);
1159INPUT_DEV_CAP_ATTR(FF, ff);
1160INPUT_DEV_CAP_ATTR(SW, sw);
1161
1162static struct attribute *input_dev_caps_attrs[] = {
1163 &dev_attr_ev.attr,
1164 &dev_attr_key.attr,
1165 &dev_attr_rel.attr,
1166 &dev_attr_abs.attr,
1167 &dev_attr_msc.attr,
1168 &dev_attr_led.attr,
1169 &dev_attr_snd.attr,
1170 &dev_attr_ff.attr,
1171 &dev_attr_sw.attr,
1172 NULL
1173};
1174
1175static struct attribute_group input_dev_caps_attr_group = {
1176 .name = "capabilities",
1177 .attrs = input_dev_caps_attrs,
1178};
1179
1180static const struct attribute_group *input_dev_attr_groups[] = {
1181 &input_dev_attr_group,
1182 &input_dev_id_attr_group,
1183 &input_dev_caps_attr_group,
1184 NULL
1185};
1186
1187static void input_dev_release(struct device *device)
1188{
1189 struct input_dev *dev = to_input_dev(device);
1190
1191 input_ff_destroy(dev);
1192 kfree(dev);
1193
1194 module_put(THIS_MODULE);
1195}
1196
1197
1198
1199
1200
1201static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1202 const char *name, unsigned long *bitmap, int max)
1203{
1204 int len;
1205
1206 if (add_uevent_var(env, "%s=", name))
1207 return -ENOMEM;
1208
1209 len = input_print_bitmap(&env->buf[env->buflen - 1],
1210 sizeof(env->buf) - env->buflen,
1211 bitmap, max, 0);
1212 if (len >= (sizeof(env->buf) - env->buflen))
1213 return -ENOMEM;
1214
1215 env->buflen += len;
1216 return 0;
1217}
1218
1219static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1220 struct input_dev *dev)
1221{
1222 int len;
1223
1224 if (add_uevent_var(env, "MODALIAS="))
1225 return -ENOMEM;
1226
1227 len = input_print_modalias(&env->buf[env->buflen - 1],
1228 sizeof(env->buf) - env->buflen,
1229 dev, 0);
1230 if (len >= (sizeof(env->buf) - env->buflen))
1231 return -ENOMEM;
1232
1233 env->buflen += len;
1234 return 0;
1235}
1236
1237#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1238 do { \
1239 int err = add_uevent_var(env, fmt, val); \
1240 if (err) \
1241 return err; \
1242 } while (0)
1243
1244#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1245 do { \
1246 int err = input_add_uevent_bm_var(env, name, bm, max); \
1247 if (err) \
1248 return err; \
1249 } while (0)
1250
1251#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1252 do { \
1253 int err = input_add_uevent_modalias_var(env, dev); \
1254 if (err) \
1255 return err; \
1256 } while (0)
1257
1258static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1259{
1260 struct input_dev *dev = to_input_dev(device);
1261
1262 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1263 dev->id.bustype, dev->id.vendor,
1264 dev->id.product, dev->id.version);
1265 if (dev->name)
1266 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1267 if (dev->phys)
1268 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1269 if (dev->uniq)
1270 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1271
1272 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1273 if (test_bit(EV_KEY, dev->evbit))
1274 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1275 if (test_bit(EV_REL, dev->evbit))
1276 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1277 if (test_bit(EV_ABS, dev->evbit))
1278 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1279 if (test_bit(EV_MSC, dev->evbit))
1280 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1281 if (test_bit(EV_LED, dev->evbit))
1282 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1283 if (test_bit(EV_SND, dev->evbit))
1284 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1285 if (test_bit(EV_FF, dev->evbit))
1286 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1287 if (test_bit(EV_SW, dev->evbit))
1288 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1289
1290 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1291
1292 return 0;
1293}
1294
1295#define INPUT_DO_TOGGLE(dev, type, bits, on) \
1296 do { \
1297 int i; \
1298 bool active; \
1299 \
1300 if (!test_bit(EV_##type, dev->evbit)) \
1301 break; \
1302 \
1303 for (i = 0; i < type##_MAX; i++) { \
1304 if (!test_bit(i, dev->bits##bit)) \
1305 continue; \
1306 \
1307 active = test_bit(i, dev->bits); \
1308 if (!active && !on) \
1309 continue; \
1310 \
1311 dev->event(dev, EV_##type, i, on ? active : 0); \
1312 } \
1313 } while (0)
1314
1315#ifdef CONFIG_PM
1316static void input_dev_reset(struct input_dev *dev, bool activate)
1317{
1318 if (!dev->event)
1319 return;
1320
1321 INPUT_DO_TOGGLE(dev, LED, led, activate);
1322 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1323
1324 if (activate && test_bit(EV_REP, dev->evbit)) {
1325 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1326 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1327 }
1328}
1329
1330static int input_dev_suspend(struct device *dev)
1331{
1332 struct input_dev *input_dev = to_input_dev(dev);
1333
1334 mutex_lock(&input_dev->mutex);
1335 input_dev_reset(input_dev, false);
1336 mutex_unlock(&input_dev->mutex);
1337
1338 return 0;
1339}
1340
1341static int input_dev_resume(struct device *dev)
1342{
1343 struct input_dev *input_dev = to_input_dev(dev);
1344
1345 mutex_lock(&input_dev->mutex);
1346 input_dev_reset(input_dev, true);
1347 mutex_unlock(&input_dev->mutex);
1348
1349 return 0;
1350}
1351
1352static const struct dev_pm_ops input_dev_pm_ops = {
1353 .suspend = input_dev_suspend,
1354 .resume = input_dev_resume,
1355 .poweroff = input_dev_suspend,
1356 .restore = input_dev_resume,
1357};
1358#endif
1359
1360static struct device_type input_dev_type = {
1361 .groups = input_dev_attr_groups,
1362 .release = input_dev_release,
1363 .uevent = input_dev_uevent,
1364#ifdef CONFIG_PM
1365 .pm = &input_dev_pm_ops,
1366#endif
1367};
1368
1369static char *input_devnode(struct device *dev, mode_t *mode)
1370{
1371 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1372}
1373
1374struct class input_class = {
1375 .name = "input",
1376 .devnode = input_devnode,
1377};
1378EXPORT_SYMBOL_GPL(input_class);
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389struct input_dev *input_allocate_device(void)
1390{
1391 struct input_dev *dev;
1392
1393 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1394 if (dev) {
1395 dev->dev.type = &input_dev_type;
1396 dev->dev.class = &input_class;
1397 device_initialize(&dev->dev);
1398 mutex_init(&dev->mutex);
1399 spin_lock_init(&dev->event_lock);
1400 INIT_LIST_HEAD(&dev->h_list);
1401 INIT_LIST_HEAD(&dev->node);
1402
1403 __module_get(THIS_MODULE);
1404 }
1405
1406 return dev;
1407}
1408EXPORT_SYMBOL(input_allocate_device);
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424void input_free_device(struct input_dev *dev)
1425{
1426 if (dev)
1427 input_put_device(dev);
1428}
1429EXPORT_SYMBOL(input_free_device);
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1441{
1442 switch (type) {
1443 case EV_KEY:
1444 __set_bit(code, dev->keybit);
1445 break;
1446
1447 case EV_REL:
1448 __set_bit(code, dev->relbit);
1449 break;
1450
1451 case EV_ABS:
1452 __set_bit(code, dev->absbit);
1453 break;
1454
1455 case EV_MSC:
1456 __set_bit(code, dev->mscbit);
1457 break;
1458
1459 case EV_SW:
1460 __set_bit(code, dev->swbit);
1461 break;
1462
1463 case EV_LED:
1464 __set_bit(code, dev->ledbit);
1465 break;
1466
1467 case EV_SND:
1468 __set_bit(code, dev->sndbit);
1469 break;
1470
1471 case EV_FF:
1472 __set_bit(code, dev->ffbit);
1473 break;
1474
1475 case EV_PWR:
1476
1477 break;
1478
1479 default:
1480 printk(KERN_ERR
1481 "input_set_capability: unknown type %u (code %u)\n",
1482 type, code);
1483 dump_stack();
1484 return;
1485 }
1486
1487 __set_bit(type, dev->evbit);
1488}
1489EXPORT_SYMBOL(input_set_capability);
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503int input_register_device(struct input_dev *dev)
1504{
1505 static atomic_t input_no = ATOMIC_INIT(0);
1506 struct input_handler *handler;
1507 const char *path;
1508 int error;
1509
1510 __set_bit(EV_SYN, dev->evbit);
1511
1512
1513
1514
1515
1516
1517 init_timer(&dev->timer);
1518 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1519 dev->timer.data = (long) dev;
1520 dev->timer.function = input_repeat_key;
1521 dev->rep[REP_DELAY] = 250;
1522 dev->rep[REP_PERIOD] = 33;
1523 }
1524
1525 if (!dev->getkeycode)
1526 dev->getkeycode = input_default_getkeycode;
1527
1528 if (!dev->setkeycode)
1529 dev->setkeycode = input_default_setkeycode;
1530
1531 dev_set_name(&dev->dev, "input%ld",
1532 (unsigned long) atomic_inc_return(&input_no) - 1);
1533
1534 error = device_add(&dev->dev);
1535 if (error)
1536 return error;
1537
1538 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1539 printk(KERN_INFO "input: %s as %s\n",
1540 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1541 kfree(path);
1542
1543 error = mutex_lock_interruptible(&input_mutex);
1544 if (error) {
1545 device_del(&dev->dev);
1546 return error;
1547 }
1548
1549 list_add_tail(&dev->node, &input_dev_list);
1550
1551 list_for_each_entry(handler, &input_handler_list, node)
1552 input_attach_handler(dev, handler);
1553
1554 input_wakeup_procfs_readers();
1555
1556 mutex_unlock(&input_mutex);
1557
1558 return 0;
1559}
1560EXPORT_SYMBOL(input_register_device);
1561
1562
1563
1564
1565
1566
1567
1568
1569void input_unregister_device(struct input_dev *dev)
1570{
1571 struct input_handle *handle, *next;
1572
1573 input_disconnect_device(dev);
1574
1575 mutex_lock(&input_mutex);
1576
1577 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1578 handle->handler->disconnect(handle);
1579 WARN_ON(!list_empty(&dev->h_list));
1580
1581 del_timer_sync(&dev->timer);
1582 list_del_init(&dev->node);
1583
1584 input_wakeup_procfs_readers();
1585
1586 mutex_unlock(&input_mutex);
1587
1588 device_unregister(&dev->dev);
1589}
1590EXPORT_SYMBOL(input_unregister_device);
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600int input_register_handler(struct input_handler *handler)
1601{
1602 struct input_dev *dev;
1603 int retval;
1604
1605 retval = mutex_lock_interruptible(&input_mutex);
1606 if (retval)
1607 return retval;
1608
1609 INIT_LIST_HEAD(&handler->h_list);
1610
1611 if (handler->fops != NULL) {
1612 if (input_table[handler->minor >> 5]) {
1613 retval = -EBUSY;
1614 goto out;
1615 }
1616 input_table[handler->minor >> 5] = handler;
1617 }
1618
1619 list_add_tail(&handler->node, &input_handler_list);
1620
1621 list_for_each_entry(dev, &input_dev_list, node)
1622 input_attach_handler(dev, handler);
1623
1624 input_wakeup_procfs_readers();
1625
1626 out:
1627 mutex_unlock(&input_mutex);
1628 return retval;
1629}
1630EXPORT_SYMBOL(input_register_handler);
1631
1632
1633
1634
1635
1636
1637
1638
1639void input_unregister_handler(struct input_handler *handler)
1640{
1641 struct input_handle *handle, *next;
1642
1643 mutex_lock(&input_mutex);
1644
1645 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1646 handler->disconnect(handle);
1647 WARN_ON(!list_empty(&handler->h_list));
1648
1649 list_del_init(&handler->node);
1650
1651 if (handler->fops != NULL)
1652 input_table[handler->minor >> 5] = NULL;
1653
1654 input_wakeup_procfs_readers();
1655
1656 mutex_unlock(&input_mutex);
1657}
1658EXPORT_SYMBOL(input_unregister_handler);
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671int input_register_handle(struct input_handle *handle)
1672{
1673 struct input_handler *handler = handle->handler;
1674 struct input_dev *dev = handle->dev;
1675 int error;
1676
1677
1678
1679
1680
1681 error = mutex_lock_interruptible(&dev->mutex);
1682 if (error)
1683 return error;
1684 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1685 mutex_unlock(&dev->mutex);
1686
1687
1688
1689
1690
1691
1692
1693 list_add_tail(&handle->h_node, &handler->h_list);
1694
1695 if (handler->start)
1696 handler->start(handle);
1697
1698 return 0;
1699}
1700EXPORT_SYMBOL(input_register_handle);
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712void input_unregister_handle(struct input_handle *handle)
1713{
1714 struct input_dev *dev = handle->dev;
1715
1716 list_del_init(&handle->h_node);
1717
1718
1719
1720
1721 mutex_lock(&dev->mutex);
1722 list_del_rcu(&handle->d_node);
1723 mutex_unlock(&dev->mutex);
1724 synchronize_rcu();
1725}
1726EXPORT_SYMBOL(input_unregister_handle);
1727
1728static int input_open_file(struct inode *inode, struct file *file)
1729{
1730 struct input_handler *handler;
1731 const struct file_operations *old_fops, *new_fops = NULL;
1732 int err;
1733
1734 lock_kernel();
1735
1736 handler = input_table[iminor(inode) >> 5];
1737 if (!handler || !(new_fops = fops_get(handler->fops))) {
1738 err = -ENODEV;
1739 goto out;
1740 }
1741
1742
1743
1744
1745
1746 if (!new_fops->open) {
1747 fops_put(new_fops);
1748 err = -ENODEV;
1749 goto out;
1750 }
1751 old_fops = file->f_op;
1752 file->f_op = new_fops;
1753
1754 err = new_fops->open(inode, file);
1755
1756 if (err) {
1757 fops_put(file->f_op);
1758 file->f_op = fops_get(old_fops);
1759 }
1760 fops_put(old_fops);
1761out:
1762 unlock_kernel();
1763 return err;
1764}
1765
1766static const struct file_operations input_fops = {
1767 .owner = THIS_MODULE,
1768 .open = input_open_file,
1769};
1770
1771static void __init input_init_abs_bypass(void)
1772{
1773 const unsigned int *p;
1774
1775 for (p = input_abs_bypass_init_data; *p; p++)
1776 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1777}
1778
1779static int __init input_init(void)
1780{
1781 int err;
1782
1783 input_init_abs_bypass();
1784
1785 err = class_register(&input_class);
1786 if (err) {
1787 printk(KERN_ERR "input: unable to register input_dev class\n");
1788 return err;
1789 }
1790
1791 err = input_proc_init();
1792 if (err)
1793 goto fail1;
1794
1795 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1796 if (err) {
1797 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1798 goto fail2;
1799 }
1800
1801 return 0;
1802
1803 fail2: input_proc_exit();
1804 fail1: class_unregister(&input_class);
1805 return err;
1806}
1807
1808static void __exit input_exit(void)
1809{
1810 input_proc_exit();
1811 unregister_chrdev(INPUT_MAJOR, "input");
1812 class_unregister(&input_class);
1813}
1814
1815subsys_initcall(input_init);
1816module_exit(input_exit);
1817