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