xref: /freebsd/sys/dev/evdev/evdev.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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 #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 			    !LIST_EMPTY(&evdev->ev_clients)) {
584 				if (*value == KEY_EVENT_DOWN)
585 					evdev_start_repeat(evdev, code);
586 				else
587 					evdev_stop_repeat(evdev);
588 			}
589 		}
590 		break;
591 
592 	case EV_ABS:
593 		/* TBD: implement fuzz */
594 		break;
595 	}
596 }
597 
598 static enum evdev_sparse_result
599 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
600     int32_t value)
601 {
602 	int32_t last_mt_slot;
603 
604 	EVDEV_LOCK_ASSERT(evdev);
605 
606 	/*
607 	 * For certain event types, update device state bits
608 	 * and convert level reporting to edge reporting
609 	 */
610 	switch (type) {
611 	case EV_KEY:
612 		switch (value) {
613 		case KEY_EVENT_UP:
614 		case KEY_EVENT_DOWN:
615 			if (bit_test(evdev->ev_key_states, code) == value)
616 				return (EV_SKIP_EVENT);
617 			bit_change(evdev->ev_key_states, code, value);
618 			break;
619 
620 		case KEY_EVENT_REPEAT:
621 			if (bit_test(evdev->ev_key_states, code) == 0 ||
622 			    !evdev_event_supported(evdev, EV_REP))
623 				return (EV_SKIP_EVENT);
624 			break;
625 
626 		default:
627 			 return (EV_SKIP_EVENT);
628 		}
629 		break;
630 
631 	case EV_LED:
632 		if (bit_test(evdev->ev_led_states, code) == value)
633 			return (EV_SKIP_EVENT);
634 		bit_change(evdev->ev_led_states, code, value);
635 		break;
636 
637 	case EV_SND:
638 		bit_change(evdev->ev_snd_states, code, value);
639 		break;
640 
641 	case EV_SW:
642 		if (bit_test(evdev->ev_sw_states, code) == value)
643 			return (EV_SKIP_EVENT);
644 		bit_change(evdev->ev_sw_states, code, value);
645 		break;
646 
647 	case EV_REP:
648 		if (evdev->ev_rep[code] == value)
649 			return (EV_SKIP_EVENT);
650 		evdev_set_repeat_params(evdev, code, value);
651 		break;
652 
653 	case EV_REL:
654 		if (value == 0)
655 			return (EV_SKIP_EVENT);
656 		break;
657 
658 	/* For EV_ABS, save last value in absinfo and ev_mt_states */
659 	case EV_ABS:
660 		switch (code) {
661 		case ABS_MT_SLOT:
662 			/* Postpone ABS_MT_SLOT till next event */
663 			evdev_set_last_mt_slot(evdev, value);
664 			return (EV_SKIP_EVENT);
665 
666 		case ABS_MT_FIRST ... ABS_MT_LAST:
667 			/* Pass MT protocol type A events as is */
668 			if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
669 				break;
670 			/* Don`t repeat MT protocol type B events */
671 			last_mt_slot = evdev_get_last_mt_slot(evdev);
672 			if (evdev_get_mt_value(evdev, last_mt_slot, code)
673 			     == value)
674 				return (EV_SKIP_EVENT);
675 			evdev_set_mt_value(evdev, last_mt_slot, code, value);
676 			if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
677 				CURRENT_MT_SLOT(evdev) = last_mt_slot;
678 				evdev->ev_report_opened = true;
679 				return (EV_REPORT_MT_SLOT);
680 			}
681 			break;
682 
683 		default:
684 			if (evdev->ev_absinfo[code].value == value)
685 				return (EV_SKIP_EVENT);
686 			evdev->ev_absinfo[code].value = value;
687 		}
688 		break;
689 
690 	case EV_SYN:
691 		if (code == SYN_REPORT) {
692 			/* Count empty reports as well as non empty */
693 			evdev->ev_report_count++;
694 			/* Skip empty reports */
695 			if (!evdev->ev_report_opened)
696 				return (EV_SKIP_EVENT);
697 			evdev->ev_report_opened = false;
698 			return (EV_REPORT_EVENT);
699 		}
700 		break;
701 	}
702 
703 	evdev->ev_report_opened = true;
704 	return (EV_REPORT_EVENT);
705 }
706 
707 static void
708 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
709     int32_t value)
710 {
711 	struct evdev_client *client;
712 
713 	debugf(evdev, "%s pushed event %d/%d/%d",
714 	    evdev->ev_shortname, type, code, value);
715 
716 	EVDEV_LOCK_ASSERT(evdev);
717 
718 	/* Propagate event through all clients */
719 	LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
720 		if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
721 			continue;
722 
723 		EVDEV_CLIENT_LOCKQ(client);
724 		evdev_client_push(client, type, code, value);
725 		if (type == EV_SYN && code == SYN_REPORT)
726 			evdev_notify_event(client);
727 		EVDEV_CLIENT_UNLOCKQ(client);
728 	}
729 
730 	evdev->ev_event_count++;
731 }
732 
733 void
734 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
735     int32_t value)
736 {
737 	enum evdev_sparse_result sparse;
738 
739 	EVDEV_LOCK_ASSERT(evdev);
740 
741 	sparse =  evdev_sparse_event(evdev, type, code, value);
742 	switch (sparse) {
743 	case EV_REPORT_MT_SLOT:
744 		/* report postponed ABS_MT_SLOT */
745 		evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
746 		    CURRENT_MT_SLOT(evdev));
747 		/* FALLTHROUGH */
748 	case EV_REPORT_EVENT:
749 		evdev_propagate_event(evdev, type, code, value);
750 		/* FALLTHROUGH */
751 	case EV_SKIP_EVENT:
752 		break;
753 	}
754 }
755 
756 int
757 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
758     int32_t value)
759 {
760 
761 	if (evdev_check_event(evdev, type, code, value) != 0)
762 		return (EINVAL);
763 
764 	EVDEV_ENTER(evdev);
765 
766 	evdev_modify_event(evdev, type, code, &value);
767 	if (type == EV_SYN && code == SYN_REPORT &&
768 	     bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
769 		evdev_send_mt_autorel(evdev);
770 	if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
771 	    bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
772 		evdev_send_mt_compat(evdev);
773 	evdev_send_event(evdev, type, code, value);
774 
775 	EVDEV_EXIT(evdev);
776 
777 	return (0);
778 }
779 
780 int
781 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
782     int32_t value)
783 {
784 	int ret = 0;
785 
786 	switch (type) {
787 	case EV_REP:
788 		/* evdev repeats should not be processed by hardware driver */
789 		if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
790 			goto push;
791 		/* FALLTHROUGH */
792 	case EV_LED:
793 	case EV_MSC:
794 	case EV_SND:
795 	case EV_FF:
796 		if (evdev->ev_methods != NULL &&
797 		    evdev->ev_methods->ev_event != NULL)
798 			evdev->ev_methods->ev_event(evdev, evdev->ev_softc,
799 			    type, code, value);
800 		/*
801 		 * Leds and driver repeats should be reported in ev_event
802 		 * method body to interoperate with kbdmux states and rates
803 		 * propagation so both ways (ioctl and evdev) of changing it
804 		 * will produce only one evdev event report to client.
805 		 */
806 		if (type == EV_LED || type == EV_REP)
807 			break;
808 		/* FALLTHROUGH */
809 	case EV_SYN:
810 	case EV_KEY:
811 	case EV_REL:
812 	case EV_ABS:
813 	case EV_SW:
814 push:
815 		if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
816 			EVDEV_LOCK(evdev);
817 		ret = evdev_push_event(evdev, type,  code, value);
818 		if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
819 			EVDEV_UNLOCK(evdev);
820 		break;
821 
822 	default:
823 		ret = EINVAL;
824 	}
825 
826 	return (ret);
827 }
828 
829 int
830 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
831 {
832 	int ret = 0;
833 
834 	debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
835 
836 	EVDEV_LOCK_ASSERT(evdev);
837 
838 	if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
839 	    evdev->ev_methods->ev_open != NULL) {
840 		debugf(evdev, "calling ev_open() on device %s",
841 		    evdev->ev_shortname);
842 		ret = evdev->ev_methods->ev_open(evdev, evdev->ev_softc);
843 	}
844 	if (ret == 0)
845 		LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
846 	return (ret);
847 }
848 
849 void
850 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
851 {
852 	debugf(evdev, "removing client for device %s", evdev->ev_shortname);
853 
854 	EVDEV_LOCK_ASSERT(evdev);
855 
856 	LIST_REMOVE(client, ec_link);
857 	if (LIST_EMPTY(&evdev->ev_clients)) {
858 		if (evdev->ev_methods != NULL &&
859 		    evdev->ev_methods->ev_close != NULL)
860 			evdev->ev_methods->ev_close(evdev, evdev->ev_softc);
861 		if (evdev_event_supported(evdev, EV_REP) &&
862 		    bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
863 			evdev_stop_repeat(evdev);
864 	}
865 	evdev_release_client(evdev, client);
866 }
867 
868 int
869 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
870 {
871 
872 	EVDEV_LOCK_ASSERT(evdev);
873 
874 	if (evdev->ev_grabber != NULL)
875 		return (EBUSY);
876 
877 	evdev->ev_grabber = client;
878 
879 	return (0);
880 }
881 
882 int
883 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
884 {
885 
886 	EVDEV_LOCK_ASSERT(evdev);
887 
888 	if (evdev->ev_grabber != client)
889 		return (EINVAL);
890 
891 	evdev->ev_grabber = NULL;
892 
893 	return (0);
894 }
895 
896 static void
897 evdev_repeat_callout(void *arg)
898 {
899 	struct evdev_dev *evdev = (struct evdev_dev *)arg;
900 
901 	evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
902 	evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
903 
904 	if (evdev->ev_rep[REP_PERIOD])
905 		callout_reset(&evdev->ev_rep_callout,
906 		    evdev->ev_rep[REP_PERIOD] * hz / 1000,
907 		    evdev_repeat_callout, evdev);
908 	else
909 		evdev->ev_rep_key = KEY_RESERVED;
910 }
911 
912 static void
913 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
914 {
915 
916 	EVDEV_LOCK_ASSERT(evdev);
917 
918 	if (evdev->ev_rep[REP_DELAY]) {
919 		evdev->ev_rep_key = key;
920 		callout_reset(&evdev->ev_rep_callout,
921 		    evdev->ev_rep[REP_DELAY] * hz / 1000,
922 		    evdev_repeat_callout, evdev);
923 	}
924 }
925 
926 static void
927 evdev_stop_repeat(struct evdev_dev *evdev)
928 {
929 
930 	EVDEV_LOCK_ASSERT(evdev);
931 
932 	if (evdev->ev_rep_key != KEY_RESERVED) {
933 		callout_stop(&evdev->ev_rep_callout);
934 		evdev->ev_rep_key = KEY_RESERVED;
935 	}
936 }
937 
938 MODULE_VERSION(evdev, 1);
939