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>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
41 #include <dev/evdev/evdev.h>
42 #include <dev/evdev/evdev_private.h>
43 #include <dev/evdev/input.h>
46 #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args)
48 #define debugf(evdev, fmt, args...)
52 FEATURE(evdev, "Input event devices support");
54 FEATURE(evdev_support, "Evdev support in hybrid drivers");
58 enum evdev_sparse_result
60 EV_SKIP_EVENT, /* Event value not changed */
61 EV_REPORT_EVENT, /* Event value changed */
62 EV_REPORT_MT_SLOT, /* Event value and MT slot number changed */
65 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
67 int evdev_rcpt_mask = EVDEV_RCPT_SYSMOUSE | EVDEV_RCPT_KBDMUX;
68 int evdev_sysmouse_t_axis = 0;
70 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args");
72 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RW, &evdev_rcpt_mask, 0,
73 "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
74 "bit2 - mouse hardware, bit3 - keyboard hardware");
75 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RW,
76 &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm");
78 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD, 0,
79 "Evdev input devices");
81 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
82 static void evdev_stop_repeat(struct evdev_dev *);
83 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
86 bit_change(bitstr_t *bitstr, int bit, int value)
91 bit_clear(bitstr, bit);
98 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
102 evdev_free(struct evdev_dev *evdev)
105 if (evdev != NULL && evdev->ev_cdev != NULL &&
106 evdev->ev_cdev->si_drv1 != NULL)
107 evdev_unregister(evdev);
109 free(evdev, M_EVDEV);
112 static struct input_absinfo *
113 evdev_alloc_absinfo(void)
116 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
121 evdev_free_absinfo(struct input_absinfo *absinfo)
124 free(absinfo, M_EVDEV);
128 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
130 if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
131 MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
134 evdev->ev_report_size = report_size;
139 evdev_estimate_report_size(struct evdev_dev *evdev)
145 * Keyboards generate one event per report but other devices with
146 * buttons like mouses can report events simultaneously
148 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
150 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
152 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
155 /* All relative axes can be reported simultaneously */
156 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
160 * All absolute axes can be reported simultaneously.
161 * Multitouch axes can be reported ABS_MT_SLOT times
163 if (evdev->ev_absinfo != NULL) {
164 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
166 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
168 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */
169 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
171 size += res * MAXIMAL_MT_SLOT(evdev);
174 size += res * (MAX_MT_REPORTS - 1);
178 /* All misc events can be reported simultaneously */
179 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
182 /* All leds can be reported simultaneously */
183 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
186 /* Assume other events are generated once per report */
187 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
190 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
193 /* XXX: FF part is not implemented yet */
195 size++; /* SYN_REPORT */
200 evdev_sysctl_create(struct evdev_dev *evdev)
202 struct sysctl_oid *ev_sysctl_tree;
205 snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
206 sysctl_ctx_init(&evdev->ev_sysctl_ctx);
208 ev_sysctl_tree = SYSCTL_ADD_NODE(&evdev->ev_sysctl_ctx,
209 SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO,
210 ev_unit_str, CTLFLAG_RD, NULL, "");
212 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
213 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
215 "Input device name");
217 SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
218 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
219 &evdev->ev_id, input_id,
220 "Input device identification");
222 /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
223 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
224 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
225 evdev->ev_shortname, 0,
226 "Input device short name");
228 /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
229 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
230 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
232 "Input device unique number");
234 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
235 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
236 evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
237 "Input device properties");
239 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
240 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
241 evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
242 "Input device supported events types");
244 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
245 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
246 evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
247 "", "Input device supported keys");
249 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
250 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
251 evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
252 "Input device supported relative events");
254 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
255 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
256 evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
257 "Input device supported absolute events");
259 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
260 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
261 evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
262 "Input device supported miscellaneous events");
264 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
265 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
266 evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
267 "Input device supported LED events");
269 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
270 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
271 evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
272 "Input device supported sound events");
274 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
275 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
276 evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
277 "Input device supported switch events");
281 evdev_register_common(struct evdev_dev *evdev)
285 debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
286 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
288 /* Initialize internal structures */
289 LIST_INIT(&evdev->ev_clients);
291 if (evdev_event_supported(evdev, EV_REP) &&
292 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
293 /* Initialize callout */
294 callout_init_mtx(&evdev->ev_rep_callout, &evdev->ev_mtx, 0);
296 if (evdev->ev_rep[REP_DELAY] == 0 &&
297 evdev->ev_rep[REP_PERIOD] == 0) {
298 /* Supply default values */
299 evdev->ev_rep[REP_DELAY] = 250;
300 evdev->ev_rep[REP_PERIOD] = 33;
304 /* Initialize multitouch protocol type B states */
305 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
306 evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
307 evdev_mt_init(evdev);
309 /* Estimate maximum report size */
310 if (evdev->ev_report_size == 0) {
311 ret = evdev_set_report_size(evdev,
312 evdev_estimate_report_size(evdev));
317 /* Create char device node */
318 ret = evdev_cdev_create(evdev);
322 /* Create sysctls (for device enumeration without /dev/input access rights) */
323 evdev_sysctl_create(evdev);
330 evdev_register(struct evdev_dev *evdev)
334 evdev->ev_lock_type = EV_LOCK_INTERNAL;
335 evdev->ev_lock = &evdev->ev_mtx;
336 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
338 ret = evdev_register_common(evdev);
340 mtx_destroy(&evdev->ev_mtx);
346 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
349 evdev->ev_lock_type = EV_LOCK_MTX;
350 evdev->ev_lock = mtx;
351 return (evdev_register_common(evdev));
355 evdev_unregister(struct evdev_dev *evdev)
357 struct evdev_client *client;
359 debugf(evdev, "%s: unregistered evdev provider: %s\n",
360 evdev->ev_shortname, evdev->ev_name);
362 sysctl_ctx_free(&evdev->ev_sysctl_ctx);
365 evdev->ev_cdev->si_drv1 = NULL;
366 /* Wake up sleepers */
367 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
368 evdev_revoke_client(client);
369 evdev_dispose_client(evdev, client);
370 EVDEV_CLIENT_LOCKQ(client);
371 evdev_notify_event(client);
372 EVDEV_CLIENT_UNLOCKQ(client);
376 /* destroy_dev can sleep so release lock */
377 ret = evdev_cdev_destroy(evdev);
378 evdev->ev_cdev = NULL;
379 if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL)
380 mtx_destroy(&evdev->ev_mtx);
382 evdev_free_absinfo(evdev->ev_absinfo);
383 evdev_mt_free(evdev);
389 evdev_set_name(struct evdev_dev *evdev, const char *name)
392 snprintf(evdev->ev_name, NAMELEN, "%s", name);
396 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
397 uint16_t product, uint16_t version)
400 evdev->ev_id = (struct input_id) {
409 evdev_set_phys(struct evdev_dev *evdev, const char *name)
412 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
416 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
419 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
423 evdev_set_methods(struct evdev_dev *evdev, void *softc,
424 const struct evdev_methods *methods)
427 evdev->ev_methods = methods;
428 evdev->ev_softc = softc;
432 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
435 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
436 bit_set(evdev->ev_prop_flags, prop);
440 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
443 KASSERT(type < EV_CNT, ("invalid evdev event property"));
444 bit_set(evdev->ev_type_flags, type);
448 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
451 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
452 bit_set(evdev->ev_key_flags, code);
456 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
459 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
460 bit_set(evdev->ev_rel_flags, code);
464 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value,
465 int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat,
468 struct input_absinfo absinfo;
470 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
472 absinfo = (struct input_absinfo) {
478 .resolution = resolution,
480 evdev_set_abs_bit(evdev, code);
481 evdev_set_absinfo(evdev, code, &absinfo);
485 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
488 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
489 if (evdev->ev_absinfo == NULL)
490 evdev->ev_absinfo = evdev_alloc_absinfo();
491 bit_set(evdev->ev_abs_flags, code);
495 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
498 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
499 bit_set(evdev->ev_msc_flags, code);
504 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
507 KASSERT(code < LED_CNT, ("invalid evdev led property"));
508 bit_set(evdev->ev_led_flags, code);
512 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
515 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
516 bit_set(evdev->ev_snd_flags, code);
520 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
523 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
524 bit_set(evdev->ev_sw_flags, code);
528 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
531 KASSERT(type < EV_CNT, ("invalid evdev event property"));
532 return (bit_test(evdev->ev_type_flags, type));
536 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
537 struct input_absinfo *absinfo)
540 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
542 if (axis == ABS_MT_SLOT &&
543 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
546 if (evdev->ev_absinfo == NULL)
547 evdev->ev_absinfo = evdev_alloc_absinfo();
549 if (axis == ABS_MT_SLOT)
550 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
552 memcpy(&evdev->ev_absinfo[axis], absinfo,
553 sizeof(struct input_absinfo));
557 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
560 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
561 evdev->ev_rep[property] = value;
565 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
568 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
569 bit_set(evdev->ev_flags, flag);
573 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
580 /* Allow SYN events implicitly */
581 if (type != EV_SYN && !evdev_event_supported(evdev, type))
593 if (!bit_test(evdev->ev_key_flags, code))
600 if (!bit_test(evdev->ev_rel_flags, code))
607 if (!bit_test(evdev->ev_abs_flags, code))
609 if (code == ABS_MT_SLOT &&
610 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
612 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
613 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
620 if (!bit_test(evdev->ev_msc_flags, code))
627 if (!bit_test(evdev->ev_led_flags, code))
634 if (!bit_test(evdev->ev_snd_flags, code))
641 if (!bit_test(evdev->ev_sw_flags, code))
658 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
662 EVDEV_LOCK_ASSERT(evdev);
666 if (!evdev_event_supported(evdev, EV_REP))
669 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
670 /* Detect driver key repeats. */
671 if (bit_test(evdev->ev_key_states, code) &&
672 *value == KEY_EVENT_DOWN)
673 *value = KEY_EVENT_REPEAT;
675 /* Start/stop callout for evdev repeats */
676 if (bit_test(evdev->ev_key_states, code) == !*value &&
677 !LIST_EMPTY(&evdev->ev_clients)) {
678 if (*value == KEY_EVENT_DOWN)
679 evdev_start_repeat(evdev, code);
681 evdev_stop_repeat(evdev);
687 /* TBD: implement fuzz */
692 static enum evdev_sparse_result
693 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
696 int32_t last_mt_slot;
698 EVDEV_LOCK_ASSERT(evdev);
701 * For certain event types, update device state bits
702 * and convert level reporting to edge reporting
709 if (bit_test(evdev->ev_key_states, code) == value)
710 return (EV_SKIP_EVENT);
711 bit_change(evdev->ev_key_states, code, value);
714 case KEY_EVENT_REPEAT:
715 if (bit_test(evdev->ev_key_states, code) == 0 ||
716 !evdev_event_supported(evdev, EV_REP))
717 return (EV_SKIP_EVENT);
721 return (EV_SKIP_EVENT);
726 if (bit_test(evdev->ev_led_states, code) == value)
727 return (EV_SKIP_EVENT);
728 bit_change(evdev->ev_led_states, code, value);
732 bit_change(evdev->ev_snd_states, code, value);
736 if (bit_test(evdev->ev_sw_states, code) == value)
737 return (EV_SKIP_EVENT);
738 bit_change(evdev->ev_sw_states, code, value);
742 if (evdev->ev_rep[code] == value)
743 return (EV_SKIP_EVENT);
744 evdev_set_repeat_params(evdev, code, value);
749 return (EV_SKIP_EVENT);
752 /* For EV_ABS, save last value in absinfo and ev_mt_states */
756 /* Postpone ABS_MT_SLOT till next event */
757 evdev_set_last_mt_slot(evdev, value);
758 return (EV_SKIP_EVENT);
760 case ABS_MT_FIRST ... ABS_MT_LAST:
761 /* Pass MT protocol type A events as is */
762 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
764 /* Don`t repeat MT protocol type B events */
765 last_mt_slot = evdev_get_last_mt_slot(evdev);
766 if (evdev_get_mt_value(evdev, last_mt_slot, code)
768 return (EV_SKIP_EVENT);
769 evdev_set_mt_value(evdev, last_mt_slot, code, value);
770 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
771 CURRENT_MT_SLOT(evdev) = last_mt_slot;
772 evdev->ev_report_opened = true;
773 return (EV_REPORT_MT_SLOT);
778 if (evdev->ev_absinfo[code].value == value)
779 return (EV_SKIP_EVENT);
780 evdev->ev_absinfo[code].value = value;
785 if (code == SYN_REPORT) {
786 /* Count empty reports as well as non empty */
787 evdev->ev_report_count++;
788 /* Skip empty reports */
789 if (!evdev->ev_report_opened)
790 return (EV_SKIP_EVENT);
791 evdev->ev_report_opened = false;
792 return (EV_REPORT_EVENT);
797 evdev->ev_report_opened = true;
798 return (EV_REPORT_EVENT);
802 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
805 struct evdev_client *client;
807 debugf(evdev, "%s pushed event %d/%d/%d",
808 evdev->ev_shortname, type, code, value);
810 EVDEV_LOCK_ASSERT(evdev);
812 /* Propagate event through all clients */
813 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
814 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
817 EVDEV_CLIENT_LOCKQ(client);
818 evdev_client_push(client, type, code, value);
819 if (type == EV_SYN && code == SYN_REPORT)
820 evdev_notify_event(client);
821 EVDEV_CLIENT_UNLOCKQ(client);
824 evdev->ev_event_count++;
828 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
831 enum evdev_sparse_result sparse;
833 EVDEV_LOCK_ASSERT(evdev);
835 sparse = evdev_sparse_event(evdev, type, code, value);
837 case EV_REPORT_MT_SLOT:
838 /* report postponed ABS_MT_SLOT */
839 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
840 CURRENT_MT_SLOT(evdev));
842 case EV_REPORT_EVENT:
843 evdev_propagate_event(evdev, type, code, value);
851 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
855 if (evdev_check_event(evdev, type, code, value) != 0)
860 evdev_modify_event(evdev, type, code, &value);
861 if (type == EV_SYN && code == SYN_REPORT &&
862 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
863 evdev_send_mt_autorel(evdev);
864 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
865 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
866 evdev_send_mt_compat(evdev);
867 evdev_send_event(evdev, type, code, value);
875 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
882 /* evdev repeats should not be processed by hardware driver */
883 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
890 if (evdev->ev_methods != NULL &&
891 evdev->ev_methods->ev_event != NULL)
892 evdev->ev_methods->ev_event(evdev, evdev->ev_softc,
895 * Leds and driver repeats should be reported in ev_event
896 * method body to interoperate with kbdmux states and rates
897 * propagation so both ways (ioctl and evdev) of changing it
898 * will produce only one evdev event report to client.
900 if (type == EV_LED || type == EV_REP)
909 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
911 ret = evdev_push_event(evdev, type, code, value);
912 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
924 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
928 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
930 EVDEV_LOCK_ASSERT(evdev);
932 if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
933 evdev->ev_methods->ev_open != NULL) {
934 debugf(evdev, "calling ev_open() on device %s",
935 evdev->ev_shortname);
936 ret = evdev->ev_methods->ev_open(evdev, evdev->ev_softc);
939 LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
944 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
946 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
948 EVDEV_LOCK_ASSERT(evdev);
950 LIST_REMOVE(client, ec_link);
951 if (LIST_EMPTY(&evdev->ev_clients)) {
952 if (evdev->ev_methods != NULL &&
953 evdev->ev_methods->ev_close != NULL)
954 evdev->ev_methods->ev_close(evdev, evdev->ev_softc);
955 if (evdev_event_supported(evdev, EV_REP) &&
956 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
957 evdev_stop_repeat(evdev);
959 evdev_release_client(evdev, client);
963 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
966 EVDEV_LOCK_ASSERT(evdev);
968 if (evdev->ev_grabber != NULL)
971 evdev->ev_grabber = client;
977 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
980 EVDEV_LOCK_ASSERT(evdev);
982 if (evdev->ev_grabber != client)
985 evdev->ev_grabber = NULL;
991 evdev_repeat_callout(void *arg)
993 struct evdev_dev *evdev = (struct evdev_dev *)arg;
995 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
996 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
998 if (evdev->ev_rep[REP_PERIOD])
999 callout_reset(&evdev->ev_rep_callout,
1000 evdev->ev_rep[REP_PERIOD] * hz / 1000,
1001 evdev_repeat_callout, evdev);
1003 evdev->ev_rep_key = KEY_RESERVED;
1007 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1010 EVDEV_LOCK_ASSERT(evdev);
1012 if (evdev->ev_rep[REP_DELAY]) {
1013 evdev->ev_rep_key = key;
1014 callout_reset(&evdev->ev_rep_callout,
1015 evdev->ev_rep[REP_DELAY] * hz / 1000,
1016 evdev_repeat_callout, evdev);
1021 evdev_stop_repeat(struct evdev_dev *evdev)
1024 EVDEV_LOCK_ASSERT(evdev);
1026 if (evdev->ev_rep_key != KEY_RESERVED) {
1027 callout_stop(&evdev->ev_rep_callout);
1028 evdev->ev_rep_key = KEY_RESERVED;
1032 MODULE_VERSION(evdev, 1);