From cb9e7e764ed6b14d0b90c27a276193a2011bf399 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Duje=20Mihanovi=C4=87?= <duje.mihanovic@skole.hr>
Date: Fri, 13 May 2022 16:30:18 +0200
Subject: [PATCH] Fix device initialization

Before doing a ps2_input_wait(), the code now first reads from the data
port once (for port 1 device) or twice (for port 2 device). This makes
the PS/2 initialization work.
---
 kernel/drivers/input/ps2.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/kernel/drivers/input/ps2.c b/kernel/drivers/input/ps2.c
index 0507024..e765b24 100644
--- a/kernel/drivers/input/ps2.c
+++ b/kernel/drivers/input/ps2.c
@@ -85,7 +85,6 @@ int ps2_initialize()
 
 	int dev_1_test, dev_2_test;
 
-/*
 	if (port_1_test) {
 		uint8_t resp;
 		
@@ -106,17 +105,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 +127,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();
-- 
2.39.5