excite_iodev.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 device *);
  32. static int __exit iodev_remove(struct 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 device_driver iodev_driver =
  56. {
  57. .name = (char *) iodev_name,
  58. .bus = &platform_bus_type,
  59. .owner = THIS_MODULE,
  60. .probe = iodev_probe,
  61. .remove = __exit_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 device *dev)
  74. {
  75. struct platform_device * const pdv = to_platform_device(dev);
  76. const struct resource * const ri =
  77. iodev_get_resource(pdv, IODEV_RESOURCE_IRQ, IORESOURCE_IRQ);
  78. if (unlikely(!ri))
  79. return -ENXIO;
  80. iodev_irq = ri->start;
  81. return misc_register(&miscdev);
  82. }
  83. static int __exit iodev_remove(struct device *dev)
  84. {
  85. return misc_deregister(&miscdev);
  86. }
  87. static int iodev_open(struct inode *i, struct file *f)
  88. {
  89. int ret;
  90. lock_kernel();
  91. ret = request_irq(iodev_irq, iodev_irqhdl, IRQF_DISABLED,
  92. iodev_name, &miscdev);
  93. unlock_kernel();
  94. return ret;
  95. }
  96. static int iodev_release(struct inode *i, struct file *f)
  97. {
  98. free_irq(iodev_irq, &miscdev);
  99. return 0;
  100. }
  101. static ssize_t
  102. iodev_read(struct file *f, char __user *d, size_t s, loff_t *o)
  103. {
  104. ssize_t ret;
  105. DEFINE_WAIT(w);
  106. prepare_to_wait(&wq, &w, TASK_INTERRUPTIBLE);
  107. if (!signal_pending(current))
  108. schedule();
  109. ret = signal_pending(current) ? -ERESTARTSYS : 0;
  110. finish_wait(&wq, &w);
  111. return ret;
  112. }
  113. static unsigned int iodev_poll(struct file *f, struct poll_table_struct *p)
  114. {
  115. poll_wait(f, &wq, p);
  116. return POLLOUT | POLLWRNORM;
  117. }
  118. static irqreturn_t iodev_irqhdl(int irq, void *ctxt)
  119. {
  120. wake_up(&wq);
  121. return IRQ_HANDLED;
  122. }
  123. static int __init iodev_init_module(void)
  124. {
  125. return driver_register(&iodev_driver);
  126. }
  127. static void __exit iodev_cleanup_module(void)
  128. {
  129. driver_unregister(&iodev_driver);
  130. }
  131. module_init(iodev_init_module);
  132. module_exit(iodev_cleanup_module);
  133. MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
  134. MODULE_DESCRIPTION("Basler eXcite i/o interrupt handler");
  135. MODULE_VERSION("0.0");
  136. MODULE_LICENSE("GPL");