usfcdev.c 11 KB

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