2

I am practicing writing a simple keyboard driver in VirtualBox guest Linux. The problem is, my code just register an interrupt handler and print scancode to the log file. And I don't send those incoming scancode to any upper level codes, like Linux input core. After insmod, I can see those captured scancode using dmesg. But why my terminal still gets correct input? There should not be anything received by the terminal.

My code looks like this:

static int __init init_simple_keyboard_driver(void)
{
    free_irq (IRQ_1, NULL);
    return request_irq (IRQ_1, my_handler, ...);
}

static irqreturn_t my_handler(int irq, void *dev_id)
{
    unsigned char scancode = get_scancode_from_port_0x60();
    printk(...scancode...);
}

After insmod, I can see messages in the kernel log.

  1. My free_irq call causes some messages like Can't free already freed IRQ. (I don't know why... It should not be freed already.)
  2. atkbd driver complains that there is someone ask to handle IRQ_1 instead.
  3. Those scancode can be correctly printed.
  4. [The Most Weird One] The active console still gets correct keyboard input. Thus I can just perform a rmmod using this simple driver.
  5. After rmmod, the guest Linux just dead because it can't receive any keyboard anymore.

Do you have any idea? Thank you!

5
  • So, you're trying to write a driver to manage hardware that is already being managed by an existing driver (rather than, say, attaching a second keyboard for your driver to play with)? I'm thinking that's probably not really feasible...
    – twalberg
    Sep 14, 2012 at 20:23
  • @twalberg Thank you for your comment. Yes, I am writing a driver to replace the original one. Is that impossible? And would you please tell me the right way to do so? Only load it at boot time? Sep 14, 2012 at 20:29
  • @twalberg And, I just cat /proc/interrupts and saw my_handler together with the original i8042 on IRQ 1. I think that means I failed to free the original interrupt. Sep 14, 2012 at 20:32
  • 1
    It's not impossible, but, unless you have a way to reliably make the original driver stop managing the device so that you can load your module to test it, you're going to keep running into issues like what you stated above that result from two drivers managing the same device. So, a second keyboard is one possibility, or a second computer so you can remotely log in in order to test...
    – twalberg
    Sep 14, 2012 at 20:32
  • @twalberg Thank you very much! I will google to find something more about how to disable the original one. Sep 14, 2012 at 20:38

1 Answer 1

1

The driver should not and could not un-register interrupt handler that is not registered by itself. For preventing the original driver handle the keyboard interrupt, you can do ether:

1) return IRQ_HANDLED in your interrupt handler: This value indicates the interrupted is well handled and the linux kernel's interrupt processing mechanism would stop calling next interrupt handler. Or

2) clear input buffer in hardware, you can reference the original keyboard driver's code to know the status register and input buffer used during a keyboard hit event.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.