xref: /freebsd/sys/dev/evdev/cdev.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
12b3f6d66SOleksandr Tymoshenko /*-
22b3f6d66SOleksandr Tymoshenko  * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3e6502802SVladimir Kondratyev  * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org>
42b3f6d66SOleksandr Tymoshenko  * All rights reserved.
52b3f6d66SOleksandr Tymoshenko  *
62b3f6d66SOleksandr Tymoshenko  * Redistribution and use in source and binary forms, with or without
72b3f6d66SOleksandr Tymoshenko  * modification, are permitted provided that the following conditions
82b3f6d66SOleksandr Tymoshenko  * are met:
92b3f6d66SOleksandr Tymoshenko  * 1. Redistributions of source code must retain the above copyright
102b3f6d66SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer.
112b3f6d66SOleksandr Tymoshenko  * 2. Redistributions in binary form must reproduce the above copyright
122b3f6d66SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer in the
132b3f6d66SOleksandr Tymoshenko  *    documentation and/or other materials provided with the distribution.
142b3f6d66SOleksandr Tymoshenko  *
152b3f6d66SOleksandr Tymoshenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162b3f6d66SOleksandr Tymoshenko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172b3f6d66SOleksandr Tymoshenko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182b3f6d66SOleksandr Tymoshenko  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
192b3f6d66SOleksandr Tymoshenko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202b3f6d66SOleksandr Tymoshenko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212b3f6d66SOleksandr Tymoshenko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222b3f6d66SOleksandr Tymoshenko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232b3f6d66SOleksandr Tymoshenko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242b3f6d66SOleksandr Tymoshenko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252b3f6d66SOleksandr Tymoshenko  * SUCH DAMAGE.
262b3f6d66SOleksandr Tymoshenko  */
272b3f6d66SOleksandr Tymoshenko 
282b3f6d66SOleksandr Tymoshenko #include "opt_evdev.h"
292b3f6d66SOleksandr Tymoshenko 
302b3f6d66SOleksandr Tymoshenko #include <sys/param.h>
31ea2e26b1SVladimir Kondratyev #include <sys/bitstring.h>
322b3f6d66SOleksandr Tymoshenko #include <sys/conf.h>
33d276eae6SVladimir Kondratyev #include <sys/epoch.h>
342b3f6d66SOleksandr Tymoshenko #include <sys/filio.h>
352b3f6d66SOleksandr Tymoshenko #include <sys/fcntl.h>
36ea2e26b1SVladimir Kondratyev #include <sys/kernel.h>
372b3f6d66SOleksandr Tymoshenko #include <sys/malloc.h>
38ea2e26b1SVladimir Kondratyev #include <sys/poll.h>
39ea2e26b1SVladimir Kondratyev #include <sys/proc.h>
40ea2e26b1SVladimir Kondratyev #include <sys/selinfo.h>
41ea2e26b1SVladimir Kondratyev #include <sys/systm.h>
422b3f6d66SOleksandr Tymoshenko #include <sys/time.h>
43ea2e26b1SVladimir Kondratyev #include <sys/uio.h>
442b3f6d66SOleksandr Tymoshenko 
452b3f6d66SOleksandr Tymoshenko #include <dev/evdev/evdev.h>
462b3f6d66SOleksandr Tymoshenko #include <dev/evdev/evdev_private.h>
47ea2e26b1SVladimir Kondratyev #include <dev/evdev/input.h>
482b3f6d66SOleksandr Tymoshenko 
4980ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32
5080ddbddfSVladimir Kondratyev #include <sys/mount.h>
5180ddbddfSVladimir Kondratyev #include <sys/sysent.h>
5280ddbddfSVladimir Kondratyev #include <compat/freebsd32/freebsd32.h>
5380ddbddfSVladimir Kondratyev struct input_event32 {
5480ddbddfSVladimir Kondratyev 	struct timeval32	time;
5580ddbddfSVladimir Kondratyev 	uint16_t		type;
5680ddbddfSVladimir Kondratyev 	uint16_t		code;
5780ddbddfSVladimir Kondratyev 	int32_t			value;
5880ddbddfSVladimir Kondratyev };
5980ddbddfSVladimir Kondratyev #endif
6080ddbddfSVladimir Kondratyev 
612b3f6d66SOleksandr Tymoshenko #ifdef EVDEV_DEBUG
62e0cfa1bcSOleksandr Tymoshenko #define	debugf(client, fmt, args...)	printf("evdev cdev: "fmt"\n", ##args)
632b3f6d66SOleksandr Tymoshenko #else
642b3f6d66SOleksandr Tymoshenko #define	debugf(client, fmt, args...)
652b3f6d66SOleksandr Tymoshenko #endif
662b3f6d66SOleksandr Tymoshenko 
672b3f6d66SOleksandr Tymoshenko #define	DEF_RING_REPORTS	8
682b3f6d66SOleksandr Tymoshenko 
692b3f6d66SOleksandr Tymoshenko static d_open_t		evdev_open;
702b3f6d66SOleksandr Tymoshenko static d_read_t		evdev_read;
712b3f6d66SOleksandr Tymoshenko static d_write_t	evdev_write;
722b3f6d66SOleksandr Tymoshenko static d_ioctl_t	evdev_ioctl;
732b3f6d66SOleksandr Tymoshenko static d_poll_t		evdev_poll;
742b3f6d66SOleksandr Tymoshenko static d_kqfilter_t	evdev_kqfilter;
752b3f6d66SOleksandr Tymoshenko 
762b3f6d66SOleksandr Tymoshenko static int evdev_kqread(struct knote *kn, long hint);
772b3f6d66SOleksandr Tymoshenko static void evdev_kqdetach(struct knote *kn);
782b3f6d66SOleksandr Tymoshenko static void evdev_dtor(void *);
797a810290SVladimir Kondratyev static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t,
807a810290SVladimir Kondratyev     struct thread *);
812b3f6d66SOleksandr Tymoshenko static void evdev_client_filter_queue(struct evdev_client *, uint16_t);
822b3f6d66SOleksandr Tymoshenko 
832b3f6d66SOleksandr Tymoshenko static struct cdevsw evdev_cdevsw = {
842b3f6d66SOleksandr Tymoshenko 	.d_version = D_VERSION,
852b3f6d66SOleksandr Tymoshenko 	.d_open = evdev_open,
862b3f6d66SOleksandr Tymoshenko 	.d_read = evdev_read,
872b3f6d66SOleksandr Tymoshenko 	.d_write = evdev_write,
882b3f6d66SOleksandr Tymoshenko 	.d_ioctl = evdev_ioctl,
892b3f6d66SOleksandr Tymoshenko 	.d_poll = evdev_poll,
902b3f6d66SOleksandr Tymoshenko 	.d_kqfilter = evdev_kqfilter,
912b3f6d66SOleksandr Tymoshenko 	.d_name = "evdev",
922b3f6d66SOleksandr Tymoshenko };
932b3f6d66SOleksandr Tymoshenko 
942b3f6d66SOleksandr Tymoshenko static struct filterops evdev_cdev_filterops = {
952b3f6d66SOleksandr Tymoshenko 	.f_isfd = 1,
962b3f6d66SOleksandr Tymoshenko 	.f_attach = NULL,
972b3f6d66SOleksandr Tymoshenko 	.f_detach = evdev_kqdetach,
982b3f6d66SOleksandr Tymoshenko 	.f_event = evdev_kqread,
992b3f6d66SOleksandr Tymoshenko };
1002b3f6d66SOleksandr Tymoshenko 
1012b3f6d66SOleksandr Tymoshenko static int
evdev_open(struct cdev * dev,int oflags,int devtype,struct thread * td)1022b3f6d66SOleksandr Tymoshenko evdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1032b3f6d66SOleksandr Tymoshenko {
1042b3f6d66SOleksandr Tymoshenko 	struct evdev_dev *evdev = dev->si_drv1;
1052b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
1062b3f6d66SOleksandr Tymoshenko 	size_t buffer_size;
1072b3f6d66SOleksandr Tymoshenko 	int ret;
1082b3f6d66SOleksandr Tymoshenko 
1092b3f6d66SOleksandr Tymoshenko 	if (evdev == NULL)
1102b3f6d66SOleksandr Tymoshenko 		return (ENODEV);
1112b3f6d66SOleksandr Tymoshenko 
1122b3f6d66SOleksandr Tymoshenko 	/* Initialize client structure */
1132b3f6d66SOleksandr Tymoshenko 	buffer_size = evdev->ev_report_size * DEF_RING_REPORTS;
1142b3f6d66SOleksandr Tymoshenko 	client = malloc(offsetof(struct evdev_client, ec_buffer) +
1152b3f6d66SOleksandr Tymoshenko 	    sizeof(struct input_event) * buffer_size,
1162b3f6d66SOleksandr Tymoshenko 	    M_EVDEV, M_WAITOK | M_ZERO);
1172b3f6d66SOleksandr Tymoshenko 
1182b3f6d66SOleksandr Tymoshenko 	/* Initialize ring buffer */
1192b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_size = buffer_size;
1202b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_head = 0;
1212b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_tail = 0;
1222b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_ready = 0;
1232b3f6d66SOleksandr Tymoshenko 
1242b3f6d66SOleksandr Tymoshenko 	client->ec_evdev = evdev;
1252b3f6d66SOleksandr Tymoshenko 	mtx_init(&client->ec_buffer_mtx, "evclient", "evdev", MTX_DEF);
1262b3f6d66SOleksandr Tymoshenko 	knlist_init_mtx(&client->ec_selp.si_note, &client->ec_buffer_mtx);
1272b3f6d66SOleksandr Tymoshenko 
1283b8c8b35SVladimir Kondratyev 	ret = EVDEV_LIST_LOCK_SIG(evdev);
1293b8c8b35SVladimir Kondratyev 	if (ret != 0)
1303b8c8b35SVladimir Kondratyev 		goto out;
1312b3f6d66SOleksandr Tymoshenko 	/* Avoid race with evdev_unregister */
1322b3f6d66SOleksandr Tymoshenko 	if (dev->si_drv1 == NULL)
1332b3f6d66SOleksandr Tymoshenko 		ret = ENODEV;
1342b3f6d66SOleksandr Tymoshenko 	else
1352b3f6d66SOleksandr Tymoshenko 		ret = evdev_register_client(evdev, client);
136d276eae6SVladimir Kondratyev 	EVDEV_LIST_UNLOCK(evdev);
1373b8c8b35SVladimir Kondratyev out:
138d276eae6SVladimir Kondratyev 	if (ret == 0)
1392b3f6d66SOleksandr Tymoshenko 		ret = devfs_set_cdevpriv(client, evdev_dtor);
1403b8c8b35SVladimir Kondratyev 	else
1413b8c8b35SVladimir Kondratyev 		client->ec_revoked = true;
1422b3f6d66SOleksandr Tymoshenko 
1432b3f6d66SOleksandr Tymoshenko 	if (ret != 0) {
1442b3f6d66SOleksandr Tymoshenko 		debugf(client, "cannot register evdev client");
1452b3f6d66SOleksandr Tymoshenko 		evdev_dtor(client);
1462b3f6d66SOleksandr Tymoshenko 	}
1472b3f6d66SOleksandr Tymoshenko 
1482b3f6d66SOleksandr Tymoshenko 	return (ret);
1492b3f6d66SOleksandr Tymoshenko }
1502b3f6d66SOleksandr Tymoshenko 
1512b3f6d66SOleksandr Tymoshenko static void
evdev_dtor(void * data)1522b3f6d66SOleksandr Tymoshenko evdev_dtor(void *data)
1532b3f6d66SOleksandr Tymoshenko {
1542b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client = (struct evdev_client *)data;
1552b3f6d66SOleksandr Tymoshenko 
156d276eae6SVladimir Kondratyev 	EVDEV_LIST_LOCK(client->ec_evdev);
1572b3f6d66SOleksandr Tymoshenko 	if (!client->ec_revoked)
1582b3f6d66SOleksandr Tymoshenko 		evdev_dispose_client(client->ec_evdev, client);
159d276eae6SVladimir Kondratyev 	EVDEV_LIST_UNLOCK(client->ec_evdev);
1602b3f6d66SOleksandr Tymoshenko 
161d276eae6SVladimir Kondratyev 	if (client->ec_evdev->ev_lock_type != EV_LOCK_MTX)
162d276eae6SVladimir Kondratyev 		epoch_wait_preempt(INPUT_EPOCH);
1632b3f6d66SOleksandr Tymoshenko 	knlist_clear(&client->ec_selp.si_note, 0);
1642b3f6d66SOleksandr Tymoshenko 	seldrain(&client->ec_selp);
1652b3f6d66SOleksandr Tymoshenko 	knlist_destroy(&client->ec_selp.si_note);
1662b3f6d66SOleksandr Tymoshenko 	funsetown(&client->ec_sigio);
1672b3f6d66SOleksandr Tymoshenko 	mtx_destroy(&client->ec_buffer_mtx);
1682b3f6d66SOleksandr Tymoshenko 	free(client, M_EVDEV);
1692b3f6d66SOleksandr Tymoshenko }
1702b3f6d66SOleksandr Tymoshenko 
1712b3f6d66SOleksandr Tymoshenko static int
evdev_read(struct cdev * dev,struct uio * uio,int ioflag)1722b3f6d66SOleksandr Tymoshenko evdev_read(struct cdev *dev, struct uio *uio, int ioflag)
1732b3f6d66SOleksandr Tymoshenko {
1742b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
17580ddbddfSVladimir Kondratyev 	union {
17680ddbddfSVladimir Kondratyev 		struct input_event t;
17780ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32
17880ddbddfSVladimir Kondratyev 		struct input_event32 t32;
17980ddbddfSVladimir Kondratyev #endif
18080ddbddfSVladimir Kondratyev 	} event;
18180ddbddfSVladimir Kondratyev 	struct input_event *head;
18280ddbddfSVladimir Kondratyev 	size_t evsize;
1832b3f6d66SOleksandr Tymoshenko 	int ret = 0;
1842b3f6d66SOleksandr Tymoshenko 	int remaining;
1852b3f6d66SOleksandr Tymoshenko 
1862b3f6d66SOleksandr Tymoshenko 	ret = devfs_get_cdevpriv((void **)&client);
1872b3f6d66SOleksandr Tymoshenko 	if (ret != 0)
1882b3f6d66SOleksandr Tymoshenko 		return (ret);
1892b3f6d66SOleksandr Tymoshenko 
1902b3f6d66SOleksandr Tymoshenko 	debugf(client, "read %zd bytes by thread %d", uio->uio_resid,
1912b3f6d66SOleksandr Tymoshenko 	    uio->uio_td->td_tid);
1922b3f6d66SOleksandr Tymoshenko 
1932b3f6d66SOleksandr Tymoshenko 	if (client->ec_revoked)
1942b3f6d66SOleksandr Tymoshenko 		return (ENODEV);
1952b3f6d66SOleksandr Tymoshenko 
19680ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32
19780ddbddfSVladimir Kondratyev 	if (SV_CURPROC_FLAG(SV_ILP32))
19880ddbddfSVladimir Kondratyev 		evsize = sizeof(struct input_event32);
19980ddbddfSVladimir Kondratyev 	else
20080ddbddfSVladimir Kondratyev #endif
20180ddbddfSVladimir Kondratyev 		evsize = sizeof(struct input_event);
20280ddbddfSVladimir Kondratyev 
2032b3f6d66SOleksandr Tymoshenko 	/* Zero-sized reads are allowed for error checking */
20480ddbddfSVladimir Kondratyev 	if (uio->uio_resid != 0 && uio->uio_resid < evsize)
2052b3f6d66SOleksandr Tymoshenko 		return (EINVAL);
2062b3f6d66SOleksandr Tymoshenko 
20780ddbddfSVladimir Kondratyev 	remaining = uio->uio_resid / evsize;
2082b3f6d66SOleksandr Tymoshenko 
2092b3f6d66SOleksandr Tymoshenko 	EVDEV_CLIENT_LOCKQ(client);
2102b3f6d66SOleksandr Tymoshenko 
2112b3f6d66SOleksandr Tymoshenko 	if (EVDEV_CLIENT_EMPTYQ(client)) {
2122b3f6d66SOleksandr Tymoshenko 		if (ioflag & O_NONBLOCK)
2132b3f6d66SOleksandr Tymoshenko 			ret = EWOULDBLOCK;
2142b3f6d66SOleksandr Tymoshenko 		else {
2152b3f6d66SOleksandr Tymoshenko 			if (remaining != 0) {
2162b3f6d66SOleksandr Tymoshenko 				client->ec_blocked = true;
2172b3f6d66SOleksandr Tymoshenko 				ret = mtx_sleep(client, &client->ec_buffer_mtx,
2182b3f6d66SOleksandr Tymoshenko 				    PCATCH, "evread", 0);
219006eb449SVladimir Kondratyev 				if (ret == 0 && client->ec_revoked)
220006eb449SVladimir Kondratyev 					ret = ENODEV;
2212b3f6d66SOleksandr Tymoshenko 			}
2222b3f6d66SOleksandr Tymoshenko 		}
2232b3f6d66SOleksandr Tymoshenko 	}
2242b3f6d66SOleksandr Tymoshenko 
2252b3f6d66SOleksandr Tymoshenko 	while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) {
22680ddbddfSVladimir Kondratyev 		head = client->ec_buffer + client->ec_buffer_head;
22780ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32
22880ddbddfSVladimir Kondratyev 		if (SV_CURPROC_FLAG(SV_ILP32)) {
22980ddbddfSVladimir Kondratyev 			bzero(&event.t32, sizeof(struct input_event32));
23080ddbddfSVladimir Kondratyev 			TV_CP(*head, event.t32, time);
23180ddbddfSVladimir Kondratyev 			CP(*head, event.t32, type);
23280ddbddfSVladimir Kondratyev 			CP(*head, event.t32, code);
23380ddbddfSVladimir Kondratyev 			CP(*head, event.t32, value);
23480ddbddfSVladimir Kondratyev 		} else
23580ddbddfSVladimir Kondratyev #endif
23680ddbddfSVladimir Kondratyev 			bcopy(head, &event.t, evsize);
23780ddbddfSVladimir Kondratyev 
2382b3f6d66SOleksandr Tymoshenko 		client->ec_buffer_head =
2392b3f6d66SOleksandr Tymoshenko 		    (client->ec_buffer_head + 1) % client->ec_buffer_size;
2402b3f6d66SOleksandr Tymoshenko 		remaining--;
2412b3f6d66SOleksandr Tymoshenko 
2422b3f6d66SOleksandr Tymoshenko 		EVDEV_CLIENT_UNLOCKQ(client);
24380ddbddfSVladimir Kondratyev 		ret = uiomove(&event, evsize, uio);
2442b3f6d66SOleksandr Tymoshenko 		EVDEV_CLIENT_LOCKQ(client);
2452b3f6d66SOleksandr Tymoshenko 	}
2462b3f6d66SOleksandr Tymoshenko 
2472b3f6d66SOleksandr Tymoshenko 	EVDEV_CLIENT_UNLOCKQ(client);
2482b3f6d66SOleksandr Tymoshenko 
2492b3f6d66SOleksandr Tymoshenko 	return (ret);
2502b3f6d66SOleksandr Tymoshenko }
2512b3f6d66SOleksandr Tymoshenko 
2522b3f6d66SOleksandr Tymoshenko static int
evdev_write(struct cdev * dev,struct uio * uio,int ioflag)2532b3f6d66SOleksandr Tymoshenko evdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2542b3f6d66SOleksandr Tymoshenko {
2552b3f6d66SOleksandr Tymoshenko 	struct evdev_dev *evdev = dev->si_drv1;
2562b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
25780ddbddfSVladimir Kondratyev 	union {
25880ddbddfSVladimir Kondratyev 		struct input_event t;
25980ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32
26080ddbddfSVladimir Kondratyev 		struct input_event32 t32;
26180ddbddfSVladimir Kondratyev #endif
26280ddbddfSVladimir Kondratyev 	} event;
26380ddbddfSVladimir Kondratyev 	size_t evsize;
2642b3f6d66SOleksandr Tymoshenko 	int ret = 0;
2652b3f6d66SOleksandr Tymoshenko 
2662b3f6d66SOleksandr Tymoshenko 	ret = devfs_get_cdevpriv((void **)&client);
2672b3f6d66SOleksandr Tymoshenko 	if (ret != 0)
2682b3f6d66SOleksandr Tymoshenko 		return (ret);
2692b3f6d66SOleksandr Tymoshenko 
2702b3f6d66SOleksandr Tymoshenko 	debugf(client, "write %zd bytes by thread %d", uio->uio_resid,
2712b3f6d66SOleksandr Tymoshenko 	    uio->uio_td->td_tid);
2722b3f6d66SOleksandr Tymoshenko 
2732b3f6d66SOleksandr Tymoshenko 	if (client->ec_revoked || evdev == NULL)
2742b3f6d66SOleksandr Tymoshenko 		return (ENODEV);
2752b3f6d66SOleksandr Tymoshenko 
27680ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32
27780ddbddfSVladimir Kondratyev 	if (SV_CURPROC_FLAG(SV_ILP32))
27880ddbddfSVladimir Kondratyev 		evsize = sizeof(struct input_event32);
27980ddbddfSVladimir Kondratyev 	else
28080ddbddfSVladimir Kondratyev #endif
28180ddbddfSVladimir Kondratyev 		evsize = sizeof(struct input_event);
28280ddbddfSVladimir Kondratyev 
28380ddbddfSVladimir Kondratyev 	if (uio->uio_resid % evsize != 0) {
2842b3f6d66SOleksandr Tymoshenko 		debugf(client, "write size not multiple of input_event size");
2852b3f6d66SOleksandr Tymoshenko 		return (EINVAL);
2862b3f6d66SOleksandr Tymoshenko 	}
2872b3f6d66SOleksandr Tymoshenko 
2882b3f6d66SOleksandr Tymoshenko 	while (uio->uio_resid > 0 && ret == 0) {
28980ddbddfSVladimir Kondratyev 		ret = uiomove(&event, evsize, uio);
29080ddbddfSVladimir Kondratyev 		if (ret == 0) {
29180ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32
29280ddbddfSVladimir Kondratyev 			if (SV_CURPROC_FLAG(SV_ILP32))
29380ddbddfSVladimir Kondratyev 				ret = evdev_inject_event(evdev, event.t32.type,
29480ddbddfSVladimir Kondratyev 				    event.t32.code, event.t32.value);
29580ddbddfSVladimir Kondratyev 			else
29680ddbddfSVladimir Kondratyev #endif
29780ddbddfSVladimir Kondratyev 				ret = evdev_inject_event(evdev, event.t.type,
29880ddbddfSVladimir Kondratyev 				    event.t.code, event.t.value);
29980ddbddfSVladimir Kondratyev 		}
3002b3f6d66SOleksandr Tymoshenko 	}
3012b3f6d66SOleksandr Tymoshenko 
3022b3f6d66SOleksandr Tymoshenko 	return (ret);
3032b3f6d66SOleksandr Tymoshenko }
3042b3f6d66SOleksandr Tymoshenko 
3052b3f6d66SOleksandr Tymoshenko static int
evdev_poll(struct cdev * dev,int events,struct thread * td)3062b3f6d66SOleksandr Tymoshenko evdev_poll(struct cdev *dev, int events, struct thread *td)
3072b3f6d66SOleksandr Tymoshenko {
3082b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
3092b3f6d66SOleksandr Tymoshenko 	int ret;
3102b3f6d66SOleksandr Tymoshenko 	int revents = 0;
3112b3f6d66SOleksandr Tymoshenko 
3122b3f6d66SOleksandr Tymoshenko 	ret = devfs_get_cdevpriv((void **)&client);
3132b3f6d66SOleksandr Tymoshenko 	if (ret != 0)
3142b3f6d66SOleksandr Tymoshenko 		return (POLLNVAL);
3152b3f6d66SOleksandr Tymoshenko 
3162b3f6d66SOleksandr Tymoshenko 	debugf(client, "poll by thread %d", td->td_tid);
3172b3f6d66SOleksandr Tymoshenko 
3182b3f6d66SOleksandr Tymoshenko 	if (client->ec_revoked)
3192b3f6d66SOleksandr Tymoshenko 		return (POLLHUP);
3202b3f6d66SOleksandr Tymoshenko 
3212b3f6d66SOleksandr Tymoshenko 	if (events & (POLLIN | POLLRDNORM)) {
3222b3f6d66SOleksandr Tymoshenko 		EVDEV_CLIENT_LOCKQ(client);
3232b3f6d66SOleksandr Tymoshenko 		if (!EVDEV_CLIENT_EMPTYQ(client))
3242b3f6d66SOleksandr Tymoshenko 			revents = events & (POLLIN | POLLRDNORM);
3252b3f6d66SOleksandr Tymoshenko 		else {
3262b3f6d66SOleksandr Tymoshenko 			client->ec_selected = true;
3272b3f6d66SOleksandr Tymoshenko 			selrecord(td, &client->ec_selp);
3282b3f6d66SOleksandr Tymoshenko 		}
3292b3f6d66SOleksandr Tymoshenko 		EVDEV_CLIENT_UNLOCKQ(client);
3302b3f6d66SOleksandr Tymoshenko 	}
3312b3f6d66SOleksandr Tymoshenko 
3322b3f6d66SOleksandr Tymoshenko 	return (revents);
3332b3f6d66SOleksandr Tymoshenko }
3342b3f6d66SOleksandr Tymoshenko 
3352b3f6d66SOleksandr Tymoshenko static int
evdev_kqfilter(struct cdev * dev,struct knote * kn)3362b3f6d66SOleksandr Tymoshenko evdev_kqfilter(struct cdev *dev, struct knote *kn)
3372b3f6d66SOleksandr Tymoshenko {
3382b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
3392b3f6d66SOleksandr Tymoshenko 	int ret;
3402b3f6d66SOleksandr Tymoshenko 
3412b3f6d66SOleksandr Tymoshenko 	ret = devfs_get_cdevpriv((void **)&client);
3422b3f6d66SOleksandr Tymoshenko 	if (ret != 0)
3432b3f6d66SOleksandr Tymoshenko 		return (ret);
3442b3f6d66SOleksandr Tymoshenko 
3452b3f6d66SOleksandr Tymoshenko 	if (client->ec_revoked)
3462b3f6d66SOleksandr Tymoshenko 		return (ENODEV);
3472b3f6d66SOleksandr Tymoshenko 
3482b3f6d66SOleksandr Tymoshenko 	switch(kn->kn_filter) {
3492b3f6d66SOleksandr Tymoshenko 	case EVFILT_READ:
3502b3f6d66SOleksandr Tymoshenko 		kn->kn_fop = &evdev_cdev_filterops;
3512b3f6d66SOleksandr Tymoshenko 		break;
3522b3f6d66SOleksandr Tymoshenko 	default:
3532b3f6d66SOleksandr Tymoshenko 		return(EINVAL);
3542b3f6d66SOleksandr Tymoshenko 	}
3552b3f6d66SOleksandr Tymoshenko 	kn->kn_hook = (caddr_t)client;
3562b3f6d66SOleksandr Tymoshenko 
3572b3f6d66SOleksandr Tymoshenko 	knlist_add(&client->ec_selp.si_note, kn, 0);
3582b3f6d66SOleksandr Tymoshenko 	return (0);
3592b3f6d66SOleksandr Tymoshenko }
3602b3f6d66SOleksandr Tymoshenko 
3612b3f6d66SOleksandr Tymoshenko static int
evdev_kqread(struct knote * kn,long hint)3622b3f6d66SOleksandr Tymoshenko evdev_kqread(struct knote *kn, long hint)
3632b3f6d66SOleksandr Tymoshenko {
3642b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
3652b3f6d66SOleksandr Tymoshenko 	int ret;
3662b3f6d66SOleksandr Tymoshenko 
3672b3f6d66SOleksandr Tymoshenko 	client = (struct evdev_client *)kn->kn_hook;
3682b3f6d66SOleksandr Tymoshenko 
3692b3f6d66SOleksandr Tymoshenko 	EVDEV_CLIENT_LOCKQ_ASSERT(client);
3702b3f6d66SOleksandr Tymoshenko 
3712b3f6d66SOleksandr Tymoshenko 	if (client->ec_revoked) {
3722b3f6d66SOleksandr Tymoshenko 		kn->kn_flags |= EV_EOF;
3732b3f6d66SOleksandr Tymoshenko 		ret = 1;
3742b3f6d66SOleksandr Tymoshenko 	} else {
3752b3f6d66SOleksandr Tymoshenko 		kn->kn_data = EVDEV_CLIENT_SIZEQ(client) *
3762b3f6d66SOleksandr Tymoshenko 		    sizeof(struct input_event);
3772b3f6d66SOleksandr Tymoshenko 		ret = !EVDEV_CLIENT_EMPTYQ(client);
3782b3f6d66SOleksandr Tymoshenko 	}
3792b3f6d66SOleksandr Tymoshenko 	return (ret);
3802b3f6d66SOleksandr Tymoshenko }
3812b3f6d66SOleksandr Tymoshenko 
3822b3f6d66SOleksandr Tymoshenko static void
evdev_kqdetach(struct knote * kn)3832b3f6d66SOleksandr Tymoshenko evdev_kqdetach(struct knote *kn)
3842b3f6d66SOleksandr Tymoshenko {
3852b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
3862b3f6d66SOleksandr Tymoshenko 
3872b3f6d66SOleksandr Tymoshenko 	client = (struct evdev_client *)kn->kn_hook;
3882b3f6d66SOleksandr Tymoshenko 	knlist_remove(&client->ec_selp.si_note, kn, 0);
3892b3f6d66SOleksandr Tymoshenko }
3902b3f6d66SOleksandr Tymoshenko 
3912b3f6d66SOleksandr Tymoshenko static int
evdev_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)3922b3f6d66SOleksandr Tymoshenko evdev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
3932b3f6d66SOleksandr Tymoshenko     struct thread *td)
3942b3f6d66SOleksandr Tymoshenko {
3952b3f6d66SOleksandr Tymoshenko 	struct evdev_dev *evdev = dev->si_drv1;
3962b3f6d66SOleksandr Tymoshenko 	struct evdev_client *client;
3972b3f6d66SOleksandr Tymoshenko 	struct input_keymap_entry *ke;
398b360682aSVladimir Kondratyev 	struct epoch_tracker et;
3992b3f6d66SOleksandr Tymoshenko 	int ret, len, limit, type_num;
4002b3f6d66SOleksandr Tymoshenko 	uint32_t code;
4012b3f6d66SOleksandr Tymoshenko 	size_t nvalues;
4022b3f6d66SOleksandr Tymoshenko 
4032b3f6d66SOleksandr Tymoshenko 	ret = devfs_get_cdevpriv((void **)&client);
4042b3f6d66SOleksandr Tymoshenko 	if (ret != 0)
4052b3f6d66SOleksandr Tymoshenko 		return (ret);
4062b3f6d66SOleksandr Tymoshenko 
4072b3f6d66SOleksandr Tymoshenko 	if (client->ec_revoked || evdev == NULL)
4082b3f6d66SOleksandr Tymoshenko 		return (ENODEV);
4092b3f6d66SOleksandr Tymoshenko 
4105ef24889SVladimir Kondratyev 	/*
4115ef24889SVladimir Kondratyev 	 * Fix evdev state corrupted with discarding of kdb events.
4125ef24889SVladimir Kondratyev 	 * EVIOCGKEY and EVIOCGLED ioctls can suffer from this.
4135ef24889SVladimir Kondratyev 	 */
4145ef24889SVladimir Kondratyev 	if (evdev->ev_kdb_active) {
4155ef24889SVladimir Kondratyev 		EVDEV_LOCK(evdev);
4165ef24889SVladimir Kondratyev 		if (evdev->ev_kdb_active) {
4175ef24889SVladimir Kondratyev 			evdev->ev_kdb_active = false;
418b360682aSVladimir Kondratyev 			if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
419b360682aSVladimir Kondratyev 				epoch_enter_preempt(INPUT_EPOCH, &et);
4205ef24889SVladimir Kondratyev 			evdev_restore_after_kdb(evdev);
421b360682aSVladimir Kondratyev 			if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
422b360682aSVladimir Kondratyev 				epoch_exit_preempt(INPUT_EPOCH, &et);
4235ef24889SVladimir Kondratyev 		}
4245ef24889SVladimir Kondratyev 		EVDEV_UNLOCK(evdev);
4255ef24889SVladimir Kondratyev 	}
4265ef24889SVladimir Kondratyev 
4272b3f6d66SOleksandr Tymoshenko 	/* file I/O ioctl handling */
4282b3f6d66SOleksandr Tymoshenko 	switch (cmd) {
4292b3f6d66SOleksandr Tymoshenko 	case FIOSETOWN:
4302b3f6d66SOleksandr Tymoshenko 		return (fsetown(*(int *)data, &client->ec_sigio));
4312b3f6d66SOleksandr Tymoshenko 
4322b3f6d66SOleksandr Tymoshenko 	case FIOGETOWN:
4332b3f6d66SOleksandr Tymoshenko 		*(int *)data = fgetown(&client->ec_sigio);
4342b3f6d66SOleksandr Tymoshenko 		return (0);
4352b3f6d66SOleksandr Tymoshenko 
4362b3f6d66SOleksandr Tymoshenko 	case FIONBIO:
4372b3f6d66SOleksandr Tymoshenko 		return (0);
4382b3f6d66SOleksandr Tymoshenko 
4392b3f6d66SOleksandr Tymoshenko 	case FIOASYNC:
4402b3f6d66SOleksandr Tymoshenko 		if (*(int *)data)
4412b3f6d66SOleksandr Tymoshenko 			client->ec_async = true;
4422b3f6d66SOleksandr Tymoshenko 		else
4432b3f6d66SOleksandr Tymoshenko 			client->ec_async = false;
4442b3f6d66SOleksandr Tymoshenko 
4452b3f6d66SOleksandr Tymoshenko 		return (0);
4462b3f6d66SOleksandr Tymoshenko 
4472b3f6d66SOleksandr Tymoshenko 	case FIONREAD:
4482b3f6d66SOleksandr Tymoshenko 		EVDEV_CLIENT_LOCKQ(client);
4492b3f6d66SOleksandr Tymoshenko 		*(int *)data =
4502b3f6d66SOleksandr Tymoshenko 		    EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event);
4512b3f6d66SOleksandr Tymoshenko 		EVDEV_CLIENT_UNLOCKQ(client);
4522b3f6d66SOleksandr Tymoshenko 		return (0);
4532b3f6d66SOleksandr Tymoshenko 	}
4542b3f6d66SOleksandr Tymoshenko 
4552b3f6d66SOleksandr Tymoshenko 	len = IOCPARM_LEN(cmd);
4562b3f6d66SOleksandr Tymoshenko 	debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data);
4572b3f6d66SOleksandr Tymoshenko 
4582b3f6d66SOleksandr Tymoshenko 	/* evdev fixed-length ioctls handling */
4592b3f6d66SOleksandr Tymoshenko 	switch (cmd) {
4602b3f6d66SOleksandr Tymoshenko 	case EVIOCGVERSION:
4612b3f6d66SOleksandr Tymoshenko 		*(int *)data = EV_VERSION;
4622b3f6d66SOleksandr Tymoshenko 		return (0);
4632b3f6d66SOleksandr Tymoshenko 
4642b3f6d66SOleksandr Tymoshenko 	case EVIOCGID:
4652b3f6d66SOleksandr Tymoshenko 		debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x",
4662b3f6d66SOleksandr Tymoshenko 		    evdev->ev_id.bustype, evdev->ev_id.vendor,
4672b3f6d66SOleksandr Tymoshenko 		    evdev->ev_id.product);
4682b3f6d66SOleksandr Tymoshenko 		memcpy(data, &evdev->ev_id, sizeof(struct input_id));
4692b3f6d66SOleksandr Tymoshenko 		return (0);
4702b3f6d66SOleksandr Tymoshenko 
4712b3f6d66SOleksandr Tymoshenko 	case EVIOCGREP:
4722b3f6d66SOleksandr Tymoshenko 		if (!evdev_event_supported(evdev, EV_REP))
4732b3f6d66SOleksandr Tymoshenko 			return (ENOTSUP);
4742b3f6d66SOleksandr Tymoshenko 
4752b3f6d66SOleksandr Tymoshenko 		memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep));
4762b3f6d66SOleksandr Tymoshenko 		return (0);
4772b3f6d66SOleksandr Tymoshenko 
4782b3f6d66SOleksandr Tymoshenko 	case EVIOCSREP:
4792b3f6d66SOleksandr Tymoshenko 		if (!evdev_event_supported(evdev, EV_REP))
4802b3f6d66SOleksandr Tymoshenko 			return (ENOTSUP);
4812b3f6d66SOleksandr Tymoshenko 
4822b3f6d66SOleksandr Tymoshenko 		evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]);
4832b3f6d66SOleksandr Tymoshenko 		evdev_inject_event(evdev, EV_REP, REP_PERIOD,
4842b3f6d66SOleksandr Tymoshenko 		    ((int *)data)[1]);
4852b3f6d66SOleksandr Tymoshenko 		return (0);
4862b3f6d66SOleksandr Tymoshenko 
4872b3f6d66SOleksandr Tymoshenko 	case EVIOCGKEYCODE:
4882b3f6d66SOleksandr Tymoshenko 		/* Fake unsupported ioctl */
4892b3f6d66SOleksandr Tymoshenko 		return (0);
4902b3f6d66SOleksandr Tymoshenko 
4912b3f6d66SOleksandr Tymoshenko 	case EVIOCGKEYCODE_V2:
4922b3f6d66SOleksandr Tymoshenko 		if (evdev->ev_methods == NULL ||
4932b3f6d66SOleksandr Tymoshenko 		    evdev->ev_methods->ev_get_keycode == NULL)
4942b3f6d66SOleksandr Tymoshenko 			return (ENOTSUP);
4952b3f6d66SOleksandr Tymoshenko 
4962b3f6d66SOleksandr Tymoshenko 		ke = (struct input_keymap_entry *)data;
497911aed94SVladimir Kondratyev 		evdev->ev_methods->ev_get_keycode(evdev, ke);
4982b3f6d66SOleksandr Tymoshenko 		return (0);
4992b3f6d66SOleksandr Tymoshenko 
5002b3f6d66SOleksandr Tymoshenko 	case EVIOCSKEYCODE:
5012b3f6d66SOleksandr Tymoshenko 		/* Fake unsupported ioctl */
5022b3f6d66SOleksandr Tymoshenko 		return (0);
5032b3f6d66SOleksandr Tymoshenko 
5042b3f6d66SOleksandr Tymoshenko 	case EVIOCSKEYCODE_V2:
5052b3f6d66SOleksandr Tymoshenko 		if (evdev->ev_methods == NULL ||
5062b3f6d66SOleksandr Tymoshenko 		    evdev->ev_methods->ev_set_keycode == NULL)
5072b3f6d66SOleksandr Tymoshenko 			return (ENOTSUP);
5082b3f6d66SOleksandr Tymoshenko 
5092b3f6d66SOleksandr Tymoshenko 		ke = (struct input_keymap_entry *)data;
510911aed94SVladimir Kondratyev 		evdev->ev_methods->ev_set_keycode(evdev, ke);
5112b3f6d66SOleksandr Tymoshenko 		return (0);
5122b3f6d66SOleksandr Tymoshenko 
5132b3f6d66SOleksandr Tymoshenko 	case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX):
5142b3f6d66SOleksandr Tymoshenko 		if (evdev->ev_absinfo == NULL)
5152b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
5162b3f6d66SOleksandr Tymoshenko 
5172b3f6d66SOleksandr Tymoshenko 		memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)],
5182b3f6d66SOleksandr Tymoshenko 		    sizeof(struct input_absinfo));
5192b3f6d66SOleksandr Tymoshenko 		return (0);
5202b3f6d66SOleksandr Tymoshenko 
5212b3f6d66SOleksandr Tymoshenko 	case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX):
5222b3f6d66SOleksandr Tymoshenko 		if (evdev->ev_absinfo == NULL)
5232b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
5242b3f6d66SOleksandr Tymoshenko 
5252b3f6d66SOleksandr Tymoshenko 		code = cmd - EVIOCSABS(0);
5262b3f6d66SOleksandr Tymoshenko 		/* mt-slot number can not be changed */
5272b3f6d66SOleksandr Tymoshenko 		if (code == ABS_MT_SLOT)
5282b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
5292b3f6d66SOleksandr Tymoshenko 
5302b3f6d66SOleksandr Tymoshenko 		EVDEV_LOCK(evdev);
5312b3f6d66SOleksandr Tymoshenko 		evdev_set_absinfo(evdev, code, (struct input_absinfo *)data);
5322b3f6d66SOleksandr Tymoshenko 		EVDEV_UNLOCK(evdev);
5332b3f6d66SOleksandr Tymoshenko 		return (0);
5342b3f6d66SOleksandr Tymoshenko 
5352b3f6d66SOleksandr Tymoshenko 	case EVIOCSFF:
5362b3f6d66SOleksandr Tymoshenko 	case EVIOCRMFF:
5372b3f6d66SOleksandr Tymoshenko 	case EVIOCGEFFECTS:
5382b3f6d66SOleksandr Tymoshenko 		/* Fake unsupported ioctls */
5392b3f6d66SOleksandr Tymoshenko 		return (0);
5402b3f6d66SOleksandr Tymoshenko 
5412b3f6d66SOleksandr Tymoshenko 	case EVIOCGRAB:
5422b3f6d66SOleksandr Tymoshenko 		EVDEV_LOCK(evdev);
5432b3f6d66SOleksandr Tymoshenko 		if (*(int *)data)
5442b3f6d66SOleksandr Tymoshenko 			ret = evdev_grab_client(evdev, client);
5452b3f6d66SOleksandr Tymoshenko 		else
5462b3f6d66SOleksandr Tymoshenko 			ret = evdev_release_client(evdev, client);
5472b3f6d66SOleksandr Tymoshenko 		EVDEV_UNLOCK(evdev);
5482b3f6d66SOleksandr Tymoshenko 		return (ret);
5492b3f6d66SOleksandr Tymoshenko 
5502b3f6d66SOleksandr Tymoshenko 	case EVIOCREVOKE:
5512b3f6d66SOleksandr Tymoshenko 		if (*(int *)data != 0)
5522b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
5532b3f6d66SOleksandr Tymoshenko 
554d276eae6SVladimir Kondratyev 		EVDEV_LIST_LOCK(evdev);
5552b3f6d66SOleksandr Tymoshenko 		if (dev->si_drv1 != NULL && !client->ec_revoked) {
5562b3f6d66SOleksandr Tymoshenko 			evdev_dispose_client(evdev, client);
5572b3f6d66SOleksandr Tymoshenko 			evdev_revoke_client(client);
5582b3f6d66SOleksandr Tymoshenko 		}
559d276eae6SVladimir Kondratyev 		EVDEV_LIST_UNLOCK(evdev);
5602b3f6d66SOleksandr Tymoshenko 		return (0);
5612b3f6d66SOleksandr Tymoshenko 
5622b3f6d66SOleksandr Tymoshenko 	case EVIOCSCLOCKID:
5632b3f6d66SOleksandr Tymoshenko 		switch (*(int *)data) {
5642b3f6d66SOleksandr Tymoshenko 		case CLOCK_REALTIME:
5652b3f6d66SOleksandr Tymoshenko 			client->ec_clock_id = EV_CLOCK_REALTIME;
5662b3f6d66SOleksandr Tymoshenko 			return (0);
5672b3f6d66SOleksandr Tymoshenko 		case CLOCK_MONOTONIC:
5682b3f6d66SOleksandr Tymoshenko 			client->ec_clock_id = EV_CLOCK_MONOTONIC;
5692b3f6d66SOleksandr Tymoshenko 			return (0);
5702b3f6d66SOleksandr Tymoshenko 		default:
5712b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
5722b3f6d66SOleksandr Tymoshenko 		}
5732b3f6d66SOleksandr Tymoshenko 	}
5742b3f6d66SOleksandr Tymoshenko 
5752b3f6d66SOleksandr Tymoshenko 	/* evdev variable-length ioctls handling */
5762b3f6d66SOleksandr Tymoshenko 	switch (IOCBASECMD(cmd)) {
5772b3f6d66SOleksandr Tymoshenko 	case EVIOCGNAME(0):
5787a810290SVladimir Kondratyev 		/* Linux evdev does not terminate truncated strings with 0 */
5797a810290SVladimir Kondratyev 		limit = MIN(strlen(evdev->ev_name) + 1, len);
5807a810290SVladimir Kondratyev 		memcpy(data, evdev->ev_name, limit);
5817a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
5822b3f6d66SOleksandr Tymoshenko 		return (0);
5832b3f6d66SOleksandr Tymoshenko 
5842b3f6d66SOleksandr Tymoshenko 	case EVIOCGPHYS(0):
5852b3f6d66SOleksandr Tymoshenko 		if (evdev->ev_shortname[0] == 0)
5862b3f6d66SOleksandr Tymoshenko 			return (ENOENT);
5872b3f6d66SOleksandr Tymoshenko 
5887a810290SVladimir Kondratyev 		limit = MIN(strlen(evdev->ev_shortname) + 1, len);
5897a810290SVladimir Kondratyev 		memcpy(data, evdev->ev_shortname, limit);
5907a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
5912b3f6d66SOleksandr Tymoshenko 		return (0);
5922b3f6d66SOleksandr Tymoshenko 
5932b3f6d66SOleksandr Tymoshenko 	case EVIOCGUNIQ(0):
5942b3f6d66SOleksandr Tymoshenko 		if (evdev->ev_serial[0] == 0)
5952b3f6d66SOleksandr Tymoshenko 			return (ENOENT);
5962b3f6d66SOleksandr Tymoshenko 
5977a810290SVladimir Kondratyev 		limit = MIN(strlen(evdev->ev_serial) + 1, len);
5987a810290SVladimir Kondratyev 		memcpy(data, evdev->ev_serial, limit);
5997a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
6002b3f6d66SOleksandr Tymoshenko 		return (0);
6012b3f6d66SOleksandr Tymoshenko 
6022b3f6d66SOleksandr Tymoshenko 	case EVIOCGPROP(0):
6032b3f6d66SOleksandr Tymoshenko 		limit = MIN(len, bitstr_size(INPUT_PROP_CNT));
6042b3f6d66SOleksandr Tymoshenko 		memcpy(data, evdev->ev_prop_flags, limit);
6057a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
6062b3f6d66SOleksandr Tymoshenko 		return (0);
6072b3f6d66SOleksandr Tymoshenko 
6082b3f6d66SOleksandr Tymoshenko 	case EVIOCGMTSLOTS(0):
6097a810290SVladimir Kondratyev 		/* EVIOCGMTSLOTS always returns 0 on success */
6102b3f6d66SOleksandr Tymoshenko 		if (evdev->ev_mt == NULL)
6112b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
6122b3f6d66SOleksandr Tymoshenko 		if (len < sizeof(uint32_t))
6132b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
6142b3f6d66SOleksandr Tymoshenko 		code = *(uint32_t *)data;
6152b3f6d66SOleksandr Tymoshenko 		if (!ABS_IS_MT(code))
6162b3f6d66SOleksandr Tymoshenko 			return (EINVAL);
6172b3f6d66SOleksandr Tymoshenko 
6182b3f6d66SOleksandr Tymoshenko 		nvalues =
6192b3f6d66SOleksandr Tymoshenko 		    MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1);
6202b3f6d66SOleksandr Tymoshenko 		for (int i = 0; i < nvalues; i++)
6212b3f6d66SOleksandr Tymoshenko 			((int32_t *)data)[i + 1] =
622*98a7606bSVladimir Kondratyev 			    evdev_mt_get_value(evdev, i, code);
6232b3f6d66SOleksandr Tymoshenko 		return (0);
6242b3f6d66SOleksandr Tymoshenko 
6252b3f6d66SOleksandr Tymoshenko 	case EVIOCGKEY(0):
6262b3f6d66SOleksandr Tymoshenko 		limit = MIN(len, bitstr_size(KEY_CNT));
6272b3f6d66SOleksandr Tymoshenko 		EVDEV_LOCK(evdev);
6282b3f6d66SOleksandr Tymoshenko 		evdev_client_filter_queue(client, EV_KEY);
6292b3f6d66SOleksandr Tymoshenko 		memcpy(data, evdev->ev_key_states, limit);
6302b3f6d66SOleksandr Tymoshenko 		EVDEV_UNLOCK(evdev);
6317a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
6322b3f6d66SOleksandr Tymoshenko 		return (0);
6332b3f6d66SOleksandr Tymoshenko 
6342b3f6d66SOleksandr Tymoshenko 	case EVIOCGLED(0):
6352b3f6d66SOleksandr Tymoshenko 		limit = MIN(len, bitstr_size(LED_CNT));
6362b3f6d66SOleksandr Tymoshenko 		EVDEV_LOCK(evdev);
6372b3f6d66SOleksandr Tymoshenko 		evdev_client_filter_queue(client, EV_LED);
6382b3f6d66SOleksandr Tymoshenko 		memcpy(data, evdev->ev_led_states, limit);
6392b3f6d66SOleksandr Tymoshenko 		EVDEV_UNLOCK(evdev);
6407a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
6412b3f6d66SOleksandr Tymoshenko 		return (0);
6422b3f6d66SOleksandr Tymoshenko 
6432b3f6d66SOleksandr Tymoshenko 	case EVIOCGSND(0):
6442b3f6d66SOleksandr Tymoshenko 		limit = MIN(len, bitstr_size(SND_CNT));
6452b3f6d66SOleksandr Tymoshenko 		EVDEV_LOCK(evdev);
6462b3f6d66SOleksandr Tymoshenko 		evdev_client_filter_queue(client, EV_SND);
6472b3f6d66SOleksandr Tymoshenko 		memcpy(data, evdev->ev_snd_states, limit);
6482b3f6d66SOleksandr Tymoshenko 		EVDEV_UNLOCK(evdev);
6497a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
6502b3f6d66SOleksandr Tymoshenko 		return (0);
6512b3f6d66SOleksandr Tymoshenko 
6522b3f6d66SOleksandr Tymoshenko 	case EVIOCGSW(0):
6532b3f6d66SOleksandr Tymoshenko 		limit = MIN(len, bitstr_size(SW_CNT));
6542b3f6d66SOleksandr Tymoshenko 		EVDEV_LOCK(evdev);
6552b3f6d66SOleksandr Tymoshenko 		evdev_client_filter_queue(client, EV_SW);
6562b3f6d66SOleksandr Tymoshenko 		memcpy(data, evdev->ev_sw_states, limit);
6572b3f6d66SOleksandr Tymoshenko 		EVDEV_UNLOCK(evdev);
6587a810290SVladimir Kondratyev 		td->td_retval[0] = limit;
6592b3f6d66SOleksandr Tymoshenko 		return (0);
6602b3f6d66SOleksandr Tymoshenko 
6612b3f6d66SOleksandr Tymoshenko 	case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0):
6622b3f6d66SOleksandr Tymoshenko 		type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0);
6632b3f6d66SOleksandr Tymoshenko 		debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num,
6642b3f6d66SOleksandr Tymoshenko 		    data, len);
6657a810290SVladimir Kondratyev 		return (evdev_ioctl_eviocgbit(evdev, type_num, len, data, td));
6662b3f6d66SOleksandr Tymoshenko 	}
6672b3f6d66SOleksandr Tymoshenko 
6682b3f6d66SOleksandr Tymoshenko 	return (EINVAL);
6692b3f6d66SOleksandr Tymoshenko }
6702b3f6d66SOleksandr Tymoshenko 
6712b3f6d66SOleksandr Tymoshenko static int
evdev_ioctl_eviocgbit(struct evdev_dev * evdev,int type,int len,caddr_t data,struct thread * td)6727a810290SVladimir Kondratyev evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data,
6737a810290SVladimir Kondratyev     struct thread *td)
6742b3f6d66SOleksandr Tymoshenko {
6752b3f6d66SOleksandr Tymoshenko 	unsigned long *bitmap;
6762b3f6d66SOleksandr Tymoshenko 	int limit;
6772b3f6d66SOleksandr Tymoshenko 
6782b3f6d66SOleksandr Tymoshenko 	switch (type) {
6792b3f6d66SOleksandr Tymoshenko 	case 0:
6802b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_type_flags;
6812b3f6d66SOleksandr Tymoshenko 		limit = EV_CNT;
6822b3f6d66SOleksandr Tymoshenko 		break;
6832b3f6d66SOleksandr Tymoshenko 	case EV_KEY:
6842b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_key_flags;
6852b3f6d66SOleksandr Tymoshenko 		limit = KEY_CNT;
6862b3f6d66SOleksandr Tymoshenko 		break;
6872b3f6d66SOleksandr Tymoshenko 	case EV_REL:
6882b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_rel_flags;
6892b3f6d66SOleksandr Tymoshenko 		limit = REL_CNT;
6902b3f6d66SOleksandr Tymoshenko 		break;
6912b3f6d66SOleksandr Tymoshenko 	case EV_ABS:
6922b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_abs_flags;
6932b3f6d66SOleksandr Tymoshenko 		limit = ABS_CNT;
6942b3f6d66SOleksandr Tymoshenko 		break;
6952b3f6d66SOleksandr Tymoshenko 	case EV_MSC:
6962b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_msc_flags;
6972b3f6d66SOleksandr Tymoshenko 		limit = MSC_CNT;
6982b3f6d66SOleksandr Tymoshenko 		break;
6992b3f6d66SOleksandr Tymoshenko 	case EV_LED:
7002b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_led_flags;
7012b3f6d66SOleksandr Tymoshenko 		limit = LED_CNT;
7022b3f6d66SOleksandr Tymoshenko 		break;
7032b3f6d66SOleksandr Tymoshenko 	case EV_SND:
7042b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_snd_flags;
7052b3f6d66SOleksandr Tymoshenko 		limit = SND_CNT;
7062b3f6d66SOleksandr Tymoshenko 		break;
7072b3f6d66SOleksandr Tymoshenko 	case EV_SW:
7082b3f6d66SOleksandr Tymoshenko 		bitmap = evdev->ev_sw_flags;
7092b3f6d66SOleksandr Tymoshenko 		limit = SW_CNT;
7102b3f6d66SOleksandr Tymoshenko 		break;
7112b3f6d66SOleksandr Tymoshenko 	case EV_FF:
7122b3f6d66SOleksandr Tymoshenko 		/*
7132b3f6d66SOleksandr Tymoshenko 		 * We don't support EV_FF now, so let's
7142b3f6d66SOleksandr Tymoshenko 		 * just fake it returning only zeros.
7152b3f6d66SOleksandr Tymoshenko 		 */
7162b3f6d66SOleksandr Tymoshenko 		bzero(data, len);
7177a810290SVladimir Kondratyev 		td->td_retval[0] = len;
7182b3f6d66SOleksandr Tymoshenko 		return (0);
7192b3f6d66SOleksandr Tymoshenko 	default:
7202b3f6d66SOleksandr Tymoshenko 		return (ENOTTY);
7212b3f6d66SOleksandr Tymoshenko 	}
7222b3f6d66SOleksandr Tymoshenko 
7232b3f6d66SOleksandr Tymoshenko 	/*
7242b3f6d66SOleksandr Tymoshenko 	 * Clear ioctl data buffer in case it's bigger than
7252b3f6d66SOleksandr Tymoshenko 	 * bitmap size
7262b3f6d66SOleksandr Tymoshenko 	 */
7272b3f6d66SOleksandr Tymoshenko 	bzero(data, len);
7282b3f6d66SOleksandr Tymoshenko 
7292b3f6d66SOleksandr Tymoshenko 	limit = bitstr_size(limit);
7302b3f6d66SOleksandr Tymoshenko 	len = MIN(limit, len);
7312b3f6d66SOleksandr Tymoshenko 	memcpy(data, bitmap, len);
7327a810290SVladimir Kondratyev 	td->td_retval[0] = len;
7332b3f6d66SOleksandr Tymoshenko 	return (0);
7342b3f6d66SOleksandr Tymoshenko }
7352b3f6d66SOleksandr Tymoshenko 
7362b3f6d66SOleksandr Tymoshenko void
evdev_revoke_client(struct evdev_client * client)7372b3f6d66SOleksandr Tymoshenko evdev_revoke_client(struct evdev_client *client)
7382b3f6d66SOleksandr Tymoshenko {
7392b3f6d66SOleksandr Tymoshenko 
740d276eae6SVladimir Kondratyev 	EVDEV_LIST_LOCK_ASSERT(client->ec_evdev);
7412b3f6d66SOleksandr Tymoshenko 
7422b3f6d66SOleksandr Tymoshenko 	client->ec_revoked = true;
7432b3f6d66SOleksandr Tymoshenko }
7442b3f6d66SOleksandr Tymoshenko 
7452b3f6d66SOleksandr Tymoshenko void
evdev_notify_event(struct evdev_client * client)7462b3f6d66SOleksandr Tymoshenko evdev_notify_event(struct evdev_client *client)
7472b3f6d66SOleksandr Tymoshenko {
7482b3f6d66SOleksandr Tymoshenko 
7492b3f6d66SOleksandr Tymoshenko 	EVDEV_CLIENT_LOCKQ_ASSERT(client);
7502b3f6d66SOleksandr Tymoshenko 
7512b3f6d66SOleksandr Tymoshenko 	if (client->ec_blocked) {
7522b3f6d66SOleksandr Tymoshenko 		client->ec_blocked = false;
7532b3f6d66SOleksandr Tymoshenko 		wakeup(client);
7542b3f6d66SOleksandr Tymoshenko 	}
7552b3f6d66SOleksandr Tymoshenko 	if (client->ec_selected) {
7562b3f6d66SOleksandr Tymoshenko 		client->ec_selected = false;
7572b3f6d66SOleksandr Tymoshenko 		selwakeup(&client->ec_selp);
7582b3f6d66SOleksandr Tymoshenko 	}
7592b3f6d66SOleksandr Tymoshenko 	KNOTE_LOCKED(&client->ec_selp.si_note, 0);
7602b3f6d66SOleksandr Tymoshenko 
7612b3f6d66SOleksandr Tymoshenko 	if (client->ec_async && client->ec_sigio != NULL)
7622b3f6d66SOleksandr Tymoshenko 		pgsigio(&client->ec_sigio, SIGIO, 0);
7632b3f6d66SOleksandr Tymoshenko }
7642b3f6d66SOleksandr Tymoshenko 
7652b3f6d66SOleksandr Tymoshenko int
evdev_cdev_create(struct evdev_dev * evdev)7662b3f6d66SOleksandr Tymoshenko evdev_cdev_create(struct evdev_dev *evdev)
7672b3f6d66SOleksandr Tymoshenko {
7682b3f6d66SOleksandr Tymoshenko 	struct make_dev_args mda;
7692b3f6d66SOleksandr Tymoshenko 	int ret, unit = 0;
7702b3f6d66SOleksandr Tymoshenko 
7712b3f6d66SOleksandr Tymoshenko 	make_dev_args_init(&mda);
7722b3f6d66SOleksandr Tymoshenko 	mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
7732b3f6d66SOleksandr Tymoshenko 	mda.mda_devsw = &evdev_cdevsw;
7742b3f6d66SOleksandr Tymoshenko 	mda.mda_uid = UID_ROOT;
7752b3f6d66SOleksandr Tymoshenko 	mda.mda_gid = GID_WHEEL;
7762b3f6d66SOleksandr Tymoshenko 	mda.mda_mode = 0600;
7772b3f6d66SOleksandr Tymoshenko 	mda.mda_si_drv1 = evdev;
7782b3f6d66SOleksandr Tymoshenko 
7792b3f6d66SOleksandr Tymoshenko 	/* Try to coexist with cuse-backed input/event devices */
7802b3f6d66SOleksandr Tymoshenko 	while ((ret = make_dev_s(&mda, &evdev->ev_cdev, "input/event%d", unit))
7812b3f6d66SOleksandr Tymoshenko 	    == EEXIST)
7822b3f6d66SOleksandr Tymoshenko 		unit++;
7832b3f6d66SOleksandr Tymoshenko 
7842b3f6d66SOleksandr Tymoshenko 	if (ret == 0)
7852b3f6d66SOleksandr Tymoshenko 		evdev->ev_unit = unit;
7862b3f6d66SOleksandr Tymoshenko 
7872b3f6d66SOleksandr Tymoshenko 	return (ret);
7882b3f6d66SOleksandr Tymoshenko }
7892b3f6d66SOleksandr Tymoshenko 
7902b3f6d66SOleksandr Tymoshenko int
evdev_cdev_destroy(struct evdev_dev * evdev)7912b3f6d66SOleksandr Tymoshenko evdev_cdev_destroy(struct evdev_dev *evdev)
7922b3f6d66SOleksandr Tymoshenko {
7932b3f6d66SOleksandr Tymoshenko 
7942b3f6d66SOleksandr Tymoshenko 	destroy_dev(evdev->ev_cdev);
7952b3f6d66SOleksandr Tymoshenko 	return (0);
7962b3f6d66SOleksandr Tymoshenko }
7972b3f6d66SOleksandr Tymoshenko 
7982b3f6d66SOleksandr Tymoshenko static void
evdev_client_gettime(struct evdev_client * client,struct timeval * tv)7992b3f6d66SOleksandr Tymoshenko evdev_client_gettime(struct evdev_client *client, struct timeval *tv)
8002b3f6d66SOleksandr Tymoshenko {
8012b3f6d66SOleksandr Tymoshenko 
8022b3f6d66SOleksandr Tymoshenko 	switch (client->ec_clock_id) {
8032b3f6d66SOleksandr Tymoshenko 	case EV_CLOCK_BOOTTIME:
8042b3f6d66SOleksandr Tymoshenko 		/*
8052b3f6d66SOleksandr Tymoshenko 		 * XXX: FreeBSD does not support true POSIX monotonic clock.
8062b3f6d66SOleksandr Tymoshenko 		 *      So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC.
8072b3f6d66SOleksandr Tymoshenko 		 */
8082b3f6d66SOleksandr Tymoshenko 	case EV_CLOCK_MONOTONIC:
8092b3f6d66SOleksandr Tymoshenko 		microuptime(tv);
8102b3f6d66SOleksandr Tymoshenko 		break;
8112b3f6d66SOleksandr Tymoshenko 
8122b3f6d66SOleksandr Tymoshenko 	case EV_CLOCK_REALTIME:
8132b3f6d66SOleksandr Tymoshenko 	default:
8142b3f6d66SOleksandr Tymoshenko 		microtime(tv);
8152b3f6d66SOleksandr Tymoshenko 		break;
8162b3f6d66SOleksandr Tymoshenko 	}
8172b3f6d66SOleksandr Tymoshenko }
8182b3f6d66SOleksandr Tymoshenko 
8192b3f6d66SOleksandr Tymoshenko void
evdev_client_push(struct evdev_client * client,uint16_t type,uint16_t code,int32_t value)8202b3f6d66SOleksandr Tymoshenko evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code,
8212b3f6d66SOleksandr Tymoshenko     int32_t value)
8222b3f6d66SOleksandr Tymoshenko {
8232b3f6d66SOleksandr Tymoshenko 	struct timeval time;
8242b3f6d66SOleksandr Tymoshenko 	size_t count, head, tail, ready;
8252b3f6d66SOleksandr Tymoshenko 
8262b3f6d66SOleksandr Tymoshenko 	EVDEV_CLIENT_LOCKQ_ASSERT(client);
8272b3f6d66SOleksandr Tymoshenko 	head = client->ec_buffer_head;
8282b3f6d66SOleksandr Tymoshenko 	tail = client->ec_buffer_tail;
8292b3f6d66SOleksandr Tymoshenko 	ready = client->ec_buffer_ready;
8302b3f6d66SOleksandr Tymoshenko 	count = client->ec_buffer_size;
8312b3f6d66SOleksandr Tymoshenko 
8322b3f6d66SOleksandr Tymoshenko 	/* If queue is full drop its content and place SYN_DROPPED event */
8332b3f6d66SOleksandr Tymoshenko 	if ((tail + 1) % count == head) {
8342b3f6d66SOleksandr Tymoshenko 		debugf(client, "client %p: buffer overflow", client);
8352b3f6d66SOleksandr Tymoshenko 
8362b3f6d66SOleksandr Tymoshenko 		head = (tail + count - 1) % count;
8372b3f6d66SOleksandr Tymoshenko 		client->ec_buffer[head] = (struct input_event) {
8382b3f6d66SOleksandr Tymoshenko 			.type = EV_SYN,
8392b3f6d66SOleksandr Tymoshenko 			.code = SYN_DROPPED,
8402b3f6d66SOleksandr Tymoshenko 			.value = 0
8412b3f6d66SOleksandr Tymoshenko 		};
8422b3f6d66SOleksandr Tymoshenko 		/*
8432b3f6d66SOleksandr Tymoshenko 		 * XXX: Here is a small race window from now till the end of
8442b3f6d66SOleksandr Tymoshenko 		 *      report. The queue is empty but client has been already
8452b3f6d66SOleksandr Tymoshenko 		 *      notified of data readyness. Can be fixed in two ways:
8462b3f6d66SOleksandr Tymoshenko 		 * 1. Implement bulk insert so queue lock would not be dropped
8472b3f6d66SOleksandr Tymoshenko 		 *    till the SYN_REPORT event.
8482b3f6d66SOleksandr Tymoshenko 		 * 2. Insert SYN_REPORT just now and skip remaining events
8492b3f6d66SOleksandr Tymoshenko 		 */
8502b3f6d66SOleksandr Tymoshenko 		client->ec_buffer_head = head;
8512b3f6d66SOleksandr Tymoshenko 		client->ec_buffer_ready = head;
8522b3f6d66SOleksandr Tymoshenko 	}
8532b3f6d66SOleksandr Tymoshenko 
8542b3f6d66SOleksandr Tymoshenko 	client->ec_buffer[tail].type = type;
8552b3f6d66SOleksandr Tymoshenko 	client->ec_buffer[tail].code = code;
8562b3f6d66SOleksandr Tymoshenko 	client->ec_buffer[tail].value = value;
8572b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_tail = (tail + 1) % count;
8582b3f6d66SOleksandr Tymoshenko 
8592b3f6d66SOleksandr Tymoshenko 	/* Allow users to read events only after report has been completed */
8602b3f6d66SOleksandr Tymoshenko 	if (type == EV_SYN && code == SYN_REPORT) {
8612b3f6d66SOleksandr Tymoshenko 		evdev_client_gettime(client, &time);
8622b3f6d66SOleksandr Tymoshenko 		for (; ready != client->ec_buffer_tail;
8632b3f6d66SOleksandr Tymoshenko 		    ready = (ready + 1) % count)
8642b3f6d66SOleksandr Tymoshenko 			client->ec_buffer[ready].time = time;
8652b3f6d66SOleksandr Tymoshenko 		client->ec_buffer_ready = client->ec_buffer_tail;
8662b3f6d66SOleksandr Tymoshenko 	}
8672b3f6d66SOleksandr Tymoshenko }
8682b3f6d66SOleksandr Tymoshenko 
8692b3f6d66SOleksandr Tymoshenko void
evdev_client_dumpqueue(struct evdev_client * client)8702b3f6d66SOleksandr Tymoshenko evdev_client_dumpqueue(struct evdev_client *client)
8712b3f6d66SOleksandr Tymoshenko {
8722b3f6d66SOleksandr Tymoshenko 	struct input_event *event;
8732b3f6d66SOleksandr Tymoshenko 	size_t i, head, tail, ready, size;
8742b3f6d66SOleksandr Tymoshenko 
8752b3f6d66SOleksandr Tymoshenko 	head = client->ec_buffer_head;
8762b3f6d66SOleksandr Tymoshenko 	tail = client->ec_buffer_tail;
8772b3f6d66SOleksandr Tymoshenko 	ready = client->ec_buffer_ready;
8782b3f6d66SOleksandr Tymoshenko 	size = client->ec_buffer_size;
8792b3f6d66SOleksandr Tymoshenko 
8802b3f6d66SOleksandr Tymoshenko 	printf("evdev client: %p\n", client);
8812b3f6d66SOleksandr Tymoshenko 	printf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n",
8822b3f6d66SOleksandr Tymoshenko 	    head, ready, tail, size);
8832b3f6d66SOleksandr Tymoshenko 
8842b3f6d66SOleksandr Tymoshenko 	printf("queue contents:\n");
8852b3f6d66SOleksandr Tymoshenko 
8862b3f6d66SOleksandr Tymoshenko 	for (i = 0; i < size; i++) {
8872b3f6d66SOleksandr Tymoshenko 		event = &client->ec_buffer[i];
8882b3f6d66SOleksandr Tymoshenko 		printf("%zu: ", i);
8892b3f6d66SOleksandr Tymoshenko 
8902b3f6d66SOleksandr Tymoshenko 		if (i < head || i > tail)
8912b3f6d66SOleksandr Tymoshenko 			printf("unused\n");
8922b3f6d66SOleksandr Tymoshenko 		else
8932b3f6d66SOleksandr Tymoshenko 			printf("type=%d code=%d value=%d ", event->type,
8942b3f6d66SOleksandr Tymoshenko 			    event->code, event->value);
8952b3f6d66SOleksandr Tymoshenko 
8962b3f6d66SOleksandr Tymoshenko 		if (i == head)
8972b3f6d66SOleksandr Tymoshenko 			printf("<- head\n");
8982b3f6d66SOleksandr Tymoshenko 		else if (i == tail)
8992b3f6d66SOleksandr Tymoshenko 			printf("<- tail\n");
9002b3f6d66SOleksandr Tymoshenko 		else if (i == ready)
9012b3f6d66SOleksandr Tymoshenko 			printf("<- ready\n");
9022b3f6d66SOleksandr Tymoshenko 		else
9032b3f6d66SOleksandr Tymoshenko 			printf("\n");
9042b3f6d66SOleksandr Tymoshenko 	}
9052b3f6d66SOleksandr Tymoshenko }
9062b3f6d66SOleksandr Tymoshenko 
9072b3f6d66SOleksandr Tymoshenko static void
evdev_client_filter_queue(struct evdev_client * client,uint16_t type)9082b3f6d66SOleksandr Tymoshenko evdev_client_filter_queue(struct evdev_client *client, uint16_t type)
9092b3f6d66SOleksandr Tymoshenko {
9102b3f6d66SOleksandr Tymoshenko 	struct input_event *event;
9112b3f6d66SOleksandr Tymoshenko 	size_t head, tail, count, i;
9122b3f6d66SOleksandr Tymoshenko 	bool last_was_syn = false;
9132b3f6d66SOleksandr Tymoshenko 
9142b3f6d66SOleksandr Tymoshenko 	EVDEV_CLIENT_LOCKQ(client);
9152b3f6d66SOleksandr Tymoshenko 
9162b3f6d66SOleksandr Tymoshenko 	i = head = client->ec_buffer_head;
9172b3f6d66SOleksandr Tymoshenko 	tail = client->ec_buffer_tail;
9182b3f6d66SOleksandr Tymoshenko 	count = client->ec_buffer_size;
9192b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_ready = client->ec_buffer_tail;
9202b3f6d66SOleksandr Tymoshenko 
9212b3f6d66SOleksandr Tymoshenko 	while (i != client->ec_buffer_tail) {
9222b3f6d66SOleksandr Tymoshenko 		event = &client->ec_buffer[i];
9232b3f6d66SOleksandr Tymoshenko 		i = (i + 1) % count;
9242b3f6d66SOleksandr Tymoshenko 
9252b3f6d66SOleksandr Tymoshenko 		/* Skip event of given type */
9262b3f6d66SOleksandr Tymoshenko 		if (event->type == type)
9272b3f6d66SOleksandr Tymoshenko 			continue;
9282b3f6d66SOleksandr Tymoshenko 
9292b3f6d66SOleksandr Tymoshenko 		/* Remove empty SYN_REPORT events */
9302b3f6d66SOleksandr Tymoshenko 		if (event->type == EV_SYN && event->code == SYN_REPORT) {
9312b3f6d66SOleksandr Tymoshenko 			if (last_was_syn)
9322b3f6d66SOleksandr Tymoshenko 				continue;
9332b3f6d66SOleksandr Tymoshenko 			else
9342b3f6d66SOleksandr Tymoshenko 				client->ec_buffer_ready = (tail + 1) % count;
9352b3f6d66SOleksandr Tymoshenko 		}
9362b3f6d66SOleksandr Tymoshenko 
9372b3f6d66SOleksandr Tymoshenko 		/* Rewrite entry */
9382b3f6d66SOleksandr Tymoshenko 		memcpy(&client->ec_buffer[tail], event,
9392b3f6d66SOleksandr Tymoshenko 		    sizeof(struct input_event));
9402b3f6d66SOleksandr Tymoshenko 
9412b3f6d66SOleksandr Tymoshenko 		last_was_syn = (event->type == EV_SYN &&
9422b3f6d66SOleksandr Tymoshenko 		    event->code == SYN_REPORT);
9432b3f6d66SOleksandr Tymoshenko 
9442b3f6d66SOleksandr Tymoshenko 		tail = (tail + 1) % count;
9452b3f6d66SOleksandr Tymoshenko 	}
9462b3f6d66SOleksandr Tymoshenko 
9472b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_head = i;
9482b3f6d66SOleksandr Tymoshenko 	client->ec_buffer_tail = tail;
9492b3f6d66SOleksandr Tymoshenko 
9502b3f6d66SOleksandr Tymoshenko 	EVDEV_CLIENT_UNLOCKQ(client);
9512b3f6d66SOleksandr Tymoshenko }
952