Here is the basic kernel module that I have coded:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
// irq number for keyboard is 1
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);
This module counts the number of generated keyboard interrupts and prints it to the kernel printk circular buffer.
The problem is that the keyboard_handler is never getting executed.
Some debugging information that I want to share:
Before loading this module, the output of sudo cat /proc/interrupts | less is
CPU0 CPU1 CPU2 CPU3 CPU4 CPU5 CPU6 CPU7 CPU8 CPU9 CPU10 CPU11 CPU12 CPU13 CPU14 CPU15
1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 93 IR-IO-APIC 1-edge i8042
After loading the module, the output of the same command is
CPU0 CPU1 CPU2 CPU3 CPU4 CPU5 CPU6 CPU7 CPU8 CPU9 CPU10 CPU11 CPU12 CPU13 CPU14 CPU15
1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 93 IR-IO-APIC 1-edge i8042, my_keyboard_handler
This suggests that my request_irq function call has registered my keyboard_handler successfully.
The dmesg output is:
[48485.839232] test_interrupt_init: In init
[48504.831010] test_interrupt_exit: In exit
The steps I have followed to get this dmesg output:
$ sudo insmod hello.ko
$ sdsds
sdsds: command not found
$ sudo rmmod hello
Details of the machine where I was trying to execute this code:
$ uname -r
5.18.0-4mx-amd64
I am using the MX-Linux distro
I was expecting the keyboard_handler function to get executed that will increase the counter variable to each keyboard interrupt and print its value in the kernel print buffer.
--Edited--
Note: The interrupt count value in my /proc/interrupts for IRQ1 is also not incrementing though I am pressing the keyboard buttons. Thanks to @Ian Abbott for pointing it out.
same question. Wonder if it has been solved...