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;
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);
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;
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);
60 if (test_and_clear_bit(0, &priv->flags))
61 enable_irq(dev_info->irq);
63 if (!test_and_set_bit(0, &priv->flags))
64 disable_irq(dev_info->irq);
66 spin_unlock_irqrestore(&priv->lock, flags);
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;
79 if (!uioinfo || !uioinfo->name || !uioinfo->version) {
80 dev_err(&pdev->dev, "missing platform_data\n");
84 if (uioinfo->handler || uioinfo->irqcontrol || uioinfo->irq_flags) {
85 dev_err(&pdev->dev, "interrupt configuration error\n");
89 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
92 dev_err(&pdev->dev, "unable to kmalloc\n");
96 priv->uioinfo = uioinfo;
97 spin_lock_init(&priv->lock);
98 priv->flags = 0; /* interrupt is enabled to begin with */
100 uiomem = &uioinfo->mem[0];
102 for (i = 0; i < pdev->num_resources; ++i) {
103 struct resource *r = &pdev->resource[i];
105 if (r->flags != IORESOURCE_MEM)
108 if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
109 dev_warn(&pdev->dev, "device has more than "
110 __stringify(MAX_UIO_MAPS)
111 " I/O memory resources.\n");
115 uiomem->memtype = UIO_MEM_PHYS;
116 uiomem->addr = r->start;
117 uiomem->size = r->end - r->start + 1;
121 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
126 /* This driver requires no hardware specific kernel code to handle
127 * interrupts. Instead, the interrupt handler simply disables the
128 * interrupt in the interrupt controller. User space is responsible
129 * for performing hardware specific acknowledge and re-enabling of
130 * the interrupt in the interrupt controller.
132 * Interrupt sharing is not supported.
135 uioinfo->irq_flags = IRQF_DISABLED;
136 uioinfo->handler = uio_pdrv_genirq_handler;
137 uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
138 uioinfo->priv = priv;
140 ret = uio_register_device(&pdev->dev, priv->uioinfo);
142 dev_err(&pdev->dev, "unable to register uio device\n");
146 platform_set_drvdata(pdev, priv);
154 static int uio_pdrv_genirq_remove(struct platform_device *pdev)
156 struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
158 uio_unregister_device(priv->uioinfo);
163 static struct platform_driver uio_pdrv_genirq = {
164 .probe = uio_pdrv_genirq_probe,
165 .remove = uio_pdrv_genirq_remove,
168 .owner = THIS_MODULE,
172 static int __init uio_pdrv_genirq_init(void)
174 return platform_driver_register(&uio_pdrv_genirq);
177 static void __exit uio_pdrv_genirq_exit(void)
179 platform_driver_unregister(&uio_pdrv_genirq);
182 module_init(uio_pdrv_genirq_init);
183 module_exit(uio_pdrv_genirq_exit);
185 MODULE_AUTHOR("Magnus Damm");
186 MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
187 MODULE_LICENSE("GPL v2");
188 MODULE_ALIAS("platform:" DRIVER_NAME);