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