Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / uio / uio_pdrv_genirq.c
blob3f06818cf9fa3dc1628f794ec80fc2c2e22d88eb
1 /*
2 * drivers/uio/uio_pdrv_genirq.c
4 * Userspace I/O platform driver with generic IRQ handling code.
6 * Copyright (C) 2008 Magnus Damm
8 * Based on uio_pdrv.c by Uwe Kleine-Koenig,
9 * Copyright (C) 2008 by Digi International Inc.
10 * All rights reserved.
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.
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>
24 #define DRIVER_NAME "uio_pdrv_genirq"
26 struct uio_pdrv_genirq_platdata {
27 struct uio_info *uioinfo;
28 spinlock_t lock;
29 unsigned long flags;
32 static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
34 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
36 /* Just disable the interrupt in the interrupt controller, and
37 * remember the state so we can allow user space to enable it later.
40 if (!test_and_set_bit(0, &priv->flags))
41 disable_irq_nosync(irq);
43 return IRQ_HANDLED;
46 static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
48 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
49 unsigned long flags;
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.
55 * Serialize this operation to support multiple tasks.
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);
66 spin_unlock_irqrestore(&priv->lock, flags);
68 return 0;
71 static int uio_pdrv_genirq_probe(struct platform_device *pdev)
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;
79 if (!uioinfo || !uioinfo->name || !uioinfo->version) {
80 dev_err(&pdev->dev, "missing platform_data\n");
81 goto bad0;
84 if (uioinfo->handler || uioinfo->irqcontrol ||
85 uioinfo->irq_flags & IRQF_SHARED) {
86 dev_err(&pdev->dev, "interrupt configuration error\n");
87 goto bad0;
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;
97 priv->uioinfo = uioinfo;
98 spin_lock_init(&priv->lock);
99 priv->flags = 0; /* interrupt is enabled to begin with */
101 uiomem = &uioinfo->mem[0];
103 for (i = 0; i < pdev->num_resources; ++i) {
104 struct resource *r = &pdev->resource[i];
106 if (r->flags != IORESOURCE_MEM)
107 continue;
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;
116 uiomem->memtype = UIO_MEM_PHYS;
117 uiomem->addr = r->start;
118 uiomem->size = r->end - r->start + 1;
119 ++uiomem;
122 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
123 uiomem->size = 0;
124 ++uiomem;
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.
133 * Interrupt sharing is not supported.
136 uioinfo->irq_flags |= IRQF_DISABLED;
137 uioinfo->handler = uio_pdrv_genirq_handler;
138 uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
139 uioinfo->priv = priv;
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;
147 platform_set_drvdata(pdev, priv);
148 return 0;
149 bad1:
150 kfree(priv);
151 bad0:
152 return ret;
155 static int uio_pdrv_genirq_remove(struct platform_device *pdev)
157 struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
159 uio_unregister_device(priv->uioinfo);
160 kfree(priv);
161 return 0;
164 static 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,
173 static int __init uio_pdrv_genirq_init(void)
175 return platform_driver_register(&uio_pdrv_genirq);
178 static void __exit uio_pdrv_genirq_exit(void)
180 platform_driver_unregister(&uio_pdrv_genirq);
183 module_init(uio_pdrv_genirq_init);
184 module_exit(uio_pdrv_genirq_exit);
186 MODULE_AUTHOR("Magnus Damm");
187 MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
188 MODULE_LICENSE("GPL v2");
189 MODULE_ALIAS("platform:" DRIVER_NAME);