diff --git a/sys/dev/evdev/evdev.c b/sys/dev/evdev/evdev.c index 74335b6f40b1..2fd7c2e201ea 100644 --- a/sys/dev/evdev/evdev.c +++ b/sys/dev/evdev/evdev.c @@ -1,1143 +1,1144 @@ /*- * Copyright (c) 2014 Jakub Wojciech Klama * Copyright (c) 2015-2016 Vladimir Kondratyev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include "opt_evdev.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef EVDEV_DEBUG #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args) #else #define debugf(evdev, fmt, args...) #endif #ifdef FEATURE FEATURE(evdev, "Input event devices support"); #ifdef EVDEV_SUPPORT FEATURE(evdev_support, "Evdev support in hybrid drivers"); #endif #endif enum evdev_sparse_result { EV_SKIP_EVENT, /* Event value not changed */ EV_REPORT_EVENT, /* Event value changed */ EV_REPORT_MT_SLOT, /* Event value and MT slot number changed */ }; MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory"); /* adb keyboard driver used on powerpc does not support evdev yet */ #if defined(__powerpc__) && !defined(__powerpc64__) int evdev_rcpt_mask = EVDEV_RCPT_KBDMUX | EVDEV_RCPT_HW_MOUSE; #else int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD; #endif int evdev_sysmouse_t_axis = 0; SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, "Evdev args"); #ifdef EVDEV_SUPPORT SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RWTUN, &evdev_rcpt_mask, 0, "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, " "bit2 - mouse hardware, bit3 - keyboard hardware"); SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RWTUN, &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm"); #endif SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Evdev input devices"); static void evdev_start_repeat(struct evdev_dev *, uint16_t); static void evdev_stop_repeat(struct evdev_dev *); static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t); struct evdev_dev * evdev_alloc(void) { return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO); } void evdev_free(struct evdev_dev *evdev) { if (evdev != NULL && evdev->ev_cdev != NULL && evdev->ev_cdev->si_drv1 != NULL) evdev_unregister(evdev); free(evdev, M_EVDEV); } static struct input_absinfo * evdev_alloc_absinfo(void) { return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV, M_WAITOK | M_ZERO)); } static void evdev_free_absinfo(struct input_absinfo *absinfo) { free(absinfo, M_EVDEV); } int evdev_set_report_size(struct evdev_dev *evdev, size_t report_size) { if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT + MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT) return (EINVAL); evdev->ev_report_size = report_size; return (0); } static size_t evdev_estimate_report_size(struct evdev_dev *evdev) { size_t size = 0; int res; /* * Keyboards generate one event per report but other devices with * buttons like mouses can report events simultaneously */ bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res); if (res == -1) bit_ffs(evdev->ev_key_flags, BTN_MISC, &res); size += (res != -1); bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res); size += res; /* All relative axes can be reported simultaneously */ bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res); size += res; /* * All absolute axes can be reported simultaneously. * Multitouch axes can be reported ABS_MT_SLOT times */ if (evdev->ev_absinfo != NULL) { bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res); size += res; bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res); if (res > 0) { res++; /* ABS_MT_SLOT or SYN_MT_REPORT */ if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) /* MT type B */ size += res * MAXIMAL_MT_SLOT(evdev); else /* MT type A */ size += res * (MAX_MT_REPORTS - 1); } } /* All misc events can be reported simultaneously */ bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res); size += res; /* All leds can be reported simultaneously */ bit_count(evdev->ev_led_flags, 0, LED_CNT, &res); size += res; /* Assume other events are generated once per report */ bit_ffs(evdev->ev_snd_flags, SND_CNT, &res); size += (res != -1); bit_ffs(evdev->ev_sw_flags, SW_CNT, &res); size += (res != -1); /* XXX: FF part is not implemented yet */ size++; /* SYN_REPORT */ return (size); } static void evdev_sysctl_create(struct evdev_dev *evdev) { struct sysctl_oid *ev_sysctl_tree; char ev_unit_str[8]; snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit); sysctl_ctx_init(&evdev->ev_sysctl_ctx); ev_sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&evdev->ev_sysctl_ctx, SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO, ev_unit_str, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device index"); SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD, evdev->ev_name, 0, "Input device name"); SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD, &evdev->ev_id, input_id, "Input device identification"); /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */ SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD, evdev->ev_shortname, 0, "Input device short name"); /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */ SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD, evdev->ev_serial, 0, "Input device unique number"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD, evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "", "Input device properties"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD, evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "", "Input device supported events types"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD, evdev->ev_key_flags, sizeof(evdev->ev_key_flags), "", "Input device supported keys"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD, evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "", "Input device supported relative events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD, evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "", "Input device supported absolute events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD, evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "", "Input device supported miscellaneous events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD, evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "", "Input device supported LED events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD, evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "", "Input device supported sound events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD, evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "", "Input device supported switch events"); } static int evdev_register_common(struct evdev_dev *evdev) { int ret; debugf(evdev, "%s: registered evdev provider: %s <%s>\n", evdev->ev_shortname, evdev->ev_name, evdev->ev_serial); /* Initialize internal structures */ CK_SLIST_INIT(&evdev->ev_clients); sx_init(&evdev->ev_list_lock, "evsx"); if (evdev_event_supported(evdev, EV_REP) && bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { /* Initialize callout */ callout_init_mtx(&evdev->ev_rep_callout, evdev->ev_state_lock, 0); if (evdev->ev_rep[REP_DELAY] == 0 && evdev->ev_rep[REP_PERIOD] == 0) { /* Supply default values */ evdev->ev_rep[REP_DELAY] = 250; evdev->ev_rep[REP_PERIOD] = 33; } } - /* Initialize multitouch protocol type B states */ - if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) + /* Initialize multitouch protocol type B states or A to B converter */ + if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) || + bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) evdev_mt_init(evdev); /* Estimate maximum report size */ if (evdev->ev_report_size == 0) { ret = evdev_set_report_size(evdev, evdev_estimate_report_size(evdev)); if (ret != 0) goto bail_out; } /* Create char device node */ ret = evdev_cdev_create(evdev); if (ret != 0) goto bail_out; /* Create sysctls (for device enumeration without /dev/input access rights) */ evdev_sysctl_create(evdev); bail_out: if (ret != 0) sx_destroy(&evdev->ev_list_lock); return (ret); } int evdev_register(struct evdev_dev *evdev) { int ret; if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH)) evdev->ev_lock_type = EV_LOCK_EXT_EPOCH; else evdev->ev_lock_type = EV_LOCK_INTERNAL; evdev->ev_state_lock = &evdev->ev_mtx; mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF); ret = evdev_register_common(evdev); if (ret != 0) mtx_destroy(&evdev->ev_mtx); return (ret); } int evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx) { evdev->ev_lock_type = EV_LOCK_MTX; evdev->ev_state_lock = mtx; return (evdev_register_common(evdev)); } int evdev_unregister(struct evdev_dev *evdev) { struct evdev_client *client, *tmp; int ret; debugf(evdev, "%s: unregistered evdev provider: %s\n", evdev->ev_shortname, evdev->ev_name); sysctl_ctx_free(&evdev->ev_sysctl_ctx); EVDEV_LIST_LOCK(evdev); evdev->ev_cdev->si_drv1 = NULL; /* Wake up sleepers */ CK_SLIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) { evdev_revoke_client(client); evdev_dispose_client(evdev, client); EVDEV_CLIENT_LOCKQ(client); evdev_notify_event(client); EVDEV_CLIENT_UNLOCKQ(client); } EVDEV_LIST_UNLOCK(evdev); /* release lock to avoid deadlock with evdev_dtor */ ret = evdev_cdev_destroy(evdev); evdev->ev_cdev = NULL; sx_destroy(&evdev->ev_list_lock); if (ret == 0 && evdev->ev_lock_type != EV_LOCK_MTX) mtx_destroy(&evdev->ev_mtx); evdev_free_absinfo(evdev->ev_absinfo); evdev_mt_free(evdev); return (ret); } inline void evdev_set_name(struct evdev_dev *evdev, const char *name) { snprintf(evdev->ev_name, NAMELEN, "%s", name); } inline void evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor, uint16_t product, uint16_t version) { evdev->ev_id = (struct input_id) { .bustype = bustype, .vendor = vendor, .product = product, .version = version }; } inline void evdev_set_phys(struct evdev_dev *evdev, const char *name) { snprintf(evdev->ev_shortname, NAMELEN, "%s", name); } inline void evdev_set_serial(struct evdev_dev *evdev, const char *serial) { snprintf(evdev->ev_serial, NAMELEN, "%s", serial); } inline void evdev_set_methods(struct evdev_dev *evdev, void *softc, const struct evdev_methods *methods) { evdev->ev_methods = methods; evdev->ev_softc = softc; } inline void * evdev_get_softc(struct evdev_dev *evdev) { return (evdev->ev_softc); } inline void evdev_support_prop(struct evdev_dev *evdev, uint16_t prop) { KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property")); bit_set(evdev->ev_prop_flags, prop); } inline void evdev_support_event(struct evdev_dev *evdev, uint16_t type) { KASSERT(type < EV_CNT, ("invalid evdev event property")); bit_set(evdev->ev_type_flags, type); } inline void evdev_support_key(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < KEY_CNT, ("invalid evdev key property")); bit_set(evdev->ev_key_flags, code); } inline void evdev_support_rel(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < REL_CNT, ("invalid evdev rel property")); bit_set(evdev->ev_rel_flags, code); } inline void evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution) { struct input_absinfo absinfo; KASSERT(code < ABS_CNT, ("invalid evdev abs property")); absinfo = (struct input_absinfo) { .value = 0, .minimum = minimum, .maximum = maximum, .fuzz = fuzz, .flat = flat, .resolution = resolution, }; evdev_set_abs_bit(evdev, code); evdev_set_absinfo(evdev, code, &absinfo); } inline void evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < ABS_CNT, ("invalid evdev abs property")); if (evdev->ev_absinfo == NULL) evdev->ev_absinfo = evdev_alloc_absinfo(); bit_set(evdev->ev_abs_flags, code); } inline void evdev_support_msc(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < MSC_CNT, ("invalid evdev msc property")); bit_set(evdev->ev_msc_flags, code); } inline void evdev_support_led(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < LED_CNT, ("invalid evdev led property")); bit_set(evdev->ev_led_flags, code); } inline void evdev_support_snd(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < SND_CNT, ("invalid evdev snd property")); bit_set(evdev->ev_snd_flags, code); } inline void evdev_support_sw(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < SW_CNT, ("invalid evdev sw property")); bit_set(evdev->ev_sw_flags, code); } bool evdev_event_supported(struct evdev_dev *evdev, uint16_t type) { KASSERT(type < EV_CNT, ("invalid evdev event property")); return (bit_test(evdev->ev_type_flags, type)); } inline void evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis, struct input_absinfo *absinfo) { KASSERT(axis < ABS_CNT, ("invalid evdev abs property")); if (axis == ABS_MT_SLOT && (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS)) return; if (evdev->ev_absinfo == NULL) evdev->ev_absinfo = evdev_alloc_absinfo(); if (axis == ABS_MT_SLOT) evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum; else memcpy(&evdev->ev_absinfo[axis], absinfo, sizeof(struct input_absinfo)); } inline void evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value) { KASSERT(property < REP_CNT, ("invalid evdev repeat property")); evdev->ev_rep[property] = value; } inline void evdev_set_flag(struct evdev_dev *evdev, uint16_t flag) { KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property")); bit_set(evdev->ev_flags, flag); } static int evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { if (type >= EV_CNT) return (EINVAL); /* Allow SYN events implicitly */ if (type != EV_SYN && !evdev_event_supported(evdev, type)) return (EINVAL); switch (type) { case EV_SYN: if (code >= SYN_CNT) return (EINVAL); break; case EV_KEY: if (code >= KEY_CNT) return (EINVAL); if (!bit_test(evdev->ev_key_flags, code)) return (EINVAL); break; case EV_REL: if (code >= REL_CNT) return (EINVAL); if (!bit_test(evdev->ev_rel_flags, code)) return (EINVAL); break; case EV_ABS: if (code >= ABS_CNT) return (EINVAL); if (!bit_test(evdev->ev_abs_flags, code)) return (EINVAL); if (code == ABS_MT_SLOT && (value < 0 || value > MAXIMAL_MT_SLOT(evdev))) return (EINVAL); if (ABS_IS_MT(code) && evdev->ev_mt == NULL && bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) return (EINVAL); break; case EV_MSC: if (code >= MSC_CNT) return (EINVAL); if (!bit_test(evdev->ev_msc_flags, code)) return (EINVAL); break; case EV_LED: if (code >= LED_CNT) return (EINVAL); if (!bit_test(evdev->ev_led_flags, code)) return (EINVAL); break; case EV_SND: if (code >= SND_CNT) return (EINVAL); if (!bit_test(evdev->ev_snd_flags, code)) return (EINVAL); break; case EV_SW: if (code >= SW_CNT) return (EINVAL); if (!bit_test(evdev->ev_sw_flags, code)) return (EINVAL); break; case EV_REP: if (code >= REP_CNT) return (EINVAL); break; default: return (EINVAL); } return (0); } static void evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t *value) { int32_t fuzz, old_value, abs_change; EVDEV_LOCK_ASSERT(evdev); switch (type) { case EV_KEY: if (!evdev_event_supported(evdev, EV_REP)) break; if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { /* Detect driver key repeats. */ if (bit_test(evdev->ev_key_states, code) && *value == KEY_EVENT_DOWN) *value = KEY_EVENT_REPEAT; } else { /* Start/stop callout for evdev repeats */ if (bit_test(evdev->ev_key_states, code) == !*value && !CK_SLIST_EMPTY(&evdev->ev_clients)) { if (*value == KEY_EVENT_DOWN) evdev_start_repeat(evdev, code); else evdev_stop_repeat(evdev); } } break; case EV_ABS: if (code == ABS_MT_SLOT) break; else if (!ABS_IS_MT(code)) old_value = evdev->ev_absinfo[code].value; else if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) /* Pass MT protocol type A events as is */ break; else if (code == ABS_MT_TRACKING_ID) { *value = evdev_mt_reassign_id(evdev, evdev_mt_get_last_slot(evdev), *value); break; } else old_value = evdev_mt_get_value(evdev, evdev_mt_get_last_slot(evdev), code); fuzz = evdev->ev_absinfo[code].fuzz; if (fuzz == 0) break; abs_change = abs(*value - old_value); if (abs_change < fuzz / 2) *value = old_value; else if (abs_change < fuzz) *value = (old_value * 3 + *value) / 4; else if (abs_change < fuzz * 2) *value = (old_value + *value) / 2; break; } } static enum evdev_sparse_result evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { int32_t last_mt_slot; EVDEV_LOCK_ASSERT(evdev); /* * For certain event types, update device state bits * and convert level reporting to edge reporting */ switch (type) { case EV_KEY: switch (value) { case KEY_EVENT_UP: case KEY_EVENT_DOWN: if (bit_test(evdev->ev_key_states, code) == value) return (EV_SKIP_EVENT); bit_change(evdev->ev_key_states, code, value); break; case KEY_EVENT_REPEAT: if (bit_test(evdev->ev_key_states, code) == 0 || !evdev_event_supported(evdev, EV_REP)) return (EV_SKIP_EVENT); break; default: return (EV_SKIP_EVENT); } break; case EV_LED: if (bit_test(evdev->ev_led_states, code) == value) return (EV_SKIP_EVENT); bit_change(evdev->ev_led_states, code, value); break; case EV_SND: bit_change(evdev->ev_snd_states, code, value); break; case EV_SW: if (bit_test(evdev->ev_sw_states, code) == value) return (EV_SKIP_EVENT); bit_change(evdev->ev_sw_states, code, value); break; case EV_REP: if (evdev->ev_rep[code] == value) return (EV_SKIP_EVENT); evdev_set_repeat_params(evdev, code, value); break; case EV_REL: if (value == 0) return (EV_SKIP_EVENT); break; /* For EV_ABS, save last value in absinfo and ev_mt_states */ case EV_ABS: switch (code) { case ABS_MT_SLOT: /* Postpone ABS_MT_SLOT till next event */ evdev_mt_set_last_slot(evdev, value); return (EV_SKIP_EVENT); case ABS_MT_FIRST ... ABS_MT_LAST: /* Pass MT protocol type A events as is */ if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) break; /* Don`t repeat MT protocol type B events */ last_mt_slot = evdev_mt_get_last_slot(evdev); if (evdev_mt_get_value(evdev, last_mt_slot, code) == value) return (EV_SKIP_EVENT); evdev_mt_set_value(evdev, last_mt_slot, code, value); if (last_mt_slot != CURRENT_MT_SLOT(evdev)) { CURRENT_MT_SLOT(evdev) = last_mt_slot; evdev->ev_report_opened = true; return (EV_REPORT_MT_SLOT); } break; default: if (evdev->ev_absinfo[code].value == value) return (EV_SKIP_EVENT); evdev->ev_absinfo[code].value = value; } break; case EV_SYN: if (code == SYN_REPORT) { /* Count empty reports as well as non empty */ evdev->ev_report_count++; /* Skip empty reports */ if (!evdev->ev_report_opened) return (EV_SKIP_EVENT); evdev->ev_report_opened = false; return (EV_REPORT_EVENT); } break; } evdev->ev_report_opened = true; return (EV_REPORT_EVENT); } static void evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { struct epoch_tracker et; struct evdev_client *client; debugf(evdev, "%s pushed event %d/%d/%d", evdev->ev_shortname, type, code, value); EVDEV_LOCK_ASSERT(evdev); /* Propagate event through all clients */ if (evdev->ev_lock_type == EV_LOCK_INTERNAL) epoch_enter_preempt(INPUT_EPOCH, &et); KASSERT( evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0, ("Input epoch has not been entered\n")); CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) { if (evdev->ev_grabber != NULL && evdev->ev_grabber != client) continue; EVDEV_CLIENT_LOCKQ(client); evdev_client_push(client, type, code, value); if (type == EV_SYN && code == SYN_REPORT) evdev_notify_event(client); EVDEV_CLIENT_UNLOCKQ(client); } if (evdev->ev_lock_type == EV_LOCK_INTERNAL) epoch_exit_preempt(INPUT_EPOCH, &et); evdev->ev_event_count++; } void evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { enum evdev_sparse_result sparse; EVDEV_LOCK_ASSERT(evdev); evdev_modify_event(evdev, type, code, &value); sparse = evdev_sparse_event(evdev, type, code, value); switch (sparse) { case EV_REPORT_MT_SLOT: /* report postponed ABS_MT_SLOT */ evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT, CURRENT_MT_SLOT(evdev)); /* FALLTHROUGH */ case EV_REPORT_EVENT: evdev_propagate_event(evdev, type, code, value); /* FALLTHROUGH */ case EV_SKIP_EVENT: break; } } void evdev_restore_after_kdb(struct evdev_dev *evdev) { int code; EVDEV_LOCK_ASSERT(evdev); /* Report postponed leds */ bit_foreach(evdev->ev_kdb_led_states, LED_CNT, code) evdev_send_event(evdev, EV_LED, code, !bit_test(evdev->ev_led_states, code)); bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX); /* Release stuck keys (CTRL + ALT + ESC) */ evdev_stop_repeat(evdev); bit_foreach(evdev->ev_key_states, KEY_CNT, code) evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP); evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); } int evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { if (evdev_check_event(evdev, type, code, value) != 0) return (EINVAL); /* * Discard all but LEDs kdb events as unrelated to userspace. * Aggregate LED updates and postpone reporting until kdb deactivation. */ if (kdb_active || SCHEDULER_STOPPED()) { evdev->ev_kdb_active = true; if (type == EV_LED) bit_set(evdev->ev_kdb_led_states, bit_test(evdev->ev_led_states, code) != value); return (0); } EVDEV_ENTER(evdev); /* Fix evdev state corrupted with discarding of kdb events */ if (evdev->ev_kdb_active) { evdev->ev_kdb_active = false; evdev_restore_after_kdb(evdev); } if (type == EV_SYN && code == SYN_REPORT && bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) evdev_mt_sync_frame(evdev); else if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) && evdev_mt_record_event(evdev, type, code, value)) goto exit; evdev_send_event(evdev, type, code, value); exit: EVDEV_EXIT(evdev); return (0); } int evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { struct epoch_tracker et; int ret = 0; switch (type) { case EV_REP: /* evdev repeats should not be processed by hardware driver */ if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) goto push; /* FALLTHROUGH */ case EV_LED: case EV_MSC: case EV_SND: case EV_FF: if (evdev->ev_methods != NULL && evdev->ev_methods->ev_event != NULL) evdev->ev_methods->ev_event(evdev, type, code, value); /* * Leds and driver repeats should be reported in ev_event * method body to interoperate with kbdmux states and rates * propagation so both ways (ioctl and evdev) of changing it * will produce only one evdev event report to client. */ if (type == EV_LED || type == EV_REP) break; /* FALLTHROUGH */ case EV_SYN: case EV_KEY: case EV_REL: case EV_ABS: case EV_SW: push: if (evdev->ev_lock_type == EV_LOCK_MTX) EVDEV_LOCK(evdev); else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_enter_preempt(INPUT_EPOCH, &et); ret = evdev_push_event(evdev, type, code, value); if (evdev->ev_lock_type == EV_LOCK_MTX) EVDEV_UNLOCK(evdev); else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_exit_preempt(INPUT_EPOCH, &et); break; default: ret = EINVAL; } return (ret); } int evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client) { int ret = 0; debugf(evdev, "adding new client for device %s", evdev->ev_shortname); EVDEV_LIST_LOCK_ASSERT(evdev); if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL && evdev->ev_methods->ev_open != NULL) { debugf(evdev, "calling ev_open() on device %s", evdev->ev_shortname); ret = evdev->ev_methods->ev_open(evdev); } if (ret == 0) CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link); return (ret); } void evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client) { debugf(evdev, "removing client for device %s", evdev->ev_shortname); EVDEV_LIST_LOCK_ASSERT(evdev); CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link); if (CK_SLIST_EMPTY(&evdev->ev_clients)) { if (evdev->ev_methods != NULL && evdev->ev_methods->ev_close != NULL) (void)evdev->ev_methods->ev_close(evdev); if (evdev_event_supported(evdev, EV_REP) && bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_LOCK(evdev); evdev_stop_repeat(evdev); if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_UNLOCK(evdev); } } if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_LOCK(evdev); evdev_release_client(evdev, client); if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_UNLOCK(evdev); } int evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_grabber != NULL) return (EBUSY); evdev->ev_grabber = client; return (0); } int evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_grabber != client) return (EINVAL); evdev->ev_grabber = NULL; return (0); } static void evdev_repeat_callout(void *arg) { struct epoch_tracker et; struct evdev_dev *evdev = (struct evdev_dev *)arg; if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_enter_preempt(INPUT_EPOCH, &et); evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT); evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_exit_preempt(INPUT_EPOCH, &et); if (evdev->ev_rep[REP_PERIOD]) callout_reset(&evdev->ev_rep_callout, evdev->ev_rep[REP_PERIOD] * hz / 1000, evdev_repeat_callout, evdev); else evdev->ev_rep_key = KEY_RESERVED; } static void evdev_start_repeat(struct evdev_dev *evdev, uint16_t key) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_rep[REP_DELAY]) { evdev->ev_rep_key = key; callout_reset(&evdev->ev_rep_callout, evdev->ev_rep[REP_DELAY] * hz / 1000, evdev_repeat_callout, evdev); } } static void evdev_stop_repeat(struct evdev_dev *evdev) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_rep_key != KEY_RESERVED) { callout_stop(&evdev->ev_rep_callout); evdev->ev_rep_key = KEY_RESERVED; } } MODULE_VERSION(evdev, 1); diff --git a/sys/dev/evdev/evdev_mt.c b/sys/dev/evdev/evdev_mt.c index d5bf4affea1b..3030a60e098a 100644 --- a/sys/dev/evdev/evdev_mt.c +++ b/sys/dev/evdev/evdev_mt.c @@ -1,653 +1,681 @@ /*- * Copyright (c) 2016, 2020 Vladimir Kondratyev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /*- * Copyright (c) 2015, 2016 Ulf Brosziewski * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #ifdef DEBUG #define debugf(fmt, args...) printf("evdev: " fmt "\n", ##args) #else #define debugf(fmt, args...) #endif typedef u_int slotset_t; _Static_assert(MAX_MT_SLOTS < sizeof(slotset_t) * 8, "MAX_MT_SLOTS too big"); #define FOREACHBIT(v, i) \ for ((i) = ffs(v) - 1; (i) != -1; (i) = ffs((v) & (~1 << (i))) - 1) struct { uint16_t mt; uint16_t st; int32_t max; } static evdev_mtstmap[] = { { ABS_MT_POSITION_X, ABS_X, 0 }, { ABS_MT_POSITION_Y, ABS_Y, 0 }, { ABS_MT_PRESSURE, ABS_PRESSURE, 255 }, { ABS_MT_TOUCH_MAJOR, ABS_TOOL_WIDTH, 15 }, }; struct evdev_mt { int last_reported_slot; uint16_t tracking_id; int32_t tracking_ids[MAX_MT_SLOTS]; + bool type_a; u_int mtst_events; /* the set of slots with active touches */ slotset_t touches; /* the set of slots with unsynchronized state */ slotset_t frame; /* the set of slots to match with active touches */ slotset_t match_frame; int match_slot; union evdev_mt_slot *match_slots; int *matrix; union evdev_mt_slot slots[]; }; static void evdev_mt_send_st_compat(struct evdev_dev *); static void evdev_mt_send_autorel(struct evdev_dev *); static void evdev_mt_replay_events(struct evdev_dev *); static inline int ffc_slot(struct evdev_dev *evdev, slotset_t slots) { return (ffs(~slots & (2U << MAXIMAL_MT_SLOT(evdev)) - 1) - 1); } void evdev_mt_init(struct evdev_dev *evdev) { struct evdev_mt *mt; size_t size = offsetof(struct evdev_mt, slots); int slot, slots; + bool type_a; + + type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); + if (type_a) { + /* Add events produced by MT type A to type B converter */ + evdev_support_abs(evdev, + ABS_MT_SLOT, 0, MAX_MT_SLOTS - 1, 0, 0, 0); + evdev_support_abs(evdev, + ABS_MT_TRACKING_ID, -1, MAX_MT_SLOTS - 1, 0, 0, 0); + } slots = MAXIMAL_MT_SLOT(evdev) + 1; size += sizeof(mt->slots[0]) * slots; if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) { size += sizeof(mt->match_slots[0]) * slots; size += sizeof(mt->matrix[0]) * (slots + 6) * slots; } mt = malloc(size, M_EVDEV, M_WAITOK | M_ZERO); evdev->ev_mt = mt; + mt->type_a = type_a; if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) { mt->match_slots = mt->slots + slots; mt->matrix = (int *)(mt->match_slots + slots); } /* Initialize multitouch protocol type B states */ for (slot = 0; slot < slots; slot++) mt->slots[slot].id = -1; if (!bit_test(evdev->ev_flags, EVDEV_FLAG_MT_KEEPID)) evdev_support_abs(evdev, ABS_MT_TRACKING_ID, -1, UINT16_MAX, 0, 0, 0); if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) evdev_support_mt_compat(evdev); } void evdev_mt_free(struct evdev_dev *evdev) { free(evdev->ev_mt, M_EVDEV); } void evdev_mt_sync_frame(struct evdev_dev *evdev) { if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) evdev_mt_replay_events(evdev); if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL)) evdev_mt_send_autorel(evdev); if (evdev->ev_report_opened && bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) evdev_mt_send_st_compat(evdev); evdev->ev_mt->frame = 0; } static void evdev_mt_send_slot(struct evdev_dev *evdev, int slot, union evdev_mt_slot *state) { int i; bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); EVDEV_LOCK_ASSERT(evdev); MPASS(type_a || (slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev))); MPASS(!type_a || state != NULL); if (!type_a) { evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot); if (state == NULL) { evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1); return; } } bit_foreach_at(evdev->ev_abs_flags, ABS_MT_FIRST, ABS_MT_LAST + 1, i) evdev_send_event(evdev, EV_ABS, i, state->val[ABS_MT_INDEX(i)]); if (type_a) evdev_send_event(evdev, EV_SYN, SYN_MT_REPORT, 1); } int evdev_mt_push_slot(struct evdev_dev *evdev, int slot, union evdev_mt_slot *state) { struct evdev_mt *mt = evdev->ev_mt; bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); - if (type_a && state == NULL) + if ((type_a || (mt != NULL && mt->type_a)) && state == NULL) return (EINVAL); if (!type_a && (slot < 0 || slot > MAXIMAL_MT_SLOT(evdev))) return (EINVAL); EVDEV_ENTER(evdev); - if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) { + if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) && mt->type_a) { + mt->match_slots[mt->match_slot] = *state; + evdev_mt_record_event(evdev, EV_SYN, SYN_MT_REPORT, 1); + } else if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) { evdev_mt_record_event(evdev, EV_ABS, ABS_MT_SLOT, slot); if (state != NULL) mt->match_slots[mt->match_slot] = *state; else evdev_mt_record_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1); } else evdev_mt_send_slot(evdev, slot, state); EVDEV_EXIT(evdev); return (0); } /* * Find a minimum-weight matching for an m-by-n matrix. * * m must be greater than or equal to n. The size of the buffer must be * at least 3m + 3n. * * On return, the first m elements of the buffer contain the row-to- * column mappings, i.e., buffer[i] is the column index for row i, or -1 * if there is no assignment for that row (which may happen if n < m). * * Wrong results because of overflows will not occur with input values * in the range of 0 to INT_MAX / 2 inclusive. * * The function applies the Dinic-Kronrod algorithm. It is not modern or * popular, but it seems to be a good choice for small matrices at least. * The original form of the algorithm is modified as follows: There is no * initial search for row minima, the initial assignments are in a * "virtual" column with the index -1 and zero values. This permits inputs * with n < m, and it simplifies the reassignments. */ static void evdev_mt_matching(int *matrix, int m, int n, int *buffer) { int i, j, k, d, e, row, col, delta; int *p; int *r2c = buffer; /* row-to-column assignments */ int *red = r2c + m; /* reduced values of the assignments */ int *mc = red + m; /* row-wise minimal elements of cs */ int *cs = mc + m; /* the column set */ int *c2r = cs + n; /* column-to-row assignments in cs */ int *cd = c2r + n; /* column deltas (reduction) */ for (p = r2c; p < red; *p++ = -1) {} for (; p < mc; *p++ = 0) {} for (col = 0; col < n; col++) { delta = INT_MAX; for (i = 0, p = matrix + col; i < m; i++, p += n) { d = *p - red[i]; if (d < delta || (d == delta && r2c[i] < 0)) { delta = d; row = i; } } cd[col] = delta; if (r2c[row] < 0) { r2c[row] = col; continue; } for (p = mc; p < cs; *p++ = col) {} for (k = 0; (j = r2c[row]) >= 0;) { cs[k++] = j; c2r[j] = row; mc[row] -= n; delta = INT_MAX; for (i = 0, p = matrix; i < m; i++, p += n) if (mc[i] >= 0) { d = p[mc[i]] - cd[mc[i]]; e = p[j] - cd[j]; if (e < d) { d = e; mc[i] = j; } d -= red[i]; if (d < delta || (d == delta && r2c[i] < 0)) { delta = d; row = i; } } cd[col] += delta; for (i = 0; i < k; i++) { cd[cs[i]] += delta; red[c2r[cs[i]]] -= delta; } } for (j = mc[row]; (r2c[row] = j) != col;) { row = c2r[j]; j = mc[row] + n; } } } /* * Assign tracking IDs to the points in the pt array. The tracking ID * assignment pairs the points with points of the previous frame in * such a way that the sum of the squared distances is minimal. Using * squares instead of simple distances favours assignments with more uniform * distances, and it is faster. * Set tracking id to -1 for unassigned (new) points. */ void evdev_mt_match_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, int size) { struct evdev_mt *mt = evdev->ev_mt; int i, j, m, n, dx, dy, slot, num_touches; int *p, *r2c, *c2r; EVDEV_LOCK_ASSERT(evdev); MPASS(mt->matrix != NULL); MPASS(size >= 0 && size <= MAXIMAL_MT_SLOT(evdev) + 1); if (size == 0) return; p = mt->matrix; num_touches = bitcount(mt->touches); if (num_touches >= size) { FOREACHBIT(mt->touches, slot) for (i = 0; i < size; i++) { dx = pt[i].x - mt->slots[slot].x; dy = pt[i].y - mt->slots[slot].y; *p++ = dx * dx + dy * dy; } m = num_touches; n = size; } else { for (i = 0; i < size; i++) FOREACHBIT(mt->touches, slot) { dx = pt[i].x - mt->slots[slot].x; dy = pt[i].y - mt->slots[slot].y; *p++ = dx * dx + dy * dy; } m = size; n = num_touches; } evdev_mt_matching(mt->matrix, m, n, p); r2c = p; c2r = p + m; for (i = 0; i < m; i++) if ((j = r2c[i]) >= 0) c2r[j] = i; p = (n == size ? c2r : r2c); for (i = 0; i < size; i++) if (*p++ < 0) pt[i].id = -1; p = (n == size ? r2c : c2r); FOREACHBIT(mt->touches, slot) if ((i = *p++) >= 0) pt[i].id = mt->tracking_ids[slot]; } static void evdev_mt_send_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, int size) { struct evdev_mt *mt = evdev->ev_mt; union evdev_mt_slot *slot; EVDEV_LOCK_ASSERT(evdev); MPASS(size >= 0 && size <= MAXIMAL_MT_SLOT(evdev) + 1); /* * While MT-matching assign tracking IDs of new contacts to be equal * to a slot number to make things simpler. */ for (slot = pt; slot < pt + size; slot++) { if (slot->id < 0) slot->id = ffc_slot(evdev, mt->touches | mt->frame); if (slot->id >= 0) evdev_mt_send_slot(evdev, slot->id, slot); } } int evdev_mt_push_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, int size) { if (size < 0 || size > MAXIMAL_MT_SLOT(evdev) + 1) return (EINVAL); EVDEV_ENTER(evdev); evdev_mt_send_frame(evdev, pt, size); EVDEV_EXIT(evdev); return (0); } bool evdev_mt_record_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { struct evdev_mt *mt = evdev->ev_mt; EVDEV_LOCK_ASSERT(evdev); switch (type) { + case EV_SYN: + if (code == SYN_MT_REPORT) { + /* MT protocol type A support */ + KASSERT(mt->type_a, ("Not a MT type A protocol")); + mt->match_frame |= 1U << mt->match_slot; + mt->match_slot++; + return (true); + } + break; case EV_ABS: if (code == ABS_MT_SLOT) { /* MT protocol type B support */ + KASSERT(!mt->type_a, ("Not a MT type B protocol")); KASSERT(value >= 0, ("Negative slot number")); mt->match_slot = value; mt->match_frame |= 1U << mt->match_slot; return (true); } else if (code == ABS_MT_TRACKING_ID) { + KASSERT(!mt->type_a, ("Not a MT type B protocol")); if (value == -1) mt->match_frame &= ~(1U << mt->match_slot); return (true); } else if (ABS_IS_MT(code)) { KASSERT(mt->match_slot >= 0, ("Negative slot")); KASSERT(mt->match_slot <= MAXIMAL_MT_SLOT(evdev), ("Slot number too big")); mt->match_slots[mt->match_slot]. val[ABS_MT_INDEX(code)] = value; return (true); } break; default: break; } return (false); } static void evdev_mt_replay_events(struct evdev_dev *evdev) { struct evdev_mt *mt = evdev->ev_mt; int slot, size = 0; EVDEV_LOCK_ASSERT(evdev); FOREACHBIT(mt->match_frame, slot) { if (slot != size) mt->match_slots[size] = mt->match_slots[slot]; size++; } evdev_mt_match_frame(evdev, mt->match_slots, size); evdev_mt_send_frame(evdev, mt->match_slots, size); mt->match_slot = 0; mt->match_frame = 0; } union evdev_mt_slot * evdev_mt_get_match_slots(struct evdev_dev *evdev) { return (evdev->ev_mt->match_slots); } int evdev_mt_get_last_slot(struct evdev_dev *evdev) { return (evdev->ev_mt->last_reported_slot); } void evdev_mt_set_last_slot(struct evdev_dev *evdev, int slot) { struct evdev_mt *mt = evdev->ev_mt; MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); mt->frame |= 1U << slot; mt->last_reported_slot = slot; } int32_t evdev_mt_get_value(struct evdev_dev *evdev, int slot, int16_t code) { struct evdev_mt *mt = evdev->ev_mt; MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); return (mt->slots[slot].val[ABS_MT_INDEX(code)]); } void evdev_mt_set_value(struct evdev_dev *evdev, int slot, int16_t code, int32_t value) { struct evdev_mt *mt = evdev->ev_mt; MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); if (code == ABS_MT_TRACKING_ID) { if (value != -1) mt->touches |= 1U << slot; else mt->touches &= ~(1U << slot); } mt->slots[slot].val[ABS_MT_INDEX(code)] = value; } int evdev_get_mt_slot_by_tracking_id(struct evdev_dev *evdev, int32_t tracking_id) { struct evdev_mt *mt = evdev->ev_mt; int slot; + KASSERT(!mt->type_a, ("Not a MT type B protocol")); + /* * Ignore tracking_id if slot assignment is performed by evdev. * Events are written sequentially to temporary matching buffer. */ if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) return (ffc_slot(evdev, mt->match_frame)); FOREACHBIT(mt->touches, slot) if (mt->tracking_ids[slot] == tracking_id) return (slot); /* * Do not allow allocation of new slot in a place of just * released one within the same report. */ return (ffc_slot(evdev, mt->touches | mt->frame)); } int32_t evdev_mt_reassign_id(struct evdev_dev *evdev, int slot, int32_t id) { struct evdev_mt *mt = evdev->ev_mt; int32_t nid; if (id == -1 || bit_test(evdev->ev_flags, EVDEV_FLAG_MT_KEEPID)) { mt->tracking_ids[slot] = id; return (id); } nid = evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID); if (nid != -1) { KASSERT(id == mt->tracking_ids[slot], ("MT-slot tracking id has changed")); return (nid); } mt->tracking_ids[slot] = id; again: nid = mt->tracking_id++; FOREACHBIT(mt->touches, slot) if (evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID) == nid) goto again; return (nid); } static inline int32_t evdev_mt_normalize(int32_t value, int32_t mtmin, int32_t mtmax, int32_t stmax) { if (stmax != 0 && mtmax != mtmin) { value = (value - mtmin) * stmax / (mtmax - mtmin); value = MAX(MIN(value, stmax), 0); } return (value); } void evdev_support_mt_compat(struct evdev_dev *evdev) { struct input_absinfo *ai; int i; if (evdev->ev_absinfo == NULL) return; evdev_support_event(evdev, EV_KEY); evdev_support_key(evdev, BTN_TOUCH); /* Touchscreens should not advertise tap tool capabilities */ if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) evdev_support_nfingers(evdev, MAXIMAL_MT_SLOT(evdev) + 1); /* Echo 0-th MT-slot as ST-slot */ for (i = 0; i < nitems(evdev_mtstmap); i++) { if (!bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].mt) || bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].st)) continue; ai = evdev->ev_absinfo + evdev_mtstmap[i].mt; evdev->ev_mt->mtst_events |= 1U << i; if (evdev_mtstmap[i].max != 0) evdev_support_abs(evdev, evdev_mtstmap[i].st, 0, evdev_mtstmap[i].max, 0, evdev_mt_normalize( ai->flat, 0, ai->maximum, evdev_mtstmap[i].max), 0); else evdev_support_abs(evdev, evdev_mtstmap[i].st, ai->minimum, ai->maximum, 0, ai->flat, ai->resolution); } } static void evdev_mt_send_st_compat(struct evdev_dev *evdev) { struct evdev_mt *mt = evdev->ev_mt; int nfingers, i, st_slot; EVDEV_LOCK_ASSERT(evdev); nfingers = bitcount(mt->touches); evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0); /* Send first active MT-slot state as single touch report */ st_slot = ffs(mt->touches) - 1; if (st_slot != -1) FOREACHBIT(mt->mtst_events, i) evdev_send_event(evdev, EV_ABS, evdev_mtstmap[i].st, evdev_mt_normalize(evdev_mt_get_value(evdev, st_slot, evdev_mtstmap[i].mt), evdev->ev_absinfo[evdev_mtstmap[i].mt].minimum, evdev->ev_absinfo[evdev_mtstmap[i].mt].maximum, evdev_mtstmap[i].max)); /* Touchscreens should not report tool taps */ if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) evdev_send_nfingers(evdev, nfingers); if (nfingers == 0) evdev_send_event(evdev, EV_ABS, ABS_PRESSURE, 0); } void evdev_push_mt_compat(struct evdev_dev *evdev) { EVDEV_ENTER(evdev); evdev_mt_send_st_compat(evdev); EVDEV_EXIT(evdev); } static void evdev_mt_send_autorel(struct evdev_dev *evdev) { struct evdev_mt *mt = evdev->ev_mt; int slot; EVDEV_LOCK_ASSERT(evdev); KASSERT(mt->match_frame == 0, ("Unmatched events exist")); FOREACHBIT(mt->touches & ~mt->frame, slot) evdev_mt_send_slot(evdev, slot, NULL); } void evdev_mt_push_autorel(struct evdev_dev *evdev) { EVDEV_ENTER(evdev); evdev_mt_send_autorel(evdev); EVDEV_EXIT(evdev); }