input-mt.c 13 KB

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