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);
97 bit_change(bitstr_t *bitstr, int bit, int value)
100 bit_set(bitstr, bit);
102 bit_clear(bitstr, bit);
109 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
113 evdev_free(struct evdev_dev *evdev)
116 if (evdev != NULL && evdev->ev_cdev != NULL &&
117 evdev->ev_cdev->si_drv1 != NULL)
118 evdev_unregister(evdev);
120 free(evdev, M_EVDEV);
123 static struct input_absinfo *
124 evdev_alloc_absinfo(void)
127 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
132 evdev_free_absinfo(struct input_absinfo *absinfo)
135 free(absinfo, M_EVDEV);
139 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
141 if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
142 MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
145 evdev->ev_report_size = report_size;
150 evdev_estimate_report_size(struct evdev_dev *evdev)
156 * Keyboards generate one event per report but other devices with
157 * buttons like mouses can report events simultaneously
159 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
161 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
163 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
166 /* All relative axes can be reported simultaneously */
167 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
171 * All absolute axes can be reported simultaneously.
172 * Multitouch axes can be reported ABS_MT_SLOT times
174 if (evdev->ev_absinfo != NULL) {
175 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
177 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
179 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */
180 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
182 size += res * MAXIMAL_MT_SLOT(evdev);
185 size += res * (MAX_MT_REPORTS - 1);
189 /* All misc events can be reported simultaneously */
190 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
193 /* All leds can be reported simultaneously */
194 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
197 /* Assume other events are generated once per report */
198 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
201 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
204 /* XXX: FF part is not implemented yet */
206 size++; /* SYN_REPORT */
211 evdev_sysctl_create(struct evdev_dev *evdev)
213 struct sysctl_oid *ev_sysctl_tree;
216 snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
217 sysctl_ctx_init(&evdev->ev_sysctl_ctx);
219 ev_sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&evdev->ev_sysctl_ctx,
220 SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO,
221 ev_unit_str, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "",
224 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
225 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
227 "Input device name");
229 SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
230 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
231 &evdev->ev_id, input_id,
232 "Input device identification");
234 /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
235 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
236 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
237 evdev->ev_shortname, 0,
238 "Input device short name");
240 /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
241 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
242 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
244 "Input device unique number");
246 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
247 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
248 evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
249 "Input device properties");
251 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
252 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
253 evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
254 "Input device supported events types");
256 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
257 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
258 evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
259 "", "Input device supported keys");
261 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
262 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
263 evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
264 "Input device supported relative events");
266 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
267 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
268 evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
269 "Input device supported absolute events");
271 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
272 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
273 evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
274 "Input device supported miscellaneous events");
276 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
277 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
278 evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
279 "Input device supported LED events");
281 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
282 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
283 evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
284 "Input device supported sound events");
286 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
287 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
288 evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
289 "Input device supported switch events");
293 evdev_register_common(struct evdev_dev *evdev)
297 debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
298 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
300 /* Initialize internal structures */
301 CK_SLIST_INIT(&evdev->ev_clients);
302 sx_init(&evdev->ev_list_lock, "evsx");
304 if (evdev_event_supported(evdev, EV_REP) &&
305 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
306 /* Initialize callout */
307 callout_init_mtx(&evdev->ev_rep_callout,
308 evdev->ev_state_lock, 0);
310 if (evdev->ev_rep[REP_DELAY] == 0 &&
311 evdev->ev_rep[REP_PERIOD] == 0) {
312 /* Supply default values */
313 evdev->ev_rep[REP_DELAY] = 250;
314 evdev->ev_rep[REP_PERIOD] = 33;
318 /* Initialize multitouch protocol type B states */
319 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
320 evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
321 evdev_mt_init(evdev);
323 /* Estimate maximum report size */
324 if (evdev->ev_report_size == 0) {
325 ret = evdev_set_report_size(evdev,
326 evdev_estimate_report_size(evdev));
331 /* Create char device node */
332 ret = evdev_cdev_create(evdev);
336 /* Create sysctls (for device enumeration without /dev/input access rights) */
337 evdev_sysctl_create(evdev);
341 sx_destroy(&evdev->ev_list_lock);
346 evdev_register(struct evdev_dev *evdev)
350 if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH))
351 evdev->ev_lock_type = EV_LOCK_EXT_EPOCH;
353 evdev->ev_lock_type = EV_LOCK_INTERNAL;
354 evdev->ev_state_lock = &evdev->ev_mtx;
355 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
357 ret = evdev_register_common(evdev);
359 mtx_destroy(&evdev->ev_mtx);
365 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
368 evdev->ev_lock_type = EV_LOCK_MTX;
369 evdev->ev_state_lock = mtx;
370 return (evdev_register_common(evdev));
374 evdev_unregister(struct evdev_dev *evdev)
376 struct evdev_client *client, *tmp;
378 debugf(evdev, "%s: unregistered evdev provider: %s\n",
379 evdev->ev_shortname, evdev->ev_name);
381 sysctl_ctx_free(&evdev->ev_sysctl_ctx);
383 EVDEV_LIST_LOCK(evdev);
384 evdev->ev_cdev->si_drv1 = NULL;
385 /* Wake up sleepers */
386 CK_SLIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) {
387 evdev_revoke_client(client);
388 evdev_dispose_client(evdev, client);
389 EVDEV_CLIENT_LOCKQ(client);
390 evdev_notify_event(client);
391 EVDEV_CLIENT_UNLOCKQ(client);
393 EVDEV_LIST_UNLOCK(evdev);
395 /* release lock to avoid deadlock with evdev_dtor */
396 ret = evdev_cdev_destroy(evdev);
397 evdev->ev_cdev = NULL;
398 sx_destroy(&evdev->ev_list_lock);
399 if (ret == 0 && evdev->ev_lock_type != EV_LOCK_MTX)
400 mtx_destroy(&evdev->ev_mtx);
402 evdev_free_absinfo(evdev->ev_absinfo);
403 evdev_mt_free(evdev);
409 evdev_set_name(struct evdev_dev *evdev, const char *name)
412 snprintf(evdev->ev_name, NAMELEN, "%s", name);
416 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
417 uint16_t product, uint16_t version)
420 evdev->ev_id = (struct input_id) {
429 evdev_set_phys(struct evdev_dev *evdev, const char *name)
432 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
436 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
439 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
443 evdev_set_methods(struct evdev_dev *evdev, void *softc,
444 const struct evdev_methods *methods)
447 evdev->ev_methods = methods;
448 evdev->ev_softc = softc;
452 evdev_get_softc(struct evdev_dev *evdev)
455 return (evdev->ev_softc);
459 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
462 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
463 bit_set(evdev->ev_prop_flags, prop);
467 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
470 KASSERT(type < EV_CNT, ("invalid evdev event property"));
471 bit_set(evdev->ev_type_flags, type);
475 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
478 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
479 bit_set(evdev->ev_key_flags, code);
483 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
486 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
487 bit_set(evdev->ev_rel_flags, code);
491 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t minimum,
492 int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution)
494 struct input_absinfo absinfo;
496 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
498 absinfo = (struct input_absinfo) {
504 .resolution = resolution,
506 evdev_set_abs_bit(evdev, code);
507 evdev_set_absinfo(evdev, code, &absinfo);
511 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
514 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
515 if (evdev->ev_absinfo == NULL)
516 evdev->ev_absinfo = evdev_alloc_absinfo();
517 bit_set(evdev->ev_abs_flags, code);
521 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
524 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
525 bit_set(evdev->ev_msc_flags, code);
530 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
533 KASSERT(code < LED_CNT, ("invalid evdev led property"));
534 bit_set(evdev->ev_led_flags, code);
538 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
541 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
542 bit_set(evdev->ev_snd_flags, code);
546 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
549 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
550 bit_set(evdev->ev_sw_flags, code);
554 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
557 KASSERT(type < EV_CNT, ("invalid evdev event property"));
558 return (bit_test(evdev->ev_type_flags, type));
562 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
563 struct input_absinfo *absinfo)
566 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
568 if (axis == ABS_MT_SLOT &&
569 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
572 if (evdev->ev_absinfo == NULL)
573 evdev->ev_absinfo = evdev_alloc_absinfo();
575 if (axis == ABS_MT_SLOT)
576 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
578 memcpy(&evdev->ev_absinfo[axis], absinfo,
579 sizeof(struct input_absinfo));
583 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
586 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
587 evdev->ev_rep[property] = value;
591 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
594 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
595 bit_set(evdev->ev_flags, flag);
599 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
606 /* Allow SYN events implicitly */
607 if (type != EV_SYN && !evdev_event_supported(evdev, type))
619 if (!bit_test(evdev->ev_key_flags, code))
626 if (!bit_test(evdev->ev_rel_flags, code))
633 if (!bit_test(evdev->ev_abs_flags, code))
635 if (code == ABS_MT_SLOT &&
636 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
638 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
639 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
646 if (!bit_test(evdev->ev_msc_flags, code))
653 if (!bit_test(evdev->ev_led_flags, code))
660 if (!bit_test(evdev->ev_snd_flags, code))
667 if (!bit_test(evdev->ev_sw_flags, code))
684 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
687 int32_t fuzz, old_value, abs_change;
689 EVDEV_LOCK_ASSERT(evdev);
693 if (!evdev_event_supported(evdev, EV_REP))
696 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
697 /* Detect driver key repeats. */
698 if (bit_test(evdev->ev_key_states, code) &&
699 *value == KEY_EVENT_DOWN)
700 *value = KEY_EVENT_REPEAT;
702 /* Start/stop callout for evdev repeats */
703 if (bit_test(evdev->ev_key_states, code) == !*value &&
704 !CK_SLIST_EMPTY(&evdev->ev_clients)) {
705 if (*value == KEY_EVENT_DOWN)
706 evdev_start_repeat(evdev, code);
708 evdev_stop_repeat(evdev);
714 fuzz = evdev->ev_absinfo[code].fuzz;
715 if (fuzz == 0 || code == ABS_MT_SLOT)
717 else if (!ABS_IS_MT(code))
718 old_value = evdev->ev_absinfo[code].value;
719 else if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
720 old_value = evdev_get_mt_value(evdev,
721 evdev_get_last_mt_slot(evdev), code);
722 else /* Pass MT protocol type A events as is */
725 abs_change = abs(*value - old_value);
726 if (abs_change < fuzz / 2)
728 else if (abs_change < fuzz)
729 *value = (old_value * 3 + *value) / 4;
730 else if (abs_change < fuzz * 2)
731 *value = (old_value + *value) / 2;
736 static enum evdev_sparse_result
737 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
740 int32_t last_mt_slot;
742 EVDEV_LOCK_ASSERT(evdev);
745 * For certain event types, update device state bits
746 * and convert level reporting to edge reporting
753 if (bit_test(evdev->ev_key_states, code) == value)
754 return (EV_SKIP_EVENT);
755 bit_change(evdev->ev_key_states, code, value);
758 case KEY_EVENT_REPEAT:
759 if (bit_test(evdev->ev_key_states, code) == 0 ||
760 !evdev_event_supported(evdev, EV_REP))
761 return (EV_SKIP_EVENT);
765 return (EV_SKIP_EVENT);
770 if (bit_test(evdev->ev_led_states, code) == value)
771 return (EV_SKIP_EVENT);
772 bit_change(evdev->ev_led_states, code, value);
776 bit_change(evdev->ev_snd_states, code, value);
780 if (bit_test(evdev->ev_sw_states, code) == value)
781 return (EV_SKIP_EVENT);
782 bit_change(evdev->ev_sw_states, code, value);
786 if (evdev->ev_rep[code] == value)
787 return (EV_SKIP_EVENT);
788 evdev_set_repeat_params(evdev, code, value);
793 return (EV_SKIP_EVENT);
796 /* For EV_ABS, save last value in absinfo and ev_mt_states */
800 /* Postpone ABS_MT_SLOT till next event */
801 evdev_set_last_mt_slot(evdev, value);
802 return (EV_SKIP_EVENT);
804 case ABS_MT_FIRST ... ABS_MT_LAST:
805 /* Pass MT protocol type A events as is */
806 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
808 /* Don`t repeat MT protocol type B events */
809 last_mt_slot = evdev_get_last_mt_slot(evdev);
810 if (evdev_get_mt_value(evdev, last_mt_slot, code)
812 return (EV_SKIP_EVENT);
813 evdev_set_mt_value(evdev, last_mt_slot, code, value);
814 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
815 CURRENT_MT_SLOT(evdev) = last_mt_slot;
816 evdev->ev_report_opened = true;
817 return (EV_REPORT_MT_SLOT);
822 if (evdev->ev_absinfo[code].value == value)
823 return (EV_SKIP_EVENT);
824 evdev->ev_absinfo[code].value = value;
829 if (code == SYN_REPORT) {
830 /* Count empty reports as well as non empty */
831 evdev->ev_report_count++;
832 /* Skip empty reports */
833 if (!evdev->ev_report_opened)
834 return (EV_SKIP_EVENT);
835 evdev->ev_report_opened = false;
836 return (EV_REPORT_EVENT);
841 evdev->ev_report_opened = true;
842 return (EV_REPORT_EVENT);
846 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
849 struct epoch_tracker et;
850 struct evdev_client *client;
852 debugf(evdev, "%s pushed event %d/%d/%d",
853 evdev->ev_shortname, type, code, value);
855 EVDEV_LOCK_ASSERT(evdev);
857 /* Propagate event through all clients */
858 if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
859 epoch_enter_preempt(INPUT_EPOCH, &et);
862 evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0,
863 ("Input epoch has not been entered\n"));
865 CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) {
866 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
869 EVDEV_CLIENT_LOCKQ(client);
870 evdev_client_push(client, type, code, value);
871 if (type == EV_SYN && code == SYN_REPORT)
872 evdev_notify_event(client);
873 EVDEV_CLIENT_UNLOCKQ(client);
875 if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
876 epoch_exit_preempt(INPUT_EPOCH, &et);
878 evdev->ev_event_count++;
882 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
885 enum evdev_sparse_result sparse;
887 EVDEV_LOCK_ASSERT(evdev);
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 for (code = 0; code < LED_CNT; code++)
913 if (bit_test(evdev->ev_kdb_led_states, code))
914 evdev_send_event(evdev, EV_LED, code,
915 !bit_test(evdev->ev_led_states, code));
916 bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
918 /* Release stuck keys (CTRL + ALT + ESC) */
919 evdev_stop_repeat(evdev);
920 for (code = 0; code < KEY_CNT; code++) {
921 if (bit_test(evdev->ev_key_states, code)) {
922 evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
923 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
929 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
933 if (evdev_check_event(evdev, type, code, value) != 0)
937 * Discard all but LEDs kdb events as unrelated to userspace.
938 * Aggregate LED updates and postpone reporting until kdb deactivation.
940 if (kdb_active || SCHEDULER_STOPPED()) {
941 evdev->ev_kdb_active = true;
943 bit_set(evdev->ev_kdb_led_states,
944 bit_test(evdev->ev_led_states, code) != value);
950 /* Fix evdev state corrupted with discarding of kdb events */
951 if (evdev->ev_kdb_active) {
952 evdev->ev_kdb_active = false;
953 evdev_restore_after_kdb(evdev);
956 evdev_modify_event(evdev, type, code, &value);
957 if (type == EV_SYN && code == SYN_REPORT &&
958 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
959 evdev_send_mt_autorel(evdev);
960 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
961 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
962 evdev_send_mt_compat(evdev);
963 evdev_send_event(evdev, type, code, value);
971 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
974 struct epoch_tracker et;
979 /* evdev repeats should not be processed by hardware driver */
980 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
987 if (evdev->ev_methods != NULL &&
988 evdev->ev_methods->ev_event != NULL)
989 evdev->ev_methods->ev_event(evdev, type, code, value);
991 * Leds and driver repeats should be reported in ev_event
992 * method body to interoperate with kbdmux states and rates
993 * propagation so both ways (ioctl and evdev) of changing it
994 * will produce only one evdev event report to client.
996 if (type == EV_LED || type == EV_REP)
1005 if (evdev->ev_lock_type == EV_LOCK_MTX)
1007 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1008 epoch_enter_preempt(INPUT_EPOCH, &et);
1009 ret = evdev_push_event(evdev, type, code, value);
1010 if (evdev->ev_lock_type == EV_LOCK_MTX)
1011 EVDEV_UNLOCK(evdev);
1012 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1013 epoch_exit_preempt(INPUT_EPOCH, &et);
1025 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
1029 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
1031 EVDEV_LIST_LOCK_ASSERT(evdev);
1033 if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
1034 evdev->ev_methods->ev_open != NULL) {
1035 debugf(evdev, "calling ev_open() on device %s",
1036 evdev->ev_shortname);
1037 ret = evdev->ev_methods->ev_open(evdev);
1040 CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
1045 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
1047 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1049 EVDEV_LIST_LOCK_ASSERT(evdev);
1051 CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link);
1052 if (CK_SLIST_EMPTY(&evdev->ev_clients)) {
1053 if (evdev->ev_methods != NULL &&
1054 evdev->ev_methods->ev_close != NULL)
1055 (void)evdev->ev_methods->ev_close(evdev);
1056 if (evdev_event_supported(evdev, EV_REP) &&
1057 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
1058 if (evdev->ev_lock_type != EV_LOCK_MTX)
1060 evdev_stop_repeat(evdev);
1061 if (evdev->ev_lock_type != EV_LOCK_MTX)
1062 EVDEV_UNLOCK(evdev);
1065 if (evdev->ev_lock_type != EV_LOCK_MTX)
1067 evdev_release_client(evdev, client);
1068 if (evdev->ev_lock_type != EV_LOCK_MTX)
1069 EVDEV_UNLOCK(evdev);
1073 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1076 EVDEV_LOCK_ASSERT(evdev);
1078 if (evdev->ev_grabber != NULL)
1081 evdev->ev_grabber = client;
1087 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1090 EVDEV_LOCK_ASSERT(evdev);
1092 if (evdev->ev_grabber != client)
1095 evdev->ev_grabber = NULL;
1101 evdev_repeat_callout(void *arg)
1103 struct epoch_tracker et;
1104 struct evdev_dev *evdev = (struct evdev_dev *)arg;
1106 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1107 epoch_enter_preempt(INPUT_EPOCH, &et);
1108 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1109 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1110 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1111 epoch_exit_preempt(INPUT_EPOCH, &et);
1113 if (evdev->ev_rep[REP_PERIOD])
1114 callout_reset(&evdev->ev_rep_callout,
1115 evdev->ev_rep[REP_PERIOD] * hz / 1000,
1116 evdev_repeat_callout, evdev);
1118 evdev->ev_rep_key = KEY_RESERVED;
1122 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1125 EVDEV_LOCK_ASSERT(evdev);
1127 if (evdev->ev_rep[REP_DELAY]) {
1128 evdev->ev_rep_key = key;
1129 callout_reset(&evdev->ev_rep_callout,
1130 evdev->ev_rep[REP_DELAY] * hz / 1000,
1131 evdev_repeat_callout, evdev);
1136 evdev_stop_repeat(struct evdev_dev *evdev)
1139 EVDEV_LOCK_ASSERT(evdev);
1141 if (evdev->ev_rep_key != KEY_RESERVED) {
1142 callout_stop(&evdev->ev_rep_callout);
1143 evdev->ev_rep_key = KEY_RESERVED;
1147 MODULE_VERSION(evdev, 1);