键盘中断处理程序导致系统冻结

时间:2016-12-06 23:24:12

标签: linux-kernel linux-device-driver interrupt-handling

这是我的第一个堆栈溢出帖(长时间潜伏),如果这个问题措辞不当,请提前抱歉。

当用户按下按钮时,我试图让内核模块模拟按键,并以http://www.staerk.de/thorsten/My_Tutorials/Writing_Linux_kernel_modules为例。

模块初始化时会模拟按键,但是当我发送中断并尝试运行相同的代码时,整个虚拟机会冻结。

以下是我的代码的信息:

static void got_char(struct work_struct *taskp) 
{
    struct myprivate *myp = container_of(taskp, struct myprivate, task);

    if ((myp->scancode == 0x01) || (myp->scancode == 0x81))
    {
        printk ("You pressed Esc !\n");
        println ("Pressed ESC ! \n");
        ch=65;
        tty_insert_flip_char(tty, ch, 0);
        con_schedule_flip(tty);
    }
    else if (myp->scancode == 0x1D) {
        printk ("You pressed Ctrl!\n");
    }
    else {
        printk("Scancode = %d", myp->scancode);
    }
}

irq_handler_t irq_handler(int irq, void *dev_id, struct pt_regs *regs)
{
    static int initialised = 0;
    /*
    * Read keyboard status
    */
    myp->scancode = inb(0x60);

    if (initialised == 0) {
        INIT_WORK(&myp->task, got_char);
        initialised = 1;
    }
    else {
        PREPARE_WORK(&myp->task, got_char);
    }
    schedule_work(&myp->task);

    return (irq_handler_t) IRQ_HANDLED;
}

/* Helper method to print stuff to the terminal */
static void println(char *string)
{
    tty = current->signal->tty;
    (tty->driver->ops)->write (tty, string, strlen(string));
    ((tty->driver->ops)->write) (tty, "\015\012", 2);
}

/* Initialize the module and Register the IRQ handler */
static int __init keybrd_int_register(void)
{
    myp = kmalloc(sizeof (*myp), GFP_KERNEL);

    int result;
    /* Request IRQ 1, the keyboard IRQ */    
    result = request_irq (1, (irq_handler_t) irq_handler, IRQF_SHARED, "keyboard_stats_irq", (void *)(irq_handler));

    /* Test simulating keypress */
    println ("inserting A ! \n");
    ch=65;
    tty_insert_flip_char(tty, ch, 0);
    con_schedule_flip(tty);

    if (result)
        printk(KERN_INFO "can't get shared interrupt for keyboard\n");

    return result;
}

除了按下ESC之外,一切都按预期工作,然后整个系统冻结,我必须重新启动我的虚拟机。

我在线查看了很多帖子和论坛,但无法找到答案。 任何建议将不胜感激,提前谢谢。

0 个答案:

没有答案
相关问题