autoprobe.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * linux/kernel/irq/autoprobe.c
  3. *
  4. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  5. *
  6. * This file contains the interrupt probing code and driver APIs.
  7. */
  8. #include <linux/irq.h>
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/delay.h>
  12. #include "internals.h"
  13. /*
  14. * Autodetection depends on the fact that any interrupt that
  15. * comes in on to an unassigned handler will get stuck with
  16. * "IRQ_WAITING" cleared and the interrupt disabled.
  17. */
  18. static DEFINE_MUTEX(probing_active);
  19. /**
  20. * probe_irq_on - begin an interrupt autodetect
  21. *
  22. * Commence probing for an interrupt. The interrupts are scanned
  23. * and a mask of potential interrupt lines is returned.
  24. *
  25. */
  26. unsigned long probe_irq_on(void)
  27. {
  28. struct irq_desc *desc;
  29. unsigned long mask = 0;
  30. unsigned int status;
  31. int i;
  32. mutex_lock(&probing_active);
  33. /*
  34. * something may have generated an irq long ago and we want to
  35. * flush such a longstanding irq before considering it as spurious.
  36. */
  37. for_each_irq_desc_reverse(i, desc) {
  38. spin_lock_irq(&desc->lock);
  39. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  40. /*
  41. * An old-style architecture might still have
  42. * the handle_bad_irq handler there:
  43. */
  44. compat_irq_chip_set_default_handler(desc);
  45. /*
  46. * Some chips need to know about probing in
  47. * progress:
  48. */
  49. if (desc->chip->set_type)
  50. desc->chip->set_type(i, IRQ_TYPE_PROBE);
  51. desc->chip->startup(i);
  52. }
  53. spin_unlock_irq(&desc->lock);
  54. }
  55. /* Wait for longstanding interrupts to trigger. */
  56. msleep(20);
  57. /*
  58. * enable any unassigned irqs
  59. * (we must startup again here because if a longstanding irq
  60. * happened in the previous stage, it may have masked itself)
  61. */
  62. for_each_irq_desc_reverse(i, desc) {
  63. spin_lock_irq(&desc->lock);
  64. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  65. desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
  66. if (desc->chip->startup(i))
  67. desc->status |= IRQ_PENDING;
  68. }
  69. spin_unlock_irq(&desc->lock);
  70. }
  71. /*
  72. * Wait for spurious interrupts to trigger
  73. */
  74. msleep(100);
  75. /*
  76. * Now filter out any obviously spurious interrupts
  77. */
  78. for_each_irq_desc(i, desc) {
  79. spin_lock_irq(&desc->lock);
  80. status = desc->status;
  81. if (status & IRQ_AUTODETECT) {
  82. /* It triggered already - consider it spurious. */
  83. if (!(status & IRQ_WAITING)) {
  84. desc->status = status & ~IRQ_AUTODETECT;
  85. desc->chip->shutdown(i);
  86. } else
  87. if (i < 32)
  88. mask |= 1 << i;
  89. }
  90. spin_unlock_irq(&desc->lock);
  91. }
  92. return mask;
  93. }
  94. EXPORT_SYMBOL(probe_irq_on);
  95. /**
  96. * probe_irq_mask - scan a bitmap of interrupt lines
  97. * @val: mask of interrupts to consider
  98. *
  99. * Scan the interrupt lines and return a bitmap of active
  100. * autodetect interrupts. The interrupt probe logic state
  101. * is then returned to its previous value.
  102. *
  103. * Note: we need to scan all the irq's even though we will
  104. * only return autodetect irq numbers - just so that we reset
  105. * them all to a known state.
  106. */
  107. unsigned int probe_irq_mask(unsigned long val)
  108. {
  109. unsigned int status, mask = 0;
  110. struct irq_desc *desc;
  111. int i;
  112. for_each_irq_desc(i, desc) {
  113. spin_lock_irq(&desc->lock);
  114. status = desc->status;
  115. if (status & IRQ_AUTODETECT) {
  116. if (i < 16 && !(status & IRQ_WAITING))
  117. mask |= 1 << i;
  118. desc->status = status & ~IRQ_AUTODETECT;
  119. desc->chip->shutdown(i);
  120. }
  121. spin_unlock_irq(&desc->lock);
  122. }
  123. mutex_unlock(&probing_active);
  124. return mask & val;
  125. }
  126. EXPORT_SYMBOL(probe_irq_mask);
  127. /**
  128. * probe_irq_off - end an interrupt autodetect
  129. * @val: mask of potential interrupts (unused)
  130. *
  131. * Scans the unused interrupt lines and returns the line which
  132. * appears to have triggered the interrupt. If no interrupt was
  133. * found then zero is returned. If more than one interrupt is
  134. * found then minus the first candidate is returned to indicate
  135. * their is doubt.
  136. *
  137. * The interrupt probe logic state is returned to its previous
  138. * value.
  139. *
  140. * BUGS: When used in a module (which arguably shouldn't happen)
  141. * nothing prevents two IRQ probe callers from overlapping. The
  142. * results of this are non-optimal.
  143. */
  144. int probe_irq_off(unsigned long val)
  145. {
  146. int i, irq_found = 0, nr_of_irqs = 0;
  147. struct irq_desc *desc;
  148. unsigned int status;
  149. for_each_irq_desc(i, desc) {
  150. spin_lock_irq(&desc->lock);
  151. status = desc->status;
  152. if (status & IRQ_AUTODETECT) {
  153. if (!(status & IRQ_WAITING)) {
  154. if (!nr_of_irqs)
  155. irq_found = i;
  156. nr_of_irqs++;
  157. }
  158. desc->status = status & ~IRQ_AUTODETECT;
  159. desc->chip->shutdown(i);
  160. }
  161. spin_unlock_irq(&desc->lock);
  162. }
  163. mutex_unlock(&probing_active);
  164. if (nr_of_irqs > 1)
  165. irq_found = -irq_found;
  166. return irq_found;
  167. }
  168. EXPORT_SYMBOL(probe_irq_off);