Advertisement
Guest User

tap2unlock.c

a guest
Jun 14th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.79 KB | None | 0 0
  1. /*
  2.  * drivers/input/touchscreen/tap2unlock.c
  3.  *
  4.  *
  5.  * Copyright (c) 2013, Dennis Rassmann <showp1984@gmail.com>
  6.  * Copyright (c) 2014, goutamniwas <goutamniwas@gmail.com>
  7.  * Copyright (c) 2015, Swapnil Solanki <swapnil133609@gmail.com>
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful, but WITHOUT
  14.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  16.  * more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License along
  19.  * with this program; if not, write to the Free Software Foundation, Inc.,
  20.  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  21.  */
  22.  
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/delay.h>
  27. #include <linux/init.h>
  28. #include <linux/err.h>
  29. #include <linux/input/tap2unlock.h>
  30. #include <linux/slab.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/input.h>
  33. #include <linux/hrtimer.h>
  34. #include <asm-generic/cputime.h>
  35.  
  36. #define WAKE_HOOKS_DEFINED
  37.  
  38. #ifndef WAKE_HOOKS_DEFINED
  39. #ifndef CONFIG_HAS_EARLYSUSPEND
  40. #include <linux/lcd_notify.h>
  41. #else
  42. #include <linux/earlysuspend.h>
  43. #endif
  44. #endif
  45.  
  46. /* uncomment since no touchscreen defines android touch, do that here */
  47. //#define ANDROID_TOUCH_DECLARED
  48.  
  49. /* if Sweep2Wake is compiled it will already have taken care of this */
  50. #ifdef CONFIG_TOUCHSCREEN_SWEEP2WAKE
  51. #define ANDROID_TOUCH_DECLARED
  52. #endif
  53.  
  54. /* Version, author, desc, etc */
  55. #define DRIVER_AUTHOR "goutamniwas <goutamniwas@gmail.com>"
  56. #define DRIVER_DESCRIPTION "tap2unlock for almost any device"
  57. #define DRIVER_VERSION "3.0"
  58. #define LOGTAG "[tap2unlock]: "
  59.  
  60. MODULE_AUTHOR(DRIVER_AUTHOR);
  61. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  62. MODULE_VERSION(DRIVER_VERSION);
  63. MODULE_LICENSE("GPLv2");
  64.  
  65. /* Tuneables */
  66. #define t2u_DEBUG       1
  67.  
  68.  
  69. #define t2u_PWRKEY_DUR      60
  70. #define t2u_TIME        600 //gap(in ms) allowed between 'each' touch (for 4 letter pattern - 4x600 =2400 ms)
  71. #define VERTICAL_SCREEN_MIDWAY  427 // Your device's vertical resolution / 2
  72. #define HORIZONTAL_SCREEN_MIDWAY  240 // Your device's horizontal resolution / 2
  73.  
  74. /* Resources */
  75. int t2u_switch = 0;
  76. int static t2u_pattern[4] = {1,2,3,4};
  77. bool t2u_scr_suspended = false, pass = false;
  78. static cputime64_t tap_time_pre = 0;
  79. static int touch_x = 0, touch_y = 0, touch_nr = 0;
  80. static bool touch_x_called = false, touch_y_called = false, touch_cnt = false;
  81. #ifndef WAKE_HOOKS_DEFINED
  82. #ifndef CONFIG_HAS_EARLYSUSPEND
  83. static struct notifier_block s2w_lcd_notif;
  84. #endif
  85. #endif
  86. static struct input_dev * tap2unlock_pwrdev;
  87. static DEFINE_MUTEX(pwrkeyworklock);
  88. static struct workqueue_struct *t2u_input_wq;
  89. static struct work_struct t2u_input_work;
  90. bool t2u_allow = false,incall_active = false,touch_isactive = false,t2u_duplicate_allow = false;
  91.  
  92. /* Read cmdline for t2u */
  93. static int __init read_t2u_cmdline(char *t2u)
  94. {
  95.     if (strcmp(t2u, "1") == 0) {
  96.         pr_info("[cmdline_t2u]: tap2unlock enabled. | t2u='%s'\n", t2u);
  97.         t2u_switch = 1;
  98.     } else if (strcmp(t2u, "0") == 0) {
  99.         pr_info("[cmdline_t2u]: tap2unlock disabled. | t2u='%s'\n", t2u);
  100.         t2u_switch = 0;
  101.     } else {
  102.         pr_info("[cmdline_t2u]: No valid input found. Going with default: | t2u='%u'\n", t2u_switch);
  103.     }
  104.     return 1;
  105. }
  106. __setup("t2u=", read_t2u_cmdline);
  107.  
  108.  
  109. /* PowerKey work func */
  110. static void tap2unlock_presspwr(struct work_struct * tap2unlock_presspwr_work) {
  111.     if (!mutex_trylock(&pwrkeyworklock))
  112.                 return;
  113.     input_event(tap2unlock_pwrdev, EV_KEY, KEY_POWER, 1);
  114.     input_event(tap2unlock_pwrdev, EV_SYN, 0, 0);
  115.     msleep(t2u_PWRKEY_DUR);
  116.     input_event(tap2unlock_pwrdev, EV_KEY, KEY_POWER, 0);
  117.     input_event(tap2unlock_pwrdev, EV_SYN, 0, 0);
  118.     msleep(t2u_PWRKEY_DUR);
  119.         mutex_unlock(&pwrkeyworklock);
  120.     return;
  121. }
  122. static DECLARE_WORK(tap2unlock_presspwr_work, tap2unlock_presspwr);
  123.  
  124. /* PowerKey trigger */
  125. static void tap2unlock_pwrtrigger(void) {
  126.     schedule_work(&tap2unlock_presspwr_work);
  127.         return;
  128. }
  129.  
  130. /* unsigned - calculate the region where the touch has occured */
  131. static unsigned int calc_feather(int coord, int prev_coord) {
  132.  
  133.  
  134.     if (prev_coord < VERTICAL_SCREEN_MIDWAY)
  135.         if (coord < HORIZONTAL_SCREEN_MIDWAY)
  136.             return 1;
  137.         else
  138.             return 2;
  139.     else
  140.         if (coord < HORIZONTAL_SCREEN_MIDWAY)
  141.             return 3;
  142.         else
  143.             return 4;
  144.  
  145. }
  146.  
  147.  
  148. /* tap2unlock main function */
  149. static void detect_tap2unlock(int x, int y)
  150. {
  151. #if t2u_DEBUG
  152.         pr_info(LOGTAG"x,y(%4d,%4d)",x, y);
  153. #endif
  154.     if ((t2u_switch > 0) && (touch_cnt)) {
  155.  
  156.         touch_cnt = false;
  157.         //check if time gap between two touches is less than t2u_TIME
  158.         if(touch_nr > 0 && (ktime_to_ms(ktime_get()) - tap_time_pre) > t2u_TIME)
  159.             touch_nr = 0;
  160.  
  161.         if (touch_nr < 3) {
  162.  
  163.             if (calc_feather(x, y) == t2u_pattern[touch_nr]) {
  164.                 tap_time_pre = ktime_to_ms(ktime_get());
  165.                 touch_nr++;
  166.             }
  167.             else
  168.                 touch_nr = 0;
  169.  
  170.         } else  {//when touch_nr ==3 i.e on the 4 th knock , it wont be allowed any further than that
  171.  
  172.              if (calc_feather(x, y) == t2u_pattern[3]) {
  173.                 t2u_allow = true;
  174.                 t2u_duplicate_allow = true;
  175.                 pr_info(LOGTAG"T2U : ON\n");
  176.                 touch_nr = 0;
  177.                 tap2unlock_pwrtrigger();
  178.             }
  179.             else
  180.                 touch_nr = 0;
  181.         }
  182.  
  183.     }
  184. }
  185.  
  186. static void t2u_input_callback(struct work_struct *unused) {
  187.  
  188.     detect_tap2unlock(touch_x, touch_y);
  189.  
  190.     return;
  191. }
  192.  
  193. static void t2u_input_event(struct input_handle *handle, unsigned int type,
  194.                 unsigned int code, int value) {
  195. #if t2u_DEBUG
  196.     pr_info("tap2unlock: code: %s|%u, val: %i\n",
  197.         ((code==ABS_MT_POSITION_X) ? "X" :
  198.         (code==ABS_MT_POSITION_Y) ? "Y" :
  199.         ((code==ABS_MT_TRACKING_ID)||
  200.             (code==330)) ? "ID" : "undef"), code, value);
  201. #endif
  202.     if (!t2u_scr_suspended)
  203.         return;
  204.  
  205.     if (code == ABS_MT_SLOT) {
  206.         touch_nr = 0;
  207.         return;
  208.     }
  209.  
  210.     if ((code == ABS_MT_TRACKING_ID && value == -1) ||
  211.         (code == 330 && value == 0)) {
  212.         touch_cnt = true;
  213.         return;
  214.     }
  215.  
  216.     if (code == ABS_MT_POSITION_X) {
  217.         touch_x = value;
  218.         touch_x_called = true;
  219.     }
  220.  
  221.     if (code == ABS_MT_POSITION_Y) {
  222.         touch_y = value;
  223.         touch_y_called = true;
  224.     }
  225.  
  226.     if (touch_x_called || touch_y_called) {
  227.         touch_x_called = false;
  228.         touch_y_called = false;
  229.         queue_work_on(0, t2u_input_wq, &t2u_input_work);
  230.     }
  231. }
  232.  
  233. static int input_dev_filter(struct input_dev *dev) {
  234.     if (strstr(dev->name, "touch") ||
  235.         strstr(dev->name, "mtk-tpd")) {
  236.         return 0;
  237.     } else {
  238.         return 1;
  239.     }
  240. }
  241.  
  242. static int t2u_input_connect(struct input_handler *handler,
  243.                 struct input_dev *dev, const struct input_device_id *id) {
  244.     struct input_handle *handle;
  245.     int error;
  246.  
  247.     if (input_dev_filter(dev))
  248.         return -ENODEV;
  249.  
  250.     handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  251.     if (!handle)
  252.         return -ENOMEM;
  253.  
  254.     handle->dev = dev;
  255.     handle->handler = handler;
  256.     handle->name = "t2u";
  257.  
  258.     error = input_register_handle(handle);
  259.     if (error)
  260.         goto err2;
  261.  
  262.     error = input_open_device(handle);
  263.     if (error)
  264.         goto err1;
  265.  
  266.     return 0;
  267. err1:
  268.     input_unregister_handle(handle);
  269. err2:
  270.     kfree(handle);
  271.     return error;
  272. }
  273.  
  274. static void t2u_input_disconnect(struct input_handle *handle) {
  275.     input_close_device(handle);
  276.     input_unregister_handle(handle);
  277.     kfree(handle);
  278. }
  279.  
  280. static const struct input_device_id t2u_ids[] = {
  281.     { .driver_info = 1 },
  282.     { },
  283. };
  284.  
  285. static struct input_handler t2u_input_handler = {
  286.     .event      = t2u_input_event,
  287.     .connect    = t2u_input_connect,
  288.     .disconnect = t2u_input_disconnect,
  289.     .name       = "t2u_inputreq",
  290.     .id_table   = t2u_ids,
  291. };
  292.  
  293. #ifndef WAKE_HOOKS_DEFINED
  294. #ifndef CONFIG_HAS_EARLYSUSPEND
  295. static int lcd_notifier_callback(struct notifier_block *this,
  296.                 unsigned long event, void *data)
  297. {
  298.     switch (event) {
  299.     case LCD_EVENT_ON_END:
  300.         t2u_scr_suspended = false;
  301.         break;
  302.     case LCD_EVENT_OFF_END:
  303.         t2u_scr_suspended = true;
  304.         break;
  305.     default:
  306.         break;
  307.     }
  308.  
  309.     return 0;
  310. }
  311. #else
  312. static void t2u_early_suspend(struct early_suspend *h) {
  313.     t2u_scr_suspended = true;
  314. }
  315.  
  316. static void t2u_late_resume(struct early_suspend *h) {
  317.     t2u_scr_suspended = false;
  318. }
  319.  
  320. static struct early_suspend t2u_early_suspend_handler = {
  321.     .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN,
  322.     .suspend = t2u_early_suspend,
  323.     .resume = t2u_late_resume,
  324. };
  325. #endif
  326. #endif
  327.  
  328. /*
  329.  * SYSFS stuff below here
  330.  */
  331. static ssize_t t2u_tap2unlock_show(struct device *dev,
  332.         struct device_attribute *attr, char *buf)
  333. {
  334.     size_t count = 0;
  335.  
  336.     count += sprintf(buf, "%d\n", t2u_switch);
  337.  
  338.     return count;
  339. }
  340.  
  341. static ssize_t t2u_tap2unlock_dump(struct device *dev,
  342.         struct device_attribute *attr, const char *buf, size_t count)
  343. {
  344.     if (buf[0] >= '0' && buf[0] <= '2' && buf[1] == '\n')
  345.                 if (t2u_switch != buf[0] - '0')
  346.                 t2u_switch = buf[0] - '0';
  347.  
  348.     return count;
  349. }
  350.  
  351. static DEVICE_ATTR(tap2unlock, (S_IWUSR|S_IRUGO),
  352.     t2u_tap2unlock_show, t2u_tap2unlock_dump);
  353.  
  354. static ssize_t t2u_pattern_show(struct device *dev,
  355.         struct device_attribute *attr, char *buf)
  356. {
  357.     size_t count = 0;
  358.  
  359.     count += sprintf(buf, "%d", pass);
  360.  
  361.     return count;
  362. }
  363.  
  364. static ssize_t t2u_pattern_dump(struct device *dev,
  365.         struct device_attribute *attr, const char *buf, size_t count)
  366. {
  367.     if (t2u_pattern[0] == buf[0] - '0' && t2u_pattern[1] == buf[1] - '0' &&
  368.                 t2u_pattern[2] == buf[2] - '0' && t2u_pattern[3] == buf[3] - '0' )
  369.     {
  370.         pass = true;
  371.  
  372.         t2u_pattern[0] = buf[4] - '0';
  373.         t2u_pattern[1] = buf[5] - '0';
  374.         t2u_pattern[2] = buf[6] - '0';
  375.         t2u_pattern[3] = buf[7] - '0';
  376.     }
  377.     else
  378.         pass = false;
  379.  
  380.     return count;
  381. }
  382.  
  383. static DEVICE_ATTR(tap2unlock_pattern, (S_IWUSR|S_IRUGO),
  384.     t2u_pattern_show, t2u_pattern_dump);
  385.  
  386. static ssize_t t2u_version_show(struct device *dev,
  387.         struct device_attribute *attr, char *buf)
  388. {
  389.     size_t count = 0;
  390.  
  391.     count += sprintf(buf, "%s\n", DRIVER_VERSION);
  392.  
  393.     return count;
  394. }
  395.  
  396. static ssize_t t2u_version_dump(struct device *dev,
  397.         struct device_attribute *attr, const char *buf, size_t count)
  398. {
  399.     return count;
  400. }
  401.  
  402. static DEVICE_ATTR(tap2unlock_version, (S_IWUSR|S_IRUGO),
  403.     t2u_version_show, t2u_version_dump);
  404.  
  405. static ssize_t t2u_allow_show(struct device *dev,
  406.         struct device_attribute *attr, char *buf)
  407. {
  408.     size_t count = 0;
  409.  
  410.     count += sprintf(buf, "%d\n", t2u_duplicate_allow);
  411.  
  412.     return count;
  413. }
  414.  
  415. static ssize_t t2u_allow_dump(struct device *dev,
  416.         struct device_attribute *attr, const char *buf, size_t count)
  417. {
  418.     if (t2u_pattern[0] == buf[0] - '0' && t2u_pattern[1] == buf[1] - '0' && t2u_pattern[2] == buf[2] - '0' && t2u_pattern[3] == buf[3] - '0')
  419.     {
  420.  
  421.  
  422.         if(buf[4] == '1') {
  423.  
  424.                 t2u_allow = true;
  425.                 incall_active = true;
  426.                 nyx_resume(h);
  427.                 //touch_isactive = true;
  428.  
  429.         }
  430.         else if (buf[4] == '0') {
  431.  
  432.                 t2u_allow = false;
  433.                 incall_active = false;
  434.                 nyx_suspend(h);
  435.                 //touch_isactive = false;
  436.  
  437.         }
  438.     }
  439.  
  440.     return count;
  441. }
  442.  
  443. static DEVICE_ATTR(t2u_allow, (S_IWUSR|S_IRUGO),
  444.     t2u_allow_show, t2u_allow_dump);
  445.  
  446. /*
  447.  * INIT / EXIT stuff below here
  448.  */
  449. #ifdef ANDROID_TOUCH_DECLARED
  450. extern struct kobject *android_touch_kobj;
  451. #else
  452. struct kobject *android_touch_kobj;
  453. EXPORT_SYMBOL_GPL(android_touch_kobj);
  454. #endif
  455. static int __init tap2unlock_init(void)
  456. {
  457.     int rc = 0;
  458.  
  459.     tap2unlock_pwrdev = input_allocate_device();
  460.     if (!tap2unlock_pwrdev) {
  461.         pr_err("Can't allocate suspend autotest power button\n");
  462.         goto err_alloc_dev;
  463.     }
  464.  
  465.     input_set_capability(tap2unlock_pwrdev, EV_KEY, KEY_POWER);
  466.     tap2unlock_pwrdev->name = "t2u_pwrkey";
  467.     tap2unlock_pwrdev->phys = "t2u_pwrkey/input0";
  468.  
  469.     rc = input_register_device(tap2unlock_pwrdev);
  470.     if (rc) {
  471.         pr_err("%s: input_register_device err=%d\n", __func__, rc);
  472.         goto err_input_dev;
  473.     }
  474.  
  475.     t2u_input_wq = create_workqueue("t2uiwq");
  476.     if (!t2u_input_wq) {
  477.         pr_err("%s: Failed to create t2uiwq workqueue\n", __func__);
  478.         return -EFAULT;
  479.     }
  480.     INIT_WORK(&t2u_input_work, t2u_input_callback);
  481.     rc = input_register_handler(&t2u_input_handler);
  482.     if (rc)
  483.         pr_err("%s: Failed to register t2u_input_handler\n", __func__);
  484.  
  485. #ifndef WAKE_HOOKS_DEFINED
  486. #ifndef CONFIG_HAS_EARLYSUSPEND
  487.     t2u_lcd_notif.notifier_call = lcd_notifier_callback;
  488.     if (lcd_register_client(&t2u_lcd_notif) != 0) {
  489.         pr_err("%s: Failed to register lcd callback\n", __func__);
  490.     }
  491. #else
  492.     register_early_suspend(&t2u_early_suspend_handler);
  493. #endif
  494. #endif
  495.  
  496. #ifndef ANDROID_TOUCH_DECLARED
  497.     android_touch_kobj = kobject_create_and_add("tap2unlock", NULL) ;
  498.     if (android_touch_kobj == NULL) {
  499.         pr_warn("%s: android_touch_kobj create_and_add failed\n", __func__);
  500.     }
  501. #endif
  502.     rc = sysfs_create_file(android_touch_kobj, &dev_attr_tap2unlock.attr);
  503.     if (rc) {
  504.         pr_warn("%s: sysfs_create_file failed for tap2unlock\n", __func__);
  505.     }
  506.     rc = sysfs_create_file(android_touch_kobj, &dev_attr_tap2unlock_pattern.attr);
  507.     if (rc) {
  508.         pr_warn("%s: sysfs_create_file failed for pattern\n", __func__);
  509.     }
  510.     rc = sysfs_create_file(android_touch_kobj, &dev_attr_tap2unlock_version.attr);
  511.     if (rc) {
  512.         pr_warn("%s: sysfs_create_file failed for tap2unlock_version\n", __func__);
  513.     }
  514.     rc = sysfs_create_file(android_touch_kobj, &dev_attr_t2u_allow.attr);
  515.     if (rc) {
  516.         pr_warn("%s: sysfs_create_file failed for t2u_allow\n", __func__);
  517.     }
  518.  
  519.  
  520. err_input_dev:
  521.     input_free_device(tap2unlock_pwrdev);
  522. err_alloc_dev:
  523.     pr_info(LOGTAG"%s done\n", __func__);
  524.  
  525.     return 0;
  526. }
  527.  
  528. static void __exit tap2unlock_exit(void)
  529. {
  530. #ifndef ANDROID_TOUCH_DECLARED
  531.     kobject_del(android_touch_kobj);
  532. #endif
  533. #ifndef WAKE_HOOKS_DEFINED
  534. #ifndef CONFIG_HAS_EARLYSUSPEND
  535.     lcd_unregister_client(&t2u_lcd_notif);
  536. #endif
  537. #endif
  538.     input_unregister_handler(&t2u_input_handler);
  539.     destroy_workqueue(t2u_input_wq);
  540.     input_unregister_device(tap2unlock_pwrdev);
  541.     input_free_device(tap2unlock_pwrdev);
  542.     return;
  543. }
  544.  
  545. module_init(tap2unlock_init);
  546. module_exit(tap2unlock_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement