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
||
85 uioinfo
->irq_flags
& IRQF_SHARED
) {
86 dev_err(&pdev
->dev
, "interrupt configuration error\n");
90 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
93 dev_err(&pdev
->dev
, "unable to kmalloc\n");
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
)
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");
116 uiomem
->memtype
= UIO_MEM_PHYS
;
117 uiomem
->addr
= r
->start
;
118 uiomem
->size
= r
->end
- r
->start
+ 1;
122 while (uiomem
< &uioinfo
->mem
[MAX_UIO_MAPS
]) {
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
);
143 dev_err(&pdev
->dev
, "unable to register uio device\n");
147 platform_set_drvdata(pdev
, priv
);
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
);
164 static struct platform_driver uio_pdrv_genirq
= {
165 .probe
= uio_pdrv_genirq_probe
,
166 .remove
= uio_pdrv_genirq_remove
,
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
);