autoprobe.c 4.6 KB

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