pseudo.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. pseudo.h (c) 1997-8 Grant R. Guenther <grant@torque.net>
  3. Under the terms of the GNU General Public License.
  4. This is the "pseudo-interrupt" logic for parallel port drivers.
  5. This module is #included into each driver. It makes one
  6. function available:
  7. ps_set_intr( void (*continuation)(void),
  8. int (*ready)(void),
  9. int timeout,
  10. int nice )
  11. Which will arrange for ready() to be evaluated frequently and
  12. when either it returns true, or timeout jiffies have passed,
  13. continuation() will be invoked.
  14. If nice is 1, the test will done approximately once a
  15. jiffy. If nice is 0, the test will also be done whenever
  16. the scheduler runs (by adding it to a task queue). If
  17. nice is greater than 1, the test will be done once every
  18. (nice-1) jiffies.
  19. */
  20. /* Changes:
  21. 1.01 1998.05.03 Switched from cli()/sti() to spinlocks
  22. 1.02 1998.12.14 Added support for nice > 1
  23. */
  24. #define PS_VERSION "1.02"
  25. #include <linux/sched.h>
  26. #include <linux/workqueue.h>
  27. static void ps_tq_int(struct work_struct *work);
  28. static void (* ps_continuation)(void);
  29. static int (* ps_ready)(void);
  30. static unsigned long ps_timeout;
  31. static int ps_tq_active = 0;
  32. static int ps_nice = 0;
  33. static DEFINE_SPINLOCK(ps_spinlock __attribute__((unused)));
  34. static DECLARE_DELAYED_WORK(ps_tq, ps_tq_int);
  35. static void ps_set_intr(void (*continuation)(void),
  36. int (*ready)(void),
  37. int timeout, int nice)
  38. {
  39. unsigned long flags;
  40. spin_lock_irqsave(&ps_spinlock,flags);
  41. ps_continuation = continuation;
  42. ps_ready = ready;
  43. ps_timeout = jiffies + timeout;
  44. ps_nice = nice;
  45. if (!ps_tq_active) {
  46. ps_tq_active = 1;
  47. if (!ps_nice)
  48. schedule_delayed_work(&ps_tq, 0);
  49. else
  50. schedule_delayed_work(&ps_tq, ps_nice-1);
  51. }
  52. spin_unlock_irqrestore(&ps_spinlock,flags);
  53. }
  54. static void ps_tq_int(struct work_struct *work)
  55. {
  56. void (*con)(void);
  57. unsigned long flags;
  58. spin_lock_irqsave(&ps_spinlock,flags);
  59. con = ps_continuation;
  60. ps_tq_active = 0;
  61. if (!con) {
  62. spin_unlock_irqrestore(&ps_spinlock,flags);
  63. return;
  64. }
  65. if (!ps_ready || ps_ready() || time_after_eq(jiffies, ps_timeout)) {
  66. ps_continuation = NULL;
  67. spin_unlock_irqrestore(&ps_spinlock,flags);
  68. con();
  69. return;
  70. }
  71. ps_tq_active = 1;
  72. if (!ps_nice)
  73. schedule_delayed_work(&ps_tq, 0);
  74. else
  75. schedule_delayed_work(&ps_tq, ps_nice-1);
  76. spin_unlock_irqrestore(&ps_spinlock,flags);
  77. }
  78. /* end of pseudo.h */