Linux Device Driver for counting keyboard strokes
For each key pressed on the keyboard, it generates two interrupts (pressed and release).
Code:
Output:
Notes:
1. IRQ line 1 is assigned for keyboard, we register our handler to be called on keyboard interrupt by calling request_irq
2. IRQF_SHARED flag is used for sharing the keyboard interrupt
3. synchronize_irq waits for the specific handler to exit.
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/kernel.h> | |
#include <linux/module.h> | |
#include <linux/interrupt.h> | |
#include <linux/delay.h> | |
MODULE_LICENSE("GPL"); | |
static int irq = 1, dev = 0xaa, counter = 0; | |
static irqreturn_t keyboard_handler(int irq, void *dev) | |
{ | |
pr_info("Keyboard Counter:%d\n", counter++); | |
return IRQ_NONE; | |
} | |
/* registering irq */ | |
static int test_interrupt_init(void) | |
{ | |
pr_info("%s: In init\n", __func__); | |
return request_irq(irq, keyboard_handler, IRQF_SHARED,"my_keyboard_handler", &dev); | |
} | |
static void test_interrupt_exit(void) | |
{ | |
pr_info("%s: In exit\n", __func__); | |
synchronize_irq(irq); /* synchronize interrupt */ | |
free_irq(irq, &dev); | |
} | |
module_init(test_interrupt_init); | |
module_exit(test_interrupt_exit); |
Output:
Notes:
1. IRQ line 1 is assigned for keyboard, we register our handler to be called on keyboard interrupt by calling request_irq
2. IRQF_SHARED flag is used for sharing the keyboard interrupt
3. synchronize_irq waits for the specific handler to exit.
Comments
Post a Comment