1 /* 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 26 struct uio_pdrv_genirq_platdata { 27 struct uio_info *uioinfo; 28 spinlock_t lock; 29 unsigned long flags; 30 }; 31 32 static 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 46 static 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 71 static 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 84 if (uioinfo->handler || uioinfo->irqcontrol || uioinfo->irq_flags) { 85 dev_err(&pdev->dev, "interrupt configuration error\n"); 86 goto bad0; 87 } 88 89 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 90 if (!priv) { 91 ret = -ENOMEM; 92 dev_err(&pdev->dev, "unable to kmalloc\n"); 93 goto bad0; 94 } 95 96 priv->uioinfo = uioinfo; 97 spin_lock_init(&priv->lock); 98 priv->flags = 0; /* interrupt is enabled to begin with */ 99 100 uiomem = &uioinfo->mem[0]; 101 102 for (i = 0; i < pdev->num_resources; ++i) { 103 struct resource *r = &pdev->resource[i]; 104 105 if (r->flags != IORESOURCE_MEM) 106 continue; 107 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"); 112 break; 113 } 114 115 uiomem->memtype = UIO_MEM_PHYS; 116 uiomem->addr = r->start; 117 uiomem->size = r->end - r->start + 1; 118 ++uiomem; 119 } 120 121 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) { 122 uiomem->size = 0; 123 ++uiomem; 124 } 125 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. 131 * 132 * Interrupt sharing is not supported. 133 */ 134 135 uioinfo->irq_flags = IRQF_DISABLED; 136 uioinfo->handler = uio_pdrv_genirq_handler; 137 uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol; 138 uioinfo->priv = priv; 139 140 ret = uio_register_device(&pdev->dev, priv->uioinfo); 141 if (ret) { 142 dev_err(&pdev->dev, "unable to register uio device\n"); 143 goto bad1; 144 } 145 146 platform_set_drvdata(pdev, priv); 147 return 0; 148 bad1: 149 kfree(priv); 150 bad0: 151 return ret; 152 } 153 154 static int uio_pdrv_genirq_remove(struct platform_device *pdev) 155 { 156 struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev); 157 158 uio_unregister_device(priv->uioinfo); 159 kfree(priv); 160 return 0; 161 } 162 163 static struct platform_driver uio_pdrv_genirq = { 164 .probe = uio_pdrv_genirq_probe, 165 .remove = uio_pdrv_genirq_remove, 166 .driver = { 167 .name = DRIVER_NAME, 168 .owner = THIS_MODULE, 169 }, 170 }; 171 172 static int __init uio_pdrv_genirq_init(void) 173 { 174 return platform_driver_register(&uio_pdrv_genirq); 175 } 176 177 static void __exit uio_pdrv_genirq_exit(void) 178 { 179 platform_driver_unregister(&uio_pdrv_genirq); 180 } 181 182 module_init(uio_pdrv_genirq_init); 183 module_exit(uio_pdrv_genirq_exit); 184 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); 189