blob: dfbfd7e2ac088887cccfc35aac853cf9cbd07785 [file] [log] [blame]
Ralf Baechle35189fa2006-06-18 16:39:46 +01001/*
2 * Copyright (C) 2005 by Basler Vision Technologies AG
3 * Author: Thomas Koeller <thomas.koeller@baslerweb.com>
4 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Ralf Baechle35189fa2006-06-18 16:39:46 +010020#include <linux/compiler.h>
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/sched.h>
24#include <linux/wait.h>
25#include <linux/poll.h>
26#include <linux/interrupt.h>
27#include <linux/platform_device.h>
28#include <linux/miscdevice.h>
Arnd Bergmann52e7c5e2008-05-20 19:15:36 +020029#include <linux/smp_lock.h>
Ralf Baechle35189fa2006-06-18 16:39:46 +010030
31#include "excite_iodev.h"
32
33
34
35static const struct resource *iodev_get_resource(struct platform_device *, const char *, unsigned int);
Ming Lei7a192ec2009-02-06 23:40:12 +080036static int __init iodev_probe(struct platform_device *);
37static int __exit iodev_remove(struct platform_device *);
Ralf Baechle35189fa2006-06-18 16:39:46 +010038static int iodev_open(struct inode *, struct file *);
39static int iodev_release(struct inode *, struct file *);
40static ssize_t iodev_read(struct file *, char __user *, size_t s, loff_t *);
41static unsigned int iodev_poll(struct file *, struct poll_table_struct *);
Ralf Baechle937a8012006-10-07 19:44:33 +010042static irqreturn_t iodev_irqhdl(int, void *);
Ralf Baechle35189fa2006-06-18 16:39:46 +010043
44
45
46static const char iodev_name[] = "iodev";
47static unsigned int iodev_irq;
48static DECLARE_WAIT_QUEUE_HEAD(wq);
49
50
51
Jan Engelhardt12323ca2008-01-22 20:42:33 +010052static const struct file_operations fops =
Ralf Baechle35189fa2006-06-18 16:39:46 +010053{
54 .owner = THIS_MODULE,
55 .open = iodev_open,
56 .release = iodev_release,
57 .read = iodev_read,
58 .poll = iodev_poll
59};
60
61static struct miscdevice miscdev =
62{
63 .minor = MISC_DYNAMIC_MINOR,
64 .name = iodev_name,
65 .fops = &fops
66};
67
Ming Lei7a192ec2009-02-06 23:40:12 +080068static struct platform_driver iodev_driver = {
69 .driver = {
70 .name = iodev_name,
71 .owner = THIS_MODULE,
72 },
Ralf Baechle35189fa2006-06-18 16:39:46 +010073 .probe = iodev_probe,
Ming Lei7a192ec2009-02-06 23:40:12 +080074 .remove = __devexit_p(iodev_remove),
Ralf Baechle35189fa2006-06-18 16:39:46 +010075};
76
77
78
79static const struct resource *
80iodev_get_resource(struct platform_device *pdv, const char *name,
81 unsigned int type)
82{
83 char buf[80];
84 if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
85 return NULL;
86 return platform_get_resource_byname(pdv, type, buf);
87}
88
89
90
91/* No hotplugging on the platform bus - use __init */
Ming Lei7a192ec2009-02-06 23:40:12 +080092static int __init iodev_probe(struct platform_device *dev)
Ralf Baechle35189fa2006-06-18 16:39:46 +010093{
Ralf Baechle35189fa2006-06-18 16:39:46 +010094 const struct resource * const ri =
Ming Lei7a192ec2009-02-06 23:40:12 +080095 iodev_get_resource(dev, IODEV_RESOURCE_IRQ, IORESOURCE_IRQ);
Ralf Baechle35189fa2006-06-18 16:39:46 +010096
97 if (unlikely(!ri))
98 return -ENXIO;
99
100 iodev_irq = ri->start;
101 return misc_register(&miscdev);
102}
103
104
105
Ming Lei7a192ec2009-02-06 23:40:12 +0800106static int __exit iodev_remove(struct platform_device *dev)
Ralf Baechle35189fa2006-06-18 16:39:46 +0100107{
108 return misc_deregister(&miscdev);
109}
110
Ralf Baechle35189fa2006-06-18 16:39:46 +0100111static int iodev_open(struct inode *i, struct file *f)
112{
Arnd Bergmann52e7c5e2008-05-20 19:15:36 +0200113 int ret;
114
115 lock_kernel();
116 ret = request_irq(iodev_irq, iodev_irqhdl, IRQF_DISABLED,
Ralf Baechle35189fa2006-06-18 16:39:46 +0100117 iodev_name, &miscdev);
Arnd Bergmann52e7c5e2008-05-20 19:15:36 +0200118 unlock_kernel();
119
120 return ret;
Ralf Baechle35189fa2006-06-18 16:39:46 +0100121}
122
Ralf Baechle35189fa2006-06-18 16:39:46 +0100123static int iodev_release(struct inode *i, struct file *f)
124{
125 free_irq(iodev_irq, &miscdev);
126 return 0;
127}
128
129
130
131
132static ssize_t
133iodev_read(struct file *f, char __user *d, size_t s, loff_t *o)
134{
135 ssize_t ret;
136 DEFINE_WAIT(w);
137
138 prepare_to_wait(&wq, &w, TASK_INTERRUPTIBLE);
139 if (!signal_pending(current))
140 schedule();
141 ret = signal_pending(current) ? -ERESTARTSYS : 0;
142 finish_wait(&wq, &w);
143 return ret;
144}
145
146
147static unsigned int iodev_poll(struct file *f, struct poll_table_struct *p)
148{
149 poll_wait(f, &wq, p);
150 return POLLOUT | POLLWRNORM;
151}
152
Ralf Baechle937a8012006-10-07 19:44:33 +0100153static irqreturn_t iodev_irqhdl(int irq, void *ctxt)
Ralf Baechle35189fa2006-06-18 16:39:46 +0100154{
155 wake_up(&wq);
Ralf Baechle937a8012006-10-07 19:44:33 +0100156
Ralf Baechle35189fa2006-06-18 16:39:46 +0100157 return IRQ_HANDLED;
158}
159
Ralf Baechle35189fa2006-06-18 16:39:46 +0100160static int __init iodev_init_module(void)
161{
Ming Lei7a192ec2009-02-06 23:40:12 +0800162 return platform_driver_register(&iodev_driver);
Ralf Baechle35189fa2006-06-18 16:39:46 +0100163}
164
165
166
167static void __exit iodev_cleanup_module(void)
168{
Ming Lei7a192ec2009-02-06 23:40:12 +0800169 platform_driver_unregister(&iodev_driver);
Ralf Baechle35189fa2006-06-18 16:39:46 +0100170}
171
172module_init(iodev_init_module);
173module_exit(iodev_cleanup_module);
174
175
176
177MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
178MODULE_DESCRIPTION("Basler eXcite i/o interrupt handler");
179MODULE_VERSION("0.0");
180MODULE_LICENSE("GPL");