glance_gesture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "glance_gesture.h"
  2. static struct glg_context *glg_context_obj;
  3. static struct glg_init_info *glance_gesture_init = { 0 }; /* modified */
  4. static void glg_early_suspend(struct early_suspend *h);
  5. static void glg_late_resume(struct early_suspend *h);
  6. static int resume_enable_status;
  7. static struct wake_lock glg_lock;
  8. static void notify_glg_timeout(unsigned long);
  9. static struct glg_context *glg_context_alloc_object(void)
  10. {
  11. struct glg_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  12. GLG_LOG("glg_context_alloc_object++++\n");
  13. if (!obj) {
  14. GLG_ERR("Alloc glg object error!\n");
  15. return NULL;
  16. }
  17. atomic_set(&obj->wake, 0);
  18. mutex_init(&obj->glg_op_mutex);
  19. GLG_LOG("glg_context_alloc_object----\n");
  20. return obj;
  21. }
  22. static void notify_glg_timeout(unsigned long data)
  23. {
  24. wake_unlock(&glg_lock);
  25. }
  26. int glg_notify(void)
  27. {
  28. int err = 0;
  29. int value = 0;
  30. struct glg_context *cxt = NULL;
  31. cxt = glg_context_obj;
  32. GLG_LOG("glg_notify++++\n");
  33. value = 1;
  34. input_report_rel(cxt->idev, EVENT_TYPE_GLG_VALUE, value);
  35. input_sync(cxt->idev);
  36. wake_lock(&glg_lock);
  37. mod_timer(&cxt->notify_timer, jiffies + HZ / 5);
  38. return err;
  39. }
  40. static int glg_real_enable(int enable)
  41. {
  42. int err = 0;
  43. struct glg_context *cxt = NULL;
  44. cxt = glg_context_obj;
  45. if (GLG_RESUME == enable)
  46. enable = resume_enable_status;
  47. if (1 == enable) {
  48. resume_enable_status = 1;
  49. if (atomic_read(&(glg_context_obj->early_suspend))) /* not allow to enable under suspend */
  50. return 0;
  51. if (false == cxt->is_active_data) {
  52. err = cxt->glg_ctl.open_report_data(1);
  53. if (err) {
  54. err = cxt->glg_ctl.open_report_data(1);
  55. if (err) {
  56. err = cxt->glg_ctl.open_report_data(1);
  57. if (err) {
  58. GLG_ERR
  59. ("enable_glance_gesture enable(%d) err 3 timers = %d\n",
  60. enable, err);
  61. return err;
  62. }
  63. }
  64. }
  65. cxt->is_active_data = true;
  66. GLG_LOG("enable_glance_gesture real enable\n");
  67. }
  68. } else if ((0 == enable) || (GLG_SUSPEND == enable)) {
  69. if (0 == enable)
  70. resume_enable_status = 0;
  71. if (true == cxt->is_active_data) {
  72. err = cxt->glg_ctl.open_report_data(0);
  73. if (err)
  74. GLG_ERR("enable_glance_gestureenable(%d) err = %d\n", enable, err);
  75. cxt->is_active_data = false;
  76. GLG_LOG("enable_glance_gesture real disable\n");
  77. }
  78. }
  79. return err;
  80. }
  81. int glg_enable_nodata(int enable)
  82. {
  83. struct glg_context *cxt = NULL;
  84. cxt = glg_context_obj;
  85. if (NULL == cxt->glg_ctl.open_report_data) {
  86. GLG_ERR("glg_enable_nodata:glg 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. glg_real_enable(enable);
  94. return 0;
  95. }
  96. static ssize_t glg_show_enable_nodata(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct glg_context *cxt = NULL;
  99. cxt = glg_context_obj;
  100. GLG_LOG("glg active: %d\n", cxt->is_active_nodata);
  101. return snprintf(buf, PAGE_SIZE, "%d\n", cxt->is_active_nodata);
  102. }
  103. static ssize_t glg_store_enable_nodata(struct device *dev, struct device_attribute *attr,
  104. const char *buf, size_t count)
  105. {
  106. struct glg_context *cxt = NULL;
  107. GLG_LOG("glg_store_enable nodata buf=%s\n", buf);
  108. mutex_lock(&glg_context_obj->glg_op_mutex);
  109. cxt = glg_context_obj;
  110. if (NULL == cxt->glg_ctl.open_report_data) {
  111. GLG_LOG("glg_ctl enable nodata NULL\n");
  112. mutex_unlock(&glg_context_obj->glg_op_mutex);
  113. return count;
  114. }
  115. if (!strncmp(buf, "1", 1))
  116. glg_enable_nodata(1);
  117. else if (!strncmp(buf, "0", 1))
  118. glg_enable_nodata(0);
  119. else
  120. GLG_ERR(" glg_store enable nodata cmd error !!\n");
  121. mutex_unlock(&glg_context_obj->glg_op_mutex);
  122. return count;
  123. }
  124. static ssize_t glg_store_active(struct device *dev, struct device_attribute *attr,
  125. const char *buf, size_t count)
  126. {
  127. struct glg_context *cxt = NULL;
  128. int res = 0;
  129. int en = 0;
  130. GLG_LOG("glg_store_active buf=%s\n", buf);
  131. mutex_lock(&glg_context_obj->glg_op_mutex);
  132. cxt = glg_context_obj;
  133. res = kstrtoint(buf, 10, &en);
  134. if (res != 0)
  135. GLG_LOG(" glg_store_active param error: res = %d\n", res);
  136. GLG_LOG(" glg_store_active en=%d\n", en);
  137. if (1 == en)
  138. glg_real_enable(1);
  139. else if (0 == en)
  140. glg_real_enable(0);
  141. else
  142. GLG_ERR(" glg_store_active error !!\n");
  143. mutex_unlock(&glg_context_obj->glg_op_mutex);
  144. GLG_LOG(" glg_store_active done\n");
  145. return count;
  146. }
  147. /*----------------------------------------------------------------------------*/
  148. static ssize_t glg_show_active(struct device *dev, struct device_attribute *attr, char *buf)
  149. {
  150. struct glg_context *cxt = NULL;
  151. cxt = glg_context_obj;
  152. GLG_LOG("glg active: %d\n", cxt->is_active_data);
  153. return snprintf(buf, PAGE_SIZE, "%d\n", cxt->is_active_data);
  154. }
  155. static ssize_t glg_store_delay(struct device *dev, struct device_attribute *attr, char *buf)
  156. {
  157. int len = 0;
  158. GLG_LOG(" not support now\n");
  159. return len;
  160. }
  161. static ssize_t glg_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  162. {
  163. int len = 0;
  164. GLG_LOG(" not support now\n");
  165. return len;
  166. }
  167. static ssize_t glg_store_batch(struct device *dev, struct device_attribute *attr,
  168. const char *buf, size_t count)
  169. {
  170. int len = 0;
  171. GLG_LOG(" not support now\n");
  172. return len;
  173. }
  174. static ssize_t glg_show_batch(struct device *dev, struct device_attribute *attr, char *buf)
  175. {
  176. int len = 0;
  177. GLG_LOG(" not support now\n");
  178. return len;
  179. }
  180. static ssize_t glg_store_flush(struct device *dev, struct device_attribute *attr,
  181. const char *buf, size_t count)
  182. {
  183. int len = 0;
  184. GLG_LOG(" not support now\n");
  185. return len;
  186. }
  187. static ssize_t glg_show_flush(struct device *dev, struct device_attribute *attr, char *buf)
  188. {
  189. int len = 0;
  190. GLG_LOG(" not support now\n");
  191. return len;
  192. }
  193. static ssize_t glg_show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  194. {
  195. char *devname = NULL;
  196. devname = dev_name(&glg_context_obj->idev->dev);
  197. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5); /* TODO: why +5? */
  198. }
  199. static int glance_gesture_remove(struct platform_device *pdev)
  200. {
  201. GLG_LOG("glance_gesture_remove\n");
  202. return 0;
  203. }
  204. static int glance_gesture_probe(struct platform_device *pdev)
  205. {
  206. GLG_LOG("glance_gesture_probe\n");
  207. return 0;
  208. }
  209. #ifdef CONFIG_OF
  210. static const struct of_device_id glance_gesture_of_match[] = {
  211. {.compatible = "mediatek,glance_gesture",},
  212. {},
  213. };
  214. #endif
  215. static struct platform_driver glance_gesture_driver = {
  216. .probe = glance_gesture_probe,
  217. .remove = glance_gesture_remove,
  218. .driver = {
  219. .name = "glance_gesture",
  220. #ifdef CONFIG_OF
  221. .of_match_table = glance_gesture_of_match,
  222. #endif
  223. }
  224. };
  225. static int glg_real_driver_init(void)
  226. {
  227. int err = 0;
  228. GLG_LOG(" glg_real_driver_init +\n");
  229. if (0 != glance_gesture_init) {
  230. GLG_LOG(" glg try to init driver %s\n", glance_gesture_init->name);
  231. err = glance_gesture_init->init();
  232. if (0 == err)
  233. GLG_LOG(" glg real driver %s probe ok\n", glance_gesture_init->name);
  234. }
  235. wake_lock_init(&glg_lock, WAKE_LOCK_SUSPEND, "glg wakelock");
  236. init_timer(&glg_context_obj->notify_timer);
  237. glg_context_obj->notify_timer.expires = HZ / 5; /* 200 ms */
  238. glg_context_obj->notify_timer.function = notify_glg_timeout;
  239. glg_context_obj->notify_timer.data = (unsigned long)glg_context_obj;
  240. return err;
  241. }
  242. int glg_driver_add(struct glg_init_info *obj)
  243. {
  244. int err = 0;
  245. GLG_FUN();
  246. GLG_LOG("register glance_gesture driver for the first time\n");
  247. if (platform_driver_register(&glance_gesture_driver))
  248. GLG_ERR("failed to register gensor driver already exist\n");
  249. if (NULL == glance_gesture_init) {
  250. obj->platform_diver_addr = &glance_gesture_driver;
  251. glance_gesture_init = obj;
  252. }
  253. if (NULL == glance_gesture_init) {
  254. GLG_ERR("GLG driver add err\n");
  255. err = -1;
  256. }
  257. return err;
  258. } EXPORT_SYMBOL_GPL(glg_driver_add);
  259. static int glg_misc_init(struct glg_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_GLG_MISC_MINOR;
  265. cxt->mdev.name = GLG_MISC_DEV_NAME;
  266. err = misc_register(&cxt->mdev);
  267. if (err)
  268. GLG_ERR("unable to register glg misc device!!\n");
  269. return err;
  270. }
  271. static void glg_input_destroy(struct glg_context *cxt)
  272. {
  273. struct input_dev *dev = cxt->idev;
  274. input_unregister_device(dev);
  275. input_free_device(dev);
  276. }
  277. static int glg_input_init(struct glg_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 = GLG_INPUTDEV_NAME;
  285. input_set_capability(dev, EV_REL, EVENT_TYPE_GLG_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(glgenablenodata, S_IWUSR | S_IRUGO, glg_show_enable_nodata, glg_store_enable_nodata);
  297. DEVICE_ATTR(glgactive, S_IWUSR | S_IRUGO, glg_show_active, glg_store_active);
  298. DEVICE_ATTR(glgdelay, S_IWUSR | S_IRUGO, glg_show_delay, glg_store_delay);
  299. DEVICE_ATTR(glgbatch, S_IWUSR | S_IRUGO, glg_show_batch, glg_store_batch);
  300. DEVICE_ATTR(glgflush, S_IWUSR | S_IRUGO, glg_show_flush, glg_store_flush);
  301. DEVICE_ATTR(glgdevnum, S_IWUSR | S_IRUGO, glg_show_devnum, NULL);
  302. static struct attribute *glg_attributes[] = {
  303. &dev_attr_glgenablenodata.attr,
  304. &dev_attr_glgactive.attr,
  305. &dev_attr_glgdelay.attr,
  306. &dev_attr_glgbatch.attr,
  307. &dev_attr_glgflush.attr,
  308. &dev_attr_glgdevnum.attr,
  309. NULL
  310. };
  311. static struct attribute_group glg_attribute_group = {
  312. .attrs = glg_attributes
  313. };
  314. int glg_register_data_path(struct glg_data_path *data)
  315. {
  316. struct glg_context *cxt = NULL;
  317. cxt = glg_context_obj;
  318. cxt->glg_data.get_data = data->get_data;
  319. if (NULL == cxt->glg_data.get_data) {
  320. GLG_LOG("glg register data path fail\n");
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. int glg_register_control_path(struct glg_control_path *ctl)
  326. {
  327. struct glg_context *cxt = NULL;
  328. int err = 0;
  329. cxt = glg_context_obj;
  330. /* cxt->glg_ctl.enable = ctl->enable; */
  331. /* cxt->glg_ctl.enable_nodata = ctl->enable_nodata; */
  332. cxt->glg_ctl.open_report_data = ctl->open_report_data;
  333. if (NULL == cxt->glg_ctl.open_report_data) {
  334. GLG_LOG("glg register control path fail\n");
  335. return -1;
  336. }
  337. /* add misc dev for sensor hal control cmd */
  338. err = glg_misc_init(glg_context_obj);
  339. if (err) {
  340. GLG_ERR("unable to register glg misc device!!\n");
  341. return -2;
  342. }
  343. err = sysfs_create_group(&glg_context_obj->mdev.this_device->kobj, &glg_attribute_group);
  344. if (err < 0) {
  345. GLG_ERR("unable to create glg attribute file\n");
  346. return -3;
  347. }
  348. kobject_uevent(&glg_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  349. return 0;
  350. }
  351. static int glg_probe(struct platform_device *pdev)
  352. {
  353. int err;
  354. GLG_LOG("+++++++++++++glg_probe!!\n");
  355. glg_context_obj = glg_context_alloc_object();
  356. if (!glg_context_obj) {
  357. err = -ENOMEM;
  358. GLG_ERR("unable to allocate devobj!\n");
  359. goto exit_alloc_data_failed;
  360. }
  361. /* init real glg driver */
  362. err = glg_real_driver_init();
  363. if (err) {
  364. GLG_ERR("glg real driver init fail\n");
  365. goto real_driver_init_fail;
  366. }
  367. /* init input dev */
  368. err = glg_input_init(glg_context_obj);
  369. if (err) {
  370. GLG_ERR("unable to register glg input device!\n");
  371. goto exit_alloc_input_dev_failed;
  372. }
  373. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  374. atomic_set(&(glg_context_obj->early_suspend), 0);
  375. glg_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1,
  376. glg_context_obj->early_drv.suspend = glg_early_suspend,
  377. glg_context_obj->early_drv.resume = glg_late_resume,
  378. register_early_suspend(&glg_context_obj->early_drv);
  379. #endif /* #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND) */
  380. GLG_LOG("----glg_probe OK !!\n");
  381. return 0;
  382. if (err) {
  383. GLG_ERR("sysfs node creation error\n");
  384. glg_input_destroy(glg_context_obj);
  385. }
  386. real_driver_init_fail:
  387. exit_alloc_input_dev_failed:
  388. kfree(glg_context_obj);
  389. exit_alloc_data_failed:
  390. GLG_LOG("----glg_probe fail !!!\n");
  391. return err;
  392. }
  393. static int glg_remove(struct platform_device *pdev)
  394. {
  395. int err = 0;
  396. GLG_FUN();
  397. input_unregister_device(glg_context_obj->idev);
  398. sysfs_remove_group(&glg_context_obj->idev->dev.kobj, &glg_attribute_group);
  399. err = misc_deregister(&glg_context_obj->mdev);
  400. if (err)
  401. GLG_ERR("misc_deregister fail: %d\n", err);
  402. kfree(glg_context_obj);
  403. return 0;
  404. }
  405. static void glg_early_suspend(struct early_suspend *h)
  406. {
  407. atomic_set(&(glg_context_obj->early_suspend), 1);
  408. if (!atomic_read(&glg_context_obj->wake)) /* not wake up, disable in early suspend */
  409. glg_real_enable(GLG_SUSPEND);
  410. GLG_LOG(" glg_early_suspend ok------->hwm_obj->early_suspend=%d\n",
  411. atomic_read(&(glg_context_obj->early_suspend)));
  412. }
  413. /*----------------------------------------------------------------------------*/
  414. static void glg_late_resume(struct early_suspend *h)
  415. {
  416. atomic_set(&(glg_context_obj->early_suspend), 0);
  417. if (!atomic_read(&glg_context_obj->wake) && resume_enable_status)
  418. glg_real_enable(GLG_RESUME);
  419. GLG_LOG(" glg_late_resume ok------->hwm_obj->early_suspend=%d\n",
  420. atomic_read(&(glg_context_obj->early_suspend)));
  421. }
  422. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  423. static int glg_suspend(struct platform_device *dev, pm_message_t state)
  424. {
  425. atomic_set(&(glg_context_obj->suspend), 1);
  426. if (!atomic_read(&glg_context_obj->wake))
  427. glg_real_enable(GLG_SUSPEND);
  428. GLG_LOG(" glg_suspend ok------->hwm_obj->suspend=%d\n",
  429. atomic_read(&(glg_context_obj->suspend)));
  430. return 0;
  431. }
  432. /*----------------------------------------------------------------------------*/
  433. static int glg_resume(struct platform_device *dev)
  434. {
  435. atomic_set(&(glg_context_obj->suspend), 0);
  436. if (!atomic_read(&glg_context_obj->wake) && resume_enable_status)
  437. glg_real_enable(GLG_RESUME);
  438. GLG_LOG(" glg_resume ok------->hwm_obj->suspend=%d\n",
  439. atomic_read(&(glg_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_glg_pl_of_match[] = {
  445. {.compatible = "mediatek,m_glg_pl",},
  446. {},
  447. };
  448. #endif
  449. static struct platform_driver glg_driver = {
  450. .probe = glg_probe,
  451. .remove = glg_remove,
  452. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  453. .suspend = glg_suspend,
  454. .resume = glg_resume,
  455. #endif
  456. .driver = {
  457. .name = GLG_PL_DEV_NAME,
  458. #ifdef CONFIG_OF
  459. .of_match_table = m_glg_pl_of_match,
  460. #endif
  461. }
  462. };
  463. static int __init glg_init(void)
  464. {
  465. GLG_FUN();
  466. if (platform_driver_register(&glg_driver)) {
  467. GLG_ERR("failed to register glg driver\n");
  468. return -ENODEV;
  469. }
  470. return 0;
  471. }
  472. static void __exit glg_exit(void)
  473. {
  474. platform_driver_unregister(&glg_driver);
  475. platform_driver_unregister(&glance_gesture_driver);
  476. }
  477. late_initcall(glg_init);
  478. /* module_init(glg_init); */
  479. /* module_exit(glg_exit); */
  480. MODULE_LICENSE("GPL");
  481. MODULE_DESCRIPTION("GLG device driver");
  482. MODULE_AUTHOR("Mediatek");