]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - kernel/irq/autoprobe.c
Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[linux-3.10.git] / 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 = 0;
34         unsigned int status;
35         int i;
36
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                 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_each_irq_desc_reverse(i, desc) {
74                 if (!desc)
75                         continue;
76
77                 spin_lock_irq(&desc->lock);
78                 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
79                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
80                         if (desc->chip->startup(i))
81                                 desc->status |= IRQ_PENDING;
82                 }
83                 spin_unlock_irq(&desc->lock);
84         }
85
86         /*
87          * Wait for spurious interrupts to trigger
88          */
89         msleep(100);
90
91         /*
92          * Now filter out any obviously spurious interrupts
93          */
94         for_each_irq_desc(i, desc) {
95                 if (!desc)
96                         continue;
97
98                 spin_lock_irq(&desc->lock);
99                 status = desc->status;
100
101                 if (status & IRQ_AUTODETECT) {
102                         /* It triggered already - consider it spurious. */
103                         if (!(status & IRQ_WAITING)) {
104                                 desc->status = status & ~IRQ_AUTODETECT;
105                                 desc->chip->shutdown(i);
106                         } else
107                                 if (i < 32)
108                                         mask |= 1 << i;
109                 }
110                 spin_unlock_irq(&desc->lock);
111         }
112
113         return mask;
114 }
115 EXPORT_SYMBOL(probe_irq_on);
116
117 /**
118  *      probe_irq_mask - scan a bitmap of interrupt lines
119  *      @val:   mask of interrupts to consider
120  *
121  *      Scan the interrupt lines and return a bitmap of active
122  *      autodetect interrupts. The interrupt probe logic state
123  *      is then returned to its previous value.
124  *
125  *      Note: we need to scan all the irq's even though we will
126  *      only return autodetect irq numbers - just so that we reset
127  *      them all to a known state.
128  */
129 unsigned int probe_irq_mask(unsigned long val)
130 {
131         unsigned int status, mask = 0;
132         struct irq_desc *desc;
133         int i;
134
135         for_each_irq_desc(i, desc) {
136                 if (!desc)
137                         continue;
138
139                 spin_lock_irq(&desc->lock);
140                 status = desc->status;
141
142                 if (status & IRQ_AUTODETECT) {
143                         if (i < 16 && !(status & IRQ_WAITING))
144                                 mask |= 1 << i;
145
146                         desc->status = status & ~IRQ_AUTODETECT;
147                         desc->chip->shutdown(i);
148                 }
149                 spin_unlock_irq(&desc->lock);
150         }
151         mutex_unlock(&probing_active);
152
153         return mask & val;
154 }
155 EXPORT_SYMBOL(probe_irq_mask);
156
157 /**
158  *      probe_irq_off   - end an interrupt autodetect
159  *      @val: mask of potential interrupts (unused)
160  *
161  *      Scans the unused interrupt lines and returns the line which
162  *      appears to have triggered the interrupt. If no interrupt was
163  *      found then zero is returned. If more than one interrupt is
164  *      found then minus the first candidate is returned to indicate
165  *      their is doubt.
166  *
167  *      The interrupt probe logic state is returned to its previous
168  *      value.
169  *
170  *      BUGS: When used in a module (which arguably shouldn't happen)
171  *      nothing prevents two IRQ probe callers from overlapping. The
172  *      results of this are non-optimal.
173  */
174 int probe_irq_off(unsigned long val)
175 {
176         int i, irq_found = 0, nr_of_irqs = 0;
177         struct irq_desc *desc;
178         unsigned int status;
179
180         for_each_irq_desc(i, desc) {
181                 if (!desc)
182                         continue;
183
184                 spin_lock_irq(&desc->lock);
185                 status = desc->status;
186
187                 if (status & IRQ_AUTODETECT) {
188                         if (!(status & IRQ_WAITING)) {
189                                 if (!nr_of_irqs)
190                                         irq_found = i;
191                                 nr_of_irqs++;
192                         }
193                         desc->status = status & ~IRQ_AUTODETECT;
194                         desc->chip->shutdown(i);
195                 }
196                 spin_unlock_irq(&desc->lock);
197         }
198         mutex_unlock(&probing_active);
199
200         if (nr_of_irqs > 1)
201                 irq_found = -irq_found;
202
203         return irq_found;
204 }
205 EXPORT_SYMBOL(probe_irq_off);
206