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