uio_pdrv_genirq.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <linux/platform_device.h>
  17. #include <linux/uio_driver.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/bitops.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/stringify.h>
  22. #define DRIVER_NAME "uio_pdrv_genirq"
  23. struct uio_pdrv_genirq_platdata {
  24. struct uio_info *uioinfo;
  25. spinlock_t lock;
  26. unsigned long flags;
  27. };
  28. static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
  29. {
  30. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  31. /* Just disable the interrupt in the interrupt controller, and
  32. * remember the state so we can allow user space to enable it later.
  33. */
  34. if (!test_and_set_bit(0, &priv->flags))
  35. disable_irq_nosync(irq);
  36. return IRQ_HANDLED;
  37. }
  38. static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
  39. {
  40. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  41. unsigned long flags;
  42. /* Allow user space to enable and disable the interrupt
  43. * in the interrupt controller, but keep track of the
  44. * state to prevent per-irq depth damage.
  45. *
  46. * Serialize this operation to support multiple tasks.
  47. */
  48. spin_lock_irqsave(&priv->lock, flags);
  49. if (irq_on) {
  50. if (test_and_clear_bit(0, &priv->flags))
  51. enable_irq(dev_info->irq);
  52. } else {
  53. if (!test_and_set_bit(0, &priv->flags))
  54. disable_irq(dev_info->irq);
  55. }
  56. spin_unlock_irqrestore(&priv->lock, flags);
  57. return 0;
  58. }
  59. static int uio_pdrv_genirq_probe(struct platform_device *pdev)
  60. {
  61. struct uio_info *uioinfo = pdev->dev.platform_data;
  62. struct uio_pdrv_genirq_platdata *priv;
  63. struct uio_mem *uiomem;
  64. int ret = -EINVAL;
  65. int i;
  66. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  67. dev_err(&pdev->dev, "missing platform_data\n");
  68. goto bad0;
  69. }
  70. if (uioinfo->handler || uioinfo->irqcontrol ||
  71. uioinfo->irq_flags & IRQF_SHARED) {
  72. dev_err(&pdev->dev, "interrupt configuration error\n");
  73. goto bad0;
  74. }
  75. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  76. if (!priv) {
  77. ret = -ENOMEM;
  78. dev_err(&pdev->dev, "unable to kmalloc\n");
  79. goto bad0;
  80. }
  81. priv->uioinfo = uioinfo;
  82. spin_lock_init(&priv->lock);
  83. priv->flags = 0; /* interrupt is enabled to begin with */
  84. uiomem = &uioinfo->mem[0];
  85. for (i = 0; i < pdev->num_resources; ++i) {
  86. struct resource *r = &pdev->resource[i];
  87. if (r->flags != IORESOURCE_MEM)
  88. continue;
  89. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  90. dev_warn(&pdev->dev, "device has more than "
  91. __stringify(MAX_UIO_MAPS)
  92. " I/O memory resources.\n");
  93. break;
  94. }
  95. uiomem->memtype = UIO_MEM_PHYS;
  96. uiomem->addr = r->start;
  97. uiomem->size = r->end - r->start + 1;
  98. ++uiomem;
  99. }
  100. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  101. uiomem->size = 0;
  102. ++uiomem;
  103. }
  104. /* This driver requires no hardware specific kernel code to handle
  105. * interrupts. Instead, the interrupt handler simply disables the
  106. * interrupt in the interrupt controller. User space is responsible
  107. * for performing hardware specific acknowledge and re-enabling of
  108. * the interrupt in the interrupt controller.
  109. *
  110. * Interrupt sharing is not supported.
  111. */
  112. uioinfo->irq_flags |= IRQF_DISABLED;
  113. uioinfo->handler = uio_pdrv_genirq_handler;
  114. uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
  115. uioinfo->priv = priv;
  116. ret = uio_register_device(&pdev->dev, priv->uioinfo);
  117. if (ret) {
  118. dev_err(&pdev->dev, "unable to register uio device\n");
  119. goto bad1;
  120. }
  121. platform_set_drvdata(pdev, priv);
  122. return 0;
  123. bad1:
  124. kfree(priv);
  125. bad0:
  126. return ret;
  127. }
  128. static int uio_pdrv_genirq_remove(struct platform_device *pdev)
  129. {
  130. struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
  131. uio_unregister_device(priv->uioinfo);
  132. kfree(priv);
  133. return 0;
  134. }
  135. static struct platform_driver uio_pdrv_genirq = {
  136. .probe = uio_pdrv_genirq_probe,
  137. .remove = uio_pdrv_genirq_remove,
  138. .driver = {
  139. .name = DRIVER_NAME,
  140. .owner = THIS_MODULE,
  141. },
  142. };
  143. static int __init uio_pdrv_genirq_init(void)
  144. {
  145. return platform_driver_register(&uio_pdrv_genirq);
  146. }
  147. static void __exit uio_pdrv_genirq_exit(void)
  148. {
  149. platform_driver_unregister(&uio_pdrv_genirq);
  150. }
  151. module_init(uio_pdrv_genirq_init);
  152. module_exit(uio_pdrv_genirq_exit);
  153. MODULE_AUTHOR("Magnus Damm");
  154. MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
  155. MODULE_LICENSE("GPL v2");
  156. MODULE_ALIAS("platform:" DRIVER_NAME);