1 // SPDX-License-Identifier: GPL-2.0-only
3 * Event char devices, giving access to raw input device events.
5 * Copyright (c) 1999-2002 Vojtech Pavlik
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #define EVDEV_MINOR_BASE 64
11 #define EVDEV_MINORS 32
12 #define EVDEV_MIN_BUFFER_SIZE 64U
13 #define EVDEV_BUF_PACKETS 8
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/input/mt.h>
23 #include <linux/major.h>
24 #include <linux/device.h>
25 #include <linux/cdev.h>
26 #include "input-compat.h"
28 enum evdev_clock_type
{
37 struct input_handle handle
;
38 wait_queue_head_t wait
;
39 struct evdev_client __rcu
*grab
;
40 struct list_head client_list
;
41 spinlock_t client_lock
; /* protects client_list */
51 unsigned int packet_head
; /* [future] position of the first element of next packet */
52 spinlock_t buffer_lock
; /* protects access to buffer, head and tail */
53 struct fasync_struct
*fasync
;
55 struct list_head node
;
56 unsigned int clk_type
;
58 unsigned long *evmasks
[EV_CNT
];
60 struct input_event buffer
[];
63 static size_t evdev_get_mask_cnt(unsigned int type
)
65 static const size_t counts
[EV_CNT
] = {
66 /* EV_SYN==0 is EV_CNT, _not_ SYN_CNT, see EVIOCGBIT */
78 return (type
< EV_CNT
) ? counts
[type
] : 0;
81 /* requires the buffer lock to be held */
82 static bool __evdev_is_filtered(struct evdev_client
*client
,
89 /* EV_SYN and unknown codes are never filtered */
90 if (type
== EV_SYN
|| type
>= EV_CNT
)
93 /* first test whether the type is filtered */
94 mask
= client
->evmasks
[0];
95 if (mask
&& !test_bit(type
, mask
))
98 /* unknown values are never filtered */
99 cnt
= evdev_get_mask_cnt(type
);
100 if (!cnt
|| code
>= cnt
)
103 mask
= client
->evmasks
[type
];
104 return mask
&& !test_bit(code
, mask
);
107 /* flush queued events of type @type, caller must hold client->buffer_lock */
108 static void __evdev_flush_queue(struct evdev_client
*client
, unsigned int type
)
110 unsigned int i
, head
, num
;
111 unsigned int mask
= client
->bufsize
- 1;
113 struct input_event
*ev
;
115 BUG_ON(type
== EV_SYN
);
118 client
->packet_head
= client
->tail
;
120 /* init to 1 so a leading SYN_REPORT will not be dropped */
123 for (i
= client
->tail
; i
!= client
->head
; i
= (i
+ 1) & mask
) {
124 ev
= &client
->buffer
[i
];
125 is_report
= ev
->type
== EV_SYN
&& ev
->code
== SYN_REPORT
;
127 if (ev
->type
== type
) {
128 /* drop matched entry */
130 } else if (is_report
&& !num
) {
131 /* drop empty SYN_REPORT groups */
133 } else if (head
!= i
) {
134 /* move entry to fill the gap */
135 client
->buffer
[head
] = *ev
;
139 head
= (head
+ 1) & mask
;
143 client
->packet_head
= head
;
150 static void __evdev_queue_syn_dropped(struct evdev_client
*client
)
152 struct input_event ev
;
154 struct timespec64 ts
;
156 time
= client
->clk_type
== EV_CLK_REAL
?
158 client
->clk_type
== EV_CLK_MONO
?
160 ktime_get_boottime();
162 ts
= ktime_to_timespec64(time
);
163 ev
.input_event_sec
= ts
.tv_sec
;
164 ev
.input_event_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
166 ev
.code
= SYN_DROPPED
;
169 client
->buffer
[client
->head
++] = ev
;
170 client
->head
&= client
->bufsize
- 1;
172 if (unlikely(client
->head
== client
->tail
)) {
173 /* drop queue but keep our SYN_DROPPED event */
174 client
->tail
= (client
->head
- 1) & (client
->bufsize
- 1);
175 client
->packet_head
= client
->tail
;
179 static void evdev_queue_syn_dropped(struct evdev_client
*client
)
183 spin_lock_irqsave(&client
->buffer_lock
, flags
);
184 __evdev_queue_syn_dropped(client
);
185 spin_unlock_irqrestore(&client
->buffer_lock
, flags
);
188 static int evdev_set_clk_type(struct evdev_client
*client
, unsigned int clkid
)
191 unsigned int clk_type
;
196 clk_type
= EV_CLK_REAL
;
198 case CLOCK_MONOTONIC
:
199 clk_type
= EV_CLK_MONO
;
202 clk_type
= EV_CLK_BOOT
;
208 if (client
->clk_type
!= clk_type
) {
209 client
->clk_type
= clk_type
;
212 * Flush pending events and queue SYN_DROPPED event,
213 * but only if the queue is not empty.
215 spin_lock_irqsave(&client
->buffer_lock
, flags
);
217 if (client
->head
!= client
->tail
) {
218 client
->packet_head
= client
->head
= client
->tail
;
219 __evdev_queue_syn_dropped(client
);
222 spin_unlock_irqrestore(&client
->buffer_lock
, flags
);
228 static void __pass_event(struct evdev_client
*client
,
229 const struct input_event
*event
)
231 client
->buffer
[client
->head
++] = *event
;
232 client
->head
&= client
->bufsize
- 1;
234 if (unlikely(client
->head
== client
->tail
)) {
236 * This effectively "drops" all unconsumed events, leaving
237 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
239 client
->tail
= (client
->head
- 2) & (client
->bufsize
- 1);
241 client
->buffer
[client
->tail
].input_event_sec
=
242 event
->input_event_sec
;
243 client
->buffer
[client
->tail
].input_event_usec
=
244 event
->input_event_usec
;
245 client
->buffer
[client
->tail
].type
= EV_SYN
;
246 client
->buffer
[client
->tail
].code
= SYN_DROPPED
;
247 client
->buffer
[client
->tail
].value
= 0;
249 client
->packet_head
= client
->tail
;
252 if (event
->type
== EV_SYN
&& event
->code
== SYN_REPORT
) {
253 client
->packet_head
= client
->head
;
254 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
258 static void evdev_pass_values(struct evdev_client
*client
,
259 const struct input_value
*vals
, unsigned int count
,
262 struct evdev
*evdev
= client
->evdev
;
263 const struct input_value
*v
;
264 struct input_event event
;
265 struct timespec64 ts
;
271 ts
= ktime_to_timespec64(ev_time
[client
->clk_type
]);
272 event
.input_event_sec
= ts
.tv_sec
;
273 event
.input_event_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
275 /* Interrupts are disabled, just acquire the lock. */
276 spin_lock(&client
->buffer_lock
);
278 for (v
= vals
; v
!= vals
+ count
; v
++) {
279 if (__evdev_is_filtered(client
, v
->type
, v
->code
))
282 if (v
->type
== EV_SYN
&& v
->code
== SYN_REPORT
) {
283 /* drop empty SYN_REPORT */
284 if (client
->packet_head
== client
->head
)
290 event
.type
= v
->type
;
291 event
.code
= v
->code
;
292 event
.value
= v
->value
;
293 __pass_event(client
, &event
);
296 spin_unlock(&client
->buffer_lock
);
299 wake_up_interruptible(&evdev
->wait
);
303 * Pass incoming events to all connected clients.
305 static void evdev_events(struct input_handle
*handle
,
306 const struct input_value
*vals
, unsigned int count
)
308 struct evdev
*evdev
= handle
->private;
309 struct evdev_client
*client
;
310 ktime_t ev_time
[EV_CLK_MAX
];
312 ev_time
[EV_CLK_MONO
] = ktime_get();
313 ev_time
[EV_CLK_REAL
] = ktime_mono_to_real(ev_time
[EV_CLK_MONO
]);
314 ev_time
[EV_CLK_BOOT
] = ktime_mono_to_any(ev_time
[EV_CLK_MONO
],
319 client
= rcu_dereference(evdev
->grab
);
322 evdev_pass_values(client
, vals
, count
, ev_time
);
324 list_for_each_entry_rcu(client
, &evdev
->client_list
, node
)
325 evdev_pass_values(client
, vals
, count
, ev_time
);
331 * Pass incoming event to all connected clients.
333 static void evdev_event(struct input_handle
*handle
,
334 unsigned int type
, unsigned int code
, int value
)
336 struct input_value vals
[] = { { type
, code
, value
} };
338 evdev_events(handle
, vals
, 1);
341 static int evdev_fasync(int fd
, struct file
*file
, int on
)
343 struct evdev_client
*client
= file
->private_data
;
345 return fasync_helper(fd
, file
, on
, &client
->fasync
);
348 static int evdev_flush(struct file
*file
, fl_owner_t id
)
350 struct evdev_client
*client
= file
->private_data
;
351 struct evdev
*evdev
= client
->evdev
;
353 mutex_lock(&evdev
->mutex
);
355 if (evdev
->exist
&& !client
->revoked
)
356 input_flush_device(&evdev
->handle
, file
);
358 mutex_unlock(&evdev
->mutex
);
362 static void evdev_free(struct device
*dev
)
364 struct evdev
*evdev
= container_of(dev
, struct evdev
, dev
);
366 input_put_device(evdev
->handle
.dev
);
371 * Grabs an event device (along with underlying input device).
372 * This function is called with evdev->mutex taken.
374 static int evdev_grab(struct evdev
*evdev
, struct evdev_client
*client
)
381 error
= input_grab_device(&evdev
->handle
);
385 rcu_assign_pointer(evdev
->grab
, client
);
390 static int evdev_ungrab(struct evdev
*evdev
, struct evdev_client
*client
)
392 struct evdev_client
*grab
= rcu_dereference_protected(evdev
->grab
,
393 lockdep_is_held(&evdev
->mutex
));
398 rcu_assign_pointer(evdev
->grab
, NULL
);
400 input_release_device(&evdev
->handle
);
405 static void evdev_attach_client(struct evdev
*evdev
,
406 struct evdev_client
*client
)
408 spin_lock(&evdev
->client_lock
);
409 list_add_tail_rcu(&client
->node
, &evdev
->client_list
);
410 spin_unlock(&evdev
->client_lock
);
413 static void evdev_detach_client(struct evdev
*evdev
,
414 struct evdev_client
*client
)
416 spin_lock(&evdev
->client_lock
);
417 list_del_rcu(&client
->node
);
418 spin_unlock(&evdev
->client_lock
);
422 static int evdev_open_device(struct evdev
*evdev
)
426 retval
= mutex_lock_interruptible(&evdev
->mutex
);
432 else if (!evdev
->open
++) {
433 retval
= input_open_device(&evdev
->handle
);
438 mutex_unlock(&evdev
->mutex
);
442 static void evdev_close_device(struct evdev
*evdev
)
444 mutex_lock(&evdev
->mutex
);
446 if (evdev
->exist
&& !--evdev
->open
)
447 input_close_device(&evdev
->handle
);
449 mutex_unlock(&evdev
->mutex
);
453 * Wake up users waiting for IO so they can disconnect from
456 static void evdev_hangup(struct evdev
*evdev
)
458 struct evdev_client
*client
;
460 spin_lock(&evdev
->client_lock
);
461 list_for_each_entry(client
, &evdev
->client_list
, node
)
462 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
463 spin_unlock(&evdev
->client_lock
);
465 wake_up_interruptible(&evdev
->wait
);
468 static int evdev_release(struct inode
*inode
, struct file
*file
)
470 struct evdev_client
*client
= file
->private_data
;
471 struct evdev
*evdev
= client
->evdev
;
474 mutex_lock(&evdev
->mutex
);
475 evdev_ungrab(evdev
, client
);
476 mutex_unlock(&evdev
->mutex
);
478 evdev_detach_client(evdev
, client
);
480 for (i
= 0; i
< EV_CNT
; ++i
)
481 bitmap_free(client
->evmasks
[i
]);
485 evdev_close_device(evdev
);
490 static unsigned int evdev_compute_buffer_size(struct input_dev
*dev
)
492 unsigned int n_events
=
493 max(dev
->hint_events_per_packet
* EVDEV_BUF_PACKETS
,
494 EVDEV_MIN_BUFFER_SIZE
);
496 return roundup_pow_of_two(n_events
);
499 static int evdev_open(struct inode
*inode
, struct file
*file
)
501 struct evdev
*evdev
= container_of(inode
->i_cdev
, struct evdev
, cdev
);
502 unsigned int bufsize
= evdev_compute_buffer_size(evdev
->handle
.dev
);
503 struct evdev_client
*client
;
506 client
= kzalloc(struct_size(client
, buffer
, bufsize
),
507 GFP_KERNEL
| __GFP_NOWARN
);
509 client
= vzalloc(struct_size(client
, buffer
, bufsize
));
513 client
->bufsize
= bufsize
;
514 spin_lock_init(&client
->buffer_lock
);
515 client
->evdev
= evdev
;
516 evdev_attach_client(evdev
, client
);
518 error
= evdev_open_device(evdev
);
520 goto err_free_client
;
522 file
->private_data
= client
;
523 stream_open(inode
, file
);
528 evdev_detach_client(evdev
, client
);
533 static ssize_t
evdev_write(struct file
*file
, const char __user
*buffer
,
534 size_t count
, loff_t
*ppos
)
536 struct evdev_client
*client
= file
->private_data
;
537 struct evdev
*evdev
= client
->evdev
;
538 struct input_event event
;
541 if (count
!= 0 && count
< input_event_size())
544 retval
= mutex_lock_interruptible(&evdev
->mutex
);
548 if (!evdev
->exist
|| client
->revoked
) {
553 while (retval
+ input_event_size() <= count
) {
555 if (input_event_from_user(buffer
+ retval
, &event
)) {
559 retval
+= input_event_size();
561 input_inject_event(&evdev
->handle
,
562 event
.type
, event
.code
, event
.value
);
567 mutex_unlock(&evdev
->mutex
);
571 static int evdev_fetch_next_event(struct evdev_client
*client
,
572 struct input_event
*event
)
576 spin_lock_irq(&client
->buffer_lock
);
578 have_event
= client
->packet_head
!= client
->tail
;
580 *event
= client
->buffer
[client
->tail
++];
581 client
->tail
&= client
->bufsize
- 1;
584 spin_unlock_irq(&client
->buffer_lock
);
589 static ssize_t
evdev_read(struct file
*file
, char __user
*buffer
,
590 size_t count
, loff_t
*ppos
)
592 struct evdev_client
*client
= file
->private_data
;
593 struct evdev
*evdev
= client
->evdev
;
594 struct input_event event
;
598 if (count
!= 0 && count
< input_event_size())
602 if (!evdev
->exist
|| client
->revoked
)
605 if (client
->packet_head
== client
->tail
&&
606 (file
->f_flags
& O_NONBLOCK
))
610 * count == 0 is special - no IO is done but we check
611 * for error conditions (see above).
616 while (read
+ input_event_size() <= count
&&
617 evdev_fetch_next_event(client
, &event
)) {
619 if (input_event_to_user(buffer
+ read
, &event
))
622 read
+= input_event_size();
628 if (!(file
->f_flags
& O_NONBLOCK
)) {
629 error
= wait_event_interruptible(evdev
->wait
,
630 client
->packet_head
!= client
->tail
||
631 !evdev
->exist
|| client
->revoked
);
640 /* No kernel lock - fine */
641 static __poll_t
evdev_poll(struct file
*file
, poll_table
*wait
)
643 struct evdev_client
*client
= file
->private_data
;
644 struct evdev
*evdev
= client
->evdev
;
647 poll_wait(file
, &evdev
->wait
, wait
);
649 if (evdev
->exist
&& !client
->revoked
)
650 mask
= EPOLLOUT
| EPOLLWRNORM
;
652 mask
= EPOLLHUP
| EPOLLERR
;
654 if (client
->packet_head
!= client
->tail
)
655 mask
|= EPOLLIN
| EPOLLRDNORM
;
662 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
663 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
666 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
667 unsigned int maxlen
, void __user
*p
, int compat
)
672 len
= BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
);
676 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
677 if (copy_to_user((compat_long_t __user
*) p
+ i
,
678 (compat_long_t
*) bits
+
679 i
+ 1 - ((i
% 2) << 1),
680 sizeof(compat_long_t
)))
683 len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
687 if (copy_to_user(p
, bits
, len
))
694 static int bits_from_user(unsigned long *bits
, unsigned int maxbit
,
695 unsigned int maxlen
, const void __user
*p
, int compat
)
700 if (maxlen
% sizeof(compat_long_t
))
703 len
= BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
);
707 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
708 if (copy_from_user((compat_long_t
*) bits
+
709 i
+ 1 - ((i
% 2) << 1),
710 (compat_long_t __user
*) p
+ i
,
711 sizeof(compat_long_t
)))
714 *((compat_long_t
*) bits
+ i
- 1) = 0;
717 if (maxlen
% sizeof(long))
720 len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
724 if (copy_from_user(bits
, p
, len
))
733 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
734 unsigned int maxlen
, void __user
*p
, int compat
)
737 BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
) :
738 BITS_TO_LONGS(maxbit
) * sizeof(long);
743 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
746 static int bits_from_user(unsigned long *bits
, unsigned int maxbit
,
747 unsigned int maxlen
, const void __user
*p
, int compat
)
749 size_t chunk_size
= compat
? sizeof(compat_long_t
) : sizeof(long);
752 if (maxlen
% chunk_size
)
755 len
= compat
? BITS_TO_LONGS_COMPAT(maxbit
) : BITS_TO_LONGS(maxbit
);
760 return copy_from_user(bits
, p
, len
) ? -EFAULT
: len
;
763 #endif /* __BIG_ENDIAN */
767 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
768 unsigned int maxlen
, void __user
*p
, int compat
)
770 int len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
775 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
778 static int bits_from_user(unsigned long *bits
, unsigned int maxbit
,
779 unsigned int maxlen
, const void __user
*p
, int compat
)
783 if (maxlen
% sizeof(long))
786 len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
790 return copy_from_user(bits
, p
, len
) ? -EFAULT
: len
;
793 #endif /* CONFIG_COMPAT */
795 static int str_to_user(const char *str
, unsigned int maxlen
, void __user
*p
)
802 len
= strlen(str
) + 1;
806 return copy_to_user(p
, str
, len
) ? -EFAULT
: len
;
809 static int handle_eviocgbit(struct input_dev
*dev
,
810 unsigned int type
, unsigned int size
,
811 void __user
*p
, int compat_mode
)
818 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
819 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
820 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
821 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
822 case EV_MSC
: bits
= dev
->mscbit
; len
= MSC_MAX
; break;
823 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
824 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
825 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
826 case EV_SW
: bits
= dev
->swbit
; len
= SW_MAX
; break;
827 default: return -EINVAL
;
830 return bits_to_user(bits
, len
, size
, p
, compat_mode
);
833 static int evdev_handle_get_keycode(struct input_dev
*dev
, void __user
*p
)
835 struct input_keymap_entry ke
= {
836 .len
= sizeof(unsigned int),
839 int __user
*ip
= (int __user
*)p
;
843 if (copy_from_user(ke
.scancode
, p
, sizeof(unsigned int)))
846 error
= input_get_keycode(dev
, &ke
);
850 if (put_user(ke
.keycode
, ip
+ 1))
856 static int evdev_handle_get_keycode_v2(struct input_dev
*dev
, void __user
*p
)
858 struct input_keymap_entry ke
;
861 if (copy_from_user(&ke
, p
, sizeof(ke
)))
864 error
= input_get_keycode(dev
, &ke
);
868 if (copy_to_user(p
, &ke
, sizeof(ke
)))
874 static int evdev_handle_set_keycode(struct input_dev
*dev
, void __user
*p
)
876 struct input_keymap_entry ke
= {
877 .len
= sizeof(unsigned int),
880 int __user
*ip
= (int __user
*)p
;
882 if (copy_from_user(ke
.scancode
, p
, sizeof(unsigned int)))
885 if (get_user(ke
.keycode
, ip
+ 1))
888 return input_set_keycode(dev
, &ke
);
891 static int evdev_handle_set_keycode_v2(struct input_dev
*dev
, void __user
*p
)
893 struct input_keymap_entry ke
;
895 if (copy_from_user(&ke
, p
, sizeof(ke
)))
898 if (ke
.len
> sizeof(ke
.scancode
))
901 return input_set_keycode(dev
, &ke
);
905 * If we transfer state to the user, we should flush all pending events
906 * of the same type from the client's queue. Otherwise, they might end up
907 * with duplicate events, which can screw up client's state tracking.
908 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
909 * event so user-space will notice missing events.
912 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
913 * need the even_lock only to guarantee consistent state. We can safely release
914 * it while flushing the queue. This allows input-core to handle filters while
915 * we flush the queue.
917 static int evdev_handle_get_val(struct evdev_client
*client
,
918 struct input_dev
*dev
, unsigned int type
,
919 unsigned long *bits
, unsigned int maxbit
,
920 unsigned int maxlen
, void __user
*p
,
926 mem
= bitmap_alloc(maxbit
, GFP_KERNEL
);
930 spin_lock_irq(&dev
->event_lock
);
931 spin_lock(&client
->buffer_lock
);
933 bitmap_copy(mem
, bits
, maxbit
);
935 spin_unlock(&dev
->event_lock
);
937 __evdev_flush_queue(client
, type
);
939 spin_unlock_irq(&client
->buffer_lock
);
941 ret
= bits_to_user(mem
, maxbit
, maxlen
, p
, compat
);
943 evdev_queue_syn_dropped(client
);
950 static int evdev_handle_mt_request(struct input_dev
*dev
,
954 const struct input_mt
*mt
= dev
->mt
;
959 if (get_user(code
, &ip
[0]))
961 if (!mt
|| !input_is_mt_value(code
))
964 max_slots
= (size
- sizeof(__u32
)) / sizeof(__s32
);
965 for (i
= 0; i
< mt
->num_slots
&& i
< max_slots
; i
++) {
966 int value
= input_mt_get_value(&mt
->slots
[i
], code
);
967 if (put_user(value
, &ip
[1 + i
]))
974 static int evdev_revoke(struct evdev
*evdev
, struct evdev_client
*client
,
977 client
->revoked
= true;
978 evdev_ungrab(evdev
, client
);
979 input_flush_device(&evdev
->handle
, file
);
980 wake_up_interruptible(&evdev
->wait
);
985 /* must be called with evdev-mutex held */
986 static int evdev_set_mask(struct evdev_client
*client
,
988 const void __user
*codes
,
992 unsigned long flags
, *mask
, *oldmask
;
996 /* we allow unknown types and 'codes_size > size' for forward-compat */
997 cnt
= evdev_get_mask_cnt(type
);
1001 mask
= bitmap_zalloc(cnt
, GFP_KERNEL
);
1005 error
= bits_from_user(mask
, cnt
- 1, codes_size
, codes
, compat
);
1011 spin_lock_irqsave(&client
->buffer_lock
, flags
);
1012 oldmask
= client
->evmasks
[type
];
1013 client
->evmasks
[type
] = mask
;
1014 spin_unlock_irqrestore(&client
->buffer_lock
, flags
);
1016 bitmap_free(oldmask
);
1021 /* must be called with evdev-mutex held */
1022 static int evdev_get_mask(struct evdev_client
*client
,
1028 unsigned long *mask
;
1029 size_t cnt
, size
, xfer_size
;
1033 /* we allow unknown types and 'codes_size > size' for forward-compat */
1034 cnt
= evdev_get_mask_cnt(type
);
1035 size
= sizeof(unsigned long) * BITS_TO_LONGS(cnt
);
1036 xfer_size
= min_t(size_t, codes_size
, size
);
1039 mask
= client
->evmasks
[type
];
1041 error
= bits_to_user(mask
, cnt
- 1,
1042 xfer_size
, codes
, compat
);
1046 /* fake mask with all bits set */
1047 for (i
= 0; i
< xfer_size
; i
++)
1048 if (put_user(0xffU
, (u8 __user
*)codes
+ i
))
1053 if (xfer_size
< codes_size
)
1054 if (clear_user(codes
+ xfer_size
, codes_size
- xfer_size
))
1060 static long evdev_do_ioctl(struct file
*file
, unsigned int cmd
,
1061 void __user
*p
, int compat_mode
)
1063 struct evdev_client
*client
= file
->private_data
;
1064 struct evdev
*evdev
= client
->evdev
;
1065 struct input_dev
*dev
= evdev
->handle
.dev
;
1066 struct input_absinfo abs
;
1067 struct input_mask mask
;
1068 struct ff_effect effect
;
1069 int __user
*ip
= (int __user
*)p
;
1070 unsigned int i
, t
, u
, v
;
1074 /* First we check for fixed-length commands */
1078 return put_user(EV_VERSION
, ip
);
1081 if (copy_to_user(p
, &dev
->id
, sizeof(struct input_id
)))
1086 if (!test_bit(EV_REP
, dev
->evbit
))
1088 if (put_user(dev
->rep
[REP_DELAY
], ip
))
1090 if (put_user(dev
->rep
[REP_PERIOD
], ip
+ 1))
1095 if (!test_bit(EV_REP
, dev
->evbit
))
1097 if (get_user(u
, ip
))
1099 if (get_user(v
, ip
+ 1))
1102 input_inject_event(&evdev
->handle
, EV_REP
, REP_DELAY
, u
);
1103 input_inject_event(&evdev
->handle
, EV_REP
, REP_PERIOD
, v
);
1108 return input_ff_erase(dev
, (int)(unsigned long) p
, file
);
1111 i
= test_bit(EV_FF
, dev
->evbit
) ?
1112 dev
->ff
->max_effects
: 0;
1113 if (put_user(i
, ip
))
1119 return evdev_grab(evdev
, client
);
1121 return evdev_ungrab(evdev
, client
);
1127 return evdev_revoke(evdev
, client
, file
);
1130 void __user
*codes_ptr
;
1132 if (copy_from_user(&mask
, p
, sizeof(mask
)))
1135 codes_ptr
= (void __user
*)(unsigned long)mask
.codes_ptr
;
1136 return evdev_get_mask(client
,
1137 mask
.type
, codes_ptr
, mask
.codes_size
,
1142 const void __user
*codes_ptr
;
1144 if (copy_from_user(&mask
, p
, sizeof(mask
)))
1147 codes_ptr
= (const void __user
*)(unsigned long)mask
.codes_ptr
;
1148 return evdev_set_mask(client
,
1149 mask
.type
, codes_ptr
, mask
.codes_size
,
1154 if (copy_from_user(&i
, p
, sizeof(unsigned int)))
1157 return evdev_set_clk_type(client
, i
);
1160 return evdev_handle_get_keycode(dev
, p
);
1163 return evdev_handle_set_keycode(dev
, p
);
1165 case EVIOCGKEYCODE_V2
:
1166 return evdev_handle_get_keycode_v2(dev
, p
);
1168 case EVIOCSKEYCODE_V2
:
1169 return evdev_handle_set_keycode_v2(dev
, p
);
1172 size
= _IOC_SIZE(cmd
);
1174 /* Now check variable-length commands */
1175 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
1176 switch (EVIOC_MASK_SIZE(cmd
)) {
1179 return bits_to_user(dev
->propbit
, INPUT_PROP_MAX
,
1180 size
, p
, compat_mode
);
1182 case EVIOCGMTSLOTS(0):
1183 return evdev_handle_mt_request(dev
, size
, ip
);
1186 return evdev_handle_get_val(client
, dev
, EV_KEY
, dev
->key
,
1187 KEY_MAX
, size
, p
, compat_mode
);
1190 return evdev_handle_get_val(client
, dev
, EV_LED
, dev
->led
,
1191 LED_MAX
, size
, p
, compat_mode
);
1194 return evdev_handle_get_val(client
, dev
, EV_SND
, dev
->snd
,
1195 SND_MAX
, size
, p
, compat_mode
);
1198 return evdev_handle_get_val(client
, dev
, EV_SW
, dev
->sw
,
1199 SW_MAX
, size
, p
, compat_mode
);
1202 return str_to_user(dev
->name
, size
, p
);
1205 return str_to_user(dev
->phys
, size
, p
);
1208 return str_to_user(dev
->uniq
, size
, p
);
1210 case EVIOC_MASK_SIZE(EVIOCSFF
):
1211 if (input_ff_effect_from_user(p
, size
, &effect
))
1214 error
= input_ff_upload(dev
, &effect
, file
);
1218 if (put_user(effect
.id
, &(((struct ff_effect __user
*)p
)->id
)))
1224 /* Multi-number variable-length handlers */
1225 if (_IOC_TYPE(cmd
) != 'E')
1228 if (_IOC_DIR(cmd
) == _IOC_READ
) {
1230 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0, 0)))
1231 return handle_eviocgbit(dev
,
1232 _IOC_NR(cmd
) & EV_MAX
, size
,
1235 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
1240 t
= _IOC_NR(cmd
) & ABS_MAX
;
1241 abs
= dev
->absinfo
[t
];
1243 if (copy_to_user(p
, &abs
, min_t(size_t,
1244 size
, sizeof(struct input_absinfo
))))
1251 if (_IOC_DIR(cmd
) == _IOC_WRITE
) {
1253 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
1258 t
= _IOC_NR(cmd
) & ABS_MAX
;
1260 if (copy_from_user(&abs
, p
, min_t(size_t,
1261 size
, sizeof(struct input_absinfo
))))
1264 if (size
< sizeof(struct input_absinfo
))
1267 /* We can't change number of reserved MT slots */
1268 if (t
== ABS_MT_SLOT
)
1272 * Take event lock to ensure that we are not
1273 * changing device parameters in the middle
1276 spin_lock_irq(&dev
->event_lock
);
1277 dev
->absinfo
[t
] = abs
;
1278 spin_unlock_irq(&dev
->event_lock
);
1287 static long evdev_ioctl_handler(struct file
*file
, unsigned int cmd
,
1288 void __user
*p
, int compat_mode
)
1290 struct evdev_client
*client
= file
->private_data
;
1291 struct evdev
*evdev
= client
->evdev
;
1294 retval
= mutex_lock_interruptible(&evdev
->mutex
);
1298 if (!evdev
->exist
|| client
->revoked
) {
1303 retval
= evdev_do_ioctl(file
, cmd
, p
, compat_mode
);
1306 mutex_unlock(&evdev
->mutex
);
1310 static long evdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1312 return evdev_ioctl_handler(file
, cmd
, (void __user
*)arg
, 0);
1315 #ifdef CONFIG_COMPAT
1316 static long evdev_ioctl_compat(struct file
*file
,
1317 unsigned int cmd
, unsigned long arg
)
1319 return evdev_ioctl_handler(file
, cmd
, compat_ptr(arg
), 1);
1323 static const struct file_operations evdev_fops
= {
1324 .owner
= THIS_MODULE
,
1326 .write
= evdev_write
,
1329 .release
= evdev_release
,
1330 .unlocked_ioctl
= evdev_ioctl
,
1331 #ifdef CONFIG_COMPAT
1332 .compat_ioctl
= evdev_ioctl_compat
,
1334 .fasync
= evdev_fasync
,
1335 .flush
= evdev_flush
,
1336 .llseek
= no_llseek
,
1340 * Mark device non-existent. This disables writes, ioctls and
1341 * prevents new users from opening the device. Already posted
1342 * blocking reads will stay, however new ones will fail.
1344 static void evdev_mark_dead(struct evdev
*evdev
)
1346 mutex_lock(&evdev
->mutex
);
1347 evdev
->exist
= false;
1348 mutex_unlock(&evdev
->mutex
);
1351 static void evdev_cleanup(struct evdev
*evdev
)
1353 struct input_handle
*handle
= &evdev
->handle
;
1355 evdev_mark_dead(evdev
);
1356 evdev_hangup(evdev
);
1358 /* evdev is marked dead so no one else accesses evdev->open */
1360 input_flush_device(handle
, NULL
);
1361 input_close_device(handle
);
1366 * Create new evdev device. Note that input core serializes calls
1367 * to connect and disconnect.
1369 static int evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
,
1370 const struct input_device_id
*id
)
1372 struct evdev
*evdev
;
1377 minor
= input_get_new_minor(EVDEV_MINOR_BASE
, EVDEV_MINORS
, true);
1380 pr_err("failed to reserve new minor: %d\n", error
);
1384 evdev
= kzalloc(sizeof(struct evdev
), GFP_KERNEL
);
1387 goto err_free_minor
;
1390 INIT_LIST_HEAD(&evdev
->client_list
);
1391 spin_lock_init(&evdev
->client_lock
);
1392 mutex_init(&evdev
->mutex
);
1393 init_waitqueue_head(&evdev
->wait
);
1394 evdev
->exist
= true;
1397 /* Normalize device number if it falls into legacy range */
1398 if (dev_no
< EVDEV_MINOR_BASE
+ EVDEV_MINORS
)
1399 dev_no
-= EVDEV_MINOR_BASE
;
1400 dev_set_name(&evdev
->dev
, "event%d", dev_no
);
1402 evdev
->handle
.dev
= input_get_device(dev
);
1403 evdev
->handle
.name
= dev_name(&evdev
->dev
);
1404 evdev
->handle
.handler
= handler
;
1405 evdev
->handle
.private = evdev
;
1407 evdev
->dev
.devt
= MKDEV(INPUT_MAJOR
, minor
);
1408 evdev
->dev
.class = &input_class
;
1409 evdev
->dev
.parent
= &dev
->dev
;
1410 evdev
->dev
.release
= evdev_free
;
1411 device_initialize(&evdev
->dev
);
1413 error
= input_register_handle(&evdev
->handle
);
1415 goto err_free_evdev
;
1417 cdev_init(&evdev
->cdev
, &evdev_fops
);
1419 error
= cdev_device_add(&evdev
->cdev
, &evdev
->dev
);
1421 goto err_cleanup_evdev
;
1426 evdev_cleanup(evdev
);
1427 input_unregister_handle(&evdev
->handle
);
1429 put_device(&evdev
->dev
);
1431 input_free_minor(minor
);
1435 static void evdev_disconnect(struct input_handle
*handle
)
1437 struct evdev
*evdev
= handle
->private;
1439 cdev_device_del(&evdev
->cdev
, &evdev
->dev
);
1440 evdev_cleanup(evdev
);
1441 input_free_minor(MINOR(evdev
->dev
.devt
));
1442 input_unregister_handle(handle
);
1443 put_device(&evdev
->dev
);
1446 static const struct input_device_id evdev_ids
[] = {
1447 { .driver_info
= 1 }, /* Matches all devices */
1448 { }, /* Terminating zero entry */
1451 MODULE_DEVICE_TABLE(input
, evdev_ids
);
1453 static struct input_handler evdev_handler
= {
1454 .event
= evdev_event
,
1455 .events
= evdev_events
,
1456 .connect
= evdev_connect
,
1457 .disconnect
= evdev_disconnect
,
1458 .legacy_minors
= true,
1459 .minor
= EVDEV_MINOR_BASE
,
1461 .id_table
= evdev_ids
,
1464 static int __init
evdev_init(void)
1466 return input_register_handler(&evdev_handler
);
1469 static void __exit
evdev_exit(void)
1471 input_unregister_handler(&evdev_handler
);
1474 module_init(evdev_init
);
1475 module_exit(evdev_exit
);
1477 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1478 MODULE_DESCRIPTION("Input driver event char devices");
1479 MODULE_LICENSE("GPL");