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