rmi_dev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Synaptics DSX touchscreen driver
  3. *
  4. * Copyright (C) 2012 Synaptics Incorporated
  5. *
  6. * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
  7. * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <asm/unaligned.h>
  22. #include <linux/slab.h>
  23. #include <linux/i2c.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/input.h>
  27. #include <linux/gpio.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/cdev.h>
  30. #include "synaptics_i2c_rmi.h"
  31. #define CHAR_DEVICE_NAME "rmi"
  32. #define DEVICE_CLASS_NAME "rmidev"
  33. #define SYSFS_FOLDER_NAME "rmidev"
  34. #define DEV_NUMBER 1
  35. #define REG_ADDR_LIMIT 0xFFFF
  36. static ssize_t rmidev_sysfs_data_show(struct file *data_file,
  37. struct kobject *kobj, struct bin_attribute *attributes,
  38. char *buf, loff_t pos, size_t count);
  39. static ssize_t rmidev_sysfs_data_store(struct file *data_file,
  40. struct kobject *kobj, struct bin_attribute *attributes,
  41. char *buf, loff_t pos, size_t count);
  42. static ssize_t rmidev_sysfs_open_store(struct device *dev,
  43. struct device_attribute *attr, const char *buf, size_t count);
  44. static ssize_t rmidev_sysfs_release_store(struct device *dev,
  45. struct device_attribute *attr, const char *buf, size_t count);
  46. static ssize_t rmidev_sysfs_attn_state_show(struct device *dev,
  47. struct device_attribute *attr, char *buf);
  48. struct rmidev_handle {
  49. dev_t dev_no;
  50. struct device dev;
  51. struct synaptics_rmi4_data *rmi4_data;
  52. struct synaptics_rmi4_exp_fn_ptr *fn_ptr;
  53. struct kobject *sysfs_dir;
  54. void *data;
  55. bool irq_enabled;
  56. };
  57. struct rmidev_data {
  58. int ref_count;
  59. struct cdev main_dev;
  60. struct class *device_class;
  61. struct mutex file_mutex;
  62. struct rmidev_handle *rmi_dev;
  63. };
  64. static struct bin_attribute attr_data = {
  65. .attr = {
  66. .name = "data",
  67. .mode = (S_IRUGO | S_IWUSR | S_IWGRP),
  68. },
  69. .size = 0,
  70. .read = rmidev_sysfs_data_show,
  71. .write = rmidev_sysfs_data_store,
  72. };
  73. static struct device_attribute attrs[] = {
  74. __ATTR(open, S_IWUSR,
  75. synaptics_rmi4_show_error,
  76. rmidev_sysfs_open_store),
  77. __ATTR(release, S_IWUSR,
  78. synaptics_rmi4_show_error,
  79. rmidev_sysfs_release_store),
  80. __ATTR(attn_state, S_IRUGO,
  81. rmidev_sysfs_attn_state_show,
  82. synaptics_rmi4_store_error),
  83. };
  84. static int rmidev_major_num;
  85. static struct class *rmidev_device_class;
  86. static struct rmidev_handle *rmidev;
  87. static irqreturn_t rmidev_sysfs_irq(int irq, void *data)
  88. {
  89. struct synaptics_rmi4_data *rmi4_data = data;
  90. sysfs_notify(&rmi4_data->input_dev->dev.kobj,
  91. SYSFS_FOLDER_NAME, "attn_state");
  92. return IRQ_HANDLED;
  93. }
  94. static int rmidev_sysfs_irq_enable(struct synaptics_rmi4_data *rmi4_data,
  95. bool enable)
  96. {
  97. int retval = 0;
  98. unsigned char intr_status[MAX_INTR_REGISTERS];
  99. unsigned long irq_flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
  100. if (enable) {
  101. if (rmidev->irq_enabled)
  102. return retval;
  103. /* Clear interrupts first */
  104. retval = rmidev->fn_ptr->read(rmi4_data,
  105. rmi4_data->f01_data_base_addr + 1,
  106. intr_status,
  107. rmi4_data->num_of_intr_regs);
  108. if (retval < 0)
  109. return retval;
  110. retval = request_threaded_irq(rmi4_data->irq, NULL,
  111. rmidev_sysfs_irq, irq_flags,
  112. "synaptics_dsx_rmidev", rmi4_data);
  113. if (retval < 0) {
  114. dev_err(&rmi4_data->i2c_client->dev,
  115. "%s: Failed to create irq thread\n",
  116. __func__);
  117. return retval;
  118. }
  119. rmidev->irq_enabled = true;
  120. } else {
  121. if (rmidev->irq_enabled) {
  122. disable_irq(rmi4_data->irq);
  123. free_irq(rmi4_data->irq, rmi4_data);
  124. rmidev->irq_enabled = false;
  125. }
  126. }
  127. return retval;
  128. }
  129. static ssize_t rmidev_sysfs_data_show(struct file *data_file,
  130. struct kobject *kobj, struct bin_attribute *attributes,
  131. char *buf, loff_t pos, size_t count)
  132. {
  133. int retval;
  134. unsigned int length = (unsigned int)count;
  135. unsigned short address = (unsigned short)pos;
  136. if (length > (REG_ADDR_LIMIT - address)) {
  137. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  138. "%s: Out of register map limit\n",
  139. __func__);
  140. return -EINVAL;
  141. }
  142. if (length) {
  143. retval = rmidev->fn_ptr->read(rmidev->rmi4_data,
  144. address,
  145. (unsigned char *)buf,
  146. length);
  147. if (retval < 0) {
  148. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  149. "%s: Failed to read data\n",
  150. __func__);
  151. return retval;
  152. }
  153. } else {
  154. return -EINVAL;
  155. }
  156. return length;
  157. }
  158. static ssize_t rmidev_sysfs_data_store(struct file *data_file,
  159. struct kobject *kobj, struct bin_attribute *attributes,
  160. char *buf, loff_t pos, size_t count)
  161. {
  162. int retval;
  163. unsigned int length = (unsigned int)count;
  164. unsigned short address = (unsigned short)pos;
  165. if (length > (REG_ADDR_LIMIT - address)) {
  166. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  167. "%s: Out of register map limit\n",
  168. __func__);
  169. return -EINVAL;
  170. }
  171. if (length) {
  172. retval = rmidev->fn_ptr->write(rmidev->rmi4_data,
  173. address,
  174. (unsigned char *)buf,
  175. length);
  176. if (retval < 0) {
  177. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  178. "%s: Failed to write data\n",
  179. __func__);
  180. return retval;
  181. }
  182. } else {
  183. return -EINVAL;
  184. }
  185. return length;
  186. }
  187. static ssize_t rmidev_sysfs_open_store(struct device *dev,
  188. struct device_attribute *attr, const char *buf, size_t count)
  189. {
  190. unsigned int input;
  191. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  192. if (sscanf(buf, "%u", &input) != 1)
  193. return -EINVAL;
  194. if (input != 1)
  195. return -EINVAL;
  196. rmidev->fn_ptr->enable(rmi4_data, false);
  197. rmidev_sysfs_irq_enable(rmi4_data, true);
  198. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  199. "%s: Attention interrupt disabled\n",
  200. __func__);
  201. return count;
  202. }
  203. static ssize_t rmidev_sysfs_release_store(struct device *dev,
  204. struct device_attribute *attr, const char *buf, size_t count)
  205. {
  206. unsigned int input;
  207. struct synaptics_rmi4_data *rmi4_data = rmidev->rmi4_data;
  208. if (sscanf(buf, "%u", &input) != 1)
  209. return -EINVAL;
  210. if (input != 1)
  211. return -EINVAL;
  212. rmidev_sysfs_irq_enable(rmi4_data, false);
  213. rmidev->fn_ptr->enable(rmi4_data, true);
  214. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  215. "%s: Attention interrupt enabled\n",
  216. __func__);
  217. return count;
  218. }
  219. static ssize_t rmidev_sysfs_attn_state_show(struct device *dev,
  220. struct device_attribute *attr, char *buf)
  221. {
  222. int attn_state;
  223. attn_state = gpio_get_value(rmidev->rmi4_data->dt_data->irq_gpio);
  224. return snprintf(buf, PAGE_SIZE, "%u\n", attn_state);
  225. }
  226. /*
  227. * rmidev_llseek - used to set up register address
  228. *
  229. * @filp: file structure for seek
  230. * @off: offset
  231. * if whence == SEEK_SET,
  232. * high 16 bits: page address
  233. * low 16 bits: register address
  234. * if whence == SEEK_CUR,
  235. * offset from current position
  236. * if whence == SEEK_END,
  237. * offset from end position (0xFFFF)
  238. * @whence: SEEK_SET, SEEK_CUR, or SEEK_END
  239. */
  240. static loff_t rmidev_llseek(struct file *filp, loff_t off, int whence)
  241. {
  242. loff_t newpos;
  243. struct rmidev_data *dev_data = filp->private_data;
  244. if (IS_ERR(dev_data)) {
  245. pr_err("%s: Pointer of char device data is invalid", __func__);
  246. return -EBADF;
  247. }
  248. mutex_lock(&(dev_data->file_mutex));
  249. switch (whence) {
  250. case SEEK_SET:
  251. newpos = off;
  252. break;
  253. case SEEK_CUR:
  254. newpos = filp->f_pos + off;
  255. break;
  256. case SEEK_END:
  257. newpos = REG_ADDR_LIMIT + off;
  258. break;
  259. default:
  260. newpos = -EINVAL;
  261. goto clean_up;
  262. }
  263. if (newpos < 0 || newpos > REG_ADDR_LIMIT) {
  264. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  265. "%s: New position 0x%04x is invalid\n",
  266. __func__, (unsigned int)newpos);
  267. newpos = -EINVAL;
  268. goto clean_up;
  269. }
  270. filp->f_pos = newpos;
  271. clean_up:
  272. mutex_unlock(&(dev_data->file_mutex));
  273. return newpos;
  274. }
  275. /*
  276. * rmidev_read: - use to read data from rmi device
  277. *
  278. * @filp: file structure for read
  279. * @buf: user space buffer pointer
  280. * @count: number of bytes to read
  281. * @f_pos: offset (starting register address)
  282. */
  283. static ssize_t rmidev_read(struct file *filp, char __user *buf,
  284. size_t count, loff_t *f_pos)
  285. {
  286. ssize_t retval;
  287. unsigned char tmpbuf[count + 1];
  288. struct rmidev_data *dev_data = filp->private_data;
  289. if (IS_ERR(dev_data)) {
  290. pr_err("%s: Pointer of char device data is invalid", __func__);
  291. return -EBADF;
  292. }
  293. if (count == 0)
  294. return 0;
  295. if (count > (REG_ADDR_LIMIT - *f_pos))
  296. count = REG_ADDR_LIMIT - *f_pos;
  297. mutex_lock(&(dev_data->file_mutex));
  298. retval = rmidev->fn_ptr->read(rmidev->rmi4_data,
  299. *f_pos,
  300. tmpbuf,
  301. count);
  302. if (retval < 0)
  303. goto clean_up;
  304. if (copy_to_user(buf, tmpbuf, count))
  305. retval = -EFAULT;
  306. else
  307. *f_pos += retval;
  308. clean_up:
  309. mutex_unlock(&(dev_data->file_mutex));
  310. return retval;
  311. }
  312. /*
  313. * rmidev_write: - used to write data to rmi device
  314. *
  315. * @filep: file structure for write
  316. * @buf: user space buffer pointer
  317. * @count: number of bytes to write
  318. * @f_pos: offset (starting register address)
  319. */
  320. static ssize_t rmidev_write(struct file *filp, const char __user *buf,
  321. size_t count, loff_t *f_pos)
  322. {
  323. ssize_t retval;
  324. unsigned char tmpbuf[count + 1];
  325. struct rmidev_data *dev_data = filp->private_data;
  326. if (IS_ERR(dev_data)) {
  327. pr_err("%s: Pointer of char device data is invalid", __func__);
  328. return -EBADF;
  329. }
  330. if (count == 0)
  331. return 0;
  332. if (count > (REG_ADDR_LIMIT - *f_pos))
  333. count = REG_ADDR_LIMIT - *f_pos;
  334. if (copy_from_user(tmpbuf, buf, count))
  335. return -EFAULT;
  336. mutex_lock(&(dev_data->file_mutex));
  337. retval = rmidev->fn_ptr->write(rmidev->rmi4_data,
  338. *f_pos,
  339. tmpbuf,
  340. count);
  341. if (retval >= 0)
  342. *f_pos += retval;
  343. mutex_unlock(&(dev_data->file_mutex));
  344. return retval;
  345. }
  346. /*
  347. * rmidev_open: enable access to rmi device
  348. * @inp: inode struture
  349. * @filp: file structure
  350. */
  351. static int rmidev_open(struct inode *inp, struct file *filp)
  352. {
  353. int retval = 0;
  354. struct rmidev_data *dev_data =
  355. container_of(inp->i_cdev, struct rmidev_data, main_dev);
  356. if (!dev_data)
  357. return -EACCES;
  358. filp->private_data = dev_data;
  359. mutex_lock(&(dev_data->file_mutex));
  360. rmidev->fn_ptr->enable(rmidev->rmi4_data, false);
  361. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  362. "%s: Attention interrupt disabled\n",
  363. __func__);
  364. if (dev_data->ref_count < 1)
  365. dev_data->ref_count++;
  366. else
  367. retval = -EACCES;
  368. mutex_unlock(&(dev_data->file_mutex));
  369. return retval;
  370. }
  371. /*
  372. * rmidev_release: - release access to rmi device
  373. * @inp: inode structure
  374. * @filp: file structure
  375. */
  376. static int rmidev_release(struct inode *inp, struct file *filp)
  377. {
  378. struct rmidev_data *dev_data =
  379. container_of(inp->i_cdev, struct rmidev_data, main_dev);
  380. if (!dev_data)
  381. return -EACCES;
  382. mutex_lock(&(dev_data->file_mutex));
  383. dev_data->ref_count--;
  384. if (dev_data->ref_count < 0)
  385. dev_data->ref_count = 0;
  386. rmidev->fn_ptr->enable(rmidev->rmi4_data, true);
  387. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  388. "%s: Attention interrupt enabled\n",
  389. __func__);
  390. mutex_unlock(&(dev_data->file_mutex));
  391. return 0;
  392. }
  393. static const struct file_operations rmidev_fops = {
  394. .owner = THIS_MODULE,
  395. .llseek = rmidev_llseek,
  396. .read = rmidev_read,
  397. .write = rmidev_write,
  398. .open = rmidev_open,
  399. .release = rmidev_release,
  400. };
  401. static void rmidev_device_cleanup(struct rmidev_data *dev_data)
  402. {
  403. dev_t devno;
  404. if (dev_data) {
  405. devno = dev_data->main_dev.dev;
  406. if (dev_data->device_class)
  407. device_destroy(dev_data->device_class, devno);
  408. cdev_del(&dev_data->main_dev);
  409. unregister_chrdev_region(devno, 1);
  410. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  411. "%s: rmidev device removed\n",
  412. __func__);
  413. }
  414. return;
  415. }
  416. static char *rmi_char_devnode(struct device *dev, mode_t *mode)
  417. {
  418. if (!mode)
  419. return NULL;
  420. *mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  421. return kasprintf(GFP_KERNEL, "rmi/%s", dev_name(dev));
  422. }
  423. static int rmidev_create_device_class(void)
  424. {
  425. rmidev_device_class = class_create(THIS_MODULE, DEVICE_CLASS_NAME);
  426. if (IS_ERR(rmidev_device_class)) {
  427. pr_err("%s: Failed to create /dev/%s\n",
  428. __func__, CHAR_DEVICE_NAME);
  429. return -ENODEV;
  430. }
  431. rmidev_device_class->devnode = rmi_char_devnode;
  432. return 0;
  433. }
  434. static int rmidev_init_device(struct synaptics_rmi4_data *rmi4_data)
  435. {
  436. int retval;
  437. dev_t dev_no;
  438. unsigned char attr_count;
  439. int attr_count_num;
  440. struct rmidev_data *dev_data;
  441. struct device *device_ptr;
  442. rmidev = kzalloc(sizeof(*rmidev), GFP_KERNEL);
  443. if (!rmidev) {
  444. dev_err(&rmi4_data->i2c_client->dev,
  445. "%s: Failed to alloc mem for rmidev\n",
  446. __func__);
  447. retval = -ENOMEM;
  448. goto err_rmidev;
  449. }
  450. rmidev->fn_ptr = kzalloc(sizeof(*(rmidev->fn_ptr)), GFP_KERNEL);
  451. if (!rmidev) {
  452. dev_err(&rmi4_data->i2c_client->dev,
  453. "%s: Failed to alloc mem for fn_ptr\n",
  454. __func__);
  455. retval = -ENOMEM;
  456. goto err_fn_ptr;
  457. }
  458. rmidev->fn_ptr->read = rmi4_data->i2c_read;
  459. rmidev->fn_ptr->write = rmi4_data->i2c_write;
  460. rmidev->fn_ptr->enable = rmi4_data->irq_enable;
  461. rmidev->rmi4_data = rmi4_data;
  462. retval = rmidev_create_device_class();
  463. if (retval < 0) {
  464. dev_err(&rmi4_data->i2c_client->dev,
  465. "%s: Failed to create device class\n",
  466. __func__);
  467. goto err_device_class;
  468. }
  469. if (rmidev_major_num) {
  470. dev_no = MKDEV(rmidev_major_num, DEV_NUMBER);
  471. retval = register_chrdev_region(dev_no, 1, CHAR_DEVICE_NAME);
  472. } else {
  473. retval = alloc_chrdev_region(&dev_no, 0, 1, CHAR_DEVICE_NAME);
  474. if (retval < 0) {
  475. dev_err(&rmi4_data->i2c_client->dev,
  476. "%s: Failed to allocate char device region\n",
  477. __func__);
  478. goto err_device_region;
  479. }
  480. rmidev_major_num = MAJOR(dev_no);
  481. dev_dbg(&rmi4_data->i2c_client->dev,
  482. "%s: Major number of rmidev = %d\n",
  483. __func__, rmidev_major_num);
  484. }
  485. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  486. if (!dev_data) {
  487. dev_err(&rmi4_data->i2c_client->dev,
  488. "%s: Failed to alloc mem for dev_data\n",
  489. __func__);
  490. retval = -ENOMEM;
  491. goto err_dev_data;
  492. }
  493. mutex_init(&dev_data->file_mutex);
  494. dev_data->rmi_dev = rmidev;
  495. rmidev->data = dev_data;
  496. cdev_init(&dev_data->main_dev, &rmidev_fops);
  497. retval = cdev_add(&dev_data->main_dev, dev_no, 1);
  498. if (retval < 0) {
  499. dev_err(&rmi4_data->i2c_client->dev,
  500. "%s: Failed to add rmi char device\n",
  501. __func__);
  502. goto err_char_device;
  503. }
  504. dev_set_name(&rmidev->dev, "rmidev%d", MINOR(dev_no));
  505. dev_data->device_class = rmidev_device_class;
  506. device_ptr = device_create(dev_data->device_class, NULL, dev_no,
  507. NULL, CHAR_DEVICE_NAME"%d", MINOR(dev_no));
  508. if (IS_ERR(device_ptr)) {
  509. dev_err(&rmi4_data->i2c_client->dev,
  510. "%s: Failed to create rmi char device\n",
  511. __func__);
  512. retval = -ENODEV;
  513. goto err_char_device;
  514. }
  515. retval = gpio_export(rmi4_data->dt_data->irq_gpio, false);
  516. if (retval < 0) {
  517. dev_err(&rmi4_data->i2c_client->dev,
  518. "%s: Failed to export attention gpio\n",
  519. __func__);
  520. } else {
  521. retval = gpio_export_link(&(rmi4_data->input_dev->dev),
  522. "attn", rmi4_data->dt_data->irq_gpio);
  523. if (retval < 0) {
  524. dev_err(&rmi4_data->input_dev->dev,
  525. "%s Failed to create gpio symlink\n",
  526. __func__);
  527. } else {
  528. dev_dbg(&rmi4_data->input_dev->dev,
  529. "%s: Exported attention gpio %d\n",
  530. __func__, rmi4_data->dt_data->irq_gpio);
  531. }
  532. }
  533. rmidev->sysfs_dir = kobject_create_and_add(SYSFS_FOLDER_NAME,
  534. &rmi4_data->input_dev->dev.kobj);
  535. if (!rmidev->sysfs_dir) {
  536. dev_err(&rmi4_data->i2c_client->dev,
  537. "%s: Failed to create sysfs directory\n",
  538. __func__);
  539. retval = -ENODEV;
  540. goto err_sysfs_dir;
  541. }
  542. retval = sysfs_create_bin_file(rmidev->sysfs_dir,
  543. &attr_data);
  544. if (retval < 0) {
  545. dev_err(&rmi4_data->i2c_client->dev,
  546. "%s: Failed to create sysfs bin file\n",
  547. __func__);
  548. goto err_sysfs_bin;
  549. }
  550. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  551. retval = sysfs_create_file(rmidev->sysfs_dir,
  552. &attrs[attr_count].attr);
  553. if (retval < 0) {
  554. dev_err(&rmi4_data->input_dev->dev,
  555. "%s: Failed to create sysfs attributes\n",
  556. __func__);
  557. retval = -ENODEV;
  558. goto err_sysfs_attrs;
  559. }
  560. }
  561. return 0;
  562. err_sysfs_attrs:
  563. attr_count_num = (int)attr_count;
  564. for (attr_count_num--; attr_count_num >= 0; attr_count_num--)
  565. sysfs_remove_file(rmidev->sysfs_dir, &attrs[attr_count_num].attr);
  566. sysfs_remove_bin_file(rmidev->sysfs_dir, &attr_data);
  567. err_sysfs_bin:
  568. kobject_put(rmidev->sysfs_dir);
  569. err_sysfs_dir:
  570. err_char_device:
  571. rmidev_device_cleanup(dev_data);
  572. mutex_destroy(&dev_data->file_mutex);
  573. kfree(dev_data);
  574. err_dev_data:
  575. unregister_chrdev_region(dev_no, 1);
  576. err_device_region:
  577. class_destroy(rmidev_device_class);
  578. err_device_class:
  579. kfree(rmidev->fn_ptr);
  580. err_fn_ptr:
  581. kfree(rmidev);
  582. rmidev = NULL;
  583. err_rmidev:
  584. return retval;
  585. }
  586. static void rmidev_remove_device(struct synaptics_rmi4_data *rmi4_data)
  587. {
  588. unsigned char attr_count;
  589. struct rmidev_data *dev_data;
  590. if (!rmidev)
  591. goto exit;
  592. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
  593. sysfs_remove_file(rmidev->sysfs_dir, &attrs[attr_count].attr);
  594. sysfs_remove_bin_file(rmidev->sysfs_dir, &attr_data);
  595. kobject_put(rmidev->sysfs_dir);
  596. dev_data = rmidev->data;
  597. if (dev_data) {
  598. rmidev_device_cleanup(dev_data);
  599. mutex_destroy(&dev_data->file_mutex);
  600. kfree(dev_data);
  601. }
  602. unregister_chrdev_region(rmidev->dev_no, 1);
  603. class_destroy(rmidev_device_class);
  604. kfree(rmidev->fn_ptr);
  605. kfree(rmidev);
  606. rmidev = NULL;
  607. exit:
  608. return;
  609. }
  610. int rmidev_module_register(void)
  611. {
  612. int retval;
  613. retval = synaptics_rmi4_new_function(RMI_DEV,
  614. rmidev_init_device,
  615. rmidev_remove_device,
  616. NULL);
  617. return retval;
  618. }