Register an Interrupt Handler With request_irq
Register, use and release an interrupt handler with request_irq and free_irq in a Linux 6.12 platform driver, with flags, return codes and common errors.

The call is one line, and most of the bugs that come with it hide in the arguments. A wrong dev_id breaks a shared line, a missing flag returns -EBUSY on the second board, and a handler registered before the device is ready fires into half-built state. This post walks through request_irq() in a Linux 6.12 platform driver: the signature, every argument, the return value the handler owes the core, the managed variant that removes most cleanup, and the failures you can expect. It is the step-by-step companion to the broader interrupt guide on this site, focused on this one call.
The signature
From include/linux/interrupt.h:
int request_irq(unsigned int irq, irq_handler_t handler,
unsigned long flags, const char *name, void *dev);Each argument matters:
| Argument | Meaning |
|---|---|
irq | The Linux IRQ number, from platform_get_irq(), pci_irq_vector() or similar. Never a hardware number or a constant |
handler | Function of type irqreturn_t (*)(int irq, void *dev_id) |
flags | IRQF_* bits: sharing, trigger type, thread behavior |
name | A string shown in /proc/interrupts. Use dev_name(dev) or the driver name |
dev | A cookie handed back to the handler as dev_id. Required, and must be unique, for IRQF_SHARED |
The function returns 0 on success and a negative errno on failure. It can sleep, so call it from process context only, such as probe(), never from an interrupt handler or while holding a spinlock.
The handler contract
The handler returns an irqreturn_t, and the value is how the core learns what happened:
IRQ_NONE: this device did not raise the interrupt. Needed for shared lines and the core's spurious-interrupt detection.IRQ_HANDLED: this handler serviced the interrupt.IRQ_WAKE_THREAD: wake the threaded half, only meaningful when registered withrequest_threaded_irq().
Return IRQ_HANDLED only when you actually had work. A handler that always says "handled" keeps a broken line alive, and one that always says "none" will get the line disabled after about 100,000 unhandled interrupts.
A complete example
This is a platform driver for a device with a 32-bit status register at offset 0 and an acknowledge register at offset 4. A set bit 0 in status means "data ready". The handler reads the status, acknowledges, and stores the value for the rest of the driver:
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#define REG_STATUS 0x0
#define REG_ACK 0x4
#define STATUS_READY BIT(0)
struct dr_dev {
void __iomem *base;
int irq;
atomic_t ready_count;
};
static irqreturn_t dr_isr(int irq, void *dev_id)
{
struct dr_dev *dr = dev_id;
u32 status = readl(dr->base + REG_STATUS);
if (!(status & STATUS_READY))
return IRQ_NONE;
writel(STATUS_READY, dr->base + REG_ACK);
atomic_inc(&dr->ready_count);
return IRQ_HANDLED;
}
static int dr_probe(struct platform_device *pdev)
{
struct dr_dev *dr;
int ret;
dr = devm_kzalloc(&pdev->dev, sizeof(*dr), GFP_KERNEL);
if (!dr)
return -ENOMEM;
dr->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(dr->base))
return PTR_ERR(dr->base);
dr->irq = platform_get_irq(pdev, 0);
if (dr->irq < 0)
return dr->irq;
ret = request_irq(dr->irq, dr_isr, 0, dev_name(&pdev->dev), dr);
if (ret) {
dev_err(&pdev->dev, "request_irq(%d) failed: %d\n", dr->irq, ret);
return ret;
}
platform_set_drvdata(pdev, dr);
dev_info(&pdev->dev, "registered handler on irq %d\n", dr->irq);
return 0;
}
static void dr_remove(struct platform_device *pdev)
{
struct dr_dev *dr = platform_get_drvdata(pdev);
free_irq(dr->irq, dr);
dev_info(&pdev->dev, "saw %d events\n", atomic_read(&dr->ready_count));
}The module registration (platform_driver struct, of_device_id table and module_platform_driver()) follows the usual pattern and is omitted to keep the focus. On a board where the device tree gives this device interrupt 54, loading the driver prints:
dataready 40002000.dr: registered handler on irq 54and after unbinding with three events counted:
dataready 40002000.dr: saw 3 eventsThree decisions in dr_probe are worth explaining. The dev_id is the device structure, not a global, so the handler works if two devices are present. The IRQ is requested after the memory and registers are mapped, so the handler cannot dereference something unset. And free_irq() in remove uses the same dev pointer that was given at registration, which is how the core picks the right handler on a shared line.
Flags that matter
Pass 0 for a dedicated line you own. Add flags for specific needs:
IRQF_SHARED: allow sharing. Every driver on the line must pass it.IRQF_TRIGGER_RISING,IRQF_TRIGGER_FALLING,IRQF_TRIGGER_HIGH,IRQF_TRIGGER_LOW: only when the platform firmware does not already set the trigger type. If the device tree specifies it, passing a conflicting one makes the call fail or log a mismatch, so prefer to leave it out.IRQF_NO_SUSPEND: keep the interrupt active while the system is suspending.IRQF_ONESHOT: used with threaded handlers, so a line stays masked until the thread finishes.
An interrupt line described as edge-triggered in the device tree, but wired to a device that holds the line low until serviced, is a classic cause of a driver that works once and hangs. The fix is in the description, not the driver.
Return values and what they mean
When request_irq() fails, the error tells you where to look:
| Error | Typical cause |
|---|---|
-EBUSY | The line is already registered, and either registration lacks IRQF_SHARED or flags conflict |
-EINVAL | Invalid IRQ number, NULL handler, IRQF_SHARED with a NULL dev_id, or a trigger flag the controller cannot do |
-ENOMEM | Allocation of the action structure failed |
-ENOSYS | The kernel was built without the IRQ support for that interrupt controller |
-EPROBE_DEFER | From platform_get_irq() when the interrupt controller has not yet been probed |
The -EPROBE_DEFER case is not an error. It tells the driver core to retry your probe later, after the controller driver appears. Return it unchanged; dev_err_probe() handles it without noisy logging.
The managed variant
devm_request_irq() has the same arguments with a device in front and no manual cleanup:
ret = devm_request_irq(&pdev->dev, dr->irq, dr_isr, 0,
dev_name(&pdev->dev), dr);
if (ret)
return dev_err_probe(&pdev->dev, ret, "cannot get irq\n");The registration is released automatically when the device is unbound, after remove() returns. That timing is the catch. If remove() frees something the handler uses before the managed release runs, the handler can fire against freed memory. Either order the teardown so the device stops raising interrupts first (write its enable register), or call devm_free_irq() explicitly at the top of remove(). For drivers whose remove does nothing but unmap, devm_request_irq() is the simpler and safer choice.
When the handler can run
The handler may be called as soon as request_irq() returns, even before the next line of your probe. That has one consequence: everything the handler touches must be initialized before the call. Initialize spinlocks, wait queues, work structures and counters first, then register, and enable the device's own interrupt generation last. A related hazard is on shared lines: another device's interrupt can call your handler at any time after registration, even if your device is not enabled, which is why the status check is mandatory.
To check registration worked, look at /proc/interrupts:
grep 40002000 /proc/interrupts 54: 0 0 0 0 GICv3 86 Level 40002000.drThe name column is the name argument, the "Level" says the trigger type came from the device tree, and zeros mean no interrupt has arrived yet.
Testing the failure paths
Two quick experiments show how well the code holds up. Load the driver twice for a non-shared line (bind a second device to the same interrupt), and the second request_irq() returns -EBUSY, with the log line from the dev_err in probe. And on a kernel with CONFIG_DEBUG_SHIRQ, the core calls every shared handler once at free_irq() time with interrupts disabled to verify that the handler tolerates a call when the device might be off. A handler that touches hardware without checking it is powered will fail this test, and that is the point of the option.


