3 Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/stat.h>
25 #include <linux/irq.h>
26 #include <linux/signal.h>
27 #include <linux/miscdevice.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-dev.h>
30 #include <linux/poll.h>
32 #include <linux/errno.h>
35 #include <linux/sched.h>
36 #include <linux/wait.h>
37 #include <linux/uaccess.h>
39 #include <linux/wait.h>
40 #include <linux/slab.h>
42 #include <linux/mpu.h>
46 /* function which gets slave data and sends it to SLAVE */
48 struct slaveirq_dev_data {
49 struct miscdevice dev;
50 struct i2c_client *slave_client;
51 struct mpuirq_data data;
52 wait_queue_head_t slaveirq_wait;
59 /* The following depends on patch fa1f68db6ca7ebb6fc4487ac215bffba06c01c28
60 * drivers: misc: pass miscdevice pointer via file private data
62 static int slaveirq_open(struct inode *inode, struct file *file)
64 /* Device node is availabe in the file->private_data, this is
65 * exactly what we want so we leave it there */
66 struct slaveirq_dev_data *data =
67 container_of(file->private_data, struct slaveirq_dev_data, dev);
69 dev_dbg(data->dev.this_device,
70 "%s current->pid %d\n", __func__, current->pid);
71 data->pid = current->pid;
75 static int slaveirq_release(struct inode *inode, struct file *file)
77 struct slaveirq_dev_data *data =
78 container_of(file->private_data, struct slaveirq_dev_data, dev);
79 dev_dbg(data->dev.this_device, "slaveirq_release\n");
83 /* read function called when from /dev/slaveirq is read */
84 static ssize_t slaveirq_read(struct file *file,
85 char *buf, size_t count, loff_t *ppos)
88 struct slaveirq_dev_data *data =
89 container_of(file->private_data, struct slaveirq_dev_data, dev);
91 if (!data->data_ready && data->timeout &&
92 !(file->f_flags & O_NONBLOCK)) {
93 wait_event_interruptible_timeout(data->slaveirq_wait,
98 if (data->data_ready && NULL != buf && count >= sizeof(data->data)) {
99 err = copy_to_user(buf, &data->data, sizeof(data->data));
100 data->data.data_type = 0;
105 dev_err(data->dev.this_device,
106 "Copy to user returned %d\n", err);
109 data->data_ready = 0;
110 len = sizeof(data->data);
114 static unsigned int slaveirq_poll(struct file *file,
115 struct poll_table_struct *poll)
118 struct slaveirq_dev_data *data =
119 container_of(file->private_data, struct slaveirq_dev_data, dev);
121 poll_wait(file, &data->slaveirq_wait, poll);
122 if (data->data_ready)
123 mask |= POLLIN | POLLRDNORM;
127 /* ioctl - I/O control */
128 static long slaveirq_ioctl(struct file *file,
129 unsigned int cmd, unsigned long arg)
133 struct slaveirq_dev_data *data =
134 container_of(file->private_data, struct slaveirq_dev_data, dev);
137 case SLAVEIRQ_SET_TIMEOUT:
141 case SLAVEIRQ_GET_INTERRUPT_CNT:
142 tmp = data->data.interruptcount - 1;
143 if (data->data.interruptcount > 1)
144 data->data.interruptcount = 1;
146 if (copy_to_user((int *)arg, &tmp, sizeof(int)))
149 case SLAVEIRQ_GET_IRQ_TIME:
150 if (copy_to_user((int *)arg, &data->data.irqtime,
151 sizeof(data->data.irqtime)))
153 data->data.irqtime = 0;
161 static irqreturn_t slaveirq_handler(int irq, void *dev_id)
163 struct slaveirq_dev_data *data = (struct slaveirq_dev_data *)dev_id;
165 struct timeval irqtime;
168 data->data.interruptcount++;
170 /* wake up (unblock) for reading data from userspace */
171 data->data_ready = 1;
173 do_gettimeofday(&irqtime);
174 data->data.irqtime = (((long long)irqtime.tv_sec) << 32);
175 data->data.irqtime += irqtime.tv_usec;
176 data->data.data_type |= 1;
178 wake_up_interruptible(&data->slaveirq_wait);
184 /* define which file operations are supported */
185 static const struct file_operations slaveirq_fops = {
186 .owner = THIS_MODULE,
187 .read = slaveirq_read,
188 .poll = slaveirq_poll,
190 #if HAVE_COMPAT_IOCTL
191 .compat_ioctl = slaveirq_ioctl,
193 #if HAVE_UNLOCKED_IOCTL
194 .unlocked_ioctl = slaveirq_ioctl,
196 .open = slaveirq_open,
197 .release = slaveirq_release,
200 int slaveirq_init(struct i2c_adapter *slave_adapter,
201 struct ext_slave_platform_data *pdata, char *name)
205 struct slaveirq_dev_data *data;
210 pdata->irq_data = kzalloc(sizeof(*data), GFP_KERNEL);
211 data = (struct slaveirq_dev_data *)pdata->irq_data;
215 data->dev.minor = MISC_DYNAMIC_MINOR;
216 data->dev.name = name;
217 data->dev.fops = &slaveirq_fops;
218 data->irq = pdata->irq;
220 data->data_ready = 0;
223 init_waitqueue_head(&data->slaveirq_wait);
225 res = request_irq(data->irq, slaveirq_handler,
226 IRQF_TRIGGER_RISING | IRQF_SHARED,
227 data->dev.name, data);
230 dev_err(&slave_adapter->dev,
231 "myirqtest: cannot register IRQ %d\n", data->irq);
232 goto out_request_irq;
235 res = misc_register(&data->dev);
237 dev_err(&slave_adapter->dev,
238 "misc_register returned %d\n", res);
239 goto out_misc_register;
245 free_irq(data->irq, data);
247 kfree(pdata->irq_data);
248 pdata->irq_data = NULL;
253 void slaveirq_exit(struct ext_slave_platform_data *pdata)
255 struct slaveirq_dev_data *data = pdata->irq_data;
257 if (!pdata->irq_data || data->irq <= 0)
260 dev_info(data->dev.this_device, "Unregistering %s\n", data->dev.name);
262 free_irq(data->irq, data);
263 misc_deregister(&data->dev);
264 kfree(pdata->irq_data);
265 pdata->irq_data = NULL;