xref: /dragonfly/sys/dev/misc/evdev/cdev.c (revision 60e242c5)
1 /*-
2  * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3  * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include "opt_evdev.h"
31 
32 #include <sys/types.h>
33 #include <sys/module.h>
34 #include <sys/devfs.h>
35 
36 #include <sys/param.h>
37 #include <sys/caps.h>
38 #include <sys/conf.h>
39 #include <sys/filio.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/poll.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/time.h>
47 #include <sys/vnode.h> /* IO_NDELAY in read() */
48 #include <sys/uio.h>
49 
50 #include <sys/errno.h>
51 
52 #include <sys/device.h>
53 #include <sys/bus.h>
54 
55 /* Use FreeBSD bitstring locally. */
56 #include "freebsd-bitstring.h"
57 
58 #include <dev/misc/evdev/evdev.h>
59 #include <dev/misc/evdev/evdev_private.h>
60 #include <dev/misc/evdev/input.h>
61 
62 #ifdef EVDEV_DEBUG
63 #define	debugf(client, fmt, args...)	kprintf("evdev cdev: "fmt"\n", ##args)
64 #else
65 #define	debugf(client, fmt, args...)
66 #endif
67 
68 #define	DEF_RING_REPORTS	8
69 
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_kqfilter_t	evdev_kqfilter;
75 static d_priv_dtor_t	evdev_dtor;
76 
77 static int evdev_kqread(struct knote *kn, long hint);
78 static void evdev_kqdetach(struct knote *kn);
79 static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t);
80 static void evdev_client_filter_queue(struct evdev_client *, uint16_t);
81 
82 static struct dev_ops evdev_cdevsw = {
83 	{ "evdev", 0, 0 },
84 	.d_open = evdev_open,
85 	.d_read = evdev_read,
86 	.d_write = evdev_write,
87 	.d_ioctl = evdev_ioctl,
88 	.d_kqfilter = evdev_kqfilter,
89 };
90 
91 static struct filterops evdev_cdev_filterops = {
92 	.f_flags = FILTEROP_ISFD,
93 	.f_attach = NULL,
94 	.f_detach = evdev_kqdetach,
95 	.f_event = evdev_kqread,
96 };
97 
98 static int
99 evdev_open(struct dev_open_args *ap)
100 {
101 	cdev_t dev = ap->a_head.a_dev;
102 	struct evdev_dev *evdev = dev->si_drv1;
103 	struct evdev_client *client;
104 	size_t buffer_size;
105 	int ret;
106 
107 	/*
108 	 * Disallow access to disk volumes if RESTRICTEDROOT
109 	 */
110 	if (caps_priv_check_self(SYSCAP_RESTRICTEDROOT))
111 		return (EPERM);
112 
113 	if (evdev == NULL)
114 		return (ENODEV);
115 
116 	/* Initialize client structure */
117 	buffer_size = evdev->ev_report_size * DEF_RING_REPORTS;
118 	client = kmalloc(offsetof(struct evdev_client, ec_buffer) +
119 	    sizeof(struct input_event) * buffer_size,
120 	    M_EVDEV, M_WAITOK | M_ZERO);
121 
122 	/* Initialize ring buffer */
123 	client->ec_buffer_size = buffer_size;
124 	client->ec_buffer_head = 0;
125 	client->ec_buffer_tail = 0;
126 	client->ec_buffer_ready = 0;
127 
128 	client->ec_evdev = evdev;
129 	lockinit(&client->ec_buffer_mtx, "evclient", 0, LK_CANRECURSE);
130 
131 	/* Avoid race with evdev_unregister */
132 	EVDEV_LOCK(evdev);
133 	if (dev->si_drv1 == NULL)
134 		ret = ENODEV;
135 	else
136 		ret = evdev_register_client(evdev, client);
137 
138 	if (ret != 0)
139 		evdev_revoke_client(client);
140 	/*
141 	 * Unlock evdev here because non-sleepable lock held
142 	 * while calling devfs_set_cdevpriv upsets WITNESS
143 	 */
144 	EVDEV_UNLOCK(evdev);
145 
146 	if (ret == 0) {
147 		struct file *fp;
148 
149 		fp = (ap->a_fpp) ? *ap->a_fpp : NULL;
150 		ret = devfs_set_cdevpriv(fp, client, &evdev_dtor);
151 	}
152 
153 	if (ret != 0) {
154 		debugf(client, "cannot register evdev client");
155 	}
156 
157 	return (ret);
158 }
159 
160 static void
161 evdev_dtor(void *data)
162 {
163 	struct evdev_client *client = (struct evdev_client *)data;
164 
165 	EVDEV_LOCK(client->ec_evdev);
166 	if (!client->ec_revoked)
167 		evdev_dispose_client(client->ec_evdev, client);
168 	EVDEV_UNLOCK(client->ec_evdev);
169 
170 	funsetown(&client->ec_sigio);
171 	lockuninit(&client->ec_buffer_mtx);
172 	kfree(client, M_EVDEV);
173 }
174 
175 static int
176 evdev_read(struct dev_read_args *ap)
177 {
178 	struct uio *uio = ap->a_uio;
179 	int ioflag = ap->a_ioflag;
180 	struct evdev_client *client;
181 	struct input_event event;
182 	int ret = 0;
183 	int remaining;
184 
185 	ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client);
186 	if (ret != 0)
187 		return (ret);
188 
189 	debugf(client, "read %zd bytes by thread %d", uio->uio_resid, 0);
190 
191 	if (client->ec_revoked)
192 		return (ENODEV);
193 
194 	/* Zero-sized reads are allowed for error checking */
195 	if (uio->uio_resid != 0 && uio->uio_resid < sizeof(struct input_event))
196 		return (EINVAL);
197 
198 	remaining = uio->uio_resid / sizeof(struct input_event);
199 
200 	EVDEV_CLIENT_LOCKQ(client);
201 
202 	if (EVDEV_CLIENT_EMPTYQ(client)) {
203 		if (ioflag & IO_NDELAY) {
204 			ret = EWOULDBLOCK;
205 		} else {
206 			if (remaining != 0) {
207 				client->ec_blocked = true;
208 				ret = lksleep(client, &client->ec_buffer_mtx,
209 				    PCATCH, "evread", 0);
210 			}
211 		}
212 	}
213 
214 	while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) {
215 		memcpy(&event, &client->ec_buffer[client->ec_buffer_head],
216 		    sizeof(struct input_event));
217 		client->ec_buffer_head =
218 		    (client->ec_buffer_head + 1) % client->ec_buffer_size;
219 		remaining--;
220 
221 		EVDEV_CLIENT_UNLOCKQ(client);
222 		ret = uiomove((void *)&event, sizeof(struct input_event), uio);
223 		EVDEV_CLIENT_LOCKQ(client);
224 	}
225 
226 	EVDEV_CLIENT_UNLOCKQ(client);
227 
228 	return (ret);
229 }
230 
231 static int
232 evdev_write(struct dev_write_args *ap)
233 {
234 	cdev_t dev = ap->a_head.a_dev;
235 	struct uio *uio = ap->a_uio;
236 	struct evdev_dev *evdev = dev->si_drv1;
237 	struct evdev_client *client;
238 	struct input_event event;
239 	int ret = 0;
240 
241 	ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client);
242 	if (ret != 0)
243 		return (ret);
244 
245 	debugf(client, "write %zd bytes by thread %d", uio->uio_resid, 0);
246 
247 	if (client->ec_revoked || evdev == NULL)
248 		return (ENODEV);
249 
250 	if (uio->uio_resid % sizeof(struct input_event) != 0) {
251 		debugf(client, "write size not multiple of input_event size");
252 		return (EINVAL);
253 	}
254 
255 	while (uio->uio_resid > 0 && ret == 0) {
256 		ret = uiomove((void *)&event, sizeof(struct input_event), uio);
257 		if (ret == 0)
258 			ret = evdev_inject_event(evdev, event.type, event.code,
259 			    event.value);
260 	}
261 
262 	return (ret);
263 }
264 
265 static int
266 evdev_kqfilter(struct dev_kqfilter_args *ap)
267 {
268 	struct knote *kn = ap->a_kn;
269 	struct klist *klist;
270 	struct evdev_client *client;
271 	int ret;
272 
273 	ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client);
274 	if (ret != 0)
275 		return (ret);
276 
277 	if (client->ec_revoked)
278 		return (ENODEV);
279 
280 	switch(kn->kn_filter) {
281 	case EVFILT_READ:
282 		kn->kn_fop = &evdev_cdev_filterops;
283 		break;
284 	default:
285 		return(EINVAL);
286 	}
287 	kn->kn_hook = (caddr_t)client;
288 
289 	klist = &client->kqinfo.ki_note;
290 	knote_insert(klist, kn);
291 	return (0);
292 }
293 
294 static int
295 evdev_kqread(struct knote *kn, long hint)
296 {
297 	struct evdev_client *client;
298 	int ret;
299 	int locked = 0;
300 
301 	client = (struct evdev_client *)kn->kn_hook;
302 
303 	/* NOTE on DragonFly v FreeBSD.
304 	 * FreeBSD locks the klist when calling f_event, i.e. evdev_kqread().
305 	 * That's why the plain assertion EVDEV_CLIENT_LOCKQ_ASSERT(client)
306 	 * fails on DragonFly: DragonFly does not ensure the lock associated
307 	 * with the klist is locked.
308 	 * To mimic FreeBSD's behavior, we will lock ec_buffer_mtx if
309 	 * it was not locked, and unlock when leaving.
310 	 */
311 	locked = lockowned(&(client)->ec_buffer_mtx);
312 	if (!locked)
313 		EVDEV_CLIENT_LOCKQ(client);
314 
315 	EVDEV_CLIENT_LOCKQ_ASSERT(client);
316 
317 	if (client->ec_revoked) {
318 		kn->kn_flags |= EV_EOF;
319 		ret = 1;
320 	} else {
321 		kn->kn_data = EVDEV_CLIENT_SIZEQ(client) *
322 		    sizeof(struct input_event);
323 		ret = !EVDEV_CLIENT_EMPTYQ(client);
324 	}
325 
326 	/* Unlock if ec_buffer_mtx was not locked. */
327 	if (!locked) {
328 		EVDEV_CLIENT_UNLOCKQ(client);
329 	}
330 
331 	return (ret);
332 }
333 
334 static void
335 evdev_kqdetach(struct knote *kn)
336 {
337 	struct evdev_client *client;
338 
339 	client = (struct evdev_client *)kn->kn_hook;
340 	knote_remove(&client->kqinfo.ki_note, kn);
341 }
342 
343 static int
344 evdev_ioctl(struct dev_ioctl_args *ap)
345 {
346 	cdev_t dev = ap->a_head.a_dev;
347 	u_long cmd = ap->a_cmd;
348 	caddr_t data = ap->a_data;
349 	struct evdev_dev *evdev = dev->si_drv1;
350 	struct evdev_client *client;
351 	struct input_keymap_entry *ke;
352 	int ret, len, limit, type_num;
353 	uint32_t code;
354 	size_t nvalues;
355 
356 	ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client);
357 	if (ret != 0)
358 		return (ret);
359 
360 	if (client->ec_revoked || evdev == NULL)
361 		return (ENODEV);
362 
363 	/* file I/O ioctl handling */
364 	switch (cmd) {
365 	case FIOSETOWN:
366 		return (fsetown(*(int *)data, &client->ec_sigio));
367 
368 	case FIOGETOWN:
369 		*(int *)data = fgetown(&client->ec_sigio);
370 		return (0);
371 
372 	case FIONBIO:
373 		return (0);
374 
375 	case FIOASYNC:
376 		if (*(int *)data)
377 			client->ec_async = true;
378 		else
379 			client->ec_async = false;
380 
381 		return (0);
382 
383 	case FIONREAD:
384 		EVDEV_CLIENT_LOCKQ(client);
385 		*(int *)data =
386 		    EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event);
387 		EVDEV_CLIENT_UNLOCKQ(client);
388 		return (0);
389 	}
390 
391 	len = IOCPARM_LEN(cmd);
392 	debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data);
393 
394 	/* evdev fixed-length ioctls handling */
395 	switch (cmd) {
396 	case EVIOCGVERSION:
397 		*(int *)data = EV_VERSION;
398 		return (0);
399 
400 	case EVIOCGID:
401 		debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x",
402 		    evdev->ev_id.bustype, evdev->ev_id.vendor,
403 		    evdev->ev_id.product);
404 		memcpy(data, &evdev->ev_id, sizeof(struct input_id));
405 		return (0);
406 
407 	case EVIOCGREP:
408 		if (!evdev_event_supported(evdev, EV_REP))
409 			return (ENOTSUP);
410 
411 		memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep));
412 		return (0);
413 
414 	case EVIOCSREP:
415 		if (!evdev_event_supported(evdev, EV_REP))
416 			return (ENOTSUP);
417 
418 		evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]);
419 		evdev_inject_event(evdev, EV_REP, REP_PERIOD,
420 		    ((int *)data)[1]);
421 		return (0);
422 
423 	case EVIOCGKEYCODE:
424 		/* Fake unsupported ioctl */
425 		return (0);
426 
427 	case EVIOCGKEYCODE_V2:
428 		if (evdev->ev_methods == NULL ||
429 		    evdev->ev_methods->ev_get_keycode == NULL)
430 			return (ENOTSUP);
431 
432 		ke = (struct input_keymap_entry *)data;
433 		evdev->ev_methods->ev_get_keycode(evdev, evdev->ev_softc, ke);
434 		return (0);
435 
436 	case EVIOCSKEYCODE:
437 		/* Fake unsupported ioctl */
438 		return (0);
439 
440 	case EVIOCSKEYCODE_V2:
441 		if (evdev->ev_methods == NULL ||
442 		    evdev->ev_methods->ev_set_keycode == NULL)
443 			return (ENOTSUP);
444 
445 		ke = (struct input_keymap_entry *)data;
446 		evdev->ev_methods->ev_set_keycode(evdev, evdev->ev_softc, ke);
447 		return (0);
448 
449 	case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX):
450 		if (evdev->ev_absinfo == NULL)
451 			return (EINVAL);
452 
453 		memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)],
454 		    sizeof(struct input_absinfo));
455 		return (0);
456 
457 	case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX):
458 		if (evdev->ev_absinfo == NULL)
459 			return (EINVAL);
460 
461 		code = cmd - EVIOCSABS(0);
462 		/* mt-slot number can not be changed */
463 		if (code == ABS_MT_SLOT)
464 			return (EINVAL);
465 
466 		EVDEV_LOCK(evdev);
467 		evdev_set_absinfo(evdev, code, (struct input_absinfo *)data);
468 		EVDEV_UNLOCK(evdev);
469 		return (0);
470 
471 	case EVIOCSFF:
472 	case EVIOCRMFF:
473 	case EVIOCGEFFECTS:
474 		/* Fake unsupported ioctls */
475 		return (0);
476 
477 	case EVIOCGRAB:
478 		EVDEV_LOCK(evdev);
479 		if (*(int *)data)
480 			ret = evdev_grab_client(evdev, client);
481 		else
482 			ret = evdev_release_client(evdev, client);
483 		EVDEV_UNLOCK(evdev);
484 		return (ret);
485 
486 	case EVIOCREVOKE:
487 		if (*(int *)data != 0)
488 			return (EINVAL);
489 
490 		EVDEV_LOCK(evdev);
491 		if (dev->si_drv1 != NULL && !client->ec_revoked) {
492 			evdev_dispose_client(evdev, client);
493 			evdev_revoke_client(client);
494 		}
495 		EVDEV_UNLOCK(evdev);
496 		return (0);
497 
498 	case EVIOCSCLOCKID:
499 		switch (*(int *)data) {
500 		case CLOCK_REALTIME:
501 			client->ec_clock_id = EV_CLOCK_REALTIME;
502 			return (0);
503 		case CLOCK_MONOTONIC:
504 			client->ec_clock_id = EV_CLOCK_MONOTONIC;
505 			return (0);
506 		default:
507 			return (EINVAL);
508 		}
509 	}
510 
511 	/* evdev variable-length ioctls handling */
512 	switch (IOCBASECMD(cmd)) {
513 	case EVIOCGNAME(0):
514 		strlcpy(data, evdev->ev_name, len);
515 		return (0);
516 
517 	case EVIOCGPHYS(0):
518 		if (evdev->ev_shortname[0] == 0)
519 			return (ENOENT);
520 
521 		strlcpy(data, evdev->ev_shortname, len);
522 		return (0);
523 
524 	case EVIOCGUNIQ(0):
525 		if (evdev->ev_serial[0] == 0)
526 			return (ENOENT);
527 
528 		strlcpy(data, evdev->ev_serial, len);
529 		return (0);
530 
531 	case EVIOCGPROP(0):
532 		limit = MIN(len, bitstr_size(INPUT_PROP_CNT));
533 		memcpy(data, evdev->ev_prop_flags, limit);
534 		return (0);
535 
536 	case EVIOCGMTSLOTS(0):
537 		if (evdev->ev_mt == NULL)
538 			return (EINVAL);
539 		if (len < sizeof(uint32_t))
540 			return (EINVAL);
541 		code = *(uint32_t *)data;
542 		if (!ABS_IS_MT(code))
543 			return (EINVAL);
544 
545 		nvalues =
546 		    MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1);
547 		for (int i = 0; i < nvalues; i++)
548 			((int32_t *)data)[i + 1] =
549 			    evdev_get_mt_value(evdev, i, code);
550 		return (0);
551 
552 	case EVIOCGKEY(0):
553 		limit = MIN(len, bitstr_size(KEY_CNT));
554 		EVDEV_LOCK(evdev);
555 		evdev_client_filter_queue(client, EV_KEY);
556 		memcpy(data, evdev->ev_key_states, limit);
557 		EVDEV_UNLOCK(evdev);
558 		return (0);
559 
560 	case EVIOCGLED(0):
561 		limit = MIN(len, bitstr_size(LED_CNT));
562 		EVDEV_LOCK(evdev);
563 		evdev_client_filter_queue(client, EV_LED);
564 		memcpy(data, evdev->ev_led_states, limit);
565 		EVDEV_UNLOCK(evdev);
566 		return (0);
567 
568 	case EVIOCGSND(0):
569 		limit = MIN(len, bitstr_size(SND_CNT));
570 		EVDEV_LOCK(evdev);
571 		evdev_client_filter_queue(client, EV_SND);
572 		memcpy(data, evdev->ev_snd_states, limit);
573 		EVDEV_UNLOCK(evdev);
574 		return (0);
575 
576 	case EVIOCGSW(0):
577 		limit = MIN(len, bitstr_size(SW_CNT));
578 		EVDEV_LOCK(evdev);
579 		evdev_client_filter_queue(client, EV_SW);
580 		memcpy(data, evdev->ev_sw_states, limit);
581 		EVDEV_UNLOCK(evdev);
582 		return (0);
583 
584 	case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0):
585 		type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0);
586 		debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num,
587 		    data, len);
588 		return (evdev_ioctl_eviocgbit(evdev, type_num, len, data));
589 	}
590 
591 	return (EINVAL);
592 }
593 
594 static int
595 evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data)
596 {
597 	/*
598 	 * We will use freebsd-bitstring.h locally. This ensures bitmap
599 	 * is of type (unsigned long *). DragonFly's original bitmap
600 	 * is (unsigned char *).
601 	 */
602 	unsigned long *bitmap;
603 	int limit;
604 
605 	switch (type) {
606 	case 0:
607 		bitmap = evdev->ev_type_flags;
608 		limit = EV_CNT;
609 		break;
610 	case EV_KEY:
611 		bitmap = evdev->ev_key_flags;
612 		limit = KEY_CNT;
613 		break;
614 	case EV_REL:
615 		bitmap = evdev->ev_rel_flags;
616 		limit = REL_CNT;
617 		break;
618 	case EV_ABS:
619 		bitmap = evdev->ev_abs_flags;
620 		limit = ABS_CNT;
621 		break;
622 	case EV_MSC:
623 		bitmap = evdev->ev_msc_flags;
624 		limit = MSC_CNT;
625 		break;
626 	case EV_LED:
627 		bitmap = evdev->ev_led_flags;
628 		limit = LED_CNT;
629 		break;
630 	case EV_SND:
631 		bitmap = evdev->ev_snd_flags;
632 		limit = SND_CNT;
633 		break;
634 	case EV_SW:
635 		bitmap = evdev->ev_sw_flags;
636 		limit = SW_CNT;
637 		break;
638 	case EV_FF:
639 		/*
640 		 * We don't support EV_FF now, so let's
641 		 * just fake it returning only zeros.
642 		 */
643 		bzero(data, len);
644 		return (0);
645 	default:
646 		return (ENOTTY);
647 	}
648 
649 	/*
650 	 * Clear ioctl data buffer in case it's bigger than
651 	 * bitmap size
652 	 */
653 	bzero(data, len);
654 
655 	limit = bitstr_size(limit);
656 	len = MIN(limit, len);
657 	memcpy(data, bitmap, len);
658 	return (0);
659 }
660 
661 void
662 evdev_revoke_client(struct evdev_client *client)
663 {
664 
665 	EVDEV_LOCK_ASSERT(client->ec_evdev);
666 
667 	client->ec_revoked = true;
668 }
669 
670 void
671 evdev_notify_event(struct evdev_client *client)
672 {
673 
674 	EVDEV_CLIENT_LOCKQ_ASSERT(client);
675 
676 	if (client->ec_blocked) {
677 		client->ec_blocked = false;
678 		wakeup(client);
679 	}
680 	if (client->ec_selected) {
681 		client->ec_selected = false;
682 		wakeup(&client->kqinfo);
683 	}
684 
685 	KNOTE(&client->kqinfo.ki_note, 0);
686 
687 	if (client->ec_async && client->ec_sigio != NULL)
688 		pgsigio(client->ec_sigio, SIGIO, 0);
689 }
690 
691 int
692 evdev_cdev_create(struct evdev_dev *evdev)
693 {
694 	cdev_t dev;
695 	int ret, unit;
696 
697 	/*
698 	 * Iterate over devices input/eventX until we find a non-existing
699 	 * one and record its number in unit.
700 	 */
701 	unit = 0;
702 	while (devfs_find_device_by_name("input/event%d", unit) != NULL) {
703 	    unit++;
704 	}
705 
706 	/*
707 	 * Put unit as minor. Minor and major will determine st_rdev of
708 	 * eventX. Ensuring that all eventX have different major and minor
709 	 * will make st_rdev unique. This is needed by libinput, which
710 	 * determines eventX from st_rdev.
711 	 */
712 	dev = make_dev(&evdev_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600,
713 	    "input/event%d", unit);
714 
715 	if (dev != NULL) {
716 		dev->si_drv1 = evdev;
717 		evdev->ev_cdev = dev;
718 		evdev->ev_unit = unit;
719 		ret = 0;
720 	} else {
721 		ret = ENODEV;
722 		goto err;
723 	}
724 
725 	reference_dev(evdev->ev_cdev);
726 
727 err:
728 	return (ret);
729 }
730 
731 int
732 evdev_cdev_destroy(struct evdev_dev *evdev)
733 {
734 
735 	if (evdev->ev_cdev) {
736 		dev_ops_remove_minor(&evdev_cdevsw, evdev->ev_unit);
737 	}
738 
739 	return (0);
740 }
741 
742 static void
743 evdev_client_gettime(struct evdev_client *client, struct timeval *tv)
744 {
745 
746 	switch (client->ec_clock_id) {
747 	case EV_CLOCK_BOOTTIME:
748 		/*
749 		 * XXX: FreeBSD does not support true POSIX monotonic clock.
750 		 *      So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC.
751 		 */
752 	case EV_CLOCK_MONOTONIC:
753 		microuptime(tv);
754 		break;
755 
756 	case EV_CLOCK_REALTIME:
757 	default:
758 		microtime(tv);
759 		break;
760 	}
761 }
762 
763 void
764 evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code,
765     int32_t value)
766 {
767 	struct timeval time;
768 	size_t count, head, tail, ready;
769 
770 	EVDEV_CLIENT_LOCKQ_ASSERT(client);
771 	head = client->ec_buffer_head;
772 	tail = client->ec_buffer_tail;
773 	ready = client->ec_buffer_ready;
774 	count = client->ec_buffer_size;
775 
776 	/* If queue is full drop its content and place SYN_DROPPED event */
777 	if ((tail + 1) % count == head) {
778 		debugf(client, "client %p: buffer overflow", client);
779 
780 		head = (tail + count - 1) % count;
781 		client->ec_buffer[head] = (struct input_event) {
782 			.type = EV_SYN,
783 			.code = SYN_DROPPED,
784 			.value = 0
785 		};
786 		/*
787 		 * XXX: Here is a small race window from now till the end of
788 		 *      report. The queue is empty but client has been already
789 		 *      notified of data readyness. Can be fixed in two ways:
790 		 * 1. Implement bulk insert so queue lock would not be dropped
791 		 *    till the SYN_REPORT event.
792 		 * 2. Insert SYN_REPORT just now and skip remaining events
793 		 */
794 		client->ec_buffer_head = head;
795 		client->ec_buffer_ready = head;
796 	}
797 
798 	client->ec_buffer[tail].type = type;
799 	client->ec_buffer[tail].code = code;
800 	client->ec_buffer[tail].value = value;
801 	client->ec_buffer_tail = (tail + 1) % count;
802 
803 	/* Allow users to read events only after report has been completed */
804 	if (type == EV_SYN && code == SYN_REPORT) {
805 		evdev_client_gettime(client, &time);
806 		for (; ready != client->ec_buffer_tail;
807 		    ready = (ready + 1) % count)
808 			client->ec_buffer[ready].time = time;
809 		client->ec_buffer_ready = client->ec_buffer_tail;
810 	}
811 }
812 
813 void
814 evdev_client_dumpqueue(struct evdev_client *client)
815 {
816 	struct input_event *event;
817 	size_t i, head, tail, ready, size;
818 
819 	head = client->ec_buffer_head;
820 	tail = client->ec_buffer_tail;
821 	ready = client->ec_buffer_ready;
822 	size = client->ec_buffer_size;
823 
824 	kprintf("evdev client: %p\n", client);
825 	kprintf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n",
826 	    head, ready, tail, size);
827 
828 	kprintf("queue contents:\n");
829 
830 	for (i = 0; i < size; i++) {
831 		event = &client->ec_buffer[i];
832 		kprintf("%zu: ", i);
833 
834 		if (i < head || i > tail)
835 			kprintf("unused\n");
836 		else
837 			kprintf("type=%d code=%d value=%d ", event->type,
838 			    event->code, event->value);
839 
840 		if (i == head)
841 			kprintf("<- head\n");
842 		else if (i == tail)
843 			kprintf("<- tail\n");
844 		else if (i == ready)
845 			kprintf("<- ready\n");
846 		else
847 			kprintf("\n");
848 	}
849 }
850 
851 static void
852 evdev_client_filter_queue(struct evdev_client *client, uint16_t type)
853 {
854 	struct input_event *event;
855 	size_t head, tail, count, i;
856 	bool last_was_syn = false;
857 
858 	EVDEV_CLIENT_LOCKQ(client);
859 
860 	i = head = client->ec_buffer_head;
861 	tail = client->ec_buffer_tail;
862 	count = client->ec_buffer_size;
863 	client->ec_buffer_ready = client->ec_buffer_tail;
864 
865 	while (i != client->ec_buffer_tail) {
866 		event = &client->ec_buffer[i];
867 		i = (i + 1) % count;
868 
869 		/* Skip event of given type */
870 		if (event->type == type)
871 			continue;
872 
873 		/* Remove empty SYN_REPORT events */
874 		if (event->type == EV_SYN && event->code == SYN_REPORT) {
875 			if (last_was_syn)
876 				continue;
877 			else
878 				client->ec_buffer_ready = (tail + 1) % count;
879 		}
880 
881 		/* Rewrite entry */
882 		memcpy(&client->ec_buffer[tail], event,
883 		    sizeof(struct input_event));
884 
885 		last_was_syn = (event->type == EV_SYN &&
886 		    event->code == SYN_REPORT);
887 
888 		tail = (tail + 1) % count;
889 	}
890 
891 	client->ec_buffer_head = i;
892 	client->ec_buffer_tail = tail;
893 
894 	EVDEV_CLIENT_UNLOCKQ(client);
895 }
896