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/vmalloc.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/input/mt.h>
26 #include <linux/major.h>
27 #include <linux/device.h>
28 #include <linux/cdev.h>
29 #include "input-compat.h"
31 enum evdev_clock_type
{
40 struct input_handle handle
;
41 wait_queue_head_t wait
;
42 struct evdev_client __rcu
*grab
;
43 struct list_head client_list
;
44 spinlock_t client_lock
; /* protects client_list */
54 unsigned int packet_head
; /* [future] position of the first element of next packet */
55 spinlock_t buffer_lock
; /* protects access to buffer, head and tail */
56 struct fasync_struct
*fasync
;
58 struct list_head node
;
62 struct input_event buffer
[];
65 static int evdev_set_clk_type(struct evdev_client
*client
, unsigned int clkid
)
70 client
->clk_type
= EV_CLK_REAL
;
73 client
->clk_type
= EV_CLK_MONO
;
76 client
->clk_type
= EV_CLK_BOOT
;
85 /* flush queued events of type @type, caller must hold client->buffer_lock */
86 static void __evdev_flush_queue(struct evdev_client
*client
, unsigned int type
)
88 unsigned int i
, head
, num
;
89 unsigned int mask
= client
->bufsize
- 1;
91 struct input_event
*ev
;
93 BUG_ON(type
== EV_SYN
);
96 client
->packet_head
= client
->tail
;
98 /* init to 1 so a leading SYN_REPORT will not be dropped */
101 for (i
= client
->tail
; i
!= client
->head
; i
= (i
+ 1) & mask
) {
102 ev
= &client
->buffer
[i
];
103 is_report
= ev
->type
== EV_SYN
&& ev
->code
== SYN_REPORT
;
105 if (ev
->type
== type
) {
106 /* drop matched entry */
108 } else if (is_report
&& !num
) {
109 /* drop empty SYN_REPORT groups */
111 } else if (head
!= i
) {
112 /* move entry to fill the gap */
113 client
->buffer
[head
].time
= ev
->time
;
114 client
->buffer
[head
].type
= ev
->type
;
115 client
->buffer
[head
].code
= ev
->code
;
116 client
->buffer
[head
].value
= ev
->value
;
120 head
= (head
+ 1) & mask
;
124 client
->packet_head
= head
;
131 /* queue SYN_DROPPED event */
132 static void evdev_queue_syn_dropped(struct evdev_client
*client
)
135 struct input_event ev
;
138 time
= client
->clk_type
== EV_CLK_REAL
?
140 client
->clk_type
== EV_CLK_MONO
?
142 ktime_get_boottime();
144 ev
.time
= ktime_to_timeval(time
);
146 ev
.code
= SYN_DROPPED
;
149 spin_lock_irqsave(&client
->buffer_lock
, flags
);
151 client
->buffer
[client
->head
++] = ev
;
152 client
->head
&= client
->bufsize
- 1;
154 if (unlikely(client
->head
== client
->tail
)) {
155 /* drop queue but keep our SYN_DROPPED event */
156 client
->tail
= (client
->head
- 1) & (client
->bufsize
- 1);
157 client
->packet_head
= client
->tail
;
160 spin_unlock_irqrestore(&client
->buffer_lock
, flags
);
163 static void __pass_event(struct evdev_client
*client
,
164 const struct input_event
*event
)
166 client
->buffer
[client
->head
++] = *event
;
167 client
->head
&= client
->bufsize
- 1;
169 if (unlikely(client
->head
== client
->tail
)) {
171 * This effectively "drops" all unconsumed events, leaving
172 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
174 client
->tail
= (client
->head
- 2) & (client
->bufsize
- 1);
176 client
->buffer
[client
->tail
].time
= event
->time
;
177 client
->buffer
[client
->tail
].type
= EV_SYN
;
178 client
->buffer
[client
->tail
].code
= SYN_DROPPED
;
179 client
->buffer
[client
->tail
].value
= 0;
181 client
->packet_head
= client
->tail
;
184 if (event
->type
== EV_SYN
&& event
->code
== SYN_REPORT
) {
185 client
->packet_head
= client
->head
;
186 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
190 static void evdev_pass_values(struct evdev_client
*client
,
191 const struct input_value
*vals
, unsigned int count
,
194 struct evdev
*evdev
= client
->evdev
;
195 const struct input_value
*v
;
196 struct input_event event
;
202 event
.time
= ktime_to_timeval(ev_time
[client
->clk_type
]);
204 /* Interrupts are disabled, just acquire the lock. */
205 spin_lock(&client
->buffer_lock
);
207 for (v
= vals
; v
!= vals
+ count
; v
++) {
208 event
.type
= v
->type
;
209 event
.code
= v
->code
;
210 event
.value
= v
->value
;
211 __pass_event(client
, &event
);
212 if (v
->type
== EV_SYN
&& v
->code
== SYN_REPORT
)
216 spin_unlock(&client
->buffer_lock
);
219 wake_up_interruptible(&evdev
->wait
);
223 * Pass incoming events to all connected clients.
225 static void evdev_events(struct input_handle
*handle
,
226 const struct input_value
*vals
, unsigned int count
)
228 struct evdev
*evdev
= handle
->private;
229 struct evdev_client
*client
;
230 ktime_t ev_time
[EV_CLK_MAX
];
232 ev_time
[EV_CLK_MONO
] = ktime_get();
233 ev_time
[EV_CLK_REAL
] = ktime_mono_to_real(ev_time
[EV_CLK_MONO
]);
234 ev_time
[EV_CLK_BOOT
] = ktime_mono_to_any(ev_time
[EV_CLK_MONO
],
239 client
= rcu_dereference(evdev
->grab
);
242 evdev_pass_values(client
, vals
, count
, ev_time
);
244 list_for_each_entry_rcu(client
, &evdev
->client_list
, node
)
245 evdev_pass_values(client
, vals
, count
, ev_time
);
251 * Pass incoming event to all connected clients.
253 static void evdev_event(struct input_handle
*handle
,
254 unsigned int type
, unsigned int code
, int value
)
256 struct input_value vals
[] = { { type
, code
, value
} };
258 evdev_events(handle
, vals
, 1);
261 static int evdev_fasync(int fd
, struct file
*file
, int on
)
263 struct evdev_client
*client
= file
->private_data
;
265 return fasync_helper(fd
, file
, on
, &client
->fasync
);
268 static int evdev_flush(struct file
*file
, fl_owner_t id
)
270 struct evdev_client
*client
= file
->private_data
;
271 struct evdev
*evdev
= client
->evdev
;
274 retval
= mutex_lock_interruptible(&evdev
->mutex
);
278 if (!evdev
->exist
|| client
->revoked
)
281 retval
= input_flush_device(&evdev
->handle
, file
);
283 mutex_unlock(&evdev
->mutex
);
287 static void evdev_free(struct device
*dev
)
289 struct evdev
*evdev
= container_of(dev
, struct evdev
, dev
);
291 input_put_device(evdev
->handle
.dev
);
296 * Grabs an event device (along with underlying input device).
297 * This function is called with evdev->mutex taken.
299 static int evdev_grab(struct evdev
*evdev
, struct evdev_client
*client
)
306 error
= input_grab_device(&evdev
->handle
);
310 rcu_assign_pointer(evdev
->grab
, client
);
315 static int evdev_ungrab(struct evdev
*evdev
, struct evdev_client
*client
)
317 struct evdev_client
*grab
= rcu_dereference_protected(evdev
->grab
,
318 lockdep_is_held(&evdev
->mutex
));
323 rcu_assign_pointer(evdev
->grab
, NULL
);
325 input_release_device(&evdev
->handle
);
330 static void evdev_attach_client(struct evdev
*evdev
,
331 struct evdev_client
*client
)
333 spin_lock(&evdev
->client_lock
);
334 list_add_tail_rcu(&client
->node
, &evdev
->client_list
);
335 spin_unlock(&evdev
->client_lock
);
338 static void evdev_detach_client(struct evdev
*evdev
,
339 struct evdev_client
*client
)
341 spin_lock(&evdev
->client_lock
);
342 list_del_rcu(&client
->node
);
343 spin_unlock(&evdev
->client_lock
);
347 static int evdev_open_device(struct evdev
*evdev
)
351 retval
= mutex_lock_interruptible(&evdev
->mutex
);
357 else if (!evdev
->open
++) {
358 retval
= input_open_device(&evdev
->handle
);
363 mutex_unlock(&evdev
->mutex
);
367 static void evdev_close_device(struct evdev
*evdev
)
369 mutex_lock(&evdev
->mutex
);
371 if (evdev
->exist
&& !--evdev
->open
)
372 input_close_device(&evdev
->handle
);
374 mutex_unlock(&evdev
->mutex
);
378 * Wake up users waiting for IO so they can disconnect from
381 static void evdev_hangup(struct evdev
*evdev
)
383 struct evdev_client
*client
;
385 spin_lock(&evdev
->client_lock
);
386 list_for_each_entry(client
, &evdev
->client_list
, node
)
387 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
388 spin_unlock(&evdev
->client_lock
);
390 wake_up_interruptible(&evdev
->wait
);
393 static int evdev_release(struct inode
*inode
, struct file
*file
)
395 struct evdev_client
*client
= file
->private_data
;
396 struct evdev
*evdev
= client
->evdev
;
398 mutex_lock(&evdev
->mutex
);
399 evdev_ungrab(evdev
, client
);
400 mutex_unlock(&evdev
->mutex
);
402 evdev_detach_client(evdev
, client
);
404 if (is_vmalloc_addr(client
))
409 evdev_close_device(evdev
);
414 static unsigned int evdev_compute_buffer_size(struct input_dev
*dev
)
416 unsigned int n_events
=
417 max(dev
->hint_events_per_packet
* EVDEV_BUF_PACKETS
,
418 EVDEV_MIN_BUFFER_SIZE
);
420 return roundup_pow_of_two(n_events
);
423 static int evdev_open(struct inode
*inode
, struct file
*file
)
425 struct evdev
*evdev
= container_of(inode
->i_cdev
, struct evdev
, cdev
);
426 unsigned int bufsize
= evdev_compute_buffer_size(evdev
->handle
.dev
);
427 unsigned int size
= sizeof(struct evdev_client
) +
428 bufsize
* sizeof(struct input_event
);
429 struct evdev_client
*client
;
432 client
= kzalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
434 client
= vzalloc(size
);
438 client
->bufsize
= bufsize
;
439 spin_lock_init(&client
->buffer_lock
);
440 client
->evdev
= evdev
;
441 evdev_attach_client(evdev
, client
);
443 error
= evdev_open_device(evdev
);
445 goto err_free_client
;
447 file
->private_data
= client
;
448 nonseekable_open(inode
, file
);
453 evdev_detach_client(evdev
, client
);
458 static ssize_t
evdev_write(struct file
*file
, const char __user
*buffer
,
459 size_t count
, loff_t
*ppos
)
461 struct evdev_client
*client
= file
->private_data
;
462 struct evdev
*evdev
= client
->evdev
;
463 struct input_event event
;
466 if (count
!= 0 && count
< input_event_size())
469 retval
= mutex_lock_interruptible(&evdev
->mutex
);
473 if (!evdev
->exist
|| client
->revoked
) {
478 while (retval
+ input_event_size() <= count
) {
480 if (input_event_from_user(buffer
+ retval
, &event
)) {
484 retval
+= input_event_size();
486 input_inject_event(&evdev
->handle
,
487 event
.type
, event
.code
, event
.value
);
491 mutex_unlock(&evdev
->mutex
);
495 static int evdev_fetch_next_event(struct evdev_client
*client
,
496 struct input_event
*event
)
500 spin_lock_irq(&client
->buffer_lock
);
502 have_event
= client
->packet_head
!= client
->tail
;
504 *event
= client
->buffer
[client
->tail
++];
505 client
->tail
&= client
->bufsize
- 1;
508 spin_unlock_irq(&client
->buffer_lock
);
513 static ssize_t
evdev_read(struct file
*file
, char __user
*buffer
,
514 size_t count
, loff_t
*ppos
)
516 struct evdev_client
*client
= file
->private_data
;
517 struct evdev
*evdev
= client
->evdev
;
518 struct input_event event
;
522 if (count
!= 0 && count
< input_event_size())
526 if (!evdev
->exist
|| client
->revoked
)
529 if (client
->packet_head
== client
->tail
&&
530 (file
->f_flags
& O_NONBLOCK
))
534 * count == 0 is special - no IO is done but we check
535 * for error conditions (see above).
540 while (read
+ input_event_size() <= count
&&
541 evdev_fetch_next_event(client
, &event
)) {
543 if (input_event_to_user(buffer
+ read
, &event
))
546 read
+= input_event_size();
552 if (!(file
->f_flags
& O_NONBLOCK
)) {
553 error
= wait_event_interruptible(evdev
->wait
,
554 client
->packet_head
!= client
->tail
||
555 !evdev
->exist
|| client
->revoked
);
564 /* No kernel lock - fine */
565 static unsigned int evdev_poll(struct file
*file
, poll_table
*wait
)
567 struct evdev_client
*client
= file
->private_data
;
568 struct evdev
*evdev
= client
->evdev
;
571 poll_wait(file
, &evdev
->wait
, wait
);
573 if (evdev
->exist
&& !client
->revoked
)
574 mask
= POLLOUT
| POLLWRNORM
;
576 mask
= POLLHUP
| POLLERR
;
578 if (client
->packet_head
!= client
->tail
)
579 mask
|= POLLIN
| POLLRDNORM
;
586 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
587 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
590 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
591 unsigned int maxlen
, void __user
*p
, int compat
)
596 len
= BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
);
600 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
601 if (copy_to_user((compat_long_t __user
*) p
+ i
,
602 (compat_long_t
*) bits
+
603 i
+ 1 - ((i
% 2) << 1),
604 sizeof(compat_long_t
)))
607 len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
611 if (copy_to_user(p
, bits
, len
))
618 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
619 unsigned int maxlen
, void __user
*p
, int compat
)
622 BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
) :
623 BITS_TO_LONGS(maxbit
) * sizeof(long);
628 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
630 #endif /* __BIG_ENDIAN */
634 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
635 unsigned int maxlen
, void __user
*p
, int compat
)
637 int len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
642 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
645 #endif /* CONFIG_COMPAT */
647 static int str_to_user(const char *str
, unsigned int maxlen
, void __user
*p
)
654 len
= strlen(str
) + 1;
658 return copy_to_user(p
, str
, len
) ? -EFAULT
: len
;
661 static int handle_eviocgbit(struct input_dev
*dev
,
662 unsigned int type
, unsigned int size
,
663 void __user
*p
, int compat_mode
)
670 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
671 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
672 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
673 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
674 case EV_MSC
: bits
= dev
->mscbit
; len
= MSC_MAX
; break;
675 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
676 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
677 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
678 case EV_SW
: bits
= dev
->swbit
; len
= SW_MAX
; break;
679 default: return -EINVAL
;
682 return bits_to_user(bits
, len
, size
, p
, compat_mode
);
685 static int evdev_handle_get_keycode(struct input_dev
*dev
, void __user
*p
)
687 struct input_keymap_entry ke
= {
688 .len
= sizeof(unsigned int),
691 int __user
*ip
= (int __user
*)p
;
695 if (copy_from_user(ke
.scancode
, p
, sizeof(unsigned int)))
698 error
= input_get_keycode(dev
, &ke
);
702 if (put_user(ke
.keycode
, ip
+ 1))
708 static int evdev_handle_get_keycode_v2(struct input_dev
*dev
, void __user
*p
)
710 struct input_keymap_entry ke
;
713 if (copy_from_user(&ke
, p
, sizeof(ke
)))
716 error
= input_get_keycode(dev
, &ke
);
720 if (copy_to_user(p
, &ke
, sizeof(ke
)))
726 static int evdev_handle_set_keycode(struct input_dev
*dev
, void __user
*p
)
728 struct input_keymap_entry ke
= {
729 .len
= sizeof(unsigned int),
732 int __user
*ip
= (int __user
*)p
;
734 if (copy_from_user(ke
.scancode
, p
, sizeof(unsigned int)))
737 if (get_user(ke
.keycode
, ip
+ 1))
740 return input_set_keycode(dev
, &ke
);
743 static int evdev_handle_set_keycode_v2(struct input_dev
*dev
, void __user
*p
)
745 struct input_keymap_entry ke
;
747 if (copy_from_user(&ke
, p
, sizeof(ke
)))
750 if (ke
.len
> sizeof(ke
.scancode
))
753 return input_set_keycode(dev
, &ke
);
757 * If we transfer state to the user, we should flush all pending events
758 * of the same type from the client's queue. Otherwise, they might end up
759 * with duplicate events, which can screw up client's state tracking.
760 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
761 * event so user-space will notice missing events.
764 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
765 * need the even_lock only to guarantee consistent state. We can safely release
766 * it while flushing the queue. This allows input-core to handle filters while
767 * we flush the queue.
769 static int evdev_handle_get_val(struct evdev_client
*client
,
770 struct input_dev
*dev
, unsigned int type
,
771 unsigned long *bits
, unsigned int maxbit
,
772 unsigned int maxlen
, void __user
*p
,
779 len
= BITS_TO_LONGS(maxbit
) * sizeof(unsigned long);
780 mem
= kmalloc(len
, GFP_KERNEL
);
784 spin_lock_irq(&dev
->event_lock
);
785 spin_lock(&client
->buffer_lock
);
787 memcpy(mem
, bits
, len
);
789 spin_unlock(&dev
->event_lock
);
791 __evdev_flush_queue(client
, type
);
793 spin_unlock_irq(&client
->buffer_lock
);
795 ret
= bits_to_user(mem
, maxbit
, maxlen
, p
, compat
);
797 evdev_queue_syn_dropped(client
);
804 static int evdev_handle_mt_request(struct input_dev
*dev
,
808 const struct input_mt
*mt
= dev
->mt
;
813 if (get_user(code
, &ip
[0]))
815 if (!mt
|| !input_is_mt_value(code
))
818 max_slots
= (size
- sizeof(__u32
)) / sizeof(__s32
);
819 for (i
= 0; i
< mt
->num_slots
&& i
< max_slots
; i
++) {
820 int value
= input_mt_get_value(&mt
->slots
[i
], code
);
821 if (put_user(value
, &ip
[1 + i
]))
828 static int evdev_revoke(struct evdev
*evdev
, struct evdev_client
*client
,
831 client
->revoked
= true;
832 evdev_ungrab(evdev
, client
);
833 input_flush_device(&evdev
->handle
, file
);
834 wake_up_interruptible(&evdev
->wait
);
839 static long evdev_do_ioctl(struct file
*file
, unsigned int cmd
,
840 void __user
*p
, int compat_mode
)
842 struct evdev_client
*client
= file
->private_data
;
843 struct evdev
*evdev
= client
->evdev
;
844 struct input_dev
*dev
= evdev
->handle
.dev
;
845 struct input_absinfo abs
;
846 struct ff_effect effect
;
847 int __user
*ip
= (int __user
*)p
;
848 unsigned int i
, t
, u
, v
;
852 /* First we check for fixed-length commands */
856 return put_user(EV_VERSION
, ip
);
859 if (copy_to_user(p
, &dev
->id
, sizeof(struct input_id
)))
864 if (!test_bit(EV_REP
, dev
->evbit
))
866 if (put_user(dev
->rep
[REP_DELAY
], ip
))
868 if (put_user(dev
->rep
[REP_PERIOD
], ip
+ 1))
873 if (!test_bit(EV_REP
, dev
->evbit
))
877 if (get_user(v
, ip
+ 1))
880 input_inject_event(&evdev
->handle
, EV_REP
, REP_DELAY
, u
);
881 input_inject_event(&evdev
->handle
, EV_REP
, REP_PERIOD
, v
);
886 return input_ff_erase(dev
, (int)(unsigned long) p
, file
);
889 i
= test_bit(EV_FF
, dev
->evbit
) ?
890 dev
->ff
->max_effects
: 0;
897 return evdev_grab(evdev
, client
);
899 return evdev_ungrab(evdev
, client
);
905 return evdev_revoke(evdev
, client
, file
);
908 if (copy_from_user(&i
, p
, sizeof(unsigned int)))
911 return evdev_set_clk_type(client
, i
);
914 return evdev_handle_get_keycode(dev
, p
);
917 return evdev_handle_set_keycode(dev
, p
);
919 case EVIOCGKEYCODE_V2
:
920 return evdev_handle_get_keycode_v2(dev
, p
);
922 case EVIOCSKEYCODE_V2
:
923 return evdev_handle_set_keycode_v2(dev
, p
);
926 size
= _IOC_SIZE(cmd
);
928 /* Now check variable-length commands */
929 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
930 switch (EVIOC_MASK_SIZE(cmd
)) {
933 return bits_to_user(dev
->propbit
, INPUT_PROP_MAX
,
934 size
, p
, compat_mode
);
936 case EVIOCGMTSLOTS(0):
937 return evdev_handle_mt_request(dev
, size
, ip
);
940 return evdev_handle_get_val(client
, dev
, EV_KEY
, dev
->key
,
941 KEY_MAX
, size
, p
, compat_mode
);
944 return evdev_handle_get_val(client
, dev
, EV_LED
, dev
->led
,
945 LED_MAX
, size
, p
, compat_mode
);
948 return evdev_handle_get_val(client
, dev
, EV_SND
, dev
->snd
,
949 SND_MAX
, size
, p
, compat_mode
);
952 return evdev_handle_get_val(client
, dev
, EV_SW
, dev
->sw
,
953 SW_MAX
, size
, p
, compat_mode
);
956 return str_to_user(dev
->name
, size
, p
);
959 return str_to_user(dev
->phys
, size
, p
);
962 return str_to_user(dev
->uniq
, size
, p
);
964 case EVIOC_MASK_SIZE(EVIOCSFF
):
965 if (input_ff_effect_from_user(p
, size
, &effect
))
968 error
= input_ff_upload(dev
, &effect
, file
);
972 if (put_user(effect
.id
, &(((struct ff_effect __user
*)p
)->id
)))
978 /* Multi-number variable-length handlers */
979 if (_IOC_TYPE(cmd
) != 'E')
982 if (_IOC_DIR(cmd
) == _IOC_READ
) {
984 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0, 0)))
985 return handle_eviocgbit(dev
,
986 _IOC_NR(cmd
) & EV_MAX
, size
,
989 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
994 t
= _IOC_NR(cmd
) & ABS_MAX
;
995 abs
= dev
->absinfo
[t
];
997 if (copy_to_user(p
, &abs
, min_t(size_t,
998 size
, sizeof(struct input_absinfo
))))
1005 if (_IOC_DIR(cmd
) == _IOC_WRITE
) {
1007 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
1012 t
= _IOC_NR(cmd
) & ABS_MAX
;
1014 if (copy_from_user(&abs
, p
, min_t(size_t,
1015 size
, sizeof(struct input_absinfo
))))
1018 if (size
< sizeof(struct input_absinfo
))
1021 /* We can't change number of reserved MT slots */
1022 if (t
== ABS_MT_SLOT
)
1026 * Take event lock to ensure that we are not
1027 * changing device parameters in the middle
1030 spin_lock_irq(&dev
->event_lock
);
1031 dev
->absinfo
[t
] = abs
;
1032 spin_unlock_irq(&dev
->event_lock
);
1041 static long evdev_ioctl_handler(struct file
*file
, unsigned int cmd
,
1042 void __user
*p
, int compat_mode
)
1044 struct evdev_client
*client
= file
->private_data
;
1045 struct evdev
*evdev
= client
->evdev
;
1048 retval
= mutex_lock_interruptible(&evdev
->mutex
);
1052 if (!evdev
->exist
|| client
->revoked
) {
1057 retval
= evdev_do_ioctl(file
, cmd
, p
, compat_mode
);
1060 mutex_unlock(&evdev
->mutex
);
1064 static long evdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1066 return evdev_ioctl_handler(file
, cmd
, (void __user
*)arg
, 0);
1069 #ifdef CONFIG_COMPAT
1070 static long evdev_ioctl_compat(struct file
*file
,
1071 unsigned int cmd
, unsigned long arg
)
1073 return evdev_ioctl_handler(file
, cmd
, compat_ptr(arg
), 1);
1077 static const struct file_operations evdev_fops
= {
1078 .owner
= THIS_MODULE
,
1080 .write
= evdev_write
,
1083 .release
= evdev_release
,
1084 .unlocked_ioctl
= evdev_ioctl
,
1085 #ifdef CONFIG_COMPAT
1086 .compat_ioctl
= evdev_ioctl_compat
,
1088 .fasync
= evdev_fasync
,
1089 .flush
= evdev_flush
,
1090 .llseek
= no_llseek
,
1094 * Mark device non-existent. This disables writes, ioctls and
1095 * prevents new users from opening the device. Already posted
1096 * blocking reads will stay, however new ones will fail.
1098 static void evdev_mark_dead(struct evdev
*evdev
)
1100 mutex_lock(&evdev
->mutex
);
1101 evdev
->exist
= false;
1102 mutex_unlock(&evdev
->mutex
);
1105 static void evdev_cleanup(struct evdev
*evdev
)
1107 struct input_handle
*handle
= &evdev
->handle
;
1109 evdev_mark_dead(evdev
);
1110 evdev_hangup(evdev
);
1112 cdev_del(&evdev
->cdev
);
1114 /* evdev is marked dead so no one else accesses evdev->open */
1116 input_flush_device(handle
, NULL
);
1117 input_close_device(handle
);
1122 * Create new evdev device. Note that input core serializes calls
1123 * to connect and disconnect.
1125 static int evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
,
1126 const struct input_device_id
*id
)
1128 struct evdev
*evdev
;
1133 minor
= input_get_new_minor(EVDEV_MINOR_BASE
, EVDEV_MINORS
, true);
1136 pr_err("failed to reserve new minor: %d\n", error
);
1140 evdev
= kzalloc(sizeof(struct evdev
), GFP_KERNEL
);
1143 goto err_free_minor
;
1146 INIT_LIST_HEAD(&evdev
->client_list
);
1147 spin_lock_init(&evdev
->client_lock
);
1148 mutex_init(&evdev
->mutex
);
1149 init_waitqueue_head(&evdev
->wait
);
1150 evdev
->exist
= true;
1153 /* Normalize device number if it falls into legacy range */
1154 if (dev_no
< EVDEV_MINOR_BASE
+ EVDEV_MINORS
)
1155 dev_no
-= EVDEV_MINOR_BASE
;
1156 dev_set_name(&evdev
->dev
, "event%d", dev_no
);
1158 evdev
->handle
.dev
= input_get_device(dev
);
1159 evdev
->handle
.name
= dev_name(&evdev
->dev
);
1160 evdev
->handle
.handler
= handler
;
1161 evdev
->handle
.private = evdev
;
1163 evdev
->dev
.devt
= MKDEV(INPUT_MAJOR
, minor
);
1164 evdev
->dev
.class = &input_class
;
1165 evdev
->dev
.parent
= &dev
->dev
;
1166 evdev
->dev
.release
= evdev_free
;
1167 device_initialize(&evdev
->dev
);
1169 error
= input_register_handle(&evdev
->handle
);
1171 goto err_free_evdev
;
1173 cdev_init(&evdev
->cdev
, &evdev_fops
);
1174 evdev
->cdev
.kobj
.parent
= &evdev
->dev
.kobj
;
1175 error
= cdev_add(&evdev
->cdev
, evdev
->dev
.devt
, 1);
1177 goto err_unregister_handle
;
1179 error
= device_add(&evdev
->dev
);
1181 goto err_cleanup_evdev
;
1186 evdev_cleanup(evdev
);
1187 err_unregister_handle
:
1188 input_unregister_handle(&evdev
->handle
);
1190 put_device(&evdev
->dev
);
1192 input_free_minor(minor
);
1196 static void evdev_disconnect(struct input_handle
*handle
)
1198 struct evdev
*evdev
= handle
->private;
1200 device_del(&evdev
->dev
);
1201 evdev_cleanup(evdev
);
1202 input_free_minor(MINOR(evdev
->dev
.devt
));
1203 input_unregister_handle(handle
);
1204 put_device(&evdev
->dev
);
1207 static const struct input_device_id evdev_ids
[] = {
1208 { .driver_info
= 1 }, /* Matches all devices */
1209 { }, /* Terminating zero entry */
1212 MODULE_DEVICE_TABLE(input
, evdev_ids
);
1214 static struct input_handler evdev_handler
= {
1215 .event
= evdev_event
,
1216 .events
= evdev_events
,
1217 .connect
= evdev_connect
,
1218 .disconnect
= evdev_disconnect
,
1219 .legacy_minors
= true,
1220 .minor
= EVDEV_MINOR_BASE
,
1222 .id_table
= evdev_ids
,
1225 static int __init
evdev_init(void)
1227 return input_register_handler(&evdev_handler
);
1230 static void __exit
evdev_exit(void)
1232 input_unregister_handler(&evdev_handler
);
1235 module_init(evdev_init
);
1236 module_exit(evdev_exit
);
1238 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1239 MODULE_DESCRIPTION("Input driver event char devices");
1240 MODULE_LICENSE("GPL");