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 | CTLFLAG_MPSAFE, 0,
75 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RW, &evdev_rcpt_mask, 0,
76 "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
77 "bit2 - mouse hardware, bit3 - keyboard hardware");
78 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RW,
79 &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm");
81 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
82 "Evdev input devices");
84 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
85 static void evdev_stop_repeat(struct evdev_dev *);
86 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
89 bit_change(bitstr_t *bitstr, int bit, int value)
94 bit_clear(bitstr, bit);
101 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
105 evdev_free(struct evdev_dev *evdev)
108 if (evdev != NULL && evdev->ev_cdev != NULL &&
109 evdev->ev_cdev->si_drv1 != NULL)
110 evdev_unregister(evdev);
112 free(evdev, M_EVDEV);
115 static struct input_absinfo *
116 evdev_alloc_absinfo(void)
119 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
124 evdev_free_absinfo(struct input_absinfo *absinfo)
127 free(absinfo, M_EVDEV);
131 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
133 if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
134 MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
137 evdev->ev_report_size = report_size;
142 evdev_estimate_report_size(struct evdev_dev *evdev)
148 * Keyboards generate one event per report but other devices with
149 * buttons like mouses can report events simultaneously
151 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
153 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
155 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
158 /* All relative axes can be reported simultaneously */
159 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
163 * All absolute axes can be reported simultaneously.
164 * Multitouch axes can be reported ABS_MT_SLOT times
166 if (evdev->ev_absinfo != NULL) {
167 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
169 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
171 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */
172 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
174 size += res * MAXIMAL_MT_SLOT(evdev);
177 size += res * (MAX_MT_REPORTS - 1);
181 /* All misc events can be reported simultaneously */
182 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
185 /* All leds can be reported simultaneously */
186 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
189 /* Assume other events are generated once per report */
190 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
193 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
196 /* XXX: FF part is not implemented yet */
198 size++; /* SYN_REPORT */
203 evdev_sysctl_create(struct evdev_dev *evdev)
205 struct sysctl_oid *ev_sysctl_tree;
208 snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
209 sysctl_ctx_init(&evdev->ev_sysctl_ctx);
211 ev_sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&evdev->ev_sysctl_ctx,
212 SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO,
213 ev_unit_str, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "",
216 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
217 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
219 "Input device name");
221 SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
222 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
223 &evdev->ev_id, input_id,
224 "Input device identification");
226 /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
227 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
228 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
229 evdev->ev_shortname, 0,
230 "Input device short name");
232 /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
233 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
234 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
236 "Input device unique number");
238 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
239 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
240 evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
241 "Input device properties");
243 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
244 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
245 evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
246 "Input device supported events types");
248 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
249 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
250 evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
251 "", "Input device supported keys");
253 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
254 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
255 evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
256 "Input device supported relative events");
258 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
259 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
260 evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
261 "Input device supported absolute events");
263 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
264 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
265 evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
266 "Input device supported miscellaneous events");
268 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
269 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
270 evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
271 "Input device supported LED events");
273 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
274 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
275 evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
276 "Input device supported sound events");
278 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
279 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
280 evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
281 "Input device supported switch events");
285 evdev_register_common(struct evdev_dev *evdev)
289 debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
290 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
292 /* Initialize internal structures */
293 LIST_INIT(&evdev->ev_clients);
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, &evdev->ev_mtx, 0);
300 if (evdev->ev_rep[REP_DELAY] == 0 &&
301 evdev->ev_rep[REP_PERIOD] == 0) {
302 /* Supply default values */
303 evdev->ev_rep[REP_DELAY] = 250;
304 evdev->ev_rep[REP_PERIOD] = 33;
308 /* Initialize multitouch protocol type B states */
309 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
310 evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
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);
334 evdev_register(struct evdev_dev *evdev)
338 evdev->ev_lock_type = EV_LOCK_INTERNAL;
339 evdev->ev_lock = &evdev->ev_mtx;
340 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
342 ret = evdev_register_common(evdev);
344 mtx_destroy(&evdev->ev_mtx);
350 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
353 evdev->ev_lock_type = EV_LOCK_MTX;
354 evdev->ev_lock = mtx;
355 return (evdev_register_common(evdev));
359 evdev_unregister(struct evdev_dev *evdev)
361 struct evdev_client *client;
363 debugf(evdev, "%s: unregistered evdev provider: %s\n",
364 evdev->ev_shortname, evdev->ev_name);
366 sysctl_ctx_free(&evdev->ev_sysctl_ctx);
369 evdev->ev_cdev->si_drv1 = NULL;
370 /* Wake up sleepers */
371 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
372 evdev_revoke_client(client);
373 evdev_dispose_client(evdev, client);
374 EVDEV_CLIENT_LOCKQ(client);
375 evdev_notify_event(client);
376 EVDEV_CLIENT_UNLOCKQ(client);
380 /* destroy_dev can sleep so release lock */
381 ret = evdev_cdev_destroy(evdev);
382 evdev->ev_cdev = NULL;
383 if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL)
384 mtx_destroy(&evdev->ev_mtx);
386 evdev_free_absinfo(evdev->ev_absinfo);
387 evdev_mt_free(evdev);
393 evdev_set_name(struct evdev_dev *evdev, const char *name)
396 snprintf(evdev->ev_name, NAMELEN, "%s", name);
400 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
401 uint16_t product, uint16_t version)
404 evdev->ev_id = (struct input_id) {
413 evdev_set_phys(struct evdev_dev *evdev, const char *name)
416 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
420 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
423 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
427 evdev_set_methods(struct evdev_dev *evdev, void *softc,
428 const struct evdev_methods *methods)
431 evdev->ev_methods = methods;
432 evdev->ev_softc = softc;
436 evdev_get_softc(struct evdev_dev *evdev)
439 return (evdev->ev_softc);
443 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
446 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
447 bit_set(evdev->ev_prop_flags, prop);
451 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
454 KASSERT(type < EV_CNT, ("invalid evdev event property"));
455 bit_set(evdev->ev_type_flags, type);
459 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
462 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
463 bit_set(evdev->ev_key_flags, code);
467 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
470 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
471 bit_set(evdev->ev_rel_flags, code);
475 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value,
476 int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat,
479 struct input_absinfo absinfo;
481 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
483 absinfo = (struct input_absinfo) {
489 .resolution = resolution,
491 evdev_set_abs_bit(evdev, code);
492 evdev_set_absinfo(evdev, code, &absinfo);
496 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
499 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
500 if (evdev->ev_absinfo == NULL)
501 evdev->ev_absinfo = evdev_alloc_absinfo();
502 bit_set(evdev->ev_abs_flags, code);
506 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
509 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
510 bit_set(evdev->ev_msc_flags, code);
515 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
518 KASSERT(code < LED_CNT, ("invalid evdev led property"));
519 bit_set(evdev->ev_led_flags, code);
523 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
526 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
527 bit_set(evdev->ev_snd_flags, code);
531 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
534 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
535 bit_set(evdev->ev_sw_flags, code);
539 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
542 KASSERT(type < EV_CNT, ("invalid evdev event property"));
543 return (bit_test(evdev->ev_type_flags, type));
547 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
548 struct input_absinfo *absinfo)
551 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
553 if (axis == ABS_MT_SLOT &&
554 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
557 if (evdev->ev_absinfo == NULL)
558 evdev->ev_absinfo = evdev_alloc_absinfo();
560 if (axis == ABS_MT_SLOT)
561 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
563 memcpy(&evdev->ev_absinfo[axis], absinfo,
564 sizeof(struct input_absinfo));
568 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
571 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
572 evdev->ev_rep[property] = value;
576 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
579 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
580 bit_set(evdev->ev_flags, flag);
584 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
591 /* Allow SYN events implicitly */
592 if (type != EV_SYN && !evdev_event_supported(evdev, type))
604 if (!bit_test(evdev->ev_key_flags, code))
611 if (!bit_test(evdev->ev_rel_flags, code))
618 if (!bit_test(evdev->ev_abs_flags, code))
620 if (code == ABS_MT_SLOT &&
621 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
623 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
624 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
631 if (!bit_test(evdev->ev_msc_flags, code))
638 if (!bit_test(evdev->ev_led_flags, code))
645 if (!bit_test(evdev->ev_snd_flags, code))
652 if (!bit_test(evdev->ev_sw_flags, code))
669 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
673 EVDEV_LOCK_ASSERT(evdev);
677 if (!evdev_event_supported(evdev, EV_REP))
680 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
681 /* Detect driver key repeats. */
682 if (bit_test(evdev->ev_key_states, code) &&
683 *value == KEY_EVENT_DOWN)
684 *value = KEY_EVENT_REPEAT;
686 /* Start/stop callout for evdev repeats */
687 if (bit_test(evdev->ev_key_states, code) == !*value &&
688 !LIST_EMPTY(&evdev->ev_clients)) {
689 if (*value == KEY_EVENT_DOWN)
690 evdev_start_repeat(evdev, code);
692 evdev_stop_repeat(evdev);
698 /* TBD: implement fuzz */
703 static enum evdev_sparse_result
704 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
707 int32_t last_mt_slot;
709 EVDEV_LOCK_ASSERT(evdev);
712 * For certain event types, update device state bits
713 * and convert level reporting to edge reporting
720 if (bit_test(evdev->ev_key_states, code) == value)
721 return (EV_SKIP_EVENT);
722 bit_change(evdev->ev_key_states, code, value);
725 case KEY_EVENT_REPEAT:
726 if (bit_test(evdev->ev_key_states, code) == 0 ||
727 !evdev_event_supported(evdev, EV_REP))
728 return (EV_SKIP_EVENT);
732 return (EV_SKIP_EVENT);
737 if (bit_test(evdev->ev_led_states, code) == value)
738 return (EV_SKIP_EVENT);
739 bit_change(evdev->ev_led_states, code, value);
743 bit_change(evdev->ev_snd_states, code, value);
747 if (bit_test(evdev->ev_sw_states, code) == value)
748 return (EV_SKIP_EVENT);
749 bit_change(evdev->ev_sw_states, code, value);
753 if (evdev->ev_rep[code] == value)
754 return (EV_SKIP_EVENT);
755 evdev_set_repeat_params(evdev, code, value);
760 return (EV_SKIP_EVENT);
763 /* For EV_ABS, save last value in absinfo and ev_mt_states */
767 /* Postpone ABS_MT_SLOT till next event */
768 evdev_set_last_mt_slot(evdev, value);
769 return (EV_SKIP_EVENT);
771 case ABS_MT_FIRST ... ABS_MT_LAST:
772 /* Pass MT protocol type A events as is */
773 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
775 /* Don`t repeat MT protocol type B events */
776 last_mt_slot = evdev_get_last_mt_slot(evdev);
777 if (evdev_get_mt_value(evdev, last_mt_slot, code)
779 return (EV_SKIP_EVENT);
780 evdev_set_mt_value(evdev, last_mt_slot, code, value);
781 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
782 CURRENT_MT_SLOT(evdev) = last_mt_slot;
783 evdev->ev_report_opened = true;
784 return (EV_REPORT_MT_SLOT);
789 if (evdev->ev_absinfo[code].value == value)
790 return (EV_SKIP_EVENT);
791 evdev->ev_absinfo[code].value = value;
796 if (code == SYN_REPORT) {
797 /* Count empty reports as well as non empty */
798 evdev->ev_report_count++;
799 /* Skip empty reports */
800 if (!evdev->ev_report_opened)
801 return (EV_SKIP_EVENT);
802 evdev->ev_report_opened = false;
803 return (EV_REPORT_EVENT);
808 evdev->ev_report_opened = true;
809 return (EV_REPORT_EVENT);
813 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
816 struct evdev_client *client;
818 debugf(evdev, "%s pushed event %d/%d/%d",
819 evdev->ev_shortname, type, code, value);
821 EVDEV_LOCK_ASSERT(evdev);
823 /* Propagate event through all clients */
824 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
825 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
828 EVDEV_CLIENT_LOCKQ(client);
829 evdev_client_push(client, type, code, value);
830 if (type == EV_SYN && code == SYN_REPORT)
831 evdev_notify_event(client);
832 EVDEV_CLIENT_UNLOCKQ(client);
835 evdev->ev_event_count++;
839 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
842 enum evdev_sparse_result sparse;
844 EVDEV_LOCK_ASSERT(evdev);
846 sparse = evdev_sparse_event(evdev, type, code, value);
848 case EV_REPORT_MT_SLOT:
849 /* report postponed ABS_MT_SLOT */
850 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
851 CURRENT_MT_SLOT(evdev));
853 case EV_REPORT_EVENT:
854 evdev_propagate_event(evdev, type, code, value);
862 evdev_restore_after_kdb(struct evdev_dev *evdev)
866 EVDEV_LOCK_ASSERT(evdev);
868 /* Report postponed leds */
869 for (code = 0; code < LED_CNT; code++)
870 if (bit_test(evdev->ev_kdb_led_states, code))
871 evdev_send_event(evdev, EV_LED, code,
872 !bit_test(evdev->ev_led_states, code));
873 bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
875 /* Release stuck keys (CTRL + ALT + ESC) */
876 evdev_stop_repeat(evdev);
877 for (code = 0; code < KEY_CNT; code++) {
878 if (bit_test(evdev->ev_key_states, code)) {
879 evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
880 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
886 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
890 if (evdev_check_event(evdev, type, code, value) != 0)
894 * Discard all but LEDs kdb events as unrelated to userspace.
895 * Aggregate LED updates and postpone reporting until kdb deactivation.
897 if (kdb_active || SCHEDULER_STOPPED()) {
898 evdev->ev_kdb_active = true;
900 bit_set(evdev->ev_kdb_led_states,
901 bit_test(evdev->ev_led_states, code) != value);
907 /* Fix evdev state corrupted with discarding of kdb events */
908 if (evdev->ev_kdb_active) {
909 evdev->ev_kdb_active = false;
910 evdev_restore_after_kdb(evdev);
913 evdev_modify_event(evdev, type, code, &value);
914 if (type == EV_SYN && code == SYN_REPORT &&
915 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
916 evdev_send_mt_autorel(evdev);
917 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
918 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
919 evdev_send_mt_compat(evdev);
920 evdev_send_event(evdev, type, code, value);
928 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
935 /* evdev repeats should not be processed by hardware driver */
936 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
943 if (evdev->ev_methods != NULL &&
944 evdev->ev_methods->ev_event != NULL)
945 evdev->ev_methods->ev_event(evdev, type, code, value);
947 * Leds and driver repeats should be reported in ev_event
948 * method body to interoperate with kbdmux states and rates
949 * propagation so both ways (ioctl and evdev) of changing it
950 * will produce only one evdev event report to client.
952 if (type == EV_LED || type == EV_REP)
961 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
963 ret = evdev_push_event(evdev, type, code, value);
964 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
976 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
980 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
982 EVDEV_LOCK_ASSERT(evdev);
984 if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
985 evdev->ev_methods->ev_open != NULL) {
986 debugf(evdev, "calling ev_open() on device %s",
987 evdev->ev_shortname);
988 ret = evdev->ev_methods->ev_open(evdev);
991 LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
996 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
998 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1000 EVDEV_LOCK_ASSERT(evdev);
1002 LIST_REMOVE(client, ec_link);
1003 if (LIST_EMPTY(&evdev->ev_clients)) {
1004 if (evdev->ev_methods != NULL &&
1005 evdev->ev_methods->ev_close != NULL)
1006 (void)evdev->ev_methods->ev_close(evdev);
1007 if (evdev_event_supported(evdev, EV_REP) &&
1008 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
1009 evdev_stop_repeat(evdev);
1011 evdev_release_client(evdev, client);
1015 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1018 EVDEV_LOCK_ASSERT(evdev);
1020 if (evdev->ev_grabber != NULL)
1023 evdev->ev_grabber = client;
1029 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1032 EVDEV_LOCK_ASSERT(evdev);
1034 if (evdev->ev_grabber != client)
1037 evdev->ev_grabber = NULL;
1043 evdev_repeat_callout(void *arg)
1045 struct evdev_dev *evdev = (struct evdev_dev *)arg;
1047 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1048 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1050 if (evdev->ev_rep[REP_PERIOD])
1051 callout_reset(&evdev->ev_rep_callout,
1052 evdev->ev_rep[REP_PERIOD] * hz / 1000,
1053 evdev_repeat_callout, evdev);
1055 evdev->ev_rep_key = KEY_RESERVED;
1059 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1062 EVDEV_LOCK_ASSERT(evdev);
1064 if (evdev->ev_rep[REP_DELAY]) {
1065 evdev->ev_rep_key = key;
1066 callout_reset(&evdev->ev_rep_callout,
1067 evdev->ev_rep[REP_DELAY] * hz / 1000,
1068 evdev_repeat_callout, evdev);
1073 evdev_stop_repeat(struct evdev_dev *evdev)
1076 EVDEV_LOCK_ASSERT(evdev);
1078 if (evdev->ev_rep_key != KEY_RESERVED) {
1079 callout_stop(&evdev->ev_rep_callout);
1080 evdev->ev_rep_key = KEY_RESERVED;
1084 MODULE_VERSION(evdev, 1);