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>
51 #define debugf(client, fmt, args...) printf("evdev cdev: "fmt"\n", ##args)
53 #define debugf(client, fmt, args...)
56 #define DEF_RING_REPORTS 8
58 static d_open_t evdev_open;
59 static d_read_t evdev_read;
60 static d_write_t evdev_write;
61 static d_ioctl_t evdev_ioctl;
62 static d_poll_t evdev_poll;
63 static d_kqfilter_t evdev_kqfilter;
65 static int evdev_kqread(struct knote *kn, long hint);
66 static void evdev_kqdetach(struct knote *kn);
67 static void evdev_dtor(void *);
68 static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t);
69 static void evdev_client_filter_queue(struct evdev_client *, uint16_t);
71 static struct cdevsw evdev_cdevsw = {
72 .d_version = D_VERSION,
75 .d_write = evdev_write,
76 .d_ioctl = evdev_ioctl,
78 .d_kqfilter = evdev_kqfilter,
82 static struct filterops evdev_cdev_filterops = {
85 .f_detach = evdev_kqdetach,
86 .f_event = evdev_kqread,
90 evdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
92 struct evdev_dev *evdev = dev->si_drv1;
93 struct evdev_client *client;
100 /* Initialize client structure */
101 buffer_size = evdev->ev_report_size * DEF_RING_REPORTS;
102 client = malloc(offsetof(struct evdev_client, ec_buffer) +
103 sizeof(struct input_event) * buffer_size,
104 M_EVDEV, M_WAITOK | M_ZERO);
106 /* Initialize ring buffer */
107 client->ec_buffer_size = buffer_size;
108 client->ec_buffer_head = 0;
109 client->ec_buffer_tail = 0;
110 client->ec_buffer_ready = 0;
112 client->ec_evdev = evdev;
113 mtx_init(&client->ec_buffer_mtx, "evclient", "evdev", MTX_DEF);
114 knlist_init_mtx(&client->ec_selp.si_note, &client->ec_buffer_mtx);
116 /* Avoid race with evdev_unregister */
118 if (dev->si_drv1 == NULL)
121 ret = evdev_register_client(evdev, client);
124 evdev_revoke_client(client);
126 * Unlock evdev here because non-sleepable lock held
127 * while calling devfs_set_cdevpriv upsets WITNESS
132 ret = devfs_set_cdevpriv(client, evdev_dtor);
135 debugf(client, "cannot register evdev client");
143 evdev_dtor(void *data)
145 struct evdev_client *client = (struct evdev_client *)data;
147 EVDEV_LOCK(client->ec_evdev);
148 if (!client->ec_revoked)
149 evdev_dispose_client(client->ec_evdev, client);
150 EVDEV_UNLOCK(client->ec_evdev);
152 knlist_clear(&client->ec_selp.si_note, 0);
153 seldrain(&client->ec_selp);
154 knlist_destroy(&client->ec_selp.si_note);
155 funsetown(&client->ec_sigio);
156 mtx_destroy(&client->ec_buffer_mtx);
157 free(client, M_EVDEV);
161 evdev_read(struct cdev *dev, struct uio *uio, int ioflag)
163 struct evdev_client *client;
164 struct input_event event;
168 ret = devfs_get_cdevpriv((void **)&client);
172 debugf(client, "read %zd bytes by thread %d", uio->uio_resid,
173 uio->uio_td->td_tid);
175 if (client->ec_revoked)
178 /* Zero-sized reads are allowed for error checking */
179 if (uio->uio_resid != 0 && uio->uio_resid < sizeof(struct input_event))
182 remaining = uio->uio_resid / sizeof(struct input_event);
184 EVDEV_CLIENT_LOCKQ(client);
186 if (EVDEV_CLIENT_EMPTYQ(client)) {
187 if (ioflag & O_NONBLOCK)
190 if (remaining != 0) {
191 client->ec_blocked = true;
192 ret = mtx_sleep(client, &client->ec_buffer_mtx,
193 PCATCH, "evread", 0);
198 while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) {
199 memcpy(&event, &client->ec_buffer[client->ec_buffer_head],
200 sizeof(struct input_event));
201 client->ec_buffer_head =
202 (client->ec_buffer_head + 1) % client->ec_buffer_size;
205 EVDEV_CLIENT_UNLOCKQ(client);
206 ret = uiomove(&event, sizeof(struct input_event), uio);
207 EVDEV_CLIENT_LOCKQ(client);
210 EVDEV_CLIENT_UNLOCKQ(client);
216 evdev_write(struct cdev *dev, struct uio *uio, int ioflag)
218 struct evdev_dev *evdev = dev->si_drv1;
219 struct evdev_client *client;
220 struct input_event event;
223 ret = devfs_get_cdevpriv((void **)&client);
227 debugf(client, "write %zd bytes by thread %d", uio->uio_resid,
228 uio->uio_td->td_tid);
230 if (client->ec_revoked || evdev == NULL)
233 if (uio->uio_resid % sizeof(struct input_event) != 0) {
234 debugf(client, "write size not multiple of input_event size");
238 while (uio->uio_resid > 0 && ret == 0) {
239 ret = uiomove(&event, sizeof(struct input_event), uio);
241 ret = evdev_inject_event(evdev, event.type, event.code,
249 evdev_poll(struct cdev *dev, int events, struct thread *td)
251 struct evdev_client *client;
255 ret = devfs_get_cdevpriv((void **)&client);
259 debugf(client, "poll by thread %d", td->td_tid);
261 if (client->ec_revoked)
264 if (events & (POLLIN | POLLRDNORM)) {
265 EVDEV_CLIENT_LOCKQ(client);
266 if (!EVDEV_CLIENT_EMPTYQ(client))
267 revents = events & (POLLIN | POLLRDNORM);
269 client->ec_selected = true;
270 selrecord(td, &client->ec_selp);
272 EVDEV_CLIENT_UNLOCKQ(client);
279 evdev_kqfilter(struct cdev *dev, struct knote *kn)
281 struct evdev_client *client;
284 ret = devfs_get_cdevpriv((void **)&client);
288 if (client->ec_revoked)
291 switch(kn->kn_filter) {
293 kn->kn_fop = &evdev_cdev_filterops;
298 kn->kn_hook = (caddr_t)client;
300 knlist_add(&client->ec_selp.si_note, kn, 0);
305 evdev_kqread(struct knote *kn, long hint)
307 struct evdev_client *client;
310 client = (struct evdev_client *)kn->kn_hook;
312 EVDEV_CLIENT_LOCKQ_ASSERT(client);
314 if (client->ec_revoked) {
315 kn->kn_flags |= EV_EOF;
318 kn->kn_data = EVDEV_CLIENT_SIZEQ(client) *
319 sizeof(struct input_event);
320 ret = !EVDEV_CLIENT_EMPTYQ(client);
326 evdev_kqdetach(struct knote *kn)
328 struct evdev_client *client;
330 client = (struct evdev_client *)kn->kn_hook;
331 knlist_remove(&client->ec_selp.si_note, kn, 0);
335 evdev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
338 struct evdev_dev *evdev = dev->si_drv1;
339 struct evdev_client *client;
340 struct input_keymap_entry *ke;
341 int ret, len, limit, type_num;
345 ret = devfs_get_cdevpriv((void **)&client);
349 if (client->ec_revoked || evdev == NULL)
353 * Fix evdev state corrupted with discarding of kdb events.
354 * EVIOCGKEY and EVIOCGLED ioctls can suffer from this.
356 if (evdev->ev_kdb_active) {
358 if (evdev->ev_kdb_active) {
359 evdev->ev_kdb_active = false;
360 evdev_restore_after_kdb(evdev);
365 /* file I/O ioctl handling */
368 return (fsetown(*(int *)data, &client->ec_sigio));
371 *(int *)data = fgetown(&client->ec_sigio);
379 client->ec_async = true;
381 client->ec_async = false;
386 EVDEV_CLIENT_LOCKQ(client);
388 EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event);
389 EVDEV_CLIENT_UNLOCKQ(client);
393 len = IOCPARM_LEN(cmd);
394 debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data);
396 /* evdev fixed-length ioctls handling */
399 *(int *)data = EV_VERSION;
403 debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x",
404 evdev->ev_id.bustype, evdev->ev_id.vendor,
405 evdev->ev_id.product);
406 memcpy(data, &evdev->ev_id, sizeof(struct input_id));
410 if (!evdev_event_supported(evdev, EV_REP))
413 memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep));
417 if (!evdev_event_supported(evdev, EV_REP))
420 evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]);
421 evdev_inject_event(evdev, EV_REP, REP_PERIOD,
426 /* Fake unsupported ioctl */
429 case EVIOCGKEYCODE_V2:
430 if (evdev->ev_methods == NULL ||
431 evdev->ev_methods->ev_get_keycode == NULL)
434 ke = (struct input_keymap_entry *)data;
435 evdev->ev_methods->ev_get_keycode(evdev, ke);
439 /* Fake unsupported ioctl */
442 case EVIOCSKEYCODE_V2:
443 if (evdev->ev_methods == NULL ||
444 evdev->ev_methods->ev_set_keycode == NULL)
447 ke = (struct input_keymap_entry *)data;
448 evdev->ev_methods->ev_set_keycode(evdev, ke);
451 case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX):
452 if (evdev->ev_absinfo == NULL)
455 memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)],
456 sizeof(struct input_absinfo));
459 case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX):
460 if (evdev->ev_absinfo == NULL)
463 code = cmd - EVIOCSABS(0);
464 /* mt-slot number can not be changed */
465 if (code == ABS_MT_SLOT)
469 evdev_set_absinfo(evdev, code, (struct input_absinfo *)data);
476 /* Fake unsupported ioctls */
482 ret = evdev_grab_client(evdev, client);
484 ret = evdev_release_client(evdev, client);
489 if (*(int *)data != 0)
493 if (dev->si_drv1 != NULL && !client->ec_revoked) {
494 evdev_dispose_client(evdev, client);
495 evdev_revoke_client(client);
501 switch (*(int *)data) {
503 client->ec_clock_id = EV_CLOCK_REALTIME;
505 case CLOCK_MONOTONIC:
506 client->ec_clock_id = EV_CLOCK_MONOTONIC;
513 /* evdev variable-length ioctls handling */
514 switch (IOCBASECMD(cmd)) {
516 strlcpy(data, evdev->ev_name, len);
520 if (evdev->ev_shortname[0] == 0)
523 strlcpy(data, evdev->ev_shortname, len);
527 if (evdev->ev_serial[0] == 0)
530 strlcpy(data, evdev->ev_serial, len);
534 limit = MIN(len, bitstr_size(INPUT_PROP_CNT));
535 memcpy(data, evdev->ev_prop_flags, limit);
538 case EVIOCGMTSLOTS(0):
539 if (evdev->ev_mt == NULL)
541 if (len < sizeof(uint32_t))
543 code = *(uint32_t *)data;
544 if (!ABS_IS_MT(code))
548 MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1);
549 for (int i = 0; i < nvalues; i++)
550 ((int32_t *)data)[i + 1] =
551 evdev_get_mt_value(evdev, i, code);
555 limit = MIN(len, bitstr_size(KEY_CNT));
557 evdev_client_filter_queue(client, EV_KEY);
558 memcpy(data, evdev->ev_key_states, limit);
563 limit = MIN(len, bitstr_size(LED_CNT));
565 evdev_client_filter_queue(client, EV_LED);
566 memcpy(data, evdev->ev_led_states, limit);
571 limit = MIN(len, bitstr_size(SND_CNT));
573 evdev_client_filter_queue(client, EV_SND);
574 memcpy(data, evdev->ev_snd_states, limit);
579 limit = MIN(len, bitstr_size(SW_CNT));
581 evdev_client_filter_queue(client, EV_SW);
582 memcpy(data, evdev->ev_sw_states, limit);
586 case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0):
587 type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0);
588 debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num,
590 return (evdev_ioctl_eviocgbit(evdev, type_num, len, data));
597 evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data)
599 unsigned long *bitmap;
604 bitmap = evdev->ev_type_flags;
608 bitmap = evdev->ev_key_flags;
612 bitmap = evdev->ev_rel_flags;
616 bitmap = evdev->ev_abs_flags;
620 bitmap = evdev->ev_msc_flags;
624 bitmap = evdev->ev_led_flags;
628 bitmap = evdev->ev_snd_flags;
632 bitmap = evdev->ev_sw_flags;
637 * We don't support EV_FF now, so let's
638 * just fake it returning only zeros.
647 * Clear ioctl data buffer in case it's bigger than
652 limit = bitstr_size(limit);
653 len = MIN(limit, len);
654 memcpy(data, bitmap, len);
659 evdev_revoke_client(struct evdev_client *client)
662 EVDEV_LOCK_ASSERT(client->ec_evdev);
664 client->ec_revoked = true;
668 evdev_notify_event(struct evdev_client *client)
671 EVDEV_CLIENT_LOCKQ_ASSERT(client);
673 if (client->ec_blocked) {
674 client->ec_blocked = false;
677 if (client->ec_selected) {
678 client->ec_selected = false;
679 selwakeup(&client->ec_selp);
681 KNOTE_LOCKED(&client->ec_selp.si_note, 0);
683 if (client->ec_async && client->ec_sigio != NULL)
684 pgsigio(&client->ec_sigio, SIGIO, 0);
688 evdev_cdev_create(struct evdev_dev *evdev)
690 struct make_dev_args mda;
693 make_dev_args_init(&mda);
694 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
695 mda.mda_devsw = &evdev_cdevsw;
696 mda.mda_uid = UID_ROOT;
697 mda.mda_gid = GID_WHEEL;
699 mda.mda_si_drv1 = evdev;
701 /* Try to coexist with cuse-backed input/event devices */
702 while ((ret = make_dev_s(&mda, &evdev->ev_cdev, "input/event%d", unit))
707 evdev->ev_unit = unit;
713 evdev_cdev_destroy(struct evdev_dev *evdev)
716 destroy_dev(evdev->ev_cdev);
721 evdev_client_gettime(struct evdev_client *client, struct timeval *tv)
724 switch (client->ec_clock_id) {
725 case EV_CLOCK_BOOTTIME:
727 * XXX: FreeBSD does not support true POSIX monotonic clock.
728 * So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC.
730 case EV_CLOCK_MONOTONIC:
734 case EV_CLOCK_REALTIME:
742 evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code,
746 size_t count, head, tail, ready;
748 EVDEV_CLIENT_LOCKQ_ASSERT(client);
749 head = client->ec_buffer_head;
750 tail = client->ec_buffer_tail;
751 ready = client->ec_buffer_ready;
752 count = client->ec_buffer_size;
754 /* If queue is full drop its content and place SYN_DROPPED event */
755 if ((tail + 1) % count == head) {
756 debugf(client, "client %p: buffer overflow", client);
758 head = (tail + count - 1) % count;
759 client->ec_buffer[head] = (struct input_event) {
765 * XXX: Here is a small race window from now till the end of
766 * report. The queue is empty but client has been already
767 * notified of data readyness. Can be fixed in two ways:
768 * 1. Implement bulk insert so queue lock would not be dropped
769 * till the SYN_REPORT event.
770 * 2. Insert SYN_REPORT just now and skip remaining events
772 client->ec_buffer_head = head;
773 client->ec_buffer_ready = head;
776 client->ec_buffer[tail].type = type;
777 client->ec_buffer[tail].code = code;
778 client->ec_buffer[tail].value = value;
779 client->ec_buffer_tail = (tail + 1) % count;
781 /* Allow users to read events only after report has been completed */
782 if (type == EV_SYN && code == SYN_REPORT) {
783 evdev_client_gettime(client, &time);
784 for (; ready != client->ec_buffer_tail;
785 ready = (ready + 1) % count)
786 client->ec_buffer[ready].time = time;
787 client->ec_buffer_ready = client->ec_buffer_tail;
792 evdev_client_dumpqueue(struct evdev_client *client)
794 struct input_event *event;
795 size_t i, head, tail, ready, size;
797 head = client->ec_buffer_head;
798 tail = client->ec_buffer_tail;
799 ready = client->ec_buffer_ready;
800 size = client->ec_buffer_size;
802 printf("evdev client: %p\n", client);
803 printf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n",
804 head, ready, tail, size);
806 printf("queue contents:\n");
808 for (i = 0; i < size; i++) {
809 event = &client->ec_buffer[i];
812 if (i < head || i > tail)
815 printf("type=%d code=%d value=%d ", event->type,
816 event->code, event->value);
823 printf("<- ready\n");
830 evdev_client_filter_queue(struct evdev_client *client, uint16_t type)
832 struct input_event *event;
833 size_t head, tail, count, i;
834 bool last_was_syn = false;
836 EVDEV_CLIENT_LOCKQ(client);
838 i = head = client->ec_buffer_head;
839 tail = client->ec_buffer_tail;
840 count = client->ec_buffer_size;
841 client->ec_buffer_ready = client->ec_buffer_tail;
843 while (i != client->ec_buffer_tail) {
844 event = &client->ec_buffer[i];
847 /* Skip event of given type */
848 if (event->type == type)
851 /* Remove empty SYN_REPORT events */
852 if (event->type == EV_SYN && event->code == SYN_REPORT) {
856 client->ec_buffer_ready = (tail + 1) % count;
860 memcpy(&client->ec_buffer[tail], event,
861 sizeof(struct input_event));
863 last_was_syn = (event->type == EV_SYN &&
864 event->code == SYN_REPORT);
866 tail = (tail + 1) % count;
869 client->ec_buffer_head = i;
870 client->ec_buffer_tail = tail;
872 EVDEV_CLIENT_UNLOCKQ(client);