I was working on a project where I was doing some action based on the keyboard interrupt received. So I registered for the keyboard interrupt and mark the interrupt as shared. But my program wasnt working as it should. To debug the issue, I wrote a small program that just registers for the keyboard interrupt and outputs some message to kernel when interrupt from keyboard is received. But I found out a lot of interrupts from keyboard are missed when interrupt rate is high. For example, in the sample code below, I register for the interrupt and output message to kernel when 'a' is pressed. From the counter value, the total number of interrupts are less than the actual number of interrupts geenrated by keyboard. I found this out by echoing the keypress to a terminal and counting the number of a's echoed to the terminal. So for example if there are 350 echoed a's on the terminal, the count will only be around 300-320. Same goes for higher number of interrupts. The larger the number of interrupts, the more interrupts are missed but the percentage of interrupts missed seems to be nearly equal in all cases. Can someone please explain what is the reason for this behaviour? Thanks in advance. Below is source code.
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/signal.h>
#include <asm/io.h>
/*
The keyboard ISR
*/
irq_handler_t irq_handler (int irq, void *dev_id, struct pt_regs *regs)
{
static int count = 0;
static unsigned char scancode;
scancode = inb (0x60);
if (0x1E == scancode)
{
count++;
printk ("\nKeyboard key 'a' pressed !");
}
return (irq_handler_t) IRQ_HANDLED;
}
/*
Register IRQ handler
*/
int init_module ()
{
return request_irq (1, (irq_handler_t) irq_handler, IRQF_SHARED, "kbd_irq_handler", (void *)(irq_handler));
}
MODULE_LICENSE ("GPL");
printk
in an interrupt handler is not such a good idea ... – too honest for this site