autoprobe.c 4.6 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 <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. * "IRQS_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 | IRQS_WAITING;
  71. if (irq_startup(desc))
  72. desc->status |= IRQ_PENDING;
  73. }
  74. raw_spin_unlock_irq(&desc->lock);
  75. }
  76. /*
  77. * Wait for spurious interrupts to trigger
  78. */
  79. msleep(100);
  80. /*
  81. * Now filter out any obviously spurious interrupts
  82. */
  83. for_each_irq_desc(i, desc) {
  84. raw_spin_lock_irq(&desc->lock);
  85. if (desc->istate & IRQS_AUTODETECT) {
  86. /* It triggered already - consider it spurious. */
  87. if (!(desc->istate & IRQS_WAITING)) {
  88. desc->istate &= ~IRQS_AUTODETECT;
  89. irq_shutdown(desc);
  90. } else
  91. if (i < 32)
  92. mask |= 1 << i;
  93. }
  94. raw_spin_unlock_irq(&desc->lock);
  95. }
  96. return mask;
  97. }
  98. EXPORT_SYMBOL(probe_irq_on);
  99. /**
  100. * probe_irq_mask - scan a bitmap of interrupt lines
  101. * @val: mask of interrupts to consider
  102. *
  103. * Scan the interrupt lines and return a bitmap of active
  104. * autodetect interrupts. The interrupt probe logic state
  105. * is then returned to its previous value.
  106. *
  107. * Note: we need to scan all the irq's even though we will
  108. * only return autodetect irq numbers - just so that we reset
  109. * them all to a known state.
  110. */
  111. unsigned int probe_irq_mask(unsigned long val)
  112. {
  113. unsigned int mask = 0;
  114. struct irq_desc *desc;
  115. int i;
  116. for_each_irq_desc(i, desc) {
  117. raw_spin_lock_irq(&desc->lock);
  118. if (desc->istate & IRQS_AUTODETECT) {
  119. if (i < 16 && !(desc->istate & IRQS_WAITING))
  120. mask |= 1 << i;
  121. desc->istate &= ~IRQS_AUTODETECT;
  122. irq_shutdown(desc);
  123. }
  124. raw_spin_unlock_irq(&desc->lock);
  125. }
  126. mutex_unlock(&probing_active);
  127. return mask & val;
  128. }
  129. EXPORT_SYMBOL(probe_irq_mask);
  130. /**
  131. * probe_irq_off - end an interrupt autodetect
  132. * @val: mask of potential interrupts (unused)
  133. *
  134. * Scans the unused interrupt lines and returns the line which
  135. * appears to have triggered the interrupt. If no interrupt was
  136. * found then zero is returned. If more than one interrupt is
  137. * found then minus the first candidate is returned to indicate
  138. * their is doubt.
  139. *
  140. * The interrupt probe logic state is returned to its previous
  141. * value.
  142. *
  143. * BUGS: When used in a module (which arguably shouldn't happen)
  144. * nothing prevents two IRQ probe callers from overlapping. The
  145. * results of this are non-optimal.
  146. */
  147. int probe_irq_off(unsigned long val)
  148. {
  149. int i, irq_found = 0, nr_of_irqs = 0;
  150. struct irq_desc *desc;
  151. for_each_irq_desc(i, desc) {
  152. raw_spin_lock_irq(&desc->lock);
  153. if (desc->istate & IRQS_AUTODETECT) {
  154. if (!(desc->istate & IRQS_WAITING)) {
  155. if (!nr_of_irqs)
  156. irq_found = i;
  157. nr_of_irqs++;
  158. }
  159. desc->istate &= ~IRQS_AUTODETECT;
  160. irq_shutdown(desc);
  161. }
  162. raw_spin_unlock_irq(&desc->lock);
  163. }
  164. mutex_unlock(&probing_active);
  165. if (nr_of_irqs > 1)
  166. irq_found = -irq_found;
  167. return irq_found;
  168. }
  169. EXPORT_SYMBOL(probe_irq_off);