From fd4f56e6776873af2693d4ddf1c672c36bd97428 Mon Sep 17 00:00:00 2001 From: aqua Date: Thu, 3 Nov 2022 17:45:28 +0200 Subject: i8042: fix broken self-test --- devices/Makefile | 2 +- devices/i8042.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ devices/keyboard.h | 2 ++ devices/pckbd.c | 3 +-- devices/ps2_controller.h | 4 ++++ devices/ps2_ctrl.h | 4 ---- devices/ps2_ctrl_8042.c | 43 --------------------------------------- 7 files changed, 61 insertions(+), 50 deletions(-) create mode 100644 devices/i8042.c create mode 100644 devices/ps2_controller.h delete mode 100644 devices/ps2_ctrl.h delete mode 100644 devices/ps2_ctrl_8042.c (limited to 'devices') diff --git a/devices/Makefile b/devices/Makefile index 4ee04ca..62a0b88 100644 --- a/devices/Makefile +++ b/devices/Makefile @@ -2,7 +2,7 @@ include ../Makefile.config CCFLAGS += -I. -I../${ARCH} -I../lib -devs,SRCS = pic_8259.c uart_16550.c vga.c ps2_ctrl_8042.c pckbd.c mouse.c +devs,SRCS = pic_8259.c uart_16550.c vga.c i8042.c pckbd.c mouse.c include ../rules.mk diff --git a/devices/i8042.c b/devices/i8042.c new file mode 100644 index 0000000..cf8c6ec --- /dev/null +++ b/devices/i8042.c @@ -0,0 +1,53 @@ +/* + * https://wiki.osdev.org/%228042%22_PS/2_Controller + */ + +#include "keyboard.h" +#include "ps2_controller.h" +#include +#include +#include + +// r status register +// w command register +#define comm_port 0x64 +#define comm_enable_first_ps2 0xae +#define comm_read_ctrl_config 0x20 +#define comm_write_ctrl_config 0x60 + +#define data_port 0x60 +#define data_enable_scanning 0xf4 + +void +ps2_ctrl_init() +{ + // eat all previous keystrikes + while (inb(comm_port) & 0x1) inb(data_port); + + uint8_t test; + outb(0xaa, comm_port); + test = inb(data_port); + printf("i8042: self test 0xaa:%x %s\n", test, test == 0x55 ? "ok" : "failed"); + outb(0xab, comm_port); + test = inb(data_port); + printf("i8042: port1 test 0xab:%x %s\n", test, test == 0x00 ? "ok" : "failed"); + outb(0xa9, comm_port); + test = inb(data_port); + printf("i8042: port2 test 0xa9:%x %s\n", test, test == 0x00 ? "ok" : "failed"); + + // printf("8042: init keyboard\n"); + + outb(comm_enable_first_ps2, comm_port); + outb(comm_read_ctrl_config, comm_port); + const uint8_t conf = (inb(data_port) | 1) & ~0x10; + outb(comm_write_ctrl_config, comm_port); + + outb(conf, data_port); + outb(data_enable_scanning, data_port); +} + +unsigned char +kbd_read_next() +{ + return inb(data_port); +} diff --git a/devices/keyboard.h b/devices/keyboard.h index 5f4fcc2..2375552 100644 --- a/devices/keyboard.h +++ b/devices/keyboard.h @@ -2,3 +2,5 @@ void ps2_keyboard_init(); void ps2_keyboard_irq_handler(); + +unsigned char kbd_read_next(); diff --git a/devices/pckbd.c b/devices/pckbd.c index f3dcb6c..916e8e6 100644 --- a/devices/pckbd.c +++ b/devices/pckbd.c @@ -1,5 +1,4 @@ #include "keyboard.h" -#include "ps2_ctrl.h" #include "vga.h" #include #include @@ -24,7 +23,7 @@ ps2_keyboard_init() void ps2_keyboard_irq_handler() { - const uint8_t key = ps2_ctrl_read(); + const uint8_t key = kbd_read_next(); switch (key) { case 0x2a: // left shift down diff --git a/devices/ps2_controller.h b/devices/ps2_controller.h new file mode 100644 index 0000000..9bed4f2 --- /dev/null +++ b/devices/ps2_controller.h @@ -0,0 +1,4 @@ +#pragma once + +void ps2_ctrl_init(); +unsigned char ps2_ctrl_read(); diff --git a/devices/ps2_ctrl.h b/devices/ps2_ctrl.h deleted file mode 100644 index 9bed4f2..0000000 --- a/devices/ps2_ctrl.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -void ps2_ctrl_init(); -unsigned char ps2_ctrl_read(); diff --git a/devices/ps2_ctrl_8042.c b/devices/ps2_ctrl_8042.c deleted file mode 100644 index 4157bd8..0000000 --- a/devices/ps2_ctrl_8042.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "ps2_ctrl.h" -#include -#include -#include - -const uint8_t comm_port = 0x64; // r status register - // w command register -const uint8_t comm_enable_first_ps2 = 0xae; -const uint8_t comm_read_ctrl_config = 0x20; -const uint8_t comm_write_ctrl_config = 0x60; - -const uint8_t data_port = 0x60; // rw -const uint8_t data_enable_scanning = 0xf4; - -void -ps2_ctrl_init() -{ - // eat all previous keystrikes - while (inb(comm_port) & 0x1) inb(data_port); - - outb(0x64, 0xaa); - printf("8042: self test 0xaa: %x\n", inb(0x60)); - outb(0x64, 0xab); - printf("8042: port1 test 0xab: %x\n", inb(0x60)); - outb(0x64, 0xa9); - printf("8042: port2 test 0xa9: %x\n", inb(0x60)); - - // printf("8042: init keyboard\n"); - - outb(comm_port, comm_enable_first_ps2); - outb(comm_port, comm_read_ctrl_config); - const uint8_t conf = (inb(data_port) | 1) & ~0x10; - outb(comm_port, comm_write_ctrl_config); - outb(data_port, conf); - outb(data_port, data_enable_scanning); - -} - -unsigned char -ps2_ctrl_read() -{ - return inb(0x60); -} -- cgit v1.2.1