wake_gesture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. #include "wake_gesture.h"
  2. static struct wag_context *wag_context_obj;
  3. static struct wag_init_info *wake_gesture_init = { 0 }; /* modified */
  4. static void wag_early_suspend(struct early_suspend *h);
  5. static void wag_late_resume(struct early_suspend *h);
  6. static int resume_enable_status;
  7. static struct wake_lock wag_lock;
  8. static void notify_wag_timeout(unsigned long);
  9. static struct wag_context *wag_context_alloc_object(void)
  10. {
  11. struct wag_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  12. WAG_LOG("wag_context_alloc_object++++\n");
  13. if (!obj) {
  14. WAG_ERR("Alloc wag object error!\n");
  15. return NULL;
  16. }
  17. atomic_set(&obj->wake, 0);
  18. mutex_init(&obj->wag_op_mutex);
  19. WAG_LOG("wag_context_alloc_object----\n");
  20. return obj;
  21. }
  22. static void notify_wag_timeout(unsigned long data)
  23. {
  24. wake_unlock(&wag_lock);
  25. }
  26. int wag_notify(void)
  27. {
  28. int err = 0;
  29. int value = 0;
  30. struct wag_context *cxt = NULL;
  31. cxt = wag_context_obj;
  32. WAG_LOG("wag_notify++++\n");
  33. value = 1;
  34. input_report_rel(cxt->idev, EVENT_TYPE_WAG_VALUE, value);
  35. input_sync(cxt->idev);
  36. wake_lock(&wag_lock);
  37. mod_timer(&cxt->notify_timer, jiffies + HZ / 5);
  38. return err;
  39. }
  40. static int wag_real_enable(int enable)
  41. {
  42. int err = 0;
  43. struct wag_context *cxt = NULL;
  44. cxt = wag_context_obj;
  45. if (WAG_RESUME == enable)
  46. enable = resume_enable_status;
  47. if (1 == enable) {
  48. resume_enable_status = 1;
  49. if (atomic_read(&(wag_context_obj->early_suspend))) /* not allow to enable under suspend */
  50. return 0;
  51. if (false == cxt->is_active_data) {
  52. err = cxt->wag_ctl.open_report_data(1);
  53. if (err) {
  54. err = cxt->wag_ctl.open_report_data(1);
  55. if (err) {
  56. err = cxt->wag_ctl.open_report_data(1);
  57. if (err) {
  58. WAG_ERR
  59. ("enable_wake_gesture enable(%d) err 3 timers = %d\n",
  60. enable, err);
  61. return err;
  62. }
  63. }
  64. }
  65. cxt->is_active_data = true;
  66. WAG_LOG("enable_wake_gesture real enable\n");
  67. }
  68. } else if ((0 == enable) || (WAG_SUSPEND == enable)) {
  69. if (0 == enable)
  70. resume_enable_status = 0;
  71. if (true == cxt->is_active_data) {
  72. err = cxt->wag_ctl.open_report_data(0);
  73. if (err)
  74. WAG_ERR("enable_wake_gestureenable(%d) err = %d\n", enable, err);
  75. cxt->is_active_data = false;
  76. WAG_LOG("enable_wake_gesture real disable\n");
  77. }
  78. }
  79. return err;
  80. }
  81. int wag_enable_nodata(int enable)
  82. {
  83. struct wag_context *cxt = NULL;
  84. cxt = wag_context_obj;
  85. if (NULL == cxt->wag_ctl.open_report_data) {
  86. WAG_ERR("wag_enable_nodata:wag ctl path is NULL\n");
  87. return -1;
  88. }
  89. if (1 == enable)
  90. cxt->is_active_nodata = true;
  91. if (0 == enable)
  92. cxt->is_active_nodata = false;
  93. wag_real_enable(enable);
  94. return 0;
  95. }
  96. static ssize_t wag_show_enable_nodata(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct wag_context *cxt = NULL;
  99. cxt = wag_context_obj;
  100. WAG_LOG("wag active: %d\n", cxt->is_active_nodata);
  101. return snprintf(buf, PAGE_SIZE, "%d\n", cxt->is_active_nodata);
  102. }
  103. static ssize_t wag_store_enable_nodata(struct device *dev, struct device_attribute *attr,
  104. const char *buf, size_t count)
  105. {
  106. struct wag_context *cxt = NULL;
  107. WAG_LOG("wag_store_enable nodata buf=%s\n", buf);
  108. mutex_lock(&wag_context_obj->wag_op_mutex);
  109. cxt = wag_context_obj;
  110. if (NULL == cxt->wag_ctl.open_report_data) {
  111. WAG_LOG("wag_ctl enable nodata NULL\n");
  112. mutex_unlock(&wag_context_obj->wag_op_mutex);
  113. return count;
  114. }
  115. if (!strncmp(buf, "1", 1))
  116. wag_enable_nodata(1);
  117. else if (!strncmp(buf, "0", 1))
  118. wag_enable_nodata(0);
  119. else
  120. WAG_ERR(" wag_store enable nodata cmd error !!\n");
  121. mutex_unlock(&wag_context_obj->wag_op_mutex);
  122. return count;
  123. }
  124. static ssize_t wag_store_active(struct device *dev, struct device_attribute *attr,
  125. const char *buf, size_t count)
  126. {
  127. struct wag_context *cxt = NULL;
  128. int res = 0;
  129. int en = 0;
  130. WAG_LOG("wag_store_active buf=%s\n", buf);
  131. mutex_lock(&wag_context_obj->wag_op_mutex);
  132. cxt = wag_context_obj;
  133. res = kstrtoint(buf, 10, &en);
  134. if (res != 0)
  135. WAG_LOG(" wag_store_active param error: res = %d\n", res);
  136. WAG_LOG(" wag_store_active en=%d\n", en);
  137. if (1 == en)
  138. wag_real_enable(1);
  139. else if (0 == en)
  140. wag_real_enable(0);
  141. else
  142. WAG_ERR(" wag_store_active error !!\n");
  143. mutex_unlock(&wag_context_obj->wag_op_mutex);
  144. WAG_LOG(" wag_store_active done\n");
  145. return count;
  146. }
  147. /*----------------------------------------------------------------------------*/
  148. static ssize_t wag_show_active(struct device *dev, struct device_attribute *attr, char *buf)
  149. {
  150. struct wag_context *cxt = NULL;
  151. cxt = wag_context_obj;
  152. WAG_LOG("wag active: %d\n", cxt->is_active_data);
  153. return snprintf(buf, PAGE_SIZE, "%d\n", cxt->is_active_data);
  154. }
  155. static ssize_t wag_store_delay(struct device *dev, struct device_attribute *attr, char *buf)
  156. {
  157. int len = 0;
  158. WAG_LOG(" not support now\n");
  159. return len;
  160. }
  161. static ssize_t wag_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  162. {
  163. int len = 0;
  164. WAG_LOG(" not support now\n");
  165. return len;
  166. }
  167. static ssize_t wag_store_batch(struct device *dev, struct device_attribute *attr,
  168. const char *buf, size_t count)
  169. {
  170. int len = 0;
  171. WAG_LOG(" not support now\n");
  172. return len;
  173. }
  174. static ssize_t wag_show_batch(struct device *dev, struct device_attribute *attr, char *buf)
  175. {
  176. int len = 0;
  177. WAG_LOG(" not support now\n");
  178. return len;
  179. }
  180. static ssize_t wag_store_flush(struct device *dev, struct device_attribute *attr,
  181. const char *buf, size_t count)
  182. {
  183. int len = 0;
  184. WAG_LOG(" not support now\n");
  185. return len;
  186. }
  187. static ssize_t wag_show_flush(struct device *dev, struct device_attribute *attr, char *buf)
  188. {
  189. int len = 0;
  190. WAG_LOG(" not support now\n");
  191. return len;
  192. }
  193. static ssize_t wag_show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  194. {
  195. char *devname = NULL;
  196. devname = dev_name(&wag_context_obj->idev->dev);
  197. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5); /* TODO: why +5? */
  198. }
  199. static int wake_gesture_remove(struct platform_device *pdev)
  200. {
  201. WAG_LOG("wake_gesture_remove\n");
  202. return 0;
  203. }
  204. static int wake_gesture_probe(struct platform_device *pdev)
  205. {
  206. WAG_LOG("wake_gesture_probe\n");
  207. return 0;
  208. }
  209. #ifdef CONFIG_OF
  210. static const struct of_device_id wake_gesture_of_match[] = {
  211. {.compatible = "mediatek,wake_gesture",},
  212. {},
  213. };
  214. #endif
  215. static struct platform_driver wake_gesture_driver = {
  216. .probe = wake_gesture_probe,
  217. .remove = wake_gesture_remove,
  218. .driver = {
  219. .name = "wake_gesture",
  220. #ifdef CONFIG_OF
  221. .of_match_table = wake_gesture_of_match,
  222. #endif
  223. }
  224. };
  225. static int wag_real_driver_init(void)
  226. {
  227. int err = 0;
  228. WAG_LOG(" wag_real_driver_init +\n");
  229. if (0 != wake_gesture_init) {
  230. WAG_LOG(" wag try to init driver %s\n", wake_gesture_init->name);
  231. err = wake_gesture_init->init();
  232. if (0 == err)
  233. WAG_LOG(" wag real driver %s probe ok\n", wake_gesture_init->name);
  234. }
  235. wake_lock_init(&wag_lock, WAKE_LOCK_SUSPEND, "wag wakelock");
  236. init_timer(&wag_context_obj->notify_timer);
  237. wag_context_obj->notify_timer.expires = HZ / 5; /* 200 ms */
  238. wag_context_obj->notify_timer.function = notify_wag_timeout;
  239. wag_context_obj->notify_timer.data = (unsigned long)wag_context_obj;
  240. return err;
  241. }
  242. int wag_driver_add(struct wag_init_info *obj)
  243. {
  244. int err = 0;
  245. WAG_FUN();
  246. WAG_LOG("register wake_gesture driver for the first time\n");
  247. if (platform_driver_register(&wake_gesture_driver))
  248. WAG_ERR("failed to register gensor driver already exist\n");
  249. if (NULL == wake_gesture_init) {
  250. obj->platform_diver_addr = &wake_gesture_driver;
  251. wake_gesture_init = obj;
  252. }
  253. if (NULL == wake_gesture_init) {
  254. WAG_ERR("WAG driver add err\n");
  255. err = -1;
  256. }
  257. return err;
  258. } EXPORT_SYMBOL_GPL(wag_driver_add);
  259. static int wag_misc_init(struct wag_context *cxt)
  260. {
  261. int err = 0;
  262. /* kernel-3.10\include\linux\Miscdevice.h */
  263. /* use MISC_DYNAMIC_MINOR exceed 64 */
  264. cxt->mdev.minor = M_WAG_MISC_MINOR;
  265. cxt->mdev.name = WAG_MISC_DEV_NAME;
  266. err = misc_register(&cxt->mdev);
  267. if (err)
  268. WAG_ERR("unable to register wag misc device!!\n");
  269. return err;
  270. }
  271. static void wag_input_destroy(struct wag_context *cxt)
  272. {
  273. struct input_dev *dev = cxt->idev;
  274. input_unregister_device(dev);
  275. input_free_device(dev);
  276. }
  277. static int wag_input_init(struct wag_context *cxt)
  278. {
  279. struct input_dev *dev;
  280. int err = 0;
  281. dev = input_allocate_device();
  282. if (NULL == dev)
  283. return -ENOMEM;
  284. dev->name = WAG_INPUTDEV_NAME;
  285. input_set_capability(dev, EV_REL, EVENT_TYPE_WAG_VALUE);
  286. input_set_drvdata(dev, cxt);
  287. set_bit(EV_REL, dev->evbit);
  288. err = input_register_device(dev);
  289. if (err < 0) {
  290. input_free_device(dev);
  291. return err;
  292. }
  293. cxt->idev = dev;
  294. return 0;
  295. }
  296. DEVICE_ATTR(wagenablenodata, S_IWUSR | S_IRUGO, wag_show_enable_nodata, wag_store_enable_nodata);
  297. DEVICE_ATTR(wagactive, S_IWUSR | S_IRUGO, wag_show_active, wag_store_active);
  298. DEVICE_ATTR(wagdelay, S_IWUSR | S_IRUGO, wag_show_delay, wag_store_delay);
  299. DEVICE_ATTR(wagbatch, S_IWUSR | S_IRUGO, wag_show_batch, wag_store_batch);
  300. DEVICE_ATTR(wagflush, S_IWUSR | S_IRUGO, wag_show_flush, wag_store_flush);
  301. DEVICE_ATTR(wagdevnum, S_IWUSR | S_IRUGO, wag_show_devnum, NULL);
  302. static struct attribute *wag_attributes[] = {
  303. &dev_attr_wagenablenodata.attr,
  304. &dev_attr_wagactive.attr,
  305. &dev_attr_wagdelay.attr,
  306. &dev_attr_wagbatch.attr,
  307. &dev_attr_wagflush.attr,
  308. &dev_attr_wagdevnum.attr,
  309. NULL
  310. };
  311. static struct attribute_group wag_attribute_group = {
  312. .attrs = wag_attributes
  313. };
  314. int wag_register_data_path(struct wag_data_path *data)
  315. {
  316. struct wag_context *cxt = NULL;
  317. cxt = wag_context_obj;
  318. cxt->wag_data.get_data = data->get_data;
  319. if (NULL == cxt->wag_data.get_data) {
  320. WAG_LOG("wag register data path fail\n");
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. int wag_register_control_path(struct wag_control_path *ctl)
  326. {
  327. struct wag_context *cxt = NULL;
  328. int err = 0;
  329. cxt = wag_context_obj;
  330. /* cxt->wag_ctl.enable = ctl->enable; */
  331. /* cxt->wag_ctl.enable_nodata = ctl->enable_nodata; */
  332. cxt->wag_ctl.open_report_data = ctl->open_report_data;
  333. if (NULL == cxt->wag_ctl.open_report_data) {
  334. WAG_LOG("wag register control path fail\n");
  335. return -1;
  336. }
  337. /* add misc dev for sensor hal control cmd */
  338. err = wag_misc_init(wag_context_obj);
  339. if (err) {
  340. WAG_ERR("unable to register wag misc device!!\n");
  341. return -2;
  342. }
  343. err = sysfs_create_group(&wag_context_obj->mdev.this_device->kobj, &wag_attribute_group);
  344. if (err < 0) {
  345. WAG_ERR("unable to create wag attribute file\n");
  346. return -3;
  347. }
  348. kobject_uevent(&wag_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  349. return 0;
  350. }
  351. static int wag_probe(struct platform_device *pdev)
  352. {
  353. int err;
  354. WAG_LOG("+++++++++++++wag_probe!!\n");
  355. wag_context_obj = wag_context_alloc_object();
  356. if (!wag_context_obj) {
  357. err = -ENOMEM;
  358. WAG_ERR("unable to allocate devobj!\n");
  359. goto exit_alloc_data_failed;
  360. }
  361. /* init real wag driver */
  362. err = wag_real_driver_init();
  363. if (err) {
  364. WAG_ERR("wag real driver init fail\n");
  365. goto real_driver_init_fail;
  366. }
  367. /* init input dev */
  368. err = wag_input_init(wag_context_obj);
  369. if (err) {
  370. WAG_ERR("unable to register wag input device!\n");
  371. goto exit_alloc_input_dev_failed;
  372. }
  373. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  374. atomic_set(&(wag_context_obj->early_suspend), 0);
  375. wag_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1,
  376. wag_context_obj->early_drv.suspend = wag_early_suspend,
  377. wag_context_obj->early_drv.resume = wag_late_resume,
  378. register_early_suspend(&wag_context_obj->early_drv);
  379. #endif /* #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND) */
  380. WAG_LOG("----wag_probe OK !!\n");
  381. return 0;
  382. if (err) {
  383. WAG_ERR("sysfs node creation error\n");
  384. wag_input_destroy(wag_context_obj);
  385. }
  386. real_driver_init_fail:
  387. exit_alloc_input_dev_failed:
  388. kfree(wag_context_obj);
  389. exit_alloc_data_failed:
  390. WAG_LOG("----wag_probe fail !!!\n");
  391. return err;
  392. }
  393. static int wag_remove(struct platform_device *pdev)
  394. {
  395. int err = 0;
  396. WAG_FUN(f);
  397. input_unregister_device(wag_context_obj->idev);
  398. sysfs_remove_group(&wag_context_obj->idev->dev.kobj, &wag_attribute_group);
  399. err = misc_deregister(&wag_context_obj->mdev);
  400. if (err)
  401. WAG_ERR("misc_deregister fail: %d\n", err);
  402. kfree(wag_context_obj);
  403. return 0;
  404. }
  405. static void wag_early_suspend(struct early_suspend *h)
  406. {
  407. atomic_set(&(wag_context_obj->early_suspend), 1);
  408. if (!atomic_read(&wag_context_obj->wake)) /* not wake up, disable in early suspend */
  409. wag_real_enable(WAG_SUSPEND);
  410. WAG_LOG(" wag_early_suspend ok------->hwm_obj->early_suspend=%d\n",
  411. atomic_read(&(wag_context_obj->early_suspend)));
  412. }
  413. /*----------------------------------------------------------------------------*/
  414. static void wag_late_resume(struct early_suspend *h)
  415. {
  416. atomic_set(&(wag_context_obj->early_suspend), 0);
  417. if (!atomic_read(&wag_context_obj->wake) && resume_enable_status)
  418. wag_real_enable(WAG_RESUME);
  419. WAG_LOG(" wag_late_resume ok------->hwm_obj->early_suspend=%d\n",
  420. atomic_read(&(wag_context_obj->early_suspend)));
  421. }
  422. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  423. static int wag_suspend(struct platform_device *dev, pm_message_t state)
  424. {
  425. atomic_set(&(wag_context_obj->suspend), 1);
  426. if (!atomic_read(&wag_context_obj->wake)) /* not wake up, disable in early suspend */
  427. wag_real_enable(WAG_SUSPEND);
  428. WAG_LOG(" wag_suspend ok------->hwm_obj->suspend=%d\n",
  429. atomic_read(&(wag_context_obj->suspend)));
  430. return 0;
  431. }
  432. /*----------------------------------------------------------------------------*/
  433. static int wag_resume(struct platform_device *dev)
  434. {
  435. atomic_set(&(wag_context_obj->suspend), 0);
  436. if (!atomic_read(&wag_context_obj->wake) && resume_enable_status)
  437. wag_real_enable(WAG_RESUME);
  438. WAG_LOG(" wag_resume ok------->hwm_obj->suspend=%d\n",
  439. atomic_read(&(wag_context_obj->suspend)));
  440. return 0;
  441. }
  442. #endif /* #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND) */
  443. #ifdef CONFIG_OF
  444. static const struct of_device_id m_wag_pl_of_match[] = {
  445. {.compatible = "mediatek,m_wag_pl",},
  446. {},
  447. };
  448. #endif
  449. static struct platform_driver wag_driver = {
  450. .probe = wag_probe,
  451. .remove = wag_remove,
  452. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  453. .suspend = wag_suspend,
  454. .resume = wag_resume,
  455. #endif
  456. .driver = {
  457. .name = WAG_PL_DEV_NAME,
  458. #ifdef CONFIG_OF
  459. .of_match_table = m_wag_pl_of_match,
  460. #endif
  461. }
  462. };
  463. static int __init wag_init(void)
  464. {
  465. WAG_FUN();
  466. if (platform_driver_register(&wag_driver)) {
  467. WAG_ERR("failed to register wag driver\n");
  468. return -ENODEV;
  469. }
  470. return 0;
  471. }
  472. static void __exit wag_exit(void)
  473. {
  474. platform_driver_unregister(&wag_driver);
  475. platform_driver_unregister(&wake_gesture_driver);
  476. }
  477. late_initcall(wag_init);
  478. /* module_init(wag_init); */
  479. /* module_exit(wag_exit); */
  480. MODULE_LICENSE("GPL");
  481. MODULE_DESCRIPTION("WAG device driver");
  482. MODULE_AUTHOR("Mediatek");