[PATCH] genirq: add IRQ_NOPROBE support
[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 /*
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.
18  */
19 static DEFINE_MUTEX(probing_active);
20
21 /**
22  *      probe_irq_on    - begin an interrupt autodetect
23  *
24  *      Commence probing for an interrupt. The interrupts are scanned
25  *      and a mask of potential interrupt lines is returned.
26  *
27  */
28 unsigned long probe_irq_on(void)
29 {
30         struct irq_desc *desc;
31         unsigned long mask;
32         unsigned int i;
33
34         mutex_lock(&probing_active);
35         /*
36          * something may have generated an irq long ago and we want to
37          * flush such a longstanding irq before considering it as spurious.
38          */
39         for (i = NR_IRQS-1; i > 0; i--) {
40                 desc = irq_desc + i;
41
42                 spin_lock_irq(&desc->lock);
43                 if (!desc->action && !(desc->status & IRQ_NOPROBE))
44                         desc->chip->startup(i);
45                 spin_unlock_irq(&desc->lock);
46         }
47
48         /* Wait for longstanding interrupts to trigger. */
49         msleep(20);
50
51         /*
52          * enable any unassigned irqs
53          * (we must startup again here because if a longstanding irq
54          * happened in the previous stage, it may have masked itself)
55          */
56         for (i = NR_IRQS-1; i > 0; i--) {
57                 desc = irq_desc + i;
58
59                 spin_lock_irq(&desc->lock);
60                 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
61                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
62                         if (desc->chip->startup(i))
63                                 desc->status |= IRQ_PENDING;
64                 }
65                 spin_unlock_irq(&desc->lock);
66         }
67
68         /*
69          * Wait for spurious interrupts to trigger
70          */
71         msleep(100);
72
73         /*
74          * Now filter out any obviously spurious interrupts
75          */
76         mask = 0;
77         for (i = 0; i < NR_IRQS; i++) {
78                 unsigned int status;
79
80                 desc = irq_desc + i;
81                 spin_lock_irq(&desc->lock);
82                 status = desc->status;
83
84                 if (status & IRQ_AUTODETECT) {
85                         /* It triggered already - consider it spurious. */
86                         if (!(status & IRQ_WAITING)) {
87                                 desc->status = status & ~IRQ_AUTODETECT;
88                                 desc->chip->shutdown(i);
89                         } else
90                                 if (i < 32)
91                                         mask |= 1 << i;
92                 }
93                 spin_unlock_irq(&desc->lock);
94         }
95
96         return mask;
97 }
98 EXPORT_SYMBOL(probe_irq_on);
99
100 /**
101  *      probe_irq_mask - scan a bitmap of interrupt lines
102  *      @val:   mask of interrupts to consider
103  *
104  *      Scan the interrupt lines and return a bitmap of active
105  *      autodetect interrupts. The interrupt probe logic state
106  *      is then returned to its previous value.
107  *
108  *      Note: we need to scan all the irq's even though we will
109  *      only return autodetect irq numbers - just so that we reset
110  *      them all to a known state.
111  */
112 unsigned int probe_irq_mask(unsigned long val)
113 {
114         unsigned int mask;
115         int i;
116
117         mask = 0;
118         for (i = 0; i < NR_IRQS; i++) {
119                 struct irq_desc *desc = irq_desc + i;
120                 unsigned int status;
121
122                 spin_lock_irq(&desc->lock);
123                 status = desc->status;
124
125                 if (status & IRQ_AUTODETECT) {
126                         if (i < 16 && !(status & IRQ_WAITING))
127                                 mask |= 1 << i;
128
129                         desc->status = status & ~IRQ_AUTODETECT;
130                         desc->chip->shutdown(i);
131                 }
132                 spin_unlock_irq(&desc->lock);
133         }
134         mutex_unlock(&probing_active);
135
136         return mask & val;
137 }
138 EXPORT_SYMBOL(probe_irq_mask);
139
140 /**
141  *      probe_irq_off   - end an interrupt autodetect
142  *      @val: mask of potential interrupts (unused)
143  *
144  *      Scans the unused interrupt lines and returns the line which
145  *      appears to have triggered the interrupt. If no interrupt was
146  *      found then zero is returned. If more than one interrupt is
147  *      found then minus the first candidate is returned to indicate
148  *      their is doubt.
149  *
150  *      The interrupt probe logic state is returned to its previous
151  *      value.
152  *
153  *      BUGS: When used in a module (which arguably shouldn't happen)
154  *      nothing prevents two IRQ probe callers from overlapping. The
155  *      results of this are non-optimal.
156  */
157 int probe_irq_off(unsigned long val)
158 {
159         int i, irq_found = 0, nr_irqs = 0;
160
161         for (i = 0; i < NR_IRQS; i++) {
162                 struct irq_desc *desc = irq_desc + i;
163                 unsigned int status;
164
165                 spin_lock_irq(&desc->lock);
166                 status = desc->status;
167
168                 if (status & IRQ_AUTODETECT) {
169                         if (!(status & IRQ_WAITING)) {
170                                 if (!nr_irqs)
171                                         irq_found = i;
172                                 nr_irqs++;
173                         }
174                         desc->status = status & ~IRQ_AUTODETECT;
175                         desc->chip->shutdown(i);
176                 }
177                 spin_unlock_irq(&desc->lock);
178         }
179         mutex_unlock(&probing_active);
180
181         if (nr_irqs > 1)
182                 irq_found = -irq_found;
183
184         return irq_found;
185 }
186 EXPORT_SYMBOL(probe_irq_off);
187