in_pocket.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #include "in_pocket.h"
  2. #define USE_EARLY_SUSPEND
  3. static struct inpk_context *inpk_context_obj;
  4. static struct inpk_init_info *in_pocket_init = { 0 }; /* modified */
  5. static void inpk_early_suspend(struct early_suspend *h);
  6. static void inpk_late_resume(struct early_suspend *h);
  7. static int resume_enable_status;
  8. static struct inpk_context *inpk_context_alloc_object(void)
  9. {
  10. struct inpk_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  11. INPK_LOG("inpk_context_alloc_object++++\n");
  12. if (!obj) {
  13. INPK_ERR("Alloc inpk object error!\n");
  14. return NULL;
  15. }
  16. atomic_set(&obj->wake, 0);
  17. mutex_init(&obj->inpk_op_mutex);
  18. INPK_LOG("inpk_context_alloc_object----\n");
  19. return obj;
  20. }
  21. int inpk_notify(void)
  22. {
  23. int err = 0;
  24. int value = 0;
  25. struct inpk_context *cxt = NULL;
  26. cxt = inpk_context_obj;
  27. INPK_LOG("inpk_notify++++\n");
  28. value = 1;
  29. input_report_rel(cxt->idev, EVENT_TYPE_INPK_VALUE, value);
  30. input_sync(cxt->idev);
  31. return err;
  32. }
  33. static int inpk_real_enable(int enable)
  34. {
  35. int err = 0;
  36. struct inpk_context *cxt = NULL;
  37. cxt = inpk_context_obj;
  38. if (INPK_RESUME == enable)
  39. enable = resume_enable_status;
  40. if (1 == enable) {
  41. resume_enable_status = 1;
  42. if (atomic_read(&(inpk_context_obj->early_suspend))) /* not allow to enable under suspend */
  43. return 0;
  44. if (false == cxt->is_active_data) {
  45. err = cxt->inpk_ctl.open_report_data(1);
  46. if (err) {
  47. err = cxt->inpk_ctl.open_report_data(1);
  48. if (err) {
  49. err = cxt->inpk_ctl.open_report_data(1);
  50. if (err) {
  51. INPK_ERR
  52. ("enable_in_pocket enable(%d) err 3 timers = %d\n",
  53. enable, err);
  54. return err;
  55. }
  56. }
  57. }
  58. cxt->is_active_data = true;
  59. INPK_LOG("enable_in_pocket real enable\n");
  60. }
  61. } else if ((0 == enable) || (INPK_SUSPEND == enable)) {
  62. if (0 == enable)
  63. resume_enable_status = 0;
  64. if (true == cxt->is_active_data) {
  65. err = cxt->inpk_ctl.open_report_data(0);
  66. if (err)
  67. INPK_ERR("enable_in_pocketenable(%d) err = %d\n", enable, err);
  68. cxt->is_active_data = false;
  69. INPK_LOG("enable_in_pocket real disable\n");
  70. }
  71. }
  72. return err;
  73. }
  74. int inpk_enable_nodata(int enable)
  75. {
  76. struct inpk_context *cxt = NULL;
  77. cxt = inpk_context_obj;
  78. if (NULL == cxt->inpk_ctl.open_report_data) {
  79. INPK_ERR("inpk_enable_nodata:inpk ctl path is NULL\n");
  80. return -1;
  81. }
  82. if (1 == enable)
  83. cxt->is_active_nodata = true;
  84. if (0 == enable)
  85. cxt->is_active_nodata = false;
  86. inpk_real_enable(enable);
  87. return 0;
  88. }
  89. static ssize_t inpk_show_enable_nodata(struct device *dev, struct device_attribute *attr, char *buf)
  90. {
  91. struct inpk_context *cxt = NULL;
  92. cxt = inpk_context_obj;
  93. INPK_LOG("inpk active: %d\n", cxt->is_active_nodata);
  94. return snprintf(buf, PAGE_SIZE, "%d\n", cxt->is_active_nodata);
  95. }
  96. static ssize_t inpk_store_enable_nodata(struct device *dev, struct device_attribute *attr,
  97. const char *buf, size_t count)
  98. {
  99. struct inpk_context *cxt = NULL;
  100. INPK_LOG("inpk_store_enable nodata buf=%s\n", buf);
  101. mutex_lock(&inpk_context_obj->inpk_op_mutex);
  102. cxt = inpk_context_obj;
  103. if (NULL == cxt->inpk_ctl.open_report_data) {
  104. INPK_LOG("inpk_ctl enable nodata NULL\n");
  105. mutex_unlock(&inpk_context_obj->inpk_op_mutex);
  106. return count;
  107. }
  108. if (!strncmp(buf, "1", 1))
  109. inpk_enable_nodata(1);
  110. else if (!strncmp(buf, "0", 1))
  111. inpk_enable_nodata(0);
  112. else
  113. INPK_ERR(" inpk_store enable nodata cmd error !!\n");
  114. mutex_unlock(&inpk_context_obj->inpk_op_mutex);
  115. return count;
  116. }
  117. static ssize_t inpk_store_active(struct device *dev, struct device_attribute *attr,
  118. const char *buf, size_t count)
  119. {
  120. struct inpk_context *cxt = NULL;
  121. int res = 0;
  122. int en = 0;
  123. INPK_LOG("inpk_store_active buf=%s\n", buf);
  124. mutex_lock(&inpk_context_obj->inpk_op_mutex);
  125. cxt = inpk_context_obj;
  126. res = kstrtoint(buf, 10, &en);
  127. if (res != 0)
  128. INPK_LOG(" inpk_store_active param error: res = %d\n", res);
  129. INPK_LOG(" inpk_store_active en=%d\n", en);
  130. if (1 == en)
  131. inpk_real_enable(1);
  132. else if (0 == en)
  133. inpk_real_enable(0);
  134. else
  135. INPK_ERR(" inpk_store_active error !!\n");
  136. mutex_unlock(&inpk_context_obj->inpk_op_mutex);
  137. INPK_LOG(" inpk_store_active done\n");
  138. return count;
  139. }
  140. /*----------------------------------------------------------------------------*/
  141. static ssize_t inpk_show_active(struct device *dev, struct device_attribute *attr, char *buf)
  142. {
  143. struct inpk_context *cxt = NULL;
  144. cxt = inpk_context_obj;
  145. INPK_LOG("inpk active: %d\n", cxt->is_active_data);
  146. return snprintf(buf, PAGE_SIZE, "%d\n", cxt->is_active_data);
  147. }
  148. static ssize_t inpk_store_delay(struct device *dev, struct device_attribute *attr, char *buf)
  149. {
  150. int len = 0;
  151. INPK_LOG(" not support now\n");
  152. return len;
  153. }
  154. static ssize_t inpk_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  155. {
  156. int len = 0;
  157. INPK_LOG(" not support now\n");
  158. return len;
  159. }
  160. static ssize_t inpk_store_batch(struct device *dev, struct device_attribute *attr,
  161. const char *buf, size_t count)
  162. {
  163. int len = 0;
  164. INPK_LOG(" not support now\n");
  165. return len;
  166. }
  167. static ssize_t inpk_show_batch(struct device *dev, struct device_attribute *attr, char *buf)
  168. {
  169. int len = 0;
  170. INPK_LOG(" not support now\n");
  171. return len;
  172. }
  173. static ssize_t inpk_store_flush(struct device *dev, struct device_attribute *attr,
  174. const char *buf, size_t count)
  175. {
  176. int len = 0;
  177. INPK_LOG(" not support now\n");
  178. return len;
  179. }
  180. static ssize_t inpk_show_flush(struct device *dev, struct device_attribute *attr, char *buf)
  181. {
  182. int len = 0;
  183. INPK_LOG(" not support now\n");
  184. return len;
  185. }
  186. static ssize_t inpk_show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  187. {
  188. char *devname = NULL;
  189. devname = dev_name(&inpk_context_obj->idev->dev);
  190. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5); /* TODO: why +5? */
  191. }
  192. static int in_pocket_remove(struct platform_device *pdev)
  193. {
  194. INPK_LOG("in_pocket_remove\n");
  195. return 0;
  196. }
  197. static int in_pocket_probe(struct platform_device *pdev)
  198. {
  199. INPK_LOG("in_pocket_probe\n");
  200. return 0;
  201. }
  202. #ifdef CONFIG_OF
  203. static const struct of_device_id in_pocket_of_match[] = {
  204. {.compatible = "mediatek,in_pocket",},
  205. {},
  206. };
  207. #endif
  208. static struct platform_driver in_pocket_driver = {
  209. .probe = in_pocket_probe,
  210. .remove = in_pocket_remove,
  211. .driver = {
  212. .name = "in_pocket",
  213. #ifdef CONFIG_OF
  214. .of_match_table = in_pocket_of_match,
  215. #endif
  216. }
  217. };
  218. static int inpk_real_driver_init(void)
  219. {
  220. int err = 0;
  221. INPK_LOG(" inpk_real_driver_init +\n");
  222. if (0 != in_pocket_init) {
  223. INPK_LOG(" inpk try to init driver %s\n", in_pocket_init->name);
  224. err = in_pocket_init->init();
  225. if (0 == err)
  226. INPK_LOG(" inpk real driver %s probe ok\n", in_pocket_init->name);
  227. }
  228. return err;
  229. }
  230. int inpk_driver_add(struct inpk_init_info *obj)
  231. {
  232. int err = 0;
  233. INPK_FUN();
  234. INPK_LOG("register in_pocket driver for the first time\n");
  235. if (platform_driver_register(&in_pocket_driver))
  236. INPK_ERR("failed to register gensor driver already exist\n");
  237. if (NULL == in_pocket_init) {
  238. obj->platform_diver_addr = &in_pocket_driver;
  239. in_pocket_init = obj;
  240. }
  241. if (NULL == in_pocket_init) {
  242. INPK_ERR("INPK driver add err\n");
  243. err = -1;
  244. }
  245. return err;
  246. } EXPORT_SYMBOL_GPL(inpk_driver_add);
  247. static int inpk_misc_init(struct inpk_context *cxt)
  248. {
  249. int err = 0;
  250. /* kernel-3.10\include\linux\Miscdevice.h */
  251. /* use MISC_DYNAMIC_MINOR exceed 64 */
  252. cxt->mdev.minor = M_INPK_MISC_MINOR;
  253. cxt->mdev.name = INPK_MISC_DEV_NAME;
  254. err = misc_register(&cxt->mdev);
  255. if (err)
  256. INPK_ERR("unable to register inpk misc device!!\n");
  257. return err;
  258. }
  259. static void inpk_input_destroy(struct inpk_context *cxt)
  260. {
  261. struct input_dev *dev = cxt->idev;
  262. input_unregister_device(dev);
  263. input_free_device(dev);
  264. }
  265. static int inpk_input_init(struct inpk_context *cxt)
  266. {
  267. struct input_dev *dev;
  268. int err = 0;
  269. dev = input_allocate_device();
  270. if (NULL == dev)
  271. return -ENOMEM;
  272. dev->name = INPK_INPUTDEV_NAME;
  273. input_set_capability(dev, EV_REL, EVENT_TYPE_INPK_VALUE);
  274. input_set_drvdata(dev, cxt);
  275. set_bit(EV_REL, dev->evbit);
  276. err = input_register_device(dev);
  277. if (err < 0) {
  278. input_free_device(dev);
  279. return err;
  280. }
  281. cxt->idev = dev;
  282. return 0;
  283. }
  284. DEVICE_ATTR(inpkenablenodata, S_IWUSR | S_IRUGO, inpk_show_enable_nodata, inpk_store_enable_nodata);
  285. DEVICE_ATTR(inpkactive, S_IWUSR | S_IRUGO, inpk_show_active, inpk_store_active);
  286. DEVICE_ATTR(inpkdelay, S_IWUSR | S_IRUGO, inpk_show_delay, inpk_store_delay);
  287. DEVICE_ATTR(inpkbatch, S_IWUSR | S_IRUGO, inpk_show_batch, inpk_store_batch);
  288. DEVICE_ATTR(inpkflush, S_IWUSR | S_IRUGO, inpk_show_flush, inpk_store_flush);
  289. DEVICE_ATTR(inpkdevnum, S_IWUSR | S_IRUGO, inpk_show_devnum, NULL);
  290. static struct attribute *inpk_attributes[] = {
  291. &dev_attr_inpkenablenodata.attr,
  292. &dev_attr_inpkactive.attr,
  293. &dev_attr_inpkdelay.attr,
  294. &dev_attr_inpkbatch.attr,
  295. &dev_attr_inpkflush.attr,
  296. &dev_attr_inpkdevnum.attr,
  297. NULL
  298. };
  299. static struct attribute_group inpk_attribute_group = {
  300. .attrs = inpk_attributes
  301. };
  302. int inpk_register_data_path(struct inpk_data_path *data)
  303. {
  304. struct inpk_context *cxt = NULL;
  305. cxt = inpk_context_obj;
  306. cxt->inpk_data.get_data = data->get_data;
  307. if (NULL == cxt->inpk_data.get_data) {
  308. INPK_LOG("inpk register data path fail\n");
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. int inpk_register_control_path(struct inpk_control_path *ctl)
  314. {
  315. struct inpk_context *cxt = NULL;
  316. int err = 0;
  317. cxt = inpk_context_obj;
  318. /* cxt->inpk_ctl.enable = ctl->enable; */
  319. /* cxt->inpk_ctl.enable_nodata = ctl->enable_nodata; */
  320. cxt->inpk_ctl.open_report_data = ctl->open_report_data;
  321. if (NULL == cxt->inpk_ctl.open_report_data) {
  322. INPK_LOG("inpk register control path fail\n");
  323. return -1;
  324. }
  325. /* add misc dev for sensor hal control cmd */
  326. err = inpk_misc_init(inpk_context_obj);
  327. if (err) {
  328. INPK_ERR("unable to register inpk misc device!!\n");
  329. return -2;
  330. }
  331. err = sysfs_create_group(&inpk_context_obj->mdev.this_device->kobj, &inpk_attribute_group);
  332. if (err < 0) {
  333. INPK_ERR("unable to create inpk attribute file\n");
  334. return -3;
  335. }
  336. kobject_uevent(&inpk_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  337. return 0;
  338. }
  339. static int inpk_probe(struct platform_device *pdev)
  340. {
  341. int err;
  342. INPK_LOG("+++++++++++++inpk_probe!!\n");
  343. inpk_context_obj = inpk_context_alloc_object();
  344. if (!inpk_context_obj) {
  345. err = -ENOMEM;
  346. INPK_ERR("unable to allocate devobj!\n");
  347. goto exit_alloc_data_failed;
  348. }
  349. /* init real inpk driver */
  350. err = inpk_real_driver_init();
  351. if (err) {
  352. INPK_ERR("inpk real driver init fail\n");
  353. goto real_driver_init_fail;
  354. }
  355. /* init input dev */
  356. err = inpk_input_init(inpk_context_obj);
  357. if (err) {
  358. INPK_ERR("unable to register inpk input device!\n");
  359. goto exit_alloc_input_dev_failed;
  360. }
  361. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND) && defined(USE_EARLY_SUSPEND)
  362. atomic_set(&(inpk_context_obj->early_suspend), 0);
  363. inpk_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1,
  364. inpk_context_obj->early_drv.suspend = inpk_early_suspend,
  365. inpk_context_obj->early_drv.resume = inpk_late_resume,
  366. register_early_suspend(&inpk_context_obj->early_drv);
  367. #endif /* #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND) */
  368. INPK_LOG("----inpk_probe OK !!\n");
  369. return 0;
  370. if (err) {
  371. INPK_ERR("sysfs node creation error\n");
  372. inpk_input_destroy(inpk_context_obj);
  373. }
  374. real_driver_init_fail:
  375. exit_alloc_input_dev_failed:
  376. kfree(inpk_context_obj);
  377. exit_alloc_data_failed:
  378. INPK_LOG("----inpk_probe fail !!!\n");
  379. return err;
  380. }
  381. static int inpk_remove(struct platform_device *pdev)
  382. {
  383. int err = 0;
  384. INPK_FUN(f);
  385. input_unregister_device(inpk_context_obj->idev);
  386. sysfs_remove_group(&inpk_context_obj->idev->dev.kobj, &inpk_attribute_group);
  387. err = misc_deregister(&inpk_context_obj->mdev);
  388. if (err)
  389. INPK_ERR("misc_deregister fail: %d\n", err);
  390. kfree(inpk_context_obj);
  391. return 0;
  392. }
  393. static void inpk_early_suspend(struct early_suspend *h)
  394. {
  395. atomic_set(&(inpk_context_obj->early_suspend), 1);
  396. INPK_LOG(" inpk_early_suspend ok------->hwm_obj->early_suspend=%d !\n",
  397. atomic_read(&(inpk_context_obj->early_suspend)));
  398. }
  399. /*----------------------------------------------------------------------------*/
  400. static void inpk_late_resume(struct early_suspend *h)
  401. {
  402. atomic_set(&(inpk_context_obj->early_suspend), 0);
  403. INPK_LOG(" inpk_late_resume ok------->hwm_obj->early_suspend=%d !\n",
  404. atomic_read(&(inpk_context_obj->early_suspend)));
  405. }
  406. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  407. static int inpk_suspend(struct platform_device *dev, pm_message_t state)
  408. {
  409. atomic_set(&(inpk_context_obj->suspend), 1);
  410. INPK_LOG(" inpk_suspend ok------->hwm_obj->suspend=%d\n",
  411. atomic_read(&(inpk_context_obj->suspend)));
  412. return 0;
  413. }
  414. /*----------------------------------------------------------------------------*/
  415. static int inpk_resume(struct platform_device *dev)
  416. {
  417. atomic_set(&(inpk_context_obj->suspend), 0);
  418. INPK_LOG(" inpk_resume ok------->hwm_obj->suspend=%d\n",
  419. atomic_read(&(inpk_context_obj->suspend)));
  420. return 0;
  421. }
  422. #endif /* #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND) */
  423. #ifdef CONFIG_OF
  424. static const struct of_device_id m_inpk_pl_of_match[] = {
  425. {.compatible = "mediatek,m_inpk_pl",},
  426. {},
  427. };
  428. #endif
  429. static struct platform_driver inpk_driver = {
  430. .probe = inpk_probe,
  431. .remove = inpk_remove,
  432. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  433. .suspend = inpk_suspend,
  434. .resume = inpk_resume,
  435. #endif
  436. .driver = {
  437. .name = INPK_PL_DEV_NAME,
  438. #ifdef CONFIG_OF
  439. .of_match_table = m_inpk_pl_of_match,
  440. #endif
  441. }
  442. };
  443. static int __init inpk_init(void)
  444. {
  445. INPK_FUN();
  446. if (platform_driver_register(&inpk_driver)) {
  447. INPK_ERR("failed to register inpk driver\n");
  448. return -ENODEV;
  449. }
  450. return 0;
  451. }
  452. static void __exit inpk_exit(void)
  453. {
  454. platform_driver_unregister(&inpk_driver);
  455. platform_driver_unregister(&in_pocket_driver);
  456. }
  457. late_initcall(inpk_init);
  458. /* module_init(inpk_init); */
  459. /* module_exit(inpk_exit); */
  460. MODULE_LICENSE("GPL");
  461. MODULE_DESCRIPTION("INPK device driver");
  462. MODULE_AUTHOR("Mediatek");