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>
27 #include "input-compat.h"
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
30 MODULE_DESCRIPTION("Input core");
31 MODULE_LICENSE("GPL");
33 #define INPUT_DEVICES 256
36 * EV_ABS events which should not be cached are listed here.
38 static unsigned int input_abs_bypass_init_data
[] __initdata
= {
52 static unsigned long input_abs_bypass
[BITS_TO_LONGS(ABS_CNT
)];
54 static LIST_HEAD(input_dev_list
);
55 static LIST_HEAD(input_handler_list
);
58 * input_mutex protects access to both input_dev_list and input_handler_list.
59 * This also causes input_[un]register_device and input_[un]register_handler
60 * be mutually exclusive which simplifies locking in drivers implementing
63 static DEFINE_MUTEX(input_mutex
);
65 static struct input_handler
*input_table
[8];
67 static inline int is_event_supported(unsigned int code
,
68 unsigned long *bm
, unsigned int max
)
70 return code
<= max
&& test_bit(code
, bm
);
73 static int input_defuzz_abs_event(int value
, int old_val
, int fuzz
)
76 if (value
> old_val
- fuzz
/ 2 && value
< old_val
+ fuzz
/ 2)
79 if (value
> old_val
- fuzz
&& value
< old_val
+ fuzz
)
80 return (old_val
* 3 + value
) / 4;
82 if (value
> old_val
- fuzz
* 2 && value
< old_val
+ fuzz
* 2)
83 return (old_val
+ value
) / 2;
90 * Pass event first through all filters and then, if event has not been
91 * filtered out, through all open handles. This function is called with
92 * dev->event_lock held and interrupts disabled.
94 static void input_pass_event(struct input_dev
*dev
,
95 unsigned int type
, unsigned int code
, int value
)
97 struct input_handler
*handler
;
98 struct input_handle
*handle
;
102 handle
= rcu_dereference(dev
->grab
);
104 handle
->handler
->event(handle
, type
, code
, value
);
106 bool filtered
= false;
108 list_for_each_entry_rcu(handle
, &dev
->h_list
, d_node
) {
112 handler
= handle
->handler
;
113 if (!handler
->filter
) {
117 handler
->event(handle
, type
, code
, value
);
119 } else if (handler
->filter(handle
, type
, code
, value
))
128 * Generate software autorepeat event. Note that we take
129 * dev->event_lock here to avoid racing with input_event
130 * which may cause keys get "stuck".
132 static void input_repeat_key(unsigned long data
)
134 struct input_dev
*dev
= (void *) data
;
137 spin_lock_irqsave(&dev
->event_lock
, flags
);
139 if (test_bit(dev
->repeat_key
, dev
->key
) &&
140 is_event_supported(dev
->repeat_key
, dev
->keybit
, KEY_MAX
)) {
142 input_pass_event(dev
, EV_KEY
, dev
->repeat_key
, 2);
146 * Only send SYN_REPORT if we are not in a middle
147 * of driver parsing a new hardware packet.
148 * Otherwise assume that the driver will send
149 * SYN_REPORT once it's done.
151 input_pass_event(dev
, EV_SYN
, SYN_REPORT
, 1);
154 if (dev
->rep
[REP_PERIOD
])
155 mod_timer(&dev
->timer
, jiffies
+
156 msecs_to_jiffies(dev
->rep
[REP_PERIOD
]));
159 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
162 static void input_start_autorepeat(struct input_dev
*dev
, int code
)
164 if (test_bit(EV_REP
, dev
->evbit
) &&
165 dev
->rep
[REP_PERIOD
] && dev
->rep
[REP_DELAY
] &&
167 dev
->repeat_key
= code
;
168 mod_timer(&dev
->timer
,
169 jiffies
+ msecs_to_jiffies(dev
->rep
[REP_DELAY
]));
173 static void input_stop_autorepeat(struct input_dev
*dev
)
175 del_timer(&dev
->timer
);
178 #define INPUT_IGNORE_EVENT 0
179 #define INPUT_PASS_TO_HANDLERS 1
180 #define INPUT_PASS_TO_DEVICE 2
181 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
183 static void input_handle_event(struct input_dev
*dev
,
184 unsigned int type
, unsigned int code
, int value
)
186 int disposition
= INPUT_IGNORE_EVENT
;
193 disposition
= INPUT_PASS_TO_ALL
;
199 disposition
= INPUT_PASS_TO_HANDLERS
;
204 disposition
= INPUT_PASS_TO_HANDLERS
;
210 if (is_event_supported(code
, dev
->keybit
, KEY_MAX
) &&
211 !!test_bit(code
, dev
->key
) != value
) {
214 __change_bit(code
, dev
->key
);
216 input_start_autorepeat(dev
, code
);
218 input_stop_autorepeat(dev
);
221 disposition
= INPUT_PASS_TO_HANDLERS
;
226 if (is_event_supported(code
, dev
->swbit
, SW_MAX
) &&
227 !!test_bit(code
, dev
->sw
) != value
) {
229 __change_bit(code
, dev
->sw
);
230 disposition
= INPUT_PASS_TO_HANDLERS
;
235 if (is_event_supported(code
, dev
->absbit
, ABS_MAX
)) {
237 if (test_bit(code
, input_abs_bypass
)) {
238 disposition
= INPUT_PASS_TO_HANDLERS
;
242 value
= input_defuzz_abs_event(value
,
243 dev
->abs
[code
], dev
->absfuzz
[code
]);
245 if (dev
->abs
[code
] != value
) {
246 dev
->abs
[code
] = value
;
247 disposition
= INPUT_PASS_TO_HANDLERS
;
253 if (is_event_supported(code
, dev
->relbit
, REL_MAX
) && value
)
254 disposition
= INPUT_PASS_TO_HANDLERS
;
259 if (is_event_supported(code
, dev
->mscbit
, MSC_MAX
))
260 disposition
= INPUT_PASS_TO_ALL
;
265 if (is_event_supported(code
, dev
->ledbit
, LED_MAX
) &&
266 !!test_bit(code
, dev
->led
) != value
) {
268 __change_bit(code
, dev
->led
);
269 disposition
= INPUT_PASS_TO_ALL
;
274 if (is_event_supported(code
, dev
->sndbit
, SND_MAX
)) {
276 if (!!test_bit(code
, dev
->snd
) != !!value
)
277 __change_bit(code
, dev
->snd
);
278 disposition
= INPUT_PASS_TO_ALL
;
283 if (code
<= REP_MAX
&& value
>= 0 && dev
->rep
[code
] != value
) {
284 dev
->rep
[code
] = value
;
285 disposition
= INPUT_PASS_TO_ALL
;
291 disposition
= INPUT_PASS_TO_ALL
;
295 disposition
= INPUT_PASS_TO_ALL
;
299 if (disposition
!= INPUT_IGNORE_EVENT
&& type
!= EV_SYN
)
302 if ((disposition
& INPUT_PASS_TO_DEVICE
) && dev
->event
)
303 dev
->event(dev
, type
, code
, value
);
305 if (disposition
& INPUT_PASS_TO_HANDLERS
)
306 input_pass_event(dev
, type
, code
, value
);
310 * input_event() - report new input event
311 * @dev: device that generated the event
312 * @type: type of the event
314 * @value: value of the event
316 * This function should be used by drivers implementing various input
317 * devices to report input events. See also input_inject_event().
319 * NOTE: input_event() may be safely used right after input device was
320 * allocated with input_allocate_device(), even before it is registered
321 * with input_register_device(), but the event will not reach any of the
322 * input handlers. Such early invocation of input_event() may be used
323 * to 'seed' initial state of a switch or initial position of absolute
326 void input_event(struct input_dev
*dev
,
327 unsigned int type
, unsigned int code
, int value
)
331 if (is_event_supported(type
, dev
->evbit
, EV_MAX
)) {
333 spin_lock_irqsave(&dev
->event_lock
, flags
);
334 add_input_randomness(type
, code
, value
);
335 input_handle_event(dev
, type
, code
, value
);
336 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
339 EXPORT_SYMBOL(input_event
);
342 * input_inject_event() - send input event from input handler
343 * @handle: input handle to send event through
344 * @type: type of the event
346 * @value: value of the event
348 * Similar to input_event() but will ignore event if device is
349 * "grabbed" and handle injecting event is not the one that owns
352 void input_inject_event(struct input_handle
*handle
,
353 unsigned int type
, unsigned int code
, int value
)
355 struct input_dev
*dev
= handle
->dev
;
356 struct input_handle
*grab
;
359 if (is_event_supported(type
, dev
->evbit
, EV_MAX
)) {
360 spin_lock_irqsave(&dev
->event_lock
, flags
);
363 grab
= rcu_dereference(dev
->grab
);
364 if (!grab
|| grab
== handle
)
365 input_handle_event(dev
, type
, code
, value
);
368 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
371 EXPORT_SYMBOL(input_inject_event
);
374 * input_grab_device - grabs device for exclusive use
375 * @handle: input handle that wants to own the device
377 * When a device is grabbed by an input handle all events generated by
378 * the device are delivered only to this handle. Also events injected
379 * by other input handles are ignored while device is grabbed.
381 int input_grab_device(struct input_handle
*handle
)
383 struct input_dev
*dev
= handle
->dev
;
386 retval
= mutex_lock_interruptible(&dev
->mutex
);
395 rcu_assign_pointer(dev
->grab
, handle
);
399 mutex_unlock(&dev
->mutex
);
402 EXPORT_SYMBOL(input_grab_device
);
404 static void __input_release_device(struct input_handle
*handle
)
406 struct input_dev
*dev
= handle
->dev
;
408 if (dev
->grab
== handle
) {
409 rcu_assign_pointer(dev
->grab
, NULL
);
410 /* Make sure input_pass_event() notices that grab is gone */
413 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
414 if (handle
->open
&& handle
->handler
->start
)
415 handle
->handler
->start(handle
);
420 * input_release_device - release previously grabbed device
421 * @handle: input handle that owns the device
423 * Releases previously grabbed device so that other input handles can
424 * start receiving input events. Upon release all handlers attached
425 * to the device have their start() method called so they have a change
426 * to synchronize device state with the rest of the system.
428 void input_release_device(struct input_handle
*handle
)
430 struct input_dev
*dev
= handle
->dev
;
432 mutex_lock(&dev
->mutex
);
433 __input_release_device(handle
);
434 mutex_unlock(&dev
->mutex
);
436 EXPORT_SYMBOL(input_release_device
);
439 * input_open_device - open input device
440 * @handle: handle through which device is being accessed
442 * This function should be called by input handlers when they
443 * want to start receive events from given input device.
445 int input_open_device(struct input_handle
*handle
)
447 struct input_dev
*dev
= handle
->dev
;
450 retval
= mutex_lock_interruptible(&dev
->mutex
);
454 if (dev
->going_away
) {
461 if (!dev
->users
++ && dev
->open
)
462 retval
= dev
->open(dev
);
466 if (!--handle
->open
) {
468 * Make sure we are not delivering any more events
469 * through this handle
476 mutex_unlock(&dev
->mutex
);
479 EXPORT_SYMBOL(input_open_device
);
481 int input_flush_device(struct input_handle
*handle
, struct file
*file
)
483 struct input_dev
*dev
= handle
->dev
;
486 retval
= mutex_lock_interruptible(&dev
->mutex
);
491 retval
= dev
->flush(dev
, file
);
493 mutex_unlock(&dev
->mutex
);
496 EXPORT_SYMBOL(input_flush_device
);
499 * input_close_device - close input device
500 * @handle: handle through which device is being accessed
502 * This function should be called by input handlers when they
503 * want to stop receive events from given input device.
505 void input_close_device(struct input_handle
*handle
)
507 struct input_dev
*dev
= handle
->dev
;
509 mutex_lock(&dev
->mutex
);
511 __input_release_device(handle
);
513 if (!--dev
->users
&& dev
->close
)
516 if (!--handle
->open
) {
518 * synchronize_rcu() makes sure that input_pass_event()
519 * completed and that no more input events are delivered
520 * through this handle
525 mutex_unlock(&dev
->mutex
);
527 EXPORT_SYMBOL(input_close_device
);
530 * Prepare device for unregistering
532 static void input_disconnect_device(struct input_dev
*dev
)
534 struct input_handle
*handle
;
538 * Mark device as going away. Note that we take dev->mutex here
539 * not to protect access to dev->going_away but rather to ensure
540 * that there are no threads in the middle of input_open_device()
542 mutex_lock(&dev
->mutex
);
543 dev
->going_away
= true;
544 mutex_unlock(&dev
->mutex
);
546 spin_lock_irq(&dev
->event_lock
);
549 * Simulate keyup events for all pressed keys so that handlers
550 * are not left with "stuck" keys. The driver may continue
551 * generate events even after we done here but they will not
552 * reach any handlers.
554 if (is_event_supported(EV_KEY
, dev
->evbit
, EV_MAX
)) {
555 for (code
= 0; code
<= KEY_MAX
; code
++) {
556 if (is_event_supported(code
, dev
->keybit
, KEY_MAX
) &&
557 __test_and_clear_bit(code
, dev
->key
)) {
558 input_pass_event(dev
, EV_KEY
, code
, 0);
561 input_pass_event(dev
, EV_SYN
, SYN_REPORT
, 1);
564 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
567 spin_unlock_irq(&dev
->event_lock
);
570 static int input_fetch_keycode(struct input_dev
*dev
, int scancode
)
572 switch (dev
->keycodesize
) {
574 return ((u8
*)dev
->keycode
)[scancode
];
577 return ((u16
*)dev
->keycode
)[scancode
];
580 return ((u32
*)dev
->keycode
)[scancode
];
584 static int input_default_getkeycode(struct input_dev
*dev
,
585 unsigned int scancode
,
586 unsigned int *keycode
)
588 if (!dev
->keycodesize
)
591 if (scancode
>= dev
->keycodemax
)
594 *keycode
= input_fetch_keycode(dev
, scancode
);
599 static int input_default_setkeycode(struct input_dev
*dev
,
600 unsigned int scancode
,
601 unsigned int keycode
)
606 if (scancode
>= dev
->keycodemax
)
609 if (!dev
->keycodesize
)
612 if (dev
->keycodesize
< sizeof(keycode
) && (keycode
>> (dev
->keycodesize
* 8)))
615 switch (dev
->keycodesize
) {
617 u8
*k
= (u8
*)dev
->keycode
;
618 old_keycode
= k
[scancode
];
619 k
[scancode
] = keycode
;
623 u16
*k
= (u16
*)dev
->keycode
;
624 old_keycode
= k
[scancode
];
625 k
[scancode
] = keycode
;
629 u32
*k
= (u32
*)dev
->keycode
;
630 old_keycode
= k
[scancode
];
631 k
[scancode
] = keycode
;
636 __clear_bit(old_keycode
, dev
->keybit
);
637 __set_bit(keycode
, dev
->keybit
);
639 for (i
= 0; i
< dev
->keycodemax
; i
++) {
640 if (input_fetch_keycode(dev
, i
) == old_keycode
) {
641 __set_bit(old_keycode
, dev
->keybit
);
642 break; /* Setting the bit twice is useless, so break */
650 * input_get_keycode - retrieve keycode currently mapped to a given scancode
651 * @dev: input device which keymap is being queried
652 * @scancode: scancode (or its equivalent for device in question) for which
656 * This function should be called by anyone interested in retrieving current
657 * keymap. Presently keyboard and evdev handlers use it.
659 int input_get_keycode(struct input_dev
*dev
,
660 unsigned int scancode
, unsigned int *keycode
)
665 spin_lock_irqsave(&dev
->event_lock
, flags
);
666 retval
= dev
->getkeycode(dev
, scancode
, keycode
);
667 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
671 EXPORT_SYMBOL(input_get_keycode
);
674 * input_get_keycode - assign new keycode to a given scancode
675 * @dev: input device which keymap is being updated
676 * @scancode: scancode (or its equivalent for device in question)
677 * @keycode: new keycode to be assigned to the scancode
679 * This function should be called by anyone needing to update current
680 * keymap. Presently keyboard and evdev handlers use it.
682 int input_set_keycode(struct input_dev
*dev
,
683 unsigned int scancode
, unsigned int keycode
)
689 if (keycode
> KEY_MAX
)
692 spin_lock_irqsave(&dev
->event_lock
, flags
);
694 retval
= dev
->getkeycode(dev
, scancode
, &old_keycode
);
698 retval
= dev
->setkeycode(dev
, scancode
, keycode
);
702 /* Make sure KEY_RESERVED did not get enabled. */
703 __clear_bit(KEY_RESERVED
, dev
->keybit
);
706 * Simulate keyup event if keycode is not present
707 * in the keymap anymore
709 if (test_bit(EV_KEY
, dev
->evbit
) &&
710 !is_event_supported(old_keycode
, dev
->keybit
, KEY_MAX
) &&
711 __test_and_clear_bit(old_keycode
, dev
->key
)) {
713 input_pass_event(dev
, EV_KEY
, old_keycode
, 0);
715 input_pass_event(dev
, EV_SYN
, SYN_REPORT
, 1);
719 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
723 EXPORT_SYMBOL(input_set_keycode
);
725 #define MATCH_BIT(bit, max) \
726 for (i = 0; i < BITS_TO_LONGS(max); i++) \
727 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
729 if (i != BITS_TO_LONGS(max)) \
732 static const struct input_device_id
*input_match_device(struct input_handler
*handler
,
733 struct input_dev
*dev
)
735 const struct input_device_id
*id
;
738 for (id
= handler
->id_table
; id
->flags
|| id
->driver_info
; id
++) {
740 if (id
->flags
& INPUT_DEVICE_ID_MATCH_BUS
)
741 if (id
->bustype
!= dev
->id
.bustype
)
744 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VENDOR
)
745 if (id
->vendor
!= dev
->id
.vendor
)
748 if (id
->flags
& INPUT_DEVICE_ID_MATCH_PRODUCT
)
749 if (id
->product
!= dev
->id
.product
)
752 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VERSION
)
753 if (id
->version
!= dev
->id
.version
)
756 MATCH_BIT(evbit
, EV_MAX
);
757 MATCH_BIT(keybit
, KEY_MAX
);
758 MATCH_BIT(relbit
, REL_MAX
);
759 MATCH_BIT(absbit
, ABS_MAX
);
760 MATCH_BIT(mscbit
, MSC_MAX
);
761 MATCH_BIT(ledbit
, LED_MAX
);
762 MATCH_BIT(sndbit
, SND_MAX
);
763 MATCH_BIT(ffbit
, FF_MAX
);
764 MATCH_BIT(swbit
, SW_MAX
);
766 if (!handler
->match
|| handler
->match(handler
, dev
))
773 static int input_attach_handler(struct input_dev
*dev
, struct input_handler
*handler
)
775 const struct input_device_id
*id
;
778 id
= input_match_device(handler
, dev
);
782 error
= handler
->connect(handler
, dev
, id
);
783 if (error
&& error
!= -ENODEV
)
785 "input: failed to attach handler %s to device %s, "
787 handler
->name
, kobject_name(&dev
->dev
.kobj
), error
);
794 static int input_bits_to_string(char *buf
, int buf_size
,
795 unsigned long bits
, bool skip_empty
)
799 if (INPUT_COMPAT_TEST
) {
800 u32 dword
= bits
>> 32;
801 if (dword
|| !skip_empty
)
802 len
+= snprintf(buf
, buf_size
, "%x ", dword
);
804 dword
= bits
& 0xffffffffUL
;
805 if (dword
|| !skip_empty
|| len
)
806 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0),
809 if (bits
|| !skip_empty
)
810 len
+= snprintf(buf
, buf_size
, "%lx", bits
);
816 #else /* !CONFIG_COMPAT */
818 static int input_bits_to_string(char *buf
, int buf_size
,
819 unsigned long bits
, bool skip_empty
)
821 return bits
|| !skip_empty
?
822 snprintf(buf
, buf_size
, "%lx", bits
) : 0;
827 #ifdef CONFIG_PROC_FS
829 static struct proc_dir_entry
*proc_bus_input_dir
;
830 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait
);
831 static int input_devices_state
;
833 static inline void input_wakeup_procfs_readers(void)
835 input_devices_state
++;
836 wake_up(&input_devices_poll_wait
);
839 static unsigned int input_proc_devices_poll(struct file
*file
, poll_table
*wait
)
841 poll_wait(file
, &input_devices_poll_wait
, wait
);
842 if (file
->f_version
!= input_devices_state
) {
843 file
->f_version
= input_devices_state
;
844 return POLLIN
| POLLRDNORM
;
850 union input_seq_state
{
858 static void *input_devices_seq_start(struct seq_file
*seq
, loff_t
*pos
)
860 union input_seq_state
*state
= (union input_seq_state
*)&seq
->private;
863 /* We need to fit into seq->private pointer */
864 BUILD_BUG_ON(sizeof(union input_seq_state
) != sizeof(seq
->private));
866 error
= mutex_lock_interruptible(&input_mutex
);
868 state
->mutex_acquired
= false;
869 return ERR_PTR(error
);
872 state
->mutex_acquired
= true;
874 return seq_list_start(&input_dev_list
, *pos
);
877 static void *input_devices_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
879 return seq_list_next(v
, &input_dev_list
, pos
);
882 static void input_seq_stop(struct seq_file
*seq
, void *v
)
884 union input_seq_state
*state
= (union input_seq_state
*)&seq
->private;
886 if (state
->mutex_acquired
)
887 mutex_unlock(&input_mutex
);
890 static void input_seq_print_bitmap(struct seq_file
*seq
, const char *name
,
891 unsigned long *bitmap
, int max
)
894 bool skip_empty
= true;
897 seq_printf(seq
, "B: %s=", name
);
899 for (i
= BITS_TO_LONGS(max
) - 1; i
>= 0; i
--) {
900 if (input_bits_to_string(buf
, sizeof(buf
),
901 bitmap
[i
], skip_empty
)) {
903 seq_printf(seq
, "%s%s", buf
, i
> 0 ? " " : "");
908 * If no output was produced print a single 0.
916 static int input_devices_seq_show(struct seq_file
*seq
, void *v
)
918 struct input_dev
*dev
= container_of(v
, struct input_dev
, node
);
919 const char *path
= kobject_get_path(&dev
->dev
.kobj
, GFP_KERNEL
);
920 struct input_handle
*handle
;
922 seq_printf(seq
, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
923 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
);
925 seq_printf(seq
, "N: Name=\"%s\"\n", dev
->name
? dev
->name
: "");
926 seq_printf(seq
, "P: Phys=%s\n", dev
->phys
? dev
->phys
: "");
927 seq_printf(seq
, "S: Sysfs=%s\n", path
? path
: "");
928 seq_printf(seq
, "U: Uniq=%s\n", dev
->uniq
? dev
->uniq
: "");
929 seq_printf(seq
, "H: Handlers=");
931 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
932 seq_printf(seq
, "%s ", handle
->name
);
935 input_seq_print_bitmap(seq
, "EV", dev
->evbit
, EV_MAX
);
936 if (test_bit(EV_KEY
, dev
->evbit
))
937 input_seq_print_bitmap(seq
, "KEY", dev
->keybit
, KEY_MAX
);
938 if (test_bit(EV_REL
, dev
->evbit
))
939 input_seq_print_bitmap(seq
, "REL", dev
->relbit
, REL_MAX
);
940 if (test_bit(EV_ABS
, dev
->evbit
))
941 input_seq_print_bitmap(seq
, "ABS", dev
->absbit
, ABS_MAX
);
942 if (test_bit(EV_MSC
, dev
->evbit
))
943 input_seq_print_bitmap(seq
, "MSC", dev
->mscbit
, MSC_MAX
);
944 if (test_bit(EV_LED
, dev
->evbit
))
945 input_seq_print_bitmap(seq
, "LED", dev
->ledbit
, LED_MAX
);
946 if (test_bit(EV_SND
, dev
->evbit
))
947 input_seq_print_bitmap(seq
, "SND", dev
->sndbit
, SND_MAX
);
948 if (test_bit(EV_FF
, dev
->evbit
))
949 input_seq_print_bitmap(seq
, "FF", dev
->ffbit
, FF_MAX
);
950 if (test_bit(EV_SW
, dev
->evbit
))
951 input_seq_print_bitmap(seq
, "SW", dev
->swbit
, SW_MAX
);
959 static const struct seq_operations input_devices_seq_ops
= {
960 .start
= input_devices_seq_start
,
961 .next
= input_devices_seq_next
,
962 .stop
= input_seq_stop
,
963 .show
= input_devices_seq_show
,
966 static int input_proc_devices_open(struct inode
*inode
, struct file
*file
)
968 return seq_open(file
, &input_devices_seq_ops
);
971 static const struct file_operations input_devices_fileops
= {
972 .owner
= THIS_MODULE
,
973 .open
= input_proc_devices_open
,
974 .poll
= input_proc_devices_poll
,
977 .release
= seq_release
,
980 static void *input_handlers_seq_start(struct seq_file
*seq
, loff_t
*pos
)
982 union input_seq_state
*state
= (union input_seq_state
*)&seq
->private;
985 /* We need to fit into seq->private pointer */
986 BUILD_BUG_ON(sizeof(union input_seq_state
) != sizeof(seq
->private));
988 error
= mutex_lock_interruptible(&input_mutex
);
990 state
->mutex_acquired
= false;
991 return ERR_PTR(error
);
994 state
->mutex_acquired
= true;
997 return seq_list_start(&input_handler_list
, *pos
);
1000 static void *input_handlers_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1002 union input_seq_state
*state
= (union input_seq_state
*)&seq
->private;
1004 state
->pos
= *pos
+ 1;
1005 return seq_list_next(v
, &input_handler_list
, pos
);
1008 static int input_handlers_seq_show(struct seq_file
*seq
, void *v
)
1010 struct input_handler
*handler
= container_of(v
, struct input_handler
, node
);
1011 union input_seq_state
*state
= (union input_seq_state
*)&seq
->private;
1013 seq_printf(seq
, "N: Number=%u Name=%s", state
->pos
, handler
->name
);
1014 if (handler
->filter
)
1015 seq_puts(seq
, " (filter)");
1017 seq_printf(seq
, " Minor=%d", handler
->minor
);
1018 seq_putc(seq
, '\n');
1023 static const struct seq_operations input_handlers_seq_ops
= {
1024 .start
= input_handlers_seq_start
,
1025 .next
= input_handlers_seq_next
,
1026 .stop
= input_seq_stop
,
1027 .show
= input_handlers_seq_show
,
1030 static int input_proc_handlers_open(struct inode
*inode
, struct file
*file
)
1032 return seq_open(file
, &input_handlers_seq_ops
);
1035 static const struct file_operations input_handlers_fileops
= {
1036 .owner
= THIS_MODULE
,
1037 .open
= input_proc_handlers_open
,
1039 .llseek
= seq_lseek
,
1040 .release
= seq_release
,
1043 static int __init
input_proc_init(void)
1045 struct proc_dir_entry
*entry
;
1047 proc_bus_input_dir
= proc_mkdir("bus/input", NULL
);
1048 if (!proc_bus_input_dir
)
1051 entry
= proc_create("devices", 0, proc_bus_input_dir
,
1052 &input_devices_fileops
);
1056 entry
= proc_create("handlers", 0, proc_bus_input_dir
,
1057 &input_handlers_fileops
);
1063 fail2
: remove_proc_entry("devices", proc_bus_input_dir
);
1064 fail1
: remove_proc_entry("bus/input", NULL
);
1068 static void input_proc_exit(void)
1070 remove_proc_entry("devices", proc_bus_input_dir
);
1071 remove_proc_entry("handlers", proc_bus_input_dir
);
1072 remove_proc_entry("bus/input", NULL
);
1075 #else /* !CONFIG_PROC_FS */
1076 static inline void input_wakeup_procfs_readers(void) { }
1077 static inline int input_proc_init(void) { return 0; }
1078 static inline void input_proc_exit(void) { }
1081 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
1082 static ssize_t input_dev_show_##name(struct device *dev, \
1083 struct device_attribute *attr, \
1086 struct input_dev *input_dev = to_input_dev(dev); \
1088 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1089 input_dev->name ? input_dev->name : ""); \
1091 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
1093 INPUT_DEV_STRING_ATTR_SHOW(name
);
1094 INPUT_DEV_STRING_ATTR_SHOW(phys
);
1095 INPUT_DEV_STRING_ATTR_SHOW(uniq
);
1097 static int input_print_modalias_bits(char *buf
, int size
,
1098 char name
, unsigned long *bm
,
1099 unsigned int min_bit
, unsigned int max_bit
)
1103 len
+= snprintf(buf
, max(size
, 0), "%c", name
);
1104 for (i
= min_bit
; i
< max_bit
; i
++)
1105 if (bm
[BIT_WORD(i
)] & BIT_MASK(i
))
1106 len
+= snprintf(buf
+ len
, max(size
- len
, 0), "%X,", i
);
1110 static int input_print_modalias(char *buf
, int size
, struct input_dev
*id
,
1115 len
= snprintf(buf
, max(size
, 0),
1116 "input:b%04Xv%04Xp%04Xe%04X-",
1117 id
->id
.bustype
, id
->id
.vendor
,
1118 id
->id
.product
, id
->id
.version
);
1120 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1121 'e', id
->evbit
, 0, EV_MAX
);
1122 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1123 'k', id
->keybit
, KEY_MIN_INTERESTING
, KEY_MAX
);
1124 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1125 'r', id
->relbit
, 0, REL_MAX
);
1126 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1127 'a', id
->absbit
, 0, ABS_MAX
);
1128 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1129 'm', id
->mscbit
, 0, MSC_MAX
);
1130 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1131 'l', id
->ledbit
, 0, LED_MAX
);
1132 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1133 's', id
->sndbit
, 0, SND_MAX
);
1134 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1135 'f', id
->ffbit
, 0, FF_MAX
);
1136 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
1137 'w', id
->swbit
, 0, SW_MAX
);
1140 len
+= snprintf(buf
+ len
, max(size
- len
, 0), "\n");
1145 static ssize_t
input_dev_show_modalias(struct device
*dev
,
1146 struct device_attribute
*attr
,
1149 struct input_dev
*id
= to_input_dev(dev
);
1152 len
= input_print_modalias(buf
, PAGE_SIZE
, id
, 1);
1154 return min_t(int, len
, PAGE_SIZE
);
1156 static DEVICE_ATTR(modalias
, S_IRUGO
, input_dev_show_modalias
, NULL
);
1158 static struct attribute
*input_dev_attrs
[] = {
1159 &dev_attr_name
.attr
,
1160 &dev_attr_phys
.attr
,
1161 &dev_attr_uniq
.attr
,
1162 &dev_attr_modalias
.attr
,
1166 static struct attribute_group input_dev_attr_group
= {
1167 .attrs
= input_dev_attrs
,
1170 #define INPUT_DEV_ID_ATTR(name) \
1171 static ssize_t input_dev_show_id_##name(struct device *dev, \
1172 struct device_attribute *attr, \
1175 struct input_dev *input_dev = to_input_dev(dev); \
1176 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1178 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1180 INPUT_DEV_ID_ATTR(bustype
);
1181 INPUT_DEV_ID_ATTR(vendor
);
1182 INPUT_DEV_ID_ATTR(product
);
1183 INPUT_DEV_ID_ATTR(version
);
1185 static struct attribute
*input_dev_id_attrs
[] = {
1186 &dev_attr_bustype
.attr
,
1187 &dev_attr_vendor
.attr
,
1188 &dev_attr_product
.attr
,
1189 &dev_attr_version
.attr
,
1193 static struct attribute_group input_dev_id_attr_group
= {
1195 .attrs
= input_dev_id_attrs
,
1198 static int input_print_bitmap(char *buf
, int buf_size
, unsigned long *bitmap
,
1199 int max
, int add_cr
)
1203 bool skip_empty
= true;
1205 for (i
= BITS_TO_LONGS(max
) - 1; i
>= 0; i
--) {
1206 len
+= input_bits_to_string(buf
+ len
, max(buf_size
- len
, 0),
1207 bitmap
[i
], skip_empty
);
1211 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0), " ");
1216 * If no output was produced print a single 0.
1219 len
= snprintf(buf
, buf_size
, "%d", 0);
1222 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0), "\n");
1227 #define INPUT_DEV_CAP_ATTR(ev, bm) \
1228 static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1229 struct device_attribute *attr, \
1232 struct input_dev *input_dev = to_input_dev(dev); \
1233 int len = input_print_bitmap(buf, PAGE_SIZE, \
1234 input_dev->bm##bit, ev##_MAX, \
1236 return min_t(int, len, PAGE_SIZE); \
1238 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1240 INPUT_DEV_CAP_ATTR(EV
, ev
);
1241 INPUT_DEV_CAP_ATTR(KEY
, key
);
1242 INPUT_DEV_CAP_ATTR(REL
, rel
);
1243 INPUT_DEV_CAP_ATTR(ABS
, abs
);
1244 INPUT_DEV_CAP_ATTR(MSC
, msc
);
1245 INPUT_DEV_CAP_ATTR(LED
, led
);
1246 INPUT_DEV_CAP_ATTR(SND
, snd
);
1247 INPUT_DEV_CAP_ATTR(FF
, ff
);
1248 INPUT_DEV_CAP_ATTR(SW
, sw
);
1250 static struct attribute
*input_dev_caps_attrs
[] = {
1263 static struct attribute_group input_dev_caps_attr_group
= {
1264 .name
= "capabilities",
1265 .attrs
= input_dev_caps_attrs
,
1268 static const struct attribute_group
*input_dev_attr_groups
[] = {
1269 &input_dev_attr_group
,
1270 &input_dev_id_attr_group
,
1271 &input_dev_caps_attr_group
,
1275 static void input_dev_release(struct device
*device
)
1277 struct input_dev
*dev
= to_input_dev(device
);
1279 input_ff_destroy(dev
);
1282 module_put(THIS_MODULE
);
1286 * Input uevent interface - loading event handlers based on
1289 static int input_add_uevent_bm_var(struct kobj_uevent_env
*env
,
1290 const char *name
, unsigned long *bitmap
, int max
)
1294 if (add_uevent_var(env
, "%s=", name
))
1297 len
= input_print_bitmap(&env
->buf
[env
->buflen
- 1],
1298 sizeof(env
->buf
) - env
->buflen
,
1299 bitmap
, max
, false);
1300 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
1307 static int input_add_uevent_modalias_var(struct kobj_uevent_env
*env
,
1308 struct input_dev
*dev
)
1312 if (add_uevent_var(env
, "MODALIAS="))
1315 len
= input_print_modalias(&env
->buf
[env
->buflen
- 1],
1316 sizeof(env
->buf
) - env
->buflen
,
1318 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
1325 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1327 int err = add_uevent_var(env, fmt, val); \
1332 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1334 int err = input_add_uevent_bm_var(env, name, bm, max); \
1339 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1341 int err = input_add_uevent_modalias_var(env, dev); \
1346 static int input_dev_uevent(struct device
*device
, struct kobj_uevent_env
*env
)
1348 struct input_dev
*dev
= to_input_dev(device
);
1350 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1351 dev
->id
.bustype
, dev
->id
.vendor
,
1352 dev
->id
.product
, dev
->id
.version
);
1354 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev
->name
);
1356 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev
->phys
);
1358 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev
->uniq
);
1360 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev
->evbit
, EV_MAX
);
1361 if (test_bit(EV_KEY
, dev
->evbit
))
1362 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev
->keybit
, KEY_MAX
);
1363 if (test_bit(EV_REL
, dev
->evbit
))
1364 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev
->relbit
, REL_MAX
);
1365 if (test_bit(EV_ABS
, dev
->evbit
))
1366 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev
->absbit
, ABS_MAX
);
1367 if (test_bit(EV_MSC
, dev
->evbit
))
1368 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev
->mscbit
, MSC_MAX
);
1369 if (test_bit(EV_LED
, dev
->evbit
))
1370 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev
->ledbit
, LED_MAX
);
1371 if (test_bit(EV_SND
, dev
->evbit
))
1372 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev
->sndbit
, SND_MAX
);
1373 if (test_bit(EV_FF
, dev
->evbit
))
1374 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev
->ffbit
, FF_MAX
);
1375 if (test_bit(EV_SW
, dev
->evbit
))
1376 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev
->swbit
, SW_MAX
);
1378 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev
);
1383 #define INPUT_DO_TOGGLE(dev, type, bits, on) \
1388 if (!test_bit(EV_##type, dev->evbit)) \
1391 for (i = 0; i < type##_MAX; i++) { \
1392 if (!test_bit(i, dev->bits##bit)) \
1395 active = test_bit(i, dev->bits); \
1396 if (!active && !on) \
1399 dev->event(dev, EV_##type, i, on ? active : 0); \
1404 static void input_dev_reset(struct input_dev
*dev
, bool activate
)
1409 INPUT_DO_TOGGLE(dev
, LED
, led
, activate
);
1410 INPUT_DO_TOGGLE(dev
, SND
, snd
, activate
);
1412 if (activate
&& test_bit(EV_REP
, dev
->evbit
)) {
1413 dev
->event(dev
, EV_REP
, REP_PERIOD
, dev
->rep
[REP_PERIOD
]);
1414 dev
->event(dev
, EV_REP
, REP_DELAY
, dev
->rep
[REP_DELAY
]);
1418 static int input_dev_suspend(struct device
*dev
)
1420 struct input_dev
*input_dev
= to_input_dev(dev
);
1422 mutex_lock(&input_dev
->mutex
);
1423 input_dev_reset(input_dev
, false);
1424 mutex_unlock(&input_dev
->mutex
);
1429 static int input_dev_resume(struct device
*dev
)
1431 struct input_dev
*input_dev
= to_input_dev(dev
);
1433 mutex_lock(&input_dev
->mutex
);
1434 input_dev_reset(input_dev
, true);
1435 mutex_unlock(&input_dev
->mutex
);
1440 static const struct dev_pm_ops input_dev_pm_ops
= {
1441 .suspend
= input_dev_suspend
,
1442 .resume
= input_dev_resume
,
1443 .poweroff
= input_dev_suspend
,
1444 .restore
= input_dev_resume
,
1446 #endif /* CONFIG_PM */
1448 static struct device_type input_dev_type
= {
1449 .groups
= input_dev_attr_groups
,
1450 .release
= input_dev_release
,
1451 .uevent
= input_dev_uevent
,
1453 .pm
= &input_dev_pm_ops
,
1457 static char *input_devnode(struct device
*dev
, mode_t
*mode
)
1459 return kasprintf(GFP_KERNEL
, "input/%s", dev_name(dev
));
1462 struct class input_class
= {
1464 .devnode
= input_devnode
,
1466 EXPORT_SYMBOL_GPL(input_class
);
1469 * input_allocate_device - allocate memory for new input device
1471 * Returns prepared struct input_dev or NULL.
1473 * NOTE: Use input_free_device() to free devices that have not been
1474 * registered; input_unregister_device() should be used for already
1475 * registered devices.
1477 struct input_dev
*input_allocate_device(void)
1479 struct input_dev
*dev
;
1481 dev
= kzalloc(sizeof(struct input_dev
), GFP_KERNEL
);
1483 dev
->dev
.type
= &input_dev_type
;
1484 dev
->dev
.class = &input_class
;
1485 device_initialize(&dev
->dev
);
1486 mutex_init(&dev
->mutex
);
1487 spin_lock_init(&dev
->event_lock
);
1488 INIT_LIST_HEAD(&dev
->h_list
);
1489 INIT_LIST_HEAD(&dev
->node
);
1491 __module_get(THIS_MODULE
);
1496 EXPORT_SYMBOL(input_allocate_device
);
1499 * input_free_device - free memory occupied by input_dev structure
1500 * @dev: input device to free
1502 * This function should only be used if input_register_device()
1503 * was not called yet or if it failed. Once device was registered
1504 * use input_unregister_device() and memory will be freed once last
1505 * reference to the device is dropped.
1507 * Device should be allocated by input_allocate_device().
1509 * NOTE: If there are references to the input device then memory
1510 * will not be freed until last reference is dropped.
1512 void input_free_device(struct input_dev
*dev
)
1515 input_put_device(dev
);
1517 EXPORT_SYMBOL(input_free_device
);
1520 * input_set_capability - mark device as capable of a certain event
1521 * @dev: device that is capable of emitting or accepting event
1522 * @type: type of the event (EV_KEY, EV_REL, etc...)
1525 * In addition to setting up corresponding bit in appropriate capability
1526 * bitmap the function also adjusts dev->evbit.
1528 void input_set_capability(struct input_dev
*dev
, unsigned int type
, unsigned int code
)
1532 __set_bit(code
, dev
->keybit
);
1536 __set_bit(code
, dev
->relbit
);
1540 __set_bit(code
, dev
->absbit
);
1544 __set_bit(code
, dev
->mscbit
);
1548 __set_bit(code
, dev
->swbit
);
1552 __set_bit(code
, dev
->ledbit
);
1556 __set_bit(code
, dev
->sndbit
);
1560 __set_bit(code
, dev
->ffbit
);
1569 "input_set_capability: unknown type %u (code %u)\n",
1575 __set_bit(type
, dev
->evbit
);
1577 EXPORT_SYMBOL(input_set_capability
);
1579 #define INPUT_CLEANSE_BITMASK(dev, type, bits) \
1581 if (!test_bit(EV_##type, dev->evbit)) \
1582 memset(dev->bits##bit, 0, \
1583 sizeof(dev->bits##bit)); \
1586 static void input_cleanse_bitmasks(struct input_dev
*dev
)
1588 INPUT_CLEANSE_BITMASK(dev
, KEY
, key
);
1589 INPUT_CLEANSE_BITMASK(dev
, REL
, rel
);
1590 INPUT_CLEANSE_BITMASK(dev
, ABS
, abs
);
1591 INPUT_CLEANSE_BITMASK(dev
, MSC
, msc
);
1592 INPUT_CLEANSE_BITMASK(dev
, LED
, led
);
1593 INPUT_CLEANSE_BITMASK(dev
, SND
, snd
);
1594 INPUT_CLEANSE_BITMASK(dev
, FF
, ff
);
1595 INPUT_CLEANSE_BITMASK(dev
, SW
, sw
);
1599 * input_register_device - register device with input core
1600 * @dev: device to be registered
1602 * This function registers device with input core. The device must be
1603 * allocated with input_allocate_device() and all it's capabilities
1604 * set up before registering.
1605 * If function fails the device must be freed with input_free_device().
1606 * Once device has been successfully registered it can be unregistered
1607 * with input_unregister_device(); input_free_device() should not be
1608 * called in this case.
1610 int input_register_device(struct input_dev
*dev
)
1612 static atomic_t input_no
= ATOMIC_INIT(0);
1613 struct input_handler
*handler
;
1617 /* Every input device generates EV_SYN/SYN_REPORT events. */
1618 __set_bit(EV_SYN
, dev
->evbit
);
1620 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
1621 __clear_bit(KEY_RESERVED
, dev
->keybit
);
1623 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
1624 input_cleanse_bitmasks(dev
);
1627 * If delay and period are pre-set by the driver, then autorepeating
1628 * is handled by the driver itself and we don't do it in input.c.
1630 init_timer(&dev
->timer
);
1631 if (!dev
->rep
[REP_DELAY
] && !dev
->rep
[REP_PERIOD
]) {
1632 dev
->timer
.data
= (long) dev
;
1633 dev
->timer
.function
= input_repeat_key
;
1634 dev
->rep
[REP_DELAY
] = 250;
1635 dev
->rep
[REP_PERIOD
] = 33;
1638 if (!dev
->getkeycode
)
1639 dev
->getkeycode
= input_default_getkeycode
;
1641 if (!dev
->setkeycode
)
1642 dev
->setkeycode
= input_default_setkeycode
;
1644 dev_set_name(&dev
->dev
, "input%ld",
1645 (unsigned long) atomic_inc_return(&input_no
) - 1);
1647 error
= device_add(&dev
->dev
);
1651 path
= kobject_get_path(&dev
->dev
.kobj
, GFP_KERNEL
);
1652 printk(KERN_INFO
"input: %s as %s\n",
1653 dev
->name
? dev
->name
: "Unspecified device", path
? path
: "N/A");
1656 error
= mutex_lock_interruptible(&input_mutex
);
1658 device_del(&dev
->dev
);
1662 list_add_tail(&dev
->node
, &input_dev_list
);
1664 list_for_each_entry(handler
, &input_handler_list
, node
)
1665 input_attach_handler(dev
, handler
);
1667 input_wakeup_procfs_readers();
1669 mutex_unlock(&input_mutex
);
1673 EXPORT_SYMBOL(input_register_device
);
1676 * input_unregister_device - unregister previously registered device
1677 * @dev: device to be unregistered
1679 * This function unregisters an input device. Once device is unregistered
1680 * the caller should not try to access it as it may get freed at any moment.
1682 void input_unregister_device(struct input_dev
*dev
)
1684 struct input_handle
*handle
, *next
;
1686 input_disconnect_device(dev
);
1688 mutex_lock(&input_mutex
);
1690 list_for_each_entry_safe(handle
, next
, &dev
->h_list
, d_node
)
1691 handle
->handler
->disconnect(handle
);
1692 WARN_ON(!list_empty(&dev
->h_list
));
1694 del_timer_sync(&dev
->timer
);
1695 list_del_init(&dev
->node
);
1697 input_wakeup_procfs_readers();
1699 mutex_unlock(&input_mutex
);
1701 device_unregister(&dev
->dev
);
1703 EXPORT_SYMBOL(input_unregister_device
);
1706 * input_register_handler - register a new input handler
1707 * @handler: handler to be registered
1709 * This function registers a new input handler (interface) for input
1710 * devices in the system and attaches it to all input devices that
1711 * are compatible with the handler.
1713 int input_register_handler(struct input_handler
*handler
)
1715 struct input_dev
*dev
;
1718 retval
= mutex_lock_interruptible(&input_mutex
);
1722 INIT_LIST_HEAD(&handler
->h_list
);
1724 if (handler
->fops
!= NULL
) {
1725 if (input_table
[handler
->minor
>> 5]) {
1729 input_table
[handler
->minor
>> 5] = handler
;
1732 list_add_tail(&handler
->node
, &input_handler_list
);
1734 list_for_each_entry(dev
, &input_dev_list
, node
)
1735 input_attach_handler(dev
, handler
);
1737 input_wakeup_procfs_readers();
1740 mutex_unlock(&input_mutex
);
1743 EXPORT_SYMBOL(input_register_handler
);
1746 * input_unregister_handler - unregisters an input handler
1747 * @handler: handler to be unregistered
1749 * This function disconnects a handler from its input devices and
1750 * removes it from lists of known handlers.
1752 void input_unregister_handler(struct input_handler
*handler
)
1754 struct input_handle
*handle
, *next
;
1756 mutex_lock(&input_mutex
);
1758 list_for_each_entry_safe(handle
, next
, &handler
->h_list
, h_node
)
1759 handler
->disconnect(handle
);
1760 WARN_ON(!list_empty(&handler
->h_list
));
1762 list_del_init(&handler
->node
);
1764 if (handler
->fops
!= NULL
)
1765 input_table
[handler
->minor
>> 5] = NULL
;
1767 input_wakeup_procfs_readers();
1769 mutex_unlock(&input_mutex
);
1771 EXPORT_SYMBOL(input_unregister_handler
);
1774 * input_handler_for_each_handle - handle iterator
1775 * @handler: input handler to iterate
1776 * @data: data for the callback
1777 * @fn: function to be called for each handle
1779 * Iterate over @bus's list of devices, and call @fn for each, passing
1780 * it @data and stop when @fn returns a non-zero value. The function is
1781 * using RCU to traverse the list and therefore may be usind in atonic
1782 * contexts. The @fn callback is invoked from RCU critical section and
1783 * thus must not sleep.
1785 int input_handler_for_each_handle(struct input_handler
*handler
, void *data
,
1786 int (*fn
)(struct input_handle
*, void *))
1788 struct input_handle
*handle
;
1793 list_for_each_entry_rcu(handle
, &handler
->h_list
, h_node
) {
1794 retval
= fn(handle
, data
);
1803 EXPORT_SYMBOL(input_handler_for_each_handle
);
1806 * input_register_handle - register a new input handle
1807 * @handle: handle to register
1809 * This function puts a new input handle onto device's
1810 * and handler's lists so that events can flow through
1811 * it once it is opened using input_open_device().
1813 * This function is supposed to be called from handler's
1816 int input_register_handle(struct input_handle
*handle
)
1818 struct input_handler
*handler
= handle
->handler
;
1819 struct input_dev
*dev
= handle
->dev
;
1823 * We take dev->mutex here to prevent race with
1824 * input_release_device().
1826 error
= mutex_lock_interruptible(&dev
->mutex
);
1831 * Filters go to the head of the list, normal handlers
1834 if (handler
->filter
)
1835 list_add_rcu(&handle
->d_node
, &dev
->h_list
);
1837 list_add_tail_rcu(&handle
->d_node
, &dev
->h_list
);
1839 mutex_unlock(&dev
->mutex
);
1842 * Since we are supposed to be called from ->connect()
1843 * which is mutually exclusive with ->disconnect()
1844 * we can't be racing with input_unregister_handle()
1845 * and so separate lock is not needed here.
1847 list_add_tail_rcu(&handle
->h_node
, &handler
->h_list
);
1850 handler
->start(handle
);
1854 EXPORT_SYMBOL(input_register_handle
);
1857 * input_unregister_handle - unregister an input handle
1858 * @handle: handle to unregister
1860 * This function removes input handle from device's
1861 * and handler's lists.
1863 * This function is supposed to be called from handler's
1864 * disconnect() method.
1866 void input_unregister_handle(struct input_handle
*handle
)
1868 struct input_dev
*dev
= handle
->dev
;
1870 list_del_rcu(&handle
->h_node
);
1873 * Take dev->mutex to prevent race with input_release_device().
1875 mutex_lock(&dev
->mutex
);
1876 list_del_rcu(&handle
->d_node
);
1877 mutex_unlock(&dev
->mutex
);
1881 EXPORT_SYMBOL(input_unregister_handle
);
1883 static int input_open_file(struct inode
*inode
, struct file
*file
)
1885 struct input_handler
*handler
;
1886 const struct file_operations
*old_fops
, *new_fops
= NULL
;
1889 err
= mutex_lock_interruptible(&input_mutex
);
1893 /* No load-on-demand here? */
1894 handler
= input_table
[iminor(inode
) >> 5];
1896 new_fops
= fops_get(handler
->fops
);
1898 mutex_unlock(&input_mutex
);
1901 * That's _really_ odd. Usually NULL ->open means "nothing special",
1902 * not "no device". Oh, well...
1904 if (!new_fops
|| !new_fops
->open
) {
1910 old_fops
= file
->f_op
;
1911 file
->f_op
= new_fops
;
1913 err
= new_fops
->open(inode
, file
);
1915 fops_put(file
->f_op
);
1916 file
->f_op
= fops_get(old_fops
);
1923 static const struct file_operations input_fops
= {
1924 .owner
= THIS_MODULE
,
1925 .open
= input_open_file
,
1928 static void __init
input_init_abs_bypass(void)
1930 const unsigned int *p
;
1932 for (p
= input_abs_bypass_init_data
; *p
; p
++)
1933 input_abs_bypass
[BIT_WORD(*p
)] |= BIT_MASK(*p
);
1936 static int __init
input_init(void)
1940 input_init_abs_bypass();
1942 err
= class_register(&input_class
);
1944 printk(KERN_ERR
"input: unable to register input_dev class\n");
1948 err
= input_proc_init();
1952 err
= register_chrdev(INPUT_MAJOR
, "input", &input_fops
);
1954 printk(KERN_ERR
"input: unable to register char major %d", INPUT_MAJOR
);
1960 fail2
: input_proc_exit();
1961 fail1
: class_unregister(&input_class
);
1965 static void __exit
input_exit(void)
1968 unregister_chrdev(INPUT_MAJOR
, "input");
1969 class_unregister(&input_class
);
1972 subsys_initcall(input_init
);
1973 module_exit(input_exit
);