Linux Device Driver to disable/enable keyboard
Theory:
As we have seen in our earlier posts, that there are three registers for keyboard controller.
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
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
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
Post a Comment