autoprobe.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. unsigned int status;
  32. int i;
  33. /*
  34. * quiesce the kernel, or at least the asynchronous portion
  35. */
  36. async_synchronize_full();
  37. mutex_lock(&probing_active);
  38. /*
  39. * something may have generated an irq long ago and we want to
  40. * flush such a longstanding irq before considering it as spurious.
  41. */
  42. for_each_irq_desc_reverse(i, desc) {
  43. spin_lock_irq(&desc->lock);
  44. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  45. /*
  46. * An old-style architecture might still have
  47. * the handle_bad_irq handler there:
  48. */
  49. compat_irq_chip_set_default_handler(desc);
  50. /*
  51. * Some chips need to know about probing in
  52. * progress:
  53. */
  54. if (desc->chip->set_type)
  55. desc->chip->set_type(i, IRQ_TYPE_PROBE);
  56. desc->chip->startup(i);
  57. }
  58. 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. spin_lock_irq(&desc->lock);
  69. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  70. desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
  71. if (desc->chip->startup(i))
  72. desc->status |= IRQ_PENDING;
  73. }
  74. 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. spin_lock_irq(&desc->lock);
  85. status = desc->status;
  86. if (status & IRQ_AUTODETECT) {
  87. /* It triggered already - consider it spurious. */
  88. if (!(status & IRQ_WAITING)) {
  89. desc->status = status & ~IRQ_AUTODETECT;
  90. desc->chip->shutdown(i);
  91. } else
  92. if (i < 32)
  93. mask |= 1 << i;
  94. }
  95. 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 status, mask = 0;
  115. struct irq_desc *desc;
  116. int i;
  117. for_each_irq_desc(i, desc) {
  118. spin_lock_irq(&desc->lock);
  119. status = desc->status;
  120. if (status & IRQ_AUTODETECT) {
  121. if (i < 16 && !(status & IRQ_WAITING))
  122. mask |= 1 << i;
  123. desc->status = status & ~IRQ_AUTODETECT;
  124. desc->chip->shutdown(i);
  125. }
  126. spin_unlock_irq(&desc->lock);
  127. }
  128. mutex_unlock(&probing_active);
  129. return mask & val;
  130. }
  131. EXPORT_SYMBOL(probe_irq_mask);
  132. /**
  133. * probe_irq_off - end an interrupt autodetect
  134. * @val: mask of potential interrupts (unused)
  135. *
  136. * Scans the unused interrupt lines and returns the line which
  137. * appears to have triggered the interrupt. If no interrupt was
  138. * found then zero is returned. If more than one interrupt is
  139. * found then minus the first candidate is returned to indicate
  140. * their is doubt.
  141. *
  142. * The interrupt probe logic state is returned to its previous
  143. * value.
  144. *
  145. * BUGS: When used in a module (which arguably shouldn't happen)
  146. * nothing prevents two IRQ probe callers from overlapping. The
  147. * results of this are non-optimal.
  148. */
  149. int probe_irq_off(unsigned long val)
  150. {
  151. int i, irq_found = 0, nr_of_irqs = 0;
  152. struct irq_desc *desc;
  153. unsigned int status;
  154. for_each_irq_desc(i, desc) {
  155. spin_lock_irq(&desc->lock);
  156. status = desc->status;
  157. if (status & IRQ_AUTODETECT) {
  158. if (!(status & IRQ_WAITING)) {
  159. if (!nr_of_irqs)
  160. irq_found = i;
  161. nr_of_irqs++;
  162. }
  163. desc->status = status & ~IRQ_AUTODETECT;
  164. desc->chip->shutdown(i);
  165. }
  166. spin_unlock_irq(&desc->lock);
  167. }
  168. mutex_unlock(&probing_active);
  169. if (nr_of_irqs > 1)
  170. irq_found = -irq_found;
  171. return irq_found;
  172. }
  173. EXPORT_SYMBOL(probe_irq_off);