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 int evdev_rcpt_mask = EVDEV_RCPT_SYSMOUSE | EVDEV_RCPT_KBDMUX;
70 int evdev_sysmouse_t_axis = 0;
72 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args");
74 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RW, &evdev_rcpt_mask, 0,
75 "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
76 "bit2 - mouse hardware, bit3 - keyboard hardware");
77 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RW,
78 &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm");
80 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD, 0,
81 "Evdev input devices");
83 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
84 static void evdev_stop_repeat(struct evdev_dev *);
85 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
88 bit_change(bitstr_t *bitstr, int bit, int value)
93 bit_clear(bitstr, bit);
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, NULL, "", "device index");
214 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
215 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
217 "Input device name");
219 SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
220 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
221 &evdev->ev_id, input_id,
222 "Input device identification");
224 /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
225 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
226 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
227 evdev->ev_shortname, 0,
228 "Input device short name");
230 /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
231 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
232 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
234 "Input device unique number");
236 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
237 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
238 evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
239 "Input device properties");
241 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
242 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
243 evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
244 "Input device supported events types");
246 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
247 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
248 evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
249 "", "Input device supported keys");
251 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
252 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
253 evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
254 "Input device supported relative events");
256 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
257 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
258 evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
259 "Input device supported absolute events");
261 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
262 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
263 evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
264 "Input device supported miscellaneous events");
266 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
267 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
268 evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
269 "Input device supported LED events");
271 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
272 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
273 evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
274 "Input device supported sound events");
276 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
277 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
278 evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
279 "Input device supported switch events");
283 evdev_register_common(struct evdev_dev *evdev)
287 debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
288 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
290 /* Initialize internal structures */
291 LIST_INIT(&evdev->ev_clients);
293 if (evdev_event_supported(evdev, EV_REP) &&
294 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
295 /* Initialize callout */
296 callout_init_mtx(&evdev->ev_rep_callout, &evdev->ev_mtx, 0);
298 if (evdev->ev_rep[REP_DELAY] == 0 &&
299 evdev->ev_rep[REP_PERIOD] == 0) {
300 /* Supply default values */
301 evdev->ev_rep[REP_DELAY] = 250;
302 evdev->ev_rep[REP_PERIOD] = 33;
306 /* Initialize multitouch protocol type B states */
307 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
308 evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
309 evdev_mt_init(evdev);
311 /* Estimate maximum report size */
312 if (evdev->ev_report_size == 0) {
313 ret = evdev_set_report_size(evdev,
314 evdev_estimate_report_size(evdev));
319 /* Create char device node */
320 ret = evdev_cdev_create(evdev);
324 /* Create sysctls (for device enumeration without /dev/input access rights) */
325 evdev_sysctl_create(evdev);
332 evdev_register(struct evdev_dev *evdev)
336 evdev->ev_lock_type = EV_LOCK_INTERNAL;
337 evdev->ev_lock = &evdev->ev_mtx;
338 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
340 ret = evdev_register_common(evdev);
342 mtx_destroy(&evdev->ev_mtx);
348 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
351 evdev->ev_lock_type = EV_LOCK_MTX;
352 evdev->ev_lock = mtx;
353 return (evdev_register_common(evdev));
357 evdev_unregister(struct evdev_dev *evdev)
359 struct evdev_client *client;
361 debugf(evdev, "%s: unregistered evdev provider: %s\n",
362 evdev->ev_shortname, evdev->ev_name);
364 sysctl_ctx_free(&evdev->ev_sysctl_ctx);
367 evdev->ev_cdev->si_drv1 = NULL;
368 /* Wake up sleepers */
369 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
370 evdev_revoke_client(client);
371 evdev_dispose_client(evdev, client);
372 EVDEV_CLIENT_LOCKQ(client);
373 evdev_notify_event(client);
374 EVDEV_CLIENT_UNLOCKQ(client);
378 /* destroy_dev can sleep so release lock */
379 ret = evdev_cdev_destroy(evdev);
380 evdev->ev_cdev = NULL;
381 if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL)
382 mtx_destroy(&evdev->ev_mtx);
384 evdev_free_absinfo(evdev->ev_absinfo);
385 evdev_mt_free(evdev);
391 evdev_set_name(struct evdev_dev *evdev, const char *name)
394 snprintf(evdev->ev_name, NAMELEN, "%s", name);
398 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
399 uint16_t product, uint16_t version)
402 evdev->ev_id = (struct input_id) {
411 evdev_set_phys(struct evdev_dev *evdev, const char *name)
414 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
418 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
421 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
425 evdev_set_methods(struct evdev_dev *evdev, void *softc,
426 const struct evdev_methods *methods)
429 evdev->ev_methods = methods;
430 evdev->ev_softc = softc;
434 evdev_get_softc(struct evdev_dev *evdev)
437 return (evdev->ev_softc);
441 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
444 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
445 bit_set(evdev->ev_prop_flags, prop);
449 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
452 KASSERT(type < EV_CNT, ("invalid evdev event property"));
453 bit_set(evdev->ev_type_flags, type);
457 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
460 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
461 bit_set(evdev->ev_key_flags, code);
465 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
468 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
469 bit_set(evdev->ev_rel_flags, code);
473 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value,
474 int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat,
477 struct input_absinfo absinfo;
479 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
481 absinfo = (struct input_absinfo) {
487 .resolution = resolution,
489 evdev_set_abs_bit(evdev, code);
490 evdev_set_absinfo(evdev, code, &absinfo);
494 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
497 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
498 if (evdev->ev_absinfo == NULL)
499 evdev->ev_absinfo = evdev_alloc_absinfo();
500 bit_set(evdev->ev_abs_flags, code);
504 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
507 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
508 bit_set(evdev->ev_msc_flags, code);
513 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
516 KASSERT(code < LED_CNT, ("invalid evdev led property"));
517 bit_set(evdev->ev_led_flags, code);
521 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
524 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
525 bit_set(evdev->ev_snd_flags, code);
529 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
532 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
533 bit_set(evdev->ev_sw_flags, code);
537 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
540 KASSERT(type < EV_CNT, ("invalid evdev event property"));
541 return (bit_test(evdev->ev_type_flags, type));
545 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
546 struct input_absinfo *absinfo)
549 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
551 if (axis == ABS_MT_SLOT &&
552 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
555 if (evdev->ev_absinfo == NULL)
556 evdev->ev_absinfo = evdev_alloc_absinfo();
558 if (axis == ABS_MT_SLOT)
559 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
561 memcpy(&evdev->ev_absinfo[axis], absinfo,
562 sizeof(struct input_absinfo));
566 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
569 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
570 evdev->ev_rep[property] = value;
574 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
577 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
578 bit_set(evdev->ev_flags, flag);
582 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
589 /* Allow SYN events implicitly */
590 if (type != EV_SYN && !evdev_event_supported(evdev, type))
602 if (!bit_test(evdev->ev_key_flags, code))
609 if (!bit_test(evdev->ev_rel_flags, code))
616 if (!bit_test(evdev->ev_abs_flags, code))
618 if (code == ABS_MT_SLOT &&
619 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
621 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
622 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
629 if (!bit_test(evdev->ev_msc_flags, code))
636 if (!bit_test(evdev->ev_led_flags, code))
643 if (!bit_test(evdev->ev_snd_flags, code))
650 if (!bit_test(evdev->ev_sw_flags, code))
667 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
671 EVDEV_LOCK_ASSERT(evdev);
675 if (!evdev_event_supported(evdev, EV_REP))
678 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
679 /* Detect driver key repeats. */
680 if (bit_test(evdev->ev_key_states, code) &&
681 *value == KEY_EVENT_DOWN)
682 *value = KEY_EVENT_REPEAT;
684 /* Start/stop callout for evdev repeats */
685 if (bit_test(evdev->ev_key_states, code) == !*value &&
686 !LIST_EMPTY(&evdev->ev_clients)) {
687 if (*value == KEY_EVENT_DOWN)
688 evdev_start_repeat(evdev, code);
690 evdev_stop_repeat(evdev);
696 /* TBD: implement fuzz */
701 static enum evdev_sparse_result
702 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
705 int32_t last_mt_slot;
707 EVDEV_LOCK_ASSERT(evdev);
710 * For certain event types, update device state bits
711 * and convert level reporting to edge reporting
718 if (bit_test(evdev->ev_key_states, code) == value)
719 return (EV_SKIP_EVENT);
720 bit_change(evdev->ev_key_states, code, value);
723 case KEY_EVENT_REPEAT:
724 if (bit_test(evdev->ev_key_states, code) == 0 ||
725 !evdev_event_supported(evdev, EV_REP))
726 return (EV_SKIP_EVENT);
730 return (EV_SKIP_EVENT);
735 if (bit_test(evdev->ev_led_states, code) == value)
736 return (EV_SKIP_EVENT);
737 bit_change(evdev->ev_led_states, code, value);
741 bit_change(evdev->ev_snd_states, code, value);
745 if (bit_test(evdev->ev_sw_states, code) == value)
746 return (EV_SKIP_EVENT);
747 bit_change(evdev->ev_sw_states, code, value);
751 if (evdev->ev_rep[code] == value)
752 return (EV_SKIP_EVENT);
753 evdev_set_repeat_params(evdev, code, value);
758 return (EV_SKIP_EVENT);
761 /* For EV_ABS, save last value in absinfo and ev_mt_states */
765 /* Postpone ABS_MT_SLOT till next event */
766 evdev_set_last_mt_slot(evdev, value);
767 return (EV_SKIP_EVENT);
769 case ABS_MT_FIRST ... ABS_MT_LAST:
770 /* Pass MT protocol type A events as is */
771 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
773 /* Don`t repeat MT protocol type B events */
774 last_mt_slot = evdev_get_last_mt_slot(evdev);
775 if (evdev_get_mt_value(evdev, last_mt_slot, code)
777 return (EV_SKIP_EVENT);
778 evdev_set_mt_value(evdev, last_mt_slot, code, value);
779 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
780 CURRENT_MT_SLOT(evdev) = last_mt_slot;
781 evdev->ev_report_opened = true;
782 return (EV_REPORT_MT_SLOT);
787 if (evdev->ev_absinfo[code].value == value)
788 return (EV_SKIP_EVENT);
789 evdev->ev_absinfo[code].value = value;
794 if (code == SYN_REPORT) {
795 /* Count empty reports as well as non empty */
796 evdev->ev_report_count++;
797 /* Skip empty reports */
798 if (!evdev->ev_report_opened)
799 return (EV_SKIP_EVENT);
800 evdev->ev_report_opened = false;
801 return (EV_REPORT_EVENT);
806 evdev->ev_report_opened = true;
807 return (EV_REPORT_EVENT);
811 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
814 struct evdev_client *client;
816 debugf(evdev, "%s pushed event %d/%d/%d",
817 evdev->ev_shortname, type, code, value);
819 EVDEV_LOCK_ASSERT(evdev);
821 /* Propagate event through all clients */
822 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
823 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
826 EVDEV_CLIENT_LOCKQ(client);
827 evdev_client_push(client, type, code, value);
828 if (type == EV_SYN && code == SYN_REPORT)
829 evdev_notify_event(client);
830 EVDEV_CLIENT_UNLOCKQ(client);
833 evdev->ev_event_count++;
837 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
840 enum evdev_sparse_result sparse;
842 EVDEV_LOCK_ASSERT(evdev);
844 sparse = evdev_sparse_event(evdev, type, code, value);
846 case EV_REPORT_MT_SLOT:
847 /* report postponed ABS_MT_SLOT */
848 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
849 CURRENT_MT_SLOT(evdev));
851 case EV_REPORT_EVENT:
852 evdev_propagate_event(evdev, type, code, value);
860 evdev_restore_after_kdb(struct evdev_dev *evdev)
864 EVDEV_LOCK_ASSERT(evdev);
866 /* Report postponed leds */
867 for (code = 0; code < LED_CNT; code++)
868 if (bit_test(evdev->ev_kdb_led_states, code))
869 evdev_send_event(evdev, EV_LED, code,
870 !bit_test(evdev->ev_led_states, code));
871 bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
873 /* Release stuck keys (CTRL + ALT + ESC) */
874 evdev_stop_repeat(evdev);
875 for (code = 0; code < KEY_CNT; code++) {
876 if (bit_test(evdev->ev_key_states, code)) {
877 evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
878 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
884 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
888 if (evdev_check_event(evdev, type, code, value) != 0)
892 * Discard all but LEDs kdb events as unrelated to userspace.
893 * Aggregate LED updates and postpone reporting until kdb deactivation.
895 if (kdb_active || SCHEDULER_STOPPED()) {
896 evdev->ev_kdb_active = true;
898 bit_set(evdev->ev_kdb_led_states,
899 bit_test(evdev->ev_led_states, code) != value);
905 /* Fix evdev state corrupted with discarding of kdb events */
906 if (evdev->ev_kdb_active) {
907 evdev->ev_kdb_active = false;
908 evdev_restore_after_kdb(evdev);
911 evdev_modify_event(evdev, type, code, &value);
912 if (type == EV_SYN && code == SYN_REPORT &&
913 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
914 evdev_send_mt_autorel(evdev);
915 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
916 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
917 evdev_send_mt_compat(evdev);
918 evdev_send_event(evdev, type, code, value);
926 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
933 /* evdev repeats should not be processed by hardware driver */
934 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
941 if (evdev->ev_methods != NULL &&
942 evdev->ev_methods->ev_event != NULL)
943 evdev->ev_methods->ev_event(evdev, type, code, value);
945 * Leds and driver repeats should be reported in ev_event
946 * method body to interoperate with kbdmux states and rates
947 * propagation so both ways (ioctl and evdev) of changing it
948 * will produce only one evdev event report to client.
950 if (type == EV_LED || type == EV_REP)
959 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
961 ret = evdev_push_event(evdev, type, code, value);
962 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
974 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
978 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
980 EVDEV_LOCK_ASSERT(evdev);
982 if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
983 evdev->ev_methods->ev_open != NULL) {
984 debugf(evdev, "calling ev_open() on device %s",
985 evdev->ev_shortname);
986 ret = evdev->ev_methods->ev_open(evdev);
989 LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
994 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
996 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
998 EVDEV_LOCK_ASSERT(evdev);
1000 LIST_REMOVE(client, ec_link);
1001 if (LIST_EMPTY(&evdev->ev_clients)) {
1002 if (evdev->ev_methods != NULL &&
1003 evdev->ev_methods->ev_close != NULL)
1004 (void)evdev->ev_methods->ev_close(evdev);
1005 if (evdev_event_supported(evdev, EV_REP) &&
1006 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
1007 evdev_stop_repeat(evdev);
1009 evdev_release_client(evdev, client);
1013 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1016 EVDEV_LOCK_ASSERT(evdev);
1018 if (evdev->ev_grabber != NULL)
1021 evdev->ev_grabber = client;
1027 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1030 EVDEV_LOCK_ASSERT(evdev);
1032 if (evdev->ev_grabber != client)
1035 evdev->ev_grabber = NULL;
1041 evdev_repeat_callout(void *arg)
1043 struct evdev_dev *evdev = (struct evdev_dev *)arg;
1045 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1046 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1048 if (evdev->ev_rep[REP_PERIOD])
1049 callout_reset(&evdev->ev_rep_callout,
1050 evdev->ev_rep[REP_PERIOD] * hz / 1000,
1051 evdev_repeat_callout, evdev);
1053 evdev->ev_rep_key = KEY_RESERVED;
1057 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1060 EVDEV_LOCK_ASSERT(evdev);
1062 if (evdev->ev_rep[REP_DELAY]) {
1063 evdev->ev_rep_key = key;
1064 callout_reset(&evdev->ev_rep_callout,
1065 evdev->ev_rep[REP_DELAY] * hz / 1000,
1066 evdev_repeat_callout, evdev);
1071 evdev_stop_repeat(struct evdev_dev *evdev)
1074 EVDEV_LOCK_ASSERT(evdev);
1076 if (evdev->ev_rep_key != KEY_RESERVED) {
1077 callout_stop(&evdev->ev_rep_callout);
1078 evdev->ev_rep_key = KEY_RESERVED;
1082 MODULE_VERSION(evdev, 1);