excite_iodev.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2005 by Basler Vision Technologies AG
  3. * Author: Thomas Koeller <thomas.koeller@baslerweb.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/compiler.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include <linux/poll.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/smp_lock.h>
  29. #include "excite_iodev.h"
  30. static const struct resource *iodev_get_resource(struct platform_device *, const char *, unsigned int);
  31. static int __init iodev_probe(struct platform_device *);
  32. static int __exit iodev_remove(struct platform_device *);
  33. static int iodev_open(struct inode *, struct file *);
  34. static int iodev_release(struct inode *, struct file *);
  35. static ssize_t iodev_read(struct file *, char __user *, size_t s, loff_t *);
  36. static unsigned int iodev_poll(struct file *, struct poll_table_struct *);
  37. static irqreturn_t iodev_irqhdl(int, void *);
  38. static const char iodev_name[] = "iodev";
  39. static unsigned int iodev_irq;
  40. static DECLARE_WAIT_QUEUE_HEAD(wq);
  41. static const struct file_operations fops =
  42. {
  43. .owner = THIS_MODULE,
  44. .open = iodev_open,
  45. .release = iodev_release,
  46. .read = iodev_read,
  47. .poll = iodev_poll
  48. };
  49. static struct miscdevice miscdev =
  50. {
  51. .minor = MISC_DYNAMIC_MINOR,
  52. .name = iodev_name,
  53. .fops = &fops
  54. };
  55. static struct platform_driver iodev_driver = {
  56. .driver = {
  57. .name = iodev_name,
  58. .owner = THIS_MODULE,
  59. },
  60. .probe = iodev_probe,
  61. .remove = __devexit_p(iodev_remove),
  62. };
  63. static const struct resource *
  64. iodev_get_resource(struct platform_device *pdv, const char *name,
  65. unsigned int type)
  66. {
  67. char buf[80];
  68. if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
  69. return NULL;
  70. return platform_get_resource_byname(pdv, type, buf);
  71. }
  72. /* No hotplugging on the platform bus - use __init */
  73. static int __init iodev_probe(struct platform_device *dev)
  74. {
  75. const struct resource * const ri =
  76. iodev_get_resource(dev, IODEV_RESOURCE_IRQ, IORESOURCE_IRQ);
  77. if (unlikely(!ri))
  78. return -ENXIO;
  79. iodev_irq = ri->start;
  80. return misc_register(&miscdev);
  81. }
  82. static int __exit iodev_remove(struct platform_device *dev)
  83. {
  84. return misc_deregister(&miscdev);
  85. }
  86. static int iodev_open(struct inode *i, struct file *f)
  87. {
  88. int ret;
  89. ret = request_irq(iodev_irq, iodev_irqhdl, IRQF_DISABLED,
  90. iodev_name, &miscdev);
  91. return ret;
  92. }
  93. static int iodev_release(struct inode *i, struct file *f)
  94. {
  95. free_irq(iodev_irq, &miscdev);
  96. return 0;
  97. }
  98. static ssize_t
  99. iodev_read(struct file *f, char __user *d, size_t s, loff_t *o)
  100. {
  101. ssize_t ret;
  102. DEFINE_WAIT(w);
  103. prepare_to_wait(&wq, &w, TASK_INTERRUPTIBLE);
  104. if (!signal_pending(current))
  105. schedule();
  106. ret = signal_pending(current) ? -ERESTARTSYS : 0;
  107. finish_wait(&wq, &w);
  108. return ret;
  109. }
  110. static unsigned int iodev_poll(struct file *f, struct poll_table_struct *p)
  111. {
  112. poll_wait(f, &wq, p);
  113. return POLLOUT | POLLWRNORM;
  114. }
  115. static irqreturn_t iodev_irqhdl(int irq, void *ctxt)
  116. {
  117. wake_up(&wq);
  118. return IRQ_HANDLED;
  119. }
  120. static int __init iodev_init_module(void)
  121. {
  122. return platform_driver_register(&iodev_driver);
  123. }
  124. static void __exit iodev_cleanup_module(void)
  125. {
  126. platform_driver_unregister(&iodev_driver);
  127. }
  128. module_init(iodev_init_module);
  129. module_exit(iodev_cleanup_module);
  130. MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
  131. MODULE_DESCRIPTION("Basler eXcite i/o interrupt handler");
  132. MODULE_VERSION("0.0");
  133. MODULE_LICENSE("GPL");