4 * Copyright (c) 1999-2002 Vojtech Pavlik
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.
13 #include <linux/init.h>
14 #include <linux/smp_lock.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/seq_file.h>
21 #include <linux/interrupt.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
24 #include <linux/mutex.h>
26 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27 MODULE_DESCRIPTION("Input core");
28 MODULE_LICENSE("GPL");
30 #define INPUT_DEVICES 256
32 static LIST_HEAD(input_dev_list
);
33 static LIST_HEAD(input_handler_list
);
35 static struct input_handler
*input_table
[8];
38 * input_event() - report new input event
39 * @dev: device that generated the event
40 * @type: type of the event
42 * @value: value of the event
44 * This function should be used by drivers implementing various input devices
45 * See also input_inject_event()
47 void input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
49 struct input_handle
*handle
;
51 if (type
> EV_MAX
|| !test_bit(type
, dev
->evbit
))
54 add_input_randomness(type
, code
, value
);
62 dev
->event(dev
, type
, code
, value
);
75 if (code
> KEY_MAX
|| !test_bit(code
, dev
->keybit
) || !!test_bit(code
, dev
->key
) == value
)
81 change_bit(code
, dev
->key
);
83 if (test_bit(EV_REP
, dev
->evbit
) && dev
->rep
[REP_PERIOD
] && dev
->rep
[REP_DELAY
] && dev
->timer
.data
&& value
) {
84 dev
->repeat_key
= code
;
85 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_DELAY
]));
92 if (code
> SW_MAX
|| !test_bit(code
, dev
->swbit
) || !!test_bit(code
, dev
->sw
) == value
)
95 change_bit(code
, dev
->sw
);
101 if (code
> ABS_MAX
|| !test_bit(code
, dev
->absbit
))
104 if (dev
->absfuzz
[code
]) {
105 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] >> 1)) &&
106 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] >> 1)))
109 if ((value
> dev
->abs
[code
] - dev
->absfuzz
[code
]) &&
110 (value
< dev
->abs
[code
] + dev
->absfuzz
[code
]))
111 value
= (dev
->abs
[code
] * 3 + value
) >> 2;
113 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] << 1)) &&
114 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] << 1)))
115 value
= (dev
->abs
[code
] + value
) >> 1;
118 if (dev
->abs
[code
] == value
)
121 dev
->abs
[code
] = value
;
126 if (code
> REL_MAX
|| !test_bit(code
, dev
->relbit
) || (value
== 0))
133 if (code
> MSC_MAX
|| !test_bit(code
, dev
->mscbit
))
137 dev
->event(dev
, type
, code
, value
);
143 if (code
> LED_MAX
|| !test_bit(code
, dev
->ledbit
) || !!test_bit(code
, dev
->led
) == value
)
146 change_bit(code
, dev
->led
);
149 dev
->event(dev
, type
, code
, value
);
155 if (code
> SND_MAX
|| !test_bit(code
, dev
->sndbit
))
158 if (!!test_bit(code
, dev
->snd
) != !!value
)
159 change_bit(code
, dev
->snd
);
162 dev
->event(dev
, type
, code
, value
);
168 if (code
> REP_MAX
|| value
< 0 || dev
->rep
[code
] == value
)
171 dev
->rep
[code
] = value
;
173 dev
->event(dev
, type
, code
, value
);
183 dev
->event(dev
, type
, code
, value
);
191 dev
->grab
->handler
->event(dev
->grab
, type
, code
, value
);
193 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
195 handle
->handler
->event(handle
, type
, code
, value
);
197 EXPORT_SYMBOL(input_event
);
200 * input_inject_event() - send input event from input handler
201 * @handle: input handle to send event through
202 * @type: type of the event
204 * @value: value of the event
206 * Similar to input_event() but will ignore event if device is "grabbed" and handle
207 * injecting event is not the one that owns the device.
209 void input_inject_event(struct input_handle
*handle
, unsigned int type
, unsigned int code
, int value
)
211 if (!handle
->dev
->grab
|| handle
->dev
->grab
== handle
)
212 input_event(handle
->dev
, type
, code
, value
);
214 EXPORT_SYMBOL(input_inject_event
);
216 static void input_repeat_key(unsigned long data
)
218 struct input_dev
*dev
= (void *) data
;
220 if (!test_bit(dev
->repeat_key
, dev
->key
))
223 input_event(dev
, EV_KEY
, dev
->repeat_key
, 2);
226 if (dev
->rep
[REP_PERIOD
])
227 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_PERIOD
]));
230 int input_grab_device(struct input_handle
*handle
)
232 if (handle
->dev
->grab
)
235 handle
->dev
->grab
= handle
;
238 EXPORT_SYMBOL(input_grab_device
);
240 void input_release_device(struct input_handle
*handle
)
242 struct input_dev
*dev
= handle
->dev
;
244 if (dev
->grab
== handle
) {
247 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
248 if (handle
->handler
->start
)
249 handle
->handler
->start(handle
);
252 EXPORT_SYMBOL(input_release_device
);
254 int input_open_device(struct input_handle
*handle
)
256 struct input_dev
*dev
= handle
->dev
;
259 err
= mutex_lock_interruptible(&dev
->mutex
);
265 if (!dev
->users
++ && dev
->open
)
266 err
= dev
->open(dev
);
271 mutex_unlock(&dev
->mutex
);
275 EXPORT_SYMBOL(input_open_device
);
277 int input_flush_device(struct input_handle
* handle
, struct file
* file
)
279 if (handle
->dev
->flush
)
280 return handle
->dev
->flush(handle
->dev
, file
);
284 EXPORT_SYMBOL(input_flush_device
);
286 void input_close_device(struct input_handle
*handle
)
288 struct input_dev
*dev
= handle
->dev
;
290 input_release_device(handle
);
292 mutex_lock(&dev
->mutex
);
294 if (!--dev
->users
&& dev
->close
)
298 mutex_unlock(&dev
->mutex
);
300 EXPORT_SYMBOL(input_close_device
);
302 static int input_fetch_keycode(struct input_dev
*dev
, int scancode
)
304 switch (dev
->keycodesize
) {
306 return ((u8
*)dev
->keycode
)[scancode
];
309 return ((u16
*)dev
->keycode
)[scancode
];
312 return ((u32
*)dev
->keycode
)[scancode
];
316 static int input_default_getkeycode(struct input_dev
*dev
,
317 int scancode
, int *keycode
)
319 if (!dev
->keycodesize
)
322 if (scancode
< 0 || scancode
>= dev
->keycodemax
)
325 *keycode
= input_fetch_keycode(dev
, scancode
);
330 static int input_default_setkeycode(struct input_dev
*dev
,
331 int scancode
, int keycode
)
336 if (scancode
< 0 || scancode
>= dev
->keycodemax
)
339 if (keycode
< 0 || keycode
> KEY_MAX
)
342 if (!dev
->keycodesize
)
345 if (dev
->keycodesize
< sizeof(keycode
) && (keycode
>> (dev
->keycodesize
* 8)))
348 switch (dev
->keycodesize
) {
350 u8
*k
= (u8
*)dev
->keycode
;
351 old_keycode
= k
[scancode
];
352 k
[scancode
] = keycode
;
356 u16
*k
= (u16
*)dev
->keycode
;
357 old_keycode
= k
[scancode
];
358 k
[scancode
] = keycode
;
362 u32
*k
= (u32
*)dev
->keycode
;
363 old_keycode
= k
[scancode
];
364 k
[scancode
] = keycode
;
369 clear_bit(old_keycode
, dev
->keybit
);
370 set_bit(keycode
, dev
->keybit
);
372 for (i
= 0; i
< dev
->keycodemax
; i
++) {
373 if (input_fetch_keycode(dev
, i
) == old_keycode
) {
374 set_bit(old_keycode
, dev
->keybit
);
375 break; /* Setting the bit twice is useless, so break */
383 #define MATCH_BIT(bit, max) \
384 for (i = 0; i < NBITS(max); i++) \
385 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
387 if (i != NBITS(max)) \
390 static const struct input_device_id
*input_match_device(const struct input_device_id
*id
,
391 struct input_dev
*dev
)
395 for (; id
->flags
|| id
->driver_info
; id
++) {
397 if (id
->flags
& INPUT_DEVICE_ID_MATCH_BUS
)
398 if (id
->bustype
!= dev
->id
.bustype
)
401 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VENDOR
)
402 if (id
->vendor
!= dev
->id
.vendor
)
405 if (id
->flags
& INPUT_DEVICE_ID_MATCH_PRODUCT
)
406 if (id
->product
!= dev
->id
.product
)
409 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VERSION
)
410 if (id
->version
!= dev
->id
.version
)
413 MATCH_BIT(evbit
, EV_MAX
);
414 MATCH_BIT(keybit
, KEY_MAX
);
415 MATCH_BIT(relbit
, REL_MAX
);
416 MATCH_BIT(absbit
, ABS_MAX
);
417 MATCH_BIT(mscbit
, MSC_MAX
);
418 MATCH_BIT(ledbit
, LED_MAX
);
419 MATCH_BIT(sndbit
, SND_MAX
);
420 MATCH_BIT(ffbit
, FF_MAX
);
421 MATCH_BIT(swbit
, SW_MAX
);
429 static int input_attach_handler(struct input_dev
*dev
, struct input_handler
*handler
)
431 const struct input_device_id
*id
;
434 if (handler
->blacklist
&& input_match_device(handler
->blacklist
, dev
))
437 id
= input_match_device(handler
->id_table
, dev
);
441 error
= handler
->connect(handler
, dev
, id
);
442 if (error
&& error
!= -ENODEV
)
444 "input: failed to attach handler %s to device %s, "
446 handler
->name
, kobject_name(&dev
->cdev
.kobj
), error
);
452 #ifdef CONFIG_PROC_FS
454 static struct proc_dir_entry
*proc_bus_input_dir
;
455 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait
);
456 static int input_devices_state
;
458 static inline void input_wakeup_procfs_readers(void)
460 input_devices_state
++;
461 wake_up(&input_devices_poll_wait
);
464 static unsigned int input_proc_devices_poll(struct file
*file
, poll_table
*wait
)
466 int state
= input_devices_state
;
468 poll_wait(file
, &input_devices_poll_wait
, wait
);
469 if (state
!= input_devices_state
)
470 return POLLIN
| POLLRDNORM
;
475 static struct list_head
*list_get_nth_element(struct list_head
*list
, loff_t
*pos
)
477 struct list_head
*node
;
480 list_for_each(node
, list
)
487 static struct list_head
*list_get_next_element(struct list_head
*list
, struct list_head
*element
, loff_t
*pos
)
489 if (element
->next
== list
)
493 return element
->next
;
496 static void *input_devices_seq_start(struct seq_file
*seq
, loff_t
*pos
)
498 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
500 return list_get_nth_element(&input_dev_list
, pos
);
503 static void *input_devices_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
505 return list_get_next_element(&input_dev_list
, v
, pos
);
508 static void input_devices_seq_stop(struct seq_file
*seq
, void *v
)
510 /* release lock here */
513 static void input_seq_print_bitmap(struct seq_file
*seq
, const char *name
,
514 unsigned long *bitmap
, int max
)
518 for (i
= NBITS(max
) - 1; i
> 0; i
--)
522 seq_printf(seq
, "B: %s=", name
);
524 seq_printf(seq
, "%lx%s", bitmap
[i
], i
> 0 ? " " : "");
528 static int input_devices_seq_show(struct seq_file
*seq
, void *v
)
530 struct input_dev
*dev
= container_of(v
, struct input_dev
, node
);
531 const char *path
= kobject_get_path(&dev
->cdev
.kobj
, GFP_KERNEL
);
532 struct input_handle
*handle
;
534 seq_printf(seq
, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
535 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
);
537 seq_printf(seq
, "N: Name=\"%s\"\n", dev
->name
? dev
->name
: "");
538 seq_printf(seq
, "P: Phys=%s\n", dev
->phys
? dev
->phys
: "");
539 seq_printf(seq
, "S: Sysfs=%s\n", path
? path
: "");
540 seq_printf(seq
, "U: Uniq=%s\n", dev
->uniq
? dev
->uniq
: "");
541 seq_printf(seq
, "H: Handlers=");
543 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
544 seq_printf(seq
, "%s ", handle
->name
);
547 input_seq_print_bitmap(seq
, "EV", dev
->evbit
, EV_MAX
);
548 if (test_bit(EV_KEY
, dev
->evbit
))
549 input_seq_print_bitmap(seq
, "KEY", dev
->keybit
, KEY_MAX
);
550 if (test_bit(EV_REL
, dev
->evbit
))
551 input_seq_print_bitmap(seq
, "REL", dev
->relbit
, REL_MAX
);
552 if (test_bit(EV_ABS
, dev
->evbit
))
553 input_seq_print_bitmap(seq
, "ABS", dev
->absbit
, ABS_MAX
);
554 if (test_bit(EV_MSC
, dev
->evbit
))
555 input_seq_print_bitmap(seq
, "MSC", dev
->mscbit
, MSC_MAX
);
556 if (test_bit(EV_LED
, dev
->evbit
))
557 input_seq_print_bitmap(seq
, "LED", dev
->ledbit
, LED_MAX
);
558 if (test_bit(EV_SND
, dev
->evbit
))
559 input_seq_print_bitmap(seq
, "SND", dev
->sndbit
, SND_MAX
);
560 if (test_bit(EV_FF
, dev
->evbit
))
561 input_seq_print_bitmap(seq
, "FF", dev
->ffbit
, FF_MAX
);
562 if (test_bit(EV_SW
, dev
->evbit
))
563 input_seq_print_bitmap(seq
, "SW", dev
->swbit
, SW_MAX
);
571 static struct seq_operations input_devices_seq_ops
= {
572 .start
= input_devices_seq_start
,
573 .next
= input_devices_seq_next
,
574 .stop
= input_devices_seq_stop
,
575 .show
= input_devices_seq_show
,
578 static int input_proc_devices_open(struct inode
*inode
, struct file
*file
)
580 return seq_open(file
, &input_devices_seq_ops
);
583 static const struct file_operations input_devices_fileops
= {
584 .owner
= THIS_MODULE
,
585 .open
= input_proc_devices_open
,
586 .poll
= input_proc_devices_poll
,
589 .release
= seq_release
,
592 static void *input_handlers_seq_start(struct seq_file
*seq
, loff_t
*pos
)
594 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
595 seq
->private = (void *)(unsigned long)*pos
;
596 return list_get_nth_element(&input_handler_list
, pos
);
599 static void *input_handlers_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
601 seq
->private = (void *)(unsigned long)(*pos
+ 1);
602 return list_get_next_element(&input_handler_list
, v
, pos
);
605 static void input_handlers_seq_stop(struct seq_file
*seq
, void *v
)
607 /* release lock here */
610 static int input_handlers_seq_show(struct seq_file
*seq
, void *v
)
612 struct input_handler
*handler
= container_of(v
, struct input_handler
, node
);
614 seq_printf(seq
, "N: Number=%ld Name=%s",
615 (unsigned long)seq
->private, handler
->name
);
617 seq_printf(seq
, " Minor=%d", handler
->minor
);
622 static struct seq_operations input_handlers_seq_ops
= {
623 .start
= input_handlers_seq_start
,
624 .next
= input_handlers_seq_next
,
625 .stop
= input_handlers_seq_stop
,
626 .show
= input_handlers_seq_show
,
629 static int input_proc_handlers_open(struct inode
*inode
, struct file
*file
)
631 return seq_open(file
, &input_handlers_seq_ops
);
634 static const struct file_operations input_handlers_fileops
= {
635 .owner
= THIS_MODULE
,
636 .open
= input_proc_handlers_open
,
639 .release
= seq_release
,
642 static int __init
input_proc_init(void)
644 struct proc_dir_entry
*entry
;
646 proc_bus_input_dir
= proc_mkdir("input", proc_bus
);
647 if (!proc_bus_input_dir
)
650 proc_bus_input_dir
->owner
= THIS_MODULE
;
652 entry
= create_proc_entry("devices", 0, proc_bus_input_dir
);
656 entry
->owner
= THIS_MODULE
;
657 entry
->proc_fops
= &input_devices_fileops
;
659 entry
= create_proc_entry("handlers", 0, proc_bus_input_dir
);
663 entry
->owner
= THIS_MODULE
;
664 entry
->proc_fops
= &input_handlers_fileops
;
668 fail2
: remove_proc_entry("devices", proc_bus_input_dir
);
669 fail1
: remove_proc_entry("input", proc_bus
);
673 static void input_proc_exit(void)
675 remove_proc_entry("devices", proc_bus_input_dir
);
676 remove_proc_entry("handlers", proc_bus_input_dir
);
677 remove_proc_entry("input", proc_bus
);
680 #else /* !CONFIG_PROC_FS */
681 static inline void input_wakeup_procfs_readers(void) { }
682 static inline int input_proc_init(void) { return 0; }
683 static inline void input_proc_exit(void) { }
686 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
687 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
689 struct input_dev *input_dev = to_input_dev(dev); \
691 return scnprintf(buf, PAGE_SIZE, "%s\n", \
692 input_dev->name ? input_dev->name : ""); \
694 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
696 INPUT_DEV_STRING_ATTR_SHOW(name
);
697 INPUT_DEV_STRING_ATTR_SHOW(phys
);
698 INPUT_DEV_STRING_ATTR_SHOW(uniq
);
700 static int input_print_modalias_bits(char *buf
, int size
,
701 char name
, unsigned long *bm
,
702 unsigned int min_bit
, unsigned int max_bit
)
706 len
+= snprintf(buf
, max(size
, 0), "%c", name
);
707 for (i
= min_bit
; i
< max_bit
; i
++)
708 if (bm
[LONG(i
)] & BIT(i
))
709 len
+= snprintf(buf
+ len
, max(size
- len
, 0), "%X,", i
);
713 static int input_print_modalias(char *buf
, int size
, struct input_dev
*id
,
718 len
= snprintf(buf
, max(size
, 0),
719 "input:b%04Xv%04Xp%04Xe%04X-",
720 id
->id
.bustype
, id
->id
.vendor
,
721 id
->id
.product
, id
->id
.version
);
723 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
724 'e', id
->evbit
, 0, EV_MAX
);
725 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
726 'k', id
->keybit
, KEY_MIN_INTERESTING
, KEY_MAX
);
727 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
728 'r', id
->relbit
, 0, REL_MAX
);
729 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
730 'a', id
->absbit
, 0, ABS_MAX
);
731 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
732 'm', id
->mscbit
, 0, MSC_MAX
);
733 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
734 'l', id
->ledbit
, 0, LED_MAX
);
735 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
736 's', id
->sndbit
, 0, SND_MAX
);
737 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
738 'f', id
->ffbit
, 0, FF_MAX
);
739 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
740 'w', id
->swbit
, 0, SW_MAX
);
743 len
+= snprintf(buf
+ len
, max(size
- len
, 0), "\n");
748 static ssize_t
input_dev_show_modalias(struct class_device
*dev
, char *buf
)
750 struct input_dev
*id
= to_input_dev(dev
);
753 len
= input_print_modalias(buf
, PAGE_SIZE
, id
, 1);
755 return min_t(int, len
, PAGE_SIZE
);
757 static CLASS_DEVICE_ATTR(modalias
, S_IRUGO
, input_dev_show_modalias
, NULL
);
759 static struct attribute
*input_dev_attrs
[] = {
760 &class_device_attr_name
.attr
,
761 &class_device_attr_phys
.attr
,
762 &class_device_attr_uniq
.attr
,
763 &class_device_attr_modalias
.attr
,
767 static struct attribute_group input_dev_attr_group
= {
768 .attrs
= input_dev_attrs
,
771 #define INPUT_DEV_ID_ATTR(name) \
772 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
774 struct input_dev *input_dev = to_input_dev(dev); \
775 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
777 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
779 INPUT_DEV_ID_ATTR(bustype
);
780 INPUT_DEV_ID_ATTR(vendor
);
781 INPUT_DEV_ID_ATTR(product
);
782 INPUT_DEV_ID_ATTR(version
);
784 static struct attribute
*input_dev_id_attrs
[] = {
785 &class_device_attr_bustype
.attr
,
786 &class_device_attr_vendor
.attr
,
787 &class_device_attr_product
.attr
,
788 &class_device_attr_version
.attr
,
792 static struct attribute_group input_dev_id_attr_group
= {
794 .attrs
= input_dev_id_attrs
,
797 static int input_print_bitmap(char *buf
, int buf_size
, unsigned long *bitmap
,
803 for (i
= NBITS(max
) - 1; i
> 0; i
--)
808 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0),
809 "%lx%s", bitmap
[i
], i
> 0 ? " " : "");
812 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0), "\n");
817 #define INPUT_DEV_CAP_ATTR(ev, bm) \
818 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
820 struct input_dev *input_dev = to_input_dev(dev); \
821 int len = input_print_bitmap(buf, PAGE_SIZE, \
822 input_dev->bm##bit, ev##_MAX, 1); \
823 return min_t(int, len, PAGE_SIZE); \
825 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
827 INPUT_DEV_CAP_ATTR(EV
, ev
);
828 INPUT_DEV_CAP_ATTR(KEY
, key
);
829 INPUT_DEV_CAP_ATTR(REL
, rel
);
830 INPUT_DEV_CAP_ATTR(ABS
, abs
);
831 INPUT_DEV_CAP_ATTR(MSC
, msc
);
832 INPUT_DEV_CAP_ATTR(LED
, led
);
833 INPUT_DEV_CAP_ATTR(SND
, snd
);
834 INPUT_DEV_CAP_ATTR(FF
, ff
);
835 INPUT_DEV_CAP_ATTR(SW
, sw
);
837 static struct attribute
*input_dev_caps_attrs
[] = {
838 &class_device_attr_ev
.attr
,
839 &class_device_attr_key
.attr
,
840 &class_device_attr_rel
.attr
,
841 &class_device_attr_abs
.attr
,
842 &class_device_attr_msc
.attr
,
843 &class_device_attr_led
.attr
,
844 &class_device_attr_snd
.attr
,
845 &class_device_attr_ff
.attr
,
846 &class_device_attr_sw
.attr
,
850 static struct attribute_group input_dev_caps_attr_group
= {
851 .name
= "capabilities",
852 .attrs
= input_dev_caps_attrs
,
855 static struct attribute_group
*input_dev_attr_groups
[] = {
856 &input_dev_attr_group
,
857 &input_dev_id_attr_group
,
858 &input_dev_caps_attr_group
,
862 static void input_dev_release(struct class_device
*class_dev
)
864 struct input_dev
*dev
= to_input_dev(class_dev
);
866 input_ff_destroy(dev
);
869 module_put(THIS_MODULE
);
873 * Input uevent interface - loading event handlers based on
876 static int input_add_uevent_bm_var(char **envp
, int num_envp
, int *cur_index
,
877 char *buffer
, int buffer_size
, int *cur_len
,
878 const char *name
, unsigned long *bitmap
, int max
)
880 if (*cur_index
>= num_envp
- 1)
883 envp
[*cur_index
] = buffer
+ *cur_len
;
885 *cur_len
+= snprintf(buffer
+ *cur_len
, max(buffer_size
- *cur_len
, 0), name
);
886 if (*cur_len
>= buffer_size
)
889 *cur_len
+= input_print_bitmap(buffer
+ *cur_len
,
890 max(buffer_size
- *cur_len
, 0),
892 if (*cur_len
> buffer_size
)
899 static int input_add_uevent_modalias_var(char **envp
, int num_envp
, int *cur_index
,
900 char *buffer
, int buffer_size
, int *cur_len
,
901 struct input_dev
*dev
)
903 if (*cur_index
>= num_envp
- 1)
906 envp
[*cur_index
] = buffer
+ *cur_len
;
908 *cur_len
+= snprintf(buffer
+ *cur_len
, max(buffer_size
- *cur_len
, 0),
910 if (*cur_len
>= buffer_size
)
913 *cur_len
+= input_print_modalias(buffer
+ *cur_len
,
914 max(buffer_size
- *cur_len
, 0),
916 if (*cur_len
> buffer_size
)
923 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
925 int err = add_uevent_var(envp, num_envp, &i, \
926 buffer, buffer_size, &len, \
932 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
934 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
935 buffer, buffer_size, &len, \
941 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
943 int err = input_add_uevent_modalias_var(envp, \
945 buffer, buffer_size, &len, \
951 static int input_dev_uevent(struct class_device
*cdev
, char **envp
,
952 int num_envp
, char *buffer
, int buffer_size
)
954 struct input_dev
*dev
= to_input_dev(cdev
);
958 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
959 dev
->id
.bustype
, dev
->id
.vendor
,
960 dev
->id
.product
, dev
->id
.version
);
962 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev
->name
);
964 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev
->phys
);
966 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev
->uniq
);
968 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev
->evbit
, EV_MAX
);
969 if (test_bit(EV_KEY
, dev
->evbit
))
970 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev
->keybit
, KEY_MAX
);
971 if (test_bit(EV_REL
, dev
->evbit
))
972 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev
->relbit
, REL_MAX
);
973 if (test_bit(EV_ABS
, dev
->evbit
))
974 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev
->absbit
, ABS_MAX
);
975 if (test_bit(EV_MSC
, dev
->evbit
))
976 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev
->mscbit
, MSC_MAX
);
977 if (test_bit(EV_LED
, dev
->evbit
))
978 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev
->ledbit
, LED_MAX
);
979 if (test_bit(EV_SND
, dev
->evbit
))
980 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev
->sndbit
, SND_MAX
);
981 if (test_bit(EV_FF
, dev
->evbit
))
982 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev
->ffbit
, FF_MAX
);
983 if (test_bit(EV_SW
, dev
->evbit
))
984 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev
->swbit
, SW_MAX
);
986 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev
);
992 struct class input_class
= {
994 .release
= input_dev_release
,
995 .uevent
= input_dev_uevent
,
997 EXPORT_SYMBOL_GPL(input_class
);
1000 * input_allocate_device - allocate memory for new input device
1002 * Returns prepared struct input_dev or NULL.
1004 * NOTE: Use input_free_device() to free devices that have not been
1005 * registered; input_unregister_device() should be used for already
1006 * registered devices.
1008 struct input_dev
*input_allocate_device(void)
1010 struct input_dev
*dev
;
1012 dev
= kzalloc(sizeof(struct input_dev
), GFP_KERNEL
);
1014 dev
->cdev
.class = &input_class
;
1015 dev
->cdev
.groups
= input_dev_attr_groups
;
1016 class_device_initialize(&dev
->cdev
);
1017 mutex_init(&dev
->mutex
);
1018 INIT_LIST_HEAD(&dev
->h_list
);
1019 INIT_LIST_HEAD(&dev
->node
);
1021 __module_get(THIS_MODULE
);
1026 EXPORT_SYMBOL(input_allocate_device
);
1029 * input_free_device - free memory occupied by input_dev structure
1030 * @dev: input device to free
1032 * This function should only be used if input_register_device()
1033 * was not called yet or if it failed. Once device was registered
1034 * use input_unregister_device() and memory will be freed once last
1035 * refrence to the device is dropped.
1037 * Device should be allocated by input_allocate_device().
1039 * NOTE: If there are references to the input device then memory
1040 * will not be freed until last reference is dropped.
1042 void input_free_device(struct input_dev
*dev
)
1045 input_put_device(dev
);
1047 EXPORT_SYMBOL(input_free_device
);
1050 * input_set_capability - mark device as capable of a certain event
1051 * @dev: device that is capable of emitting or accepting event
1052 * @type: type of the event (EV_KEY, EV_REL, etc...)
1055 * In addition to setting up corresponding bit in appropriate capability
1056 * bitmap the function also adjusts dev->evbit.
1058 void input_set_capability(struct input_dev
*dev
, unsigned int type
, unsigned int code
)
1062 __set_bit(code
, dev
->keybit
);
1066 __set_bit(code
, dev
->relbit
);
1070 __set_bit(code
, dev
->absbit
);
1074 __set_bit(code
, dev
->mscbit
);
1078 __set_bit(code
, dev
->swbit
);
1082 __set_bit(code
, dev
->ledbit
);
1086 __set_bit(code
, dev
->sndbit
);
1090 __set_bit(code
, dev
->ffbit
);
1095 "input_set_capability: unknown type %u (code %u)\n",
1101 __set_bit(type
, dev
->evbit
);
1103 EXPORT_SYMBOL(input_set_capability
);
1105 int input_register_device(struct input_dev
*dev
)
1107 static atomic_t input_no
= ATOMIC_INIT(0);
1108 struct input_handler
*handler
;
1112 set_bit(EV_SYN
, dev
->evbit
);
1115 * If delay and period are pre-set by the driver, then autorepeating
1116 * is handled by the driver itself and we don't do it in input.c.
1119 init_timer(&dev
->timer
);
1120 if (!dev
->rep
[REP_DELAY
] && !dev
->rep
[REP_PERIOD
]) {
1121 dev
->timer
.data
= (long) dev
;
1122 dev
->timer
.function
= input_repeat_key
;
1123 dev
->rep
[REP_DELAY
] = 250;
1124 dev
->rep
[REP_PERIOD
] = 33;
1127 if (!dev
->getkeycode
)
1128 dev
->getkeycode
= input_default_getkeycode
;
1130 if (!dev
->setkeycode
)
1131 dev
->setkeycode
= input_default_setkeycode
;
1133 list_add_tail(&dev
->node
, &input_dev_list
);
1135 snprintf(dev
->cdev
.class_id
, sizeof(dev
->cdev
.class_id
),
1136 "input%ld", (unsigned long) atomic_inc_return(&input_no
) - 1);
1139 dev
->cdev
.dev
= dev
->dev
.parent
;
1141 error
= class_device_add(&dev
->cdev
);
1145 path
= kobject_get_path(&dev
->cdev
.kobj
, GFP_KERNEL
);
1146 printk(KERN_INFO
"input: %s as %s\n",
1147 dev
->name
? dev
->name
: "Unspecified device", path
? path
: "N/A");
1150 list_for_each_entry(handler
, &input_handler_list
, node
)
1151 input_attach_handler(dev
, handler
);
1153 input_wakeup_procfs_readers();
1157 EXPORT_SYMBOL(input_register_device
);
1159 void input_unregister_device(struct input_dev
*dev
)
1161 struct input_handle
*handle
, *next
;
1164 for (code
= 0; code
<= KEY_MAX
; code
++)
1165 if (test_bit(code
, dev
->key
))
1166 input_report_key(dev
, code
, 0);
1169 del_timer_sync(&dev
->timer
);
1171 list_for_each_entry_safe(handle
, next
, &dev
->h_list
, d_node
)
1172 handle
->handler
->disconnect(handle
);
1173 WARN_ON(!list_empty(&dev
->h_list
));
1175 list_del_init(&dev
->node
);
1177 class_device_unregister(&dev
->cdev
);
1179 input_wakeup_procfs_readers();
1181 EXPORT_SYMBOL(input_unregister_device
);
1183 int input_register_handler(struct input_handler
*handler
)
1185 struct input_dev
*dev
;
1187 INIT_LIST_HEAD(&handler
->h_list
);
1189 if (handler
->fops
!= NULL
) {
1190 if (input_table
[handler
->minor
>> 5])
1193 input_table
[handler
->minor
>> 5] = handler
;
1196 list_add_tail(&handler
->node
, &input_handler_list
);
1198 list_for_each_entry(dev
, &input_dev_list
, node
)
1199 input_attach_handler(dev
, handler
);
1201 input_wakeup_procfs_readers();
1204 EXPORT_SYMBOL(input_register_handler
);
1206 void input_unregister_handler(struct input_handler
*handler
)
1208 struct input_handle
*handle
, *next
;
1210 list_for_each_entry_safe(handle
, next
, &handler
->h_list
, h_node
)
1211 handler
->disconnect(handle
);
1212 WARN_ON(!list_empty(&handler
->h_list
));
1214 list_del_init(&handler
->node
);
1216 if (handler
->fops
!= NULL
)
1217 input_table
[handler
->minor
>> 5] = NULL
;
1219 input_wakeup_procfs_readers();
1221 EXPORT_SYMBOL(input_unregister_handler
);
1223 int input_register_handle(struct input_handle
*handle
)
1225 struct input_handler
*handler
= handle
->handler
;
1227 list_add_tail(&handle
->d_node
, &handle
->dev
->h_list
);
1228 list_add_tail(&handle
->h_node
, &handler
->h_list
);
1231 handler
->start(handle
);
1235 EXPORT_SYMBOL(input_register_handle
);
1237 void input_unregister_handle(struct input_handle
*handle
)
1239 list_del_init(&handle
->h_node
);
1240 list_del_init(&handle
->d_node
);
1242 EXPORT_SYMBOL(input_unregister_handle
);
1244 static int input_open_file(struct inode
*inode
, struct file
*file
)
1246 struct input_handler
*handler
= input_table
[iminor(inode
) >> 5];
1247 const struct file_operations
*old_fops
, *new_fops
= NULL
;
1250 /* No load-on-demand here? */
1251 if (!handler
|| !(new_fops
= fops_get(handler
->fops
)))
1255 * That's _really_ odd. Usually NULL ->open means "nothing special",
1256 * not "no device". Oh, well...
1258 if (!new_fops
->open
) {
1262 old_fops
= file
->f_op
;
1263 file
->f_op
= new_fops
;
1265 err
= new_fops
->open(inode
, file
);
1268 fops_put(file
->f_op
);
1269 file
->f_op
= fops_get(old_fops
);
1275 static const struct file_operations input_fops
= {
1276 .owner
= THIS_MODULE
,
1277 .open
= input_open_file
,
1280 static int __init
input_init(void)
1284 err
= class_register(&input_class
);
1286 printk(KERN_ERR
"input: unable to register input_dev class\n");
1290 err
= input_proc_init();
1294 err
= register_chrdev(INPUT_MAJOR
, "input", &input_fops
);
1296 printk(KERN_ERR
"input: unable to register char major %d", INPUT_MAJOR
);
1302 fail2
: input_proc_exit();
1303 fail1
: class_unregister(&input_class
);
1307 static void __exit
input_exit(void)
1310 unregister_chrdev(INPUT_MAJOR
, "input");
1311 class_unregister(&input_class
);
1314 subsys_initcall(input_init
);
1315 module_exit(input_exit
);