b3a5549ea81eddf22cdb444e97e96377e27e2ba0
[safe/jmp/linux-2.6] / kernel / irq / autoprobe.c
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
9 #include <linux/irq.h>
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13
14 #include "internals.h"
15
16 /*
17  * Autodetection depends on the fact that any interrupt that
18  * comes in on to an unassigned handler will get stuck with
19  * "IRQ_WAITING" cleared and the interrupt disabled.
20  */
21 static DEFINE_MUTEX(probing_active);
22
23 /**
24  *      probe_irq_on    - begin an interrupt autodetect
25  *
26  *      Commence probing for an interrupt. The interrupts are scanned
27  *      and a mask of potential interrupt lines is returned.
28  *
29  */
30 unsigned long probe_irq_on(void)
31 {
32         struct irq_desc *desc;
33         unsigned long mask;
34         unsigned int i;
35
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 (i = nr_irqs-1; i > 0; i--) {
42                 desc = irq_to_desc(i);
43                 if (!desc)
44                         continue;
45
46                 spin_lock_irq(&desc->lock);
47                 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
48                         /*
49                          * An old-style architecture might still have
50                          * the handle_bad_irq handler there:
51                          */
52                         compat_irq_chip_set_default_handler(desc);
53
54                         /*
55                          * Some chips need to know about probing in
56                          * progress:
57                          */
58                         if (desc->chip->set_type)
59                                 desc->chip->set_type(i, IRQ_TYPE_PROBE);
60                         desc->chip->startup(i);
61                 }
62                 spin_unlock_irq(&desc->lock);
63         }
64
65         /* Wait for longstanding interrupts to trigger. */
66         msleep(20);
67
68         /*
69          * enable any unassigned irqs
70          * (we must startup again here because if a longstanding irq
71          * happened in the previous stage, it may have masked itself)
72          */
73         for (i = nr_irqs-1; i > 0; i--) {
74                 desc = irq_to_desc(i);
75                 if (!desc)
76                         continue;
77
78                 spin_lock_irq(&desc->lock);
79                 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
80                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
81                         if (desc->chip->startup(i))
82                                 desc->status |= IRQ_PENDING;
83                 }
84                 spin_unlock_irq(&desc->lock);
85         }
86
87         /*
88          * Wait for spurious interrupts to trigger
89          */
90         msleep(100);
91
92         /*
93          * Now filter out any obviously spurious interrupts
94          */
95         mask = 0;
96         for (i = 0; i < nr_irqs; i++) {
97                 unsigned int status;
98
99                 desc = irq_to_desc(i);
100                 if (!desc)
101                         continue;
102                 spin_lock_irq(&desc->lock);
103                 status = desc->status;
104
105                 if (status & IRQ_AUTODETECT) {
106                         /* It triggered already - consider it spurious. */
107                         if (!(status & IRQ_WAITING)) {
108                                 desc->status = status & ~IRQ_AUTODETECT;
109                                 desc->chip->shutdown(i);
110                         } else
111                                 if (i < 32)
112                                         mask |= 1 << i;
113                 }
114                 spin_unlock_irq(&desc->lock);
115         }
116
117         return mask;
118 }
119 EXPORT_SYMBOL(probe_irq_on);
120
121 /**
122  *      probe_irq_mask - scan a bitmap of interrupt lines
123  *      @val:   mask of interrupts to consider
124  *
125  *      Scan the interrupt lines and return a bitmap of active
126  *      autodetect interrupts. The interrupt probe logic state
127  *      is then returned to its previous value.
128  *
129  *      Note: we need to scan all the irq's even though we will
130  *      only return autodetect irq numbers - just so that we reset
131  *      them all to a known state.
132  */
133 unsigned int probe_irq_mask(unsigned long val)
134 {
135         unsigned int mask;
136         int i;
137
138         mask = 0;
139         for (i = 0; i < nr_irqs; i++) {
140                 struct irq_desc *desc = irq_to_desc(i);
141                 unsigned int status;
142
143                 if (!desc)
144                         continue;
145                 spin_lock_irq(&desc->lock);
146                 status = desc->status;
147
148                 if (status & IRQ_AUTODETECT) {
149                         if (i < 16 && !(status & IRQ_WAITING))
150                                 mask |= 1 << i;
151
152                         desc->status = status & ~IRQ_AUTODETECT;
153                         desc->chip->shutdown(i);
154                 }
155                 spin_unlock_irq(&desc->lock);
156         }
157         mutex_unlock(&probing_active);
158
159         return mask & val;
160 }
161 EXPORT_SYMBOL(probe_irq_mask);
162
163 /**
164  *      probe_irq_off   - end an interrupt autodetect
165  *      @val: mask of potential interrupts (unused)
166  *
167  *      Scans the unused interrupt lines and returns the line which
168  *      appears to have triggered the interrupt. If no interrupt was
169  *      found then zero is returned. If more than one interrupt is
170  *      found then minus the first candidate is returned to indicate
171  *      their is doubt.
172  *
173  *      The interrupt probe logic state is returned to its previous
174  *      value.
175  *
176  *      BUGS: When used in a module (which arguably shouldn't happen)
177  *      nothing prevents two IRQ probe callers from overlapping. The
178  *      results of this are non-optimal.
179  */
180 int probe_irq_off(unsigned long val)
181 {
182         int i, irq_found = 0, nr_irqs = 0;
183
184         for (i = 0; i < nr_irqs; i++) {
185                 struct irq_desc *desc = irq_to_desc(i);
186                 unsigned int status;
187
188                 if (!desc)
189                         continue;
190                 spin_lock_irq(&desc->lock);
191                 status = desc->status;
192
193                 if (status & IRQ_AUTODETECT) {
194                         if (!(status & IRQ_WAITING)) {
195                                 if (!nr_irqs)
196                                         irq_found = i;
197                                 nr_irqs++;
198                         }
199                         desc->status = status & ~IRQ_AUTODETECT;
200                         desc->chip->shutdown(i);
201                 }
202                 spin_unlock_irq(&desc->lock);
203         }
204         mutex_unlock(&probing_active);
205
206         if (nr_irqs > 1)
207                 irq_found = -irq_found;
208
209         return irq_found;
210 }
211 EXPORT_SYMBOL(probe_irq_off);
212