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