From 89345100c95ad9573551edc21612e6f0b2ec5674 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 11 Sep 2024 10:16:50 -0500 Subject: [PATCH] Enable interrupts --- Cargo.lock | 1 + Cargo.toml | 1 + src/controller.rs | 30 +++++++++++++++++++++++++++++- src/main.rs | 23 ++++++++++++++++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 002368c..8245173 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,7 @@ name = "ps2" version = "0.1.0" dependencies = [ "bitflags", + "parking_lot", "syslog_rpc", "x86_64", ] diff --git a/Cargo.toml b/Cargo.toml index 45a98b3..df25e6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,6 @@ edition = "2021" [dependencies] bitflags = "2.6.0" +parking_lot = "0.12.3" syslog_rpc = { version = "0.1.0", path = "../syslog/syslog_rpc" } x86_64 = "0.15.1" diff --git a/src/controller.rs b/src/controller.rs index 7930549..d2cdc93 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -187,7 +187,12 @@ impl Ps2Controller { config_byte.set(Ps2ConfigByte::Port1Translation, false); self.set_config_byte(config_byte); - let self_test_result = self.send_command(Ps2ControllerCommand::TestController).unwrap(); + // Flush the output buffer + unsafe { self.data_port.read() }; + + let self_test_result = self + .send_command(Ps2ControllerCommand::TestController) + .unwrap(); if self_test_result != 0x55 { return Err(Ps2InitFailure::SelfTestFailed); } @@ -218,6 +223,9 @@ impl Ps2Controller { if port1_test_result == 0x0 { self.port1_ok = true; self.send_command(Ps2ControllerCommand::EnablePort1); + let mut config_byte = self.get_config_byte(); + config_byte.set(Ps2ConfigByte::Port1Interrupt, true); + self.set_config_byte(config_byte); } if self.two_ports { @@ -225,6 +233,9 @@ impl Ps2Controller { if port2_test_result == 0x0 { self.port2_ok = true; self.send_command(Ps2ControllerCommand::EnablePort2); + let mut config_byte = self.get_config_byte(); + config_byte.set(Ps2ConfigByte::Port2Interrupt, true); + self.set_config_byte(config_byte); } } @@ -242,4 +253,21 @@ impl Ps2Controller { pub fn port2_ok(&self) -> bool { self.port2_ok } + + pub fn write_port1(&mut self, data: u8) { + self.send_command(Ps2ControllerCommand::WritePort1Input(data)); + } + + + pub fn write_port2(&mut self, data: u8) { + self.send_command(Ps2ControllerCommand::WritePort2Input(data)); + } + + pub fn handle_interrupt(&mut self) { + if self.get_status().contains(Ps2Status::OutputBufFull) { + println!("Data: {:#x}", unsafe {self.data_port.read()}); + } else { + println!("Spurious interrupt"); + } + } } diff --git a/src/main.rs b/src/main.rs index bf32ac7..bdbfc3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,14 @@ mod controller; -use std::os::mikros::syscalls; +use std::os::mikros::{ipc, syscalls}; use controller::Ps2Controller; +use parking_lot::Mutex; + +static CONTROLLER: Mutex> = Mutex::new(None); fn main() { + dbg!(syscalls::get_pid()); let syslog_pid = loop { if let Some(pid) = syscalls::try_get_registered(2) { break pid; @@ -48,4 +52,21 @@ fn main() { .unwrap(); } } + + if controller.port1_ok() { + controller.write_port1(0xFF); + } + + *CONTROLLER.lock() = Some(controller); + + ipc::register_callback(2, |_msg| { + println!("Keyboard interrupt"); + if let Some(controller) = CONTROLLER.lock().as_mut() { + controller.handle_interrupt(); + } + }); + + loop { + ipc::process_messages() + } }