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/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <sys/sysctl.h>
41 #include <sys/systm.h>
43 #include <dev/evdev/evdev.h>
44 #include <dev/evdev/evdev_private.h>
45 #include <dev/evdev/input.h>
48 #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args)
50 #define debugf(evdev, fmt, args...)
54 FEATURE(evdev, "Input event devices support");
56 FEATURE(evdev_support, "Evdev support in hybrid drivers");
60 enum evdev_sparse_result
62 EV_SKIP_EVENT, /* Event value not changed */
63 EV_REPORT_EVENT, /* Event value changed */
64 EV_REPORT_MT_SLOT, /* Event value and MT slot number changed */
67 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
69 /* adb keyboard driver used on powerpc does not support evdev yet */
70 #if defined(__powerpc__) && !defined(__powerpc64__)
71 int evdev_rcpt_mask = EVDEV_RCPT_KBDMUX | EVDEV_RCPT_HW_MOUSE;
73 int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD;
75 int evdev_sysmouse_t_axis = 0;
77 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args");
79 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RWTUN, &evdev_rcpt_mask, 0,
80 "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
81 "bit2 - mouse hardware, bit3 - keyboard hardware");
82 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RWTUN,
83 &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm");
85 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD, 0,
86 "Evdev input devices");
88 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
89 static void evdev_stop_repeat(struct evdev_dev *);
90 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
93 bit_change(bitstr_t *bitstr, int bit, int value)
98 bit_clear(bitstr, bit);
105 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
109 evdev_free(struct evdev_dev *evdev)
112 if (evdev != NULL && evdev->ev_cdev != NULL &&
113 evdev->ev_cdev->si_drv1 != NULL)
114 evdev_unregister(evdev);
116 free(evdev, M_EVDEV);
119 static struct input_absinfo *
120 evdev_alloc_absinfo(void)
123 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
128 evdev_free_absinfo(struct input_absinfo *absinfo)
131 free(absinfo, M_EVDEV);
135 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
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)
141 evdev->ev_report_size = report_size;
146 evdev_estimate_report_size(struct evdev_dev *evdev)
152 * Keyboards generate one event per report but other devices with
153 * buttons like mouses can report events simultaneously
155 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
157 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
159 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
162 /* All relative axes can be reported simultaneously */
163 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
167 * All absolute axes can be reported simultaneously.
168 * Multitouch axes can be reported ABS_MT_SLOT times
170 if (evdev->ev_absinfo != NULL) {
171 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
173 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
175 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */
176 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
178 size += res * MAXIMAL_MT_SLOT(evdev);
181 size += res * (MAX_MT_REPORTS - 1);
185 /* All misc events can be reported simultaneously */
186 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
189 /* All leds can be reported simultaneously */
190 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
193 /* Assume other events are generated once per report */
194 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
197 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
200 /* XXX: FF part is not implemented yet */
202 size++; /* SYN_REPORT */
207 evdev_sysctl_create(struct evdev_dev *evdev)
209 struct sysctl_oid *ev_sysctl_tree;
212 snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
213 sysctl_ctx_init(&evdev->ev_sysctl_ctx);
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, NULL, "", "device index");
219 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
220 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
222 "Input device name");
224 SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
225 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
226 &evdev->ev_id, input_id,
227 "Input device identification");
229 /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
230 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
231 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
232 evdev->ev_shortname, 0,
233 "Input device short name");
235 /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
236 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
237 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
239 "Input device unique number");
241 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
242 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
243 evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
244 "Input device properties");
246 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
247 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
248 evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
249 "Input device supported events types");
251 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
252 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
253 evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
254 "", "Input device supported keys");
256 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
257 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
258 evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
259 "Input device supported relative events");
261 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
262 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
263 evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
264 "Input device supported absolute events");
266 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
267 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
268 evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
269 "Input device supported miscellaneous events");
271 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
272 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
273 evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
274 "Input device supported LED events");
276 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
277 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
278 evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
279 "Input device supported sound events");
281 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
282 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
283 evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
284 "Input device supported switch events");
288 evdev_register_common(struct evdev_dev *evdev)
292 debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
293 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
295 /* Initialize internal structures */
296 LIST_INIT(&evdev->ev_clients);
298 if (evdev_event_supported(evdev, EV_REP) &&
299 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
300 /* Initialize callout */
301 callout_init_mtx(&evdev->ev_rep_callout, evdev->ev_lock, 0);
303 if (evdev->ev_rep[REP_DELAY] == 0 &&
304 evdev->ev_rep[REP_PERIOD] == 0) {
305 /* Supply default values */
306 evdev->ev_rep[REP_DELAY] = 250;
307 evdev->ev_rep[REP_PERIOD] = 33;
311 /* Initialize multitouch protocol type B states */
312 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
313 evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
314 evdev_mt_init(evdev);
316 /* Estimate maximum report size */
317 if (evdev->ev_report_size == 0) {
318 ret = evdev_set_report_size(evdev,
319 evdev_estimate_report_size(evdev));
324 /* Create char device node */
325 ret = evdev_cdev_create(evdev);
329 /* Create sysctls (for device enumeration without /dev/input access rights) */
330 evdev_sysctl_create(evdev);
337 evdev_register(struct evdev_dev *evdev)
341 evdev->ev_lock_type = EV_LOCK_INTERNAL;
342 evdev->ev_lock = &evdev->ev_mtx;
343 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
345 ret = evdev_register_common(evdev);
347 mtx_destroy(&evdev->ev_mtx);
353 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
356 evdev->ev_lock_type = EV_LOCK_MTX;
357 evdev->ev_lock = mtx;
358 return (evdev_register_common(evdev));
362 evdev_unregister(struct evdev_dev *evdev)
364 struct evdev_client *client, *tmp;
366 debugf(evdev, "%s: unregistered evdev provider: %s\n",
367 evdev->ev_shortname, evdev->ev_name);
369 sysctl_ctx_free(&evdev->ev_sysctl_ctx);
372 evdev->ev_cdev->si_drv1 = NULL;
373 /* Wake up sleepers */
374 LIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) {
375 evdev_revoke_client(client);
376 evdev_dispose_client(evdev, client);
377 EVDEV_CLIENT_LOCKQ(client);
378 evdev_notify_event(client);
379 EVDEV_CLIENT_UNLOCKQ(client);
383 /* destroy_dev can sleep so release lock */
384 ret = evdev_cdev_destroy(evdev);
385 evdev->ev_cdev = NULL;
386 if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL)
387 mtx_destroy(&evdev->ev_mtx);
389 evdev_free_absinfo(evdev->ev_absinfo);
390 evdev_mt_free(evdev);
396 evdev_set_name(struct evdev_dev *evdev, const char *name)
399 snprintf(evdev->ev_name, NAMELEN, "%s", name);
403 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
404 uint16_t product, uint16_t version)
407 evdev->ev_id = (struct input_id) {
416 evdev_set_phys(struct evdev_dev *evdev, const char *name)
419 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
423 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
426 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
430 evdev_set_methods(struct evdev_dev *evdev, void *softc,
431 const struct evdev_methods *methods)
434 evdev->ev_methods = methods;
435 evdev->ev_softc = softc;
439 evdev_get_softc(struct evdev_dev *evdev)
442 return (evdev->ev_softc);
446 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
449 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
450 bit_set(evdev->ev_prop_flags, prop);
454 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
457 KASSERT(type < EV_CNT, ("invalid evdev event property"));
458 bit_set(evdev->ev_type_flags, type);
462 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
465 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
466 bit_set(evdev->ev_key_flags, code);
470 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
473 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
474 bit_set(evdev->ev_rel_flags, code);
478 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value,
479 int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat,
482 struct input_absinfo absinfo;
484 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
486 absinfo = (struct input_absinfo) {
492 .resolution = resolution,
494 evdev_set_abs_bit(evdev, code);
495 evdev_set_absinfo(evdev, code, &absinfo);
499 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
502 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
503 if (evdev->ev_absinfo == NULL)
504 evdev->ev_absinfo = evdev_alloc_absinfo();
505 bit_set(evdev->ev_abs_flags, code);
509 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
512 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
513 bit_set(evdev->ev_msc_flags, code);
518 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
521 KASSERT(code < LED_CNT, ("invalid evdev led property"));
522 bit_set(evdev->ev_led_flags, code);
526 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
529 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
530 bit_set(evdev->ev_snd_flags, code);
534 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
537 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
538 bit_set(evdev->ev_sw_flags, code);
542 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
545 KASSERT(type < EV_CNT, ("invalid evdev event property"));
546 return (bit_test(evdev->ev_type_flags, type));
550 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
551 struct input_absinfo *absinfo)
554 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
556 if (axis == ABS_MT_SLOT &&
557 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
560 if (evdev->ev_absinfo == NULL)
561 evdev->ev_absinfo = evdev_alloc_absinfo();
563 if (axis == ABS_MT_SLOT)
564 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
566 memcpy(&evdev->ev_absinfo[axis], absinfo,
567 sizeof(struct input_absinfo));
571 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
574 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
575 evdev->ev_rep[property] = value;
579 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
582 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
583 bit_set(evdev->ev_flags, flag);
587 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
594 /* Allow SYN events implicitly */
595 if (type != EV_SYN && !evdev_event_supported(evdev, type))
607 if (!bit_test(evdev->ev_key_flags, code))
614 if (!bit_test(evdev->ev_rel_flags, code))
621 if (!bit_test(evdev->ev_abs_flags, code))
623 if (code == ABS_MT_SLOT &&
624 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
626 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
627 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
634 if (!bit_test(evdev->ev_msc_flags, code))
641 if (!bit_test(evdev->ev_led_flags, code))
648 if (!bit_test(evdev->ev_snd_flags, code))
655 if (!bit_test(evdev->ev_sw_flags, code))
672 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
676 EVDEV_LOCK_ASSERT(evdev);
680 if (!evdev_event_supported(evdev, EV_REP))
683 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
684 /* Detect driver key repeats. */
685 if (bit_test(evdev->ev_key_states, code) &&
686 *value == KEY_EVENT_DOWN)
687 *value = KEY_EVENT_REPEAT;
689 /* Start/stop callout for evdev repeats */
690 if (bit_test(evdev->ev_key_states, code) == !*value &&
691 !LIST_EMPTY(&evdev->ev_clients)) {
692 if (*value == KEY_EVENT_DOWN)
693 evdev_start_repeat(evdev, code);
695 evdev_stop_repeat(evdev);
701 /* TBD: implement fuzz */
706 static enum evdev_sparse_result
707 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
710 int32_t last_mt_slot;
712 EVDEV_LOCK_ASSERT(evdev);
715 * For certain event types, update device state bits
716 * and convert level reporting to edge reporting
723 if (bit_test(evdev->ev_key_states, code) == value)
724 return (EV_SKIP_EVENT);
725 bit_change(evdev->ev_key_states, code, value);
728 case KEY_EVENT_REPEAT:
729 if (bit_test(evdev->ev_key_states, code) == 0 ||
730 !evdev_event_supported(evdev, EV_REP))
731 return (EV_SKIP_EVENT);
735 return (EV_SKIP_EVENT);
740 if (bit_test(evdev->ev_led_states, code) == value)
741 return (EV_SKIP_EVENT);
742 bit_change(evdev->ev_led_states, code, value);
746 bit_change(evdev->ev_snd_states, code, value);
750 if (bit_test(evdev->ev_sw_states, code) == value)
751 return (EV_SKIP_EVENT);
752 bit_change(evdev->ev_sw_states, code, value);
756 if (evdev->ev_rep[code] == value)
757 return (EV_SKIP_EVENT);
758 evdev_set_repeat_params(evdev, code, value);
763 return (EV_SKIP_EVENT);
766 /* For EV_ABS, save last value in absinfo and ev_mt_states */
770 /* Postpone ABS_MT_SLOT till next event */
771 evdev_set_last_mt_slot(evdev, value);
772 return (EV_SKIP_EVENT);
774 case ABS_MT_FIRST ... ABS_MT_LAST:
775 /* Pass MT protocol type A events as is */
776 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
778 /* Don`t repeat MT protocol type B events */
779 last_mt_slot = evdev_get_last_mt_slot(evdev);
780 if (evdev_get_mt_value(evdev, last_mt_slot, code)
782 return (EV_SKIP_EVENT);
783 evdev_set_mt_value(evdev, last_mt_slot, code, value);
784 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
785 CURRENT_MT_SLOT(evdev) = last_mt_slot;
786 evdev->ev_report_opened = true;
787 return (EV_REPORT_MT_SLOT);
792 if (evdev->ev_absinfo[code].value == value)
793 return (EV_SKIP_EVENT);
794 evdev->ev_absinfo[code].value = value;
799 if (code == SYN_REPORT) {
800 /* Count empty reports as well as non empty */
801 evdev->ev_report_count++;
802 /* Skip empty reports */
803 if (!evdev->ev_report_opened)
804 return (EV_SKIP_EVENT);
805 evdev->ev_report_opened = false;
806 return (EV_REPORT_EVENT);
811 evdev->ev_report_opened = true;
812 return (EV_REPORT_EVENT);
816 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
819 struct evdev_client *client;
821 debugf(evdev, "%s pushed event %d/%d/%d",
822 evdev->ev_shortname, type, code, value);
824 EVDEV_LOCK_ASSERT(evdev);
826 /* Propagate event through all clients */
827 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
828 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
831 EVDEV_CLIENT_LOCKQ(client);
832 evdev_client_push(client, type, code, value);
833 if (type == EV_SYN && code == SYN_REPORT)
834 evdev_notify_event(client);
835 EVDEV_CLIENT_UNLOCKQ(client);
838 evdev->ev_event_count++;
842 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
845 enum evdev_sparse_result sparse;
847 EVDEV_LOCK_ASSERT(evdev);
849 sparse = evdev_sparse_event(evdev, type, code, value);
851 case EV_REPORT_MT_SLOT:
852 /* report postponed ABS_MT_SLOT */
853 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
854 CURRENT_MT_SLOT(evdev));
856 case EV_REPORT_EVENT:
857 evdev_propagate_event(evdev, type, code, value);
865 evdev_restore_after_kdb(struct evdev_dev *evdev)
869 EVDEV_LOCK_ASSERT(evdev);
871 /* Report postponed leds */
872 for (code = 0; code < LED_CNT; code++)
873 if (bit_test(evdev->ev_kdb_led_states, code))
874 evdev_send_event(evdev, EV_LED, code,
875 !bit_test(evdev->ev_led_states, code));
876 bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
878 /* Release stuck keys (CTRL + ALT + ESC) */
879 evdev_stop_repeat(evdev);
880 for (code = 0; code < KEY_CNT; code++) {
881 if (bit_test(evdev->ev_key_states, code)) {
882 evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
883 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
889 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
893 if (evdev_check_event(evdev, type, code, value) != 0)
897 * Discard all but LEDs kdb events as unrelated to userspace.
898 * Aggregate LED updates and postpone reporting until kdb deactivation.
900 if (kdb_active || SCHEDULER_STOPPED()) {
901 evdev->ev_kdb_active = true;
903 bit_set(evdev->ev_kdb_led_states,
904 bit_test(evdev->ev_led_states, code) != value);
910 /* Fix evdev state corrupted with discarding of kdb events */
911 if (evdev->ev_kdb_active) {
912 evdev->ev_kdb_active = false;
913 evdev_restore_after_kdb(evdev);
916 evdev_modify_event(evdev, type, code, &value);
917 if (type == EV_SYN && code == SYN_REPORT &&
918 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
919 evdev_send_mt_autorel(evdev);
920 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
921 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
922 evdev_send_mt_compat(evdev);
923 evdev_send_event(evdev, type, code, value);
931 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
938 /* evdev repeats should not be processed by hardware driver */
939 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
946 if (evdev->ev_methods != NULL &&
947 evdev->ev_methods->ev_event != NULL)
948 evdev->ev_methods->ev_event(evdev, type, code, value);
950 * Leds and driver repeats should be reported in ev_event
951 * method body to interoperate with kbdmux states and rates
952 * propagation so both ways (ioctl and evdev) of changing it
953 * will produce only one evdev event report to client.
955 if (type == EV_LED || type == EV_REP)
964 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
966 ret = evdev_push_event(evdev, type, code, value);
967 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
979 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
983 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
985 EVDEV_LOCK_ASSERT(evdev);
987 if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
988 evdev->ev_methods->ev_open != NULL) {
989 debugf(evdev, "calling ev_open() on device %s",
990 evdev->ev_shortname);
991 ret = evdev->ev_methods->ev_open(evdev);
994 LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
999 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
1001 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1003 EVDEV_LOCK_ASSERT(evdev);
1005 LIST_REMOVE(client, ec_link);
1006 if (LIST_EMPTY(&evdev->ev_clients)) {
1007 if (evdev->ev_methods != NULL &&
1008 evdev->ev_methods->ev_close != NULL)
1009 (void)evdev->ev_methods->ev_close(evdev);
1010 if (evdev_event_supported(evdev, EV_REP) &&
1011 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
1012 evdev_stop_repeat(evdev);
1014 evdev_release_client(evdev, client);
1018 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1021 EVDEV_LOCK_ASSERT(evdev);
1023 if (evdev->ev_grabber != NULL)
1026 evdev->ev_grabber = client;
1032 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1035 EVDEV_LOCK_ASSERT(evdev);
1037 if (evdev->ev_grabber != client)
1040 evdev->ev_grabber = NULL;
1046 evdev_repeat_callout(void *arg)
1048 struct evdev_dev *evdev = (struct evdev_dev *)arg;
1050 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1051 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1053 if (evdev->ev_rep[REP_PERIOD])
1054 callout_reset(&evdev->ev_rep_callout,
1055 evdev->ev_rep[REP_PERIOD] * hz / 1000,
1056 evdev_repeat_callout, evdev);
1058 evdev->ev_rep_key = KEY_RESERVED;
1062 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1065 EVDEV_LOCK_ASSERT(evdev);
1067 if (evdev->ev_rep[REP_DELAY]) {
1068 evdev->ev_rep_key = key;
1069 callout_reset(&evdev->ev_rep_callout,
1070 evdev->ev_rep[REP_DELAY] * hz / 1000,
1071 evdev_repeat_callout, evdev);
1076 evdev_stop_repeat(struct evdev_dev *evdev)
1079 EVDEV_LOCK_ASSERT(evdev);
1081 if (evdev->ev_rep_key != KEY_RESERVED) {
1082 callout_stop(&evdev->ev_rep_callout);
1083 evdev->ev_rep_key = KEY_RESERVED;
1087 MODULE_VERSION(evdev, 1);