2 * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3 * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "opt_evdev.h"
32 #include <sys/param.h>
33 #include <sys/bitstring.h>
35 #include <sys/epoch.h>
36 #include <sys/filio.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
42 #include <sys/selinfo.h>
43 #include <sys/systm.h>
47 #include <dev/evdev/evdev.h>
48 #include <dev/evdev/evdev_private.h>
49 #include <dev/evdev/input.h>
51 #ifdef COMPAT_FREEBSD32
52 #include <sys/mount.h>
53 #include <sys/sysent.h>
54 #include <compat/freebsd32/freebsd32.h>
55 struct input_event32 {
56 struct timeval32 time;
64 #define debugf(client, fmt, args...) printf("evdev cdev: "fmt"\n", ##args)
66 #define debugf(client, fmt, args...)
69 #define DEF_RING_REPORTS 8
71 static d_open_t evdev_open;
72 static d_read_t evdev_read;
73 static d_write_t evdev_write;
74 static d_ioctl_t evdev_ioctl;
75 static d_poll_t evdev_poll;
76 static d_kqfilter_t evdev_kqfilter;
78 static int evdev_kqread(struct knote *kn, long hint);
79 static void evdev_kqdetach(struct knote *kn);
80 static void evdev_dtor(void *);
81 static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t);
82 static void evdev_client_filter_queue(struct evdev_client *, uint16_t);
84 static struct cdevsw evdev_cdevsw = {
85 .d_version = D_VERSION,
88 .d_write = evdev_write,
89 .d_ioctl = evdev_ioctl,
91 .d_kqfilter = evdev_kqfilter,
95 static struct filterops evdev_cdev_filterops = {
98 .f_detach = evdev_kqdetach,
99 .f_event = evdev_kqread,
103 evdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
105 struct evdev_dev *evdev = dev->si_drv1;
106 struct evdev_client *client;
113 /* Initialize client structure */
114 buffer_size = evdev->ev_report_size * DEF_RING_REPORTS;
115 client = malloc(offsetof(struct evdev_client, ec_buffer) +
116 sizeof(struct input_event) * buffer_size,
117 M_EVDEV, M_WAITOK | M_ZERO);
119 /* Initialize ring buffer */
120 client->ec_buffer_size = buffer_size;
121 client->ec_buffer_head = 0;
122 client->ec_buffer_tail = 0;
123 client->ec_buffer_ready = 0;
125 client->ec_evdev = evdev;
126 mtx_init(&client->ec_buffer_mtx, "evclient", "evdev", MTX_DEF);
127 knlist_init_mtx(&client->ec_selp.si_note, &client->ec_buffer_mtx);
129 /* Avoid race with evdev_unregister */
130 EVDEV_LIST_LOCK(evdev);
131 if (dev->si_drv1 == NULL)
134 ret = evdev_register_client(evdev, client);
137 evdev_revoke_client(client);
138 EVDEV_LIST_UNLOCK(evdev);
141 ret = devfs_set_cdevpriv(client, evdev_dtor);
144 debugf(client, "cannot register evdev client");
152 evdev_dtor(void *data)
154 struct evdev_client *client = (struct evdev_client *)data;
156 EVDEV_LIST_LOCK(client->ec_evdev);
157 if (!client->ec_revoked)
158 evdev_dispose_client(client->ec_evdev, client);
159 EVDEV_LIST_UNLOCK(client->ec_evdev);
161 if (client->ec_evdev->ev_lock_type != EV_LOCK_MTX)
162 epoch_wait_preempt(INPUT_EPOCH);
163 knlist_clear(&client->ec_selp.si_note, 0);
164 seldrain(&client->ec_selp);
165 knlist_destroy(&client->ec_selp.si_note);
166 funsetown(&client->ec_sigio);
167 mtx_destroy(&client->ec_buffer_mtx);
168 free(client, M_EVDEV);
172 evdev_read(struct cdev *dev, struct uio *uio, int ioflag)
174 struct evdev_client *client;
176 struct input_event t;
177 #ifdef COMPAT_FREEBSD32
178 struct input_event32 t32;
181 struct input_event *head;
186 ret = devfs_get_cdevpriv((void **)&client);
190 debugf(client, "read %zd bytes by thread %d", uio->uio_resid,
191 uio->uio_td->td_tid);
193 if (client->ec_revoked)
196 #ifdef COMPAT_FREEBSD32
197 if (SV_CURPROC_FLAG(SV_ILP32))
198 evsize = sizeof(struct input_event32);
201 evsize = sizeof(struct input_event);
203 /* Zero-sized reads are allowed for error checking */
204 if (uio->uio_resid != 0 && uio->uio_resid < evsize)
207 remaining = uio->uio_resid / evsize;
209 EVDEV_CLIENT_LOCKQ(client);
211 if (EVDEV_CLIENT_EMPTYQ(client)) {
212 if (ioflag & O_NONBLOCK)
215 if (remaining != 0) {
216 client->ec_blocked = true;
217 ret = mtx_sleep(client, &client->ec_buffer_mtx,
218 PCATCH, "evread", 0);
219 if (ret == 0 && client->ec_revoked)
225 while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) {
226 head = client->ec_buffer + client->ec_buffer_head;
227 #ifdef COMPAT_FREEBSD32
228 if (SV_CURPROC_FLAG(SV_ILP32)) {
229 bzero(&event.t32, sizeof(struct input_event32));
230 TV_CP(*head, event.t32, time);
231 CP(*head, event.t32, type);
232 CP(*head, event.t32, code);
233 CP(*head, event.t32, value);
236 bcopy(head, &event.t, evsize);
238 client->ec_buffer_head =
239 (client->ec_buffer_head + 1) % client->ec_buffer_size;
242 EVDEV_CLIENT_UNLOCKQ(client);
243 ret = uiomove(&event, evsize, uio);
244 EVDEV_CLIENT_LOCKQ(client);
247 EVDEV_CLIENT_UNLOCKQ(client);
253 evdev_write(struct cdev *dev, struct uio *uio, int ioflag)
255 struct evdev_dev *evdev = dev->si_drv1;
256 struct evdev_client *client;
258 struct input_event t;
259 #ifdef COMPAT_FREEBSD32
260 struct input_event32 t32;
266 ret = devfs_get_cdevpriv((void **)&client);
270 debugf(client, "write %zd bytes by thread %d", uio->uio_resid,
271 uio->uio_td->td_tid);
273 if (client->ec_revoked || evdev == NULL)
276 #ifdef COMPAT_FREEBSD32
277 if (SV_CURPROC_FLAG(SV_ILP32))
278 evsize = sizeof(struct input_event32);
281 evsize = sizeof(struct input_event);
283 if (uio->uio_resid % evsize != 0) {
284 debugf(client, "write size not multiple of input_event size");
288 while (uio->uio_resid > 0 && ret == 0) {
289 ret = uiomove(&event, evsize, uio);
291 #ifdef COMPAT_FREEBSD32
292 if (SV_CURPROC_FLAG(SV_ILP32))
293 ret = evdev_inject_event(evdev, event.t32.type,
294 event.t32.code, event.t32.value);
297 ret = evdev_inject_event(evdev, event.t.type,
298 event.t.code, event.t.value);
306 evdev_poll(struct cdev *dev, int events, struct thread *td)
308 struct evdev_client *client;
312 ret = devfs_get_cdevpriv((void **)&client);
316 debugf(client, "poll by thread %d", td->td_tid);
318 if (client->ec_revoked)
321 if (events & (POLLIN | POLLRDNORM)) {
322 EVDEV_CLIENT_LOCKQ(client);
323 if (!EVDEV_CLIENT_EMPTYQ(client))
324 revents = events & (POLLIN | POLLRDNORM);
326 client->ec_selected = true;
327 selrecord(td, &client->ec_selp);
329 EVDEV_CLIENT_UNLOCKQ(client);
336 evdev_kqfilter(struct cdev *dev, struct knote *kn)
338 struct evdev_client *client;
341 ret = devfs_get_cdevpriv((void **)&client);
345 if (client->ec_revoked)
348 switch(kn->kn_filter) {
350 kn->kn_fop = &evdev_cdev_filterops;
355 kn->kn_hook = (caddr_t)client;
357 knlist_add(&client->ec_selp.si_note, kn, 0);
362 evdev_kqread(struct knote *kn, long hint)
364 struct evdev_client *client;
367 client = (struct evdev_client *)kn->kn_hook;
369 EVDEV_CLIENT_LOCKQ_ASSERT(client);
371 if (client->ec_revoked) {
372 kn->kn_flags |= EV_EOF;
375 kn->kn_data = EVDEV_CLIENT_SIZEQ(client) *
376 sizeof(struct input_event);
377 ret = !EVDEV_CLIENT_EMPTYQ(client);
383 evdev_kqdetach(struct knote *kn)
385 struct evdev_client *client;
387 client = (struct evdev_client *)kn->kn_hook;
388 knlist_remove(&client->ec_selp.si_note, kn, 0);
392 evdev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
395 struct evdev_dev *evdev = dev->si_drv1;
396 struct evdev_client *client;
397 struct input_keymap_entry *ke;
398 int ret, len, limit, type_num;
402 ret = devfs_get_cdevpriv((void **)&client);
406 if (client->ec_revoked || evdev == NULL)
410 * Fix evdev state corrupted with discarding of kdb events.
411 * EVIOCGKEY and EVIOCGLED ioctls can suffer from this.
413 if (evdev->ev_kdb_active) {
415 if (evdev->ev_kdb_active) {
416 evdev->ev_kdb_active = false;
417 evdev_restore_after_kdb(evdev);
422 /* file I/O ioctl handling */
425 return (fsetown(*(int *)data, &client->ec_sigio));
428 *(int *)data = fgetown(&client->ec_sigio);
436 client->ec_async = true;
438 client->ec_async = false;
443 EVDEV_CLIENT_LOCKQ(client);
445 EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event);
446 EVDEV_CLIENT_UNLOCKQ(client);
450 len = IOCPARM_LEN(cmd);
451 debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data);
453 /* evdev fixed-length ioctls handling */
456 *(int *)data = EV_VERSION;
460 debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x",
461 evdev->ev_id.bustype, evdev->ev_id.vendor,
462 evdev->ev_id.product);
463 memcpy(data, &evdev->ev_id, sizeof(struct input_id));
467 if (!evdev_event_supported(evdev, EV_REP))
470 memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep));
474 if (!evdev_event_supported(evdev, EV_REP))
477 evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]);
478 evdev_inject_event(evdev, EV_REP, REP_PERIOD,
483 /* Fake unsupported ioctl */
486 case EVIOCGKEYCODE_V2:
487 if (evdev->ev_methods == NULL ||
488 evdev->ev_methods->ev_get_keycode == NULL)
491 ke = (struct input_keymap_entry *)data;
492 evdev->ev_methods->ev_get_keycode(evdev, ke);
496 /* Fake unsupported ioctl */
499 case EVIOCSKEYCODE_V2:
500 if (evdev->ev_methods == NULL ||
501 evdev->ev_methods->ev_set_keycode == NULL)
504 ke = (struct input_keymap_entry *)data;
505 evdev->ev_methods->ev_set_keycode(evdev, ke);
508 case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX):
509 if (evdev->ev_absinfo == NULL)
512 memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)],
513 sizeof(struct input_absinfo));
516 case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX):
517 if (evdev->ev_absinfo == NULL)
520 code = cmd - EVIOCSABS(0);
521 /* mt-slot number can not be changed */
522 if (code == ABS_MT_SLOT)
526 evdev_set_absinfo(evdev, code, (struct input_absinfo *)data);
533 /* Fake unsupported ioctls */
539 ret = evdev_grab_client(evdev, client);
541 ret = evdev_release_client(evdev, client);
546 if (*(int *)data != 0)
549 EVDEV_LIST_LOCK(evdev);
550 if (dev->si_drv1 != NULL && !client->ec_revoked) {
551 evdev_dispose_client(evdev, client);
552 evdev_revoke_client(client);
554 EVDEV_LIST_UNLOCK(evdev);
558 switch (*(int *)data) {
560 client->ec_clock_id = EV_CLOCK_REALTIME;
562 case CLOCK_MONOTONIC:
563 client->ec_clock_id = EV_CLOCK_MONOTONIC;
570 /* evdev variable-length ioctls handling */
571 switch (IOCBASECMD(cmd)) {
573 strlcpy(data, evdev->ev_name, len);
577 if (evdev->ev_shortname[0] == 0)
580 strlcpy(data, evdev->ev_shortname, len);
584 if (evdev->ev_serial[0] == 0)
587 strlcpy(data, evdev->ev_serial, len);
591 limit = MIN(len, bitstr_size(INPUT_PROP_CNT));
592 memcpy(data, evdev->ev_prop_flags, limit);
595 case EVIOCGMTSLOTS(0):
596 if (evdev->ev_mt == NULL)
598 if (len < sizeof(uint32_t))
600 code = *(uint32_t *)data;
601 if (!ABS_IS_MT(code))
605 MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1);
606 for (int i = 0; i < nvalues; i++)
607 ((int32_t *)data)[i + 1] =
608 evdev_get_mt_value(evdev, i, code);
612 limit = MIN(len, bitstr_size(KEY_CNT));
614 evdev_client_filter_queue(client, EV_KEY);
615 memcpy(data, evdev->ev_key_states, limit);
620 limit = MIN(len, bitstr_size(LED_CNT));
622 evdev_client_filter_queue(client, EV_LED);
623 memcpy(data, evdev->ev_led_states, limit);
628 limit = MIN(len, bitstr_size(SND_CNT));
630 evdev_client_filter_queue(client, EV_SND);
631 memcpy(data, evdev->ev_snd_states, limit);
636 limit = MIN(len, bitstr_size(SW_CNT));
638 evdev_client_filter_queue(client, EV_SW);
639 memcpy(data, evdev->ev_sw_states, limit);
643 case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0):
644 type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0);
645 debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num,
647 return (evdev_ioctl_eviocgbit(evdev, type_num, len, data));
654 evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data)
656 unsigned long *bitmap;
661 bitmap = evdev->ev_type_flags;
665 bitmap = evdev->ev_key_flags;
669 bitmap = evdev->ev_rel_flags;
673 bitmap = evdev->ev_abs_flags;
677 bitmap = evdev->ev_msc_flags;
681 bitmap = evdev->ev_led_flags;
685 bitmap = evdev->ev_snd_flags;
689 bitmap = evdev->ev_sw_flags;
694 * We don't support EV_FF now, so let's
695 * just fake it returning only zeros.
704 * Clear ioctl data buffer in case it's bigger than
709 limit = bitstr_size(limit);
710 len = MIN(limit, len);
711 memcpy(data, bitmap, len);
716 evdev_revoke_client(struct evdev_client *client)
719 EVDEV_LIST_LOCK_ASSERT(client->ec_evdev);
721 client->ec_revoked = true;
725 evdev_notify_event(struct evdev_client *client)
728 EVDEV_CLIENT_LOCKQ_ASSERT(client);
730 if (client->ec_blocked) {
731 client->ec_blocked = false;
734 if (client->ec_selected) {
735 client->ec_selected = false;
736 selwakeup(&client->ec_selp);
738 KNOTE_LOCKED(&client->ec_selp.si_note, 0);
740 if (client->ec_async && client->ec_sigio != NULL)
741 pgsigio(&client->ec_sigio, SIGIO, 0);
745 evdev_cdev_create(struct evdev_dev *evdev)
747 struct make_dev_args mda;
750 make_dev_args_init(&mda);
751 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
752 mda.mda_devsw = &evdev_cdevsw;
753 mda.mda_uid = UID_ROOT;
754 mda.mda_gid = GID_WHEEL;
756 mda.mda_si_drv1 = evdev;
758 /* Try to coexist with cuse-backed input/event devices */
759 while ((ret = make_dev_s(&mda, &evdev->ev_cdev, "input/event%d", unit))
764 evdev->ev_unit = unit;
770 evdev_cdev_destroy(struct evdev_dev *evdev)
773 destroy_dev(evdev->ev_cdev);
778 evdev_client_gettime(struct evdev_client *client, struct timeval *tv)
781 switch (client->ec_clock_id) {
782 case EV_CLOCK_BOOTTIME:
784 * XXX: FreeBSD does not support true POSIX monotonic clock.
785 * So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC.
787 case EV_CLOCK_MONOTONIC:
791 case EV_CLOCK_REALTIME:
799 evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code,
803 size_t count, head, tail, ready;
805 EVDEV_CLIENT_LOCKQ_ASSERT(client);
806 head = client->ec_buffer_head;
807 tail = client->ec_buffer_tail;
808 ready = client->ec_buffer_ready;
809 count = client->ec_buffer_size;
811 /* If queue is full drop its content and place SYN_DROPPED event */
812 if ((tail + 1) % count == head) {
813 debugf(client, "client %p: buffer overflow", client);
815 head = (tail + count - 1) % count;
816 client->ec_buffer[head] = (struct input_event) {
822 * XXX: Here is a small race window from now till the end of
823 * report. The queue is empty but client has been already
824 * notified of data readyness. Can be fixed in two ways:
825 * 1. Implement bulk insert so queue lock would not be dropped
826 * till the SYN_REPORT event.
827 * 2. Insert SYN_REPORT just now and skip remaining events
829 client->ec_buffer_head = head;
830 client->ec_buffer_ready = head;
833 client->ec_buffer[tail].type = type;
834 client->ec_buffer[tail].code = code;
835 client->ec_buffer[tail].value = value;
836 client->ec_buffer_tail = (tail + 1) % count;
838 /* Allow users to read events only after report has been completed */
839 if (type == EV_SYN && code == SYN_REPORT) {
840 evdev_client_gettime(client, &time);
841 for (; ready != client->ec_buffer_tail;
842 ready = (ready + 1) % count)
843 client->ec_buffer[ready].time = time;
844 client->ec_buffer_ready = client->ec_buffer_tail;
849 evdev_client_dumpqueue(struct evdev_client *client)
851 struct input_event *event;
852 size_t i, head, tail, ready, size;
854 head = client->ec_buffer_head;
855 tail = client->ec_buffer_tail;
856 ready = client->ec_buffer_ready;
857 size = client->ec_buffer_size;
859 printf("evdev client: %p\n", client);
860 printf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n",
861 head, ready, tail, size);
863 printf("queue contents:\n");
865 for (i = 0; i < size; i++) {
866 event = &client->ec_buffer[i];
869 if (i < head || i > tail)
872 printf("type=%d code=%d value=%d ", event->type,
873 event->code, event->value);
880 printf("<- ready\n");
887 evdev_client_filter_queue(struct evdev_client *client, uint16_t type)
889 struct input_event *event;
890 size_t head, tail, count, i;
891 bool last_was_syn = false;
893 EVDEV_CLIENT_LOCKQ(client);
895 i = head = client->ec_buffer_head;
896 tail = client->ec_buffer_tail;
897 count = client->ec_buffer_size;
898 client->ec_buffer_ready = client->ec_buffer_tail;
900 while (i != client->ec_buffer_tail) {
901 event = &client->ec_buffer[i];
904 /* Skip event of given type */
905 if (event->type == type)
908 /* Remove empty SYN_REPORT events */
909 if (event->type == EV_SYN && event->code == SYN_REPORT) {
913 client->ec_buffer_ready = (tail + 1) % count;
917 memcpy(&client->ec_buffer[tail], event,
918 sizeof(struct input_event));
920 last_was_syn = (event->type == EV_SYN &&
921 event->code == SYN_REPORT);
923 tail = (tail + 1) % count;
926 client->ec_buffer_head = i;
927 client->ec_buffer_tail = tail;
929 EVDEV_CLIENT_UNLOCKQ(client);