1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/platform_device.h>
18#include <linux/uio_driver.h>
19#include <linux/spinlock.h>
20#include <linux/bitops.h>
21#include <linux/interrupt.h>
22#include <linux/stringify.h>
23#include <linux/pm_runtime.h>
24
25#define DRIVER_NAME "uio_pdrv_genirq"
26
27struct uio_pdrv_genirq_platdata {
28 struct uio_info *uioinfo;
29 spinlock_t lock;
30 unsigned long flags;
31 struct platform_device *pdev;
32};
33
34static int uio_pdrv_genirq_open(struct uio_info *info, struct inode *inode)
35{
36 struct uio_pdrv_genirq_platdata *priv = info->priv;
37
38
39 pm_runtime_get_sync(&priv->pdev->dev);
40 return 0;
41}
42
43static int uio_pdrv_genirq_release(struct uio_info *info, struct inode *inode)
44{
45 struct uio_pdrv_genirq_platdata *priv = info->priv;
46
47
48 pm_runtime_put_sync(&priv->pdev->dev);
49 return 0;
50}
51
52static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
53{
54 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
55
56
57
58
59
60 if (!test_and_set_bit(0, &priv->flags))
61 disable_irq_nosync(irq);
62
63 return IRQ_HANDLED;
64}
65
66static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
67{
68 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
69 unsigned long flags;
70
71
72
73
74
75
76
77
78 spin_lock_irqsave(&priv->lock, flags);
79 if (irq_on) {
80 if (test_and_clear_bit(0, &priv->flags))
81 enable_irq(dev_info->irq);
82 } else {
83 if (!test_and_set_bit(0, &priv->flags))
84 disable_irq(dev_info->irq);
85 }
86 spin_unlock_irqrestore(&priv->lock, flags);
87
88 return 0;
89}
90
91static int uio_pdrv_genirq_probe(struct platform_device *pdev)
92{
93 struct uio_info *uioinfo = pdev->dev.platform_data;
94 struct uio_pdrv_genirq_platdata *priv;
95 struct uio_mem *uiomem;
96 int ret = -EINVAL;
97 int i;
98
99 if (!uioinfo || !uioinfo->name || !uioinfo->version) {
100 dev_err(&pdev->dev, "missing platform_data\n");
101 goto bad0;
102 }
103
104 if (uioinfo->handler || uioinfo->irqcontrol ||
105 uioinfo->irq_flags & IRQF_SHARED) {
106 dev_err(&pdev->dev, "interrupt configuration error\n");
107 goto bad0;
108 }
109
110 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
111 if (!priv) {
112 ret = -ENOMEM;
113 dev_err(&pdev->dev, "unable to kmalloc\n");
114 goto bad0;
115 }
116
117 priv->uioinfo = uioinfo;
118 spin_lock_init(&priv->lock);
119 priv->flags = 0;
120 priv->pdev = pdev;
121
122 uiomem = &uioinfo->mem[0];
123
124 for (i = 0; i < pdev->num_resources; ++i) {
125 struct resource *r = &pdev->resource[i];
126
127 if (r->flags != IORESOURCE_MEM)
128 continue;
129
130 if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
131 dev_warn(&pdev->dev, "device has more than "
132 __stringify(MAX_UIO_MAPS)
133 " I/O memory resources.\n");
134 break;
135 }
136
137 uiomem->memtype = UIO_MEM_PHYS;
138 uiomem->addr = r->start;
139 uiomem->size = r->end - r->start + 1;
140 ++uiomem;
141 }
142
143 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
144 uiomem->size = 0;
145 ++uiomem;
146 }
147
148
149
150
151
152
153
154
155
156
157 uioinfo->irq_flags |= IRQF_DISABLED;
158 uioinfo->handler = uio_pdrv_genirq_handler;
159 uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
160 uioinfo->open = uio_pdrv_genirq_open;
161 uioinfo->release = uio_pdrv_genirq_release;
162 uioinfo->priv = priv;
163
164
165
166
167
168
169 pm_runtime_enable(&pdev->dev);
170
171 ret = uio_register_device(&pdev->dev, priv->uioinfo);
172 if (ret) {
173 dev_err(&pdev->dev, "unable to register uio device\n");
174 goto bad1;
175 }
176
177 platform_set_drvdata(pdev, priv);
178 return 0;
179 bad1:
180 kfree(priv);
181 pm_runtime_disable(&pdev->dev);
182 bad0:
183 return ret;
184}
185
186static int uio_pdrv_genirq_remove(struct platform_device *pdev)
187{
188 struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
189
190 uio_unregister_device(priv->uioinfo);
191 pm_runtime_disable(&pdev->dev);
192 kfree(priv);
193 return 0;
194}
195
196static int uio_pdrv_genirq_runtime_nop(struct device *dev)
197{
198
199
200
201
202
203
204
205
206
207
208
209
210 return 0;
211}
212
213static struct dev_pm_ops uio_pdrv_genirq_dev_pm_ops = {
214 .runtime_suspend = uio_pdrv_genirq_runtime_nop,
215 .runtime_resume = uio_pdrv_genirq_runtime_nop,
216};
217
218static struct platform_driver uio_pdrv_genirq = {
219 .probe = uio_pdrv_genirq_probe,
220 .remove = uio_pdrv_genirq_remove,
221 .driver = {
222 .name = DRIVER_NAME,
223 .owner = THIS_MODULE,
224 .pm = &uio_pdrv_genirq_dev_pm_ops,
225 },
226};
227
228static int __init uio_pdrv_genirq_init(void)
229{
230 return platform_driver_register(&uio_pdrv_genirq);
231}
232
233static void __exit uio_pdrv_genirq_exit(void)
234{
235 platform_driver_unregister(&uio_pdrv_genirq);
236}
237
238module_init(uio_pdrv_genirq_init);
239module_exit(uio_pdrv_genirq_exit);
240
241MODULE_AUTHOR("Magnus Damm");
242MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
243MODULE_LICENSE("GPL v2");
244MODULE_ALIAS("platform:" DRIVER_NAME);
245