uio_pdrv_genirq.c 6.4 KB

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