]> git.dujemihanovic.xyz Git - nameless-os.git/blobdiff - kernel/drivers/input/ps2.c
Rework interrupt handling
[nameless-os.git] / kernel / drivers / input / ps2.c
index 050702426f39e9114ad5468e7754ba1177e0c46e..ab4be5c5bbcbb9338ed7a545f59dd9517471605f 100644 (file)
@@ -2,11 +2,43 @@
 #include <io.h>
 #include <input/ps2.h>
 #include <bitflags.h>
+#include <irq/interrupt.h>
+
+static int was_released = 0, is_caps = 0;
+
+int ps2_keyb_handler()
+{
+       uint8_t scancode = inb(PS2_DATA_PORT);
+       if (was_released) {
+               was_released = 0;
+               return 0;
+       }
+
+       if (scancode == 0xf0) {
+               was_released = 1;
+               uint8_t scancode = inb(PS2_DATA_PORT);
+               if (scancode == 0x12) {
+                       is_caps = 0;
+               }
+               return 0;
+       } else {
+               if (scancode == 0x12) {
+                       is_caps = 1;
+                       return 0;
+               }
+               if (!is_caps) {
+                       kprintc(scancodes[scancode], 0);
+               } else {
+                       kprintc(scancodes[scancode] - ('a'-'A'), 0);
+               }
+       }
+       return 0;
+}
 
 int ps2_initialize()
 {
        uint8_t ccb, is_2channel, port_1_test, port_2_test;
-       
+
        kprint("ps2: Begin initializing PS/2 controller\n", 0);
        outb(PS2_CMD_STS_PORT, PS2_CMD_PORT_2_DISABLE);
        ps2_input_wait();
@@ -21,7 +53,7 @@ int ps2_initialize()
        CLEAR(ccb, 0);
        CLEAR(ccb, 1);
        CLEAR(ccb, 6);
-       
+
        ps2_input_wait();
        outb(PS2_CMD_STS_PORT, PS2_CMD_WRITE_CCB);
        ps2_input_wait();
@@ -34,7 +66,7 @@ int ps2_initialize()
                kprint("ps2: Controller self test failed, exiting!\n", 0);
                return -1;
        };
-       
+
        ps2_input_wait();
        outb(PS2_CMD_STS_PORT, PS2_CMD_PORT_2_ENABLE);
        ps2_input_wait();
@@ -55,7 +87,7 @@ int ps2_initialize()
        ps2_input_wait();
        outb(PS2_CMD_STS_PORT, PS2_CMD_PORT_1_TEST);
        ps2_output_wait();
-       
+
        if (inb(PS2_DATA_PORT) != 0) {
                port_1_test = 0;
                kprint("ps2: Port 1 test failed!\n", 0);
@@ -85,10 +117,9 @@ int ps2_initialize()
 
        int dev_1_test, dev_2_test;
 
-/*
        if (port_1_test) {
                uint8_t resp;
-               
+
                do {
                        ps2_input_wait();
                        outb(PS2_DATA_PORT, PS2_DEV_RESET);
@@ -106,17 +137,20 @@ int ps2_initialize()
                } else { dev_1_test = 1; kprint("ps2: Port 1 device test successful\n", 0); }
        }
 
+       inb(PS2_DATA_PORT);
+       ps2_input_wait();
+
        if (port_2_test) {
-               int resp = RESEND;
+               uint8_t resp;
 
-               while (resp == RESEND) {
+               do {
                        ps2_input_wait();
                        outb(PS2_CMD_STS_PORT, PS2_CMD_PORT_2_WRITE);
                        ps2_input_wait();
                        outb(PS2_DATA_PORT, PS2_DEV_RESET);
                        ps2_output_wait();
                        resp = inb(PS2_DATA_PORT);
-               }
+               } while (resp == RESEND);
 
                if (resp == SELF_TEST_BAD || resp == SELF_TEST_BAD_2) {
                        dev_2_test = 0;
@@ -125,10 +159,11 @@ int ps2_initialize()
                                kprint("ps2: No functioning devices, exiting!\n", 0);
                                return -1;
                        }
-               } else { dev_2_test = 1; kprint("ps2: Port 2 test successful\n", 0); }
+               } else { dev_2_test = 1; kprint("ps2: Port 2 device test successful\n", 0); }
        }
-*/
 
+       inb(PS2_DATA_PORT);
+       inb(PS2_DATA_PORT);
        ps2_input_wait();
        outb(PS2_CMD_STS_PORT, PS2_CMD_READ_CCB);
        ps2_output_wait();
@@ -141,6 +176,8 @@ int ps2_initialize()
        ps2_input_wait();
        outb(PS2_DATA_PORT, ccb);
 
+       if (dev_1_test) register_interrupt(33, &ps2_keyb_handler);
+
        if (dev_2_test && dev_1_test) return 1;
        else return 0;
 }