2 * linux/kernel/irq/manage.c
4 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
6 * This file contains driver APIs to the irq subsystem.
10 #include <linux/module.h>
11 #include <linux/random.h>
12 #include <linux/interrupt.h>
14 #include "internals.h"
19 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
21 * This function waits for any pending IRQ handlers for this interrupt
22 * to complete before returning. If you use this function while
23 * holding a resource the IRQ handler may need you will deadlock.
25 * This function may be called - with care - from IRQ context.
27 void synchronize_irq(unsigned int irq)
29 struct irq_desc *desc = irq_desc + irq;
31 while (desc->status & IRQ_INPROGRESS)
35 EXPORT_SYMBOL(synchronize_irq);
40 * disable_irq_nosync - disable an irq without waiting
41 * @irq: Interrupt to disable
43 * Disable the selected interrupt line. Disables and Enables are
45 * Unlike disable_irq(), this function does not ensure existing
46 * instances of the IRQ handler have completed before returning.
48 * This function may be called from IRQ context.
50 void disable_irq_nosync(unsigned int irq)
52 irq_desc_t *desc = irq_desc + irq;
55 spin_lock_irqsave(&desc->lock, flags);
57 desc->status |= IRQ_DISABLED;
58 desc->handler->disable(irq);
60 spin_unlock_irqrestore(&desc->lock, flags);
63 EXPORT_SYMBOL(disable_irq_nosync);
66 * disable_irq - disable an irq and wait for completion
67 * @irq: Interrupt to disable
69 * Disable the selected interrupt line. Enables and Disables are
71 * This function waits for any pending IRQ handlers for this interrupt
72 * to complete before returning. If you use this function while
73 * holding a resource the IRQ handler may need you will deadlock.
75 * This function may be called - with care - from IRQ context.
77 void disable_irq(unsigned int irq)
79 irq_desc_t *desc = irq_desc + irq;
81 disable_irq_nosync(irq);
86 EXPORT_SYMBOL(disable_irq);
89 * enable_irq - enable handling of an irq
90 * @irq: Interrupt to enable
92 * Undoes the effect of one call to disable_irq(). If this
93 * matches the last disable, processing of interrupts on this
94 * IRQ line is re-enabled.
96 * This function may be called from IRQ context.
98 void enable_irq(unsigned int irq)
100 irq_desc_t *desc = irq_desc + irq;
103 spin_lock_irqsave(&desc->lock, flags);
104 switch (desc->depth) {
109 unsigned int status = desc->status & ~IRQ_DISABLED;
111 desc->status = status;
112 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
113 desc->status = status | IRQ_REPLAY;
114 hw_resend_irq(desc->handler,irq);
116 desc->handler->enable(irq);
122 spin_unlock_irqrestore(&desc->lock, flags);
125 EXPORT_SYMBOL(enable_irq);
128 * Internal function that tells the architecture code whether a
129 * particular irq has been exclusively allocated or is available
132 int can_request_irq(unsigned int irq, unsigned long irqflags)
134 struct irqaction *action;
139 action = irq_desc[irq].action;
141 if (irqflags & action->flags & SA_SHIRQ)
148 * Internal function to register an irqaction - typically used to
149 * allocate special interrupts that are part of the architecture.
151 int setup_irq(unsigned int irq, struct irqaction * new)
153 struct irq_desc *desc = irq_desc + irq;
154 struct irqaction *old, **p;
158 if (desc->handler == &no_irq_type)
161 * Some drivers like serial.c use request_irq() heavily,
162 * so we have to be careful not to interfere with a
165 if (new->flags & SA_SAMPLE_RANDOM) {
167 * This function might sleep, we want to call it first,
168 * outside of the atomic block.
169 * Yes, this might clear the entropy pool if the wrong
170 * driver is attempted to be loaded, without actually
171 * installing a new handler, but is this really a problem,
172 * only the sysadmin is able to do this.
174 rand_initialize_irq(irq);
178 * The following block of code has to be executed atomically
180 spin_lock_irqsave(&desc->lock,flags);
182 if ((old = *p) != NULL) {
183 /* Can't share interrupts unless both agree to */
184 if (!(old->flags & new->flags & SA_SHIRQ)) {
185 spin_unlock_irqrestore(&desc->lock,flags);
189 /* add new interrupt at end of irq queue */
201 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
202 IRQ_WAITING | IRQ_INPROGRESS);
203 if (desc->handler->startup)
204 desc->handler->startup(irq);
206 desc->handler->enable(irq);
208 spin_unlock_irqrestore(&desc->lock,flags);
211 register_irq_proc(irq);
213 register_handler_proc(irq, new);
219 * free_irq - free an interrupt
220 * @irq: Interrupt line to free
221 * @dev_id: Device identity to free
223 * Remove an interrupt handler. The handler is removed and if the
224 * interrupt line is no longer in use by any driver it is disabled.
225 * On a shared IRQ the caller must ensure the interrupt is disabled
226 * on the card it drives before calling this function. The function
227 * does not return until any executing interrupts for this IRQ
230 * This function must not be called from interrupt context.
232 void free_irq(unsigned int irq, void *dev_id)
234 struct irq_desc *desc;
235 struct irqaction **p;
241 desc = irq_desc + irq;
242 spin_lock_irqsave(&desc->lock,flags);
245 struct irqaction * action = *p;
248 struct irqaction **pp = p;
251 if (action->dev_id != dev_id)
254 /* Found it - now remove it from the list of entries */
257 desc->status |= IRQ_DISABLED;
258 if (desc->handler->shutdown)
259 desc->handler->shutdown(irq);
261 desc->handler->disable(irq);
263 spin_unlock_irqrestore(&desc->lock,flags);
264 unregister_handler_proc(irq, action);
266 /* Make sure it's not being used on another CPU */
267 synchronize_irq(irq);
271 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
272 spin_unlock_irqrestore(&desc->lock,flags);
277 EXPORT_SYMBOL(free_irq);
280 * request_irq - allocate an interrupt line
281 * @irq: Interrupt line to allocate
282 * @handler: Function to be called when the IRQ occurs
283 * @irqflags: Interrupt type flags
284 * @devname: An ascii name for the claiming device
285 * @dev_id: A cookie passed back to the handler function
287 * This call allocates interrupt resources and enables the
288 * interrupt line and IRQ handling. From the point this
289 * call is made your handler function may be invoked. Since
290 * your handler function must clear any interrupt the board
291 * raises, you must take care both to initialise your hardware
292 * and to set up the interrupt handler in the right order.
294 * Dev_id must be globally unique. Normally the address of the
295 * device data structure is used as the cookie. Since the handler
296 * receives this value it makes sense to use it.
298 * If your interrupt is shared you must pass a non NULL dev_id
299 * as this is required when freeing the interrupt.
303 * SA_SHIRQ Interrupt is shared
304 * SA_INTERRUPT Disable local interrupts while processing
305 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
308 int request_irq(unsigned int irq,
309 irqreturn_t (*handler)(int, void *, struct pt_regs *),
310 unsigned long irqflags, const char * devname, void *dev_id)
312 struct irqaction * action;
316 * Sanity-check: shared interrupts must pass in a real dev-ID,
317 * otherwise we'll have trouble later trying to figure out
318 * which interrupt is which (messes up the interrupt freeing
321 if ((irqflags & SA_SHIRQ) && !dev_id)
328 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
332 action->handler = handler;
333 action->flags = irqflags;
334 cpus_clear(action->mask);
335 action->name = devname;
337 action->dev_id = dev_id;
339 retval = setup_irq(irq, action);
346 EXPORT_SYMBOL(request_irq);