aboutsummaryrefslogtreecommitdiff
path: root/devices
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-10-31 15:39:51 +0200
committeraqua <aqua@iserlohn-fortress.net>2022-10-31 21:59:49 +0200
commitcbd3cdf7cb34529e269bb27c654765a0c9c21799 (patch)
tree4ee555d5cb575af003e10e597666b4a7b17b17a7 /devices
parentmakefile: auto-generate OBJS from SRCS (diff)
downloadkernel-cbd3cdf7cb34529e269bb27c654765a0c9c21799.tar.xz
add ps2_keyboard driver
Diffstat (limited to 'devices')
-rw-r--r--devices/Makefile4
-rw-r--r--devices/pic_8259.c2
-rw-r--r--devices/ps2_keyboard.c40
-rw-r--r--devices/ps2_keyboard.h4
-rw-r--r--devices/uart_16550.h1
5 files changed, 48 insertions, 3 deletions
diff --git a/devices/Makefile b/devices/Makefile
index 41edaaa..0e07a83 100644
--- a/devices/Makefile
+++ b/devices/Makefile
@@ -1,10 +1,10 @@
include ../${ARCH}/toolchain.mk
-CCFLAGS += -I. -I../${ARCH}
+CCFLAGS += -I. -I../${ARCH} -I../lib
all: devs.a
-devs,SRCS = pic_8259.c uart_16550.c vga.c
+devs,SRCS = pic_8259.c uart_16550.c vga.c ps2_keyboard.c
include ../rules.mk
diff --git a/devices/pic_8259.c b/devices/pic_8259.c
index 43b090f..74c576a 100644
--- a/devices/pic_8259.c
+++ b/devices/pic_8259.c
@@ -28,7 +28,7 @@ pic_init()
outb(ICW4_8086, PIC2 + DATA);
// PIC masks
- outb(0xff, PIC1 + DATA);
+ outb(0xfc, PIC1 + DATA);
outb(0xff, PIC2 + DATA);
}
diff --git a/devices/ps2_keyboard.c b/devices/ps2_keyboard.c
new file mode 100644
index 0000000..6171d18
--- /dev/null
+++ b/devices/ps2_keyboard.c
@@ -0,0 +1,40 @@
+#include "ps2_keyboard.h"
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/io.h>
+
+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;
+
+const char scancodes[] = {'E', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', 'T', 'q', 'w', 'e',
+ 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 'C', 'a', 's', 'd', 'f', 'g', 'h', 'j',
+ 'k', 'l', ';', '\'', '`', 'L', '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 'R'};
+
+void
+ps2_keyboard_init()
+{
+ // eat all previous keystrikes
+ while (inb(comm_port) & 0x1) inb(data_port);
+ 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);
+}
+
+void
+ps2_keyboard_irq_handler()
+{
+ const uint8_t key = inb(data_port);
+ if (key >= 0x80) return;
+
+ if (key < 0x37) printf("%c", scancodes[key - 1]);
+ else
+ printf("key pressed: %x\n", key);
+}
diff --git a/devices/ps2_keyboard.h b/devices/ps2_keyboard.h
new file mode 100644
index 0000000..5f4fcc2
--- /dev/null
+++ b/devices/ps2_keyboard.h
@@ -0,0 +1,4 @@
+#pragma once
+
+void ps2_keyboard_init();
+void ps2_keyboard_irq_handler();
diff --git a/devices/uart_16550.h b/devices/uart_16550.h
index d053985..0e181b2 100644
--- a/devices/uart_16550.h
+++ b/devices/uart_16550.h
@@ -53,4 +53,5 @@ enum UART {
};
int uart_init(enum UART port);
+void uart_write(enum UART port, char a);
int uart_puts(enum UART port, const char *string, int length);