xref: /freebsd/sys/dev/evdev/evdev.c (revision 97549c34ecaf74580941fdc9c5bd1050e4b1f6ce)
1 /*-
2  * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3  * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@cicgroup.ru>
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/systm.h>
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/conf.h>
37 #include <sys/malloc.h>
38 #include <sys/bitstring.h>
39 #include <sys/sysctl.h>
40 
41 #include <dev/evdev/input.h>
42 #include <dev/evdev/evdev.h>
43 #include <dev/evdev/evdev_private.h>
44 
45 #ifdef EVDEV_DEBUG
46 #define	debugf(evdev, fmt, args...)	printf("evdev: " fmt "\n", ##args)
47 #else
48 #define	debugf(evdev, fmt, args...)
49 #endif
50 
51 #ifdef FEATURE
52 FEATURE(evdev, "Input event devices support");
53 #endif
54 
55 enum evdev_sparse_result
56 {
57 	EV_SKIP_EVENT,		/* Event value not changed */
58 	EV_REPORT_EVENT,	/* Event value changed */
59 	EV_REPORT_MT_SLOT,	/* Event value and MT slot number changed */
60 };
61 
62 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
63 
64 int evdev_rcpt_mask = EVDEV_RCPT_SYSMOUSE | EVDEV_RCPT_KBDMUX;
65 
66 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args");
67 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RW, &evdev_rcpt_mask, 0,
68     "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
69     "bit2 - mouse hardware, bit3 - keyboard hardware");
70 
71 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
72 static void evdev_stop_repeat(struct evdev_dev *);
73 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
74 
75 static inline void
76 bit_change(bitstr_t *bitstr, int bit, int value)
77 {
78 	if (value)
79 		bit_set(bitstr, bit);
80 	else
81 		bit_clear(bitstr, bit);
82 }
83 
84 struct evdev_dev *
85 evdev_alloc(void)
86 {
87 
88 	return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
89 }
90 
91 void
92 evdev_free(struct evdev_dev *evdev)
93 {
94 
95 	if (evdev != NULL && evdev->ev_cdev != NULL &&
96 	    evdev->ev_cdev->si_drv1 != NULL)
97 		evdev_unregister(evdev);
98 
99 	free(evdev, M_EVDEV);
100 }
101 
102 static struct input_absinfo *
103 evdev_alloc_absinfo(void)
104 {
105 
106 	return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
107 	    M_WAITOK | M_ZERO));
108 }
109 
110 static void
111 evdev_free_absinfo(struct input_absinfo *absinfo)
112 {
113 
114 	free(absinfo, M_EVDEV);
115 }
116 
117 int
118 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
119 {
120 	if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
121 	    MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
122 		return (EINVAL);
123 
124 	evdev->ev_report_size = report_size;
125 	return (0);
126 }
127 
128 static size_t
129 evdev_estimate_report_size(struct evdev_dev *evdev)
130 {
131 	size_t size = 0;
132 	int res;
133 
134 	/*
135 	 * Keyboards generate one event per report but other devices with
136 	 * buttons like mouses can report events simultaneously
137 	 */
138 	bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
139 	if (res == -1)
140 		bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
141 	size += (res != -1);
142 	bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
143 	size += res;
144 
145 	/* All relative axes can be reported simultaneously */
146 	bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
147 	size += res;
148 
149 	/*
150 	 * All absolute axes can be reported simultaneously.
151 	 * Multitouch axes can be reported ABS_MT_SLOT times
152 	 */
153 	if (evdev->ev_absinfo != NULL) {
154 		bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
155 		size += res;
156 		bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
157 		if (res > 0) {
158 			res++;	/* ABS_MT_SLOT or SYN_MT_REPORT */
159 			if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
160 				/* MT type B */
161 				size += res * MAXIMAL_MT_SLOT(evdev);
162 			else
163 				/* MT type A */
164 				size += res * (MAX_MT_REPORTS - 1);
165 		}
166 	}
167 
168 	/* All misc events can be reported simultaneously */
169 	bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
170 	size += res;
171 
172 	/* All leds can be reported simultaneously */
173 	bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
174 	size += res;
175 
176 	/* Assume other events are generated once per report */
177 	bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
178 	size += (res != -1);
179 
180 	bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
181 	size += (res != -1);
182 
183 	/* XXX: FF part is not implemented yet */
184 
185 	size++;		/* SYN_REPORT */
186 	return (size);
187 }
188 
189 int
190 evdev_register(struct evdev_dev *evdev)
191 {
192 	int ret;
193 
194 	debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
195 	    evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
196 
197 	/* Initialize internal structures */
198 	mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
199 	LIST_INIT(&evdev->ev_clients);
200 
201 	if (evdev_event_supported(evdev, EV_REP) &&
202 	    bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
203 		/* Initialize callout */
204 		callout_init_mtx(&evdev->ev_rep_callout, &evdev->ev_mtx, 0);
205 
206 		if (evdev->ev_rep[REP_DELAY] == 0 &&
207 		    evdev->ev_rep[REP_PERIOD] == 0) {
208 			/* Supply default values */
209 			evdev->ev_rep[REP_DELAY] = 250;
210 			evdev->ev_rep[REP_PERIOD] = 33;
211 		}
212 	}
213 
214 	/* Initialize multitouch protocol type B states */
215 	if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
216 	    evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
217 		evdev_mt_init(evdev);
218 
219 	/* Estimate maximum report size */
220 	if (evdev->ev_report_size == 0) {
221 		ret = evdev_set_report_size(evdev,
222 		    evdev_estimate_report_size(evdev));
223 		if (ret != 0)
224 			goto bail_out;
225 	}
226 
227 	/* Create char device node */
228 	ret = evdev_cdev_create(evdev);
229 bail_out:
230 	if (ret != 0)
231 		mtx_destroy(&evdev->ev_mtx);
232 
233 	return (ret);
234 }
235 
236 int
237 evdev_unregister(struct evdev_dev *evdev)
238 {
239 	struct evdev_client *client;
240 	int ret;
241 	debugf(evdev, "%s: unregistered evdev provider: %s\n",
242 	    evdev->ev_shortname, evdev->ev_name);
243 
244 	EVDEV_LOCK(evdev);
245 	evdev->ev_cdev->si_drv1 = NULL;
246 	/* Wake up sleepers */
247 	LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
248 		evdev_revoke_client(client);
249 		evdev_dispose_client(evdev, client);
250 		EVDEV_CLIENT_LOCKQ(client);
251 		evdev_notify_event(client);
252 		EVDEV_CLIENT_UNLOCKQ(client);
253 	}
254 	EVDEV_UNLOCK(evdev);
255 
256 	/* destroy_dev can sleep so release lock */
257 	ret = evdev_cdev_destroy(evdev);
258 	evdev->ev_cdev = NULL;
259 	if (ret == 0)
260 		mtx_destroy(&evdev->ev_mtx);
261 
262 	evdev_free_absinfo(evdev->ev_absinfo);
263 	evdev_mt_free(evdev);
264 
265 	return (ret);
266 }
267 
268 inline void
269 evdev_set_name(struct evdev_dev *evdev, const char *name)
270 {
271 
272 	snprintf(evdev->ev_name, NAMELEN, "%s", name);
273 }
274 
275 inline void
276 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
277     uint16_t product, uint16_t version)
278 {
279 
280 	evdev->ev_id = (struct input_id) {
281 		.bustype = bustype,
282 		.vendor = vendor,
283 		.product = product,
284 		.version = version
285 	};
286 }
287 
288 inline void
289 evdev_set_phys(struct evdev_dev *evdev, const char *name)
290 {
291 
292 	snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
293 }
294 
295 inline void
296 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
297 {
298 
299 	snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
300 }
301 
302 inline void
303 evdev_set_methods(struct evdev_dev *evdev, void *softc,
304     struct evdev_methods *methods)
305 {
306 
307 	evdev->ev_methods = methods;
308 	evdev->ev_softc = softc;
309 }
310 
311 inline void
312 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
313 {
314 
315 	KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
316 	bit_set(evdev->ev_prop_flags, prop);
317 }
318 
319 inline void
320 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
321 {
322 
323 	KASSERT(type < EV_CNT, ("invalid evdev event property"));
324 	bit_set(evdev->ev_type_flags, type);
325 }
326 
327 inline void
328 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
329 {
330 
331 	KASSERT(code < KEY_CNT, ("invalid evdev key property"));
332 	bit_set(evdev->ev_key_flags, code);
333 }
334 
335 inline void
336 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
337 {
338 
339 	KASSERT(code < REL_CNT, ("invalid evdev rel property"));
340 	bit_set(evdev->ev_rel_flags, code);
341 }
342 
343 inline void
344 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value,
345     int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat,
346     int32_t resolution)
347 {
348 	struct input_absinfo absinfo;
349 
350 	KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
351 
352 	absinfo = (struct input_absinfo) {
353 		.value = value,
354 		.minimum = minimum,
355 		.maximum = maximum,
356 		.fuzz = fuzz,
357 		.flat = flat,
358 		.resolution = resolution,
359 	};
360 	evdev_set_abs_bit(evdev, code);
361 	evdev_set_absinfo(evdev, code, &absinfo);
362 }
363 
364 inline void
365 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
366 {
367 
368 	KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
369 	if (evdev->ev_absinfo == NULL)
370 		evdev->ev_absinfo = evdev_alloc_absinfo();
371 	bit_set(evdev->ev_abs_flags, code);
372 }
373 
374 inline void
375 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
376 {
377 
378 	KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
379 	bit_set(evdev->ev_msc_flags, code);
380 }
381 
382 
383 inline void
384 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
385 {
386 
387 	KASSERT(code < LED_CNT, ("invalid evdev led property"));
388 	bit_set(evdev->ev_led_flags, code);
389 }
390 
391 inline void
392 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
393 {
394 
395 	KASSERT(code < SND_CNT, ("invalid evdev snd property"));
396 	bit_set(evdev->ev_snd_flags, code);
397 }
398 
399 inline void
400 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
401 {
402 
403 	KASSERT(code < SW_CNT, ("invalid evdev sw property"));
404 	bit_set(evdev->ev_sw_flags, code);
405 }
406 
407 bool
408 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
409 {
410 
411 	KASSERT(type < EV_CNT, ("invalid evdev event property"));
412 	return (bit_test(evdev->ev_type_flags, type));
413 }
414 
415 inline void
416 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
417     struct input_absinfo *absinfo)
418 {
419 
420 	KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
421 
422 	if (axis == ABS_MT_SLOT &&
423 	    (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
424 		return;
425 
426 	if (evdev->ev_absinfo == NULL)
427 		evdev->ev_absinfo = evdev_alloc_absinfo();
428 
429 	if (axis == ABS_MT_SLOT)
430 		evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
431 	else
432 		memcpy(&evdev->ev_absinfo[axis], absinfo,
433 		    sizeof(struct input_absinfo));
434 }
435 
436 inline void
437 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
438 {
439 
440 	KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
441 	evdev->ev_rep[property] = value;
442 }
443 
444 inline void
445 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
446 {
447 
448 	KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
449 	bit_set(evdev->ev_flags, flag);
450 }
451 
452 static int
453 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
454     int32_t value)
455 {
456 
457 	if (type >= EV_CNT)
458 		return (EINVAL);
459 
460 	/* Allow SYN events implicitly */
461 	if (type != EV_SYN && !evdev_event_supported(evdev, type))
462 		return (EINVAL);
463 
464 	switch (type) {
465 	case EV_SYN:
466 		if (code >= SYN_CNT)
467 			return (EINVAL);
468 		break;
469 
470 	case EV_KEY:
471 		if (code >= KEY_CNT)
472 			return (EINVAL);
473 		if (!bit_test(evdev->ev_key_flags, code))
474 			return (EINVAL);
475 		break;
476 
477 	case EV_REL:
478 		if (code >= REL_CNT)
479 			return (EINVAL);
480 		if (!bit_test(evdev->ev_rel_flags, code))
481 			return (EINVAL);
482 		break;
483 
484 	case EV_ABS:
485 		if (code >= ABS_CNT)
486 			return (EINVAL);
487 		if (!bit_test(evdev->ev_abs_flags, code))
488 			return (EINVAL);
489 		if (code == ABS_MT_SLOT &&
490 		    (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
491 			return (EINVAL);
492 		if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
493 		    bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
494 			return (EINVAL);
495 		break;
496 
497 	case EV_MSC:
498 		if (code >= MSC_CNT)
499 			return (EINVAL);
500 		if (!bit_test(evdev->ev_msc_flags, code))
501 			return (EINVAL);
502 		break;
503 
504 	case EV_LED:
505 		if (code >= LED_CNT)
506 			return (EINVAL);
507 		if (!bit_test(evdev->ev_led_flags, code))
508 			return (EINVAL);
509 		break;
510 
511 	case EV_SND:
512 		if (code >= SND_CNT)
513 			return (EINVAL);
514 		if (!bit_test(evdev->ev_snd_flags, code))
515 			return (EINVAL);
516 		break;
517 
518 	case EV_SW:
519 		if (code >= SW_CNT)
520 			return (EINVAL);
521 		if (!bit_test(evdev->ev_sw_flags, code))
522 			return (EINVAL);
523 		break;
524 
525 	case EV_REP:
526 		if (code >= REP_CNT)
527 			return (EINVAL);
528 		break;
529 
530 	default:
531 		return (EINVAL);
532 	}
533 
534 	return (0);
535 }
536 
537 static void
538 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
539     int32_t *value)
540 {
541 
542 	EVDEV_LOCK_ASSERT(evdev);
543 
544 	switch (type) {
545 	case EV_KEY:
546 		if (!evdev_event_supported(evdev, EV_REP))
547 			break;
548 
549 		if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
550 			/* Detect driver key repeats. */
551 			if (bit_test(evdev->ev_key_states, code) &&
552 			    *value == KEY_EVENT_DOWN)
553 				*value = KEY_EVENT_REPEAT;
554 		} else {
555 			/* Start/stop callout for evdev repeats */
556 			if (bit_test(evdev->ev_key_states, code) == !*value) {
557 				if (*value == KEY_EVENT_DOWN)
558 					evdev_start_repeat(evdev, code);
559 				else
560 					evdev_stop_repeat(evdev);
561 			}
562 		}
563 		break;
564 
565 	case EV_ABS:
566 		/* TBD: implement fuzz */
567 		break;
568 	}
569 }
570 
571 static enum evdev_sparse_result
572 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
573     int32_t value)
574 {
575 	int32_t last_mt_slot;
576 
577 	EVDEV_LOCK_ASSERT(evdev);
578 
579 	/*
580 	 * For certain event types, update device state bits
581 	 * and convert level reporting to edge reporting
582 	 */
583 	switch (type) {
584 	case EV_KEY:
585 		switch (value) {
586 		case KEY_EVENT_UP:
587 		case KEY_EVENT_DOWN:
588 			if (bit_test(evdev->ev_key_states, code) == value)
589 				return (EV_SKIP_EVENT);
590 			bit_change(evdev->ev_key_states, code, value);
591 			break;
592 
593 		case KEY_EVENT_REPEAT:
594 			if (bit_test(evdev->ev_key_states, code) == 0 ||
595 			    !evdev_event_supported(evdev, EV_REP))
596 				return (EV_SKIP_EVENT);
597 			break;
598 
599 		default:
600 			 return (EV_SKIP_EVENT);
601 		}
602 		break;
603 
604 	case EV_LED:
605 		if (bit_test(evdev->ev_led_states, code) == value)
606 			return (EV_SKIP_EVENT);
607 		bit_change(evdev->ev_led_states, code, value);
608 		break;
609 
610 	case EV_SND:
611 		if (bit_test(evdev->ev_snd_states, code) == value)
612 			return (EV_SKIP_EVENT);
613 		bit_change(evdev->ev_snd_states, code, value);
614 		break;
615 
616 	case EV_SW:
617 		if (bit_test(evdev->ev_sw_states, code) == value)
618 			return (EV_SKIP_EVENT);
619 		bit_change(evdev->ev_sw_states, code, value);
620 		break;
621 
622 	case EV_REP:
623 		if (evdev->ev_rep[code] == value)
624 			return (EV_SKIP_EVENT);
625 		evdev_set_repeat_params(evdev, code, value);
626 		break;
627 
628 	case EV_REL:
629 		if (value == 0)
630 			return (EV_SKIP_EVENT);
631 		break;
632 
633 	/* For EV_ABS, save last value in absinfo and ev_mt_states */
634 	case EV_ABS:
635 		switch (code) {
636 		case ABS_MT_SLOT:
637 			/* Postpone ABS_MT_SLOT till next event */
638 			evdev_set_last_mt_slot(evdev, value);
639 			return (EV_SKIP_EVENT);
640 
641 		case ABS_MT_FIRST ... ABS_MT_LAST:
642 			/* Pass MT protocol type A events as is */
643 			if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
644 				break;
645 			/* Don`t repeat MT protocol type B events */
646 			last_mt_slot = evdev_get_last_mt_slot(evdev);
647 			if (evdev_get_mt_value(evdev, last_mt_slot, code)
648 			     == value)
649 				return (EV_SKIP_EVENT);
650 			evdev_set_mt_value(evdev, last_mt_slot, code, value);
651 			if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
652 				CURRENT_MT_SLOT(evdev) = last_mt_slot;
653 				evdev->ev_report_opened = true;
654 				return (EV_REPORT_MT_SLOT);
655 			}
656 			break;
657 
658 		default:
659 			if (evdev->ev_absinfo[code].value == value)
660 				return (EV_SKIP_EVENT);
661 			evdev->ev_absinfo[code].value = value;
662 		}
663 		break;
664 
665 	case EV_SYN:
666 		if (code == SYN_REPORT) {
667 			/* Skip empty reports */
668 			if (!evdev->ev_report_opened)
669 				return (EV_SKIP_EVENT);
670 			evdev->ev_report_opened = false;
671 			return (EV_REPORT_EVENT);
672 		}
673 		break;
674 	}
675 
676 	evdev->ev_report_opened = true;
677 	return (EV_REPORT_EVENT);
678 }
679 
680 static void
681 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
682     int32_t value)
683 {
684 	struct evdev_client *client;
685 
686 	debugf(evdev, "%s pushed event %d/%d/%d",
687 	    evdev->ev_shortname, type, code, value);
688 
689 	EVDEV_LOCK_ASSERT(evdev);
690 
691 	/* Propagate event through all clients */
692 	LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
693 		if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
694 			continue;
695 
696 		EVDEV_CLIENT_LOCKQ(client);
697 		evdev_client_push(client, type, code, value);
698 		if (type == EV_SYN && code == SYN_REPORT)
699 			evdev_notify_event(client);
700 		EVDEV_CLIENT_UNLOCKQ(client);
701 	}
702 
703 	/* Update counters */
704 	evdev->ev_event_count++;
705 	if (type == EV_SYN && code == SYN_REPORT)
706 		evdev->ev_report_count++;
707 }
708 
709 void
710 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
711     int32_t value)
712 {
713 	enum evdev_sparse_result sparse;
714 
715 	EVDEV_LOCK_ASSERT(evdev);
716 
717 	sparse =  evdev_sparse_event(evdev, type, code, value);
718 	switch (sparse) {
719 	case EV_REPORT_MT_SLOT:
720 		/* report postponed ABS_MT_SLOT */
721 		evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
722 		    CURRENT_MT_SLOT(evdev));
723 		/* FALLTHROUGH */
724 	case EV_REPORT_EVENT:
725 		evdev_propagate_event(evdev, type, code, value);
726 		/* FALLTHROUGH */
727 	case EV_SKIP_EVENT:
728 		break;
729 	}
730 }
731 
732 int
733 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
734     int32_t value)
735 {
736 
737 	if (evdev_check_event(evdev, type, code, value) != 0)
738 		return (EINVAL);
739 
740 	EVDEV_LOCK(evdev);
741 	evdev_modify_event(evdev, type, code, &value);
742 	if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
743 	    bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
744 		evdev_send_mt_compat(evdev);
745 	evdev_send_event(evdev, type, code, value);
746 	EVDEV_UNLOCK(evdev);
747 
748 	return (0);
749 }
750 
751 int
752 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
753     int32_t value)
754 {
755 	int ret = 0;
756 
757 	switch (type) {
758 	case EV_REP:
759 		/* evdev repeats should not be processed by hardware driver */
760 		if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
761 			goto push;
762 		/* FALLTHROUGH */
763 	case EV_LED:
764 	case EV_MSC:
765 	case EV_SND:
766 	case EV_FF:
767 		if (evdev->ev_methods != NULL &&
768 		    evdev->ev_methods->ev_event != NULL)
769 			evdev->ev_methods->ev_event(evdev, evdev->ev_softc,
770 			    type, code, value);
771 		/*
772 		 * Leds and driver repeats should be reported in ev_event
773 		 * method body to interoperate with kbdmux states and rates
774 		 * propagation so both ways (ioctl and evdev) of changing it
775 		 * will produce only one evdev event report to client.
776 		 */
777 		if (type == EV_LED || type == EV_REP)
778 			break;
779 		/* FALLTHROUGH */
780 	case EV_SYN:
781 	case EV_KEY:
782 	case EV_REL:
783 	case EV_ABS:
784 	case EV_SW:
785 push:
786 		ret = evdev_push_event(evdev, type,  code, value);
787 		break;
788 
789 	default:
790 		ret = EINVAL;
791 	}
792 
793 	return (ret);
794 }
795 
796 inline int
797 evdev_sync(struct evdev_dev *evdev)
798 {
799 
800 	return (evdev_push_event(evdev, EV_SYN, SYN_REPORT, 1));
801 }
802 
803 
804 inline int
805 evdev_mt_sync(struct evdev_dev *evdev)
806 {
807 
808 	return (evdev_push_event(evdev, EV_SYN, SYN_MT_REPORT, 1));
809 }
810 
811 int
812 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
813 {
814 	int ret = 0;
815 
816 	debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
817 
818 	EVDEV_LOCK_ASSERT(evdev);
819 
820 	if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
821 	    evdev->ev_methods->ev_open != NULL) {
822 		debugf(evdev, "calling ev_open() on device %s",
823 		    evdev->ev_shortname);
824 		ret = evdev->ev_methods->ev_open(evdev, evdev->ev_softc);
825 	}
826 	if (ret == 0)
827 		LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
828 	return (ret);
829 }
830 
831 void
832 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
833 {
834 	debugf(evdev, "removing client for device %s", evdev->ev_shortname);
835 
836 	EVDEV_LOCK_ASSERT(evdev);
837 
838 	LIST_REMOVE(client, ec_link);
839 	if (LIST_EMPTY(&evdev->ev_clients)) {
840 		if (evdev->ev_methods != NULL &&
841 		    evdev->ev_methods->ev_close != NULL)
842 			evdev->ev_methods->ev_close(evdev, evdev->ev_softc);
843 		if (evdev_event_supported(evdev, EV_REP) &&
844 		    bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
845 			evdev_stop_repeat(evdev);
846 	}
847 	evdev_release_client(evdev, client);
848 }
849 
850 int
851 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
852 {
853 
854 	EVDEV_LOCK_ASSERT(evdev);
855 
856 	if (evdev->ev_grabber != NULL)
857 		return (EBUSY);
858 
859 	evdev->ev_grabber = client;
860 
861 	return (0);
862 }
863 
864 int
865 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
866 {
867 
868 	EVDEV_LOCK_ASSERT(evdev);
869 
870 	if (evdev->ev_grabber != client)
871 		return (EINVAL);
872 
873 	evdev->ev_grabber = NULL;
874 
875 	return (0);
876 }
877 
878 static void
879 evdev_repeat_callout(void *arg)
880 {
881 	struct evdev_dev *evdev = (struct evdev_dev *)arg;
882 
883 	evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
884 	evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
885 
886 	if (evdev->ev_rep[REP_PERIOD])
887 		callout_reset(&evdev->ev_rep_callout,
888 		    evdev->ev_rep[REP_PERIOD] * hz / 1000,
889 		    evdev_repeat_callout, evdev);
890 	else
891 		evdev->ev_rep_key = KEY_RESERVED;
892 }
893 
894 static void
895 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
896 {
897 
898 	EVDEV_LOCK_ASSERT(evdev);
899 
900 	if (evdev->ev_rep[REP_DELAY]) {
901 		evdev->ev_rep_key = key;
902 		callout_reset(&evdev->ev_rep_callout,
903 		    evdev->ev_rep[REP_DELAY] * hz / 1000,
904 		    evdev_repeat_callout, evdev);
905 	}
906 }
907 
908 static void
909 evdev_stop_repeat(struct evdev_dev *evdev)
910 {
911 
912 	EVDEV_LOCK_ASSERT(evdev);
913 
914 	if (evdev->ev_rep_key != KEY_RESERVED) {
915 		callout_stop(&evdev->ev_rep_callout);
916 		evdev->ev_rep_key = KEY_RESERVED;
917 	}
918 }
919