ar9170: atomic pending A-MPDU counter
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / uio / uio_pdrv_genirq.c
blob02347c57357d20511e3e0e25f34572ab053078c0
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>
23 #include <linux/pm_runtime.h>
25 #define DRIVER_NAME "uio_pdrv_genirq"
27 struct uio_pdrv_genirq_platdata {
28 struct uio_info *uioinfo;
29 spinlock_t lock;
30 unsigned long flags;
31 struct platform_device *pdev;
34 static int uio_pdrv_genirq_open(struct uio_info *info, struct inode *inode)
36 struct uio_pdrv_genirq_platdata *priv = info->priv;
38 /* Wait until the Runtime PM code has woken up the device */
39 pm_runtime_get_sync(&priv->pdev->dev);
40 return 0;
43 static int uio_pdrv_genirq_release(struct uio_info *info, struct inode *inode)
45 struct uio_pdrv_genirq_platdata *priv = info->priv;
47 /* Tell the Runtime PM code that the device has become idle */
48 pm_runtime_put_sync(&priv->pdev->dev);
49 return 0;
52 static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
54 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
56 /* Just disable the interrupt in the interrupt controller, and
57 * remember the state so we can allow user space to enable it later.
60 if (!test_and_set_bit(0, &priv->flags))
61 disable_irq_nosync(irq);
63 return IRQ_HANDLED;
66 static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
68 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
69 unsigned long flags;
71 /* Allow user space to enable and disable the interrupt
72 * in the interrupt controller, but keep track of the
73 * state to prevent per-irq depth damage.
75 * Serialize this operation to support multiple tasks.
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);
86 spin_unlock_irqrestore(&priv->lock, flags);
88 return 0;
91 static int uio_pdrv_genirq_probe(struct platform_device *pdev)
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;
99 if (!uioinfo || !uioinfo->name || !uioinfo->version) {
100 dev_err(&pdev->dev, "missing platform_data\n");
101 goto bad0;
104 if (uioinfo->handler || uioinfo->irqcontrol ||
105 uioinfo->irq_flags & IRQF_SHARED) {
106 dev_err(&pdev->dev, "interrupt configuration error\n");
107 goto bad0;
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;
117 priv->uioinfo = uioinfo;
118 spin_lock_init(&priv->lock);
119 priv->flags = 0; /* interrupt is enabled to begin with */
120 priv->pdev = pdev;
122 uiomem = &uioinfo->mem[0];
124 for (i = 0; i < pdev->num_resources; ++i) {
125 struct resource *r = &pdev->resource[i];
127 if (r->flags != IORESOURCE_MEM)
128 continue;
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;
137 uiomem->memtype = UIO_MEM_PHYS;
138 uiomem->addr = r->start;
139 uiomem->size = r->end - r->start + 1;
140 ++uiomem;
143 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
144 uiomem->size = 0;
145 ++uiomem;
148 /* This driver requires no hardware specific kernel code to handle
149 * interrupts. Instead, the interrupt handler simply disables the
150 * interrupt in the interrupt controller. User space is responsible
151 * for performing hardware specific acknowledge and re-enabling of
152 * the interrupt in the interrupt controller.
154 * Interrupt sharing is not supported.
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;
164 /* Enable Runtime PM for this device:
165 * The device starts in suspended state to allow the hardware to be
166 * turned off by default. The Runtime PM bus code should power on the
167 * hardware and enable clocks at open().
169 pm_runtime_enable(&pdev->dev);
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;
177 platform_set_drvdata(pdev, priv);
178 return 0;
179 bad1:
180 kfree(priv);
181 bad0:
182 return ret;
185 static int uio_pdrv_genirq_remove(struct platform_device *pdev)
187 struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
189 uio_unregister_device(priv->uioinfo);
190 pm_runtime_disable(&pdev->dev);
191 kfree(priv);
192 return 0;
195 static int uio_pdrv_genirq_runtime_nop(struct device *dev)
197 /* Runtime PM callback shared between ->runtime_suspend()
198 * and ->runtime_resume(). Simply returns success.
200 * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
201 * are used at open() and release() time. This allows the
202 * Runtime PM code to turn off power to the device while the
203 * device is unused, ie before open() and after release().
205 * This Runtime PM callback does not need to save or restore
206 * any registers since user space is responsbile for hardware
207 * register reinitialization after open().
209 return 0;
212 static struct dev_pm_ops uio_pdrv_genirq_dev_pm_ops = {
213 .runtime_suspend = uio_pdrv_genirq_runtime_nop,
214 .runtime_resume = uio_pdrv_genirq_runtime_nop,
217 static struct platform_driver uio_pdrv_genirq = {
218 .probe = uio_pdrv_genirq_probe,
219 .remove = uio_pdrv_genirq_remove,
220 .driver = {
221 .name = DRIVER_NAME,
222 .owner = THIS_MODULE,
223 .pm = &uio_pdrv_genirq_dev_pm_ops,
227 static int __init uio_pdrv_genirq_init(void)
229 return platform_driver_register(&uio_pdrv_genirq);
232 static void __exit uio_pdrv_genirq_exit(void)
234 platform_driver_unregister(&uio_pdrv_genirq);
237 module_init(uio_pdrv_genirq_init);
238 module_exit(uio_pdrv_genirq_exit);
240 MODULE_AUTHOR("Magnus Damm");
241 MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
242 MODULE_LICENSE("GPL v2");
243 MODULE_ALIAS("platform:" DRIVER_NAME);