From ad877e5ca439b5ce9f54c1673b2bcc9607db828f Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 6 Sep 2026 19:14:52 +0800 Subject: refactor: earlyprintk and uart_16550 --- kernel/src/io/output/serial/uart_16550.rs | 55 +++++++------------------------ 1 file changed, 12 insertions(+), 43 deletions(-) (limited to 'kernel/src/io') diff --git a/kernel/src/io/output/serial/uart_16550.rs b/kernel/src/io/output/serial/uart_16550.rs index 65ba4e9..3ecce1b 100644 --- a/kernel/src/io/output/serial/uart_16550.rs +++ b/kernel/src/io/output/serial/uart_16550.rs @@ -1,4 +1,3 @@ -use bitflags::bitflags; use uart_16550::{ Config, Uart16550Tty, Uart16550TtyError, backend::{PioBackend, PortIoAddress}, @@ -13,15 +12,6 @@ pub enum SerialPortAddress { Com4 = 0x2E8, } -bitflags! { - pub struct SerialPortBitMask: u8 { - const COM1 = 1 << 0; - const COM2 = 1 << 1; - const COM3 = 1 << 2; - const COM4 = 1 << 3; - } -} - impl From for u16 { fn from(value: SerialPortAddress) -> Self { value as u16 @@ -32,6 +22,14 @@ pub struct Uart16550 { pub coms: [Option>; 4], } +#[inline(always)] +fn init_port( + cfg: &mut Option, + addr: SerialPortAddress, +) -> Result>, Uart16550TtyError> { + cfg.take().map(|c| unsafe { Uart16550Tty::new_port(addr.into(), c) }).transpose() +} + impl Uart16550 { /// # Safety /// @@ -42,44 +40,15 @@ impl Uart16550 { /// /// [`NUM_REGISTERS`]: crate::spec::NUM_REGISTERS pub unsafe fn init( - coms_enabled: SerialPortBitMask, mut config: [Option; 4], ) -> Result> { unsafe { Ok(Self { coms: [ - if coms_enabled.contains(SerialPortBitMask::COM1) { - Some(Uart16550Tty::new_port( - SerialPortAddress::COM1.into(), - config[0].take().unwrap_or_default(), - )?) - } else { - None - }, - if coms_enabled.contains(SerialPortBitMask::COM2) { - Some(Uart16550Tty::new_port( - SerialPortAddress::Com2.into(), - config[1].take().unwrap_or_default(), - )?) - } else { - None - }, - if coms_enabled.contains(SerialPortBitMask::COM3) { - Some(Uart16550Tty::new_port( - SerialPortAddress::Com3.into(), - config[2].take().unwrap_or_default(), - )?) - } else { - None - }, - if coms_enabled.contains(SerialPortBitMask::COM4) { - Some(Uart16550Tty::new_port( - SerialPortAddress::Com4.into(), - config[3].take().unwrap_or_default(), - )?) - } else { - None - }, + init_port(&mut config[0], SerialPortAddress::COM1)?, + init_port(&mut config[1], SerialPortAddress::Com2)?, + init_port(&mut config[2], SerialPortAddress::Com3)?, + init_port(&mut config[3], SerialPortAddress::Com4)?, ], }) } -- cgit v1.2.3