^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Input Multitouch Library
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2008-2010 Henrik Rydberg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/input/mt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static void copy_abs(struct input_dev *dev, unsigned int dst, unsigned int src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (dev->absinfo && test_bit(src, dev->absbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) dev->absinfo[dst] = dev->absinfo[src];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) dev->absinfo[dst].fuzz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) __set_bit(dst, dev->absbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * input_mt_init_slots() - initialize MT input slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @dev: input device supporting MT events and finger tracking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @num_slots: number of slots used by the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @flags: mt tasks to handle in core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * This function allocates all necessary memory for MT slot handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * in the input device, prepares the ABS_MT_SLOT and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * ABS_MT_TRACKING_ID events for use and sets up appropriate buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Depending on the flags set, it also performs pointer emulation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * frame synchronization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * May be called repeatedly. Returns -EINVAL if attempting to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * reinitialize with a different number of slots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (!num_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (mt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return mt->num_slots != num_slots ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mt = kzalloc(struct_size(mt, slots, num_slots), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!mt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) goto err_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) mt->num_slots = num_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) mt->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (flags & (INPUT_MT_POINTER | INPUT_MT_DIRECT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) __set_bit(EV_KEY, dev->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) __set_bit(BTN_TOUCH, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) copy_abs(dev, ABS_X, ABS_MT_POSITION_X);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) copy_abs(dev, ABS_Y, ABS_MT_POSITION_Y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) copy_abs(dev, ABS_PRESSURE, ABS_MT_PRESSURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (flags & INPUT_MT_POINTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) __set_bit(BTN_TOOL_FINGER, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (num_slots >= 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (num_slots >= 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (num_slots >= 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) __set_bit(INPUT_PROP_POINTER, dev->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (flags & INPUT_MT_DIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) __set_bit(INPUT_PROP_DIRECT, dev->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (flags & INPUT_MT_SEMI_MT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (flags & INPUT_MT_TRACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned int n2 = num_slots * num_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) mt->red = kcalloc(n2, sizeof(*mt->red), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!mt->red)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto err_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* Mark slots as 'inactive' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) for (i = 0; i < num_slots; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) input_mt_set_value(&mt->slots[i], ABS_MT_TRACKING_ID, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Mark slots as 'unused' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) mt->frame = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dev->mt = mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) err_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) kfree(mt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) EXPORT_SYMBOL(input_mt_init_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * input_mt_destroy_slots() - frees the MT slots of the input device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * This function is only needed in error path as the input core will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * automatically free the MT slots when the device is destroyed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) void input_mt_destroy_slots(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (dev->mt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) kfree(dev->mt->red);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) kfree(dev->mt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dev->mt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) EXPORT_SYMBOL(input_mt_destroy_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * input_mt_report_slot_state() - report contact state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * @tool_type: the tool type to use in this slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * @active: true if contact is active, false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Reports a contact via ABS_MT_TRACKING_ID, and optionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * ABS_MT_TOOL_TYPE. If active is true and the slot is currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * inactive, or if the tool type is changed, a new tracking id is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * assigned to the slot. The tool type is only reported if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * corresponding absbit field is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Returns true if contact is active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) bool input_mt_report_slot_state(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned int tool_type, bool active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct input_mt_slot *slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!mt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) slot = &mt->slots[mt->slot];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) slot->frame = mt->frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) id = input_mt_get_value(slot, ABS_MT_TRACKING_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) id = input_mt_new_trkid(mt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) EXPORT_SYMBOL(input_mt_report_slot_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * input_mt_report_finger_count() - report contact count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * @count: the number of contacts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * Reports the contact count via BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * BTN_TOOL_TRIPLETAP and BTN_TOOL_QUADTAP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * The input core ensures only the KEY events already setup for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * this device will produce output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) void input_mt_report_finger_count(struct input_dev *dev, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) input_event(dev, EV_KEY, BTN_TOOL_FINGER, count == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) input_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, count == 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) input_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, count == 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) input_event(dev, EV_KEY, BTN_TOOL_QUADTAP, count == 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) input_event(dev, EV_KEY, BTN_TOOL_QUINTTAP, count == 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) EXPORT_SYMBOL(input_mt_report_finger_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * input_mt_report_pointer_emulation() - common pointer emulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * @use_count: report number of active contacts as finger count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * Performs legacy pointer emulation via BTN_TOUCH, ABS_X, ABS_Y and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * ABS_PRESSURE. Touchpad finger count is emulated if use_count is true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * The input core ensures only the KEY and ABS axes already setup for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * this device will produce output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct input_mt_slot *oldest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int oldid, count, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (!mt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) oldest = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) oldid = mt->trkid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) for (i = 0; i < mt->num_slots; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct input_mt_slot *ps = &mt->slots[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if ((id - oldid) & TRKID_SGN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) oldest = ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) oldid = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) input_event(dev, EV_KEY, BTN_TOUCH, count > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (use_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (count == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) !test_bit(ABS_MT_DISTANCE, dev->absbit) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) test_bit(ABS_DISTANCE, dev->absbit) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) input_abs_get_val(dev, ABS_DISTANCE) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * Force reporting BTN_TOOL_FINGER for devices that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * only report general hover (and not per-contact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * distance) when contact is in proximity but not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * on the surface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) input_mt_report_finger_count(dev, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (oldest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int x = input_mt_get_value(oldest, ABS_MT_POSITION_X);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) input_event(dev, EV_ABS, ABS_X, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) input_event(dev, EV_ABS, ABS_Y, y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (test_bit(ABS_MT_PRESSURE, dev->absbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) input_event(dev, EV_ABS, ABS_PRESSURE, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (test_bit(ABS_MT_PRESSURE, dev->absbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) input_event(dev, EV_ABS, ABS_PRESSURE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) EXPORT_SYMBOL(input_mt_report_pointer_emulation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void __input_mt_drop_unused(struct input_dev *dev, struct input_mt *mt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) for (i = 0; i < mt->num_slots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (!input_mt_is_used(mt, &mt->slots[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) input_mt_slot(dev, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * input_mt_drop_unused() - Inactivate slots not seen in this frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * Lift all slots not seen since the last call to this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) void input_mt_drop_unused(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (mt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) __input_mt_drop_unused(dev, mt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) mt->frame++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) EXPORT_SYMBOL(input_mt_drop_unused);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * input_mt_sync_frame() - synchronize mt frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Close the frame and prepare the internal state for a new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Depending on the flags, marks unused slots as inactive and performs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * pointer emulation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) void input_mt_sync_frame(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) bool use_count = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!mt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (mt->flags & INPUT_MT_DROP_UNUSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) __input_mt_drop_unused(dev, mt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if ((mt->flags & INPUT_MT_POINTER) && !(mt->flags & INPUT_MT_SEMI_MT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) use_count = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) input_mt_report_pointer_emulation(dev, use_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) mt->frame++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) EXPORT_SYMBOL(input_mt_sync_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int adjust_dual(int *begin, int step, int *end, int eq, int mu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int f, *p, s, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (begin == end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) f = *begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) p = begin + step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) s = p == end ? f + 1 : *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) for (; p != end; p += step) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (*p < f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) s = f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) f = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) } else if (*p < s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) s = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) c = (f + s + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (c == 0 || (c > mu && (!eq || mu > 0)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /* Improve convergence for positive matrices by penalizing overcovers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (s < 0 && mu <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) c *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) for (p = begin; p != end; p += step)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) *p -= c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return (c < s && s <= 0) || (f >= 0 && f < c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static void find_reduced_matrix(int *w, int nr, int nc, int nrc, int mu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int i, k, sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) for (k = 0; k < nrc; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) for (i = 0; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) adjust_dual(w + i, nr, w + i + nrc, nr <= nc, mu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) for (i = 0; i < nrc; i += nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) sum += adjust_dual(w + i, 1, w + i + nr, nc <= nr, mu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int input_mt_set_matrix(struct input_mt *mt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) const struct input_mt_pos *pos, int num_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int mu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) const struct input_mt_pos *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct input_mt_slot *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) int *w = mt->red;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (!input_mt_is_active(s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) x = input_mt_get_value(s, ABS_MT_POSITION_X);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) y = input_mt_get_value(s, ABS_MT_POSITION_Y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) for (p = pos; p != pos + num_pos; p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int dx = x - p->x, dy = y - p->y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) *w++ = dx * dx + dy * dy - mu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return w - mt->red;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void input_mt_set_slots(struct input_mt *mt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) int *slots, int num_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) struct input_mt_slot *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int *w = mt->red, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) for (j = 0; j != num_pos; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) slots[j] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (!input_mt_is_active(s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) for (j = 0; j != num_pos; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (w[j] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) slots[j] = s - mt->slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) w += num_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (input_mt_is_active(s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) for (j = 0; j != num_pos; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (slots[j] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) slots[j] = s - mt->slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * input_mt_assign_slots() - perform a best-match assignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @slots: the slot assignment to be filled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * @pos: the position array to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * @num_pos: number of positions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * @dmax: maximum ABS_MT_POSITION displacement (zero for infinite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * Performs a best match against the current contacts and returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * the slot assignment list. New contacts are assigned to unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * slots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * The assignments are balanced so that all coordinate displacements are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * below the euclidian distance dmax. If no such assignment can be found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * some contacts are assigned to unused slots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * Returns zero on success, or negative error in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) int input_mt_assign_slots(struct input_dev *dev, int *slots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) const struct input_mt_pos *pos, int num_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) int dmax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int mu = 2 * dmax * dmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) int nrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (!mt || !mt->red)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (num_pos > mt->num_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (num_pos < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) nrc = input_mt_set_matrix(mt, pos, num_pos, mu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) find_reduced_matrix(mt->red, num_pos, nrc / num_pos, nrc, mu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) input_mt_set_slots(mt, slots, num_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) EXPORT_SYMBOL(input_mt_assign_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * input_mt_get_slot_by_key() - return slot matching key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @dev: input device with allocated MT slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * @key: the key of the sought slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * Returns the slot of the given key, if it exists, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * set the key on the first unused slot and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * If no available slot can be found, -1 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * Note that for this function to work properly, input_mt_sync_frame() has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * to be called at each frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) int input_mt_get_slot_by_key(struct input_dev *dev, int key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct input_mt_slot *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (!mt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) for (s = mt->slots; s != mt->slots + mt->num_slots; s++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (input_mt_is_active(s) && s->key == key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return s - mt->slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) for (s = mt->slots; s != mt->slots + mt->num_slots; s++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (!input_mt_is_active(s) && !input_mt_is_used(mt, s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) s->key = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return s - mt->slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) EXPORT_SYMBOL(input_mt_get_slot_by_key);