evdev_booster.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #define ITAG " [Evdev Booster] "
  2. #include <linux/input/input_booster.h>
  3. #include <linux/input.h>
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/device.h>
  7. #include <linux/syscalls.h>
  8. #if IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER)
  9. spinlock_t ib_ev_lock;
  10. struct workqueue_struct *ev_unbound_wq;
  11. static struct device *evbst_dev;
  12. void input_booster(struct ib_event_data *ib_ev_data);
  13. int evdev_ib_notify_callback(struct notifier_block *nb,
  14. unsigned long event_type, void *data)
  15. {
  16. if (ib_init_succeed) {
  17. spin_lock(&ib_ev_lock);
  18. struct ib_event_data *ib_ev_data = data;
  19. switch (event_type) {
  20. case IB_EVENT_TOUCH_BOOSTER:
  21. input_booster(ib_ev_data);
  22. break;
  23. default:
  24. break;
  25. }
  26. spin_unlock(&ib_ev_lock);
  27. }
  28. return 0;
  29. }
  30. static struct notifier_block ib_event_notifier = {
  31. .notifier_call = evdev_ib_notify_callback,
  32. .priority = 1,
  33. };
  34. #if defined(CONFIG_SEC_INPUT_BOOSTER_HANDLER)
  35. struct workqueue_struct *ib_unbound_highwq;
  36. spinlock_t ib_idx_lock;
  37. struct ib_event_work *ib_evt_work;
  38. int ib_work_cnt;
  39. int ib_notifier_register(struct notifier_block *nb) {
  40. /* nothing to do here */
  41. return 0;
  42. }
  43. int ib_notifier_unregister(struct notifier_block *nb) {
  44. /* nothing to do here */
  45. return 0;
  46. }
  47. static void evdev_ib_trigger(struct work_struct* work)
  48. {
  49. struct ib_event_work *ib_work = container_of(work, struct ib_event_work, evdev_work);
  50. struct ib_event_data ib_data = {0, };
  51. spin_lock(&ib_ev_lock);
  52. ib_data.evt_cnt = ib_work->evt_cnt;
  53. ib_data.vals = ib_work->vals;
  54. input_booster(&ib_data);
  55. spin_unlock(&ib_ev_lock);
  56. }
  57. static void sec_input_boost_events(struct input_handle *handle,
  58. const struct input_value *vals, unsigned int count) {
  59. int cur_ib_idx;
  60. spin_lock(&ib_idx_lock);
  61. cur_ib_idx = ib_work_cnt++;
  62. if (ib_work_cnt >= MAX_IB_COUNT) {
  63. pr_info("[Input Booster] Ib_Work_Cnt(%d), Event_Cnt(%d)\n", ib_work_cnt, count);
  64. ib_work_cnt = 0;
  65. }
  66. if (ib_evt_work != NULL) {
  67. ib_evt_work[cur_ib_idx].evt_cnt = count;
  68. memcpy(ib_evt_work[cur_ib_idx].vals, vals, sizeof(struct input_value) * count);
  69. queue_work(ib_unbound_highwq, &(ib_evt_work[cur_ib_idx].evdev_work));
  70. }
  71. spin_unlock(&ib_idx_lock);
  72. }
  73. static void sec_input_boost_event(struct input_handle *handle,
  74. unsigned int type, unsigned int code, int value)
  75. {
  76. struct input_value vals[] = { { type, code, value } };
  77. sec_input_boost_events(handle, vals, 1);
  78. }
  79. static int sec_input_boost_connect(struct input_handler *handler,
  80. struct input_dev *dev, const struct input_device_id *id)
  81. {
  82. struct input_handle *handle;
  83. int error;
  84. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  85. if (!handle)
  86. return -ENOMEM;
  87. /*
  88. dev_name(&dev->dev) : eventX
  89. dev->name : input driver name (like touchsreen)
  90. */
  91. handle->dev = input_get_device(dev);
  92. handle->name = "sec_input_booster";
  93. handle->handler = handler;
  94. error = input_register_handle(handle);
  95. if (error)
  96. goto err2;
  97. error = input_open_device(handle);
  98. if (error)
  99. goto err1;
  100. return 0;
  101. err1:
  102. input_unregister_handle(handle);
  103. err2:
  104. kfree(handle);
  105. return error;
  106. }
  107. static void sec_input_boost_disconnect(struct input_handle *handle)
  108. {
  109. input_close_device(handle);
  110. input_unregister_handle(handle);
  111. kfree(handle);
  112. }
  113. static const struct input_device_id sec_input_boost_ids[] = {
  114. /* multi-touch touchscreen */
  115. {
  116. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  117. INPUT_DEVICE_ID_MATCH_ABSBIT,
  118. .evbit = { BIT_MASK(EV_ABS) },
  119. .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
  120. BIT_MASK(ABS_MT_POSITION_X) |
  121. BIT_MASK(ABS_MT_POSITION_Y)
  122. },
  123. },
  124. /* touchpad */
  125. {
  126. .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
  127. INPUT_DEVICE_ID_MATCH_ABSBIT,
  128. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  129. .absbit = { [BIT_WORD(ABS_X)] =
  130. BIT_MASK(ABS_X) | BIT_MASK(ABS_Y)
  131. },
  132. },
  133. /* Keypad */
  134. {
  135. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  136. .evbit = { BIT_MASK(EV_KEY) },
  137. },
  138. { },
  139. };
  140. static struct input_handler ib_input_handler = {
  141. .event = sec_input_boost_event,
  142. .events = sec_input_boost_events,
  143. .connect = sec_input_boost_connect,
  144. .disconnect = sec_input_boost_disconnect,
  145. .name = "sec_input_booster",
  146. .id_table = sec_input_boost_ids,
  147. };
  148. static int init_input_handler(void)
  149. {
  150. int i;
  151. ib_work_cnt = 0;
  152. spin_lock_init(&ib_idx_lock);
  153. ib_evt_work = kmalloc(sizeof(struct ib_event_work) * MAX_IB_COUNT, GFP_KERNEL);
  154. if (ib_evt_work != NULL)
  155. for (i = 0; i < MAX_IB_COUNT; i++)
  156. INIT_WORK(&(ib_evt_work[i].evdev_work), evdev_ib_trigger);
  157. ib_unbound_highwq
  158. = alloc_ordered_workqueue("ib_unbound_highwq", WQ_HIGHPRI);
  159. if (!ib_unbound_highwq)
  160. return -EPERM;
  161. return input_register_handler(&ib_input_handler);
  162. }
  163. #endif
  164. int chk_next_data(const struct input_value *vals, int next_idx, int input_type)
  165. {
  166. int ret_val = 0;
  167. int next_type = -1;
  168. int next_code = -1;
  169. next_type = vals[next_idx].type;
  170. next_code = vals[next_idx].code;
  171. switch (input_type) {
  172. case BTN_TOUCH:
  173. if ((next_type == EV_ABS) && (next_code == ABS_PRESSURE))
  174. ret_val = 1;
  175. break;
  176. case EV_KEY:
  177. ret_val = 1;
  178. break;
  179. default:
  180. break;
  181. }
  182. return ret_val;
  183. }
  184. int chk_boost_on_off(const struct input_value *vals, int idx, int dev_type)
  185. {
  186. int ret_val = -1;
  187. if (dev_type < 0)
  188. return ret_val;
  189. /* In case of SPEN or HOVER, it must be empty multi event
  190. * Before starting input booster.
  191. */
  192. if (dev_type == SPEN || dev_type == HOVER) {
  193. if (!evdev_mt_event[dev_type] && vals[idx].value)
  194. ret_val = 1;
  195. else if (evdev_mt_event[dev_type] && !vals[idx].value)
  196. ret_val = 0;
  197. } else if (dev_type == TOUCH || dev_type == MULTI_TOUCH) {
  198. if (vals[idx].value >= 0)
  199. ret_val = 1;
  200. else
  201. ret_val = 0;
  202. } else if (vals[idx].value > 0)
  203. ret_val = 1;
  204. else if (vals[idx].value <= 0)
  205. ret_val = 0;
  206. return ret_val;
  207. }
  208. /*
  209. * get_device_type : Define type of device for input_booster.
  210. * device_type : Current device that in which input events triggered.
  211. * keyId : Each device get given unique keyid using Type, Code, Slot values
  212. * to identify which booster will be triggered.
  213. * cur_idx : Pointing current handling index from input booster.
  214. * cur_idx will be updated when cur_idx is same as head.
  215. * vals : Event set to determine what booster would be triggered.
  216. * evt_cnt : Total count of event in this time.
  217. */
  218. int get_device_type(int *device_type, unsigned int *keyId,
  219. int *cur_idx, const struct input_value *vals, int evt_cnt)
  220. {
  221. int i;
  222. int ret_val = -1;
  223. int dev_type = NONE_TYPE_DEVICE;
  224. int uniq_slot = 0;
  225. int next_idx = 0 ;
  226. int target_idx = 0;
  227. if (evt_cnt > MAX_EVENTS) {
  228. dev_warn_ratelimited(evbst_dev, "Exceed max event number\n");
  229. return ret_val;
  230. }
  231. /* Initializing device type before finding the proper device type. */
  232. *device_type = dev_type;
  233. for (i = *cur_idx; i < evt_cnt; i++) {
  234. pr_booster(" %s Type : %d, Code : %d, Value : %d, idx : %d\n",
  235. "Input Data || ", vals[i].type,
  236. vals[i].code, vals[i].value, i);
  237. if (vals[i].type == EV_SYN || vals[i].code == SYN_REPORT) {
  238. break;
  239. }
  240. if (vals[i].type == EV_KEY) {
  241. target_idx = i;
  242. switch (vals[i].code) {
  243. case BTN_TOUCH:
  244. next_idx = i+1;
  245. if (!chk_next_data(vals, next_idx, BTN_TOUCH))
  246. break;
  247. dev_type = SPEN;
  248. break;
  249. case BTN_TOOL_PEN:
  250. dev_type = HOVER;
  251. break;
  252. case KEY_BACK:
  253. case KEY_HOMEPAGE:
  254. dev_type = TOUCH_KEY;
  255. break;
  256. case KEY_VOLUMEUP:
  257. case KEY_VOLUMEDOWN:
  258. case KEY_POWER:
  259. #if IS_ENABLED(CONFIG_SOC_S5E5515) // watch - lower key(back key) Code : 0x244(580)
  260. case KEY_APPSELECT:
  261. #endif
  262. dev_type = KEY;
  263. break;
  264. default:
  265. break;
  266. }
  267. } else if (vals[i].type == EV_ABS) {
  268. target_idx = i;
  269. switch (vals[i].code) {
  270. case ABS_MT_TRACKING_ID:
  271. if (vals[i].value >= 0) {
  272. evdev_mt_slot++;
  273. } else {
  274. evdev_mt_slot--;
  275. if (evdev_mt_slot < 0) {
  276. evdev_mt_slot = 0;
  277. break;
  278. }
  279. }
  280. if (vals[i].value >= 0) {
  281. if (evdev_mt_slot == 1) {
  282. dev_type = TOUCH;
  283. uniq_slot = 1;
  284. } else if (evdev_mt_slot == 2) {
  285. dev_type = MULTI_TOUCH;
  286. uniq_slot = 2;
  287. }
  288. } else if (vals[i].value < 0) {
  289. if (evdev_mt_slot == 0) {
  290. dev_type = TOUCH;
  291. uniq_slot = 1;
  292. } else if (evdev_mt_slot == 1) {
  293. dev_type = MULTI_TOUCH;
  294. uniq_slot = 2;
  295. }
  296. }
  297. pr_booster("Touch Booster Trigger(%d), T(%d) C(%d) V(%d), Idx(%d), Cnt(%d)",
  298. evdev_mt_slot,
  299. vals[i].type, vals[i].code, vals[i].value,
  300. i, evt_cnt);
  301. break;
  302. }
  303. } else if (vals[i].type == EV_MSC &&
  304. vals[i].code == MSC_SCAN) {
  305. next_idx = i++;
  306. if (!chk_next_data(vals, next_idx, EV_KEY))
  307. break;
  308. next_idx = i++;
  309. target_idx = next_idx;
  310. switch (vals[next_idx].code) {
  311. case BTN_LEFT: /* Checking Touch Button Event */
  312. case BTN_RIGHT:
  313. case BTN_MIDDLE:
  314. dev_type = MOUSE;
  315. //Remain the last of CODE value as a uniq_slot to recognize BTN Type (LEFT, RIGHT, MIDDLE)
  316. uniq_slot = vals[next_idx].code;
  317. break;
  318. default: /* Checking Keyboard Event */
  319. dev_type = KEYBOARD;
  320. uniq_slot = vals[next_idx].code;
  321. pr_booster("KBD Booster Trigger(%d), Type(%d), Code(%d), Val(%d), Idx(%d), Cnt(%d)\n",
  322. vals[next_idx].code,
  323. vals[i].type, vals[i].code, vals[i].value,
  324. i, evt_cnt);
  325. break;
  326. }
  327. }
  328. if (dev_type != NONE_TYPE_DEVICE ) {
  329. *keyId = create_uniq_id(vals[i].type, vals[i].code, uniq_slot);
  330. ret_val = chk_boost_on_off(vals, target_idx, dev_type);
  331. pr_booster("Dev type Find(%d), KeyID(%d), enable(%d), Target(%d)\n",
  332. dev_type, *keyId, ret_val, target_idx);
  333. break;
  334. }
  335. }
  336. *cur_idx = i+1;
  337. *device_type = dev_type;
  338. return ret_val;
  339. }
  340. // ********** Detect Events ********** //
  341. //void input_booster(int head, int bufsize)
  342. void input_booster(struct ib_event_data *ib_ev_data)
  343. {
  344. #if IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER_QC) || \
  345. IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER_SLSI) || \
  346. IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER_MTK)
  347. int dev_type = 0;
  348. int keyId = 0;
  349. int cur_idx = -1;
  350. pr_booster("%s Triggered :: evt_cnt(%d), ib_init_succeed(%d)",
  351. __func__, ib_ev_data->evt_cnt, ib_init_succeed);
  352. if (!ib_init_succeed || ib_ev_data->evt_cnt == 0) {
  353. pr_err(ITAG"ev_cnt(%d) dev is Null OR dt_infor hasn't mem alloc\n", ib_ev_data->evt_cnt);
  354. return;
  355. }
  356. cur_idx = 0;
  357. while (cur_idx < ib_ev_data->evt_cnt) {
  358. keyId = 0;
  359. int enable = get_device_type(&dev_type, &keyId, &cur_idx, ib_ev_data->vals, ib_ev_data->evt_cnt);
  360. if (enable < 0 || keyId == 0) {
  361. continue;
  362. }
  363. if (dev_type <= NONE_TYPE_DEVICE || dev_type >= MAX_DEVICE_TYPE_NUM) {
  364. continue;
  365. }
  366. if (enable == BOOSTER_ON) {
  367. evdev_mt_event[dev_type]++;
  368. } else {
  369. evdev_mt_event[dev_type]--;
  370. }
  371. pr_booster("Device_Type(%d), KeyId(%d), IB_Cnt(%d), enable(%d)",
  372. dev_type, keyId, trigger_cnt, enable);
  373. ib_trigger[trigger_cnt].key_id = keyId;
  374. ib_trigger[trigger_cnt].event_type = enable;
  375. ib_trigger[trigger_cnt].dev_type = dev_type;
  376. queue_work(ev_unbound_wq, &(ib_trigger[trigger_cnt++].ib_trigger_work));
  377. trigger_cnt = (trigger_cnt == MAX_IB_COUNT) ? 0 : trigger_cnt;
  378. }
  379. #endif
  380. }
  381. static int __init ev_boost_init(void)
  382. {
  383. int err;
  384. pr_info(ITAG" Input Booster Module Init\n");
  385. input_booster_init();
  386. pr_info(ITAG" Input Booster Module Init End\n");
  387. spin_lock_init(&ib_ev_lock);
  388. ib_notifier_register(&ib_event_notifier);
  389. ev_unbound_wq =
  390. alloc_ordered_workqueue("ev_unbound_wq", WQ_HIGHPRI);
  391. evbst_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  392. dev_set_name(evbst_dev, "evdev_booster_dev");
  393. evbst_dev->release = NULL;
  394. err = device_register(evbst_dev);
  395. if (err)
  396. pr_err(ITAG" evdev device register failed\n");
  397. #if defined(CONFIG_SEC_INPUT_BOOSTER_HANDLER)
  398. err = init_input_handler();
  399. if (err)
  400. pr_err(ITAG" input handler fail\n");
  401. #endif
  402. return 0;
  403. }
  404. static void __exit ev_boost_exit(void)
  405. {
  406. ib_notifier_unregister(&ib_event_notifier);
  407. device_unregister(evbst_dev);
  408. kfree(evbst_dev);
  409. input_booster_exit();
  410. }
  411. late_initcall(ev_boost_init);
  412. module_exit(ev_boost_exit);
  413. MODULE_LICENSE("GPL");
  414. MODULE_AUTHOR("hongc.shim");
  415. MODULE_DESCRIPTION("EVDEV Booster in kernel");
  416. #endif //--CONFIG_SEC_INPUT_BOOSTER