slaveirq.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. $License:
  3. Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. $
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/stat.h>
  22. #include <linux/irq.h>
  23. #include <linux/signal.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c-dev.h>
  27. #include <linux/poll.h>
  28. #include <linux/errno.h>
  29. #include <linux/fs.h>
  30. #include <linux/mm.h>
  31. #include <linux/sched.h>
  32. #include <linux/wait.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/wait.h>
  36. #include <linux/slab.h>
  37. #include "mpu.h"
  38. #include "slaveirq.h"
  39. #include "mldl_cfg.h"
  40. #include "mpu-i2c.h"
  41. /* function which gets slave data and sends it to SLAVE */
  42. struct slaveirq_dev_data {
  43. struct miscdevice dev;
  44. struct i2c_client *slave_client;
  45. struct mpuirq_data data;
  46. wait_queue_head_t slaveirq_wait;
  47. int irq;
  48. int pid;
  49. int data_ready;
  50. int timeout;
  51. };
  52. /* The following depends on patch fa1f68db6ca7ebb6fc4487ac215bffba06c01c28
  53. * drivers: misc: pass miscdevice pointer via file private data
  54. */
  55. static int slaveirq_open(struct inode *inode, struct file *file)
  56. {
  57. /* Device node is availabe in the file->private_data, this is
  58. * exactly what we want so we leave it there */
  59. struct slaveirq_dev_data *data =
  60. container_of(file->private_data, struct slaveirq_dev_data, dev);
  61. dev_dbg(data->dev.this_device,
  62. "%s current->pid %d\n", __func__, current->pid);
  63. data->pid = current->pid;
  64. return 0;
  65. }
  66. static int slaveirq_release(struct inode *inode, struct file *file)
  67. {
  68. struct slaveirq_dev_data *data =
  69. container_of(file->private_data, struct slaveirq_dev_data, dev);
  70. dev_dbg(data->dev.this_device, "slaveirq_release\n");
  71. return 0;
  72. }
  73. /* read function called when from /dev/slaveirq is read */
  74. static ssize_t slaveirq_read(struct file *file,
  75. char *buf, size_t count, loff_t *ppos)
  76. {
  77. int len, err;
  78. struct slaveirq_dev_data *data =
  79. container_of(file->private_data, struct slaveirq_dev_data, dev);
  80. if (!data->data_ready &&
  81. data->timeout &&
  82. !(file->f_flags & O_NONBLOCK)) {
  83. wait_event_interruptible_timeout(data->slaveirq_wait,
  84. data->data_ready,
  85. data->timeout);
  86. }
  87. if (data->data_ready && NULL != buf
  88. && count >= sizeof(data->data)) {
  89. err = copy_to_user(buf, &data->data, sizeof(data->data));
  90. data->data.data_type = 0;
  91. } else {
  92. return 0;
  93. }
  94. if (err != 0) {
  95. dev_err(data->dev.this_device,
  96. "Copy to user returned %d\n", err);
  97. return -EFAULT;
  98. }
  99. data->data_ready = 0;
  100. len = sizeof(data->data);
  101. return len;
  102. }
  103. static unsigned int slaveirq_poll(struct file *file,
  104. struct poll_table_struct *poll)
  105. {
  106. int mask = 0;
  107. struct slaveirq_dev_data *data =
  108. container_of(file->private_data, struct slaveirq_dev_data, dev);
  109. poll_wait(file, &data->slaveirq_wait, poll);
  110. if (data->data_ready)
  111. mask |= POLLIN | POLLRDNORM;
  112. return mask;
  113. }
  114. /* ioctl - I/O control */
  115. static long slaveirq_ioctl(struct file *file,
  116. unsigned int cmd, unsigned long arg)
  117. {
  118. int retval = 0;
  119. int tmp;
  120. struct slaveirq_dev_data *data =
  121. container_of(file->private_data, struct slaveirq_dev_data, dev);
  122. switch (cmd) {
  123. case SLAVEIRQ_SET_TIMEOUT:
  124. data->timeout = arg;
  125. break;
  126. case SLAVEIRQ_GET_INTERRUPT_CNT:
  127. tmp = data->data.interruptcount - 1;
  128. if (data->data.interruptcount > 1)
  129. data->data.interruptcount = 1;
  130. if (copy_to_user((int *) arg, &tmp, sizeof(int)))
  131. return -EFAULT;
  132. break;
  133. case SLAVEIRQ_GET_IRQ_TIME:
  134. if (copy_to_user((int *) arg, &data->data.irqtime,
  135. sizeof(data->data.irqtime)))
  136. return -EFAULT;
  137. data->data.irqtime = 0;
  138. break;
  139. default:
  140. retval = -EINVAL;
  141. }
  142. return retval;
  143. }
  144. static irqreturn_t slaveirq_handler(int irq, void *dev_id)
  145. {
  146. struct slaveirq_dev_data *data = (struct slaveirq_dev_data *)dev_id;
  147. static int mycount;
  148. struct timeval irqtime;
  149. mycount++;
  150. data->data.interruptcount++;
  151. /* wake up (unblock) for reading data from userspace */
  152. data->data_ready = 1;
  153. do_gettimeofday(&irqtime);
  154. data->data.irqtime = (((long long) irqtime.tv_sec) << 32);
  155. data->data.irqtime += irqtime.tv_usec;
  156. data->data.data_type |= 1;
  157. wake_up_interruptible(&data->slaveirq_wait);
  158. return IRQ_HANDLED;
  159. }
  160. /* define which file operations are supported */
  161. static const struct file_operations slaveirq_fops = {
  162. .owner = THIS_MODULE,
  163. .read = slaveirq_read,
  164. .poll = slaveirq_poll,
  165. #if HAVE_COMPAT_IOCTL
  166. .compat_ioctl = slaveirq_ioctl,
  167. #endif
  168. #if HAVE_UNLOCKED_IOCTL
  169. .unlocked_ioctl = slaveirq_ioctl,
  170. #endif
  171. .open = slaveirq_open,
  172. .release = slaveirq_release,
  173. };
  174. int slaveirq_init(struct i2c_adapter *slave_adapter,
  175. struct ext_slave_platform_data *pdata,
  176. char *name)
  177. {
  178. int res;
  179. struct slaveirq_dev_data *data;
  180. if (!pdata->irq)
  181. return -EINVAL;
  182. pdata->irq_data = kzalloc(sizeof(*data),
  183. GFP_KERNEL);
  184. data = (struct slaveirq_dev_data *) pdata->irq_data;
  185. if (!data)
  186. return -ENOMEM;
  187. data->dev.minor = MISC_DYNAMIC_MINOR;
  188. data->dev.name = name;
  189. data->dev.fops = &slaveirq_fops;
  190. data->irq = pdata->irq;
  191. data->pid = 0;
  192. data->data_ready = 0;
  193. data->timeout = 0;
  194. init_waitqueue_head(&data->slaveirq_wait);
  195. res = request_irq(data->irq, slaveirq_handler, IRQF_TRIGGER_RISING,
  196. data->dev.name, data);
  197. if (res) {
  198. dev_err(&slave_adapter->dev,
  199. "myirqtest: cannot register IRQ %d\n",
  200. data->irq);
  201. goto out_request_irq;
  202. }
  203. res = misc_register(&data->dev);
  204. if (res < 0) {
  205. dev_err(&slave_adapter->dev,
  206. "misc_register returned %d\n",
  207. res);
  208. goto out_misc_register;
  209. }
  210. return res;
  211. out_misc_register:
  212. free_irq(data->irq, data);
  213. out_request_irq:
  214. kfree(pdata->irq_data);
  215. pdata->irq_data = NULL;
  216. return res;
  217. }
  218. void slaveirq_exit(struct ext_slave_platform_data *pdata)
  219. {
  220. struct slaveirq_dev_data *data = pdata->irq_data;
  221. if (!pdata->irq_data || data->irq <= 0)
  222. return;
  223. dev_info(data->dev.this_device, "Unregistering %s\n",
  224. data->dev.name);
  225. free_irq(data->irq, data);
  226. misc_deregister(&data->dev);
  227. kfree(pdata->irq_data);
  228. pdata->irq_data = NULL;
  229. }