diff --git a/Cargo.lock b/Cargo.lock index d971355db9..a35f309c3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1013,8 +1013,10 @@ version = "0.1.0" dependencies = [ "bitflags 1.3.2", "libredox 0.1.3", + "log", "orbclient", "redox-daemon", + "redox-log", "redox_syscall 0.5.3", ] diff --git a/ps2d/Cargo.toml b/ps2d/Cargo.toml index fb629ea72a..013830a661 100644 --- a/ps2d/Cargo.toml +++ b/ps2d/Cargo.toml @@ -5,7 +5,9 @@ edition = "2018" [dependencies] bitflags = "1" +log = "0.4" orbclient = "0.3.27" redox_syscall = "0.5" redox-daemon = "0.1" +redox-log = "0.1.1" libredox = "0.1.3" diff --git a/ps2d/src/controller.rs b/ps2d/src/controller.rs index 1b0ae6fecb..ecb9036794 100644 --- a/ps2d/src/controller.rs +++ b/ps2d/src/controller.rs @@ -1,3 +1,4 @@ +use log::{debug, error, info, trace}; use syscall::io::{Io, Pio, ReadOnly, WriteOnly}; use std::fmt; @@ -39,7 +40,7 @@ bitflags! { bitflags! { pub struct ConfigFlags: u8 { - const FIRST_INTERRUPT = 1; + const FIRST_INTERRUPT = 1 << 0; const SECOND_INTERRUPT = 1 << 1; const POST_PASSED = 1 << 2; // 1 << 3 should be zero @@ -153,7 +154,8 @@ impl Ps2 { let mut timeout = 100; while timeout > 0 { if self.status().contains(StatusFlags::OUTPUT_FULL) { - eprintln!("ps2d: flush {}: {:X}", message, self.data.read()); + let val = self.data.read(); + trace!("ps2d: flush {}: {:X}", message, val); } unsafe { pause(); } timeout -= 1; @@ -178,7 +180,7 @@ impl Ps2 { } fn retry Result>(&mut self, name: fmt::Arguments, retries: usize, f: F) -> Result { - eprintln!("ps2d: {}", name); + trace!("ps2d: {}", name); let mut res = Err(Error::NoMoreTries); for retry in 0..retries { res = f(self); @@ -187,7 +189,7 @@ impl Ps2 { return Ok(ok); }, Err(ref err) => { - eprintln!("ps2d: {}: retry {}/{}: {:?}", name, retry + 1, retries, err); + info!("ps2d: {}: retry {}/{}: {:?}", name, retry + 1, retries, err); } } } @@ -311,10 +313,10 @@ impl Ps2 { if b == 0xFA { b = self.read().unwrap_or(0); if b != 0xAA { - eprintln!("ps2d: keyboard failed self test: {:02X}", b); + error!("ps2d: keyboard failed self test: {:02X}", b); } } else { - eprintln!("ps2d: keyboard failed to reset: {:02X}", b); + error!("ps2d: keyboard failed to reset: {:02X}", b); } // Clear remaining data @@ -330,7 +332,7 @@ impl Ps2 { // Set defaults and disable scanning let b = x.keyboard_command(KeyboardCommand::SetDefaultsDisable)?; if b != 0xFA { - eprintln!("ps2d: keyboard failed to set defaults: {:02X}", b); + error!("ps2d: keyboard failed to set defaults: {:02X}", b); return Err(Error::CommandRetry); } @@ -346,7 +348,7 @@ impl Ps2 { let scancode_set = 2; b = self.keyboard_command_data(KeyboardCommandData::ScancodeSet, scancode_set)?; if b != 0xFA { - eprintln!("ps2d: keyboard failed to set scancode set {}: {:02X}", scancode_set, b); + error!("ps2d: keyboard failed to set scancode set {}: {:02X}", scancode_set, b); } // Clear remaining data @@ -379,17 +381,17 @@ impl Ps2 { if b == 0xFA { b = x.read()?; if b != 0xAA { - eprintln!("ps2d: mouse failed self test 1: {:02X}", b); + error!("ps2d: mouse failed self test 1: {:02X}", b); return Err(Error::CommandRetry); } b = x.read()?; if b != 0x00 { - eprintln!("ps2d: mouse failed self test 2: {:02X}", b); + error!("ps2d: mouse failed self test 2: {:02X}", b); return Err(Error::CommandRetry); } } else { - eprintln!("ps2d: mouse failed to reset: {:02X}", b); + error!("ps2d: mouse failed to reset: {:02X}", b); return Err(Error::CommandRetry); } @@ -404,7 +406,7 @@ impl Ps2 { // Set defaults b = self.mouse_command(MouseCommand::SetDefaults)?; if b != 0xFA { - eprintln!("ps2d: mouse failed to set defaults: {:02X}", b); + error!("ps2d: mouse failed to set defaults: {:02X}", b); } // Clear remaining data @@ -417,7 +419,7 @@ impl Ps2 { if self.mouse_command_data(MouseCommandData::SetSampleRate, 200)? != 0xFA || self.mouse_command_data(MouseCommandData::SetSampleRate, 100)? != 0xFA || self.mouse_command_data(MouseCommandData::SetSampleRate, 80)? != 0xFA { - eprintln!("ps2d: mouse failed to enable extra packet"); + error!("ps2d: mouse failed to enable extra packet"); } // Clear remaining data @@ -428,7 +430,7 @@ impl Ps2 { let mouse_extra = if b == 0xFA { self.read()? == 3 } else { - eprintln!("ps2d: mouse failed to get device id: {:02X}", b); + error!("ps2d: mouse failed to get device id: {:02X}", b); false }; @@ -440,7 +442,7 @@ impl Ps2 { let resolution = 3; b = self.mouse_command_data(MouseCommandData::SetResolution, resolution)?; if b != 0xFA { - eprintln!("ps2d: mouse failed to set resolution to {}: {:02X}", resolution, b); + error!("ps2d: mouse failed to set resolution to {}: {:02X}", resolution, b); } // Clear remaining data @@ -451,7 +453,7 @@ impl Ps2 { // Set scaling to 1:1 b = self.mouse_command(MouseCommand::SetScaling1To1)?; if b != 0xFA { - eprintln!("ps2d: mouse failed to set scaling: {:02X}", b); + error!("ps2d: mouse failed to set scaling: {:02X}", b); } // Clear remaining data @@ -463,7 +465,7 @@ impl Ps2 { let sample_rate = 200; b = self.mouse_command_data(MouseCommandData::SetSampleRate, sample_rate)?; if b != 0xFA { - eprintln!("ps2d: mouse failed to set sample rate to {}: {:02X}", sample_rate, b); + error!("ps2d: mouse failed to set sample rate to {}: {:02X}", sample_rate, b); } // Clear remaining data @@ -473,13 +475,13 @@ impl Ps2 { { b = self.mouse_command(MouseCommand::StatusRequest)?; if b != 0xFA { - eprintln!("ps2d: mouse failed to request status: {:02X}", b); + error!("ps2d: mouse failed to request status: {:02X}", b); } else { let a = self.read()?; let b = self.read()?; let c = self.read()?; - eprintln!("ps2d: mouse status {:#x} resolution {:#x} sample rate {:#x}", a, b, c); + debug!("ps2d: mouse status {:#x} resolution {:#x} sample rate {:#x}", a, b, c); } } @@ -507,7 +509,7 @@ impl Ps2 { config = ConfigFlags::POST_PASSED | ConfigFlags::FIRST_DISABLED | ConfigFlags::SECOND_DISABLED; - eprintln!("ps2d: config set {:?}", config); + trace!("ps2d: config set {:?}", config); self.set_config(config)?; // Clear remaining data @@ -530,7 +532,7 @@ impl Ps2 { let (mouse_found, mouse_extra) = match self.init_mouse() { Ok(ok) => (true, ok), Err(err) => { - eprintln!("ps2d: failed to initialize mouse: {:?}", err); + error!("ps2d: failed to initialize mouse: {:?}", err); (false, false) } }; @@ -563,7 +565,7 @@ impl Ps2 { config.insert(ConfigFlags::SECOND_DISABLED); config.remove(ConfigFlags::SECOND_INTERRUPT); } - eprintln!("ps2d: config set {:?}", config); + trace!("ps2d: config set {:?}", config); self.set_config(config)?; } diff --git a/ps2d/src/main.rs b/ps2d/src/main.rs index 64910d95ef..15f3537903 100644 --- a/ps2d/src/main.rs +++ b/ps2d/src/main.rs @@ -6,11 +6,13 @@ extern crate orbclient; extern crate syscall; use std::{env, process}; -use std::fs::{File, OpenOptions}; +use std::fs::OpenOptions; use std::io::{Read, Write}; use std::os::unix::fs::OpenOptionsExt; use std::os::unix::io::AsRawFd; +use log::info; +use redox_log::{OutputBuilder, RedoxLogger}; use syscall::call::iopl; use crate::state::Ps2d; @@ -20,7 +22,54 @@ mod keymap; mod state; mod vm; + +fn setup_logging() -> Option<&'static RedoxLogger> { + let mut logger = RedoxLogger::new() + .with_output( + OutputBuilder::stderr() + .with_filter(log::LevelFilter::Info) // limit global output to important info + .with_ansi_escape_codes() + .flush_on_newline(true) + .build() + ); + + #[cfg(target_os = "redox")] + match OutputBuilder::in_redox_logging_scheme("misc", "ps2", "ps2.log") { + Ok(b) => logger = logger.with_output( + // TODO: Add a configuration file for this + b.with_filter(log::LevelFilter::Info) + .flush_on_newline(true) + .build() + ), + Err(error) => eprintln!("ps2d: failed to create ps2.log: {}", error), + } + + #[cfg(target_os = "redox")] + match OutputBuilder::in_redox_logging_scheme("misc", "ps2", "ps2.ansi.log") { + Ok(b) => logger = logger.with_output( + b.with_filter(log::LevelFilter::Info) + .with_ansi_escape_codes() + .flush_on_newline(true) + .build() + ), + Err(error) => eprintln!("ps2d: failed to create ps2.ansi.log: {}", error), + } + + match logger.enable() { + Ok(logger_ref) => { + eprintln!("ps2d: enabled logger"); + Some(logger_ref) + } + Err(error) => { + eprintln!("ps2d: failed to set default logger: {}", error); + None + } + } +} + fn daemon(daemon: redox_daemon::Daemon) -> ! { + setup_logging(); + unsafe { iopl(3).expect("ps2d: failed to get I/O permission"); } @@ -41,7 +90,7 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { None => (keymap::us::get_char, "us"), }; - eprintln!("ps2d: using keymap '{}'", keymap_name); + info!("ps2d: using keymap '{}'", keymap_name); let input = OpenOptions::new() .write(true) diff --git a/ps2d/src/state.rs b/ps2d/src/state.rs index 381b642d3f..22db4e26e0 100644 --- a/ps2d/src/state.rs +++ b/ps2d/src/state.rs @@ -3,6 +3,7 @@ use std::io::Write; use std::os::unix::io::AsRawFd; use std::str; +use log::{error, warn}; use orbclient::{KeyEvent, MouseEvent, MouseRelativeEvent, ButtonEvent, ScrollEvent}; use crate::controller::Ps2; @@ -115,7 +116,7 @@ impl char> Ps2d { /* 0x80 to 0xFF used for press/release detection */ _ => { if pressed { - println!("ps2d: unknown extended scancode {:02X}", ps2_scancode); + warn!("ps2d: unknown extended scancode {:02X}", ps2_scancode); } 0 } @@ -213,7 +214,7 @@ impl char> Ps2d { /* 0x80 to 0xFF used for press/release detection */ _ => { if pressed { - println!("ps2d: unknown scancode {:02X}", ps2_scancode); + warn!("ps2d: unknown scancode {:02X}", ps2_scancode); } 0 } @@ -245,7 +246,7 @@ impl char> Ps2d { } if queue_length % 4 != 0 { - eprintln!("ps2d: queue length not a multiple of 4: {}", queue_length); + error!("ps2d: queue length not a multiple of 4: {}", queue_length); break; } @@ -297,7 +298,7 @@ impl char> Ps2d { let flags = MousePacketFlags::from_bits_truncate(self.packets[0]); if ! flags.contains(MousePacketFlags::ALWAYS_ON) { - eprintln!("ps2d: mouse misalign {:X}", self.packets[0]); + error!("ps2d: mouse misalign {:X}", self.packets[0]); self.packets = [0; 4]; self.packet_i = 0; @@ -350,7 +351,7 @@ impl char> Ps2d { }.to_event()).expect("ps2d: failed to write button event"); } } else { - eprintln!("ps2d: overflow {:X} {:X} {:X} {:X}", self.packets[0], self.packets[1], self.packets[2], self.packets[3]); + warn!("ps2d: overflow {:X} {:X} {:X} {:X}", self.packets[0], self.packets[1], self.packets[2], self.packets[3]); } self.packets = [0; 4]; diff --git a/ps2d/src/vm.rs b/ps2d/src/vm.rs index 3885c83ff0..8832dc0ac9 100644 --- a/ps2d/src/vm.rs +++ b/ps2d/src/vm.rs @@ -6,6 +6,8 @@ use core::arch::asm; +use log::{error, info, trace}; + const MAGIC: u32 = 0x564D5868; const PORT: u16 = 0x5658; @@ -58,38 +60,40 @@ pub unsafe fn cmd(cmd: u32, arg: u32) -> (u32, u32, u32, u32) { ); (a, b, c, d) - } pub fn enable(relative: bool) -> bool { - eprintln!("ps2d: Enable vmmouse"); + trace!("ps2d: Enable vmmouse"); unsafe { let (eax, ebx, _, _) = cmd(GETVERSION, 0); if ebx != MAGIC || eax == 0xFFFFFFFF { - eprintln!("ps2d: No vmmouse support"); + info!("ps2d: No vmmouse support"); return false; } let _ = cmd(ABSPOINTER_COMMAND, CMD_ENABLE); let (status, _, _, _) = cmd(ABSPOINTER_STATUS, 0); - if (status & 0x0000ffff) == 0 { - eprintln!("ps2d: No vmmouse"); - return false; - } + if (status & 0x0000ffff) == 0 { + info!("ps2d: No vmmouse"); + return false; + } let (version, _, _, _) = cmd(ABSPOINTER_DATA, 1); if version != VERSION { - eprintln!("ps2d: Invalid vmmouse version: {} instead of {}", version, VERSION); + error!( + "ps2d: Invalid vmmouse version: {} instead of {}", + version, VERSION + ); let _ = cmd(ABSPOINTER_COMMAND, CMD_DISABLE); return false; } if relative { - cmd(ABSPOINTER_COMMAND, CMD_REQUEST_RELATIVE); + cmd(ABSPOINTER_COMMAND, CMD_REQUEST_RELATIVE); } else { - cmd(ABSPOINTER_COMMAND, CMD_REQUEST_ABSOLUTE); + cmd(ABSPOINTER_COMMAND, CMD_REQUEST_ABSOLUTE); } }