summaryrefslogtreecommitdiff
path: root/kernel/src/io
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2026-09-06 19:14:52 +0800
committerlittleblack111 <littleblack11111@gmail.com>2026-09-06 19:14:52 +0800
commitad877e5ca439b5ce9f54c1673b2bcc9607db828f (patch)
treec2d7ef3112259e366a4d09bd2f9c7e55202904bd /kernel/src/io
parent4820b2dd73543a6fbd5db547d009529d0fe11ccd (diff)
refactor: earlyprintk and uart_16550
Diffstat (limited to 'kernel/src/io')
-rw-r--r--kernel/src/io/output/serial/uart_16550.rs55
1 files changed, 12 insertions, 43 deletions
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<SerialPortAddress> for u16 {
fn from(value: SerialPortAddress) -> Self {
value as u16
@@ -32,6 +22,14 @@ pub struct Uart16550 {
pub coms: [Option<Uart16550Tty<PioBackend>>; 4],
}
+#[inline(always)]
+fn init_port(
+ cfg: &mut Option<Config>,
+ addr: SerialPortAddress,
+) -> Result<Option<Uart16550Tty<PioBackend>>, Uart16550TtyError<PortIoAddress>> {
+ 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<Config>; 4],
) -> Result<Self, Uart16550TtyError<PortIoAddress>> {
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)?,
],
})
}