2 * Event char devices, giving access to raw input device events.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #define EVDEV_MINOR_BASE 64
14 #define EVDEV_MINORS 32
15 #define EVDEV_MIN_BUFFER_SIZE 64U
16 #define EVDEV_BUF_PACKETS 8
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/input.h>
24 #include <linux/major.h>
25 #include <linux/device.h>
26 #include <linux/wakelock.h>
27 #include "input-compat.h"
32 struct input_handle handle
;
33 wait_queue_head_t wait
;
34 struct evdev_client __rcu
*grab
;
35 struct list_head client_list
;
36 spinlock_t client_lock
; /* protects client_list */
45 unsigned int packet_head
; /* [future] position of the first element of next packet */
46 spinlock_t buffer_lock
; /* protects access to buffer, head and tail */
47 struct wake_lock wake_lock
;
50 struct fasync_struct
*fasync
;
52 struct list_head node
;
54 struct input_event buffer
[];
57 static struct evdev
*evdev_table
[EVDEV_MINORS
];
58 static DEFINE_MUTEX(evdev_table_mutex
);
60 static void evdev_pass_event(struct evdev_client
*client
,
61 struct input_event
*event
)
63 /* Interrupts are disabled, just acquire the lock. */
64 spin_lock(&client
->buffer_lock
);
66 client
->buffer
[client
->head
++] = *event
;
67 client
->head
&= client
->bufsize
- 1;
69 if (unlikely(client
->head
== client
->tail
)) {
71 * This effectively "drops" all unconsumed events, leaving
72 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
74 client
->tail
= (client
->head
- 2) & (client
->bufsize
- 1);
76 client
->buffer
[client
->tail
].time
= event
->time
;
77 client
->buffer
[client
->tail
].type
= EV_SYN
;
78 client
->buffer
[client
->tail
].code
= SYN_DROPPED
;
79 client
->buffer
[client
->tail
].value
= 0;
81 client
->packet_head
= client
->tail
;
82 if (client
->use_wake_lock
)
83 wake_unlock(&client
->wake_lock
);
86 if (event
->type
== EV_SYN
&& event
->code
== SYN_REPORT
) {
87 client
->packet_head
= client
->head
;
88 if (client
->use_wake_lock
)
89 wake_lock(&client
->wake_lock
);
90 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
93 spin_unlock(&client
->buffer_lock
);
97 * Pass incoming event to all connected clients.
99 static void evdev_event(struct input_handle
*handle
,
100 unsigned int type
, unsigned int code
, int value
)
102 struct evdev
*evdev
= handle
->private;
103 struct evdev_client
*client
;
104 struct input_event event
;
108 event
.time
.tv_sec
= ts
.tv_sec
;
109 event
.time
.tv_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
116 client
= rcu_dereference(evdev
->grab
);
118 evdev_pass_event(client
, &event
);
120 list_for_each_entry_rcu(client
, &evdev
->client_list
, node
)
121 evdev_pass_event(client
, &event
);
125 if (type
== EV_SYN
&& code
== SYN_REPORT
)
126 wake_up_interruptible(&evdev
->wait
);
129 static int evdev_fasync(int fd
, struct file
*file
, int on
)
131 struct evdev_client
*client
= file
->private_data
;
133 return fasync_helper(fd
, file
, on
, &client
->fasync
);
136 static int evdev_flush(struct file
*file
, fl_owner_t id
)
138 struct evdev_client
*client
= file
->private_data
;
139 struct evdev
*evdev
= client
->evdev
;
142 retval
= mutex_lock_interruptible(&evdev
->mutex
);
149 retval
= input_flush_device(&evdev
->handle
, file
);
151 mutex_unlock(&evdev
->mutex
);
155 static void evdev_free(struct device
*dev
)
157 struct evdev
*evdev
= container_of(dev
, struct evdev
, dev
);
159 input_put_device(evdev
->handle
.dev
);
164 * Grabs an event device (along with underlying input device).
165 * This function is called with evdev->mutex taken.
167 static int evdev_grab(struct evdev
*evdev
, struct evdev_client
*client
)
174 error
= input_grab_device(&evdev
->handle
);
178 rcu_assign_pointer(evdev
->grab
, client
);
183 static int evdev_ungrab(struct evdev
*evdev
, struct evdev_client
*client
)
185 if (evdev
->grab
!= client
)
188 rcu_assign_pointer(evdev
->grab
, NULL
);
190 input_release_device(&evdev
->handle
);
195 static void evdev_attach_client(struct evdev
*evdev
,
196 struct evdev_client
*client
)
198 spin_lock(&evdev
->client_lock
);
199 list_add_tail_rcu(&client
->node
, &evdev
->client_list
);
200 spin_unlock(&evdev
->client_lock
);
203 static void evdev_detach_client(struct evdev
*evdev
,
204 struct evdev_client
*client
)
206 spin_lock(&evdev
->client_lock
);
207 list_del_rcu(&client
->node
);
208 spin_unlock(&evdev
->client_lock
);
212 static int evdev_open_device(struct evdev
*evdev
)
216 retval
= mutex_lock_interruptible(&evdev
->mutex
);
222 else if (!evdev
->open
++) {
223 retval
= input_open_device(&evdev
->handle
);
228 mutex_unlock(&evdev
->mutex
);
232 static void evdev_close_device(struct evdev
*evdev
)
234 mutex_lock(&evdev
->mutex
);
236 if (evdev
->exist
&& !--evdev
->open
)
237 input_close_device(&evdev
->handle
);
239 mutex_unlock(&evdev
->mutex
);
243 * Wake up users waiting for IO so they can disconnect from
246 static void evdev_hangup(struct evdev
*evdev
)
248 struct evdev_client
*client
;
250 spin_lock(&evdev
->client_lock
);
251 list_for_each_entry(client
, &evdev
->client_list
, node
)
252 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
253 spin_unlock(&evdev
->client_lock
);
255 wake_up_interruptible(&evdev
->wait
);
258 static int evdev_release(struct inode
*inode
, struct file
*file
)
260 struct evdev_client
*client
= file
->private_data
;
261 struct evdev
*evdev
= client
->evdev
;
263 mutex_lock(&evdev
->mutex
);
264 if (evdev
->grab
== client
)
265 evdev_ungrab(evdev
, client
);
266 mutex_unlock(&evdev
->mutex
);
268 evdev_detach_client(evdev
, client
);
269 if (client
->use_wake_lock
)
270 wake_lock_destroy(&client
->wake_lock
);
273 evdev_close_device(evdev
);
274 put_device(&evdev
->dev
);
279 static unsigned int evdev_compute_buffer_size(struct input_dev
*dev
)
281 unsigned int n_events
=
282 max(dev
->hint_events_per_packet
* EVDEV_BUF_PACKETS
,
283 EVDEV_MIN_BUFFER_SIZE
);
285 return roundup_pow_of_two(n_events
);
288 static int evdev_open(struct inode
*inode
, struct file
*file
)
291 struct evdev_client
*client
;
292 int i
= iminor(inode
) - EVDEV_MINOR_BASE
;
293 unsigned int bufsize
;
296 if (i
>= EVDEV_MINORS
)
299 error
= mutex_lock_interruptible(&evdev_table_mutex
);
302 evdev
= evdev_table
[i
];
304 get_device(&evdev
->dev
);
305 mutex_unlock(&evdev_table_mutex
);
310 bufsize
= evdev_compute_buffer_size(evdev
->handle
.dev
);
312 client
= kzalloc(sizeof(struct evdev_client
) +
313 bufsize
* sizeof(struct input_event
),
320 client
->bufsize
= bufsize
;
321 spin_lock_init(&client
->buffer_lock
);
322 snprintf(client
->name
, sizeof(client
->name
), "%s-%d",
323 dev_name(&evdev
->dev
), task_tgid_vnr(current
));
324 client
->evdev
= evdev
;
325 evdev_attach_client(evdev
, client
);
327 error
= evdev_open_device(evdev
);
329 goto err_free_client
;
331 file
->private_data
= client
;
332 nonseekable_open(inode
, file
);
337 evdev_detach_client(evdev
, client
);
340 put_device(&evdev
->dev
);
344 static ssize_t
evdev_write(struct file
*file
, const char __user
*buffer
,
345 size_t count
, loff_t
*ppos
)
347 struct evdev_client
*client
= file
->private_data
;
348 struct evdev
*evdev
= client
->evdev
;
349 struct input_event event
;
352 if (count
< input_event_size())
355 retval
= mutex_lock_interruptible(&evdev
->mutex
);
365 if (input_event_from_user(buffer
+ retval
, &event
)) {
369 retval
+= input_event_size();
371 input_inject_event(&evdev
->handle
,
372 event
.type
, event
.code
, event
.value
);
373 } while (retval
+ input_event_size() <= count
);
376 mutex_unlock(&evdev
->mutex
);
380 static int evdev_fetch_next_event(struct evdev_client
*client
,
381 struct input_event
*event
)
385 spin_lock_irq(&client
->buffer_lock
);
387 have_event
= client
->packet_head
!= client
->tail
;
389 *event
= client
->buffer
[client
->tail
++];
390 client
->tail
&= client
->bufsize
- 1;
391 if (client
->use_wake_lock
&&
392 client
->packet_head
== client
->tail
)
393 wake_unlock(&client
->wake_lock
);
396 spin_unlock_irq(&client
->buffer_lock
);
401 static ssize_t
evdev_read(struct file
*file
, char __user
*buffer
,
402 size_t count
, loff_t
*ppos
)
404 struct evdev_client
*client
= file
->private_data
;
405 struct evdev
*evdev
= client
->evdev
;
406 struct input_event event
;
409 if (count
< input_event_size())
412 if (!(file
->f_flags
& O_NONBLOCK
)) {
413 retval
= wait_event_interruptible(evdev
->wait
,
414 client
->packet_head
!= client
->tail
|| !evdev
->exist
);
422 while (retval
+ input_event_size() <= count
&&
423 evdev_fetch_next_event(client
, &event
)) {
425 if (input_event_to_user(buffer
+ retval
, &event
))
428 retval
+= input_event_size();
431 if (retval
== 0 && file
->f_flags
& O_NONBLOCK
)
436 /* No kernel lock - fine */
437 static unsigned int evdev_poll(struct file
*file
, poll_table
*wait
)
439 struct evdev_client
*client
= file
->private_data
;
440 struct evdev
*evdev
= client
->evdev
;
443 poll_wait(file
, &evdev
->wait
, wait
);
445 mask
= evdev
->exist
? POLLOUT
| POLLWRNORM
: POLLHUP
| POLLERR
;
446 if (client
->packet_head
!= client
->tail
)
447 mask
|= POLLIN
| POLLRDNORM
;
454 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
455 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
458 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
459 unsigned int maxlen
, void __user
*p
, int compat
)
464 len
= BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
);
468 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
469 if (copy_to_user((compat_long_t __user
*) p
+ i
,
470 (compat_long_t
*) bits
+
471 i
+ 1 - ((i
% 2) << 1),
472 sizeof(compat_long_t
)))
475 len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
479 if (copy_to_user(p
, bits
, len
))
486 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
487 unsigned int maxlen
, void __user
*p
, int compat
)
490 BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
) :
491 BITS_TO_LONGS(maxbit
) * sizeof(long);
496 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
498 #endif /* __BIG_ENDIAN */
502 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
503 unsigned int maxlen
, void __user
*p
, int compat
)
505 int len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
510 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
513 #endif /* CONFIG_COMPAT */
515 static int str_to_user(const char *str
, unsigned int maxlen
, void __user
*p
)
522 len
= strlen(str
) + 1;
526 return copy_to_user(p
, str
, len
) ? -EFAULT
: len
;
529 #define OLD_KEY_MAX 0x1ff
530 static int handle_eviocgbit(struct input_dev
*dev
,
531 unsigned int type
, unsigned int size
,
532 void __user
*p
, int compat_mode
)
534 static unsigned long keymax_warn_time
;
540 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
541 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
542 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
543 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
544 case EV_MSC
: bits
= dev
->mscbit
; len
= MSC_MAX
; break;
545 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
546 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
547 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
548 case EV_SW
: bits
= dev
->swbit
; len
= SW_MAX
; break;
549 default: return -EINVAL
;
553 * Work around bugs in userspace programs that like to do
554 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
555 * should be in bytes, not in bits.
557 if (type
== EV_KEY
&& size
== OLD_KEY_MAX
) {
559 if (printk_timed_ratelimit(&keymax_warn_time
, 10 * 1000))
560 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
561 "limiting output to %zu bytes. See "
562 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
564 BITS_TO_LONGS(OLD_KEY_MAX
) * sizeof(long));
567 return bits_to_user(bits
, len
, size
, p
, compat_mode
);
571 static int evdev_handle_get_keycode(struct input_dev
*dev
, void __user
*p
)
573 struct input_keymap_entry ke
= {
574 .len
= sizeof(unsigned int),
577 int __user
*ip
= (int __user
*)p
;
581 if (copy_from_user(ke
.scancode
, p
, sizeof(unsigned int)))
584 error
= input_get_keycode(dev
, &ke
);
588 if (put_user(ke
.keycode
, ip
+ 1))
594 static int evdev_handle_get_keycode_v2(struct input_dev
*dev
, void __user
*p
)
596 struct input_keymap_entry ke
;
599 if (copy_from_user(&ke
, p
, sizeof(ke
)))
602 error
= input_get_keycode(dev
, &ke
);
606 if (copy_to_user(p
, &ke
, sizeof(ke
)))
612 static int evdev_handle_set_keycode(struct input_dev
*dev
, void __user
*p
)
614 struct input_keymap_entry ke
= {
615 .len
= sizeof(unsigned int),
618 int __user
*ip
= (int __user
*)p
;
620 if (copy_from_user(ke
.scancode
, p
, sizeof(unsigned int)))
623 if (get_user(ke
.keycode
, ip
+ 1))
626 return input_set_keycode(dev
, &ke
);
629 static int evdev_handle_set_keycode_v2(struct input_dev
*dev
, void __user
*p
)
631 struct input_keymap_entry ke
;
633 if (copy_from_user(&ke
, p
, sizeof(ke
)))
636 if (ke
.len
> sizeof(ke
.scancode
))
639 return input_set_keycode(dev
, &ke
);
642 static int evdev_enable_suspend_block(struct evdev
*evdev
,
643 struct evdev_client
*client
)
645 if (client
->use_wake_lock
)
648 spin_lock_irq(&client
->buffer_lock
);
649 wake_lock_init(&client
->wake_lock
, WAKE_LOCK_SUSPEND
, client
->name
);
650 client
->use_wake_lock
= true;
651 if (client
->packet_head
!= client
->tail
)
652 wake_lock(&client
->wake_lock
);
653 spin_unlock_irq(&client
->buffer_lock
);
657 static int evdev_disable_suspend_block(struct evdev
*evdev
,
658 struct evdev_client
*client
)
660 if (!client
->use_wake_lock
)
663 spin_lock_irq(&client
->buffer_lock
);
664 client
->use_wake_lock
= false;
665 wake_lock_destroy(&client
->wake_lock
);
666 spin_unlock_irq(&client
->buffer_lock
);
671 static long evdev_do_ioctl(struct file
*file
, unsigned int cmd
,
672 void __user
*p
, int compat_mode
)
674 struct evdev_client
*client
= file
->private_data
;
675 struct evdev
*evdev
= client
->evdev
;
676 struct input_dev
*dev
= evdev
->handle
.dev
;
677 struct input_absinfo abs
;
678 struct ff_effect effect
;
679 int __user
*ip
= (int __user
*)p
;
680 unsigned int i
, t
, u
, v
;
684 /* First we check for fixed-length commands */
688 return put_user(EV_VERSION
, ip
);
691 if (copy_to_user(p
, &dev
->id
, sizeof(struct input_id
)))
696 if (!test_bit(EV_REP
, dev
->evbit
))
698 if (put_user(dev
->rep
[REP_DELAY
], ip
))
700 if (put_user(dev
->rep
[REP_PERIOD
], ip
+ 1))
705 if (!test_bit(EV_REP
, dev
->evbit
))
709 if (get_user(v
, ip
+ 1))
712 input_inject_event(&evdev
->handle
, EV_REP
, REP_DELAY
, u
);
713 input_inject_event(&evdev
->handle
, EV_REP
, REP_PERIOD
, v
);
718 return input_ff_erase(dev
, (int)(unsigned long) p
, file
);
721 i
= test_bit(EV_FF
, dev
->evbit
) ?
722 dev
->ff
->max_effects
: 0;
729 return evdev_grab(evdev
, client
);
731 return evdev_ungrab(evdev
, client
);
734 return evdev_handle_get_keycode(dev
, p
);
737 return evdev_handle_set_keycode(dev
, p
);
739 case EVIOCGKEYCODE_V2
:
740 return evdev_handle_get_keycode_v2(dev
, p
);
742 case EVIOCSKEYCODE_V2
:
743 return evdev_handle_set_keycode_v2(dev
, p
);
745 case EVIOCGSUSPENDBLOCK
:
746 return put_user(client
->use_wake_lock
, ip
);
748 case EVIOCSSUSPENDBLOCK
:
750 return evdev_enable_suspend_block(evdev
, client
);
752 return evdev_disable_suspend_block(evdev
, client
);
755 size
= _IOC_SIZE(cmd
);
757 /* Now check variable-length commands */
758 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
759 switch (EVIOC_MASK_SIZE(cmd
)) {
762 return bits_to_user(dev
->propbit
, INPUT_PROP_MAX
,
763 size
, p
, compat_mode
);
766 return bits_to_user(dev
->key
, KEY_MAX
, size
, p
, compat_mode
);
769 return bits_to_user(dev
->led
, LED_MAX
, size
, p
, compat_mode
);
772 return bits_to_user(dev
->snd
, SND_MAX
, size
, p
, compat_mode
);
775 return bits_to_user(dev
->sw
, SW_MAX
, size
, p
, compat_mode
);
778 return str_to_user(dev
->name
, size
, p
);
781 return str_to_user(dev
->phys
, size
, p
);
784 return str_to_user(dev
->uniq
, size
, p
);
786 case EVIOC_MASK_SIZE(EVIOCSFF
):
787 if (input_ff_effect_from_user(p
, size
, &effect
))
790 error
= input_ff_upload(dev
, &effect
, file
);
792 if (put_user(effect
.id
, &(((struct ff_effect __user
*)p
)->id
)))
798 /* Multi-number variable-length handlers */
799 if (_IOC_TYPE(cmd
) != 'E')
802 if (_IOC_DIR(cmd
) == _IOC_READ
) {
804 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0, 0)))
805 return handle_eviocgbit(dev
,
806 _IOC_NR(cmd
) & EV_MAX
, size
,
809 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
814 t
= _IOC_NR(cmd
) & ABS_MAX
;
815 abs
= dev
->absinfo
[t
];
817 if (copy_to_user(p
, &abs
, min_t(size_t,
818 size
, sizeof(struct input_absinfo
))))
825 if (_IOC_DIR(cmd
) == _IOC_WRITE
) {
827 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
832 t
= _IOC_NR(cmd
) & ABS_MAX
;
834 if (copy_from_user(&abs
, p
, min_t(size_t,
835 size
, sizeof(struct input_absinfo
))))
838 if (size
< sizeof(struct input_absinfo
))
841 /* We can't change number of reserved MT slots */
842 if (t
== ABS_MT_SLOT
)
846 * Take event lock to ensure that we are not
847 * changing device parameters in the middle
850 spin_lock_irq(&dev
->event_lock
);
851 dev
->absinfo
[t
] = abs
;
852 spin_unlock_irq(&dev
->event_lock
);
861 static long evdev_ioctl_handler(struct file
*file
, unsigned int cmd
,
862 void __user
*p
, int compat_mode
)
864 struct evdev_client
*client
= file
->private_data
;
865 struct evdev
*evdev
= client
->evdev
;
868 retval
= mutex_lock_interruptible(&evdev
->mutex
);
877 retval
= evdev_do_ioctl(file
, cmd
, p
, compat_mode
);
880 mutex_unlock(&evdev
->mutex
);
884 static long evdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
886 return evdev_ioctl_handler(file
, cmd
, (void __user
*)arg
, 0);
890 static long evdev_ioctl_compat(struct file
*file
,
891 unsigned int cmd
, unsigned long arg
)
893 return evdev_ioctl_handler(file
, cmd
, compat_ptr(arg
), 1);
897 static const struct file_operations evdev_fops
= {
898 .owner
= THIS_MODULE
,
900 .write
= evdev_write
,
903 .release
= evdev_release
,
904 .unlocked_ioctl
= evdev_ioctl
,
906 .compat_ioctl
= evdev_ioctl_compat
,
908 .fasync
= evdev_fasync
,
909 .flush
= evdev_flush
,
913 static int evdev_install_chrdev(struct evdev
*evdev
)
916 * No need to do any locking here as calls to connect and
917 * disconnect are serialized by the input core
919 evdev_table
[evdev
->minor
] = evdev
;
923 static void evdev_remove_chrdev(struct evdev
*evdev
)
926 * Lock evdev table to prevent race with evdev_open()
928 mutex_lock(&evdev_table_mutex
);
929 evdev_table
[evdev
->minor
] = NULL
;
930 mutex_unlock(&evdev_table_mutex
);
934 * Mark device non-existent. This disables writes, ioctls and
935 * prevents new users from opening the device. Already posted
936 * blocking reads will stay, however new ones will fail.
938 static void evdev_mark_dead(struct evdev
*evdev
)
940 mutex_lock(&evdev
->mutex
);
941 evdev
->exist
= false;
942 mutex_unlock(&evdev
->mutex
);
945 static void evdev_cleanup(struct evdev
*evdev
)
947 struct input_handle
*handle
= &evdev
->handle
;
949 evdev_mark_dead(evdev
);
951 evdev_remove_chrdev(evdev
);
953 /* evdev is marked dead so no one else accesses evdev->open */
955 input_flush_device(handle
, NULL
);
956 input_close_device(handle
);
961 * Create new evdev device. Note that input core serializes calls
962 * to connect and disconnect so we don't need to lock evdev_table here.
964 static int evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
,
965 const struct input_device_id
*id
)
971 for (minor
= 0; minor
< EVDEV_MINORS
; minor
++)
972 if (!evdev_table
[minor
])
975 if (minor
== EVDEV_MINORS
) {
976 pr_err("no more free evdev devices\n");
980 evdev
= kzalloc(sizeof(struct evdev
), GFP_KERNEL
);
984 INIT_LIST_HEAD(&evdev
->client_list
);
985 spin_lock_init(&evdev
->client_lock
);
986 mutex_init(&evdev
->mutex
);
987 init_waitqueue_head(&evdev
->wait
);
989 dev_set_name(&evdev
->dev
, "event%d", minor
);
991 evdev
->minor
= minor
;
993 evdev
->handle
.dev
= input_get_device(dev
);
994 evdev
->handle
.name
= dev_name(&evdev
->dev
);
995 evdev
->handle
.handler
= handler
;
996 evdev
->handle
.private = evdev
;
998 evdev
->dev
.devt
= MKDEV(INPUT_MAJOR
, EVDEV_MINOR_BASE
+ minor
);
999 evdev
->dev
.class = &input_class
;
1000 evdev
->dev
.parent
= &dev
->dev
;
1001 evdev
->dev
.release
= evdev_free
;
1002 device_initialize(&evdev
->dev
);
1004 error
= input_register_handle(&evdev
->handle
);
1006 goto err_free_evdev
;
1008 error
= evdev_install_chrdev(evdev
);
1010 goto err_unregister_handle
;
1012 error
= device_add(&evdev
->dev
);
1014 goto err_cleanup_evdev
;
1019 evdev_cleanup(evdev
);
1020 err_unregister_handle
:
1021 input_unregister_handle(&evdev
->handle
);
1023 put_device(&evdev
->dev
);
1027 static void evdev_disconnect(struct input_handle
*handle
)
1029 struct evdev
*evdev
= handle
->private;
1031 device_del(&evdev
->dev
);
1032 evdev_cleanup(evdev
);
1033 input_unregister_handle(handle
);
1034 put_device(&evdev
->dev
);
1037 static const struct input_device_id evdev_ids
[] = {
1038 { .driver_info
= 1 }, /* Matches all devices */
1039 { }, /* Terminating zero entry */
1042 MODULE_DEVICE_TABLE(input
, evdev_ids
);
1044 static struct input_handler evdev_handler
= {
1045 .event
= evdev_event
,
1046 .connect
= evdev_connect
,
1047 .disconnect
= evdev_disconnect
,
1048 .fops
= &evdev_fops
,
1049 .minor
= EVDEV_MINOR_BASE
,
1051 .id_table
= evdev_ids
,
1054 static int __init
evdev_init(void)
1056 return input_register_handler(&evdev_handler
);
1059 static void __exit
evdev_exit(void)
1061 input_unregister_handler(&evdev_handler
);
1064 module_init(evdev_init
);
1065 module_exit(evdev_exit
);
1067 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1068 MODULE_DESCRIPTION("Input driver event char devices");
1069 MODULE_LICENSE("GPL");