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 */
310 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
311 evdev_mt_init(evdev);
313 /* Estimate maximum report size */
314 if (evdev->ev_report_size == 0) {
315 ret = evdev_set_report_size(evdev,
316 evdev_estimate_report_size(evdev));
321 /* Create char device node */
322 ret = evdev_cdev_create(evdev);
326 /* Create sysctls (for device enumeration without /dev/input access rights) */
327 evdev_sysctl_create(evdev);
331 sx_destroy(&evdev->ev_list_lock);
336 evdev_register(struct evdev_dev *evdev)
340 if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH))
341 evdev->ev_lock_type = EV_LOCK_EXT_EPOCH;
343 evdev->ev_lock_type = EV_LOCK_INTERNAL;
344 evdev->ev_state_lock = &evdev->ev_mtx;
345 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
347 ret = evdev_register_common(evdev);
349 mtx_destroy(&evdev->ev_mtx);
355 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
358 evdev->ev_lock_type = EV_LOCK_MTX;
359 evdev->ev_state_lock = mtx;
360 return (evdev_register_common(evdev));
364 evdev_unregister(struct evdev_dev *evdev)
366 struct evdev_client *client, *tmp;
368 debugf(evdev, "%s: unregistered evdev provider: %s\n",
369 evdev->ev_shortname, evdev->ev_name);
371 sysctl_ctx_free(&evdev->ev_sysctl_ctx);
373 EVDEV_LIST_LOCK(evdev);
374 evdev->ev_cdev->si_drv1 = NULL;
375 /* Wake up sleepers */
376 CK_SLIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) {
377 evdev_revoke_client(client);
378 evdev_dispose_client(evdev, client);
379 EVDEV_CLIENT_LOCKQ(client);
380 evdev_notify_event(client);
381 EVDEV_CLIENT_UNLOCKQ(client);
383 EVDEV_LIST_UNLOCK(evdev);
385 /* release lock to avoid deadlock with evdev_dtor */
386 ret = evdev_cdev_destroy(evdev);
387 evdev->ev_cdev = NULL;
388 sx_destroy(&evdev->ev_list_lock);
389 if (ret == 0 && evdev->ev_lock_type != EV_LOCK_MTX)
390 mtx_destroy(&evdev->ev_mtx);
392 evdev_free_absinfo(evdev->ev_absinfo);
393 evdev_mt_free(evdev);
399 evdev_set_name(struct evdev_dev *evdev, const char *name)
402 snprintf(evdev->ev_name, NAMELEN, "%s", name);
406 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
407 uint16_t product, uint16_t version)
410 evdev->ev_id = (struct input_id) {
419 evdev_set_phys(struct evdev_dev *evdev, const char *name)
422 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
426 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
429 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
433 evdev_set_methods(struct evdev_dev *evdev, void *softc,
434 const struct evdev_methods *methods)
437 evdev->ev_methods = methods;
438 evdev->ev_softc = softc;
442 evdev_get_softc(struct evdev_dev *evdev)
445 return (evdev->ev_softc);
449 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
452 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
453 bit_set(evdev->ev_prop_flags, prop);
457 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
460 KASSERT(type < EV_CNT, ("invalid evdev event property"));
461 bit_set(evdev->ev_type_flags, type);
465 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
468 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
469 bit_set(evdev->ev_key_flags, code);
473 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
476 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
477 bit_set(evdev->ev_rel_flags, code);
481 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t minimum,
482 int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution)
484 struct input_absinfo absinfo;
486 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
488 absinfo = (struct input_absinfo) {
494 .resolution = resolution,
496 evdev_set_abs_bit(evdev, code);
497 evdev_set_absinfo(evdev, code, &absinfo);
501 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
504 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
505 if (evdev->ev_absinfo == NULL)
506 evdev->ev_absinfo = evdev_alloc_absinfo();
507 bit_set(evdev->ev_abs_flags, code);
511 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
514 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
515 bit_set(evdev->ev_msc_flags, code);
520 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
523 KASSERT(code < LED_CNT, ("invalid evdev led property"));
524 bit_set(evdev->ev_led_flags, code);
528 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
531 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
532 bit_set(evdev->ev_snd_flags, code);
536 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
539 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
540 bit_set(evdev->ev_sw_flags, code);
544 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
547 KASSERT(type < EV_CNT, ("invalid evdev event property"));
548 return (bit_test(evdev->ev_type_flags, type));
552 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
553 struct input_absinfo *absinfo)
556 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
558 if (axis == ABS_MT_SLOT &&
559 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
562 if (evdev->ev_absinfo == NULL)
563 evdev->ev_absinfo = evdev_alloc_absinfo();
565 if (axis == ABS_MT_SLOT)
566 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
568 memcpy(&evdev->ev_absinfo[axis], absinfo,
569 sizeof(struct input_absinfo));
573 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
576 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
577 evdev->ev_rep[property] = value;
581 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
584 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
585 bit_set(evdev->ev_flags, flag);
589 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
596 /* Allow SYN events implicitly */
597 if (type != EV_SYN && !evdev_event_supported(evdev, type))
609 if (!bit_test(evdev->ev_key_flags, code))
616 if (!bit_test(evdev->ev_rel_flags, code))
623 if (!bit_test(evdev->ev_abs_flags, code))
625 if (code == ABS_MT_SLOT &&
626 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
628 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
629 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
636 if (!bit_test(evdev->ev_msc_flags, code))
643 if (!bit_test(evdev->ev_led_flags, code))
650 if (!bit_test(evdev->ev_snd_flags, code))
657 if (!bit_test(evdev->ev_sw_flags, code))
674 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
677 int32_t fuzz, old_value, abs_change;
679 EVDEV_LOCK_ASSERT(evdev);
683 if (!evdev_event_supported(evdev, EV_REP))
686 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
687 /* Detect driver key repeats. */
688 if (bit_test(evdev->ev_key_states, code) &&
689 *value == KEY_EVENT_DOWN)
690 *value = KEY_EVENT_REPEAT;
692 /* Start/stop callout for evdev repeats */
693 if (bit_test(evdev->ev_key_states, code) == !*value &&
694 !CK_SLIST_EMPTY(&evdev->ev_clients)) {
695 if (*value == KEY_EVENT_DOWN)
696 evdev_start_repeat(evdev, code);
698 evdev_stop_repeat(evdev);
704 fuzz = evdev->ev_absinfo[code].fuzz;
705 if (fuzz == 0 || 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 old_value = evdev_mt_get_value(evdev,
711 evdev_mt_get_last_slot(evdev), code);
712 else /* Pass MT protocol type A events as is */
715 abs_change = abs(*value - old_value);
716 if (abs_change < fuzz / 2)
718 else if (abs_change < fuzz)
719 *value = (old_value * 3 + *value) / 4;
720 else if (abs_change < fuzz * 2)
721 *value = (old_value + *value) / 2;
726 static enum evdev_sparse_result
727 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
730 int32_t last_mt_slot;
732 EVDEV_LOCK_ASSERT(evdev);
735 * For certain event types, update device state bits
736 * and convert level reporting to edge reporting
743 if (bit_test(evdev->ev_key_states, code) == value)
744 return (EV_SKIP_EVENT);
745 bit_change(evdev->ev_key_states, code, value);
748 case KEY_EVENT_REPEAT:
749 if (bit_test(evdev->ev_key_states, code) == 0 ||
750 !evdev_event_supported(evdev, EV_REP))
751 return (EV_SKIP_EVENT);
755 return (EV_SKIP_EVENT);
760 if (bit_test(evdev->ev_led_states, code) == value)
761 return (EV_SKIP_EVENT);
762 bit_change(evdev->ev_led_states, code, value);
766 bit_change(evdev->ev_snd_states, code, value);
770 if (bit_test(evdev->ev_sw_states, code) == value)
771 return (EV_SKIP_EVENT);
772 bit_change(evdev->ev_sw_states, code, value);
776 if (evdev->ev_rep[code] == value)
777 return (EV_SKIP_EVENT);
778 evdev_set_repeat_params(evdev, code, value);
783 return (EV_SKIP_EVENT);
786 /* For EV_ABS, save last value in absinfo and ev_mt_states */
790 /* Postpone ABS_MT_SLOT till next event */
791 evdev_mt_set_last_slot(evdev, value);
792 return (EV_SKIP_EVENT);
794 case ABS_MT_FIRST ... ABS_MT_LAST:
795 /* Pass MT protocol type A events as is */
796 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
798 /* Don`t repeat MT protocol type B events */
799 last_mt_slot = evdev_mt_get_last_slot(evdev);
800 if (evdev_mt_get_value(evdev, last_mt_slot, code)
802 return (EV_SKIP_EVENT);
803 evdev_mt_set_value(evdev, last_mt_slot, code, value);
804 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
805 CURRENT_MT_SLOT(evdev) = last_mt_slot;
806 evdev->ev_report_opened = true;
807 return (EV_REPORT_MT_SLOT);
812 if (evdev->ev_absinfo[code].value == value)
813 return (EV_SKIP_EVENT);
814 evdev->ev_absinfo[code].value = value;
819 if (code == SYN_REPORT) {
820 /* Count empty reports as well as non empty */
821 evdev->ev_report_count++;
822 /* Skip empty reports */
823 if (!evdev->ev_report_opened)
824 return (EV_SKIP_EVENT);
825 evdev->ev_report_opened = false;
826 return (EV_REPORT_EVENT);
831 evdev->ev_report_opened = true;
832 return (EV_REPORT_EVENT);
836 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
839 struct epoch_tracker et;
840 struct evdev_client *client;
842 debugf(evdev, "%s pushed event %d/%d/%d",
843 evdev->ev_shortname, type, code, value);
845 EVDEV_LOCK_ASSERT(evdev);
847 /* Propagate event through all clients */
848 if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
849 epoch_enter_preempt(INPUT_EPOCH, &et);
852 evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0,
853 ("Input epoch has not been entered\n"));
855 CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) {
856 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
859 EVDEV_CLIENT_LOCKQ(client);
860 evdev_client_push(client, type, code, value);
861 if (type == EV_SYN && code == SYN_REPORT)
862 evdev_notify_event(client);
863 EVDEV_CLIENT_UNLOCKQ(client);
865 if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
866 epoch_exit_preempt(INPUT_EPOCH, &et);
868 evdev->ev_event_count++;
872 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
875 enum evdev_sparse_result sparse;
877 EVDEV_LOCK_ASSERT(evdev);
879 evdev_modify_event(evdev, type, code, &value);
880 sparse = evdev_sparse_event(evdev, type, code, value);
882 case EV_REPORT_MT_SLOT:
883 /* report postponed ABS_MT_SLOT */
884 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
885 CURRENT_MT_SLOT(evdev));
887 case EV_REPORT_EVENT:
888 evdev_propagate_event(evdev, type, code, value);
896 evdev_restore_after_kdb(struct evdev_dev *evdev)
900 EVDEV_LOCK_ASSERT(evdev);
902 /* Report postponed leds */
903 bit_foreach(evdev->ev_kdb_led_states, LED_CNT, code)
904 evdev_send_event(evdev, EV_LED, code,
905 !bit_test(evdev->ev_led_states, code));
906 bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
908 /* Release stuck keys (CTRL + ALT + ESC) */
909 evdev_stop_repeat(evdev);
910 bit_foreach(evdev->ev_key_states, KEY_CNT, code)
911 evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
912 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
916 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
920 if (evdev_check_event(evdev, type, code, value) != 0)
924 * Discard all but LEDs kdb events as unrelated to userspace.
925 * Aggregate LED updates and postpone reporting until kdb deactivation.
927 if (kdb_active || SCHEDULER_STOPPED()) {
928 evdev->ev_kdb_active = true;
930 bit_set(evdev->ev_kdb_led_states,
931 bit_test(evdev->ev_led_states, code) != value);
937 /* Fix evdev state corrupted with discarding of kdb events */
938 if (evdev->ev_kdb_active) {
939 evdev->ev_kdb_active = false;
940 evdev_restore_after_kdb(evdev);
943 if (type == EV_SYN && code == SYN_REPORT &&
944 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
945 evdev_mt_sync_frame(evdev);
946 evdev_send_event(evdev, type, code, value);
954 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
957 struct epoch_tracker et;
962 /* evdev repeats should not be processed by hardware driver */
963 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
970 if (evdev->ev_methods != NULL &&
971 evdev->ev_methods->ev_event != NULL)
972 evdev->ev_methods->ev_event(evdev, type, code, value);
974 * Leds and driver repeats should be reported in ev_event
975 * method body to interoperate with kbdmux states and rates
976 * propagation so both ways (ioctl and evdev) of changing it
977 * will produce only one evdev event report to client.
979 if (type == EV_LED || type == EV_REP)
988 if (evdev->ev_lock_type == EV_LOCK_MTX)
990 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
991 epoch_enter_preempt(INPUT_EPOCH, &et);
992 ret = evdev_push_event(evdev, type, code, value);
993 if (evdev->ev_lock_type == EV_LOCK_MTX)
995 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
996 epoch_exit_preempt(INPUT_EPOCH, &et);
1008 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
1012 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
1014 EVDEV_LIST_LOCK_ASSERT(evdev);
1016 if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
1017 evdev->ev_methods->ev_open != NULL) {
1018 debugf(evdev, "calling ev_open() on device %s",
1019 evdev->ev_shortname);
1020 ret = evdev->ev_methods->ev_open(evdev);
1023 CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
1028 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
1030 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1032 EVDEV_LIST_LOCK_ASSERT(evdev);
1034 CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link);
1035 if (CK_SLIST_EMPTY(&evdev->ev_clients)) {
1036 if (evdev->ev_methods != NULL &&
1037 evdev->ev_methods->ev_close != NULL)
1038 (void)evdev->ev_methods->ev_close(evdev);
1039 if (evdev_event_supported(evdev, EV_REP) &&
1040 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
1041 if (evdev->ev_lock_type != EV_LOCK_MTX)
1043 evdev_stop_repeat(evdev);
1044 if (evdev->ev_lock_type != EV_LOCK_MTX)
1045 EVDEV_UNLOCK(evdev);
1048 if (evdev->ev_lock_type != EV_LOCK_MTX)
1050 evdev_release_client(evdev, client);
1051 if (evdev->ev_lock_type != EV_LOCK_MTX)
1052 EVDEV_UNLOCK(evdev);
1056 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1059 EVDEV_LOCK_ASSERT(evdev);
1061 if (evdev->ev_grabber != NULL)
1064 evdev->ev_grabber = client;
1070 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1073 EVDEV_LOCK_ASSERT(evdev);
1075 if (evdev->ev_grabber != client)
1078 evdev->ev_grabber = NULL;
1084 evdev_repeat_callout(void *arg)
1086 struct epoch_tracker et;
1087 struct evdev_dev *evdev = (struct evdev_dev *)arg;
1089 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1090 epoch_enter_preempt(INPUT_EPOCH, &et);
1091 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1092 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1093 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH)
1094 epoch_exit_preempt(INPUT_EPOCH, &et);
1096 if (evdev->ev_rep[REP_PERIOD])
1097 callout_reset(&evdev->ev_rep_callout,
1098 evdev->ev_rep[REP_PERIOD] * hz / 1000,
1099 evdev_repeat_callout, evdev);
1101 evdev->ev_rep_key = KEY_RESERVED;
1105 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1108 EVDEV_LOCK_ASSERT(evdev);
1110 if (evdev->ev_rep[REP_DELAY]) {
1111 evdev->ev_rep_key = key;
1112 callout_reset(&evdev->ev_rep_callout,
1113 evdev->ev_rep[REP_DELAY] * hz / 1000,
1114 evdev_repeat_callout, evdev);
1119 evdev_stop_repeat(struct evdev_dev *evdev)
1122 EVDEV_LOCK_ASSERT(evdev);
1124 if (evdev->ev_rep_key != KEY_RESERVED) {
1125 callout_stop(&evdev->ev_rep_callout);
1126 evdev->ev_rep_key = KEY_RESERVED;
1130 MODULE_VERSION(evdev, 1);