usfcdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2013, 2016-2017 The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/input/mt.h>
  11. #include <linux/syscalls.h>
  12. #include "usfcdev.h"
  13. #define UNDEF_ID 0xffffffff
  14. #define SLOT_CMD_ID 0
  15. #define MAX_RETRIES 10
  16. enum usdev_event_status {
  17. USFCDEV_EVENT_ENABLED,
  18. USFCDEV_EVENT_DISABLING,
  19. USFCDEV_EVENT_DISABLED,
  20. };
  21. struct usfcdev_event {
  22. bool (*match_cb)(uint16_t, struct input_dev *dev);
  23. bool registered_event;
  24. bool interleaved;
  25. enum usdev_event_status event_status;
  26. };
  27. static struct usfcdev_event s_usfcdev_events[MAX_EVENT_TYPE_NUM];
  28. struct usfcdev_input_command {
  29. unsigned int type;
  30. unsigned int code;
  31. unsigned int value;
  32. };
  33. static long s_usf_pid;
  34. static bool usfcdev_filter(struct input_handle *handle,
  35. unsigned int type, unsigned int code, int value);
  36. static bool usfcdev_match(struct input_handler *handler,
  37. struct input_dev *dev);
  38. static int usfcdev_connect(struct input_handler *handler,
  39. struct input_dev *dev,
  40. const struct input_device_id *id);
  41. static void usfcdev_disconnect(struct input_handle *handle);
  42. static const struct input_device_id usfc_tsc_ids[] = {
  43. {
  44. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  45. INPUT_DEVICE_ID_MATCH_KEYBIT |
  46. INPUT_DEVICE_ID_MATCH_ABSBIT,
  47. .evbit = { BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY) },
  48. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  49. /* assumption: ABS_X & ABS_Y are in the same long */
  50. .absbit = { [BIT_WORD(ABS_X)] = BIT_MASK(ABS_X) |
  51. BIT_MASK(ABS_Y) },
  52. },
  53. {
  54. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  55. INPUT_DEVICE_ID_MATCH_KEYBIT |
  56. INPUT_DEVICE_ID_MATCH_ABSBIT,
  57. .evbit = { BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY) },
  58. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  59. /* assumption: MT_.._X & MT_.._Y are in the same long */
  60. .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
  61. BIT_MASK(ABS_MT_POSITION_X) |
  62. BIT_MASK(ABS_MT_POSITION_Y) },
  63. },
  64. { } /* Terminating entry */
  65. };
  66. MODULE_DEVICE_TABLE(input, usfc_tsc_ids);
  67. static struct input_handler s_usfc_handlers[MAX_EVENT_TYPE_NUM] = {
  68. { /* TSC handler */
  69. .filter = usfcdev_filter,
  70. .match = usfcdev_match,
  71. .connect = usfcdev_connect,
  72. .disconnect = usfcdev_disconnect,
  73. /* .minor can be used as index in the container, */
  74. /* because .fops isn't supported */
  75. .minor = TSC_EVENT_TYPE_IND,
  76. .name = "usfc_tsc_handler",
  77. .id_table = usfc_tsc_ids,
  78. },
  79. };
  80. /*
  81. * For each event type, there are a number conflicting devices (handles)
  82. * The first registered device (primary) is real TSC device; it's mandatory
  83. * Optionally, later registered devices are simulated ones.
  84. * They are dynamically managed
  85. * The primary device's handles are stored in the below static array
  86. */
  87. static struct input_handle s_usfc_primary_handles[MAX_EVENT_TYPE_NUM] = {
  88. { /* TSC handle */
  89. .handler = &s_usfc_handlers[TSC_EVENT_TYPE_IND],
  90. .name = "usfc_tsc_handle",
  91. },
  92. };
  93. static struct usfcdev_input_command initial_clear_cmds[] = {
  94. {EV_ABS, ABS_PRESSURE, 0},
  95. {EV_KEY, BTN_TOUCH, 0},
  96. };
  97. static struct usfcdev_input_command slot_clear_cmds[] = {
  98. {EV_ABS, ABS_MT_SLOT, 0},
  99. {EV_ABS, ABS_MT_TRACKING_ID, UNDEF_ID},
  100. };
  101. static struct usfcdev_input_command no_filter_cmds[] = {
  102. {EV_ABS, ABS_MT_SLOT, 0},
  103. {EV_ABS, ABS_MT_TRACKING_ID, UNDEF_ID},
  104. {EV_SYN, SYN_REPORT, 0},
  105. };
  106. static bool usfcdev_match(struct input_handler *handler, struct input_dev *dev)
  107. {
  108. bool rc = false;
  109. int ind = handler->minor;
  110. pr_debug("%s: name=[%s]; ind=%d\n", __func__, dev->name, ind);
  111. if (s_usfcdev_events[ind].registered_event &&
  112. s_usfcdev_events[ind].match_cb) {
  113. rc = (*s_usfcdev_events[ind].match_cb)((uint16_t)ind, dev);
  114. pr_debug("%s: [%s]; rc=%d\n", __func__, dev->name, rc);
  115. }
  116. return rc;
  117. }
  118. static int usfcdev_connect(struct input_handler *handler, struct input_dev *dev,
  119. const struct input_device_id *id)
  120. {
  121. int ret = 0;
  122. uint16_t ind = handler->minor;
  123. struct input_handle *usfc_handle = NULL;
  124. if (s_usfc_primary_handles[ind].dev == NULL) {
  125. pr_debug("%s: primary device; ind=%d\n",
  126. __func__,
  127. ind);
  128. usfc_handle = &s_usfc_primary_handles[ind];
  129. } else {
  130. pr_debug("%s: secondary device; ind=%d\n",
  131. __func__,
  132. ind);
  133. usfc_handle = kzalloc(sizeof(struct input_handle),
  134. GFP_KERNEL);
  135. if (!usfc_handle)
  136. return -ENOMEM;
  137. usfc_handle->handler = &s_usfc_handlers[ind];
  138. usfc_handle->name = s_usfc_primary_handles[ind].name;
  139. }
  140. usfc_handle->dev = dev;
  141. ret = input_register_handle(usfc_handle);
  142. pr_debug("%s: name=[%s]; ind=%d; dev=0x%pK\n",
  143. __func__,
  144. dev->name,
  145. ind,
  146. usfc_handle->dev);
  147. if (ret)
  148. pr_err("%s: input_register_handle[%d] failed: ret=%d\n",
  149. __func__,
  150. ind,
  151. ret);
  152. else {
  153. ret = input_open_device(usfc_handle);
  154. if (ret) {
  155. pr_err("%s: input_open_device[%d] failed: ret=%d\n",
  156. __func__,
  157. ind,
  158. ret);
  159. input_unregister_handle(usfc_handle);
  160. } else
  161. pr_debug("%s: device[%d] is opened\n",
  162. __func__,
  163. ind);
  164. }
  165. return ret;
  166. }
  167. static void usfcdev_disconnect(struct input_handle *handle)
  168. {
  169. int ind = handle->handler->minor;
  170. input_close_device(handle);
  171. input_unregister_handle(handle);
  172. pr_debug("%s: handle[%d], name=[%s] is disconnected\n",
  173. __func__,
  174. ind,
  175. handle->dev->name);
  176. if (s_usfc_primary_handles[ind].dev == handle->dev)
  177. s_usfc_primary_handles[ind].dev = NULL;
  178. else
  179. kfree(handle);
  180. }
  181. static bool usfcdev_filter(struct input_handle *handle,
  182. unsigned int type, unsigned int code, int value)
  183. {
  184. uint16_t i = 0;
  185. uint16_t ind = (uint16_t)handle->handler->minor;
  186. bool rc = (s_usfcdev_events[ind].event_status != USFCDEV_EVENT_ENABLED);
  187. if (s_usf_pid == current->pid) {
  188. /* Pass events from usfcdev driver */
  189. rc = false;
  190. pr_debug("%s: event_type=%d; type=%d; code=%d; val=%d",
  191. __func__,
  192. ind,
  193. type,
  194. code,
  195. value);
  196. } else if (s_usfcdev_events[ind].event_status ==
  197. USFCDEV_EVENT_DISABLING) {
  198. uint32_t u_value = value;
  199. s_usfcdev_events[ind].interleaved = true;
  200. /* Pass events for freeing slots from TSC driver */
  201. for (i = 0; i < ARRAY_SIZE(no_filter_cmds); ++i) {
  202. if ((no_filter_cmds[i].type == type) &&
  203. (no_filter_cmds[i].code == code) &&
  204. (no_filter_cmds[i].value <= u_value)) {
  205. rc = false;
  206. pr_debug("%s: no_filter_cmds[%d]; %d",
  207. __func__,
  208. i,
  209. no_filter_cmds[i].value);
  210. break;
  211. }
  212. }
  213. }
  214. return rc;
  215. }
  216. bool usfcdev_register(
  217. uint16_t event_type_ind,
  218. bool (*match_cb)(uint16_t, struct input_dev *dev))
  219. {
  220. int ret = 0;
  221. bool rc = false;
  222. if ((event_type_ind >= MAX_EVENT_TYPE_NUM) || !match_cb) {
  223. pr_err("%s: wrong input: event_type_ind=%d; match_cb=0x%pK\n",
  224. __func__,
  225. event_type_ind,
  226. match_cb);
  227. return false;
  228. }
  229. if (s_usfcdev_events[event_type_ind].registered_event) {
  230. pr_info("%s: handler[%d] was already registered\n",
  231. __func__,
  232. event_type_ind);
  233. return true;
  234. }
  235. s_usfcdev_events[event_type_ind].registered_event = true;
  236. s_usfcdev_events[event_type_ind].match_cb = match_cb;
  237. s_usfcdev_events[event_type_ind].event_status = USFCDEV_EVENT_ENABLED;
  238. ret = input_register_handler(&s_usfc_handlers[event_type_ind]);
  239. if (!ret) {
  240. rc = true;
  241. pr_debug("%s: handler[%d] was registered\n",
  242. __func__,
  243. event_type_ind);
  244. } else {
  245. s_usfcdev_events[event_type_ind].registered_event = false;
  246. s_usfcdev_events[event_type_ind].match_cb = NULL;
  247. pr_err("%s: handler[%d] registration failed: ret=%d\n",
  248. __func__,
  249. event_type_ind,
  250. ret);
  251. }
  252. return rc;
  253. }
  254. void usfcdev_unregister(uint16_t event_type_ind)
  255. {
  256. if (event_type_ind >= MAX_EVENT_TYPE_NUM) {
  257. pr_err("%s: wrong input: event_type_ind=%d\n",
  258. __func__,
  259. event_type_ind);
  260. return;
  261. }
  262. if (s_usfcdev_events[event_type_ind].registered_event) {
  263. input_unregister_handler(&s_usfc_handlers[event_type_ind]);
  264. pr_debug("%s: handler[%d] was unregistered\n",
  265. __func__,
  266. event_type_ind);
  267. s_usfcdev_events[event_type_ind].registered_event = false;
  268. s_usfcdev_events[event_type_ind].match_cb = NULL;
  269. s_usfcdev_events[event_type_ind].event_status =
  270. USFCDEV_EVENT_ENABLED;
  271. }
  272. }
  273. static inline void usfcdev_send_cmd(
  274. struct input_dev *dev,
  275. struct usfcdev_input_command cmd)
  276. {
  277. input_event(dev, cmd.type, cmd.code, cmd.value);
  278. }
  279. static void usfcdev_clean_dev(uint16_t event_type_ind)
  280. {
  281. struct input_dev *dev = NULL;
  282. int i;
  283. int j;
  284. int retries = 0;
  285. if (event_type_ind >= MAX_EVENT_TYPE_NUM) {
  286. pr_err("%s: wrong input: event_type_ind=%d\n",
  287. __func__,
  288. event_type_ind);
  289. return;
  290. }
  291. /* Only primary device must exist */
  292. dev = s_usfc_primary_handles[event_type_ind].dev;
  293. if (dev == NULL) {
  294. pr_err("%s: NULL primary device\n",
  295. __func__);
  296. return;
  297. }
  298. for (i = 0; i < ARRAY_SIZE(initial_clear_cmds); i++)
  299. usfcdev_send_cmd(dev, initial_clear_cmds[i]);
  300. input_sync(dev);
  301. /* Send commands to free all slots */
  302. for (i = 0; i < dev->mt->num_slots; i++) {
  303. s_usfcdev_events[event_type_ind].interleaved = false;
  304. if (input_mt_get_value(&dev->mt->slots[i],
  305. ABS_MT_TRACKING_ID) < 0) {
  306. pr_debug("%s: skipping slot %d",
  307. __func__, i);
  308. continue;
  309. }
  310. slot_clear_cmds[SLOT_CMD_ID].value = i;
  311. for (j = 0; j < ARRAY_SIZE(slot_clear_cmds); j++)
  312. usfcdev_send_cmd(dev, slot_clear_cmds[j]);
  313. if (s_usfcdev_events[event_type_ind].interleaved) {
  314. pr_debug("%s: interleaved(%d): slot(%d)",
  315. __func__, i, dev->mt->slot);
  316. if (retries++ < MAX_RETRIES) {
  317. --i;
  318. continue;
  319. }
  320. pr_warn("%s: index(%d) reached max retires",
  321. __func__, i);
  322. }
  323. retries = 0;
  324. input_sync(dev);
  325. }
  326. }
  327. bool usfcdev_set_filter(uint16_t event_type_ind, bool filter)
  328. {
  329. bool rc = true;
  330. if (event_type_ind >= MAX_EVENT_TYPE_NUM) {
  331. pr_err("%s: wrong input: event_type_ind=%d\n",
  332. __func__,
  333. event_type_ind);
  334. return false;
  335. }
  336. if (s_usfcdev_events[event_type_ind].registered_event) {
  337. pr_debug("%s: event_type[%d]; filter=%d\n",
  338. __func__,
  339. event_type_ind,
  340. filter
  341. );
  342. if (filter) {
  343. s_usfcdev_events[event_type_ind].event_status =
  344. USFCDEV_EVENT_DISABLING;
  345. s_usf_pid = current->pid;
  346. usfcdev_clean_dev(event_type_ind);
  347. s_usfcdev_events[event_type_ind].event_status =
  348. USFCDEV_EVENT_DISABLED;
  349. } else
  350. s_usfcdev_events[event_type_ind].event_status =
  351. USFCDEV_EVENT_ENABLED;
  352. } else {
  353. pr_err("%s: event_type[%d] isn't registered\n",
  354. __func__,
  355. event_type_ind);
  356. rc = false;
  357. }
  358. return rc;
  359. }