Linux Device Driver to disable/enable keyboard

Theory:

As we have seen in our earlier posts, that there are three registers for keyboard controller.

  • Control Register (0x64) -- Write Operation
  • Data Register (0x60) -- Write/Read Operation
  • Status Register (0x60) -- Read Operation
Process to disable Keyboard:
  • Wait until the keyboard controller is not busy by reading the status register
  • Send the command 0xAD to Control Register
Process to Enable Keyboard:
  • Wait until the keyboard controller is not busy by reading the status register
  • Send the command 0xAE to Control Register.
Code:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
int sleep_duration = 10000;
module_param(sleep_duration, int, S_IRUGO);
#define KBD_DATA_REG 0x60 /* I/O port for keyboard data */
#define KBD_CONTROL_REG 0x64
#define DELAY do { mdelay(1); if (++delay > 10) break; } while(0)
static void disable_keyboard(void)
{
long delay = 0;
//wait till the input buffer is empty
while (inb(KBD_CONTROL_REG) & 0x02)
DELAY;
outb(0xAD, KBD_CONTROL_REG);
}
static void enable_keyboard(void)
{
long delay = 0;
//wait till the input buffer is empty
while (inb(KBD_CONTROL_REG) & 0x02)
DELAY;
outb(0xAE, KBD_CONTROL_REG);
}
static int test_kbd_disable_init(void)
{
pr_info("%s: In init\n", __func__);
disable_keyboard();
msleep(sleep_duration);
enable_keyboard();
return 0;
}
static void test_kbd_disable_exit(void)
{
pr_info("%s: In exit\n", __func__);
}
module_init(test_kbd_disable_init);
module_exit(test_kbd_disable_exit);
view raw kbd_disable.c hosted with ❤ by GitHub



Output:




Notes:

We have loaded the module using the command "sleep 1 && insmod ./kbd_disable.ko". If you try only "insmod ./kbd_disable.ko", the keyboard will disable at "Enter Press", so the console will be continuously receiving Enter Press Keys, until keyboard gets enabled back

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto