Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 12 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 19 | static DEFINE_MUTEX(probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame^] | 30 | unsigned long mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | irq_desc_t *desc; |
| 32 | unsigned int i; |
| 33 | |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 34 | mutex_lock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame^] | 43 | if (!desc->action) |
| 44 | desc->chip->startup(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | spin_unlock_irq(&desc->lock); |
| 46 | } |
| 47 | |
| 48 | /* Wait for longstanding interrupts to trigger. */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 49 | msleep(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
| 61 | desc->status |= IRQ_AUTODETECT | IRQ_WAITING; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 62 | if (desc->chip->startup(i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | desc->status |= IRQ_PENDING; |
| 64 | } |
| 65 | spin_unlock_irq(&desc->lock); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Wait for spurious interrupts to trigger |
| 70 | */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 71 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Now filter out any obviously spurious interrupts |
| 75 | */ |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame^] | 76 | mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | for (i = 0; i < NR_IRQS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | unsigned int status; |
| 79 | |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame^] | 80 | desc = irq_desc + i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 88 | desc->chip->shutdown(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } else |
| 90 | if (i < 32) |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame^] | 91 | mask |= 1 << i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | spin_unlock_irq(&desc->lock); |
| 94 | } |
| 95 | |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame^] | 96 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | irq_desc_t *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; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 130 | desc->chip->shutdown(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | spin_unlock_irq(&desc->lock); |
| 133 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 134 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | irq_desc_t *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; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 175 | desc->chip->shutdown(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | spin_unlock_irq(&desc->lock); |
| 178 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 179 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | if (nr_irqs > 1) |
| 182 | irq_found = -irq_found; |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return irq_found; |
| 185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | EXPORT_SYMBOL(probe_irq_off); |
| 187 | |