touch-overlay.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Helper functions for overlay objects on touchscreens
  4. *
  5. * Copyright (c) 2023 Javier Carrasco <javier.carrasco@wolfvision.net>
  6. */
  7. #include <linux/export.h>
  8. #include <linux/input.h>
  9. #include <linux/input/mt.h>
  10. #include <linux/input/touch-overlay.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/property.h>
  14. struct touch_overlay_segment {
  15. struct list_head list;
  16. u32 x_origin;
  17. u32 y_origin;
  18. u32 x_size;
  19. u32 y_size;
  20. u32 key;
  21. bool pressed;
  22. int slot;
  23. };
  24. static int touch_overlay_get_segment(struct fwnode_handle *segment_node,
  25. struct touch_overlay_segment *segment,
  26. struct input_dev *input)
  27. {
  28. int error;
  29. error = fwnode_property_read_u32(segment_node, "x-origin",
  30. &segment->x_origin);
  31. if (error)
  32. return error;
  33. error = fwnode_property_read_u32(segment_node, "y-origin",
  34. &segment->y_origin);
  35. if (error)
  36. return error;
  37. error = fwnode_property_read_u32(segment_node, "x-size",
  38. &segment->x_size);
  39. if (error)
  40. return error;
  41. error = fwnode_property_read_u32(segment_node, "y-size",
  42. &segment->y_size);
  43. if (error)
  44. return error;
  45. error = fwnode_property_read_u32(segment_node, "linux,code",
  46. &segment->key);
  47. if (!error)
  48. input_set_capability(input, EV_KEY, segment->key);
  49. else if (error != -EINVAL)
  50. return error;
  51. return 0;
  52. }
  53. /**
  54. * touch_overlay_map - map overlay objects from the device tree and set
  55. * key capabilities if buttons are defined.
  56. * @list: pointer to the list that will hold the segments
  57. * @input: pointer to the already allocated input_dev
  58. *
  59. * Returns 0 on success and error number otherwise.
  60. *
  61. * If buttons are defined, key capabilities are set accordingly.
  62. */
  63. int touch_overlay_map(struct list_head *list, struct input_dev *input)
  64. {
  65. struct fwnode_handle *fw_segment;
  66. struct device *dev = input->dev.parent;
  67. struct touch_overlay_segment *segment;
  68. int error;
  69. struct fwnode_handle *overlay __free(fwnode_handle) =
  70. device_get_named_child_node(dev, "touch-overlay");
  71. if (!overlay)
  72. return 0;
  73. fwnode_for_each_available_child_node(overlay, fw_segment) {
  74. segment = devm_kzalloc(dev, sizeof(*segment), GFP_KERNEL);
  75. if (!segment) {
  76. fwnode_handle_put(fw_segment);
  77. return -ENOMEM;
  78. }
  79. error = touch_overlay_get_segment(fw_segment, segment, input);
  80. if (error) {
  81. fwnode_handle_put(fw_segment);
  82. return error;
  83. }
  84. list_add_tail(&segment->list, list);
  85. }
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(touch_overlay_map);
  89. /**
  90. * touch_overlay_get_touchscreen_abs - get abs size from the touchscreen area.
  91. * @list: pointer to the list that holds the segments
  92. * @x: horizontal abs
  93. * @y: vertical abs
  94. */
  95. void touch_overlay_get_touchscreen_abs(struct list_head *list, u16 *x, u16 *y)
  96. {
  97. struct touch_overlay_segment *segment;
  98. struct list_head *ptr;
  99. list_for_each(ptr, list) {
  100. segment = list_entry(ptr, struct touch_overlay_segment, list);
  101. if (!segment->key) {
  102. *x = segment->x_size - 1;
  103. *y = segment->y_size - 1;
  104. break;
  105. }
  106. }
  107. }
  108. EXPORT_SYMBOL(touch_overlay_get_touchscreen_abs);
  109. static bool touch_overlay_segment_event(struct touch_overlay_segment *seg,
  110. struct input_mt_pos *pos)
  111. {
  112. if (pos->x >= seg->x_origin && pos->x < (seg->x_origin + seg->x_size) &&
  113. pos->y >= seg->y_origin && pos->y < (seg->y_origin + seg->y_size))
  114. return true;
  115. return false;
  116. }
  117. /**
  118. * touch_overlay_mapped_touchscreen - check if a touchscreen area is mapped
  119. * @list: pointer to the list that holds the segments
  120. *
  121. * Returns true if a touchscreen area is mapped or false otherwise.
  122. */
  123. bool touch_overlay_mapped_touchscreen(struct list_head *list)
  124. {
  125. struct touch_overlay_segment *segment;
  126. struct list_head *ptr;
  127. list_for_each(ptr, list) {
  128. segment = list_entry(ptr, struct touch_overlay_segment, list);
  129. if (!segment->key)
  130. return true;
  131. }
  132. return false;
  133. }
  134. EXPORT_SYMBOL(touch_overlay_mapped_touchscreen);
  135. static bool touch_overlay_event_on_ts(struct list_head *list,
  136. struct input_mt_pos *pos)
  137. {
  138. struct touch_overlay_segment *segment;
  139. struct list_head *ptr;
  140. list_for_each(ptr, list) {
  141. segment = list_entry(ptr, struct touch_overlay_segment, list);
  142. if (segment->key)
  143. continue;
  144. if (touch_overlay_segment_event(segment, pos)) {
  145. pos->x -= segment->x_origin;
  146. pos->y -= segment->y_origin;
  147. return true;
  148. }
  149. /* ignore touch events outside the defined area */
  150. return false;
  151. }
  152. return true;
  153. }
  154. static bool touch_overlay_button_event(struct input_dev *input,
  155. struct touch_overlay_segment *segment,
  156. struct input_mt_pos *pos, int slot)
  157. {
  158. struct input_mt *mt = input->mt;
  159. struct input_mt_slot *s = &mt->slots[slot];
  160. bool button_contact = touch_overlay_segment_event(segment, pos);
  161. if (segment->slot == slot && segment->pressed) {
  162. /* sliding out of the button releases it */
  163. if (!button_contact) {
  164. input_report_key(input, segment->key, false);
  165. segment->pressed = false;
  166. /* keep available for a possible touch event */
  167. return false;
  168. }
  169. /* ignore sliding on the button while pressed */
  170. s->frame = mt->frame;
  171. return true;
  172. } else if (button_contact) {
  173. input_report_key(input, segment->key, true);
  174. s->frame = mt->frame;
  175. segment->slot = slot;
  176. segment->pressed = true;
  177. return true;
  178. }
  179. return false;
  180. }
  181. /**
  182. * touch_overlay_sync_frame - update the status of the segments and report
  183. * buttons whose tracked slot is unused.
  184. * @list: pointer to the list that holds the segments
  185. * @input: pointer to the input device associated to the contact
  186. */
  187. void touch_overlay_sync_frame(struct list_head *list, struct input_dev *input)
  188. {
  189. struct touch_overlay_segment *segment;
  190. struct input_mt *mt = input->mt;
  191. struct input_mt_slot *s;
  192. struct list_head *ptr;
  193. list_for_each(ptr, list) {
  194. segment = list_entry(ptr, struct touch_overlay_segment, list);
  195. if (!segment->key)
  196. continue;
  197. s = &mt->slots[segment->slot];
  198. if (!input_mt_is_used(mt, s) && segment->pressed) {
  199. input_report_key(input, segment->key, false);
  200. segment->pressed = false;
  201. }
  202. }
  203. }
  204. EXPORT_SYMBOL(touch_overlay_sync_frame);
  205. /**
  206. * touch_overlay_process_contact - process contacts according to the overlay
  207. * mapping. This function acts as a filter to release the calling driver
  208. * from the contacts that are either related to overlay buttons or out of the
  209. * overlay touchscreen area, if defined.
  210. * @list: pointer to the list that holds the segments
  211. * @input: pointer to the input device associated to the contact
  212. * @pos: pointer to the contact position
  213. * @slot: slot associated to the contact (0 if multitouch is not supported)
  214. *
  215. * Returns true if the contact was processed (reported for valid key events
  216. * and dropped for contacts outside the overlay touchscreen area) or false
  217. * if the contact must be processed by the caller. In that case this function
  218. * shifts the (x,y) coordinates to the overlay touchscreen axis if required.
  219. */
  220. bool touch_overlay_process_contact(struct list_head *list,
  221. struct input_dev *input,
  222. struct input_mt_pos *pos, int slot)
  223. {
  224. struct touch_overlay_segment *segment;
  225. struct list_head *ptr;
  226. /*
  227. * buttons must be prioritized over overlay touchscreens to account for
  228. * overlappings e.g. a button inside the touchscreen area.
  229. */
  230. list_for_each(ptr, list) {
  231. segment = list_entry(ptr, struct touch_overlay_segment, list);
  232. if (segment->key &&
  233. touch_overlay_button_event(input, segment, pos, slot))
  234. return true;
  235. }
  236. /*
  237. * valid contacts on the overlay touchscreen are left for the client
  238. * to be processed/reported according to its (possibly) unique features.
  239. */
  240. return !touch_overlay_event_on_ts(list, pos);
  241. }
  242. EXPORT_SYMBOL(touch_overlay_process_contact);
  243. MODULE_LICENSE("GPL");
  244. MODULE_DESCRIPTION("Helper functions for overlay objects on touch devices");