blob: 3f06818cf9fa3dc1628f794ec80fc2c2e22d88eb [file] [log] [blame]
Magnus Dammc767db02008-07-11 18:55:27 +09001/*
2 * drivers/uio/uio_pdrv_genirq.c
3 *
4 * Userspace I/O platform driver with generic IRQ handling code.
5 *
6 * Copyright (C) 2008 Magnus Damm
7 *
8 * Based on uio_pdrv.c by Uwe Kleine-Koenig,
9 * Copyright (C) 2008 by Digi International Inc.
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
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
24#define DRIVER_NAME "uio_pdrv_genirq"
25
26struct uio_pdrv_genirq_platdata {
27 struct uio_info *uioinfo;
28 spinlock_t lock;
29 unsigned long flags;
30};
31
32static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
33{
34 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
35
36 /* Just disable the interrupt in the interrupt controller, and
37 * remember the state so we can allow user space to enable it later.
38 */
39
40 if (!test_and_set_bit(0, &priv->flags))
41 disable_irq_nosync(irq);
42
43 return IRQ_HANDLED;
44}
45
46static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
47{
48 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
49 unsigned long flags;
50
51 /* Allow user space to enable and disable the interrupt
52 * in the interrupt controller, but keep track of the
53 * state to prevent per-irq depth damage.
54 *
55 * Serialize this operation to support multiple tasks.
56 */
57
58 spin_lock_irqsave(&priv->lock, flags);
59 if (irq_on) {
60 if (test_and_clear_bit(0, &priv->flags))
61 enable_irq(dev_info->irq);
62 } else {
63 if (!test_and_set_bit(0, &priv->flags))
64 disable_irq(dev_info->irq);
65 }
66 spin_unlock_irqrestore(&priv->lock, flags);
67
68 return 0;
69}
70
71static int uio_pdrv_genirq_probe(struct platform_device *pdev)
72{
73 struct uio_info *uioinfo = pdev->dev.platform_data;
74 struct uio_pdrv_genirq_platdata *priv;
75 struct uio_mem *uiomem;
76 int ret = -EINVAL;
77 int i;
78
79 if (!uioinfo || !uioinfo->name || !uioinfo->version) {
80 dev_err(&pdev->dev, "missing platform_data\n");
81 goto bad0;
82 }
83
Mike Frysingere543ae82008-10-29 18:35:52 -040084 if (uioinfo->handler || uioinfo->irqcontrol ||
85 uioinfo->irq_flags & IRQF_SHARED) {
Magnus Dammc767db02008-07-11 18:55:27 +090086 dev_err(&pdev->dev, "interrupt configuration error\n");
87 goto bad0;
88 }
89
90 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
91 if (!priv) {
92 ret = -ENOMEM;
93 dev_err(&pdev->dev, "unable to kmalloc\n");
94 goto bad0;
95 }
96
97 priv->uioinfo = uioinfo;
98 spin_lock_init(&priv->lock);
99 priv->flags = 0; /* interrupt is enabled to begin with */
100
101 uiomem = &uioinfo->mem[0];
102
103 for (i = 0; i < pdev->num_resources; ++i) {
104 struct resource *r = &pdev->resource[i];
105
106 if (r->flags != IORESOURCE_MEM)
107 continue;
108
109 if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
110 dev_warn(&pdev->dev, "device has more than "
111 __stringify(MAX_UIO_MAPS)
112 " I/O memory resources.\n");
113 break;
114 }
115
116 uiomem->memtype = UIO_MEM_PHYS;
117 uiomem->addr = r->start;
118 uiomem->size = r->end - r->start + 1;
119 ++uiomem;
120 }
121
122 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
123 uiomem->size = 0;
124 ++uiomem;
125 }
126
127 /* This driver requires no hardware specific kernel code to handle
128 * interrupts. Instead, the interrupt handler simply disables the
129 * interrupt in the interrupt controller. User space is responsible
130 * for performing hardware specific acknowledge and re-enabling of
131 * the interrupt in the interrupt controller.
132 *
133 * Interrupt sharing is not supported.
134 */
135
Mike Frysingere543ae82008-10-29 18:35:52 -0400136 uioinfo->irq_flags |= IRQF_DISABLED;
Magnus Dammc767db02008-07-11 18:55:27 +0900137 uioinfo->handler = uio_pdrv_genirq_handler;
138 uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
139 uioinfo->priv = priv;
140
141 ret = uio_register_device(&pdev->dev, priv->uioinfo);
142 if (ret) {
143 dev_err(&pdev->dev, "unable to register uio device\n");
144 goto bad1;
145 }
146
147 platform_set_drvdata(pdev, priv);
148 return 0;
149 bad1:
150 kfree(priv);
151 bad0:
152 return ret;
153}
154
155static int uio_pdrv_genirq_remove(struct platform_device *pdev)
156{
157 struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
158
159 uio_unregister_device(priv->uioinfo);
160 kfree(priv);
161 return 0;
162}
163
164static struct platform_driver uio_pdrv_genirq = {
165 .probe = uio_pdrv_genirq_probe,
166 .remove = uio_pdrv_genirq_remove,
167 .driver = {
168 .name = DRIVER_NAME,
169 .owner = THIS_MODULE,
170 },
171};
172
173static int __init uio_pdrv_genirq_init(void)
174{
175 return platform_driver_register(&uio_pdrv_genirq);
176}
177
178static void __exit uio_pdrv_genirq_exit(void)
179{
180 platform_driver_unregister(&uio_pdrv_genirq);
181}
182
183module_init(uio_pdrv_genirq_init);
184module_exit(uio_pdrv_genirq_exit);
185
186MODULE_AUTHOR("Magnus Damm");
187MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
188MODULE_LICENSE("GPL v2");
189MODULE_ALIAS("platform:" DRIVER_NAME);