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/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>
29 MODULE_DESCRIPTION("Input core");
30 MODULE_LICENSE("GPL");
32 #define INPUT_DEVICES 256
35 * EV_ABS events which should not be cached are listed here.
37 static unsigned int input_abs_bypass_init_data[] __initdata = {
50 static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
52 static LIST_HEAD(input_dev_list);
53 static LIST_HEAD(input_handler_list);
56 * input_mutex protects access to both input_dev_list and input_handler_list.
57 * This also causes input_[un]register_device and input_[un]register_handler
58 * be mutually exclusive which simplifies locking in drivers implementing
61 static DEFINE_MUTEX(input_mutex);
63 static struct input_handler *input_table[8];
65 static inline int is_event_supported(unsigned int code,
66 unsigned long *bm, unsigned int max)
68 return code <= max && test_bit(code, bm);
71 static int input_defuzz_abs_event(int value, int old_val, int fuzz)
74 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
77 if (value > old_val - fuzz && value < old_val + fuzz)
78 return (old_val * 3 + value) / 4;
80 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
81 return (old_val + value) / 2;
88 * Pass event through all open handles. This function is called with
89 * dev->event_lock held and interrupts disabled.
91 static void input_pass_event(struct input_dev *dev,
92 unsigned int type, unsigned int code, int value)
94 struct input_handle *handle;
98 handle = rcu_dereference(dev->grab);
100 handle->handler->event(handle, type, code, value);
102 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
104 handle->handler->event(handle,
110 * Generate software autorepeat event. Note that we take
111 * dev->event_lock here to avoid racing with input_event
112 * which may cause keys get "stuck".
114 static void input_repeat_key(unsigned long data)
116 struct input_dev *dev = (void *) data;
119 spin_lock_irqsave(&dev->event_lock, flags);
121 if (test_bit(dev->repeat_key, dev->key) &&
122 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
124 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
128 * Only send SYN_REPORT if we are not in a middle
129 * of driver parsing a new hardware packet.
130 * Otherwise assume that the driver will send
131 * SYN_REPORT once it's done.
133 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
136 if (dev->rep[REP_PERIOD])
137 mod_timer(&dev->timer, jiffies +
138 msecs_to_jiffies(dev->rep[REP_PERIOD]));
141 spin_unlock_irqrestore(&dev->event_lock, flags);
144 static void input_start_autorepeat(struct input_dev *dev, int code)
146 if (test_bit(EV_REP, dev->evbit) &&
147 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
149 dev->repeat_key = code;
150 mod_timer(&dev->timer,
151 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
155 static void input_stop_autorepeat(struct input_dev *dev)
157 del_timer(&dev->timer);
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)
165 static void input_handle_event(struct input_dev *dev,
166 unsigned int type, unsigned int code, int value)
168 int disposition = INPUT_IGNORE_EVENT;
175 disposition = INPUT_PASS_TO_ALL;
181 disposition = INPUT_PASS_TO_HANDLERS;
186 disposition = INPUT_PASS_TO_HANDLERS;
192 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
193 !!test_bit(code, dev->key) != value) {
196 __change_bit(code, dev->key);
198 input_start_autorepeat(dev, code);
200 input_stop_autorepeat(dev);
203 disposition = INPUT_PASS_TO_HANDLERS;
208 if (is_event_supported(code, dev->swbit, SW_MAX) &&
209 !!test_bit(code, dev->sw) != value) {
211 __change_bit(code, dev->sw);
212 disposition = INPUT_PASS_TO_HANDLERS;
217 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
219 if (test_bit(code, input_abs_bypass)) {
220 disposition = INPUT_PASS_TO_HANDLERS;
224 value = input_defuzz_abs_event(value,
225 dev->abs[code], dev->absfuzz[code]);
227 if (dev->abs[code] != value) {
228 dev->abs[code] = value;
229 disposition = INPUT_PASS_TO_HANDLERS;
235 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
236 disposition = INPUT_PASS_TO_HANDLERS;
241 if (is_event_supported(code, dev->mscbit, MSC_MAX))
242 disposition = INPUT_PASS_TO_ALL;
247 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
248 !!test_bit(code, dev->led) != value) {
250 __change_bit(code, dev->led);
251 disposition = INPUT_PASS_TO_ALL;
256 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
258 if (!!test_bit(code, dev->snd) != !!value)
259 __change_bit(code, dev->snd);
260 disposition = INPUT_PASS_TO_ALL;
265 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
266 dev->rep[code] = value;
267 disposition = INPUT_PASS_TO_ALL;
273 disposition = INPUT_PASS_TO_ALL;
277 disposition = INPUT_PASS_TO_ALL;
281 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
284 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
285 dev->event(dev, type, code, value);
287 if (disposition & INPUT_PASS_TO_HANDLERS)
288 input_pass_event(dev, type, code, value);
292 * input_event() - report new input event
293 * @dev: device that generated the event
294 * @type: type of the event
296 * @value: value of the event
298 * This function should be used by drivers implementing various input
299 * devices. See also input_inject_event().
302 void input_event(struct input_dev *dev,
303 unsigned int type, unsigned int code, int value)
307 if (is_event_supported(type, dev->evbit, EV_MAX)) {
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);
315 EXPORT_SYMBOL(input_event);
318 * input_inject_event() - send input event from input handler
319 * @handle: input handle to send event through
320 * @type: type of the event
322 * @value: value of the event
324 * Similar to input_event() but will ignore event if device is
325 * "grabbed" and handle injecting event is not the one that owns
328 void input_inject_event(struct input_handle *handle,
329 unsigned int type, unsigned int code, int value)
331 struct input_dev *dev = handle->dev;
332 struct input_handle *grab;
335 if (is_event_supported(type, dev->evbit, EV_MAX)) {
336 spin_lock_irqsave(&dev->event_lock, flags);
339 grab = rcu_dereference(dev->grab);
340 if (!grab || grab == handle)
341 input_handle_event(dev, type, code, value);
344 spin_unlock_irqrestore(&dev->event_lock, flags);
347 EXPORT_SYMBOL(input_inject_event);
350 * input_grab_device - grabs device for exclusive use
351 * @handle: input handle that wants to own the device
353 * When a device is grabbed by an input handle all events generated by
354 * the device are delivered only to this handle. Also events injected
355 * by other input handles are ignored while device is grabbed.
357 int input_grab_device(struct input_handle *handle)
359 struct input_dev *dev = handle->dev;
362 retval = mutex_lock_interruptible(&dev->mutex);
371 rcu_assign_pointer(dev->grab, handle);
375 mutex_unlock(&dev->mutex);
378 EXPORT_SYMBOL(input_grab_device);
380 static void __input_release_device(struct input_handle *handle)
382 struct input_dev *dev = handle->dev;
384 if (dev->grab == handle) {
385 rcu_assign_pointer(dev->grab, NULL);
386 /* Make sure input_pass_event() notices that grab is gone */
389 list_for_each_entry(handle, &dev->h_list, d_node)
390 if (handle->open && handle->handler->start)
391 handle->handler->start(handle);
396 * input_release_device - release previously grabbed device
397 * @handle: input handle that owns the device
399 * Releases previously grabbed device so that other input handles can
400 * start receiving input events. Upon release all handlers attached
401 * to the device have their start() method called so they have a change
402 * to synchronize device state with the rest of the system.
404 void input_release_device(struct input_handle *handle)
406 struct input_dev *dev = handle->dev;
408 mutex_lock(&dev->mutex);
409 __input_release_device(handle);
410 mutex_unlock(&dev->mutex);
412 EXPORT_SYMBOL(input_release_device);
415 * input_open_device - open input device
416 * @handle: handle through which device is being accessed
418 * This function should be called by input handlers when they
419 * want to start receive events from given input device.
421 int input_open_device(struct input_handle *handle)
423 struct input_dev *dev = handle->dev;
426 retval = mutex_lock_interruptible(&dev->mutex);
430 if (dev->going_away) {
437 if (!dev->users++ && dev->open)
438 retval = dev->open(dev);
442 if (!--handle->open) {
444 * Make sure we are not delivering any more events
445 * through this handle
452 mutex_unlock(&dev->mutex);
455 EXPORT_SYMBOL(input_open_device);
457 int input_flush_device(struct input_handle *handle, struct file *file)
459 struct input_dev *dev = handle->dev;
462 retval = mutex_lock_interruptible(&dev->mutex);
467 retval = dev->flush(dev, file);
469 mutex_unlock(&dev->mutex);
472 EXPORT_SYMBOL(input_flush_device);
475 * input_close_device - close input device
476 * @handle: handle through which device is being accessed
478 * This function should be called by input handlers when they
479 * want to stop receive events from given input device.
481 void input_close_device(struct input_handle *handle)
483 struct input_dev *dev = handle->dev;
485 mutex_lock(&dev->mutex);
487 __input_release_device(handle);
489 if (!--dev->users && dev->close)
492 if (!--handle->open) {
494 * synchronize_rcu() makes sure that input_pass_event()
495 * completed and that no more input events are delivered
496 * through this handle
501 mutex_unlock(&dev->mutex);
503 EXPORT_SYMBOL(input_close_device);
506 * Prepare device for unregistering
508 static void input_disconnect_device(struct input_dev *dev)
510 struct input_handle *handle;
514 * Mark device as going away. Note that we take dev->mutex here
515 * not to protect access to dev->going_away but rather to ensure
516 * that there are no threads in the middle of input_open_device()
518 mutex_lock(&dev->mutex);
519 dev->going_away = true;
520 mutex_unlock(&dev->mutex);
522 spin_lock_irq(&dev->event_lock);
525 * Simulate keyup events for all pressed keys so that handlers
526 * are not left with "stuck" keys. The driver may continue
527 * generate events even after we done here but they will not
528 * reach any handlers.
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);
537 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
540 list_for_each_entry(handle, &dev->h_list, d_node)
543 spin_unlock_irq(&dev->event_lock);
546 static int input_fetch_keycode(struct input_dev *dev, int scancode)
548 switch (dev->keycodesize) {
550 return ((u8 *)dev->keycode)[scancode];
553 return ((u16 *)dev->keycode)[scancode];
556 return ((u32 *)dev->keycode)[scancode];
560 static int input_default_getkeycode(struct input_dev *dev,
561 int scancode, int *keycode)
563 if (!dev->keycodesize)
566 if (scancode >= dev->keycodemax)
569 *keycode = input_fetch_keycode(dev, scancode);
574 static int input_default_setkeycode(struct input_dev *dev,
575 int scancode, int keycode)
580 if (scancode >= dev->keycodemax)
583 if (!dev->keycodesize)
586 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
589 switch (dev->keycodesize) {
591 u8 *k = (u8 *)dev->keycode;
592 old_keycode = k[scancode];
593 k[scancode] = keycode;
597 u16 *k = (u16 *)dev->keycode;
598 old_keycode = k[scancode];
599 k[scancode] = keycode;
603 u32 *k = (u32 *)dev->keycode;
604 old_keycode = k[scancode];
605 k[scancode] = keycode;
610 clear_bit(old_keycode, dev->keybit);
611 set_bit(keycode, dev->keybit);
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; /* Setting the bit twice is useless, so break */
624 * input_get_keycode - retrieve keycode currently mapped to a given scancode
625 * @dev: input device which keymap is being queried
626 * @scancode: scancode (or its equivalent for device in question) for which
630 * This function should be called by anyone interested in retrieving current
631 * keymap. Presently keyboard and evdev handlers use it.
633 int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
638 return dev->getkeycode(dev, scancode, keycode);
640 EXPORT_SYMBOL(input_get_keycode);
643 * input_get_keycode - assign new keycode to a given scancode
644 * @dev: input device which keymap is being updated
645 * @scancode: scancode (or its equivalent for device in question)
646 * @keycode: new keycode to be assigned to the scancode
648 * This function should be called by anyone needing to update current
649 * keymap. Presently keyboard and evdev handlers use it.
651 int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
660 if (keycode < 0 || keycode > KEY_MAX)
663 spin_lock_irqsave(&dev->event_lock, flags);
665 retval = dev->getkeycode(dev, scancode, &old_keycode);
669 retval = dev->setkeycode(dev, scancode, keycode);
674 * Simulate keyup event if keycode is not present
675 * in the keymap anymore
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)) {
681 input_pass_event(dev, EV_KEY, old_keycode, 0);
683 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
687 spin_unlock_irqrestore(&dev->event_lock, flags);
691 EXPORT_SYMBOL(input_set_keycode);
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]) \
697 if (i != BITS_TO_LONGS(max)) \
700 static const struct input_device_id *input_match_device(const struct input_device_id *id,
701 struct input_dev *dev)
705 for (; id->flags || id->driver_info; id++) {
707 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
708 if (id->bustype != dev->id.bustype)
711 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
712 if (id->vendor != dev->id.vendor)
715 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
716 if (id->product != dev->id.product)
719 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
720 if (id->version != dev->id.version)
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);
739 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
741 const struct input_device_id *id;
744 if (handler->blacklist && input_match_device(handler->blacklist, dev))
747 id = input_match_device(handler->id_table, dev);
751 error = handler->connect(handler, dev, id);
752 if (error && error != -ENODEV)
754 "input: failed to attach handler %s to device %s, "
756 handler->name, kobject_name(&dev->dev.kobj), error);
762 #ifdef CONFIG_PROC_FS
764 static struct proc_dir_entry *proc_bus_input_dir;
765 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
766 static int input_devices_state;
768 static inline void input_wakeup_procfs_readers(void)
770 input_devices_state++;
771 wake_up(&input_devices_poll_wait);
774 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
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;
785 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
787 if (mutex_lock_interruptible(&input_mutex))
790 return seq_list_start(&input_dev_list, *pos);
793 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
795 return seq_list_next(v, &input_dev_list, pos);
798 static void input_devices_seq_stop(struct seq_file *seq, void *v)
800 mutex_unlock(&input_mutex);
803 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
804 unsigned long *bitmap, int max)
808 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
812 seq_printf(seq, "B: %s=", name);
814 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
818 static int input_devices_seq_show(struct seq_file *seq, void *v)
820 struct input_dev *dev = container_of(v, struct input_dev, node);
821 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
822 struct input_handle *handle;
824 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
825 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
827 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
828 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
829 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
830 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
831 seq_printf(seq, "H: Handlers=");
833 list_for_each_entry(handle, &dev->h_list, d_node)
834 seq_printf(seq, "%s ", handle->name);
837 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
838 if (test_bit(EV_KEY, dev->evbit))
839 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
840 if (test_bit(EV_REL, dev->evbit))
841 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
842 if (test_bit(EV_ABS, dev->evbit))
843 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
844 if (test_bit(EV_MSC, dev->evbit))
845 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
846 if (test_bit(EV_LED, dev->evbit))
847 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
848 if (test_bit(EV_SND, dev->evbit))
849 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
850 if (test_bit(EV_FF, dev->evbit))
851 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
852 if (test_bit(EV_SW, dev->evbit))
853 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
861 static const struct seq_operations input_devices_seq_ops = {
862 .start = input_devices_seq_start,
863 .next = input_devices_seq_next,
864 .stop = input_devices_seq_stop,
865 .show = input_devices_seq_show,
868 static int input_proc_devices_open(struct inode *inode, struct file *file)
870 return seq_open(file, &input_devices_seq_ops);
873 static const struct file_operations input_devices_fileops = {
874 .owner = THIS_MODULE,
875 .open = input_proc_devices_open,
876 .poll = input_proc_devices_poll,
879 .release = seq_release,
882 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
884 if (mutex_lock_interruptible(&input_mutex))
887 seq->private = (void *)(unsigned long)*pos;
888 return seq_list_start(&input_handler_list, *pos);
891 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
893 seq->private = (void *)(unsigned long)(*pos + 1);
894 return seq_list_next(v, &input_handler_list, pos);
897 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
899 mutex_unlock(&input_mutex);
902 static int input_handlers_seq_show(struct seq_file *seq, void *v)
904 struct input_handler *handler = container_of(v, struct input_handler, node);
906 seq_printf(seq, "N: Number=%ld Name=%s",
907 (unsigned long)seq->private, handler->name);
909 seq_printf(seq, " Minor=%d", handler->minor);
914 static const struct seq_operations input_handlers_seq_ops = {
915 .start = input_handlers_seq_start,
916 .next = input_handlers_seq_next,
917 .stop = input_handlers_seq_stop,
918 .show = input_handlers_seq_show,
921 static int input_proc_handlers_open(struct inode *inode, struct file *file)
923 return seq_open(file, &input_handlers_seq_ops);
926 static const struct file_operations input_handlers_fileops = {
927 .owner = THIS_MODULE,
928 .open = input_proc_handlers_open,
931 .release = seq_release,
934 static int __init input_proc_init(void)
936 struct proc_dir_entry *entry;
938 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
939 if (!proc_bus_input_dir)
942 entry = proc_create("devices", 0, proc_bus_input_dir,
943 &input_devices_fileops);
947 entry = proc_create("handlers", 0, proc_bus_input_dir,
948 &input_handlers_fileops);
954 fail2: remove_proc_entry("devices", proc_bus_input_dir);
955 fail1: remove_proc_entry("bus/input", NULL);
959 static void input_proc_exit(void)
961 remove_proc_entry("devices", proc_bus_input_dir);
962 remove_proc_entry("handlers", proc_bus_input_dir);
963 remove_proc_entry("bus/input", NULL);
966 #else /* !CONFIG_PROC_FS */
967 static inline void input_wakeup_procfs_readers(void) { }
968 static inline int input_proc_init(void) { return 0; }
969 static inline void input_proc_exit(void) { }
972 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
973 static ssize_t input_dev_show_##name(struct device *dev, \
974 struct device_attribute *attr, \
977 struct input_dev *input_dev = to_input_dev(dev); \
979 return scnprintf(buf, PAGE_SIZE, "%s\n", \
980 input_dev->name ? input_dev->name : ""); \
982 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
984 INPUT_DEV_STRING_ATTR_SHOW(name);
985 INPUT_DEV_STRING_ATTR_SHOW(phys);
986 INPUT_DEV_STRING_ATTR_SHOW(uniq);
988 static int input_print_modalias_bits(char *buf, int size,
989 char name, unsigned long *bm,
990 unsigned int min_bit, unsigned int max_bit)
994 len += snprintf(buf, max(size, 0), "%c", name);
995 for (i = min_bit; i < max_bit; i++)
996 if (bm[BIT_WORD(i)] & BIT_MASK(i))
997 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1001 static int input_print_modalias(char *buf, int size, struct input_dev *id,
1006 len = snprintf(buf, max(size, 0),
1007 "input:b%04Xv%04Xp%04Xe%04X-",
1008 id->id.bustype, id->id.vendor,
1009 id->id.product, id->id.version);
1011 len += input_print_modalias_bits(buf + len, size - len,
1012 'e', id->evbit, 0, EV_MAX);
1013 len += input_print_modalias_bits(buf + len, size - len,
1014 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1015 len += input_print_modalias_bits(buf + len, size - len,
1016 'r', id->relbit, 0, REL_MAX);
1017 len += input_print_modalias_bits(buf + len, size - len,
1018 'a', id->absbit, 0, ABS_MAX);
1019 len += input_print_modalias_bits(buf + len, size - len,
1020 'm', id->mscbit, 0, MSC_MAX);
1021 len += input_print_modalias_bits(buf + len, size - len,
1022 'l', id->ledbit, 0, LED_MAX);
1023 len += input_print_modalias_bits(buf + len, size - len,
1024 's', id->sndbit, 0, SND_MAX);
1025 len += input_print_modalias_bits(buf + len, size - len,
1026 'f', id->ffbit, 0, FF_MAX);
1027 len += input_print_modalias_bits(buf + len, size - len,
1028 'w', id->swbit, 0, SW_MAX);
1031 len += snprintf(buf + len, max(size - len, 0), "\n");
1036 static ssize_t input_dev_show_modalias(struct device *dev,
1037 struct device_attribute *attr,
1040 struct input_dev *id = to_input_dev(dev);
1043 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1045 return min_t(int, len, PAGE_SIZE);
1047 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1049 static struct attribute *input_dev_attrs[] = {
1050 &dev_attr_name.attr,
1051 &dev_attr_phys.attr,
1052 &dev_attr_uniq.attr,
1053 &dev_attr_modalias.attr,
1057 static struct attribute_group input_dev_attr_group = {
1058 .attrs = input_dev_attrs,
1061 #define INPUT_DEV_ID_ATTR(name) \
1062 static ssize_t input_dev_show_id_##name(struct device *dev, \
1063 struct device_attribute *attr, \
1066 struct input_dev *input_dev = to_input_dev(dev); \
1067 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1069 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1071 INPUT_DEV_ID_ATTR(bustype);
1072 INPUT_DEV_ID_ATTR(vendor);
1073 INPUT_DEV_ID_ATTR(product);
1074 INPUT_DEV_ID_ATTR(version);
1076 static struct attribute *input_dev_id_attrs[] = {
1077 &dev_attr_bustype.attr,
1078 &dev_attr_vendor.attr,
1079 &dev_attr_product.attr,
1080 &dev_attr_version.attr,
1084 static struct attribute_group input_dev_id_attr_group = {
1086 .attrs = input_dev_id_attrs,
1089 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1090 int max, int add_cr)
1095 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
1100 len += snprintf(buf + len, max(buf_size - len, 0),
1101 "%lx%s", bitmap[i], i > 0 ? " " : "");
1104 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1109 #define INPUT_DEV_CAP_ATTR(ev, bm) \
1110 static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1111 struct device_attribute *attr, \
1114 struct input_dev *input_dev = to_input_dev(dev); \
1115 int len = input_print_bitmap(buf, PAGE_SIZE, \
1116 input_dev->bm##bit, ev##_MAX, 1); \
1117 return min_t(int, len, PAGE_SIZE); \
1119 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1121 INPUT_DEV_CAP_ATTR(EV, ev);
1122 INPUT_DEV_CAP_ATTR(KEY, key);
1123 INPUT_DEV_CAP_ATTR(REL, rel);
1124 INPUT_DEV_CAP_ATTR(ABS, abs);
1125 INPUT_DEV_CAP_ATTR(MSC, msc);
1126 INPUT_DEV_CAP_ATTR(LED, led);
1127 INPUT_DEV_CAP_ATTR(SND, snd);
1128 INPUT_DEV_CAP_ATTR(FF, ff);
1129 INPUT_DEV_CAP_ATTR(SW, sw);
1131 static struct attribute *input_dev_caps_attrs[] = {
1144 static struct attribute_group input_dev_caps_attr_group = {
1145 .name = "capabilities",
1146 .attrs = input_dev_caps_attrs,
1149 static const struct attribute_group *input_dev_attr_groups[] = {
1150 &input_dev_attr_group,
1151 &input_dev_id_attr_group,
1152 &input_dev_caps_attr_group,
1156 static void input_dev_release(struct device *device)
1158 struct input_dev *dev = to_input_dev(device);
1160 input_ff_destroy(dev);
1163 module_put(THIS_MODULE);
1167 * Input uevent interface - loading event handlers based on
1170 static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1171 const char *name, unsigned long *bitmap, int max)
1175 if (add_uevent_var(env, "%s=", name))
1178 len = input_print_bitmap(&env->buf[env->buflen - 1],
1179 sizeof(env->buf) - env->buflen,
1181 if (len >= (sizeof(env->buf) - env->buflen))
1188 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1189 struct input_dev *dev)
1193 if (add_uevent_var(env, "MODALIAS="))
1196 len = input_print_modalias(&env->buf[env->buflen - 1],
1197 sizeof(env->buf) - env->buflen,
1199 if (len >= (sizeof(env->buf) - env->buflen))
1206 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1208 int err = add_uevent_var(env, fmt, val); \
1213 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1215 int err = input_add_uevent_bm_var(env, name, bm, max); \
1220 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1222 int err = input_add_uevent_modalias_var(env, dev); \
1227 static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1229 struct input_dev *dev = to_input_dev(device);
1231 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1232 dev->id.bustype, dev->id.vendor,
1233 dev->id.product, dev->id.version);
1235 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1237 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1239 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1241 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1242 if (test_bit(EV_KEY, dev->evbit))
1243 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1244 if (test_bit(EV_REL, dev->evbit))
1245 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1246 if (test_bit(EV_ABS, dev->evbit))
1247 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1248 if (test_bit(EV_MSC, dev->evbit))
1249 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1250 if (test_bit(EV_LED, dev->evbit))
1251 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1252 if (test_bit(EV_SND, dev->evbit))
1253 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1254 if (test_bit(EV_FF, dev->evbit))
1255 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1256 if (test_bit(EV_SW, dev->evbit))
1257 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1259 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1264 #define INPUT_DO_TOGGLE(dev, type, bits, on) \
1267 if (!test_bit(EV_##type, dev->evbit)) \
1269 for (i = 0; i < type##_MAX; i++) { \
1270 if (!test_bit(i, dev->bits##bit) || \
1271 !test_bit(i, dev->bits)) \
1273 dev->event(dev, EV_##type, i, on); \
1278 static void input_dev_reset(struct input_dev *dev, bool activate)
1283 INPUT_DO_TOGGLE(dev, LED, led, activate);
1284 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1286 if (activate && test_bit(EV_REP, dev->evbit)) {
1287 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1288 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1292 static int input_dev_suspend(struct device *dev)
1294 struct input_dev *input_dev = to_input_dev(dev);
1296 mutex_lock(&input_dev->mutex);
1297 input_dev_reset(input_dev, false);
1298 mutex_unlock(&input_dev->mutex);
1303 static int input_dev_resume(struct device *dev)
1305 struct input_dev *input_dev = to_input_dev(dev);
1307 mutex_lock(&input_dev->mutex);
1308 input_dev_reset(input_dev, true);
1309 mutex_unlock(&input_dev->mutex);
1314 static const struct dev_pm_ops input_dev_pm_ops = {
1315 .suspend = input_dev_suspend,
1316 .resume = input_dev_resume,
1317 .poweroff = input_dev_suspend,
1318 .restore = input_dev_resume,
1320 #endif /* CONFIG_PM */
1322 static struct device_type input_dev_type = {
1323 .groups = input_dev_attr_groups,
1324 .release = input_dev_release,
1325 .uevent = input_dev_uevent,
1327 .pm = &input_dev_pm_ops,
1331 static char *input_devnode(struct device *dev, mode_t *mode)
1333 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1336 struct class input_class = {
1338 .devnode = input_devnode,
1340 EXPORT_SYMBOL_GPL(input_class);
1343 * input_allocate_device - allocate memory for new input device
1345 * Returns prepared struct input_dev or NULL.
1347 * NOTE: Use input_free_device() to free devices that have not been
1348 * registered; input_unregister_device() should be used for already
1349 * registered devices.
1351 struct input_dev *input_allocate_device(void)
1353 struct input_dev *dev;
1355 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1357 dev->dev.type = &input_dev_type;
1358 dev->dev.class = &input_class;
1359 device_initialize(&dev->dev);
1360 mutex_init(&dev->mutex);
1361 spin_lock_init(&dev->event_lock);
1362 INIT_LIST_HEAD(&dev->h_list);
1363 INIT_LIST_HEAD(&dev->node);
1365 __module_get(THIS_MODULE);
1370 EXPORT_SYMBOL(input_allocate_device);
1373 * input_free_device - free memory occupied by input_dev structure
1374 * @dev: input device to free
1376 * This function should only be used if input_register_device()
1377 * was not called yet or if it failed. Once device was registered
1378 * use input_unregister_device() and memory will be freed once last
1379 * reference to the device is dropped.
1381 * Device should be allocated by input_allocate_device().
1383 * NOTE: If there are references to the input device then memory
1384 * will not be freed until last reference is dropped.
1386 void input_free_device(struct input_dev *dev)
1389 input_put_device(dev);
1391 EXPORT_SYMBOL(input_free_device);
1394 * input_set_capability - mark device as capable of a certain event
1395 * @dev: device that is capable of emitting or accepting event
1396 * @type: type of the event (EV_KEY, EV_REL, etc...)
1399 * In addition to setting up corresponding bit in appropriate capability
1400 * bitmap the function also adjusts dev->evbit.
1402 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1406 __set_bit(code, dev->keybit);
1410 __set_bit(code, dev->relbit);
1414 __set_bit(code, dev->absbit);
1418 __set_bit(code, dev->mscbit);
1422 __set_bit(code, dev->swbit);
1426 __set_bit(code, dev->ledbit);
1430 __set_bit(code, dev->sndbit);
1434 __set_bit(code, dev->ffbit);
1443 "input_set_capability: unknown type %u (code %u)\n",
1449 __set_bit(type, dev->evbit);
1451 EXPORT_SYMBOL(input_set_capability);
1454 * input_register_device - register device with input core
1455 * @dev: device to be registered
1457 * This function registers device with input core. The device must be
1458 * allocated with input_allocate_device() and all it's capabilities
1459 * set up before registering.
1460 * If function fails the device must be freed with input_free_device().
1461 * Once device has been successfully registered it can be unregistered
1462 * with input_unregister_device(); input_free_device() should not be
1463 * called in this case.
1465 int input_register_device(struct input_dev *dev)
1467 static atomic_t input_no = ATOMIC_INIT(0);
1468 struct input_handler *handler;
1472 __set_bit(EV_SYN, dev->evbit);
1475 * If delay and period are pre-set by the driver, then autorepeating
1476 * is handled by the driver itself and we don't do it in input.c.
1479 init_timer(&dev->timer);
1480 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1481 dev->timer.data = (long) dev;
1482 dev->timer.function = input_repeat_key;
1483 dev->rep[REP_DELAY] = 250;
1484 dev->rep[REP_PERIOD] = 33;
1487 if (!dev->getkeycode)
1488 dev->getkeycode = input_default_getkeycode;
1490 if (!dev->setkeycode)
1491 dev->setkeycode = input_default_setkeycode;
1493 dev_set_name(&dev->dev, "input%ld",
1494 (unsigned long) atomic_inc_return(&input_no) - 1);
1496 error = device_add(&dev->dev);
1500 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1501 printk(KERN_INFO "input: %s as %s\n",
1502 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1505 error = mutex_lock_interruptible(&input_mutex);
1507 device_del(&dev->dev);
1511 list_add_tail(&dev->node, &input_dev_list);
1513 list_for_each_entry(handler, &input_handler_list, node)
1514 input_attach_handler(dev, handler);
1516 input_wakeup_procfs_readers();
1518 mutex_unlock(&input_mutex);
1522 EXPORT_SYMBOL(input_register_device);
1525 * input_unregister_device - unregister previously registered device
1526 * @dev: device to be unregistered
1528 * This function unregisters an input device. Once device is unregistered
1529 * the caller should not try to access it as it may get freed at any moment.
1531 void input_unregister_device(struct input_dev *dev)
1533 struct input_handle *handle, *next;
1535 input_disconnect_device(dev);
1537 mutex_lock(&input_mutex);
1539 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1540 handle->handler->disconnect(handle);
1541 WARN_ON(!list_empty(&dev->h_list));
1543 del_timer_sync(&dev->timer);
1544 list_del_init(&dev->node);
1546 input_wakeup_procfs_readers();
1548 mutex_unlock(&input_mutex);
1550 device_unregister(&dev->dev);
1552 EXPORT_SYMBOL(input_unregister_device);
1555 * input_register_handler - register a new input handler
1556 * @handler: handler to be registered
1558 * This function registers a new input handler (interface) for input
1559 * devices in the system and attaches it to all input devices that
1560 * are compatible with the handler.
1562 int input_register_handler(struct input_handler *handler)
1564 struct input_dev *dev;
1567 retval = mutex_lock_interruptible(&input_mutex);
1571 INIT_LIST_HEAD(&handler->h_list);
1573 if (handler->fops != NULL) {
1574 if (input_table[handler->minor >> 5]) {
1578 input_table[handler->minor >> 5] = handler;
1581 list_add_tail(&handler->node, &input_handler_list);
1583 list_for_each_entry(dev, &input_dev_list, node)
1584 input_attach_handler(dev, handler);
1586 input_wakeup_procfs_readers();
1589 mutex_unlock(&input_mutex);
1592 EXPORT_SYMBOL(input_register_handler);
1595 * input_unregister_handler - unregisters an input handler
1596 * @handler: handler to be unregistered
1598 * This function disconnects a handler from its input devices and
1599 * removes it from lists of known handlers.
1601 void input_unregister_handler(struct input_handler *handler)
1603 struct input_handle *handle, *next;
1605 mutex_lock(&input_mutex);
1607 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1608 handler->disconnect(handle);
1609 WARN_ON(!list_empty(&handler->h_list));
1611 list_del_init(&handler->node);
1613 if (handler->fops != NULL)
1614 input_table[handler->minor >> 5] = NULL;
1616 input_wakeup_procfs_readers();
1618 mutex_unlock(&input_mutex);
1620 EXPORT_SYMBOL(input_unregister_handler);
1623 * input_register_handle - register a new input handle
1624 * @handle: handle to register
1626 * This function puts a new input handle onto device's
1627 * and handler's lists so that events can flow through
1628 * it once it is opened using input_open_device().
1630 * This function is supposed to be called from handler's
1633 int input_register_handle(struct input_handle *handle)
1635 struct input_handler *handler = handle->handler;
1636 struct input_dev *dev = handle->dev;
1640 * We take dev->mutex here to prevent race with
1641 * input_release_device().
1643 error = mutex_lock_interruptible(&dev->mutex);
1646 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1647 mutex_unlock(&dev->mutex);
1650 * Since we are supposed to be called from ->connect()
1651 * which is mutually exclusive with ->disconnect()
1652 * we can't be racing with input_unregister_handle()
1653 * and so separate lock is not needed here.
1655 list_add_tail(&handle->h_node, &handler->h_list);
1658 handler->start(handle);
1662 EXPORT_SYMBOL(input_register_handle);
1665 * input_unregister_handle - unregister an input handle
1666 * @handle: handle to unregister
1668 * This function removes input handle from device's
1669 * and handler's lists.
1671 * This function is supposed to be called from handler's
1672 * disconnect() method.
1674 void input_unregister_handle(struct input_handle *handle)
1676 struct input_dev *dev = handle->dev;
1678 list_del_init(&handle->h_node);
1681 * Take dev->mutex to prevent race with input_release_device().
1683 mutex_lock(&dev->mutex);
1684 list_del_rcu(&handle->d_node);
1685 mutex_unlock(&dev->mutex);
1688 EXPORT_SYMBOL(input_unregister_handle);
1690 static int input_open_file(struct inode *inode, struct file *file)
1692 struct input_handler *handler;
1693 const struct file_operations *old_fops, *new_fops = NULL;
1697 /* No load-on-demand here? */
1698 handler = input_table[iminor(inode) >> 5];
1699 if (!handler || !(new_fops = fops_get(handler->fops))) {
1705 * That's _really_ odd. Usually NULL ->open means "nothing special",
1706 * not "no device". Oh, well...
1708 if (!new_fops->open) {
1713 old_fops = file->f_op;
1714 file->f_op = new_fops;
1716 err = new_fops->open(inode, file);
1719 fops_put(file->f_op);
1720 file->f_op = fops_get(old_fops);
1728 static const struct file_operations input_fops = {
1729 .owner = THIS_MODULE,
1730 .open = input_open_file,
1733 static void __init input_init_abs_bypass(void)
1735 const unsigned int *p;
1737 for (p = input_abs_bypass_init_data; *p; p++)
1738 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1741 static int __init input_init(void)
1745 input_init_abs_bypass();
1747 err = class_register(&input_class);
1749 printk(KERN_ERR "input: unable to register input_dev class\n");
1753 err = input_proc_init();
1757 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1759 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1765 fail2: input_proc_exit();
1766 fail1: class_unregister(&input_class);
1770 static void __exit input_exit(void)
1773 unregister_chrdev(INPUT_MAJOR, "input");
1774 class_unregister(&input_class);
1777 subsys_initcall(input_init);
1778 module_exit(input_exit);