autoprobe.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_desc + i;
  38. spin_lock_irq(&desc->lock);
  39. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  40. /*
  41. * An old-style architecture might still have
  42. * the handle_bad_irq handler there:
  43. */
  44. compat_irq_chip_set_default_handler(desc);
  45. /*
  46. * Some chips need to know about probing in
  47. * progress:
  48. */
  49. if (desc->chip->set_type)
  50. desc->chip->set_type(i, IRQ_TYPE_PROBE);
  51. desc->chip->startup(i);
  52. }
  53. 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 (i = NR_IRQS-1; i > 0; i--) {
  63. desc = irq_desc + i;
  64. spin_lock_irq(&desc->lock);
  65. if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
  66. desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
  67. if (desc->chip->startup(i))
  68. desc->status |= IRQ_PENDING;
  69. }
  70. spin_unlock_irq(&desc->lock);
  71. }
  72. /*
  73. * Wait for spurious interrupts to trigger
  74. */
  75. msleep(100);
  76. /*
  77. * Now filter out any obviously spurious interrupts
  78. */
  79. mask = 0;
  80. for (i = 0; i < NR_IRQS; i++) {
  81. unsigned int status;
  82. desc = irq_desc + i;
  83. spin_lock_irq(&desc->lock);
  84. status = desc->status;
  85. if (status & IRQ_AUTODETECT) {
  86. /* It triggered already - consider it spurious. */
  87. if (!(status & IRQ_WAITING)) {
  88. desc->status = status & ~IRQ_AUTODETECT;
  89. desc->chip->shutdown(i);
  90. } else
  91. if (i < 32)
  92. mask |= 1 << i;
  93. }
  94. 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;
  114. int i;
  115. mask = 0;
  116. for (i = 0; i < NR_IRQS; i++) {
  117. struct irq_desc *desc = irq_desc + i;
  118. unsigned int status;
  119. 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->chip->shutdown(i);
  126. }
  127. 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_irqs = 0;
  153. for (i = 0; i < NR_IRQS; i++) {
  154. struct irq_desc *desc = irq_desc + i;
  155. unsigned int status;
  156. spin_lock_irq(&desc->lock);
  157. status = desc->status;
  158. if (status & IRQ_AUTODETECT) {
  159. if (!(status & IRQ_WAITING)) {
  160. if (!nr_irqs)
  161. irq_found = i;
  162. nr_irqs++;
  163. }
  164. desc->status = status & ~IRQ_AUTODETECT;
  165. desc->chip->shutdown(i);
  166. }
  167. spin_unlock_irq(&desc->lock);
  168. }
  169. mutex_unlock(&probing_active);
  170. if (nr_irqs > 1)
  171. irq_found = -irq_found;
  172. return irq_found;
  173. }
  174. EXPORT_SYMBOL(probe_irq_off);