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