autoprobe.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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;
  30. unsigned int i;
  31. mutex_lock(&probing_active);
  32. /*
  33. * something may have generated an irq long ago and we want to
  34. * flush such a longstanding irq before considering it as spurious.
  35. */
  36. for (i = nr_irqs-1; i > 0; i--) {
  37. desc = irq_to_desc(i);
  38. if (!desc)
  39. continue;
  40. spin_lock_irq(&desc->lock);
  41. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  42. /*
  43. * An old-style architecture might still have
  44. * the handle_bad_irq handler there:
  45. */
  46. compat_irq_chip_set_default_handler(desc);
  47. /*
  48. * Some chips need to know about probing in
  49. * progress:
  50. */
  51. if (desc->chip->set_type)
  52. desc->chip->set_type(i, IRQ_TYPE_PROBE);
  53. desc->chip->startup(i);
  54. }
  55. spin_unlock_irq(&desc->lock);
  56. }
  57. /* Wait for longstanding interrupts to trigger. */
  58. msleep(20);
  59. /*
  60. * enable any unassigned irqs
  61. * (we must startup again here because if a longstanding irq
  62. * happened in the previous stage, it may have masked itself)
  63. */
  64. for (i = nr_irqs-1; i > 0; i--) {
  65. desc = irq_to_desc(i);
  66. if (!desc)
  67. continue;
  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. mask = 0;
  84. for (i = 0; i < nr_irqs; i++) {
  85. unsigned int status;
  86. desc = irq_to_desc(i);
  87. if (!desc)
  88. continue;
  89. spin_lock_irq(&desc->lock);
  90. status = desc->status;
  91. if (status & IRQ_AUTODETECT) {
  92. /* It triggered already - consider it spurious. */
  93. if (!(status & IRQ_WAITING)) {
  94. desc->status = status & ~IRQ_AUTODETECT;
  95. desc->chip->shutdown(i);
  96. } else
  97. if (i < 32)
  98. mask |= 1 << i;
  99. }
  100. spin_unlock_irq(&desc->lock);
  101. }
  102. return mask;
  103. }
  104. EXPORT_SYMBOL(probe_irq_on);
  105. /**
  106. * probe_irq_mask - scan a bitmap of interrupt lines
  107. * @val: mask of interrupts to consider
  108. *
  109. * Scan the interrupt lines and return a bitmap of active
  110. * autodetect interrupts. The interrupt probe logic state
  111. * is then returned to its previous value.
  112. *
  113. * Note: we need to scan all the irq's even though we will
  114. * only return autodetect irq numbers - just so that we reset
  115. * them all to a known state.
  116. */
  117. unsigned int probe_irq_mask(unsigned long val)
  118. {
  119. unsigned int mask;
  120. int i;
  121. mask = 0;
  122. for (i = 0; i < nr_irqs; i++) {
  123. struct irq_desc *desc = irq_to_desc(i);
  124. unsigned int status;
  125. if (!desc)
  126. continue;
  127. spin_lock_irq(&desc->lock);
  128. status = desc->status;
  129. if (status & IRQ_AUTODETECT) {
  130. if (i < 16 && !(status & IRQ_WAITING))
  131. mask |= 1 << i;
  132. desc->status = status & ~IRQ_AUTODETECT;
  133. desc->chip->shutdown(i);
  134. }
  135. spin_unlock_irq(&desc->lock);
  136. }
  137. mutex_unlock(&probing_active);
  138. return mask & val;
  139. }
  140. EXPORT_SYMBOL(probe_irq_mask);
  141. /**
  142. * probe_irq_off - end an interrupt autodetect
  143. * @val: mask of potential interrupts (unused)
  144. *
  145. * Scans the unused interrupt lines and returns the line which
  146. * appears to have triggered the interrupt. If no interrupt was
  147. * found then zero is returned. If more than one interrupt is
  148. * found then minus the first candidate is returned to indicate
  149. * their is doubt.
  150. *
  151. * The interrupt probe logic state is returned to its previous
  152. * value.
  153. *
  154. * BUGS: When used in a module (which arguably shouldn't happen)
  155. * nothing prevents two IRQ probe callers from overlapping. The
  156. * results of this are non-optimal.
  157. */
  158. int probe_irq_off(unsigned long val)
  159. {
  160. int i, irq_found = 0, nr_irqs = 0;
  161. for (i = 0; i < nr_irqs; i++) {
  162. struct irq_desc *desc = irq_to_desc(i);
  163. unsigned int status;
  164. if (!desc)
  165. continue;
  166. spin_lock_irq(&desc->lock);
  167. status = desc->status;
  168. if (status & IRQ_AUTODETECT) {
  169. if (!(status & IRQ_WAITING)) {
  170. if (!nr_irqs)
  171. irq_found = i;
  172. nr_irqs++;
  173. }
  174. desc->status = status & ~IRQ_AUTODETECT;
  175. desc->chip->shutdown(i);
  176. }
  177. spin_unlock_irq(&desc->lock);
  178. }
  179. mutex_unlock(&probing_active);
  180. if (nr_irqs > 1)
  181. irq_found = -irq_found;
  182. return irq_found;
  183. }
  184. EXPORT_SYMBOL(probe_irq_off);