xref: /freebsd/sys/dev/evdev/evdev.c (revision ecccc9d99901dd874e8d122853026d7c28fa4fcf)
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 *
evdev_alloc(void)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
evdev_free(struct evdev_dev * evdev)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 *
evdev_alloc_absinfo(void)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
evdev_free_absinfo(struct input_absinfo * absinfo)128 evdev_free_absinfo(struct input_absinfo *absinfo)
129 {
130 
131 	free(absinfo, M_EVDEV);
132 }
133 
134 int
evdev_set_report_size(struct evdev_dev * evdev,size_t report_size)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
evdev_estimate_report_size(struct evdev_dev * evdev)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
evdev_sysctl_create(struct evdev_dev * evdev)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 
288 static int
evdev_register_common(struct evdev_dev * evdev)289 evdev_register_common(struct evdev_dev *evdev)
290 {
291 	int ret;
292 
293 	debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
294 	    evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
295 
296 	/* Initialize internal structures */
297 	CK_SLIST_INIT(&evdev->ev_clients);
298 	sx_init(&evdev->ev_list_lock, "evsx");
299 
300 	if (evdev_event_supported(evdev, EV_REP) &&
301 	    bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
302 		/* Initialize callout */
303 		callout_init_mtx(&evdev->ev_rep_callout,
304 		    evdev->ev_state_lock, 0);
305 
306 		if (evdev->ev_rep[REP_DELAY] == 0 &&
307 		    evdev->ev_rep[REP_PERIOD] == 0) {
308 			/* Supply default values */
309 			evdev->ev_rep[REP_DELAY] = 250;
310 			evdev->ev_rep[REP_PERIOD] = 33;
311 		}
312 	}
313 
314 	/* Initialize multitouch protocol type B states or A to B converter */
315 	if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) ||
316 	    bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK))
317 		evdev_mt_init(evdev);
318 
319 	/* Estimate maximum report size */
320 	if (evdev->ev_report_size == 0) {
321 		ret = evdev_set_report_size(evdev,
322 		    evdev_estimate_report_size(evdev));
323 		if (ret != 0)
324 			goto bail_out;
325 	}
326 
327 	/* Create char device node */
328 	ret = evdev_cdev_create(evdev);
329 	if (ret != 0)
330 		goto bail_out;
331 
332 	/* Create sysctls (for device enumeration without /dev/input access rights) */
333 	evdev_sysctl_create(evdev);
334 
335 bail_out:
336 	if (ret != 0)
337 		sx_destroy(&evdev->ev_list_lock);
338 	return (ret);
339 }
340 
341 int
evdev_register(struct evdev_dev * evdev)342 evdev_register(struct evdev_dev *evdev)
343 {
344 	int ret;
345 
346 	if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH))
347 		evdev->ev_lock_type = EV_LOCK_EXT_EPOCH;
348 	else
349 		evdev->ev_lock_type = EV_LOCK_INTERNAL;
350 	evdev->ev_state_lock = &evdev->ev_mtx;
351 	mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
352 
353 	ret = evdev_register_common(evdev);
354 	if (ret != 0)
355 		mtx_destroy(&evdev->ev_mtx);
356 
357 	return (ret);
358 }
359 
360 int
evdev_register_mtx(struct evdev_dev * evdev,struct mtx * mtx)361 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
362 {
363 
364 	evdev->ev_lock_type = EV_LOCK_MTX;
365 	evdev->ev_state_lock = mtx;
366 	return (evdev_register_common(evdev));
367 }
368 
369 int
evdev_unregister(struct evdev_dev * evdev)370 evdev_unregister(struct evdev_dev *evdev)
371 {
372 	struct evdev_client *client, *tmp;
373 	int ret;
374 	debugf(evdev, "%s: unregistered evdev provider: %s\n",
375 	    evdev->ev_shortname, evdev->ev_name);
376 
377 	sysctl_ctx_free(&evdev->ev_sysctl_ctx);
378 
379 	EVDEV_LIST_LOCK(evdev);
380 	evdev->ev_cdev->si_drv1 = NULL;
381 	/* Wake up sleepers */
382 	CK_SLIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) {
383 		evdev_revoke_client(client);
384 		evdev_dispose_client(evdev, client);
385 		EVDEV_CLIENT_LOCKQ(client);
386 		evdev_notify_event(client);
387 		EVDEV_CLIENT_UNLOCKQ(client);
388 	}
389 	EVDEV_LIST_UNLOCK(evdev);
390 
391 	/* release lock to avoid deadlock with evdev_dtor */
392 	ret = evdev_cdev_destroy(evdev);
393 	evdev->ev_cdev = NULL;
394 	sx_destroy(&evdev->ev_list_lock);
395 	if (ret == 0 && evdev->ev_lock_type != EV_LOCK_MTX)
396 		mtx_destroy(&evdev->ev_mtx);
397 
398 	evdev_free_absinfo(evdev->ev_absinfo);
399 	evdev_mt_free(evdev);
400 
401 	return (ret);
402 }
403 
404 inline void
evdev_set_name(struct evdev_dev * evdev,const char * name)405 evdev_set_name(struct evdev_dev *evdev, const char *name)
406 {
407 
408 	snprintf(evdev->ev_name, NAMELEN, "%s", name);
409 }
410 
411 inline void
evdev_set_id(struct evdev_dev * evdev,uint16_t bustype,uint16_t vendor,uint16_t product,uint16_t version)412 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
413     uint16_t product, uint16_t version)
414 {
415 
416 	evdev->ev_id = (struct input_id) {
417 		.bustype = bustype,
418 		.vendor = vendor,
419 		.product = product,
420 		.version = version
421 	};
422 }
423 
424 inline void
evdev_set_phys(struct evdev_dev * evdev,const char * name)425 evdev_set_phys(struct evdev_dev *evdev, const char *name)
426 {
427 
428 	snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
429 }
430 
431 inline void
evdev_set_serial(struct evdev_dev * evdev,const char * serial)432 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
433 {
434 
435 	snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
436 }
437 
438 inline void
evdev_set_methods(struct evdev_dev * evdev,void * softc,const struct evdev_methods * methods)439 evdev_set_methods(struct evdev_dev *evdev, void *softc,
440     const struct evdev_methods *methods)
441 {
442 
443 	evdev->ev_methods = methods;
444 	evdev->ev_softc = softc;
445 }
446 
447 inline void *
evdev_get_softc(struct evdev_dev * evdev)448 evdev_get_softc(struct evdev_dev *evdev)
449 {
450 
451 	return (evdev->ev_softc);
452 }
453 
454 inline void
evdev_support_prop(struct evdev_dev * evdev,uint16_t prop)455 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
456 {
457 
458 	KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
459 	bit_set(evdev->ev_prop_flags, prop);
460 }
461 
462 inline void
evdev_support_event(struct evdev_dev * evdev,uint16_t type)463 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
464 {
465 
466 	KASSERT(type < EV_CNT, ("invalid evdev event property"));
467 	bit_set(evdev->ev_type_flags, type);
468 }
469 
470 inline void
evdev_support_key(struct evdev_dev * evdev,uint16_t code)471 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
472 {
473 
474 	KASSERT(code < KEY_CNT, ("invalid evdev key property"));
475 	bit_set(evdev->ev_key_flags, code);
476 }
477 
478 inline void
evdev_support_rel(struct evdev_dev * evdev,uint16_t code)479 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
480 {
481 
482 	KASSERT(code < REL_CNT, ("invalid evdev rel property"));
483 	bit_set(evdev->ev_rel_flags, code);
484 }
485 
486 inline void
evdev_support_abs(struct evdev_dev * evdev,uint16_t code,int32_t minimum,int32_t maximum,int32_t fuzz,int32_t flat,int32_t resolution)487 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t minimum,
488     int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution)
489 {
490 	struct input_absinfo absinfo;
491 
492 	KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
493 
494 	absinfo = (struct input_absinfo) {
495 		.value = 0,
496 		.minimum = minimum,
497 		.maximum = maximum,
498 		.fuzz = fuzz,
499 		.flat = flat,
500 		.resolution = resolution,
501 	};
502 	evdev_set_abs_bit(evdev, code);
503 	evdev_set_absinfo(evdev, code, &absinfo);
504 }
505 
506 inline void
evdev_set_abs_bit(struct evdev_dev * evdev,uint16_t code)507 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
508 {
509 
510 	KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
511 	if (evdev->ev_absinfo == NULL)
512 		evdev->ev_absinfo = evdev_alloc_absinfo();
513 	bit_set(evdev->ev_abs_flags, code);
514 }
515 
516 inline void
evdev_support_msc(struct evdev_dev * evdev,uint16_t code)517 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
518 {
519 
520 	KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
521 	bit_set(evdev->ev_msc_flags, code);
522 }
523 
524 
525 inline void
evdev_support_led(struct evdev_dev * evdev,uint16_t code)526 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
527 {
528 
529 	KASSERT(code < LED_CNT, ("invalid evdev led property"));
530 	bit_set(evdev->ev_led_flags, code);
531 }
532 
533 inline void
evdev_support_snd(struct evdev_dev * evdev,uint16_t code)534 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
535 {
536 
537 	KASSERT(code < SND_CNT, ("invalid evdev snd property"));
538 	bit_set(evdev->ev_snd_flags, code);
539 }
540 
541 inline void
evdev_support_sw(struct evdev_dev * evdev,uint16_t code)542 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
543 {
544 
545 	KASSERT(code < SW_CNT, ("invalid evdev sw property"));
546 	bit_set(evdev->ev_sw_flags, code);
547 }
548 
549 bool
evdev_event_supported(struct evdev_dev * evdev,uint16_t type)550 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
551 {
552 
553 	KASSERT(type < EV_CNT, ("invalid evdev event property"));
554 	return (bit_test(evdev->ev_type_flags, type));
555 }
556 
557 inline void
evdev_set_absinfo(struct evdev_dev * evdev,uint16_t axis,struct input_absinfo * absinfo)558 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
559     struct input_absinfo *absinfo)
560 {
561 
562 	KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
563 
564 	if (axis == ABS_MT_SLOT &&
565 	    (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
566 		return;
567 
568 	if (evdev->ev_absinfo == NULL)
569 		evdev->ev_absinfo = evdev_alloc_absinfo();
570 
571 	if (axis == ABS_MT_SLOT)
572 		evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
573 	else
574 		memcpy(&evdev->ev_absinfo[axis], absinfo,
575 		    sizeof(struct input_absinfo));
576 }
577 
578 inline void
evdev_set_repeat_params(struct evdev_dev * evdev,uint16_t property,int value)579 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
580 {
581 
582 	KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
583 	evdev->ev_rep[property] = value;
584 }
585 
586 inline void
evdev_set_flag(struct evdev_dev * evdev,uint16_t flag)587 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
588 {
589 
590 	KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
591 	bit_set(evdev->ev_flags, flag);
592 }
593 
594 void
evdev_set_cdev_mode(struct evdev_dev * evdev,uid_t uid,gid_t gid,int mode)595 evdev_set_cdev_mode(struct evdev_dev *evdev, uid_t uid, gid_t gid, int mode)
596 {
597 	evdev->ev_cdev_uid = uid;
598 	evdev->ev_cdev_gid = gid;
599 	evdev->ev_cdev_mode = mode;
600 }
601 
602 static int
evdev_check_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)603 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
604     int32_t value)
605 {
606 
607 	if (type >= EV_CNT)
608 		return (EINVAL);
609 
610 	/* Allow SYN events implicitly */
611 	if (type != EV_SYN && !evdev_event_supported(evdev, type))
612 		return (EINVAL);
613 
614 	switch (type) {
615 	case EV_SYN:
616 		if (code >= SYN_CNT)
617 			return (EINVAL);
618 		break;
619 
620 	case EV_KEY:
621 		if (code >= KEY_CNT)
622 			return (EINVAL);
623 		if (!bit_test(evdev->ev_key_flags, code))
624 			return (EINVAL);
625 		break;
626 
627 	case EV_REL:
628 		if (code >= REL_CNT)
629 			return (EINVAL);
630 		if (!bit_test(evdev->ev_rel_flags, code))
631 			return (EINVAL);
632 		break;
633 
634 	case EV_ABS:
635 		if (code >= ABS_CNT)
636 			return (EINVAL);
637 		if (!bit_test(evdev->ev_abs_flags, code))
638 			return (EINVAL);
639 		if (code == ABS_MT_SLOT &&
640 		    (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
641 			return (EINVAL);
642 		if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
643 		    bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
644 			return (EINVAL);
645 		break;
646 
647 	case EV_MSC:
648 		if (code >= MSC_CNT)
649 			return (EINVAL);
650 		if (!bit_test(evdev->ev_msc_flags, code))
651 			return (EINVAL);
652 		break;
653 
654 	case EV_LED:
655 		if (code >= LED_CNT)
656 			return (EINVAL);
657 		if (!bit_test(evdev->ev_led_flags, code))
658 			return (EINVAL);
659 		break;
660 
661 	case EV_SND:
662 		if (code >= SND_CNT)
663 			return (EINVAL);
664 		if (!bit_test(evdev->ev_snd_flags, code))
665 			return (EINVAL);
666 		break;
667 
668 	case EV_SW:
669 		if (code >= SW_CNT)
670 			return (EINVAL);
671 		if (!bit_test(evdev->ev_sw_flags, code))
672 			return (EINVAL);
673 		break;
674 
675 	case EV_REP:
676 		if (code >= REP_CNT)
677 			return (EINVAL);
678 		break;
679 
680 	default:
681 		return (EINVAL);
682 	}
683 
684 	return (0);
685 }
686 
687 static void
evdev_modify_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t * value)688 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
689     int32_t *value)
690 {
691 	int32_t fuzz, old_value, abs_change;
692 
693 	EVDEV_LOCK_ASSERT(evdev);
694 
695 	switch (type) {
696 	case EV_KEY:
697 		if (!evdev_event_supported(evdev, EV_REP))
698 			break;
699 
700 		if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
701 			/* Detect driver key repeats. */
702 			if (bit_test(evdev->ev_key_states, code) &&
703 			    *value == KEY_EVENT_DOWN)
704 				*value = KEY_EVENT_REPEAT;
705 		} else {
706 			/* Start/stop callout for evdev repeats */
707 			if (bit_test(evdev->ev_key_states, code) == !*value &&
708 			    !CK_SLIST_EMPTY(&evdev->ev_clients)) {
709 				if (*value == KEY_EVENT_DOWN)
710 					evdev_start_repeat(evdev, code);
711 				else
712 					evdev_stop_repeat(evdev);
713 			}
714 		}
715 		break;
716 
717 	case EV_ABS:
718 		if (code == ABS_MT_SLOT)
719 			break;
720 		else if (!ABS_IS_MT(code))
721 			old_value = evdev->ev_absinfo[code].value;
722 		else if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
723 			/* Pass MT protocol type A events as is */
724 			break;
725 		else if (code == ABS_MT_TRACKING_ID) {
726 			*value = evdev_mt_reassign_id(evdev,
727 			    evdev_mt_get_last_slot(evdev), *value);
728 			break;
729 		} else
730 			old_value = evdev_mt_get_value(evdev,
731 			    evdev_mt_get_last_slot(evdev), code);
732 
733 		fuzz = evdev->ev_absinfo[code].fuzz;
734 		if (fuzz == 0)
735 			break;
736 
737 		abs_change = abs(*value - old_value);
738 		if (abs_change < fuzz / 2)
739 			*value = old_value;
740 		else if (abs_change < fuzz)
741 			*value = (old_value * 3 + *value) / 4;
742 		else if (abs_change < fuzz * 2)
743 			*value = (old_value + *value) / 2;
744 		break;
745 	}
746 }
747 
748 static enum evdev_sparse_result
evdev_sparse_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)749 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
750     int32_t value)
751 {
752 	int32_t last_mt_slot;
753 
754 	EVDEV_LOCK_ASSERT(evdev);
755 
756 	/*
757 	 * For certain event types, update device state bits
758 	 * and convert level reporting to edge reporting
759 	 */
760 	switch (type) {
761 	case EV_KEY:
762 		switch (value) {
763 		case KEY_EVENT_UP:
764 		case KEY_EVENT_DOWN:
765 			if (bit_test(evdev->ev_key_states, code) == value)
766 				return (EV_SKIP_EVENT);
767 			bit_change(evdev->ev_key_states, code, value);
768 			break;
769 
770 		case KEY_EVENT_REPEAT:
771 			if (bit_test(evdev->ev_key_states, code) == 0 ||
772 			    !evdev_event_supported(evdev, EV_REP))
773 				return (EV_SKIP_EVENT);
774 			break;
775 
776 		default:
777 			 return (EV_SKIP_EVENT);
778 		}
779 		break;
780 
781 	case EV_LED:
782 		if (bit_test(evdev->ev_led_states, code) == value)
783 			return (EV_SKIP_EVENT);
784 		bit_change(evdev->ev_led_states, code, value);
785 		break;
786 
787 	case EV_SND:
788 		bit_change(evdev->ev_snd_states, code, value);
789 		break;
790 
791 	case EV_SW:
792 		if (bit_test(evdev->ev_sw_states, code) == value)
793 			return (EV_SKIP_EVENT);
794 		bit_change(evdev->ev_sw_states, code, value);
795 		break;
796 
797 	case EV_REP:
798 		if (evdev->ev_rep[code] == value)
799 			return (EV_SKIP_EVENT);
800 		evdev_set_repeat_params(evdev, code, value);
801 		break;
802 
803 	case EV_REL:
804 		if (value == 0)
805 			return (EV_SKIP_EVENT);
806 		break;
807 
808 	/* For EV_ABS, save last value in absinfo and ev_mt_states */
809 	case EV_ABS:
810 		switch (code) {
811 		case ABS_MT_SLOT:
812 			/* Postpone ABS_MT_SLOT till next event */
813 			evdev_mt_set_last_slot(evdev, value);
814 			return (EV_SKIP_EVENT);
815 
816 		case ABS_MT_FIRST ... ABS_MT_LAST:
817 			/* Pass MT protocol type A events as is */
818 			if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
819 				break;
820 			/* Don`t repeat MT protocol type B events */
821 			last_mt_slot = evdev_mt_get_last_slot(evdev);
822 			if (evdev_mt_get_value(evdev, last_mt_slot, code)
823 			     == value)
824 				return (EV_SKIP_EVENT);
825 			evdev_mt_set_value(evdev, last_mt_slot, code, value);
826 			if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
827 				CURRENT_MT_SLOT(evdev) = last_mt_slot;
828 				evdev->ev_report_opened = true;
829 				return (EV_REPORT_MT_SLOT);
830 			}
831 			break;
832 
833 		default:
834 			if (evdev->ev_absinfo[code].value == value)
835 				return (EV_SKIP_EVENT);
836 			evdev->ev_absinfo[code].value = value;
837 		}
838 		break;
839 
840 	case EV_SYN:
841 		if (code == SYN_REPORT) {
842 			/* Count empty reports as well as non empty */
843 			evdev->ev_report_count++;
844 			/* Skip empty reports */
845 			if (!evdev->ev_report_opened)
846 				return (EV_SKIP_EVENT);
847 			evdev->ev_report_opened = false;
848 			return (EV_REPORT_EVENT);
849 		}
850 		break;
851 	}
852 
853 	evdev->ev_report_opened = true;
854 	return (EV_REPORT_EVENT);
855 }
856 
857 static void
evdev_propagate_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)858 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
859     int32_t value)
860 {
861 	struct epoch_tracker et;
862 	struct evdev_client *client;
863 
864 	debugf(evdev, "%s pushed event %d/%d/%d",
865 	    evdev->ev_shortname, type, code, value);
866 
867 	EVDEV_LOCK_ASSERT(evdev);
868 
869 	/* Propagate event through all clients */
870 	if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
871 		epoch_enter_preempt(INPUT_EPOCH, &et);
872 
873 	KASSERT(
874 	    evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0,
875 	    ("Input epoch has not been entered\n"));
876 
877 	CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) {
878 		if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
879 			continue;
880 
881 		EVDEV_CLIENT_LOCKQ(client);
882 		evdev_client_push(client, type, code, value);
883 		if (type == EV_SYN && code == SYN_REPORT)
884 			evdev_notify_event(client);
885 		EVDEV_CLIENT_UNLOCKQ(client);
886 	}
887 	if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
888 		epoch_exit_preempt(INPUT_EPOCH, &et);
889 
890 	evdev->ev_event_count++;
891 }
892 
893 void
evdev_send_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)894 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
895     int32_t value)
896 {
897 	enum evdev_sparse_result sparse;
898 
899 	EVDEV_LOCK_ASSERT(evdev);
900 
901 	evdev_modify_event(evdev, type, code, &value);
902 	sparse =  evdev_sparse_event(evdev, type, code, value);
903 	switch (sparse) {
904 	case EV_REPORT_MT_SLOT:
905 		/* report postponed ABS_MT_SLOT */
906 		evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
907 		    CURRENT_MT_SLOT(evdev));
908 		/* FALLTHROUGH */
909 	case EV_REPORT_EVENT:
910 		evdev_propagate_event(evdev, type, code, value);
911 		/* FALLTHROUGH */
912 	case EV_SKIP_EVENT:
913 		break;
914 	}
915 }
916 
917 void
evdev_restore_after_kdb(struct evdev_dev * evdev)918 evdev_restore_after_kdb(struct evdev_dev *evdev)
919 {
920 	int code;
921 
922 	EVDEV_LOCK_ASSERT(evdev);
923 
924 	/* Report postponed leds */
925 	bit_foreach(evdev->ev_kdb_led_states, LED_CNT, code)
926 		evdev_send_event(evdev, EV_LED, code,
927 		    !bit_test(evdev->ev_led_states, code));
928 	bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
929 
930 	/* Release stuck keys (CTRL + ALT + ESC) */
931 	evdev_stop_repeat(evdev);
932 	bit_foreach(evdev->ev_key_states, KEY_CNT, code)
933 		evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
934 	evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
935 }
936 
937 int
evdev_push_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)938 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
939     int32_t value)
940 {
941 
942 	if (evdev_check_event(evdev, type, code, value) != 0)
943 		return (EINVAL);
944 
945 	/*
946 	 * Discard all but LEDs kdb events as unrelated to userspace.
947 	 * Aggregate LED updates and postpone reporting until kdb deactivation.
948 	 */
949 	if (kdb_active || SCHEDULER_STOPPED()) {
950 		evdev->ev_kdb_active = true;
951 		if (type == EV_LED)
952 			bit_set(evdev->ev_kdb_led_states,
953 			    bit_test(evdev->ev_led_states, code) != value);
954 		return (0);
955 	}
956 
957 	EVDEV_ENTER(evdev);
958 
959 	/* Fix evdev state corrupted with discarding of kdb events */
960 	if (evdev->ev_kdb_active) {
961 		evdev->ev_kdb_active = false;
962 		evdev_restore_after_kdb(evdev);
963 	}
964 
965 	if (type == EV_SYN && code == SYN_REPORT &&
966 	    bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
967 		evdev_mt_sync_frame(evdev);
968 	else
969 		if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) &&
970 		    evdev_mt_record_event(evdev, type, code, value))
971 			goto exit;
972 
973 	evdev_send_event(evdev, type, code, value);
974 exit:
975 	EVDEV_EXIT(evdev);
976 
977 	return (0);
978 }
979 
980 int
evdev_inject_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)981 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
982     int32_t value)
983 {
984 	struct epoch_tracker et;
985 	int ret = 0;
986 
987 	switch (type) {
988 	case EV_REP:
989 		/* evdev repeats should not be processed by hardware driver */
990 		if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
991 			goto push;
992 		/* FALLTHROUGH */
993 	case EV_LED:
994 	case EV_MSC:
995 	case EV_SND:
996 	case EV_FF:
997 		if (evdev->ev_methods != NULL &&
998 		    evdev->ev_methods->ev_event != NULL)
999 			evdev->ev_methods->ev_event(evdev, type, code, value);
1000 		/*
1001 		 * Leds and driver repeats should be reported in ev_event
1002 		 * method body to interoperate with kbdmux states and rates
1003 		 * propagation so both ways (ioctl and evdev) of changing it
1004 		 * will produce only one evdev event report to client.
1005 		 */
1006 		if (type == EV_LED || type == EV_REP)
1007 			break;
1008 		/* FALLTHROUGH */
1009 	case EV_SYN:
1010 	case EV_KEY:
1011 	case EV_REL:
1012 	case EV_ABS:
1013 	case EV_SW:
1014 push:
1015 		if (evdev->ev_lock_type == EV_LOCK_MTX)
1016 			EVDEV_LOCK(evdev);
1017 		else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1018 			epoch_enter_preempt(INPUT_EPOCH, &et);
1019 		ret = evdev_push_event(evdev, type,  code, value);
1020 		if (evdev->ev_lock_type == EV_LOCK_MTX)
1021 			EVDEV_UNLOCK(evdev);
1022 		else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1023 			epoch_exit_preempt(INPUT_EPOCH, &et);
1024 
1025 		break;
1026 
1027 	default:
1028 		ret = EINVAL;
1029 	}
1030 
1031 	return (ret);
1032 }
1033 
1034 int
evdev_register_client(struct evdev_dev * evdev,struct evdev_client * client)1035 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
1036 {
1037 	int ret = 0;
1038 
1039 	debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
1040 
1041 	EVDEV_LIST_LOCK_ASSERT(evdev);
1042 
1043 	if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
1044 	    evdev->ev_methods->ev_open != NULL) {
1045 		debugf(evdev, "calling ev_open() on device %s",
1046 		    evdev->ev_shortname);
1047 		ret = evdev->ev_methods->ev_open(evdev);
1048 	}
1049 	if (ret == 0)
1050 		CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
1051 	return (ret);
1052 }
1053 
1054 void
evdev_dispose_client(struct evdev_dev * evdev,struct evdev_client * client)1055 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
1056 {
1057 	debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1058 
1059 	EVDEV_LIST_LOCK_ASSERT(evdev);
1060 
1061 	CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link);
1062 	if (CK_SLIST_EMPTY(&evdev->ev_clients)) {
1063 		if (evdev->ev_methods != NULL &&
1064 		    evdev->ev_methods->ev_close != NULL)
1065 			(void)evdev->ev_methods->ev_close(evdev);
1066 		if (evdev_event_supported(evdev, EV_REP) &&
1067 		    bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
1068 			if (evdev->ev_lock_type != EV_LOCK_MTX)
1069 				EVDEV_LOCK(evdev);
1070 			evdev_stop_repeat(evdev);
1071 			if (evdev->ev_lock_type != EV_LOCK_MTX)
1072 				EVDEV_UNLOCK(evdev);
1073 		}
1074 	}
1075 	if (evdev->ev_lock_type != EV_LOCK_MTX)
1076 		EVDEV_LOCK(evdev);
1077 	evdev_release_client(evdev, client);
1078 	if (evdev->ev_lock_type != EV_LOCK_MTX)
1079 		EVDEV_UNLOCK(evdev);
1080 }
1081 
1082 int
evdev_grab_client(struct evdev_dev * evdev,struct evdev_client * client)1083 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1084 {
1085 
1086 	EVDEV_LOCK_ASSERT(evdev);
1087 
1088 	if (evdev->ev_grabber != NULL)
1089 		return (EBUSY);
1090 
1091 	evdev->ev_grabber = client;
1092 
1093 	return (0);
1094 }
1095 
1096 int
evdev_release_client(struct evdev_dev * evdev,struct evdev_client * client)1097 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1098 {
1099 
1100 	EVDEV_LOCK_ASSERT(evdev);
1101 
1102 	if (evdev->ev_grabber != client)
1103 		return (EINVAL);
1104 
1105 	evdev->ev_grabber = NULL;
1106 
1107 	return (0);
1108 }
1109 
1110 bool
evdev_is_grabbed(struct evdev_dev * evdev)1111 evdev_is_grabbed(struct evdev_dev *evdev)
1112 {
1113 	if (kdb_active || SCHEDULER_STOPPED())
1114 		return (false);
1115 	/*
1116 	 * The function is intended to be called from evdev-unrelated parts of
1117 	 * code like syscons-compatible parts of mouse and keyboard drivers.
1118 	 * That makes unlocked read-only access acceptable.
1119 	 */
1120 	return (evdev->ev_grabber != NULL);
1121 }
1122 
1123 static void
evdev_repeat_callout(void * arg)1124 evdev_repeat_callout(void *arg)
1125 {
1126 	struct epoch_tracker et;
1127 	struct evdev_dev *evdev = (struct evdev_dev *)arg;
1128 
1129 	if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1130 		epoch_enter_preempt(INPUT_EPOCH, &et);
1131 	evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1132 	evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1133 	if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1134 		epoch_exit_preempt(INPUT_EPOCH, &et);
1135 
1136 	if (evdev->ev_rep[REP_PERIOD])
1137 		callout_reset(&evdev->ev_rep_callout,
1138 		    evdev->ev_rep[REP_PERIOD] * hz / 1000,
1139 		    evdev_repeat_callout, evdev);
1140 	else
1141 		evdev->ev_rep_key = KEY_RESERVED;
1142 }
1143 
1144 static void
evdev_start_repeat(struct evdev_dev * evdev,uint16_t key)1145 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1146 {
1147 
1148 	EVDEV_LOCK_ASSERT(evdev);
1149 
1150 	if (evdev->ev_rep[REP_DELAY]) {
1151 		evdev->ev_rep_key = key;
1152 		callout_reset(&evdev->ev_rep_callout,
1153 		    evdev->ev_rep[REP_DELAY] * hz / 1000,
1154 		    evdev_repeat_callout, evdev);
1155 	}
1156 }
1157 
1158 static void
evdev_stop_repeat(struct evdev_dev * evdev)1159 evdev_stop_repeat(struct evdev_dev *evdev)
1160 {
1161 
1162 	EVDEV_LOCK_ASSERT(evdev);
1163 
1164 	if (evdev->ev_rep_key != KEY_RESERVED) {
1165 		callout_stop(&evdev->ev_rep_callout);
1166 		evdev->ev_rep_key = KEY_RESERVED;
1167 	}
1168 }
1169 
1170 MODULE_VERSION(evdev, 1);
1171