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