1 use core::{fmt::Debug, sync::atomic::AtomicU32}; 2 3 use alloc::sync::Arc; 4 use system_error::SystemError; 5 6 use crate::{driver::base::device::device_number::DeviceNumber, mm::VirtAddr}; 7 8 use self::serial8250::serial8250_manager; 9 10 pub mod serial8250; 11 12 #[allow(dead_code)] 13 pub trait UartDriver: Debug + Send + Sync { 14 fn device_number(&self) -> DeviceNumber; 15 16 /// 获取最大的设备数量 17 fn max_devs_num(&self) -> i32; 18 19 // todo: 获取指向console的指针(在我们系统里面,将来可能是改进后的Textui Window) 20 } 21 22 /// 串口端口应当实现的trait 23 /// 24 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/serial_core.h#428 25 #[allow(dead_code)] 26 pub trait UartPort { 27 fn iobase(&self) -> Option<usize> { 28 None 29 } 30 fn membase(&self) -> Option<VirtAddr> { 31 None 32 } 33 fn serial_in(&self, offset: u32) -> u32; 34 fn serial_out(&self, offset: u32, value: u32); 35 fn divisor(&self, baud: BaudRate) -> (u32, DivisorFraction); 36 fn set_divisor(&self, baud: BaudRate) -> Result<(), SystemError>; 37 fn baud_rate(&self) -> Option<BaudRate>; 38 fn startup(&self) -> Result<(), SystemError>; 39 fn shutdown(&self); 40 fn handle_irq(&self) -> Result<(), SystemError>; 41 } 42 43 int_like!(BaudRate, AtomicBaudRate, u32, AtomicU32); 44 int_like!(DivisorFraction, u32); 45 46 #[inline(always)] 47 #[allow(dead_code)] 48 pub(super) fn uart_manager() -> &'static UartManager { 49 &UartManager 50 } 51 52 #[derive(Debug)] 53 pub(super) struct UartManager; 54 55 impl UartManager { 56 /// todo: 把uart设备注册到tty层 57 /// 58 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/tty/serial/serial_core.c?fi=uart_register_driver#2720 59 #[allow(dead_code)] 60 pub fn register_driver(&self, _driver: &Arc<dyn UartDriver>) -> Result<(), SystemError> { 61 return Ok(()); 62 } 63 } 64 65 pub fn serial_early_init() -> Result<(), SystemError> { 66 serial8250_manager().early_init()?; 67 return Ok(()); 68 } 69 70 pub(super) fn serial_init() -> Result<(), SystemError> { 71 serial8250_manager().init()?; 72 return Ok(()); 73 } 74