synaptics_dsx_rmi_dev.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * Synaptics DSX touchscreen driver
  3. *
  4. * Copyright (C) 2012-2016 Synaptics Incorporated. All rights reserved.
  5. *
  6. * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
  7. * Copyright (C) 2012 Alexandra Chin <[email protected]>
  8. * Copyright (C) 2012 Scott Lin <[email protected]>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS," AND SYNAPTICS
  21. * EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING ANY
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  23. * AND ANY WARRANTIES OF NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS.
  24. * IN NO EVENT SHALL SYNAPTICS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION
  26. * WITH THE USE OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED
  27. * AND BASED ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  28. * NEGLIGENCE OR OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS ADVISED OF
  29. * THE POSSIBILITY OF SUCH DAMAGE. IF A TRIBUNAL OF COMPETENT JURISDICTION DOES
  30. * NOT PERMIT THE DISCLAIMER OF DIRECT DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS'
  31. * TOTAL CUMULATIVE LIABILITY TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S.
  32. * DOLLARS.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/slab.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/delay.h>
  39. #include <linux/input.h>
  40. #include <linux/signal.h>
  41. #include <linux/sched.h>
  42. #include <linux/gpio.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/cdev.h>
  45. #include <linux/sched/signal.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/input/synaptics_dsx.h>
  48. #include "synaptics_dsx_core.h"
  49. #define CHAR_DEVICE_NAME "rmi"
  50. #define DEVICE_CLASS_NAME "rmidev"
  51. #define SYSFS_FOLDER_NAME "rmidev"
  52. #define DEV_NUMBER 1
  53. #define REG_ADDR_LIMIT 0xFFFF
  54. #define RMIDEV_MAJOR_NUM 0
  55. static ssize_t rmidev_sysfs_data_show(struct file *data_file,
  56. struct kobject *kobj, struct bin_attribute *attributes,
  57. char *buf, loff_t pos, size_t count);
  58. static ssize_t rmidev_sysfs_data_store(struct file *data_file,
  59. struct kobject *kobj, struct bin_attribute *attributes,
  60. char *buf, loff_t pos, size_t count);
  61. static ssize_t rmidev_sysfs_open_store(struct device *dev,
  62. struct device_attribute *attr, const char *buf, size_t count);
  63. static ssize_t rmidev_sysfs_release_store(struct device *dev,
  64. struct device_attribute *attr, const char *buf, size_t count);
  65. static ssize_t rmidev_sysfs_attn_state_show(struct device *dev,
  66. struct device_attribute *attr, char *buf);
  67. static ssize_t rmidev_sysfs_pid_show(struct device *dev,
  68. struct device_attribute *attr, char *buf);
  69. static ssize_t rmidev_sysfs_pid_store(struct device *dev,
  70. struct device_attribute *attr, const char *buf, size_t count);
  71. static ssize_t rmidev_sysfs_term_store(struct device *dev,
  72. struct device_attribute *attr, const char *buf, size_t count);
  73. static ssize_t rmidev_sysfs_intr_mask_show(struct device *dev,
  74. struct device_attribute *attr, char *buf);
  75. static ssize_t rmidev_sysfs_intr_mask_store(struct device *dev,
  76. struct device_attribute *attr, const char *buf, size_t count);
  77. static ssize_t rmidev_sysfs_concurrent_show(struct device *dev,
  78. struct device_attribute *attr, char *buf);
  79. static ssize_t rmidev_sysfs_concurrent_store(struct device *dev,
  80. struct device_attribute *attr, const char *buf, size_t count);
  81. struct rmidev_handle {
  82. dev_t dev_no;
  83. pid_t pid;
  84. unsigned char intr_mask;
  85. unsigned char *tmpbuf;
  86. unsigned int tmpbuf_size;
  87. struct device dev;
  88. struct synaptics_rmi4_data *rmi4_data;
  89. struct kobject *sysfs_dir;
  90. struct kernel_siginfo interrupt_signal;
  91. struct kernel_siginfo terminate_signal;
  92. struct task_struct *task;
  93. void *data;
  94. bool concurrent;
  95. };
  96. struct rmidev_data {
  97. int ref_count;
  98. struct cdev main_dev;
  99. struct class *device_class;
  100. struct mutex file_mutex;
  101. struct rmidev_handle *rmi_dev;
  102. };
  103. static struct bin_attribute attr_data = {
  104. .attr = {
  105. .name = "data",
  106. .mode = 0664,
  107. },
  108. .size = 0,
  109. .read = rmidev_sysfs_data_show,
  110. .write = rmidev_sysfs_data_store,
  111. };
  112. static struct device_attribute attrs[] = {
  113. __ATTR(open, 0220,
  114. synaptics_rmi4_show_error,
  115. rmidev_sysfs_open_store),
  116. __ATTR(release, 0220,
  117. synaptics_rmi4_show_error,
  118. rmidev_sysfs_release_store),
  119. __ATTR(attn_state, 0444,
  120. rmidev_sysfs_attn_state_show,
  121. synaptics_rmi4_store_error),
  122. __ATTR(pid, 0664,
  123. rmidev_sysfs_pid_show,
  124. rmidev_sysfs_pid_store),
  125. __ATTR(term, 0220,
  126. synaptics_rmi4_show_error,
  127. rmidev_sysfs_term_store),
  128. __ATTR(intr_mask, 0664,
  129. rmidev_sysfs_intr_mask_show,
  130. rmidev_sysfs_intr_mask_store),
  131. __ATTR(concurrent, 0664,
  132. rmidev_sysfs_concurrent_show,
  133. rmidev_sysfs_concurrent_store),
  134. };
  135. static int rmidev_major_num = RMIDEV_MAJOR_NUM;
  136. static struct class *rmidev_device_class;
  137. static struct rmidev_handle *rmidev;
  138. DECLARE_COMPLETION(rmidev_remove_complete);
  139. static irqreturn_t rmidev_sysfs_irq(int irq, void *data)
  140. {
  141. struct synaptics_rmi4_data *rmi4_data = data;
  142. sysfs_notify(&rmi4_data->input_dev->dev.kobj,
  143. SYSFS_FOLDER_NAME, "attn_state");
  144. return IRQ_HANDLED;
  145. }
  146. static int rmidev_sysfs_irq_enable(struct synaptics_rmi4_data *rmi4_data,
  147. bool enable)
  148. {
  149. int retval = 0;
  150. unsigned char intr_status[MAX_INTR_REGISTERS];
  151. unsigned long irq_flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
  152. IRQF_ONESHOT;
  153. mutex_lock(&(rmi4_data->rmi4_irq_enable_mutex));
  154. if (enable) {
  155. if (rmi4_data->irq_enabled) {
  156. dev_err(rmi4_data->pdev->dev.parent,
  157. "%s: Interrupt already enabled\n",
  158. __func__);
  159. goto exit;
  160. }
  161. /* Clear interrupts first */
  162. retval = synaptics_rmi4_reg_read(rmi4_data,
  163. rmi4_data->f01_data_base_addr + 1,
  164. intr_status,
  165. rmi4_data->num_of_intr_regs);
  166. if (retval < 0)
  167. goto exit;
  168. retval = request_threaded_irq(rmi4_data->irq, NULL,
  169. rmidev_sysfs_irq, irq_flags,
  170. PLATFORM_DRIVER_NAME, rmi4_data);
  171. if (retval < 0) {
  172. dev_err(rmi4_data->pdev->dev.parent,
  173. "%s: Failed to create irq thread\n",
  174. __func__);
  175. goto exit;
  176. }
  177. rmi4_data->irq_enabled = true;
  178. } else {
  179. if (rmi4_data->irq_enabled) {
  180. disable_irq(rmi4_data->irq);
  181. free_irq(rmi4_data->irq, rmi4_data);
  182. rmi4_data->irq_enabled = false;
  183. }
  184. }
  185. exit:
  186. mutex_unlock(&(rmi4_data->rmi4_irq_enable_mutex));
  187. return retval;
  188. }
  189. static ssize_t rmidev_sysfs_data_show(struct file *data_file,
  190. struct kobject *kobj, struct bin_attribute *attributes,
  191. char *buf, loff_t pos, size_t count)
  192. {
  193. int retval;
  194. unsigned char intr_status = 0;
  195. unsigned int length = (unsigned int)count;
  196. unsigned short address = (unsigned short)pos;
  197. struct synaptics_rmi4_fn *fhandler;
  198. struct synaptics_rmi4_device_info *rmi;
  199. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  200. rmi = &(rmi4_data->rmi4_mod_info);
  201. if (length > (REG_ADDR_LIMIT - address)) {
  202. dev_err(rmi4_data->pdev->dev.parent,
  203. "%s: Out of register map limit\n",
  204. __func__);
  205. return -EINVAL;
  206. }
  207. if (length) {
  208. retval = synaptics_rmi4_reg_read(rmi4_data,
  209. address,
  210. (unsigned char *)buf,
  211. length);
  212. if (retval < 0) {
  213. dev_err(rmi4_data->pdev->dev.parent,
  214. "%s: Failed to read data\n",
  215. __func__);
  216. return retval;
  217. }
  218. } else {
  219. return -EINVAL;
  220. }
  221. if (!rmidev->concurrent)
  222. goto exit;
  223. if (address != rmi4_data->f01_data_base_addr)
  224. goto exit;
  225. if (length <= 1)
  226. goto exit;
  227. intr_status = buf[1];
  228. if (!list_empty(&rmi->support_fn_list)) {
  229. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  230. if (fhandler->num_of_data_sources) {
  231. if (fhandler->intr_mask & intr_status) {
  232. rmi4_data->report_touch(rmi4_data,
  233. fhandler);
  234. }
  235. }
  236. }
  237. }
  238. exit:
  239. return length;
  240. }
  241. static ssize_t rmidev_sysfs_data_store(struct file *data_file,
  242. struct kobject *kobj, struct bin_attribute *attributes,
  243. char *buf, loff_t pos, size_t count)
  244. {
  245. int retval;
  246. unsigned int length = (unsigned int)count;
  247. unsigned short address = (unsigned short)pos;
  248. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  249. if (length > (REG_ADDR_LIMIT - address)) {
  250. dev_err(rmi4_data->pdev->dev.parent,
  251. "%s: Out of register map limit\n",
  252. __func__);
  253. return -EINVAL;
  254. }
  255. if (length) {
  256. retval = synaptics_rmi4_reg_write(rmi4_data,
  257. address,
  258. (unsigned char *)buf,
  259. length);
  260. if (retval < 0) {
  261. dev_err(rmi4_data->pdev->dev.parent,
  262. "%s: Failed to write data\n",
  263. __func__);
  264. return retval;
  265. }
  266. } else {
  267. return -EINVAL;
  268. }
  269. return length;
  270. }
  271. static ssize_t rmidev_sysfs_open_store(struct device *dev,
  272. struct device_attribute *attr, const char *buf, size_t count)
  273. {
  274. unsigned int input;
  275. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  276. if (kstrtouint(buf, 10, &input) != 1)
  277. return -EINVAL;
  278. if (input != 1)
  279. return -EINVAL;
  280. if (rmi4_data->sensor_sleep) {
  281. dev_err(rmi4_data->pdev->dev.parent,
  282. "%s: Sensor sleeping\n",
  283. __func__);
  284. return -ENODEV;
  285. }
  286. rmi4_data->stay_awake = true;
  287. rmi4_data->irq_enable(rmi4_data, false, false);
  288. rmidev_sysfs_irq_enable(rmi4_data, true);
  289. dev_dbg(rmi4_data->pdev->dev.parent,
  290. "%s: Attention interrupt disabled\n",
  291. __func__);
  292. return count;
  293. }
  294. static ssize_t rmidev_sysfs_release_store(struct device *dev,
  295. struct device_attribute *attr, const char *buf, size_t count)
  296. {
  297. unsigned int input;
  298. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  299. if (kstrtouint(buf, 10, &input) != 1)
  300. return -EINVAL;
  301. if (input != 1)
  302. return -EINVAL;
  303. rmidev_sysfs_irq_enable(rmi4_data, false);
  304. rmi4_data->reset_device(rmi4_data, false);
  305. rmi4_data->stay_awake = false;
  306. return count;
  307. }
  308. static ssize_t rmidev_sysfs_attn_state_show(struct device *dev,
  309. struct device_attribute *attr, char *buf)
  310. {
  311. int attn_state;
  312. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  313. const struct synaptics_dsx_board_data *bdata =
  314. rmi4_data->hw_if->board_data;
  315. attn_state = gpio_get_value(bdata->irq_gpio);
  316. return snprintf(buf, PAGE_SIZE, "%u\n", attn_state);
  317. }
  318. static ssize_t rmidev_sysfs_pid_show(struct device *dev,
  319. struct device_attribute *attr, char *buf)
  320. {
  321. return snprintf(buf, PAGE_SIZE, "%u\n", rmidev->pid);
  322. }
  323. static ssize_t rmidev_sysfs_pid_store(struct device *dev,
  324. struct device_attribute *attr, const char *buf, size_t count)
  325. {
  326. unsigned int input;
  327. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  328. if (kstrtouint(buf, 10, &input) != 1)
  329. return -EINVAL;
  330. rmidev->pid = input;
  331. if (rmidev->pid) {
  332. rmidev->task = pid_task(find_vpid(rmidev->pid), PIDTYPE_PID);
  333. if (!rmidev->task) {
  334. dev_err(rmi4_data->pdev->dev.parent,
  335. "%s: Failed to locate PID of data logging tool\n",
  336. __func__);
  337. return -EINVAL;
  338. }
  339. }
  340. return count;
  341. }
  342. static ssize_t rmidev_sysfs_term_store(struct device *dev,
  343. struct device_attribute *attr, const char *buf, size_t count)
  344. {
  345. unsigned int input;
  346. if (kstrtouint(buf, 10, &input) != 1)
  347. return -EINVAL;
  348. if (input != 1)
  349. return -EINVAL;
  350. if (rmidev->pid)
  351. send_sig_info(SIGTERM, &rmidev->terminate_signal, rmidev->task);
  352. return count;
  353. }
  354. static ssize_t rmidev_sysfs_intr_mask_show(struct device *dev,
  355. struct device_attribute *attr, char *buf)
  356. {
  357. return snprintf(buf, PAGE_SIZE, "0x%02x\n", rmidev->intr_mask);
  358. }
  359. static ssize_t rmidev_sysfs_intr_mask_store(struct device *dev,
  360. struct device_attribute *attr, const char *buf, size_t count)
  361. {
  362. unsigned int input;
  363. if (kstrtouint(buf, 10, &input) != 1)
  364. return -EINVAL;
  365. rmidev->intr_mask = (unsigned char)input;
  366. return count;
  367. }
  368. static ssize_t rmidev_sysfs_concurrent_show(struct device *dev,
  369. struct device_attribute *attr, char *buf)
  370. {
  371. return snprintf(buf, PAGE_SIZE, "%d\n", rmidev->concurrent);
  372. }
  373. static ssize_t rmidev_sysfs_concurrent_store(struct device *dev,
  374. struct device_attribute *attr, const char *buf, size_t count)
  375. {
  376. unsigned int input;
  377. if (kstrtouint(buf, 10, &input) != 1)
  378. return -EINVAL;
  379. rmidev->concurrent = input > 0 ? true : false;
  380. return count;
  381. }
  382. static int rmidev_allocate_buffer(int count)
  383. {
  384. if (count + 1 > rmidev->tmpbuf_size) {
  385. if (rmidev->tmpbuf_size)
  386. kfree(rmidev->tmpbuf);
  387. rmidev->tmpbuf = kzalloc(count + 1, GFP_KERNEL);
  388. if (!rmidev->tmpbuf) {
  389. dev_err(rmidev->rmi4_data->pdev->dev.parent,
  390. "%s: Failed to alloc mem for buffer\n",
  391. __func__);
  392. rmidev->tmpbuf_size = 0;
  393. return -ENOMEM;
  394. }
  395. rmidev->tmpbuf_size = count + 1;
  396. }
  397. return 0;
  398. }
  399. /*
  400. * rmidev_llseek - set register address to access for RMI device
  401. *
  402. * @filp: pointer to file structure
  403. * @off:
  404. * if whence == SEEK_SET,
  405. * off: 16-bit RMI register address
  406. * if whence == SEEK_CUR,
  407. * off: offset from current position
  408. * if whence == SEEK_END,
  409. * off: offset from end position (0xFFFF)
  410. * @whence: SEEK_SET, SEEK_CUR, or SEEK_END
  411. */
  412. static loff_t rmidev_llseek(struct file *filp, loff_t off, int whence)
  413. {
  414. loff_t newpos;
  415. struct rmidev_data *dev_data = filp->private_data;
  416. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  417. if (IS_ERR(dev_data)) {
  418. pr_err("%s: Pointer of char device data is invalid", __func__);
  419. return -EBADF;
  420. }
  421. mutex_lock(&(dev_data->file_mutex));
  422. switch (whence) {
  423. case SEEK_SET:
  424. newpos = off;
  425. break;
  426. case SEEK_CUR:
  427. newpos = filp->f_pos + off;
  428. break;
  429. case SEEK_END:
  430. newpos = REG_ADDR_LIMIT + off;
  431. break;
  432. default:
  433. newpos = -EINVAL;
  434. goto clean_up;
  435. }
  436. if (newpos < 0 || newpos > REG_ADDR_LIMIT) {
  437. dev_err(rmi4_data->pdev->dev.parent,
  438. "%s: New position 0x%04x is invalid\n",
  439. __func__, (unsigned int)newpos);
  440. newpos = -EINVAL;
  441. goto clean_up;
  442. }
  443. filp->f_pos = newpos;
  444. clean_up:
  445. mutex_unlock(&(dev_data->file_mutex));
  446. return newpos;
  447. }
  448. /*
  449. * rmidev_read: read register data from RMI device
  450. *
  451. * @filp: pointer to file structure
  452. * @buf: pointer to user space buffer
  453. * @count: number of bytes to read
  454. * @f_pos: starting RMI register address
  455. */
  456. static ssize_t rmidev_read(struct file *filp, char __user *buf,
  457. size_t count, loff_t *f_pos)
  458. {
  459. ssize_t retval;
  460. unsigned char intr_status = 0;
  461. unsigned short address;
  462. struct rmidev_data *dev_data = filp->private_data;
  463. struct synaptics_rmi4_fn *fhandler;
  464. struct synaptics_rmi4_device_info *rmi;
  465. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  466. rmi = &(rmi4_data->rmi4_mod_info);
  467. if (IS_ERR(dev_data)) {
  468. pr_err("%s: Pointer of char device data is invalid", __func__);
  469. return -EBADF;
  470. }
  471. mutex_lock(&(dev_data->file_mutex));
  472. if (*f_pos > REG_ADDR_LIMIT) {
  473. retval = -EFAULT;
  474. goto clean_up;
  475. }
  476. if (count > (REG_ADDR_LIMIT - *f_pos))
  477. count = REG_ADDR_LIMIT - *f_pos;
  478. if (count == 0) {
  479. retval = 0;
  480. goto clean_up;
  481. }
  482. address = (unsigned short)(*f_pos);
  483. rmidev_allocate_buffer(count);
  484. retval = synaptics_rmi4_reg_read(rmidev->rmi4_data,
  485. *f_pos,
  486. rmidev->tmpbuf,
  487. count);
  488. if (retval < 0)
  489. goto clean_up;
  490. if (copy_to_user(buf, rmidev->tmpbuf, count))
  491. retval = -EFAULT;
  492. else
  493. *f_pos += retval;
  494. if (!rmidev->concurrent)
  495. goto clean_up;
  496. if (address != rmi4_data->f01_data_base_addr)
  497. goto clean_up;
  498. if (count <= 1)
  499. goto clean_up;
  500. intr_status = rmidev->tmpbuf[1];
  501. if (!list_empty(&rmi->support_fn_list)) {
  502. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  503. if (fhandler->num_of_data_sources) {
  504. if (fhandler->intr_mask & intr_status) {
  505. rmi4_data->report_touch(rmi4_data,
  506. fhandler);
  507. }
  508. }
  509. }
  510. }
  511. clean_up:
  512. mutex_unlock(&(dev_data->file_mutex));
  513. return retval;
  514. }
  515. /*
  516. * rmidev_write: write register data to RMI device
  517. *
  518. * @filp: pointer to file structure
  519. * @buf: pointer to user space buffer
  520. * @count: number of bytes to write
  521. * @f_pos: starting RMI register address
  522. */
  523. static ssize_t rmidev_write(struct file *filp, const char __user *buf,
  524. size_t count, loff_t *f_pos)
  525. {
  526. ssize_t retval;
  527. struct rmidev_data *dev_data = filp->private_data;
  528. if (IS_ERR(dev_data)) {
  529. pr_err("%s: Pointer of char device data is invalid", __func__);
  530. return -EBADF;
  531. }
  532. mutex_lock(&(dev_data->file_mutex));
  533. if (*f_pos > REG_ADDR_LIMIT) {
  534. retval = -EFAULT;
  535. goto unlock;
  536. }
  537. if (count > (REG_ADDR_LIMIT - *f_pos))
  538. count = REG_ADDR_LIMIT - *f_pos;
  539. if (count == 0) {
  540. retval = 0;
  541. goto unlock;
  542. }
  543. rmidev_allocate_buffer(count);
  544. if (copy_from_user(rmidev->tmpbuf, buf, count)) {
  545. retval = -EFAULT;
  546. goto unlock;
  547. }
  548. retval = synaptics_rmi4_reg_write(rmidev->rmi4_data,
  549. *f_pos,
  550. rmidev->tmpbuf,
  551. count);
  552. if (retval >= 0)
  553. *f_pos += retval;
  554. unlock:
  555. mutex_unlock(&(dev_data->file_mutex));
  556. return retval;
  557. }
  558. static int rmidev_open(struct inode *inp, struct file *filp)
  559. {
  560. int retval = 0;
  561. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  562. struct rmidev_data *dev_data =
  563. container_of(inp->i_cdev, struct rmidev_data, main_dev);
  564. if (!dev_data)
  565. return -EACCES;
  566. if (rmi4_data->sensor_sleep) {
  567. dev_err(rmi4_data->pdev->dev.parent,
  568. "%s: Sensor sleeping\n",
  569. __func__);
  570. return -ENODEV;
  571. }
  572. rmi4_data->stay_awake = true;
  573. filp->private_data = dev_data;
  574. mutex_lock(&(dev_data->file_mutex));
  575. rmi4_data->irq_enable(rmi4_data, false, false);
  576. dev_dbg(rmi4_data->pdev->dev.parent,
  577. "%s: Attention interrupt disabled\n",
  578. __func__);
  579. if (dev_data->ref_count < 1)
  580. dev_data->ref_count++;
  581. else
  582. retval = -EACCES;
  583. mutex_unlock(&(dev_data->file_mutex));
  584. return retval;
  585. }
  586. static int rmidev_release(struct inode *inp, struct file *filp)
  587. {
  588. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  589. struct rmidev_data *dev_data =
  590. container_of(inp->i_cdev, struct rmidev_data, main_dev);
  591. if (!dev_data)
  592. return -EACCES;
  593. mutex_lock(&(dev_data->file_mutex));
  594. dev_data->ref_count--;
  595. if (dev_data->ref_count < 0)
  596. dev_data->ref_count = 0;
  597. rmi4_data->reset_device(rmi4_data, false);
  598. rmi4_data->stay_awake = false;
  599. mutex_unlock(&(dev_data->file_mutex));
  600. return 0;
  601. }
  602. static const struct file_operations rmidev_fops = {
  603. .owner = THIS_MODULE,
  604. .llseek = rmidev_llseek,
  605. .read = rmidev_read,
  606. .write = rmidev_write,
  607. .open = rmidev_open,
  608. .release = rmidev_release,
  609. };
  610. static void rmidev_device_cleanup(struct rmidev_data *dev_data)
  611. {
  612. dev_t devno;
  613. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  614. if (dev_data) {
  615. devno = dev_data->main_dev.dev;
  616. if (dev_data->device_class)
  617. device_destroy(dev_data->device_class, devno);
  618. cdev_del(&dev_data->main_dev);
  619. unregister_chrdev_region(devno, 1);
  620. dev_dbg(rmi4_data->pdev->dev.parent,
  621. "%s: rmidev device removed\n",
  622. __func__);
  623. }
  624. }
  625. static char *rmi_char_devnode(struct device *dev, umode_t *mode)
  626. {
  627. if (!mode)
  628. return NULL;
  629. *mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  630. return kasprintf(GFP_KERNEL, "rmi/%s", dev_name(dev));
  631. }
  632. static int rmidev_create_device_class(void)
  633. {
  634. if (rmidev_device_class != NULL)
  635. return 0;
  636. rmidev_device_class = class_create(THIS_MODULE, DEVICE_CLASS_NAME);
  637. if (IS_ERR(rmidev_device_class)) {
  638. pr_err("%s: Failed to create /dev/%s\n",
  639. __func__, CHAR_DEVICE_NAME);
  640. return -ENODEV;
  641. }
  642. rmidev_device_class->devnode = rmi_char_devnode;
  643. return 0;
  644. }
  645. static void rmidev_attn(struct synaptics_rmi4_data *rmi4_data,
  646. unsigned char intr_mask)
  647. {
  648. if (!rmidev)
  649. return;
  650. if (rmidev->pid && (rmidev->intr_mask & intr_mask))
  651. send_sig_info(SIGIO, &rmidev->interrupt_signal, rmidev->task);
  652. return;
  653. }
  654. static int rmidev_init_device(struct synaptics_rmi4_data *rmi4_data)
  655. {
  656. int retval;
  657. dev_t dev_no;
  658. unsigned char attr_count;
  659. struct rmidev_data *dev_data;
  660. struct device *device_ptr;
  661. const struct synaptics_dsx_board_data *bdata =
  662. rmi4_data->hw_if->board_data;
  663. if (rmidev) {
  664. dev_dbg(rmi4_data->pdev->dev.parent,
  665. "%s: Handle already exists\n",
  666. __func__);
  667. return 0;
  668. }
  669. rmidev = kzalloc(sizeof(*rmidev), GFP_KERNEL);
  670. if (!rmidev) {
  671. dev_err(rmi4_data->pdev->dev.parent,
  672. "%s: Failed to alloc mem for rmidev\n",
  673. __func__);
  674. retval = -ENOMEM;
  675. goto err_rmidev;
  676. }
  677. rmidev->rmi4_data = rmi4_data;
  678. memset(&rmidev->interrupt_signal, 0, sizeof(rmidev->interrupt_signal));
  679. rmidev->interrupt_signal.si_signo = SIGIO;
  680. rmidev->interrupt_signal.si_code = SI_USER;
  681. memset(&rmidev->terminate_signal, 0, sizeof(rmidev->terminate_signal));
  682. rmidev->terminate_signal.si_signo = SIGTERM;
  683. rmidev->terminate_signal.si_code = SI_USER;
  684. retval = rmidev_create_device_class();
  685. if (retval < 0) {
  686. dev_err(rmi4_data->pdev->dev.parent,
  687. "%s: Failed to create device class\n",
  688. __func__);
  689. goto err_device_class;
  690. }
  691. if (rmidev_major_num) {
  692. dev_no = MKDEV(rmidev_major_num, DEV_NUMBER);
  693. retval = register_chrdev_region(dev_no, 1, CHAR_DEVICE_NAME);
  694. } else {
  695. retval = alloc_chrdev_region(&dev_no, 0, 1, CHAR_DEVICE_NAME);
  696. if (retval < 0) {
  697. dev_err(rmi4_data->pdev->dev.parent,
  698. "%s: Failed to allocate char device region\n",
  699. __func__);
  700. goto err_device_region;
  701. }
  702. rmidev_major_num = MAJOR(dev_no);
  703. dev_dbg(rmi4_data->pdev->dev.parent,
  704. "%s: Major number of rmidev = %d\n",
  705. __func__, rmidev_major_num);
  706. }
  707. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  708. if (!dev_data) {
  709. dev_err(rmi4_data->pdev->dev.parent,
  710. "%s: Failed to alloc mem for dev_data\n",
  711. __func__);
  712. retval = -ENOMEM;
  713. goto err_dev_data;
  714. }
  715. mutex_init(&dev_data->file_mutex);
  716. dev_data->rmi_dev = rmidev;
  717. rmidev->data = dev_data;
  718. cdev_init(&dev_data->main_dev, &rmidev_fops);
  719. retval = cdev_add(&dev_data->main_dev, dev_no, 1);
  720. if (retval < 0) {
  721. dev_err(rmi4_data->pdev->dev.parent,
  722. "%s: Failed to add rmi char device\n",
  723. __func__);
  724. goto err_char_device;
  725. }
  726. dev_set_name(&rmidev->dev, "rmidev%d", MINOR(dev_no));
  727. dev_data->device_class = rmidev_device_class;
  728. device_ptr = device_create(dev_data->device_class, NULL, dev_no,
  729. NULL, CHAR_DEVICE_NAME"%d", MINOR(dev_no));
  730. if (IS_ERR(device_ptr)) {
  731. dev_err(rmi4_data->pdev->dev.parent,
  732. "%s: Failed to create rmi char device\n",
  733. __func__);
  734. retval = -ENODEV;
  735. goto err_char_device;
  736. }
  737. retval = gpio_export(bdata->irq_gpio, false);
  738. if (retval < 0) {
  739. dev_err(rmi4_data->pdev->dev.parent,
  740. "%s: Failed to export attention gpio\n",
  741. __func__);
  742. } else {
  743. retval = gpio_export_link(&(rmi4_data->input_dev->dev),
  744. "attn", bdata->irq_gpio);
  745. if (retval < 0) {
  746. dev_err(rmi4_data->pdev->dev.parent,
  747. "%s Failed to create gpio symlink\n",
  748. __func__);
  749. } else {
  750. dev_dbg(rmi4_data->pdev->dev.parent,
  751. "%s: Exported attention gpio %d\n",
  752. __func__, bdata->irq_gpio);
  753. }
  754. }
  755. rmidev->sysfs_dir = kobject_create_and_add(SYSFS_FOLDER_NAME,
  756. &rmi4_data->input_dev->dev.kobj);
  757. if (!rmidev->sysfs_dir) {
  758. dev_err(rmi4_data->pdev->dev.parent,
  759. "%s: Failed to create sysfs directory\n",
  760. __func__);
  761. retval = -ENODEV;
  762. goto err_sysfs_dir;
  763. }
  764. retval = sysfs_create_bin_file(rmidev->sysfs_dir,
  765. &attr_data);
  766. if (retval < 0) {
  767. dev_err(rmi4_data->pdev->dev.parent,
  768. "%s: Failed to create sysfs bin file\n",
  769. __func__);
  770. goto err_sysfs_bin;
  771. }
  772. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  773. retval = sysfs_create_file(rmidev->sysfs_dir,
  774. &attrs[attr_count].attr);
  775. if (retval < 0) {
  776. dev_err(rmi4_data->pdev->dev.parent,
  777. "%s: Failed to create sysfs attributes\n",
  778. __func__);
  779. retval = -ENODEV;
  780. goto err_sysfs_attrs;
  781. }
  782. }
  783. return 0;
  784. err_sysfs_attrs:
  785. for (attr_count--; attr_count >= 0; attr_count--)
  786. sysfs_remove_file(rmidev->sysfs_dir, &attrs[attr_count].attr);
  787. sysfs_remove_bin_file(rmidev->sysfs_dir, &attr_data);
  788. err_sysfs_bin:
  789. kobject_put(rmidev->sysfs_dir);
  790. err_sysfs_dir:
  791. sysfs_remove_link(&(rmi4_data->input_dev->dev.kobj), "attn");
  792. gpio_unexport(bdata->irq_gpio);
  793. err_char_device:
  794. rmidev_device_cleanup(dev_data);
  795. kfree(dev_data);
  796. err_dev_data:
  797. unregister_chrdev_region(dev_no, 1);
  798. err_device_region:
  799. if (rmidev_device_class != NULL) {
  800. class_destroy(rmidev_device_class);
  801. rmidev_device_class = NULL;
  802. }
  803. err_device_class:
  804. kfree(rmidev);
  805. rmidev = NULL;
  806. err_rmidev:
  807. return retval;
  808. }
  809. static void rmidev_remove_device(struct synaptics_rmi4_data *rmi4_data)
  810. {
  811. unsigned char attr_count;
  812. struct rmidev_data *dev_data;
  813. const struct synaptics_dsx_board_data *bdata =
  814. rmi4_data->hw_if->board_data;
  815. if (!rmidev)
  816. goto exit;
  817. rmidev_major_num = RMIDEV_MAJOR_NUM;
  818. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
  819. sysfs_remove_file(rmidev->sysfs_dir, &attrs[attr_count].attr);
  820. sysfs_remove_bin_file(rmidev->sysfs_dir, &attr_data);
  821. kobject_put(rmidev->sysfs_dir);
  822. sysfs_remove_link(&(rmi4_data->input_dev->dev.kobj), "attn");
  823. gpio_unexport(bdata->irq_gpio);
  824. dev_data = rmidev->data;
  825. if (dev_data) {
  826. rmidev_device_cleanup(dev_data);
  827. kfree(dev_data);
  828. }
  829. unregister_chrdev_region(rmidev->dev_no, 1);
  830. if (rmidev_device_class != NULL) {
  831. class_destroy(rmidev_device_class);
  832. rmidev_device_class = NULL;
  833. }
  834. kfree(rmidev->tmpbuf);
  835. kfree(rmidev);
  836. rmidev = NULL;
  837. exit:
  838. complete(&rmidev_remove_complete);
  839. }
  840. static struct synaptics_rmi4_exp_fn rmidev_module = {
  841. .fn_type = RMI_DEV,
  842. .init = rmidev_init_device,
  843. .remove = rmidev_remove_device,
  844. .reset = NULL,
  845. .reinit = NULL,
  846. .early_suspend = NULL,
  847. .suspend = NULL,
  848. .resume = NULL,
  849. .late_resume = NULL,
  850. .attn = rmidev_attn,
  851. };
  852. static int __init rmidev_module_init(void)
  853. {
  854. synaptics_rmi4_new_function(&rmidev_module, true);
  855. return 0;
  856. }
  857. static void __exit rmidev_module_exit(void)
  858. {
  859. synaptics_rmi4_new_function(&rmidev_module, false);
  860. wait_for_completion(&rmidev_remove_complete);
  861. }
  862. module_init(rmidev_module_init);
  863. module_exit(rmidev_module_exit);
  864. MODULE_AUTHOR("Synaptics, Inc.");
  865. MODULE_DESCRIPTION("Synaptics DSX RMI Dev Module");
  866. MODULE_LICENSE("GPL v2");