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