uio_pdrv_genirq.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 || uioinfo->irq_flags) {
  71. dev_err(&pdev->dev, "interrupt configuration error\n");
  72. goto bad0;
  73. }
  74. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  75. if (!priv) {
  76. ret = -ENOMEM;
  77. dev_err(&pdev->dev, "unable to kmalloc\n");
  78. goto bad0;
  79. }
  80. priv->uioinfo = uioinfo;
  81. spin_lock_init(&priv->lock);
  82. priv->flags = 0; /* interrupt is enabled to begin with */
  83. uiomem = &uioinfo->mem[0];
  84. for (i = 0; i < pdev->num_resources; ++i) {
  85. struct resource *r = &pdev->resource[i];
  86. if (r->flags != IORESOURCE_MEM)
  87. continue;
  88. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  89. dev_warn(&pdev->dev, "device has more than "
  90. __stringify(MAX_UIO_MAPS)
  91. " I/O memory resources.\n");
  92. break;
  93. }
  94. uiomem->memtype = UIO_MEM_PHYS;
  95. uiomem->addr = r->start;
  96. uiomem->size = r->end - r->start + 1;
  97. ++uiomem;
  98. }
  99. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  100. uiomem->size = 0;
  101. ++uiomem;
  102. }
  103. /* This driver requires no hardware specific kernel code to handle
  104. * interrupts. Instead, the interrupt handler simply disables the
  105. * interrupt in the interrupt controller. User space is responsible
  106. * for performing hardware specific acknowledge and re-enabling of
  107. * the interrupt in the interrupt controller.
  108. *
  109. * Interrupt sharing is not supported.
  110. */
  111. uioinfo->irq_flags = IRQF_DISABLED;
  112. uioinfo->handler = uio_pdrv_genirq_handler;
  113. uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
  114. uioinfo->priv = priv;
  115. ret = uio_register_device(&pdev->dev, priv->uioinfo);
  116. if (ret) {
  117. dev_err(&pdev->dev, "unable to register uio device\n");
  118. goto bad1;
  119. }
  120. platform_set_drvdata(pdev, priv);
  121. return 0;
  122. bad1:
  123. kfree(priv);
  124. bad0:
  125. return ret;
  126. }
  127. static int uio_pdrv_genirq_remove(struct platform_device *pdev)
  128. {
  129. struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
  130. uio_unregister_device(priv->uioinfo);
  131. kfree(priv);
  132. return 0;
  133. }
  134. static struct platform_driver uio_pdrv_genirq = {
  135. .probe = uio_pdrv_genirq_probe,
  136. .remove = uio_pdrv_genirq_remove,
  137. .driver = {
  138. .name = DRIVER_NAME,
  139. .owner = THIS_MODULE,
  140. },
  141. };
  142. static int __init uio_pdrv_genirq_init(void)
  143. {
  144. return platform_driver_register(&uio_pdrv_genirq);
  145. }
  146. static void __exit uio_pdrv_genirq_exit(void)
  147. {
  148. platform_driver_unregister(&uio_pdrv_genirq);
  149. }
  150. module_init(uio_pdrv_genirq_init);
  151. module_exit(uio_pdrv_genirq_exit);
  152. MODULE_AUTHOR("Magnus Damm");
  153. MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
  154. MODULE_LICENSE("GPL v2");
  155. MODULE_ALIAS("platform:" DRIVER_NAME);