Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interrupt handling in Device Driver

I have written a simple character driver and requested IRQ on a gpio pin and wrtten a handler for it.

err = request_irq( irq, irq_handler,IRQF_SHARED | IRQF_TRIGGER_RISING, INTERRUPT_DEVICE_NAME, raspi_gpio_devp);

static irqreturn_t irq_handler(int irq, void *arg);

now from theory i know that Upon interrupt the interrupt Controller with tell the processor to call do_IRQ() which will check the IDT and call my interrupt handler for this line.

how does the kernel know that the interrupt handler was for this particular device file

Also I know that Interrupt handlers do not run in any process context. But let say I am accessing any variable declared out side scope of handler, a static global flag = 0, In the handler I make flag = 1 indicating that an interrupt has occurred. That variable is in process context. So I am confused how this handler not in any process context modify a variable in process context.

Thanks

like image 737
Haswell Avatar asked Nov 20 '14 04:11

Haswell


1 Answers

The kernel does not know that this particular interrupt is for a particular device.

The only thing it knows is that it must call irq_handler with raspi_gpio_devp as a parameter. (like this: irq_handler(irq, raspi_gpio_devp)).

If your irq line is shared, you should check if your device generated an IRQ or not. Code:

int irq_handler(int irq, void* dev_id) {
    struct raspi_gpio_dev *raspi_gpio_devp = (struct raspi_gpio_dev *) dev_id;
    if (!my_gpio_irq_occured(raspi_gpio_devp))
        return IRQ_NONE;
    /* do stuff here */
    return IRQ_HANDLED;
}

The interrupt handler runs in interrupt context. But you can access static variables declared outside the scope of the interrupt.

Usually, what an interrupt handler does is:

  • check interrupt status
  • retrieve information from the hardware and store it somewhere (a buffer/fifo for example)
  • wake_up() a kernel process waiting for that information

If you want to be really confident with the do and don't of interrupt handling, the best thing to read about is what a process is for the kernel.

An excellent book dealing with this is Linux Kernel Developpement by Robert Love.

like image 128
Gilles Gregoire Avatar answered Oct 21 '22 23:10

Gilles Gregoire