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