excite_iodev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "excite_iodev.h"
  29. static const struct resource *iodev_get_resource(struct platform_device *, const char *, unsigned int);
  30. static int __init iodev_probe(struct device *);
  31. static int __exit iodev_remove(struct device *);
  32. static int iodev_open(struct inode *, struct file *);
  33. static int iodev_release(struct inode *, struct file *);
  34. static ssize_t iodev_read(struct file *, char __user *, size_t s, loff_t *);
  35. static unsigned int iodev_poll(struct file *, struct poll_table_struct *);
  36. static irqreturn_t iodev_irqhdl(int, void *);
  37. static const char iodev_name[] = "iodev";
  38. static unsigned int iodev_irq;
  39. static DECLARE_WAIT_QUEUE_HEAD(wq);
  40. static struct file_operations fops =
  41. {
  42. .owner = THIS_MODULE,
  43. .open = iodev_open,
  44. .release = iodev_release,
  45. .read = iodev_read,
  46. .poll = iodev_poll
  47. };
  48. static struct miscdevice miscdev =
  49. {
  50. .minor = MISC_DYNAMIC_MINOR,
  51. .name = iodev_name,
  52. .fops = &fops
  53. };
  54. static struct device_driver iodev_driver =
  55. {
  56. .name = (char *) iodev_name,
  57. .bus = &platform_bus_type,
  58. .owner = THIS_MODULE,
  59. .probe = iodev_probe,
  60. .remove = __exit_p(iodev_remove)
  61. };
  62. static const struct resource *
  63. iodev_get_resource(struct platform_device *pdv, const char *name,
  64. unsigned int type)
  65. {
  66. char buf[80];
  67. if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
  68. return NULL;
  69. return platform_get_resource_byname(pdv, type, buf);
  70. }
  71. /* No hotplugging on the platform bus - use __init */
  72. static int __init iodev_probe(struct device *dev)
  73. {
  74. struct platform_device * const pdv = to_platform_device(dev);
  75. const struct resource * const ri =
  76. iodev_get_resource(pdv, 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 device *dev)
  83. {
  84. return misc_deregister(&miscdev);
  85. }
  86. static int iodev_open(struct inode *i, struct file *f)
  87. {
  88. return request_irq(iodev_irq, iodev_irqhdl, IRQF_DISABLED,
  89. iodev_name, &miscdev);
  90. }
  91. static int iodev_release(struct inode *i, struct file *f)
  92. {
  93. free_irq(iodev_irq, &miscdev);
  94. return 0;
  95. }
  96. static ssize_t
  97. iodev_read(struct file *f, char __user *d, size_t s, loff_t *o)
  98. {
  99. ssize_t ret;
  100. DEFINE_WAIT(w);
  101. prepare_to_wait(&wq, &w, TASK_INTERRUPTIBLE);
  102. if (!signal_pending(current))
  103. schedule();
  104. ret = signal_pending(current) ? -ERESTARTSYS : 0;
  105. finish_wait(&wq, &w);
  106. return ret;
  107. }
  108. static unsigned int iodev_poll(struct file *f, struct poll_table_struct *p)
  109. {
  110. poll_wait(f, &wq, p);
  111. return POLLOUT | POLLWRNORM;
  112. }
  113. static irqreturn_t iodev_irqhdl(int irq, void *ctxt)
  114. {
  115. wake_up(&wq);
  116. return IRQ_HANDLED;
  117. }
  118. static int __init iodev_init_module(void)
  119. {
  120. return driver_register(&iodev_driver);
  121. }
  122. static void __exit iodev_cleanup_module(void)
  123. {
  124. driver_unregister(&iodev_driver);
  125. }
  126. module_init(iodev_init_module);
  127. module_exit(iodev_cleanup_module);
  128. MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
  129. MODULE_DESCRIPTION("Basler eXcite i/o interrupt handler");
  130. MODULE_VERSION("0.0");
  131. MODULE_LICENSE("GPL");