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/filio.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <sys/selinfo.h>
42 #include <sys/systm.h>
46 #include <dev/evdev/evdev.h>
47 #include <dev/evdev/evdev_private.h>
48 #include <dev/evdev/input.h>
50 #ifdef COMPAT_FREEBSD32
51 #include <sys/mount.h>
52 #include <sys/sysent.h>
53 #include <compat/freebsd32/freebsd32.h>
54 struct input_event32 {
55 struct timeval32 time;
63 #define debugf(client, fmt, args...) printf("evdev cdev: "fmt"\n", ##args)
65 #define debugf(client, fmt, args...)
68 #define DEF_RING_REPORTS 8
70 static d_open_t evdev_open;
71 static d_read_t evdev_read;
72 static d_write_t evdev_write;
73 static d_ioctl_t evdev_ioctl;
74 static d_poll_t evdev_poll;
75 static d_kqfilter_t evdev_kqfilter;
77 static int evdev_kqread(struct knote *kn, long hint);
78 static void evdev_kqdetach(struct knote *kn);
79 static void evdev_dtor(void *);
80 static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t);
81 static void evdev_client_filter_queue(struct evdev_client *, uint16_t);
83 static struct cdevsw evdev_cdevsw = {
84 .d_version = D_VERSION,
87 .d_write = evdev_write,
88 .d_ioctl = evdev_ioctl,
90 .d_kqfilter = evdev_kqfilter,
94 static struct filterops evdev_cdev_filterops = {
97 .f_detach = evdev_kqdetach,
98 .f_event = evdev_kqread,
102 evdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
104 struct evdev_dev *evdev = dev->si_drv1;
105 struct evdev_client *client;
112 /* Initialize client structure */
113 buffer_size = evdev->ev_report_size * DEF_RING_REPORTS;
114 client = malloc(offsetof(struct evdev_client, ec_buffer) +
115 sizeof(struct input_event) * buffer_size,
116 M_EVDEV, M_WAITOK | M_ZERO);
118 /* Initialize ring buffer */
119 client->ec_buffer_size = buffer_size;
120 client->ec_buffer_head = 0;
121 client->ec_buffer_tail = 0;
122 client->ec_buffer_ready = 0;
124 client->ec_evdev = evdev;
125 mtx_init(&client->ec_buffer_mtx, "evclient", "evdev", MTX_DEF);
126 knlist_init_mtx(&client->ec_selp.si_note, &client->ec_buffer_mtx);
128 /* Avoid race with evdev_unregister */
130 if (dev->si_drv1 == NULL)
133 ret = evdev_register_client(evdev, client);
136 evdev_revoke_client(client);
138 * Unlock evdev here because non-sleepable lock held
139 * while calling devfs_set_cdevpriv upsets WITNESS
144 ret = devfs_set_cdevpriv(client, evdev_dtor);
147 debugf(client, "cannot register evdev client");
155 evdev_dtor(void *data)
157 struct evdev_client *client = (struct evdev_client *)data;
159 EVDEV_LOCK(client->ec_evdev);
160 if (!client->ec_revoked)
161 evdev_dispose_client(client->ec_evdev, client);
162 EVDEV_UNLOCK(client->ec_evdev);
164 knlist_clear(&client->ec_selp.si_note, 0);
165 seldrain(&client->ec_selp);
166 knlist_destroy(&client->ec_selp.si_note);
167 funsetown(&client->ec_sigio);
168 mtx_destroy(&client->ec_buffer_mtx);
169 free(client, M_EVDEV);
173 evdev_read(struct cdev *dev, struct uio *uio, int ioflag)
175 struct evdev_client *client;
177 struct input_event t;
178 #ifdef COMPAT_FREEBSD32
179 struct input_event32 t32;
182 struct input_event *head;
187 ret = devfs_get_cdevpriv((void **)&client);
191 debugf(client, "read %zd bytes by thread %d", uio->uio_resid,
192 uio->uio_td->td_tid);
194 if (client->ec_revoked)
197 #ifdef COMPAT_FREEBSD32
198 if (SV_CURPROC_FLAG(SV_ILP32))
199 evsize = sizeof(struct input_event32);
202 evsize = sizeof(struct input_event);
204 /* Zero-sized reads are allowed for error checking */
205 if (uio->uio_resid != 0 && uio->uio_resid < evsize)
208 remaining = uio->uio_resid / evsize;
210 EVDEV_CLIENT_LOCKQ(client);
212 if (EVDEV_CLIENT_EMPTYQ(client)) {
213 if (ioflag & O_NONBLOCK)
216 if (remaining != 0) {
217 client->ec_blocked = true;
218 ret = mtx_sleep(client, &client->ec_buffer_mtx,
219 PCATCH, "evread", 0);
220 if (ret == 0 && client->ec_revoked)
226 while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) {
227 head = client->ec_buffer + client->ec_buffer_head;
228 #ifdef COMPAT_FREEBSD32
229 if (SV_CURPROC_FLAG(SV_ILP32)) {
230 bzero(&event.t32, sizeof(struct input_event32));
231 TV_CP(*head, event.t32, time);
232 CP(*head, event.t32, type);
233 CP(*head, event.t32, code);
234 CP(*head, event.t32, value);
237 bcopy(head, &event.t, evsize);
239 client->ec_buffer_head =
240 (client->ec_buffer_head + 1) % client->ec_buffer_size;
243 EVDEV_CLIENT_UNLOCKQ(client);
244 ret = uiomove(&event, evsize, uio);
245 EVDEV_CLIENT_LOCKQ(client);
248 EVDEV_CLIENT_UNLOCKQ(client);
254 evdev_write(struct cdev *dev, struct uio *uio, int ioflag)
256 struct evdev_dev *evdev = dev->si_drv1;
257 struct evdev_client *client;
259 struct input_event t;
260 #ifdef COMPAT_FREEBSD32
261 struct input_event32 t32;
267 ret = devfs_get_cdevpriv((void **)&client);
271 debugf(client, "write %zd bytes by thread %d", uio->uio_resid,
272 uio->uio_td->td_tid);
274 if (client->ec_revoked || evdev == NULL)
277 #ifdef COMPAT_FREEBSD32
278 if (SV_CURPROC_FLAG(SV_ILP32))
279 evsize = sizeof(struct input_event32);
282 evsize = sizeof(struct input_event);
284 if (uio->uio_resid % evsize != 0) {
285 debugf(client, "write size not multiple of input_event size");
289 while (uio->uio_resid > 0 && ret == 0) {
290 ret = uiomove(&event, evsize, uio);
292 #ifdef COMPAT_FREEBSD32
293 if (SV_CURPROC_FLAG(SV_ILP32))
294 ret = evdev_inject_event(evdev, event.t32.type,
295 event.t32.code, event.t32.value);
298 ret = evdev_inject_event(evdev, event.t.type,
299 event.t.code, event.t.value);
307 evdev_poll(struct cdev *dev, int events, struct thread *td)
309 struct evdev_client *client;
313 ret = devfs_get_cdevpriv((void **)&client);
317 debugf(client, "poll by thread %d", td->td_tid);
319 if (client->ec_revoked)
322 if (events & (POLLIN | POLLRDNORM)) {
323 EVDEV_CLIENT_LOCKQ(client);
324 if (!EVDEV_CLIENT_EMPTYQ(client))
325 revents = events & (POLLIN | POLLRDNORM);
327 client->ec_selected = true;
328 selrecord(td, &client->ec_selp);
330 EVDEV_CLIENT_UNLOCKQ(client);
337 evdev_kqfilter(struct cdev *dev, struct knote *kn)
339 struct evdev_client *client;
342 ret = devfs_get_cdevpriv((void **)&client);
346 if (client->ec_revoked)
349 switch(kn->kn_filter) {
351 kn->kn_fop = &evdev_cdev_filterops;
356 kn->kn_hook = (caddr_t)client;
358 knlist_add(&client->ec_selp.si_note, kn, 0);
363 evdev_kqread(struct knote *kn, long hint)
365 struct evdev_client *client;
368 client = (struct evdev_client *)kn->kn_hook;
370 EVDEV_CLIENT_LOCKQ_ASSERT(client);
372 if (client->ec_revoked) {
373 kn->kn_flags |= EV_EOF;
376 kn->kn_data = EVDEV_CLIENT_SIZEQ(client) *
377 sizeof(struct input_event);
378 ret = !EVDEV_CLIENT_EMPTYQ(client);
384 evdev_kqdetach(struct knote *kn)
386 struct evdev_client *client;
388 client = (struct evdev_client *)kn->kn_hook;
389 knlist_remove(&client->ec_selp.si_note, kn, 0);
393 evdev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
396 struct evdev_dev *evdev = dev->si_drv1;
397 struct evdev_client *client;
398 struct input_keymap_entry *ke;
399 int ret, len, limit, type_num;
403 ret = devfs_get_cdevpriv((void **)&client);
407 if (client->ec_revoked || evdev == NULL)
411 * Fix evdev state corrupted with discarding of kdb events.
412 * EVIOCGKEY and EVIOCGLED ioctls can suffer from this.
414 if (evdev->ev_kdb_active) {
416 if (evdev->ev_kdb_active) {
417 evdev->ev_kdb_active = false;
418 evdev_restore_after_kdb(evdev);
423 /* file I/O ioctl handling */
426 return (fsetown(*(int *)data, &client->ec_sigio));
429 *(int *)data = fgetown(&client->ec_sigio);
437 client->ec_async = true;
439 client->ec_async = false;
444 EVDEV_CLIENT_LOCKQ(client);
446 EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event);
447 EVDEV_CLIENT_UNLOCKQ(client);
451 len = IOCPARM_LEN(cmd);
452 debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data);
454 /* evdev fixed-length ioctls handling */
457 *(int *)data = EV_VERSION;
461 debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x",
462 evdev->ev_id.bustype, evdev->ev_id.vendor,
463 evdev->ev_id.product);
464 memcpy(data, &evdev->ev_id, sizeof(struct input_id));
468 if (!evdev_event_supported(evdev, EV_REP))
471 memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep));
475 if (!evdev_event_supported(evdev, EV_REP))
478 evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]);
479 evdev_inject_event(evdev, EV_REP, REP_PERIOD,
484 /* Fake unsupported ioctl */
487 case EVIOCGKEYCODE_V2:
488 if (evdev->ev_methods == NULL ||
489 evdev->ev_methods->ev_get_keycode == NULL)
492 ke = (struct input_keymap_entry *)data;
493 evdev->ev_methods->ev_get_keycode(evdev, ke);
497 /* Fake unsupported ioctl */
500 case EVIOCSKEYCODE_V2:
501 if (evdev->ev_methods == NULL ||
502 evdev->ev_methods->ev_set_keycode == NULL)
505 ke = (struct input_keymap_entry *)data;
506 evdev->ev_methods->ev_set_keycode(evdev, ke);
509 case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX):
510 if (evdev->ev_absinfo == NULL)
513 memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)],
514 sizeof(struct input_absinfo));
517 case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX):
518 if (evdev->ev_absinfo == NULL)
521 code = cmd - EVIOCSABS(0);
522 /* mt-slot number can not be changed */
523 if (code == ABS_MT_SLOT)
527 evdev_set_absinfo(evdev, code, (struct input_absinfo *)data);
534 /* Fake unsupported ioctls */
540 ret = evdev_grab_client(evdev, client);
542 ret = evdev_release_client(evdev, client);
547 if (*(int *)data != 0)
551 if (dev->si_drv1 != NULL && !client->ec_revoked) {
552 evdev_dispose_client(evdev, client);
553 evdev_revoke_client(client);
559 switch (*(int *)data) {
561 client->ec_clock_id = EV_CLOCK_REALTIME;
563 case CLOCK_MONOTONIC:
564 client->ec_clock_id = EV_CLOCK_MONOTONIC;
571 /* evdev variable-length ioctls handling */
572 switch (IOCBASECMD(cmd)) {
574 strlcpy(data, evdev->ev_name, len);
578 if (evdev->ev_shortname[0] == 0)
581 strlcpy(data, evdev->ev_shortname, len);
585 if (evdev->ev_serial[0] == 0)
588 strlcpy(data, evdev->ev_serial, len);
592 limit = MIN(len, bitstr_size(INPUT_PROP_CNT));
593 memcpy(data, evdev->ev_prop_flags, limit);
596 case EVIOCGMTSLOTS(0):
597 if (evdev->ev_mt == NULL)
599 if (len < sizeof(uint32_t))
601 code = *(uint32_t *)data;
602 if (!ABS_IS_MT(code))
606 MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1);
607 for (int i = 0; i < nvalues; i++)
608 ((int32_t *)data)[i + 1] =
609 evdev_get_mt_value(evdev, i, code);
613 limit = MIN(len, bitstr_size(KEY_CNT));
615 evdev_client_filter_queue(client, EV_KEY);
616 memcpy(data, evdev->ev_key_states, limit);
621 limit = MIN(len, bitstr_size(LED_CNT));
623 evdev_client_filter_queue(client, EV_LED);
624 memcpy(data, evdev->ev_led_states, limit);
629 limit = MIN(len, bitstr_size(SND_CNT));
631 evdev_client_filter_queue(client, EV_SND);
632 memcpy(data, evdev->ev_snd_states, limit);
637 limit = MIN(len, bitstr_size(SW_CNT));
639 evdev_client_filter_queue(client, EV_SW);
640 memcpy(data, evdev->ev_sw_states, limit);
644 case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0):
645 type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0);
646 debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num,
648 return (evdev_ioctl_eviocgbit(evdev, type_num, len, data));
655 evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data)
657 unsigned long *bitmap;
662 bitmap = evdev->ev_type_flags;
666 bitmap = evdev->ev_key_flags;
670 bitmap = evdev->ev_rel_flags;
674 bitmap = evdev->ev_abs_flags;
678 bitmap = evdev->ev_msc_flags;
682 bitmap = evdev->ev_led_flags;
686 bitmap = evdev->ev_snd_flags;
690 bitmap = evdev->ev_sw_flags;
695 * We don't support EV_FF now, so let's
696 * just fake it returning only zeros.
705 * Clear ioctl data buffer in case it's bigger than
710 limit = bitstr_size(limit);
711 len = MIN(limit, len);
712 memcpy(data, bitmap, len);
717 evdev_revoke_client(struct evdev_client *client)
720 EVDEV_LOCK_ASSERT(client->ec_evdev);
722 client->ec_revoked = true;
726 evdev_notify_event(struct evdev_client *client)
729 EVDEV_CLIENT_LOCKQ_ASSERT(client);
731 if (client->ec_blocked) {
732 client->ec_blocked = false;
735 if (client->ec_selected) {
736 client->ec_selected = false;
737 selwakeup(&client->ec_selp);
739 KNOTE_LOCKED(&client->ec_selp.si_note, 0);
741 if (client->ec_async && client->ec_sigio != NULL)
742 pgsigio(&client->ec_sigio, SIGIO, 0);
746 evdev_cdev_create(struct evdev_dev *evdev)
748 struct make_dev_args mda;
751 make_dev_args_init(&mda);
752 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
753 mda.mda_devsw = &evdev_cdevsw;
754 mda.mda_uid = UID_ROOT;
755 mda.mda_gid = GID_WHEEL;
757 mda.mda_si_drv1 = evdev;
759 /* Try to coexist with cuse-backed input/event devices */
760 while ((ret = make_dev_s(&mda, &evdev->ev_cdev, "input/event%d", unit))
765 evdev->ev_unit = unit;
771 evdev_cdev_destroy(struct evdev_dev *evdev)
774 destroy_dev(evdev->ev_cdev);
779 evdev_client_gettime(struct evdev_client *client, struct timeval *tv)
782 switch (client->ec_clock_id) {
783 case EV_CLOCK_BOOTTIME:
785 * XXX: FreeBSD does not support true POSIX monotonic clock.
786 * So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC.
788 case EV_CLOCK_MONOTONIC:
792 case EV_CLOCK_REALTIME:
800 evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code,
804 size_t count, head, tail, ready;
806 EVDEV_CLIENT_LOCKQ_ASSERT(client);
807 head = client->ec_buffer_head;
808 tail = client->ec_buffer_tail;
809 ready = client->ec_buffer_ready;
810 count = client->ec_buffer_size;
812 /* If queue is full drop its content and place SYN_DROPPED event */
813 if ((tail + 1) % count == head) {
814 debugf(client, "client %p: buffer overflow", client);
816 head = (tail + count - 1) % count;
817 client->ec_buffer[head] = (struct input_event) {
823 * XXX: Here is a small race window from now till the end of
824 * report. The queue is empty but client has been already
825 * notified of data readyness. Can be fixed in two ways:
826 * 1. Implement bulk insert so queue lock would not be dropped
827 * till the SYN_REPORT event.
828 * 2. Insert SYN_REPORT just now and skip remaining events
830 client->ec_buffer_head = head;
831 client->ec_buffer_ready = head;
834 client->ec_buffer[tail].type = type;
835 client->ec_buffer[tail].code = code;
836 client->ec_buffer[tail].value = value;
837 client->ec_buffer_tail = (tail + 1) % count;
839 /* Allow users to read events only after report has been completed */
840 if (type == EV_SYN && code == SYN_REPORT) {
841 evdev_client_gettime(client, &time);
842 for (; ready != client->ec_buffer_tail;
843 ready = (ready + 1) % count)
844 client->ec_buffer[ready].time = time;
845 client->ec_buffer_ready = client->ec_buffer_tail;
850 evdev_client_dumpqueue(struct evdev_client *client)
852 struct input_event *event;
853 size_t i, head, tail, ready, size;
855 head = client->ec_buffer_head;
856 tail = client->ec_buffer_tail;
857 ready = client->ec_buffer_ready;
858 size = client->ec_buffer_size;
860 printf("evdev client: %p\n", client);
861 printf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n",
862 head, ready, tail, size);
864 printf("queue contents:\n");
866 for (i = 0; i < size; i++) {
867 event = &client->ec_buffer[i];
870 if (i < head || i > tail)
873 printf("type=%d code=%d value=%d ", event->type,
874 event->code, event->value);
881 printf("<- ready\n");
888 evdev_client_filter_queue(struct evdev_client *client, uint16_t type)
890 struct input_event *event;
891 size_t head, tail, count, i;
892 bool last_was_syn = false;
894 EVDEV_CLIENT_LOCKQ(client);
896 i = head = client->ec_buffer_head;
897 tail = client->ec_buffer_tail;
898 count = client->ec_buffer_size;
899 client->ec_buffer_ready = client->ec_buffer_tail;
901 while (i != client->ec_buffer_tail) {
902 event = &client->ec_buffer[i];
905 /* Skip event of given type */
906 if (event->type == type)
909 /* Remove empty SYN_REPORT events */
910 if (event->type == EV_SYN && event->code == SYN_REPORT) {
914 client->ec_buffer_ready = (tail + 1) % count;
918 memcpy(&client->ec_buffer[tail], event,
919 sizeof(struct input_event));
921 last_was_syn = (event->type == EV_SYN &&
922 event->code == SYN_REPORT);
924 tail = (tail + 1) % count;
927 client->ec_buffer_head = i;
928 client->ec_buffer_tail = tail;
930 EVDEV_CLIENT_UNLOCKQ(client);