autoprobe.c 4.5 KB

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