rmi_dev.c 17 KB

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