input-mt.c 13 KB

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