aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/input-mt.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/input-mt.c')
-rw-r--r--drivers/input/input-mt.c305
1 files changed, 277 insertions, 28 deletions
diff --git a/drivers/input/input-mt.c b/drivers/input/input-mt.c
index 70a16c7da8cc..c0ec7d42c3be 100644
--- a/drivers/input/input-mt.c
+++ b/drivers/input/input-mt.c
@@ -14,6 +14,14 @@
14 14
15#define TRKID_SGN ((TRKID_MAX + 1) >> 1) 15#define TRKID_SGN ((TRKID_MAX + 1) >> 1)
16 16
17static void copy_abs(struct input_dev *dev, unsigned int dst, unsigned int src)
18{
19 if (dev->absinfo && test_bit(src, dev->absbit)) {
20 dev->absinfo[dst] = dev->absinfo[src];
21 dev->absbit[BIT_WORD(dst)] |= BIT_MASK(dst);
22 }
23}
24
17/** 25/**
18 * input_mt_init_slots() - initialize MT input slots 26 * input_mt_init_slots() - initialize MT input slots
19 * @dev: input device supporting MT events and finger tracking 27 * @dev: input device supporting MT events and finger tracking
@@ -25,29 +33,63 @@
25 * May be called repeatedly. Returns -EINVAL if attempting to 33 * May be called repeatedly. Returns -EINVAL if attempting to
26 * reinitialize with a different number of slots. 34 * reinitialize with a different number of slots.
27 */ 35 */
28int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots) 36int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots,
37 unsigned int flags)
29{ 38{
39 struct input_mt *mt = dev->mt;
30 int i; 40 int i;
31 41
32 if (!num_slots) 42 if (!num_slots)
33 return 0; 43 return 0;
34 if (dev->mt) 44 if (mt)
35 return dev->mtsize != num_slots ? -EINVAL : 0; 45 return mt->num_slots != num_slots ? -EINVAL : 0;
36 46
37 dev->mt = kcalloc(num_slots, sizeof(struct input_mt_slot), GFP_KERNEL); 47 mt = kzalloc(sizeof(*mt) + num_slots * sizeof(*mt->slots), GFP_KERNEL);
38 if (!dev->mt) 48 if (!mt)
39 return -ENOMEM; 49 goto err_mem;
40 50
41 dev->mtsize = num_slots; 51 mt->num_slots = num_slots;
52 mt->flags = flags;
42 input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0); 53 input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
43 input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0); 54 input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0);
44 input_set_events_per_packet(dev, 6 * num_slots); 55
56 if (flags & (INPUT_MT_POINTER | INPUT_MT_DIRECT)) {
57 __set_bit(EV_KEY, dev->evbit);
58 __set_bit(BTN_TOUCH, dev->keybit);
59
60 copy_abs(dev, ABS_X, ABS_MT_POSITION_X);
61 copy_abs(dev, ABS_Y, ABS_MT_POSITION_Y);
62 copy_abs(dev, ABS_PRESSURE, ABS_MT_PRESSURE);
63 }
64 if (flags & INPUT_MT_POINTER) {
65 __set_bit(BTN_TOOL_FINGER, dev->keybit);
66 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
67 if (num_slots >= 3)
68 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
69 if (num_slots >= 4)
70 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
71 if (num_slots >= 5)
72 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
73 __set_bit(INPUT_PROP_POINTER, dev->propbit);
74 }
75 if (flags & INPUT_MT_DIRECT)
76 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
77 if (flags & INPUT_MT_TRACK) {
78 unsigned int n2 = num_slots * num_slots;
79 mt->red = kcalloc(n2, sizeof(*mt->red), GFP_KERNEL);
80 if (!mt->red)
81 goto err_mem;
82 }
45 83
46 /* Mark slots as 'unused' */ 84 /* Mark slots as 'unused' */
47 for (i = 0; i < num_slots; i++) 85 for (i = 0; i < num_slots; i++)
48 input_mt_set_value(&dev->mt[i], ABS_MT_TRACKING_ID, -1); 86 input_mt_set_value(&mt->slots[i], ABS_MT_TRACKING_ID, -1);
49 87
88 dev->mt = mt;
50 return 0; 89 return 0;
90err_mem:
91 kfree(mt);
92 return -ENOMEM;
51} 93}
52EXPORT_SYMBOL(input_mt_init_slots); 94EXPORT_SYMBOL(input_mt_init_slots);
53 95
@@ -60,11 +102,11 @@ EXPORT_SYMBOL(input_mt_init_slots);
60 */ 102 */
61void input_mt_destroy_slots(struct input_dev *dev) 103void input_mt_destroy_slots(struct input_dev *dev)
62{ 104{
63 kfree(dev->mt); 105 if (dev->mt) {
106 kfree(dev->mt->red);
107 kfree(dev->mt);
108 }
64 dev->mt = NULL; 109 dev->mt = NULL;
65 dev->mtsize = 0;
66 dev->slot = 0;
67 dev->trkid = 0;
68} 110}
69EXPORT_SYMBOL(input_mt_destroy_slots); 111EXPORT_SYMBOL(input_mt_destroy_slots);
70 112
@@ -83,18 +125,24 @@ EXPORT_SYMBOL(input_mt_destroy_slots);
83void input_mt_report_slot_state(struct input_dev *dev, 125void input_mt_report_slot_state(struct input_dev *dev,
84 unsigned int tool_type, bool active) 126 unsigned int tool_type, bool active)
85{ 127{
86 struct input_mt_slot *mt; 128 struct input_mt *mt = dev->mt;
129 struct input_mt_slot *slot;
87 int id; 130 int id;
88 131
89 if (!dev->mt || !active) { 132 if (!mt)
133 return;
134
135 slot = &mt->slots[mt->slot];
136 slot->frame = mt->frame;
137
138 if (!active) {
90 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1); 139 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
91 return; 140 return;
92 } 141 }
93 142
94 mt = &dev->mt[dev->slot]; 143 id = input_mt_get_value(slot, ABS_MT_TRACKING_ID);
95 id = input_mt_get_value(mt, ABS_MT_TRACKING_ID); 144 if (id < 0 || input_mt_get_value(slot, ABS_MT_TOOL_TYPE) != tool_type)
96 if (id < 0 || input_mt_get_value(mt, ABS_MT_TOOL_TYPE) != tool_type) 145 id = input_mt_new_trkid(mt);
97 id = input_mt_new_trkid(dev);
98 146
99 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id); 147 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id);
100 input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type); 148 input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type);
@@ -135,13 +183,19 @@ EXPORT_SYMBOL(input_mt_report_finger_count);
135 */ 183 */
136void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count) 184void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count)
137{ 185{
138 struct input_mt_slot *oldest = NULL; 186 struct input_mt *mt = dev->mt;
139 int oldid = dev->trkid; 187 struct input_mt_slot *oldest;
140 int count = 0; 188 int oldid, count, i;
141 int i; 189
190 if (!mt)
191 return;
192
193 oldest = 0;
194 oldid = mt->trkid;
195 count = 0;
142 196
143 for (i = 0; i < dev->mtsize; ++i) { 197 for (i = 0; i < mt->num_slots; ++i) {
144 struct input_mt_slot *ps = &dev->mt[i]; 198 struct input_mt_slot *ps = &mt->slots[i];
145 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID); 199 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
146 200
147 if (id < 0) 201 if (id < 0)
@@ -160,13 +214,208 @@ void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count)
160 if (oldest) { 214 if (oldest) {
161 int x = input_mt_get_value(oldest, ABS_MT_POSITION_X); 215 int x = input_mt_get_value(oldest, ABS_MT_POSITION_X);
162 int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y); 216 int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y);
163 int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
164 217
165 input_event(dev, EV_ABS, ABS_X, x); 218 input_event(dev, EV_ABS, ABS_X, x);
166 input_event(dev, EV_ABS, ABS_Y, y); 219 input_event(dev, EV_ABS, ABS_Y, y);
167 input_event(dev, EV_ABS, ABS_PRESSURE, p); 220
221 if (test_bit(ABS_MT_PRESSURE, dev->absbit)) {
222 int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
223 input_event(dev, EV_ABS, ABS_PRESSURE, p);
224 }
168 } else { 225 } else {
169 input_event(dev, EV_ABS, ABS_PRESSURE, 0); 226 if (test_bit(ABS_MT_PRESSURE, dev->absbit))
227 input_event(dev, EV_ABS, ABS_PRESSURE, 0);
170 } 228 }
171} 229}
172EXPORT_SYMBOL(input_mt_report_pointer_emulation); 230EXPORT_SYMBOL(input_mt_report_pointer_emulation);
231
232/**
233 * input_mt_sync_frame() - synchronize mt frame
234 * @dev: input device with allocated MT slots
235 *
236 * Close the frame and prepare the internal state for a new one.
237 * Depending on the flags, marks unused slots as inactive and performs
238 * pointer emulation.
239 */
240void input_mt_sync_frame(struct input_dev *dev)
241{
242 struct input_mt *mt = dev->mt;
243 struct input_mt_slot *s;
244
245 if (!mt)
246 return;
247
248 if (mt->flags & INPUT_MT_DROP_UNUSED) {
249 for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
250 if (s->frame == mt->frame)
251 continue;
252 input_mt_slot(dev, s - mt->slots);
253 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
254 }
255 }
256
257 input_mt_report_pointer_emulation(dev, (mt->flags & INPUT_MT_POINTER));
258
259 mt->frame++;
260}
261EXPORT_SYMBOL(input_mt_sync_frame);
262
263static int adjust_dual(int *begin, int step, int *end, int eq)
264{
265 int f, *p, s, c;
266
267 if (begin == end)
268 return 0;
269
270 f = *begin;
271 p = begin + step;
272 s = p == end ? f + 1 : *p;
273
274 for (; p != end; p += step)
275 if (*p < f)
276 s = f, f = *p;
277 else if (*p < s)
278 s = *p;
279
280 c = (f + s + 1) / 2;
281 if (c == 0 || (c > 0 && !eq))
282 return 0;
283 if (s < 0)
284 c *= 2;
285
286 for (p = begin; p != end; p += step)
287 *p -= c;
288
289 return (c < s && s <= 0) || (f >= 0 && f < c);
290}
291
292static void find_reduced_matrix(int *w, int nr, int nc, int nrc)
293{
294 int i, k, sum;
295
296 for (k = 0; k < nrc; k++) {
297 for (i = 0; i < nr; i++)
298 adjust_dual(w + i, nr, w + i + nrc, nr <= nc);
299 sum = 0;
300 for (i = 0; i < nrc; i += nr)
301 sum += adjust_dual(w + i, 1, w + i + nr, nc <= nr);
302 if (!sum)
303 break;
304 }
305}
306
307static int input_mt_set_matrix(struct input_mt *mt,
308 const struct input_mt_pos *pos, int num_pos)
309{
310 const struct input_mt_pos *p;
311 struct input_mt_slot *s;
312 int *w = mt->red;
313 int x, y;
314
315 for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
316 if (!input_mt_is_active(s))
317 continue;
318 x = input_mt_get_value(s, ABS_MT_POSITION_X);
319 y = input_mt_get_value(s, ABS_MT_POSITION_Y);
320 for (p = pos; p != pos + num_pos; p++) {
321 int dx = x - p->x, dy = y - p->y;
322 *w++ = dx * dx + dy * dy;
323 }
324 }
325
326 return w - mt->red;
327}
328
329static void input_mt_set_slots(struct input_mt *mt,
330 int *slots, int num_pos)
331{
332 struct input_mt_slot *s;
333 int *w = mt->red, *p;
334
335 for (p = slots; p != slots + num_pos; p++)
336 *p = -1;
337
338 for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
339 if (!input_mt_is_active(s))
340 continue;
341 for (p = slots; p != slots + num_pos; p++)
342 if (*w++ < 0)
343 *p = s - mt->slots;
344 }
345
346 for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
347 if (input_mt_is_active(s))
348 continue;
349 for (p = slots; p != slots + num_pos; p++)
350 if (*p < 0) {
351 *p = s - mt->slots;
352 break;
353 }
354 }
355}
356
357/**
358 * input_mt_assign_slots() - perform a best-match assignment
359 * @dev: input device with allocated MT slots
360 * @slots: the slot assignment to be filled
361 * @pos: the position array to match
362 * @num_pos: number of positions
363 *
364 * Performs a best match against the current contacts and returns
365 * the slot assignment list. New contacts are assigned to unused
366 * slots.
367 *
368 * Returns zero on success, or negative error in case of failure.
369 */
370int input_mt_assign_slots(struct input_dev *dev, int *slots,
371 const struct input_mt_pos *pos, int num_pos)
372{
373 struct input_mt *mt = dev->mt;
374 int nrc;
375
376 if (!mt || !mt->red)
377 return -ENXIO;
378 if (num_pos > mt->num_slots)
379 return -EINVAL;
380 if (num_pos < 1)
381 return 0;
382
383 nrc = input_mt_set_matrix(mt, pos, num_pos);
384 find_reduced_matrix(mt->red, num_pos, nrc / num_pos, nrc);
385 input_mt_set_slots(mt, slots, num_pos);
386
387 return 0;
388}
389EXPORT_SYMBOL(input_mt_assign_slots);
390
391/**
392 * input_mt_get_slot_by_key() - return slot matching key
393 * @dev: input device with allocated MT slots
394 * @key: the key of the sought slot
395 *
396 * Returns the slot of the given key, if it exists, otherwise
397 * set the key on the first unused slot and return.
398 *
399 * If no available slot can be found, -1 is returned.
400 */
401int input_mt_get_slot_by_key(struct input_dev *dev, int key)
402{
403 struct input_mt *mt = dev->mt;
404 struct input_mt_slot *s;
405
406 if (!mt)
407 return -1;
408
409 for (s = mt->slots; s != mt->slots + mt->num_slots; s++)
410 if (input_mt_is_active(s) && s->key == key)
411 return s - mt->slots;
412
413 for (s = mt->slots; s != mt->slots + mt->num_slots; s++)
414 if (!input_mt_is_active(s)) {
415 s->key = key;
416 return s - mt->slots;
417 }
418
419 return -1;
420}
421EXPORT_SYMBOL(input_mt_get_slot_by_key);