diff --git a/kernel/device/ps2/ps2_kb.c b/kernel/device/ps2/ps2_kb.c index b47c51c..3b83b75 100644 --- a/kernel/device/ps2/ps2_kb.c +++ b/kernel/device/ps2/ps2_kb.c @@ -15,10 +15,13 @@ #include #include #include +#include #define KB_CTL_STATUS 0x64 #define KB_DATA_IN_BUF 0x01 #define KB_DATA 0x60 +#define KB_IDENTIFY 0xF2 +#define KB_ACK 0xFA #define PS2KB_RINGBUFFER_MAX 2048 @@ -225,7 +228,34 @@ static void ps2kb_set_typematic(uint8_t delay, uint8_t rate) { outb(KB_DATA, (delay << 5) | (rate & 0x1F)); } +static bool ps2kb_check(void) { + while (inb(KB_CTL_STATUS) & KB_DATA_IN_BUF) + inb(KB_DATA); + + while (inb(KB_CTL_STATUS) & 0x02) + ; + + outb(KB_DATA, KB_IDENTIFY); + + for (int i = 0; i < 1000; i++) { + if (inb(KB_CTL_STATUS) & KB_DATA_IN_BUF) { + uint8_t response = inb(KB_DATA); + if (response == KB_ACK) + return true; + } + + stall_ms(2); + } + + return false; +} + DEFINE_DEVICE_INIT(ps2kb_init) { + if (!ps2kb_check()) { + DEBUG("PS/2 keyboard not detected!\n"); + return false; + } + ioapic_route_irq(INTR_PS2KB, 1, 0, thiscpu->lapic_id); irq_attach(&ps2kb_irq, NULL, INTR_PS2KB);