[PATCH] genirq: core
[linux-2.6/kvm.git] / kernel / irq / autoprobe.c
blobcfdb63eb5c9450c288e7c76836e39cebf398d3b8
1 /*
2 * linux/kernel/irq/autoprobe.c
4 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
6 * This file contains the interrupt probing code and driver APIs.
7 */
9 #include <linux/irq.h>
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
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.
19 static DEFINE_MUTEX(probing_active);
21 /**
22 * probe_irq_on - begin an interrupt autodetect
24 * Commence probing for an interrupt. The interrupts are scanned
25 * and a mask of potential interrupt lines is returned.
28 unsigned long probe_irq_on(void)
30 struct irq_desc *desc;
31 unsigned long mask;
32 unsigned int i;
34 mutex_lock(&probing_active);
36 * something may have generated an irq long ago and we want to
37 * flush such a longstanding irq before considering it as spurious.
39 for (i = NR_IRQS-1; i > 0; i--) {
40 desc = irq_desc + i;
42 spin_lock_irq(&desc->lock);
43 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
45 * Some chips need to know about probing in
46 * progress:
48 if (desc->chip->set_type)
49 desc->chip->set_type(i, IRQ_TYPE_PROBE);
50 desc->chip->startup(i);
52 spin_unlock_irq(&desc->lock);
55 /* Wait for longstanding interrupts to trigger. */
56 msleep(20);
59 * enable any unassigned irqs
60 * (we must startup again here because if a longstanding irq
61 * happened in the previous stage, it may have masked itself)
63 for (i = NR_IRQS-1; i > 0; i--) {
64 desc = irq_desc + i;
66 spin_lock_irq(&desc->lock);
67 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
68 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
69 if (desc->chip->startup(i))
70 desc->status |= IRQ_PENDING;
72 spin_unlock_irq(&desc->lock);
76 * Wait for spurious interrupts to trigger
78 msleep(100);
81 * Now filter out any obviously spurious interrupts
83 mask = 0;
84 for (i = 0; i < NR_IRQS; i++) {
85 unsigned int status;
87 desc = irq_desc + i;
88 spin_lock_irq(&desc->lock);
89 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;
100 spin_unlock_irq(&desc->lock);
103 return mask;
105 EXPORT_SYMBOL(probe_irq_on);
108 * probe_irq_mask - scan a bitmap of interrupt lines
109 * @val: mask of interrupts to consider
111 * Scan the interrupt lines and return a bitmap of active
112 * autodetect interrupts. The interrupt probe logic state
113 * is then returned to its previous value.
115 * Note: we need to scan all the irq's even though we will
116 * only return autodetect irq numbers - just so that we reset
117 * them all to a known state.
119 unsigned int probe_irq_mask(unsigned long val)
121 unsigned int mask;
122 int i;
124 mask = 0;
125 for (i = 0; i < NR_IRQS; i++) {
126 struct irq_desc *desc = irq_desc + i;
127 unsigned int status;
129 spin_lock_irq(&desc->lock);
130 status = desc->status;
132 if (status & IRQ_AUTODETECT) {
133 if (i < 16 && !(status & IRQ_WAITING))
134 mask |= 1 << i;
136 desc->status = status & ~IRQ_AUTODETECT;
137 desc->chip->shutdown(i);
139 spin_unlock_irq(&desc->lock);
141 mutex_unlock(&probing_active);
143 return mask & val;
145 EXPORT_SYMBOL(probe_irq_mask);
148 * probe_irq_off - end an interrupt autodetect
149 * @val: mask of potential interrupts (unused)
151 * Scans the unused interrupt lines and returns the line which
152 * appears to have triggered the interrupt. If no interrupt was
153 * found then zero is returned. If more than one interrupt is
154 * found then minus the first candidate is returned to indicate
155 * their is doubt.
157 * The interrupt probe logic state is returned to its previous
158 * value.
160 * BUGS: When used in a module (which arguably shouldn't happen)
161 * nothing prevents two IRQ probe callers from overlapping. The
162 * results of this are non-optimal.
164 int probe_irq_off(unsigned long val)
166 int i, irq_found = 0, nr_irqs = 0;
168 for (i = 0; i < NR_IRQS; i++) {
169 struct irq_desc *desc = irq_desc + i;
170 unsigned int status;
172 spin_lock_irq(&desc->lock);
173 status = desc->status;
175 if (status & IRQ_AUTODETECT) {
176 if (!(status & IRQ_WAITING)) {
177 if (!nr_irqs)
178 irq_found = i;
179 nr_irqs++;
181 desc->status = status & ~IRQ_AUTODETECT;
182 desc->chip->shutdown(i);
184 spin_unlock_irq(&desc->lock);
186 mutex_unlock(&probing_active);
188 if (nr_irqs > 1)
189 irq_found = -irq_found;
191 return irq_found;
193 EXPORT_SYMBOL(probe_irq_off);