autoprobe.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. raw_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->irq_data.chip->irq_set_type)
  55. desc->irq_data.chip->irq_set_type(&desc->irq_data,
  56. IRQ_TYPE_PROBE);
  57. desc->irq_data.chip->irq_startup(&desc->irq_data);
  58. }
  59. raw_spin_unlock_irq(&desc->lock);
  60. }
  61. /* Wait for longstanding interrupts to trigger. */
  62. msleep(20);
  63. /*
  64. * enable any unassigned irqs
  65. * (we must startup again here because if a longstanding irq
  66. * happened in the previous stage, it may have masked itself)
  67. */
  68. for_each_irq_desc_reverse(i, desc) {
  69. raw_spin_lock_irq(&desc->lock);
  70. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  71. desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
  72. if (desc->irq_data.chip->irq_startup(&desc->irq_data))
  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. status = desc->status;
  87. if (status & IRQ_AUTODETECT) {
  88. /* It triggered already - consider it spurious. */
  89. if (!(status & IRQ_WAITING)) {
  90. desc->status = status & ~IRQ_AUTODETECT;
  91. desc->irq_data.chip->irq_shutdown(&desc->irq_data);
  92. } else
  93. if (i < 32)
  94. mask |= 1 << i;
  95. }
  96. raw_spin_unlock_irq(&desc->lock);
  97. }
  98. return mask;
  99. }
  100. EXPORT_SYMBOL(probe_irq_on);
  101. /**
  102. * probe_irq_mask - scan a bitmap of interrupt lines
  103. * @val: mask of interrupts to consider
  104. *
  105. * Scan the interrupt lines and return a bitmap of active
  106. * autodetect interrupts. The interrupt probe logic state
  107. * is then returned to its previous value.
  108. *
  109. * Note: we need to scan all the irq's even though we will
  110. * only return autodetect irq numbers - just so that we reset
  111. * them all to a known state.
  112. */
  113. unsigned int probe_irq_mask(unsigned long val)
  114. {
  115. unsigned int status, mask = 0;
  116. struct irq_desc *desc;
  117. int i;
  118. for_each_irq_desc(i, desc) {
  119. raw_spin_lock_irq(&desc->lock);
  120. status = desc->status;
  121. if (status & IRQ_AUTODETECT) {
  122. if (i < 16 && !(status & IRQ_WAITING))
  123. mask |= 1 << i;
  124. desc->status = status & ~IRQ_AUTODETECT;
  125. desc->irq_data.chip->irq_shutdown(&desc->irq_data);
  126. }
  127. raw_spin_unlock_irq(&desc->lock);
  128. }
  129. mutex_unlock(&probing_active);
  130. return mask & val;
  131. }
  132. EXPORT_SYMBOL(probe_irq_mask);
  133. /**
  134. * probe_irq_off - end an interrupt autodetect
  135. * @val: mask of potential interrupts (unused)
  136. *
  137. * Scans the unused interrupt lines and returns the line which
  138. * appears to have triggered the interrupt. If no interrupt was
  139. * found then zero is returned. If more than one interrupt is
  140. * found then minus the first candidate is returned to indicate
  141. * their is doubt.
  142. *
  143. * The interrupt probe logic state is returned to its previous
  144. * value.
  145. *
  146. * BUGS: When used in a module (which arguably shouldn't happen)
  147. * nothing prevents two IRQ probe callers from overlapping. The
  148. * results of this are non-optimal.
  149. */
  150. int probe_irq_off(unsigned long val)
  151. {
  152. int i, irq_found = 0, nr_of_irqs = 0;
  153. struct irq_desc *desc;
  154. unsigned int status;
  155. for_each_irq_desc(i, desc) {
  156. raw_spin_lock_irq(&desc->lock);
  157. status = desc->status;
  158. if (status & IRQ_AUTODETECT) {
  159. if (!(status & IRQ_WAITING)) {
  160. if (!nr_of_irqs)
  161. irq_found = i;
  162. nr_of_irqs++;
  163. }
  164. desc->status = status & ~IRQ_AUTODETECT;
  165. desc->irq_data.chip->irq_shutdown(&desc->irq_data);
  166. }
  167. raw_spin_unlock_irq(&desc->lock);
  168. }
  169. mutex_unlock(&probing_active);
  170. if (nr_of_irqs > 1)
  171. irq_found = -irq_found;
  172. return irq_found;
  173. }
  174. EXPORT_SYMBOL(probe_irq_off);