我正在编写一个示例request_irq代码
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
int irq = 1;
int dev = 0xaa;
static irqreturn_t keyboard_handler(int irq, void *dev)
{
static int counter = 0;
pr_info("Keyboard Counter:%d\n", counter++);
return IRQ_NONE;
}
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);
free_irq(irq, &dev);
}
module_init(test_interrupt_init);
module_exit(test_interrupt_exit);
在删除此模块时,出现错误:
rmmod: ERROR: ../libkmod/libkmod-module.c:769 kmod_module_remove_module() could not remove 'interrupt': Device or resource busy
rmmod: ERROR: could not remove module interrupt: Device or resource busy
为什么出现此错误,因为我正在使用IRQF_SHARED并在request_irq和free_irq中传递了dev参数
更新:从dmesg中,我在rmmod上看不到任何printk 添加了dmesg日志:
[ 4007.223281] Keyboard Counter:884
[ 4007.560846] Keyboard Counter:885
[ 4007.670339] Keyboard Counter:886
[ 4008.240139] Keyboard Counter:887
[ 4008.275412] Keyboard Counter:888
[ 4008.359338] Keyboard Counter:889
[ 4008.425101] Keyboard Counter:890
[ 4008.433026] Keyboard Counter:891
[ 4008.501763] Keyboard Counter:892
[ 4008.579201] Keyboard Counter:893
[ 4008.653335] Keyboard Counter:894
[ 4008.682862] Keyboard Counter:895
[ 4008.733950] Keyboard Counter:896
$ cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 1 0 0 0 IO-APIC 2-edge timer
1: 449 0 0 707 IO-APIC 1-edge i8042, my_keyboard_handler
6: 0 0 2 0 IO-APIC 6-edge floppy
7: 0 0 0 0 IO-APIC 7-edge parport0
8: 0 1 0 0 IO-APIC 8-edge rtc0
9: 0 0 0 0 IO-APIC 9-fasteoi acpi
答案 0 :(得分:0)
尝试一下:
static int irq = 1, dev = 0xaa;
您需要授予变量irq
的权限,然后像下面这样调用module_param()
module_param(irq, int, S_IRUGO);
示例代码
MODULE_LICENSE("GPL");
static int irq = 1, dev = 0xaa, counter = 0;
module_param(irq, int, S_IRUGO); /* give the permission to user space variable */
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__);
if(request_irq(irq, keyboard_handler, IRQF_SHARED,"my_keyboard_handler", &dev)) {
return -1;
}
else
{
pr_info("success \n");
return 0;
}
}
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);