input-mt.c 13 KB

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