input-mt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
  11. static void copy_abs(struct input_dev *dev, unsigned int dst, unsigned int src)
  12. {
  13. if (dev->absinfo && test_bit(src, dev->absbit)) {
  14. dev->absinfo[dst] = dev->absinfo[src];
  15. dev->absinfo[dst].fuzz = 0;
  16. __set_bit(dst, dev->absbit);
  17. }
  18. }
  19. /**
  20. * input_mt_init_slots() - initialize MT input slots
  21. * @dev: input device supporting MT events and finger tracking
  22. * @num_slots: number of slots used by the device
  23. * @flags: mt tasks to handle in core
  24. *
  25. * This function allocates all necessary memory for MT slot handling
  26. * in the input device, prepares the ABS_MT_SLOT and
  27. * ABS_MT_TRACKING_ID events for use and sets up appropriate buffers.
  28. * Depending on the flags set, it also performs pointer emulation and
  29. * frame synchronization.
  30. *
  31. * May be called repeatedly. Returns -EINVAL if attempting to
  32. * reinitialize with a different number of slots.
  33. */
  34. int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots,
  35. unsigned int flags)
  36. {
  37. struct input_mt *mt = dev->mt;
  38. int i;
  39. if (!num_slots)
  40. return 0;
  41. if (mt)
  42. return mt->num_slots != num_slots ? -EINVAL : 0;
  43. mt = kzalloc(struct_size(mt, slots, num_slots), GFP_KERNEL);
  44. if (!mt)
  45. goto err_mem;
  46. mt->num_slots = num_slots;
  47. mt->flags = flags;
  48. input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
  49. input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0);
  50. if (flags & (INPUT_MT_POINTER | INPUT_MT_DIRECT)) {
  51. __set_bit(EV_KEY, dev->evbit);
  52. __set_bit(BTN_TOUCH, dev->keybit);
  53. copy_abs(dev, ABS_X, ABS_MT_POSITION_X);
  54. copy_abs(dev, ABS_Y, ABS_MT_POSITION_Y);
  55. copy_abs(dev, ABS_PRESSURE, ABS_MT_PRESSURE);
  56. }
  57. if (flags & INPUT_MT_POINTER) {
  58. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  59. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  60. if (num_slots >= 3)
  61. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  62. if (num_slots >= 4)
  63. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  64. if (num_slots >= 5)
  65. __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
  66. __set_bit(INPUT_PROP_POINTER, dev->propbit);
  67. }
  68. if (flags & INPUT_MT_DIRECT)
  69. __set_bit(INPUT_PROP_DIRECT, dev->propbit);
  70. if (flags & INPUT_MT_SEMI_MT)
  71. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  72. if (flags & INPUT_MT_TRACK) {
  73. unsigned int n2 = num_slots * num_slots;
  74. mt->red = kcalloc(n2, sizeof(*mt->red), GFP_KERNEL);
  75. if (!mt->red)
  76. goto err_mem;
  77. }
  78. /* Mark slots as 'inactive' */
  79. for (i = 0; i < num_slots; i++)
  80. input_mt_set_value(&mt->slots[i], ABS_MT_TRACKING_ID, -1);
  81. /* Mark slots as 'unused' */
  82. mt->frame = 1;
  83. dev->mt = mt;
  84. return 0;
  85. err_mem:
  86. kfree(mt);
  87. return -ENOMEM;
  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. if (!mt)
  179. return;
  180. oldest = NULL;
  181. oldid = mt->trkid;
  182. count = 0;
  183. for (i = 0; i < mt->num_slots; ++i) {
  184. struct input_mt_slot *ps = &mt->slots[i];
  185. int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
  186. if (id < 0)
  187. continue;
  188. if ((id - oldid) & TRKID_SGN) {
  189. oldest = ps;
  190. oldid = id;
  191. }
  192. count++;
  193. }
  194. input_event(dev, EV_KEY, BTN_TOUCH, count > 0);
  195. if (use_count) {
  196. if (count == 0 &&
  197. !test_bit(ABS_MT_DISTANCE, dev->absbit) &&
  198. test_bit(ABS_DISTANCE, dev->absbit) &&
  199. input_abs_get_val(dev, ABS_DISTANCE) != 0) {
  200. /*
  201. * Force reporting BTN_TOOL_FINGER for devices that
  202. * only report general hover (and not per-contact
  203. * distance) when contact is in proximity but not
  204. * on the surface.
  205. */
  206. count = 1;
  207. }
  208. input_mt_report_finger_count(dev, count);
  209. }
  210. if (oldest) {
  211. int x = input_mt_get_value(oldest, ABS_MT_POSITION_X);
  212. int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y);
  213. input_event(dev, EV_ABS, ABS_X, x);
  214. input_event(dev, EV_ABS, ABS_Y, y);
  215. if (test_bit(ABS_MT_PRESSURE, dev->absbit)) {
  216. int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
  217. input_event(dev, EV_ABS, ABS_PRESSURE, p);
  218. }
  219. } else {
  220. if (test_bit(ABS_MT_PRESSURE, dev->absbit))
  221. input_event(dev, EV_ABS, ABS_PRESSURE, 0);
  222. }
  223. }
  224. EXPORT_SYMBOL(input_mt_report_pointer_emulation);
  225. static void __input_mt_drop_unused(struct input_dev *dev, struct input_mt *mt)
  226. {
  227. int i;
  228. for (i = 0; i < mt->num_slots; i++) {
  229. if (!input_mt_is_used(mt, &mt->slots[i])) {
  230. input_mt_slot(dev, i);
  231. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
  232. }
  233. }
  234. }
  235. /**
  236. * input_mt_drop_unused() - Inactivate slots not seen in this frame
  237. * @dev: input device with allocated MT slots
  238. *
  239. * Lift all slots not seen since the last call to this function.
  240. */
  241. void input_mt_drop_unused(struct input_dev *dev)
  242. {
  243. struct input_mt *mt = dev->mt;
  244. if (mt) {
  245. __input_mt_drop_unused(dev, mt);
  246. mt->frame++;
  247. }
  248. }
  249. EXPORT_SYMBOL(input_mt_drop_unused);
  250. /**
  251. * input_mt_sync_frame() - synchronize mt frame
  252. * @dev: input device with allocated MT slots
  253. *
  254. * Close the frame and prepare the internal state for a new one.
  255. * Depending on the flags, marks unused slots as inactive and performs
  256. * pointer emulation.
  257. */
  258. void input_mt_sync_frame(struct input_dev *dev)
  259. {
  260. struct input_mt *mt = dev->mt;
  261. bool use_count = false;
  262. if (!mt)
  263. return;
  264. if (mt->flags & INPUT_MT_DROP_UNUSED)
  265. __input_mt_drop_unused(dev, mt);
  266. if ((mt->flags & INPUT_MT_POINTER) && !(mt->flags & INPUT_MT_SEMI_MT))
  267. use_count = true;
  268. input_mt_report_pointer_emulation(dev, use_count);
  269. mt->frame++;
  270. }
  271. EXPORT_SYMBOL(input_mt_sync_frame);
  272. static int adjust_dual(int *begin, int step, int *end, int eq, int mu)
  273. {
  274. int f, *p, s, c;
  275. if (begin == end)
  276. return 0;
  277. f = *begin;
  278. p = begin + step;
  279. s = p == end ? f + 1 : *p;
  280. for (; p != end; p += step) {
  281. if (*p < f) {
  282. s = f;
  283. f = *p;
  284. } else if (*p < s) {
  285. s = *p;
  286. }
  287. }
  288. c = (f + s + 1) / 2;
  289. if (c == 0 || (c > mu && (!eq || mu > 0)))
  290. return 0;
  291. /* Improve convergence for positive matrices by penalizing overcovers */
  292. if (s < 0 && mu <= 0)
  293. c *= 2;
  294. for (p = begin; p != end; p += step)
  295. *p -= c;
  296. return (c < s && s <= 0) || (f >= 0 && f < c);
  297. }
  298. static void find_reduced_matrix(int *w, int nr, int nc, int nrc, int mu)
  299. {
  300. int i, k, sum;
  301. for (k = 0; k < nrc; k++) {
  302. for (i = 0; i < nr; i++)
  303. adjust_dual(w + i, nr, w + i + nrc, nr <= nc, mu);
  304. sum = 0;
  305. for (i = 0; i < nrc; i += nr)
  306. sum += adjust_dual(w + i, 1, w + i + nr, nc <= nr, mu);
  307. if (!sum)
  308. break;
  309. }
  310. }
  311. static int input_mt_set_matrix(struct input_mt *mt,
  312. const struct input_mt_pos *pos, int num_pos,
  313. int mu)
  314. {
  315. const struct input_mt_pos *p;
  316. struct input_mt_slot *s;
  317. int *w = mt->red;
  318. int x, y;
  319. for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
  320. if (!input_mt_is_active(s))
  321. continue;
  322. x = input_mt_get_value(s, ABS_MT_POSITION_X);
  323. y = input_mt_get_value(s, ABS_MT_POSITION_Y);
  324. for (p = pos; p != pos + num_pos; p++) {
  325. int dx = x - p->x, dy = y - p->y;
  326. *w++ = dx * dx + dy * dy - mu;
  327. }
  328. }
  329. return w - mt->red;
  330. }
  331. static void input_mt_set_slots(struct input_mt *mt,
  332. int *slots, int num_pos)
  333. {
  334. struct input_mt_slot *s;
  335. int *w = mt->red, j;
  336. for (j = 0; j != num_pos; j++)
  337. slots[j] = -1;
  338. for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
  339. if (!input_mt_is_active(s))
  340. continue;
  341. for (j = 0; j != num_pos; j++) {
  342. if (w[j] < 0) {
  343. slots[j] = s - mt->slots;
  344. break;
  345. }
  346. }
  347. w += num_pos;
  348. }
  349. for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
  350. if (input_mt_is_active(s))
  351. continue;
  352. for (j = 0; j != num_pos; j++) {
  353. if (slots[j] < 0) {
  354. slots[j] = s - mt->slots;
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. /**
  361. * input_mt_assign_slots() - perform a best-match assignment
  362. * @dev: input device with allocated MT slots
  363. * @slots: the slot assignment to be filled
  364. * @pos: the position array to match
  365. * @num_pos: number of positions
  366. * @dmax: maximum ABS_MT_POSITION displacement (zero for infinite)
  367. *
  368. * Performs a best match against the current contacts and returns
  369. * the slot assignment list. New contacts are assigned to unused
  370. * slots.
  371. *
  372. * The assignments are balanced so that all coordinate displacements are
  373. * below the euclidian distance dmax. If no such assignment can be found,
  374. * some contacts are assigned to unused slots.
  375. *
  376. * Returns zero on success, or negative error in case of failure.
  377. */
  378. int input_mt_assign_slots(struct input_dev *dev, int *slots,
  379. const struct input_mt_pos *pos, int num_pos,
  380. int dmax)
  381. {
  382. struct input_mt *mt = dev->mt;
  383. int mu = 2 * dmax * dmax;
  384. int nrc;
  385. if (!mt || !mt->red)
  386. return -ENXIO;
  387. if (num_pos > mt->num_slots)
  388. return -EINVAL;
  389. if (num_pos < 1)
  390. return 0;
  391. nrc = input_mt_set_matrix(mt, pos, num_pos, mu);
  392. find_reduced_matrix(mt->red, num_pos, nrc / num_pos, nrc, mu);
  393. input_mt_set_slots(mt, slots, num_pos);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL(input_mt_assign_slots);
  397. /**
  398. * input_mt_get_slot_by_key() - return slot matching key
  399. * @dev: input device with allocated MT slots
  400. * @key: the key of the sought slot
  401. *
  402. * Returns the slot of the given key, if it exists, otherwise
  403. * set the key on the first unused slot and return.
  404. *
  405. * If no available slot can be found, -1 is returned.
  406. * Note that for this function to work properly, input_mt_sync_frame() has
  407. * to be called at each frame.
  408. */
  409. int input_mt_get_slot_by_key(struct input_dev *dev, int key)
  410. {
  411. struct input_mt *mt = dev->mt;
  412. struct input_mt_slot *s;
  413. if (!mt)
  414. return -1;
  415. for (s = mt->slots; s != mt->slots + mt->num_slots; s++)
  416. if (input_mt_is_active(s) && s->key == key)
  417. return s - mt->slots;
  418. for (s = mt->slots; s != mt->slots + mt->num_slots; s++)
  419. if (!input_mt_is_active(s) && !input_mt_is_used(mt, s)) {
  420. s->key = key;
  421. return s - mt->slots;
  422. }
  423. return -1;
  424. }
  425. EXPORT_SYMBOL(input_mt_get_slot_by_key);