2 * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3 * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #include "opt_evdev.h"
32 #include <sys/param.h>
33 #include <sys/bitstring.h>
36 #include <sys/epoch.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
46 #include <dev/evdev/evdev.h>
47 #include <dev/evdev/evdev_private.h>
48 #include <dev/evdev/input.h>
51 #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args)
53 #define debugf(evdev, fmt, args...)
57 FEATURE(evdev, "Input event devices support");
59 FEATURE(evdev_support, "Evdev support in hybrid drivers");
63 enum evdev_sparse_result
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 */
70 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
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;
76 int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD;
78 int evdev_sysmouse_t_axis = 0;
80 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
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");
89 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
90 "Evdev input devices");
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);
100 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
104 evdev_free(struct evdev_dev *evdev)
107 if (evdev != NULL && evdev->ev_cdev != NULL &&
108 evdev->ev_cdev->si_drv1 != NULL)
109 evdev_unregister(evdev);
111 free(evdev, M_EVDEV);
114 static struct input_absinfo *
115 evdev_alloc_absinfo(void)
118 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
123 evdev_free_absinfo(struct input_absinfo *absinfo)
126 free(absinfo, M_EVDEV);
130 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
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)
136 evdev->ev_report_size = report_size;
141 evdev_estimate_report_size(struct evdev_dev *evdev)
147 * Keyboards generate one event per report but other devices with
148 * buttons like mouses can report events simultaneously
150 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
152 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
154 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
157 /* All relative axes can be reported simultaneously */
158 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
162 * All absolute axes can be reported simultaneously.
163 * Multitouch axes can be reported ABS_MT_SLOT times
165 if (evdev->ev_absinfo != NULL) {
166 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
168 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
170 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */
171 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
173 size += res * MAXIMAL_MT_SLOT(evdev);
176 size += res * (MAX_MT_REPORTS - 1);
180 /* All misc events can be reported simultaneously */
181 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
184 /* All leds can be reported simultaneously */
185 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
188 /* Assume other events are generated once per report */
189 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
192 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
195 /* XXX: FF part is not implemented yet */
197 size++; /* SYN_REPORT */
202 evdev_sysctl_create(struct evdev_dev *evdev)
204 struct sysctl_oid *ev_sysctl_tree;
207 snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
208 sysctl_ctx_init(&evdev->ev_sysctl_ctx);
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, "",
215 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
216 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
218 "Input device name");
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");
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");
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,
235 "Input device unique number");
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");
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");
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");
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");
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");
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");
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");
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");
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");
284 evdev_register_common(struct evdev_dev *evdev)
288 debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
289 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
291 /* Initialize internal structures */
292 CK_SLIST_INIT(&evdev->ev_clients);
293 sx_init(&evdev->ev_list_lock, "evsx");
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);
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;
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);
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));
322 /* Create char device node */
323 ret = evdev_cdev_create(evdev);
327 /* Create sysctls (for device enumeration without /dev/input access rights) */
328 evdev_sysctl_create(evdev);
332 sx_destroy(&evdev->ev_list_lock);
337 evdev_register(struct evdev_dev *evdev)
341 if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH))
342 evdev->ev_lock_type = EV_LOCK_EXT_EPOCH;
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);
348 ret = evdev_register_common(evdev);
350 mtx_destroy(&evdev->ev_mtx);
356 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
359 evdev->ev_lock_type = EV_LOCK_MTX;
360 evdev->ev_state_lock = mtx;
361 return (evdev_register_common(evdev));
365 evdev_unregister(struct evdev_dev *evdev)
367 struct evdev_client *client, *tmp;
369 debugf(evdev, "%s: unregistered evdev provider: %s\n",
370 evdev->ev_shortname, evdev->ev_name);
372 sysctl_ctx_free(&evdev->ev_sysctl_ctx);
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);
384 EVDEV_LIST_UNLOCK(evdev);
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);
393 evdev_free_absinfo(evdev->ev_absinfo);
394 evdev_mt_free(evdev);
400 evdev_set_name(struct evdev_dev *evdev, const char *name)
403 snprintf(evdev->ev_name, NAMELEN, "%s", name);
407 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
408 uint16_t product, uint16_t version)
411 evdev->ev_id = (struct input_id) {
420 evdev_set_phys(struct evdev_dev *evdev, const char *name)
423 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
427 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
430 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
434 evdev_set_methods(struct evdev_dev *evdev, void *softc,
435 const struct evdev_methods *methods)
438 evdev->ev_methods = methods;
439 evdev->ev_softc = softc;
443 evdev_get_softc(struct evdev_dev *evdev)
446 return (evdev->ev_softc);
450 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
453 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
454 bit_set(evdev->ev_prop_flags, prop);
458 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
461 KASSERT(type < EV_CNT, ("invalid evdev event property"));
462 bit_set(evdev->ev_type_flags, type);
466 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
469 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
470 bit_set(evdev->ev_key_flags, code);
474 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
477 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
478 bit_set(evdev->ev_rel_flags, code);
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)
485 struct input_absinfo absinfo;
487 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
489 absinfo = (struct input_absinfo) {
495 .resolution = resolution,
497 evdev_set_abs_bit(evdev, code);
498 evdev_set_absinfo(evdev, code, &absinfo);
502 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
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);
512 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
515 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
516 bit_set(evdev->ev_msc_flags, code);
521 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
524 KASSERT(code < LED_CNT, ("invalid evdev led property"));
525 bit_set(evdev->ev_led_flags, code);
529 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
532 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
533 bit_set(evdev->ev_snd_flags, code);
537 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
540 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
541 bit_set(evdev->ev_sw_flags, code);
545 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
548 KASSERT(type < EV_CNT, ("invalid evdev event property"));
549 return (bit_test(evdev->ev_type_flags, type));
553 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
554 struct input_absinfo *absinfo)
557 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
559 if (axis == ABS_MT_SLOT &&
560 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
563 if (evdev->ev_absinfo == NULL)
564 evdev->ev_absinfo = evdev_alloc_absinfo();
566 if (axis == ABS_MT_SLOT)
567 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
569 memcpy(&evdev->ev_absinfo[axis], absinfo,
570 sizeof(struct input_absinfo));
574 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
577 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
578 evdev->ev_rep[property] = value;
582 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
585 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
586 bit_set(evdev->ev_flags, flag);
590 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
597 /* Allow SYN events implicitly */
598 if (type != EV_SYN && !evdev_event_supported(evdev, type))
610 if (!bit_test(evdev->ev_key_flags, code))
617 if (!bit_test(evdev->ev_rel_flags, code))
624 if (!bit_test(evdev->ev_abs_flags, code))
626 if (code == ABS_MT_SLOT &&
627 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
629 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
630 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
637 if (!bit_test(evdev->ev_msc_flags, code))
644 if (!bit_test(evdev->ev_led_flags, code))
651 if (!bit_test(evdev->ev_snd_flags, code))
658 if (!bit_test(evdev->ev_sw_flags, code))
675 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
678 int32_t fuzz, old_value, abs_change;
680 EVDEV_LOCK_ASSERT(evdev);
684 if (!evdev_event_supported(evdev, EV_REP))
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;
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);
699 evdev_stop_repeat(evdev);
705 if (code == ABS_MT_SLOT)
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 */
712 else if (code == ABS_MT_TRACKING_ID) {
713 *value = evdev_mt_reassign_id(evdev,
714 evdev_mt_get_last_slot(evdev), *value);
717 old_value = evdev_mt_get_value(evdev,
718 evdev_mt_get_last_slot(evdev), code);
720 fuzz = evdev->ev_absinfo[code].fuzz;
724 abs_change = abs(*value - old_value);
725 if (abs_change < fuzz / 2)
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;
735 static enum evdev_sparse_result
736 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
739 int32_t last_mt_slot;
741 EVDEV_LOCK_ASSERT(evdev);
744 * For certain event types, update device state bits
745 * and convert level reporting to edge reporting
752 if (bit_test(evdev->ev_key_states, code) == value)
753 return (EV_SKIP_EVENT);
754 bit_change(evdev->ev_key_states, code, value);
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);
764 return (EV_SKIP_EVENT);
769 if (bit_test(evdev->ev_led_states, code) == value)
770 return (EV_SKIP_EVENT);
771 bit_change(evdev->ev_led_states, code, value);
775 bit_change(evdev->ev_snd_states, code, value);
779 if (bit_test(evdev->ev_sw_states, code) == value)
780 return (EV_SKIP_EVENT);
781 bit_change(evdev->ev_sw_states, code, value);
785 if (evdev->ev_rep[code] == value)
786 return (EV_SKIP_EVENT);
787 evdev_set_repeat_params(evdev, code, value);
792 return (EV_SKIP_EVENT);
795 /* For EV_ABS, save last value in absinfo and ev_mt_states */
799 /* Postpone ABS_MT_SLOT till next event */
800 evdev_mt_set_last_slot(evdev, value);
801 return (EV_SKIP_EVENT);
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))
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)
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);
821 if (evdev->ev_absinfo[code].value == value)
822 return (EV_SKIP_EVENT);
823 evdev->ev_absinfo[code].value = value;
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);
840 evdev->ev_report_opened = true;
841 return (EV_REPORT_EVENT);
845 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
848 struct epoch_tracker et;
849 struct evdev_client *client;
851 debugf(evdev, "%s pushed event %d/%d/%d",
852 evdev->ev_shortname, type, code, value);
854 EVDEV_LOCK_ASSERT(evdev);
856 /* Propagate event through all clients */
857 if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
858 epoch_enter_preempt(INPUT_EPOCH, &et);
861 evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0,
862 ("Input epoch has not been entered\n"));
864 CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) {
865 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
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);
874 if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
875 epoch_exit_preempt(INPUT_EPOCH, &et);
877 evdev->ev_event_count++;
881 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
884 enum evdev_sparse_result sparse;
886 EVDEV_LOCK_ASSERT(evdev);
888 evdev_modify_event(evdev, type, code, &value);
889 sparse = evdev_sparse_event(evdev, type, code, value);
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));
896 case EV_REPORT_EVENT:
897 evdev_propagate_event(evdev, type, code, value);
905 evdev_restore_after_kdb(struct evdev_dev *evdev)
909 EVDEV_LOCK_ASSERT(evdev);
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);
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);
925 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
929 if (evdev_check_event(evdev, type, code, value) != 0)
933 * Discard all but LEDs kdb events as unrelated to userspace.
934 * Aggregate LED updates and postpone reporting until kdb deactivation.
936 if (kdb_active || SCHEDULER_STOPPED()) {
937 evdev->ev_kdb_active = true;
939 bit_set(evdev->ev_kdb_led_states,
940 bit_test(evdev->ev_led_states, code) != value);
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);
952 if (type == EV_SYN && code == SYN_REPORT &&
953 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
954 evdev_mt_sync_frame(evdev);
956 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) &&
957 evdev_mt_record_event(evdev, type, code, value))
960 evdev_send_event(evdev, type, code, value);
968 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
971 struct epoch_tracker et;
976 /* evdev repeats should not be processed by hardware driver */
977 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
984 if (evdev->ev_methods != NULL &&
985 evdev->ev_methods->ev_event != NULL)
986 evdev->ev_methods->ev_event(evdev, type, code, value);
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.
993 if (type == EV_LED || type == EV_REP)
1002 if (evdev->ev_lock_type == EV_LOCK_MTX)
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);
1022 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
1026 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
1028 EVDEV_LIST_LOCK_ASSERT(evdev);
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);
1037 CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
1042 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
1044 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1046 EVDEV_LIST_LOCK_ASSERT(evdev);
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)
1057 evdev_stop_repeat(evdev);
1058 if (evdev->ev_lock_type != EV_LOCK_MTX)
1059 EVDEV_UNLOCK(evdev);
1062 if (evdev->ev_lock_type != EV_LOCK_MTX)
1064 evdev_release_client(evdev, client);
1065 if (evdev->ev_lock_type != EV_LOCK_MTX)
1066 EVDEV_UNLOCK(evdev);
1070 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1073 EVDEV_LOCK_ASSERT(evdev);
1075 if (evdev->ev_grabber != NULL)
1078 evdev->ev_grabber = client;
1084 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1087 EVDEV_LOCK_ASSERT(evdev);
1089 if (evdev->ev_grabber != client)
1092 evdev->ev_grabber = NULL;
1098 evdev_is_grabbed(struct evdev_dev *evdev)
1101 * The function is intended to be called from evdev-unrelated parts of
1102 * code like syscons-compatible parts of mouse and keyboard drivers.
1103 * That makes unlocked read-only access acceptable.
1105 return (evdev->ev_grabber != NULL);
1109 evdev_repeat_callout(void *arg)
1111 struct epoch_tracker et;
1112 struct evdev_dev *evdev = (struct evdev_dev *)arg;
1114 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1115 epoch_enter_preempt(INPUT_EPOCH, &et);
1116 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1117 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1118 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1119 epoch_exit_preempt(INPUT_EPOCH, &et);
1121 if (evdev->ev_rep[REP_PERIOD])
1122 callout_reset(&evdev->ev_rep_callout,
1123 evdev->ev_rep[REP_PERIOD] * hz / 1000,
1124 evdev_repeat_callout, evdev);
1126 evdev->ev_rep_key = KEY_RESERVED;
1130 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1133 EVDEV_LOCK_ASSERT(evdev);
1135 if (evdev->ev_rep[REP_DELAY]) {
1136 evdev->ev_rep_key = key;
1137 callout_reset(&evdev->ev_rep_callout,
1138 evdev->ev_rep[REP_DELAY] * hz / 1000,
1139 evdev_repeat_callout, evdev);
1144 evdev_stop_repeat(struct evdev_dev *evdev)
1147 EVDEV_LOCK_ASSERT(evdev);
1149 if (evdev->ev_rep_key != KEY_RESERVED) {
1150 callout_stop(&evdev->ev_rep_callout);
1151 evdev->ev_rep_key = KEY_RESERVED;
1155 MODULE_VERSION(evdev, 1);