uio_pdrv_genirq.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include <linux/pm_runtime.h>
  23. #define DRIVER_NAME "uio_pdrv_genirq"
  24. struct uio_pdrv_genirq_platdata {
  25. struct uio_info *uioinfo;
  26. spinlock_t lock;
  27. unsigned long flags;
  28. struct platform_device *pdev;
  29. };
  30. static int uio_pdrv_genirq_open(struct uio_info *info, struct inode *inode)
  31. {
  32. struct uio_pdrv_genirq_platdata *priv = info->priv;
  33. /* Wait until the Runtime PM code has woken up the device */
  34. pm_runtime_get_sync(&priv->pdev->dev);
  35. return 0;
  36. }
  37. static int uio_pdrv_genirq_release(struct uio_info *info, struct inode *inode)
  38. {
  39. struct uio_pdrv_genirq_platdata *priv = info->priv;
  40. /* Tell the Runtime PM code that the device has become idle */
  41. pm_runtime_put_sync(&priv->pdev->dev);
  42. return 0;
  43. }
  44. static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
  45. {
  46. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  47. /* Just disable the interrupt in the interrupt controller, and
  48. * remember the state so we can allow user space to enable it later.
  49. */
  50. if (!test_and_set_bit(0, &priv->flags))
  51. disable_irq_nosync(irq);
  52. return IRQ_HANDLED;
  53. }
  54. static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
  55. {
  56. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  57. unsigned long flags;
  58. /* Allow user space to enable and disable the interrupt
  59. * in the interrupt controller, but keep track of the
  60. * state to prevent per-irq depth damage.
  61. *
  62. * Serialize this operation to support multiple tasks.
  63. */
  64. spin_lock_irqsave(&priv->lock, flags);
  65. if (irq_on) {
  66. if (test_and_clear_bit(0, &priv->flags))
  67. enable_irq(dev_info->irq);
  68. } else {
  69. if (!test_and_set_bit(0, &priv->flags))
  70. disable_irq(dev_info->irq);
  71. }
  72. spin_unlock_irqrestore(&priv->lock, flags);
  73. return 0;
  74. }
  75. static int uio_pdrv_genirq_probe(struct platform_device *pdev)
  76. {
  77. struct uio_info *uioinfo = pdev->dev.platform_data;
  78. struct uio_pdrv_genirq_platdata *priv;
  79. struct uio_mem *uiomem;
  80. int ret = -EINVAL;
  81. int i;
  82. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  83. dev_err(&pdev->dev, "missing platform_data\n");
  84. goto bad0;
  85. }
  86. if (uioinfo->handler || uioinfo->irqcontrol ||
  87. uioinfo->irq_flags & IRQF_SHARED) {
  88. dev_err(&pdev->dev, "interrupt configuration error\n");
  89. goto bad0;
  90. }
  91. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  92. if (!priv) {
  93. ret = -ENOMEM;
  94. dev_err(&pdev->dev, "unable to kmalloc\n");
  95. goto bad0;
  96. }
  97. priv->uioinfo = uioinfo;
  98. spin_lock_init(&priv->lock);
  99. priv->flags = 0; /* interrupt is enabled to begin with */
  100. priv->pdev = pdev;
  101. uiomem = &uioinfo->mem[0];
  102. for (i = 0; i < pdev->num_resources; ++i) {
  103. struct resource *r = &pdev->resource[i];
  104. if (r->flags != IORESOURCE_MEM)
  105. continue;
  106. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  107. dev_warn(&pdev->dev, "device has more than "
  108. __stringify(MAX_UIO_MAPS)
  109. " I/O memory resources.\n");
  110. break;
  111. }
  112. uiomem->memtype = UIO_MEM_PHYS;
  113. uiomem->addr = r->start;
  114. uiomem->size = r->end - r->start + 1;
  115. ++uiomem;
  116. }
  117. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  118. uiomem->size = 0;
  119. ++uiomem;
  120. }
  121. /* This driver requires no hardware specific kernel code to handle
  122. * interrupts. Instead, the interrupt handler simply disables the
  123. * interrupt in the interrupt controller. User space is responsible
  124. * for performing hardware specific acknowledge and re-enabling of
  125. * the interrupt in the interrupt controller.
  126. *
  127. * Interrupt sharing is not supported.
  128. */
  129. uioinfo->irq_flags |= IRQF_DISABLED;
  130. uioinfo->handler = uio_pdrv_genirq_handler;
  131. uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
  132. uioinfo->open = uio_pdrv_genirq_open;
  133. uioinfo->release = uio_pdrv_genirq_release;
  134. uioinfo->priv = priv;
  135. /* Enable Runtime PM for this device:
  136. * The device starts in suspended state to allow the hardware to be
  137. * turned off by default. The Runtime PM bus code should power on the
  138. * hardware and enable clocks at open().
  139. */
  140. pm_runtime_enable(&pdev->dev);
  141. ret = uio_register_device(&pdev->dev, priv->uioinfo);
  142. if (ret) {
  143. dev_err(&pdev->dev, "unable to register uio device\n");
  144. goto bad1;
  145. }
  146. platform_set_drvdata(pdev, priv);
  147. return 0;
  148. bad1:
  149. kfree(priv);
  150. pm_runtime_disable(&pdev->dev);
  151. bad0:
  152. return ret;
  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. uio_unregister_device(priv->uioinfo);
  158. pm_runtime_disable(&pdev->dev);
  159. kfree(priv);
  160. return 0;
  161. }
  162. static int uio_pdrv_genirq_runtime_nop(struct device *dev)
  163. {
  164. /* Runtime PM callback shared between ->runtime_suspend()
  165. * and ->runtime_resume(). Simply returns success.
  166. *
  167. * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
  168. * are used at open() and release() time. This allows the
  169. * Runtime PM code to turn off power to the device while the
  170. * device is unused, ie before open() and after release().
  171. *
  172. * This Runtime PM callback does not need to save or restore
  173. * any registers since user space is responsbile for hardware
  174. * register reinitialization after open().
  175. */
  176. return 0;
  177. }
  178. static const struct dev_pm_ops uio_pdrv_genirq_dev_pm_ops = {
  179. .runtime_suspend = uio_pdrv_genirq_runtime_nop,
  180. .runtime_resume = uio_pdrv_genirq_runtime_nop,
  181. };
  182. static struct platform_driver uio_pdrv_genirq = {
  183. .probe = uio_pdrv_genirq_probe,
  184. .remove = uio_pdrv_genirq_remove,
  185. .driver = {
  186. .name = DRIVER_NAME,
  187. .owner = THIS_MODULE,
  188. .pm = &uio_pdrv_genirq_dev_pm_ops,
  189. },
  190. };
  191. static int __init uio_pdrv_genirq_init(void)
  192. {
  193. return platform_driver_register(&uio_pdrv_genirq);
  194. }
  195. static void __exit uio_pdrv_genirq_exit(void)
  196. {
  197. platform_driver_unregister(&uio_pdrv_genirq);
  198. }
  199. module_init(uio_pdrv_genirq_init);
  200. module_exit(uio_pdrv_genirq_exit);
  201. MODULE_AUTHOR("Magnus Damm");
  202. MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
  203. MODULE_LICENSE("GPL v2");
  204. MODULE_ALIAS("platform:" DRIVER_NAME);