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