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