xref: /freebsd/sys/dev/evdev/evdev.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 
28 #include "opt_evdev.h"
29 
30 #include <sys/param.h>
31 #include <sys/bitstring.h>
32 #include <sys/ck.h>
33 #include <sys/conf.h>
34 #include <sys/epoch.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/sx.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
43 
44 #include <dev/evdev/evdev.h>
45 #include <dev/evdev/evdev_private.h>
46 #include <dev/evdev/input.h>
47 
48 #ifdef EVDEV_DEBUG
49 #define	debugf(evdev, fmt, args...)	printf("evdev: " fmt "\n", ##args)
50 #else
51 #define	debugf(evdev, fmt, args...)
52 #endif
53 
54 #ifdef FEATURE
55 FEATURE(evdev, "Input event devices support");
56 #ifdef EVDEV_SUPPORT
57 FEATURE(evdev_support, "Evdev support in hybrid drivers");
58 #endif
59 #endif
60 
61 enum evdev_sparse_result
62 {
63 	EV_SKIP_EVENT,		/* Event value not changed */
64 	EV_REPORT_EVENT,	/* Event value changed */
65 	EV_REPORT_MT_SLOT,	/* Event value and MT slot number changed */
66 };
67 
68 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
69 
70 /* adb keyboard driver used on powerpc does not support evdev yet */
71 #if defined(__powerpc__) && !defined(__powerpc64__)
72 int evdev_rcpt_mask = EVDEV_RCPT_KBDMUX | EVDEV_RCPT_HW_MOUSE;
73 #else
74 int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD;
75 #endif
76 int evdev_sysmouse_t_axis = 0;
77 
78 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
79     "Evdev args");
80 #ifdef EVDEV_SUPPORT
81 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RWTUN, &evdev_rcpt_mask, 0,
82     "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
83     "bit2 - mouse hardware, bit3 - keyboard hardware");
84 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RWTUN,
85     &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm");
86 #endif
87 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
88     "Evdev input devices");
89 
90 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
91 static void evdev_stop_repeat(struct evdev_dev *);
92 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
93 
94 struct evdev_dev *
95 evdev_alloc(void)
96 {
97 
98 	return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
99 }
100 
101 void
102 evdev_free(struct evdev_dev *evdev)
103 {
104 
105 	if (evdev != NULL && evdev->ev_cdev != NULL &&
106 	    evdev->ev_cdev->si_drv1 != NULL)
107 		evdev_unregister(evdev);
108 
109 	free(evdev, M_EVDEV);
110 }
111 
112 static struct input_absinfo *
113 evdev_alloc_absinfo(void)
114 {
115 
116 	return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
117 	    M_WAITOK | M_ZERO));
118 }
119 
120 static void
121 evdev_free_absinfo(struct input_absinfo *absinfo)
122 {
123 
124 	free(absinfo, M_EVDEV);
125 }
126 
127 int
128 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
129 {
130 	if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
131 	    MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
132 		return (EINVAL);
133 
134 	evdev->ev_report_size = report_size;
135 	return (0);
136 }
137 
138 static size_t
139 evdev_estimate_report_size(struct evdev_dev *evdev)
140 {
141 	size_t size = 0;
142 	int res;
143 
144 	/*
145 	 * Keyboards generate one event per report but other devices with
146 	 * buttons like mouses can report events simultaneously
147 	 */
148 	bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
149 	if (res == -1)
150 		bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
151 	size += (res != -1);
152 	bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
153 	size += res;
154 
155 	/* All relative axes can be reported simultaneously */
156 	bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
157 	size += res;
158 
159 	/*
160 	 * All absolute axes can be reported simultaneously.
161 	 * Multitouch axes can be reported ABS_MT_SLOT times
162 	 */
163 	if (evdev->ev_absinfo != NULL) {
164 		bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
165 		size += res;
166 		bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
167 		if (res > 0) {
168 			res++;	/* ABS_MT_SLOT or SYN_MT_REPORT */
169 			if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
170 				/* MT type B */
171 				size += res * MAXIMAL_MT_SLOT(evdev);
172 			else
173 				/* MT type A */
174 				size += res * (MAX_MT_REPORTS - 1);
175 		}
176 	}
177 
178 	/* All misc events can be reported simultaneously */
179 	bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
180 	size += res;
181 
182 	/* All leds can be reported simultaneously */
183 	bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
184 	size += res;
185 
186 	/* Assume other events are generated once per report */
187 	bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
188 	size += (res != -1);
189 
190 	bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
191 	size += (res != -1);
192 
193 	/* XXX: FF part is not implemented yet */
194 
195 	size++;		/* SYN_REPORT */
196 	return (size);
197 }
198 
199 static void
200 evdev_sysctl_create(struct evdev_dev *evdev)
201 {
202 	struct sysctl_oid *ev_sysctl_tree;
203 	char ev_unit_str[8];
204 
205 	snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
206 	sysctl_ctx_init(&evdev->ev_sysctl_ctx);
207 
208 	ev_sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&evdev->ev_sysctl_ctx,
209 	    SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO,
210 	    ev_unit_str, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "",
211 	    "device index");
212 
213 	SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
214 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
215 	    evdev->ev_name, 0,
216 	    "Input device name");
217 
218 	SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
219 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
220 	    &evdev->ev_id, input_id,
221 	    "Input device identification");
222 
223 	/* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
224 	SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
225 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
226 	    evdev->ev_shortname, 0,
227 	    "Input device short name");
228 
229 	/* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
230 	SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
231 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
232 	    evdev->ev_serial, 0,
233 	    "Input device unique number");
234 
235 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
236 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
237 	    evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
238 	    "Input device properties");
239 
240 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
241 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
242 	    evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
243 	    "Input device supported events types");
244 
245 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
246 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
247 	    evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
248 	    "", "Input device supported keys");
249 
250 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
251 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
252 	    evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
253 	    "Input device supported relative events");
254 
255 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
256 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
257 	    evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
258 	    "Input device supported absolute events");
259 
260 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
261 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
262 	    evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
263 	    "Input device supported miscellaneous events");
264 
265 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
266 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
267 	    evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
268 	    "Input device supported LED events");
269 
270 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
271 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
272 	    evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
273 	    "Input device supported sound events");
274 
275 	SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
276 	    SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
277 	    evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
278 	    "Input device supported switch events");
279 }
280 
281 static int
282 evdev_register_common(struct evdev_dev *evdev)
283 {
284 	int ret;
285 
286 	debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
287 	    evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
288 
289 	/* Initialize internal structures */
290 	CK_SLIST_INIT(&evdev->ev_clients);
291 	sx_init(&evdev->ev_list_lock, "evsx");
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,
297 		    evdev->ev_state_lock, 0);
298 
299 		if (evdev->ev_rep[REP_DELAY] == 0 &&
300 		    evdev->ev_rep[REP_PERIOD] == 0) {
301 			/* Supply default values */
302 			evdev->ev_rep[REP_DELAY] = 250;
303 			evdev->ev_rep[REP_PERIOD] = 33;
304 		}
305 	}
306 
307 	/* Initialize multitouch protocol type B states or A to B converter */
308 	if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) ||
309 	    bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK))
310 		evdev_mt_init(evdev);
311 
312 	/* Estimate maximum report size */
313 	if (evdev->ev_report_size == 0) {
314 		ret = evdev_set_report_size(evdev,
315 		    evdev_estimate_report_size(evdev));
316 		if (ret != 0)
317 			goto bail_out;
318 	}
319 
320 	/* Create char device node */
321 	ret = evdev_cdev_create(evdev);
322 	if (ret != 0)
323 		goto bail_out;
324 
325 	/* Create sysctls (for device enumeration without /dev/input access rights) */
326 	evdev_sysctl_create(evdev);
327 
328 bail_out:
329 	if (ret != 0)
330 		sx_destroy(&evdev->ev_list_lock);
331 	return (ret);
332 }
333 
334 int
335 evdev_register(struct evdev_dev *evdev)
336 {
337 	int ret;
338 
339 	if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH))
340 		evdev->ev_lock_type = EV_LOCK_EXT_EPOCH;
341 	else
342 		evdev->ev_lock_type = EV_LOCK_INTERNAL;
343 	evdev->ev_state_lock = &evdev->ev_mtx;
344 	mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
345 
346 	ret = evdev_register_common(evdev);
347 	if (ret != 0)
348 		mtx_destroy(&evdev->ev_mtx);
349 
350 	return (ret);
351 }
352 
353 int
354 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
355 {
356 
357 	evdev->ev_lock_type = EV_LOCK_MTX;
358 	evdev->ev_state_lock = mtx;
359 	return (evdev_register_common(evdev));
360 }
361 
362 int
363 evdev_unregister(struct evdev_dev *evdev)
364 {
365 	struct evdev_client *client, *tmp;
366 	int ret;
367 	debugf(evdev, "%s: unregistered evdev provider: %s\n",
368 	    evdev->ev_shortname, evdev->ev_name);
369 
370 	sysctl_ctx_free(&evdev->ev_sysctl_ctx);
371 
372 	EVDEV_LIST_LOCK(evdev);
373 	evdev->ev_cdev->si_drv1 = NULL;
374 	/* Wake up sleepers */
375 	CK_SLIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) {
376 		evdev_revoke_client(client);
377 		evdev_dispose_client(evdev, client);
378 		EVDEV_CLIENT_LOCKQ(client);
379 		evdev_notify_event(client);
380 		EVDEV_CLIENT_UNLOCKQ(client);
381 	}
382 	EVDEV_LIST_UNLOCK(evdev);
383 
384 	/* release lock to avoid deadlock with evdev_dtor */
385 	ret = evdev_cdev_destroy(evdev);
386 	evdev->ev_cdev = NULL;
387 	sx_destroy(&evdev->ev_list_lock);
388 	if (ret == 0 && evdev->ev_lock_type != EV_LOCK_MTX)
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 minimum,
481     int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution)
482 {
483 	struct input_absinfo absinfo;
484 
485 	KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
486 
487 	absinfo = (struct input_absinfo) {
488 		.value = 0,
489 		.minimum = minimum,
490 		.maximum = maximum,
491 		.fuzz = fuzz,
492 		.flat = flat,
493 		.resolution = resolution,
494 	};
495 	evdev_set_abs_bit(evdev, code);
496 	evdev_set_absinfo(evdev, code, &absinfo);
497 }
498 
499 inline void
500 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
501 {
502 
503 	KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
504 	if (evdev->ev_absinfo == NULL)
505 		evdev->ev_absinfo = evdev_alloc_absinfo();
506 	bit_set(evdev->ev_abs_flags, code);
507 }
508 
509 inline void
510 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
511 {
512 
513 	KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
514 	bit_set(evdev->ev_msc_flags, code);
515 }
516 
517 
518 inline void
519 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
520 {
521 
522 	KASSERT(code < LED_CNT, ("invalid evdev led property"));
523 	bit_set(evdev->ev_led_flags, code);
524 }
525 
526 inline void
527 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
528 {
529 
530 	KASSERT(code < SND_CNT, ("invalid evdev snd property"));
531 	bit_set(evdev->ev_snd_flags, code);
532 }
533 
534 inline void
535 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
536 {
537 
538 	KASSERT(code < SW_CNT, ("invalid evdev sw property"));
539 	bit_set(evdev->ev_sw_flags, code);
540 }
541 
542 bool
543 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
544 {
545 
546 	KASSERT(type < EV_CNT, ("invalid evdev event property"));
547 	return (bit_test(evdev->ev_type_flags, type));
548 }
549 
550 inline void
551 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
552     struct input_absinfo *absinfo)
553 {
554 
555 	KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
556 
557 	if (axis == ABS_MT_SLOT &&
558 	    (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
559 		return;
560 
561 	if (evdev->ev_absinfo == NULL)
562 		evdev->ev_absinfo = evdev_alloc_absinfo();
563 
564 	if (axis == ABS_MT_SLOT)
565 		evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
566 	else
567 		memcpy(&evdev->ev_absinfo[axis], absinfo,
568 		    sizeof(struct input_absinfo));
569 }
570 
571 inline void
572 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
573 {
574 
575 	KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
576 	evdev->ev_rep[property] = value;
577 }
578 
579 inline void
580 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
581 {
582 
583 	KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
584 	bit_set(evdev->ev_flags, flag);
585 }
586 
587 static int
588 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
589     int32_t value)
590 {
591 
592 	if (type >= EV_CNT)
593 		return (EINVAL);
594 
595 	/* Allow SYN events implicitly */
596 	if (type != EV_SYN && !evdev_event_supported(evdev, type))
597 		return (EINVAL);
598 
599 	switch (type) {
600 	case EV_SYN:
601 		if (code >= SYN_CNT)
602 			return (EINVAL);
603 		break;
604 
605 	case EV_KEY:
606 		if (code >= KEY_CNT)
607 			return (EINVAL);
608 		if (!bit_test(evdev->ev_key_flags, code))
609 			return (EINVAL);
610 		break;
611 
612 	case EV_REL:
613 		if (code >= REL_CNT)
614 			return (EINVAL);
615 		if (!bit_test(evdev->ev_rel_flags, code))
616 			return (EINVAL);
617 		break;
618 
619 	case EV_ABS:
620 		if (code >= ABS_CNT)
621 			return (EINVAL);
622 		if (!bit_test(evdev->ev_abs_flags, code))
623 			return (EINVAL);
624 		if (code == ABS_MT_SLOT &&
625 		    (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
626 			return (EINVAL);
627 		if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
628 		    bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
629 			return (EINVAL);
630 		break;
631 
632 	case EV_MSC:
633 		if (code >= MSC_CNT)
634 			return (EINVAL);
635 		if (!bit_test(evdev->ev_msc_flags, code))
636 			return (EINVAL);
637 		break;
638 
639 	case EV_LED:
640 		if (code >= LED_CNT)
641 			return (EINVAL);
642 		if (!bit_test(evdev->ev_led_flags, code))
643 			return (EINVAL);
644 		break;
645 
646 	case EV_SND:
647 		if (code >= SND_CNT)
648 			return (EINVAL);
649 		if (!bit_test(evdev->ev_snd_flags, code))
650 			return (EINVAL);
651 		break;
652 
653 	case EV_SW:
654 		if (code >= SW_CNT)
655 			return (EINVAL);
656 		if (!bit_test(evdev->ev_sw_flags, code))
657 			return (EINVAL);
658 		break;
659 
660 	case EV_REP:
661 		if (code >= REP_CNT)
662 			return (EINVAL);
663 		break;
664 
665 	default:
666 		return (EINVAL);
667 	}
668 
669 	return (0);
670 }
671 
672 static void
673 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
674     int32_t *value)
675 {
676 	int32_t fuzz, old_value, abs_change;
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 			    !CK_SLIST_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 		if (code == ABS_MT_SLOT)
704 			break;
705 		else if (!ABS_IS_MT(code))
706 			old_value = evdev->ev_absinfo[code].value;
707 		else if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
708 			/* Pass MT protocol type A events as is */
709 			break;
710 		else if (code == ABS_MT_TRACKING_ID) {
711 			*value = evdev_mt_reassign_id(evdev,
712 			    evdev_mt_get_last_slot(evdev), *value);
713 			break;
714 		} else
715 			old_value = evdev_mt_get_value(evdev,
716 			    evdev_mt_get_last_slot(evdev), code);
717 
718 		fuzz = evdev->ev_absinfo[code].fuzz;
719 		if (fuzz == 0)
720 			break;
721 
722 		abs_change = abs(*value - old_value);
723 		if (abs_change < fuzz / 2)
724 			*value = old_value;
725 		else if (abs_change < fuzz)
726 			*value = (old_value * 3 + *value) / 4;
727 		else if (abs_change < fuzz * 2)
728 			*value = (old_value + *value) / 2;
729 		break;
730 	}
731 }
732 
733 static enum evdev_sparse_result
734 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
735     int32_t value)
736 {
737 	int32_t last_mt_slot;
738 
739 	EVDEV_LOCK_ASSERT(evdev);
740 
741 	/*
742 	 * For certain event types, update device state bits
743 	 * and convert level reporting to edge reporting
744 	 */
745 	switch (type) {
746 	case EV_KEY:
747 		switch (value) {
748 		case KEY_EVENT_UP:
749 		case KEY_EVENT_DOWN:
750 			if (bit_test(evdev->ev_key_states, code) == value)
751 				return (EV_SKIP_EVENT);
752 			bit_change(evdev->ev_key_states, code, value);
753 			break;
754 
755 		case KEY_EVENT_REPEAT:
756 			if (bit_test(evdev->ev_key_states, code) == 0 ||
757 			    !evdev_event_supported(evdev, EV_REP))
758 				return (EV_SKIP_EVENT);
759 			break;
760 
761 		default:
762 			 return (EV_SKIP_EVENT);
763 		}
764 		break;
765 
766 	case EV_LED:
767 		if (bit_test(evdev->ev_led_states, code) == value)
768 			return (EV_SKIP_EVENT);
769 		bit_change(evdev->ev_led_states, code, value);
770 		break;
771 
772 	case EV_SND:
773 		bit_change(evdev->ev_snd_states, code, value);
774 		break;
775 
776 	case EV_SW:
777 		if (bit_test(evdev->ev_sw_states, code) == value)
778 			return (EV_SKIP_EVENT);
779 		bit_change(evdev->ev_sw_states, code, value);
780 		break;
781 
782 	case EV_REP:
783 		if (evdev->ev_rep[code] == value)
784 			return (EV_SKIP_EVENT);
785 		evdev_set_repeat_params(evdev, code, value);
786 		break;
787 
788 	case EV_REL:
789 		if (value == 0)
790 			return (EV_SKIP_EVENT);
791 		break;
792 
793 	/* For EV_ABS, save last value in absinfo and ev_mt_states */
794 	case EV_ABS:
795 		switch (code) {
796 		case ABS_MT_SLOT:
797 			/* Postpone ABS_MT_SLOT till next event */
798 			evdev_mt_set_last_slot(evdev, value);
799 			return (EV_SKIP_EVENT);
800 
801 		case ABS_MT_FIRST ... ABS_MT_LAST:
802 			/* Pass MT protocol type A events as is */
803 			if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
804 				break;
805 			/* Don`t repeat MT protocol type B events */
806 			last_mt_slot = evdev_mt_get_last_slot(evdev);
807 			if (evdev_mt_get_value(evdev, last_mt_slot, code)
808 			     == value)
809 				return (EV_SKIP_EVENT);
810 			evdev_mt_set_value(evdev, last_mt_slot, code, value);
811 			if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
812 				CURRENT_MT_SLOT(evdev) = last_mt_slot;
813 				evdev->ev_report_opened = true;
814 				return (EV_REPORT_MT_SLOT);
815 			}
816 			break;
817 
818 		default:
819 			if (evdev->ev_absinfo[code].value == value)
820 				return (EV_SKIP_EVENT);
821 			evdev->ev_absinfo[code].value = value;
822 		}
823 		break;
824 
825 	case EV_SYN:
826 		if (code == SYN_REPORT) {
827 			/* Count empty reports as well as non empty */
828 			evdev->ev_report_count++;
829 			/* Skip empty reports */
830 			if (!evdev->ev_report_opened)
831 				return (EV_SKIP_EVENT);
832 			evdev->ev_report_opened = false;
833 			return (EV_REPORT_EVENT);
834 		}
835 		break;
836 	}
837 
838 	evdev->ev_report_opened = true;
839 	return (EV_REPORT_EVENT);
840 }
841 
842 static void
843 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
844     int32_t value)
845 {
846 	struct epoch_tracker et;
847 	struct evdev_client *client;
848 
849 	debugf(evdev, "%s pushed event %d/%d/%d",
850 	    evdev->ev_shortname, type, code, value);
851 
852 	EVDEV_LOCK_ASSERT(evdev);
853 
854 	/* Propagate event through all clients */
855 	if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
856 		epoch_enter_preempt(INPUT_EPOCH, &et);
857 
858 	KASSERT(
859 	    evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0,
860 	    ("Input epoch has not been entered\n"));
861 
862 	CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) {
863 		if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
864 			continue;
865 
866 		EVDEV_CLIENT_LOCKQ(client);
867 		evdev_client_push(client, type, code, value);
868 		if (type == EV_SYN && code == SYN_REPORT)
869 			evdev_notify_event(client);
870 		EVDEV_CLIENT_UNLOCKQ(client);
871 	}
872 	if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
873 		epoch_exit_preempt(INPUT_EPOCH, &et);
874 
875 	evdev->ev_event_count++;
876 }
877 
878 void
879 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
880     int32_t value)
881 {
882 	enum evdev_sparse_result sparse;
883 
884 	EVDEV_LOCK_ASSERT(evdev);
885 
886 	evdev_modify_event(evdev, type, code, &value);
887 	sparse =  evdev_sparse_event(evdev, type, code, value);
888 	switch (sparse) {
889 	case EV_REPORT_MT_SLOT:
890 		/* report postponed ABS_MT_SLOT */
891 		evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
892 		    CURRENT_MT_SLOT(evdev));
893 		/* FALLTHROUGH */
894 	case EV_REPORT_EVENT:
895 		evdev_propagate_event(evdev, type, code, value);
896 		/* FALLTHROUGH */
897 	case EV_SKIP_EVENT:
898 		break;
899 	}
900 }
901 
902 void
903 evdev_restore_after_kdb(struct evdev_dev *evdev)
904 {
905 	int code;
906 
907 	EVDEV_LOCK_ASSERT(evdev);
908 
909 	/* Report postponed leds */
910 	bit_foreach(evdev->ev_kdb_led_states, LED_CNT, code)
911 		evdev_send_event(evdev, EV_LED, code,
912 		    !bit_test(evdev->ev_led_states, code));
913 	bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
914 
915 	/* Release stuck keys (CTRL + ALT + ESC) */
916 	evdev_stop_repeat(evdev);
917 	bit_foreach(evdev->ev_key_states, KEY_CNT, code)
918 		evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
919 	evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
920 }
921 
922 int
923 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
924     int32_t value)
925 {
926 
927 	if (evdev_check_event(evdev, type, code, value) != 0)
928 		return (EINVAL);
929 
930 	/*
931 	 * Discard all but LEDs kdb events as unrelated to userspace.
932 	 * Aggregate LED updates and postpone reporting until kdb deactivation.
933 	 */
934 	if (kdb_active || SCHEDULER_STOPPED()) {
935 		evdev->ev_kdb_active = true;
936 		if (type == EV_LED)
937 			bit_set(evdev->ev_kdb_led_states,
938 			    bit_test(evdev->ev_led_states, code) != value);
939 		return (0);
940 	}
941 
942 	EVDEV_ENTER(evdev);
943 
944 	/* Fix evdev state corrupted with discarding of kdb events */
945 	if (evdev->ev_kdb_active) {
946 		evdev->ev_kdb_active = false;
947 		evdev_restore_after_kdb(evdev);
948 	}
949 
950 	if (type == EV_SYN && code == SYN_REPORT &&
951 	    bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
952 		evdev_mt_sync_frame(evdev);
953 	else
954 		if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) &&
955 		    evdev_mt_record_event(evdev, type, code, value))
956 			goto exit;
957 
958 	evdev_send_event(evdev, type, code, value);
959 exit:
960 	EVDEV_EXIT(evdev);
961 
962 	return (0);
963 }
964 
965 int
966 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
967     int32_t value)
968 {
969 	struct epoch_tracker et;
970 	int ret = 0;
971 
972 	switch (type) {
973 	case EV_REP:
974 		/* evdev repeats should not be processed by hardware driver */
975 		if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
976 			goto push;
977 		/* FALLTHROUGH */
978 	case EV_LED:
979 	case EV_MSC:
980 	case EV_SND:
981 	case EV_FF:
982 		if (evdev->ev_methods != NULL &&
983 		    evdev->ev_methods->ev_event != NULL)
984 			evdev->ev_methods->ev_event(evdev, type, code, value);
985 		/*
986 		 * Leds and driver repeats should be reported in ev_event
987 		 * method body to interoperate with kbdmux states and rates
988 		 * propagation so both ways (ioctl and evdev) of changing it
989 		 * will produce only one evdev event report to client.
990 		 */
991 		if (type == EV_LED || type == EV_REP)
992 			break;
993 		/* FALLTHROUGH */
994 	case EV_SYN:
995 	case EV_KEY:
996 	case EV_REL:
997 	case EV_ABS:
998 	case EV_SW:
999 push:
1000 		if (evdev->ev_lock_type == EV_LOCK_MTX)
1001 			EVDEV_LOCK(evdev);
1002 		else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1003 			epoch_enter_preempt(INPUT_EPOCH, &et);
1004 		ret = evdev_push_event(evdev, type,  code, value);
1005 		if (evdev->ev_lock_type == EV_LOCK_MTX)
1006 			EVDEV_UNLOCK(evdev);
1007 		else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1008 			epoch_exit_preempt(INPUT_EPOCH, &et);
1009 
1010 		break;
1011 
1012 	default:
1013 		ret = EINVAL;
1014 	}
1015 
1016 	return (ret);
1017 }
1018 
1019 int
1020 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
1021 {
1022 	int ret = 0;
1023 
1024 	debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
1025 
1026 	EVDEV_LIST_LOCK_ASSERT(evdev);
1027 
1028 	if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
1029 	    evdev->ev_methods->ev_open != NULL) {
1030 		debugf(evdev, "calling ev_open() on device %s",
1031 		    evdev->ev_shortname);
1032 		ret = evdev->ev_methods->ev_open(evdev);
1033 	}
1034 	if (ret == 0)
1035 		CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
1036 	return (ret);
1037 }
1038 
1039 void
1040 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
1041 {
1042 	debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1043 
1044 	EVDEV_LIST_LOCK_ASSERT(evdev);
1045 
1046 	CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link);
1047 	if (CK_SLIST_EMPTY(&evdev->ev_clients)) {
1048 		if (evdev->ev_methods != NULL &&
1049 		    evdev->ev_methods->ev_close != NULL)
1050 			(void)evdev->ev_methods->ev_close(evdev);
1051 		if (evdev_event_supported(evdev, EV_REP) &&
1052 		    bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
1053 			if (evdev->ev_lock_type != EV_LOCK_MTX)
1054 				EVDEV_LOCK(evdev);
1055 			evdev_stop_repeat(evdev);
1056 			if (evdev->ev_lock_type != EV_LOCK_MTX)
1057 				EVDEV_UNLOCK(evdev);
1058 		}
1059 	}
1060 	if (evdev->ev_lock_type != EV_LOCK_MTX)
1061 		EVDEV_LOCK(evdev);
1062 	evdev_release_client(evdev, client);
1063 	if (evdev->ev_lock_type != EV_LOCK_MTX)
1064 		EVDEV_UNLOCK(evdev);
1065 }
1066 
1067 int
1068 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1069 {
1070 
1071 	EVDEV_LOCK_ASSERT(evdev);
1072 
1073 	if (evdev->ev_grabber != NULL)
1074 		return (EBUSY);
1075 
1076 	evdev->ev_grabber = client;
1077 
1078 	return (0);
1079 }
1080 
1081 int
1082 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1083 {
1084 
1085 	EVDEV_LOCK_ASSERT(evdev);
1086 
1087 	if (evdev->ev_grabber != client)
1088 		return (EINVAL);
1089 
1090 	evdev->ev_grabber = NULL;
1091 
1092 	return (0);
1093 }
1094 
1095 bool
1096 evdev_is_grabbed(struct evdev_dev *evdev)
1097 {
1098 	if (kdb_active || SCHEDULER_STOPPED())
1099 		return (false);
1100 	/*
1101 	 * The function is intended to be called from evdev-unrelated parts of
1102 	 * code like syscons-compatible parts of mouse and keyboard drivers.
1103 	 * That makes unlocked read-only access acceptable.
1104 	 */
1105 	return (evdev->ev_grabber != NULL);
1106 }
1107 
1108 static void
1109 evdev_repeat_callout(void *arg)
1110 {
1111 	struct epoch_tracker et;
1112 	struct evdev_dev *evdev = (struct evdev_dev *)arg;
1113 
1114 	if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1115 		epoch_enter_preempt(INPUT_EPOCH, &et);
1116 	evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1117 	evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1118 	if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1119 		epoch_exit_preempt(INPUT_EPOCH, &et);
1120 
1121 	if (evdev->ev_rep[REP_PERIOD])
1122 		callout_reset(&evdev->ev_rep_callout,
1123 		    evdev->ev_rep[REP_PERIOD] * hz / 1000,
1124 		    evdev_repeat_callout, evdev);
1125 	else
1126 		evdev->ev_rep_key = KEY_RESERVED;
1127 }
1128 
1129 static void
1130 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1131 {
1132 
1133 	EVDEV_LOCK_ASSERT(evdev);
1134 
1135 	if (evdev->ev_rep[REP_DELAY]) {
1136 		evdev->ev_rep_key = key;
1137 		callout_reset(&evdev->ev_rep_callout,
1138 		    evdev->ev_rep[REP_DELAY] * hz / 1000,
1139 		    evdev_repeat_callout, evdev);
1140 	}
1141 }
1142 
1143 static void
1144 evdev_stop_repeat(struct evdev_dev *evdev)
1145 {
1146 
1147 	EVDEV_LOCK_ASSERT(evdev);
1148 
1149 	if (evdev->ev_rep_key != KEY_RESERVED) {
1150 		callout_stop(&evdev->ev_rep_callout);
1151 		evdev->ev_rep_key = KEY_RESERVED;
1152 	}
1153 }
1154 
1155 MODULE_VERSION(evdev, 1);
1156