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