2 * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3 * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@cicgroup.ru>
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/types.h>
33 #include <sys/systm.h>
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/bitstring.h>
40 #include <sys/sysctl.h>
42 #include <dev/evdev/input.h>
43 #include <dev/evdev/evdev.h>
44 #include <dev/evdev/evdev_private.h>
47 #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args)
49 #define debugf(evdev, fmt, args...)
53 FEATURE(evdev, "Input event devices support");
56 enum evdev_sparse_result
58 EV_SKIP_EVENT, /* Event value not changed */
59 EV_REPORT_EVENT, /* Event value changed */
60 EV_REPORT_MT_SLOT, /* Event value and MT slot number changed */
63 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
65 int evdev_rcpt_mask = EVDEV_RCPT_SYSMOUSE | EVDEV_RCPT_KBDMUX;
67 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args");
68 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RW, &evdev_rcpt_mask, 0,
69 "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
70 "bit2 - mouse hardware, bit3 - keyboard hardware");
72 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
73 static void evdev_stop_repeat(struct evdev_dev *);
74 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
77 bit_change(bitstr_t *bitstr, int bit, int value)
82 bit_clear(bitstr, bit);
89 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
93 evdev_free(struct evdev_dev *evdev)
96 if (evdev != NULL && evdev->ev_cdev != NULL &&
97 evdev->ev_cdev->si_drv1 != NULL)
98 evdev_unregister(evdev);
100 free(evdev, M_EVDEV);
103 static struct input_absinfo *
104 evdev_alloc_absinfo(void)
107 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
112 evdev_free_absinfo(struct input_absinfo *absinfo)
115 free(absinfo, M_EVDEV);
119 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
121 if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
122 MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
125 evdev->ev_report_size = report_size;
130 evdev_estimate_report_size(struct evdev_dev *evdev)
136 * Keyboards generate one event per report but other devices with
137 * buttons like mouses can report events simultaneously
139 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
141 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
143 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
146 /* All relative axes can be reported simultaneously */
147 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
151 * All absolute axes can be reported simultaneously.
152 * Multitouch axes can be reported ABS_MT_SLOT times
154 if (evdev->ev_absinfo != NULL) {
155 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
157 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
159 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */
160 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
162 size += res * MAXIMAL_MT_SLOT(evdev);
165 size += res * (MAX_MT_REPORTS - 1);
169 /* All misc events can be reported simultaneously */
170 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
173 /* All leds can be reported simultaneously */
174 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
177 /* Assume other events are generated once per report */
178 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
181 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
184 /* XXX: FF part is not implemented yet */
186 size++; /* SYN_REPORT */
191 evdev_register(struct evdev_dev *evdev)
195 debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
196 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
198 /* Initialize internal structures */
199 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
200 LIST_INIT(&evdev->ev_clients);
202 if (evdev_event_supported(evdev, EV_REP) &&
203 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
204 /* Initialize callout */
205 callout_init_mtx(&evdev->ev_rep_callout, &evdev->ev_mtx, 0);
207 if (evdev->ev_rep[REP_DELAY] == 0 &&
208 evdev->ev_rep[REP_PERIOD] == 0) {
209 /* Supply default values */
210 evdev->ev_rep[REP_DELAY] = 250;
211 evdev->ev_rep[REP_PERIOD] = 33;
215 /* Initialize multitouch protocol type B states */
216 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
217 evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
218 evdev_mt_init(evdev);
220 /* Estimate maximum report size */
221 if (evdev->ev_report_size == 0) {
222 ret = evdev_set_report_size(evdev,
223 evdev_estimate_report_size(evdev));
228 /* Create char device node */
229 ret = evdev_cdev_create(evdev);
232 mtx_destroy(&evdev->ev_mtx);
238 evdev_unregister(struct evdev_dev *evdev)
240 struct evdev_client *client;
242 debugf(evdev, "%s: unregistered evdev provider: %s\n",
243 evdev->ev_shortname, evdev->ev_name);
246 evdev->ev_cdev->si_drv1 = NULL;
247 /* Wake up sleepers */
248 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
249 evdev_revoke_client(client);
250 evdev_dispose_client(evdev, client);
251 EVDEV_CLIENT_LOCKQ(client);
252 evdev_notify_event(client);
253 EVDEV_CLIENT_UNLOCKQ(client);
257 /* destroy_dev can sleep so release lock */
258 ret = evdev_cdev_destroy(evdev);
259 evdev->ev_cdev = NULL;
261 mtx_destroy(&evdev->ev_mtx);
263 evdev_free_absinfo(evdev->ev_absinfo);
264 evdev_mt_free(evdev);
270 evdev_set_name(struct evdev_dev *evdev, const char *name)
273 snprintf(evdev->ev_name, NAMELEN, "%s", name);
277 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
278 uint16_t product, uint16_t version)
281 evdev->ev_id = (struct input_id) {
290 evdev_set_phys(struct evdev_dev *evdev, const char *name)
293 snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
297 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
300 snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
304 evdev_set_methods(struct evdev_dev *evdev, void *softc,
305 const struct evdev_methods *methods)
308 evdev->ev_methods = methods;
309 evdev->ev_softc = softc;
313 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
316 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
317 bit_set(evdev->ev_prop_flags, prop);
321 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
324 KASSERT(type < EV_CNT, ("invalid evdev event property"));
325 bit_set(evdev->ev_type_flags, type);
329 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
332 KASSERT(code < KEY_CNT, ("invalid evdev key property"));
333 bit_set(evdev->ev_key_flags, code);
337 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
340 KASSERT(code < REL_CNT, ("invalid evdev rel property"));
341 bit_set(evdev->ev_rel_flags, code);
345 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value,
346 int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat,
349 struct input_absinfo absinfo;
351 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
353 absinfo = (struct input_absinfo) {
359 .resolution = resolution,
361 evdev_set_abs_bit(evdev, code);
362 evdev_set_absinfo(evdev, code, &absinfo);
366 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
369 KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
370 if (evdev->ev_absinfo == NULL)
371 evdev->ev_absinfo = evdev_alloc_absinfo();
372 bit_set(evdev->ev_abs_flags, code);
376 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
379 KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
380 bit_set(evdev->ev_msc_flags, code);
385 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
388 KASSERT(code < LED_CNT, ("invalid evdev led property"));
389 bit_set(evdev->ev_led_flags, code);
393 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
396 KASSERT(code < SND_CNT, ("invalid evdev snd property"));
397 bit_set(evdev->ev_snd_flags, code);
401 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
404 KASSERT(code < SW_CNT, ("invalid evdev sw property"));
405 bit_set(evdev->ev_sw_flags, code);
409 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
412 KASSERT(type < EV_CNT, ("invalid evdev event property"));
413 return (bit_test(evdev->ev_type_flags, type));
417 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
418 struct input_absinfo *absinfo)
421 KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
423 if (axis == ABS_MT_SLOT &&
424 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
427 if (evdev->ev_absinfo == NULL)
428 evdev->ev_absinfo = evdev_alloc_absinfo();
430 if (axis == ABS_MT_SLOT)
431 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
433 memcpy(&evdev->ev_absinfo[axis], absinfo,
434 sizeof(struct input_absinfo));
438 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
441 KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
442 evdev->ev_rep[property] = value;
446 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
449 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
450 bit_set(evdev->ev_flags, flag);
454 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
461 /* Allow SYN events implicitly */
462 if (type != EV_SYN && !evdev_event_supported(evdev, type))
474 if (!bit_test(evdev->ev_key_flags, code))
481 if (!bit_test(evdev->ev_rel_flags, code))
488 if (!bit_test(evdev->ev_abs_flags, code))
490 if (code == ABS_MT_SLOT &&
491 (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
493 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
494 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
501 if (!bit_test(evdev->ev_msc_flags, code))
508 if (!bit_test(evdev->ev_led_flags, code))
515 if (!bit_test(evdev->ev_snd_flags, code))
522 if (!bit_test(evdev->ev_sw_flags, code))
539 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
543 EVDEV_LOCK_ASSERT(evdev);
547 if (!evdev_event_supported(evdev, EV_REP))
550 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
551 /* Detect driver key repeats. */
552 if (bit_test(evdev->ev_key_states, code) &&
553 *value == KEY_EVENT_DOWN)
554 *value = KEY_EVENT_REPEAT;
556 /* Start/stop callout for evdev repeats */
557 if (bit_test(evdev->ev_key_states, code) == !*value) {
558 if (*value == KEY_EVENT_DOWN)
559 evdev_start_repeat(evdev, code);
561 evdev_stop_repeat(evdev);
567 /* TBD: implement fuzz */
572 static enum evdev_sparse_result
573 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
576 int32_t last_mt_slot;
578 EVDEV_LOCK_ASSERT(evdev);
581 * For certain event types, update device state bits
582 * and convert level reporting to edge reporting
589 if (bit_test(evdev->ev_key_states, code) == value)
590 return (EV_SKIP_EVENT);
591 bit_change(evdev->ev_key_states, code, value);
594 case KEY_EVENT_REPEAT:
595 if (bit_test(evdev->ev_key_states, code) == 0 ||
596 !evdev_event_supported(evdev, EV_REP))
597 return (EV_SKIP_EVENT);
601 return (EV_SKIP_EVENT);
606 if (bit_test(evdev->ev_led_states, code) == value)
607 return (EV_SKIP_EVENT);
608 bit_change(evdev->ev_led_states, code, value);
612 if (bit_test(evdev->ev_snd_states, code) == value)
613 return (EV_SKIP_EVENT);
614 bit_change(evdev->ev_snd_states, code, value);
618 if (bit_test(evdev->ev_sw_states, code) == value)
619 return (EV_SKIP_EVENT);
620 bit_change(evdev->ev_sw_states, code, value);
624 if (evdev->ev_rep[code] == value)
625 return (EV_SKIP_EVENT);
626 evdev_set_repeat_params(evdev, code, value);
631 return (EV_SKIP_EVENT);
634 /* For EV_ABS, save last value in absinfo and ev_mt_states */
638 /* Postpone ABS_MT_SLOT till next event */
639 evdev_set_last_mt_slot(evdev, value);
640 return (EV_SKIP_EVENT);
642 case ABS_MT_FIRST ... ABS_MT_LAST:
643 /* Pass MT protocol type A events as is */
644 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
646 /* Don`t repeat MT protocol type B events */
647 last_mt_slot = evdev_get_last_mt_slot(evdev);
648 if (evdev_get_mt_value(evdev, last_mt_slot, code)
650 return (EV_SKIP_EVENT);
651 evdev_set_mt_value(evdev, last_mt_slot, code, value);
652 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
653 CURRENT_MT_SLOT(evdev) = last_mt_slot;
654 evdev->ev_report_opened = true;
655 return (EV_REPORT_MT_SLOT);
660 if (evdev->ev_absinfo[code].value == value)
661 return (EV_SKIP_EVENT);
662 evdev->ev_absinfo[code].value = value;
667 if (code == SYN_REPORT) {
668 /* Skip empty reports */
669 if (!evdev->ev_report_opened)
670 return (EV_SKIP_EVENT);
671 evdev->ev_report_opened = false;
672 return (EV_REPORT_EVENT);
677 evdev->ev_report_opened = true;
678 return (EV_REPORT_EVENT);
682 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
685 struct evdev_client *client;
687 debugf(evdev, "%s pushed event %d/%d/%d",
688 evdev->ev_shortname, type, code, value);
690 EVDEV_LOCK_ASSERT(evdev);
692 /* Propagate event through all clients */
693 LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
694 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
697 EVDEV_CLIENT_LOCKQ(client);
698 evdev_client_push(client, type, code, value);
699 if (type == EV_SYN && code == SYN_REPORT)
700 evdev_notify_event(client);
701 EVDEV_CLIENT_UNLOCKQ(client);
704 /* Update counters */
705 evdev->ev_event_count++;
706 if (type == EV_SYN && code == SYN_REPORT)
707 evdev->ev_report_count++;
711 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
714 enum evdev_sparse_result sparse;
716 EVDEV_LOCK_ASSERT(evdev);
718 sparse = evdev_sparse_event(evdev, type, code, value);
720 case EV_REPORT_MT_SLOT:
721 /* report postponed ABS_MT_SLOT */
722 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
723 CURRENT_MT_SLOT(evdev));
725 case EV_REPORT_EVENT:
726 evdev_propagate_event(evdev, type, code, value);
734 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
738 if (evdev_check_event(evdev, type, code, value) != 0)
742 evdev_modify_event(evdev, type, code, &value);
743 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
744 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
745 evdev_send_mt_compat(evdev);
746 evdev_send_event(evdev, type, code, value);
753 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
760 /* evdev repeats should not be processed by hardware driver */
761 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
768 if (evdev->ev_methods != NULL &&
769 evdev->ev_methods->ev_event != NULL)
770 evdev->ev_methods->ev_event(evdev, evdev->ev_softc,
773 * Leds and driver repeats should be reported in ev_event
774 * method body to interoperate with kbdmux states and rates
775 * propagation so both ways (ioctl and evdev) of changing it
776 * will produce only one evdev event report to client.
778 if (type == EV_LED || type == EV_REP)
787 ret = evdev_push_event(evdev, type, code, value);
798 evdev_sync(struct evdev_dev *evdev)
801 return (evdev_push_event(evdev, EV_SYN, SYN_REPORT, 1));
806 evdev_mt_sync(struct evdev_dev *evdev)
809 return (evdev_push_event(evdev, EV_SYN, SYN_MT_REPORT, 1));
813 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
817 debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
819 EVDEV_LOCK_ASSERT(evdev);
821 if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
822 evdev->ev_methods->ev_open != NULL) {
823 debugf(evdev, "calling ev_open() on device %s",
824 evdev->ev_shortname);
825 ret = evdev->ev_methods->ev_open(evdev, evdev->ev_softc);
828 LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
833 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
835 debugf(evdev, "removing client for device %s", evdev->ev_shortname);
837 EVDEV_LOCK_ASSERT(evdev);
839 LIST_REMOVE(client, ec_link);
840 if (LIST_EMPTY(&evdev->ev_clients)) {
841 if (evdev->ev_methods != NULL &&
842 evdev->ev_methods->ev_close != NULL)
843 evdev->ev_methods->ev_close(evdev, evdev->ev_softc);
844 if (evdev_event_supported(evdev, EV_REP) &&
845 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
846 evdev_stop_repeat(evdev);
848 evdev_release_client(evdev, client);
852 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
855 EVDEV_LOCK_ASSERT(evdev);
857 if (evdev->ev_grabber != NULL)
860 evdev->ev_grabber = client;
866 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
869 EVDEV_LOCK_ASSERT(evdev);
871 if (evdev->ev_grabber != client)
874 evdev->ev_grabber = NULL;
880 evdev_repeat_callout(void *arg)
882 struct evdev_dev *evdev = (struct evdev_dev *)arg;
884 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
885 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
887 if (evdev->ev_rep[REP_PERIOD])
888 callout_reset(&evdev->ev_rep_callout,
889 evdev->ev_rep[REP_PERIOD] * hz / 1000,
890 evdev_repeat_callout, evdev);
892 evdev->ev_rep_key = KEY_RESERVED;
896 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
899 EVDEV_LOCK_ASSERT(evdev);
901 if (evdev->ev_rep[REP_DELAY]) {
902 evdev->ev_rep_key = key;
903 callout_reset(&evdev->ev_rep_callout,
904 evdev->ev_rep[REP_DELAY] * hz / 1000,
905 evdev_repeat_callout, evdev);
910 evdev_stop_repeat(struct evdev_dev *evdev)
913 EVDEV_LOCK_ASSERT(evdev);
915 if (evdev->ev_rep_key != KEY_RESERVED) {
916 callout_stop(&evdev->ev_rep_callout);
917 evdev->ev_rep_key = KEY_RESERVED;
921 MODULE_VERSION(evdev, 1);