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