1*dfe53cf0SGnoCiYeH use core::fmt::Debug; 2a03c4f9dSLoGin 3b5b571e0SLoGin use alloc::{ 4b5b571e0SLoGin string::{String, ToString}, 5*dfe53cf0SGnoCiYeH sync::{Arc, Weak}, 6b5b571e0SLoGin vec::Vec, 7b5b571e0SLoGin }; 852da9a59SGnoCiYeH use hashbrown::HashMap; 952da9a59SGnoCiYeH use system_error::SystemError; 10a03c4f9dSLoGin 1152da9a59SGnoCiYeH use crate::{ 1252da9a59SGnoCiYeH driver::{ 1352da9a59SGnoCiYeH base::{ 1452da9a59SGnoCiYeH char::CharDevOps, 1552da9a59SGnoCiYeH device::{ 1652da9a59SGnoCiYeH device_number::{DeviceNumber, Major}, 1752da9a59SGnoCiYeH driver::Driver, 1852da9a59SGnoCiYeH }, 1952da9a59SGnoCiYeH kobject::KObject, 2052da9a59SGnoCiYeH }, 2152da9a59SGnoCiYeH tty::tty_port::TtyPortState, 2252da9a59SGnoCiYeH }, 23*dfe53cf0SGnoCiYeH libs::{ 24*dfe53cf0SGnoCiYeH rwlock::RwLock, 25*dfe53cf0SGnoCiYeH spinlock::{SpinLock, SpinLockGuard}, 26*dfe53cf0SGnoCiYeH }, 2752da9a59SGnoCiYeH }; 28a03c4f9dSLoGin 2952da9a59SGnoCiYeH use super::{ 3052da9a59SGnoCiYeH termios::Termios, 3152da9a59SGnoCiYeH tty_core::{TtyCore, TtyCoreData}, 3252da9a59SGnoCiYeH tty_ldisc::TtyLdiscManager, 33*dfe53cf0SGnoCiYeH tty_port::{DefaultTtyPort, TtyPort}, 3452da9a59SGnoCiYeH }; 35a03c4f9dSLoGin 3652da9a59SGnoCiYeH lazy_static! { 37*dfe53cf0SGnoCiYeH pub static ref TTY_DRIVERS: SpinLock<Vec<Arc<TtyDriver>>> = SpinLock::new(Vec::new()); 38*dfe53cf0SGnoCiYeH } 39*dfe53cf0SGnoCiYeH 40*dfe53cf0SGnoCiYeH pub enum TtyDriverPrivateData { 41*dfe53cf0SGnoCiYeH Unused, 42*dfe53cf0SGnoCiYeH /// true表示主设备 false表示从设备 43*dfe53cf0SGnoCiYeH Pty(bool), 4452da9a59SGnoCiYeH } 45a03c4f9dSLoGin 4652da9a59SGnoCiYeH pub struct TtyDriverManager; 4752da9a59SGnoCiYeH impl TtyDriverManager { 4852da9a59SGnoCiYeH pub fn lookup_tty_driver(dev_num: DeviceNumber) -> Option<(usize, Arc<TtyDriver>)> { 4952da9a59SGnoCiYeH let drivers_guard = TTY_DRIVERS.lock(); 50*dfe53cf0SGnoCiYeH for driver in drivers_guard.iter() { 5152da9a59SGnoCiYeH let base = DeviceNumber::new(driver.major, driver.minor_start); 5252da9a59SGnoCiYeH if dev_num < base || dev_num.data() > base.data() + driver.device_count { 5352da9a59SGnoCiYeH continue; 5452da9a59SGnoCiYeH } 55*dfe53cf0SGnoCiYeH return Some(((dev_num.data() - base.data()) as usize, driver.clone())); 5652da9a59SGnoCiYeH } 57a03c4f9dSLoGin 58a03c4f9dSLoGin None 59a03c4f9dSLoGin } 6052da9a59SGnoCiYeH 6152da9a59SGnoCiYeH /// ## 注册驱动 62*dfe53cf0SGnoCiYeH pub fn tty_register_driver(mut driver: TtyDriver) -> Result<Arc<TtyDriver>, SystemError> { 6352da9a59SGnoCiYeH // 查看是否注册设备号 6452da9a59SGnoCiYeH if driver.major == Major::UNNAMED_MAJOR { 6552da9a59SGnoCiYeH let dev_num = CharDevOps::alloc_chardev_region( 6652da9a59SGnoCiYeH driver.minor_start, 6752da9a59SGnoCiYeH driver.device_count, 6852da9a59SGnoCiYeH driver.name, 6952da9a59SGnoCiYeH )?; 7052da9a59SGnoCiYeH driver.major = dev_num.major(); 7152da9a59SGnoCiYeH driver.minor_start = dev_num.minor(); 7252da9a59SGnoCiYeH } else { 7352da9a59SGnoCiYeH let dev_num = DeviceNumber::new(driver.major, driver.minor_start); 7452da9a59SGnoCiYeH CharDevOps::register_chardev_region(dev_num, driver.device_count, driver.name)?; 75a03c4f9dSLoGin } 76a03c4f9dSLoGin 7752da9a59SGnoCiYeH driver.flags |= TtyDriverFlag::TTY_DRIVER_INSTALLED; 7852da9a59SGnoCiYeH 7952da9a59SGnoCiYeH // 加入全局TtyDriver表 80*dfe53cf0SGnoCiYeH let driver = Arc::new(driver); 81*dfe53cf0SGnoCiYeH TTY_DRIVERS.lock().push(driver.clone()); 8252da9a59SGnoCiYeH 8352da9a59SGnoCiYeH // TODO: 加入procfs? 8452da9a59SGnoCiYeH 85*dfe53cf0SGnoCiYeH Ok(driver) 8652da9a59SGnoCiYeH } 8752da9a59SGnoCiYeH } 8852da9a59SGnoCiYeH 89a03c4f9dSLoGin #[allow(dead_code)] 9052da9a59SGnoCiYeH #[derive(Debug)] 9152da9a59SGnoCiYeH #[cast_to([sync] Driver)] 9252da9a59SGnoCiYeH pub struct TtyDriver { 9352da9a59SGnoCiYeH /// /proc/tty中使用的驱动程序名称 9452da9a59SGnoCiYeH driver_name: String, 9552da9a59SGnoCiYeH /// 用于构造/dev节点名称,例如name设置为tty,则按照name_base分配节点tty0,tty1等 9652da9a59SGnoCiYeH name: &'static str, 9752da9a59SGnoCiYeH /// 命名基数 9852da9a59SGnoCiYeH name_base: usize, 9952da9a59SGnoCiYeH /// 主设备号 10052da9a59SGnoCiYeH major: Major, 10152da9a59SGnoCiYeH /// 起始次设备号 10252da9a59SGnoCiYeH minor_start: u32, 10352da9a59SGnoCiYeH /// 最多支持的tty数量 10452da9a59SGnoCiYeH device_count: u32, 10552da9a59SGnoCiYeH /// tty驱动程序类型 10652da9a59SGnoCiYeH tty_driver_type: TtyDriverType, 10752da9a59SGnoCiYeH /// 驱动程序子类型 10852da9a59SGnoCiYeH tty_driver_sub_type: TtyDriverSubType, 10952da9a59SGnoCiYeH /// 每个tty的默认termios 11052da9a59SGnoCiYeH init_termios: Termios, 11152da9a59SGnoCiYeH /// 懒加载termios,在tty设备关闭时,会将termios按照设备的index保存进这个集合,以便下次打开使用 11252da9a59SGnoCiYeH saved_termios: Vec<Termios>, 11352da9a59SGnoCiYeH /// 驱动程序标志 11452da9a59SGnoCiYeH flags: TtyDriverFlag, 11552da9a59SGnoCiYeH /// pty链接此driver的入口 116*dfe53cf0SGnoCiYeH other_pty_driver: RwLock<Weak<TtyDriver>>, 11752da9a59SGnoCiYeH /// 具体类型的tty驱动方法 11852da9a59SGnoCiYeH driver_funcs: Arc<dyn TtyOperation>, 11952da9a59SGnoCiYeH /// 管理的tty设备列表 12052da9a59SGnoCiYeH ttys: SpinLock<HashMap<usize, Arc<TtyCore>>>, 121*dfe53cf0SGnoCiYeH /// 管理的端口列表 122*dfe53cf0SGnoCiYeH ports: RwLock<Vec<Arc<dyn TtyPort>>>, 12352da9a59SGnoCiYeH // procfs入口? 124a03c4f9dSLoGin } 125a03c4f9dSLoGin 12652da9a59SGnoCiYeH impl TtyDriver { 127b5b571e0SLoGin #[allow(clippy::too_many_arguments)] 12852da9a59SGnoCiYeH pub fn new( 12952da9a59SGnoCiYeH count: u32, 13052da9a59SGnoCiYeH node_name: &'static str, 13152da9a59SGnoCiYeH node_name_base: usize, 13252da9a59SGnoCiYeH major: Major, 13352da9a59SGnoCiYeH minor_start: u32, 13452da9a59SGnoCiYeH tty_driver_type: TtyDriverType, 13552da9a59SGnoCiYeH default_termios: Termios, 13652da9a59SGnoCiYeH driver_funcs: Arc<dyn TtyOperation>, 13752da9a59SGnoCiYeH ) -> Self { 138*dfe53cf0SGnoCiYeH let mut ports: Vec<Arc<dyn TtyPort>> = Vec::with_capacity(count as usize); 139*dfe53cf0SGnoCiYeH for _ in 0..count { 140*dfe53cf0SGnoCiYeH ports.push(Arc::new(DefaultTtyPort::new())) 141*dfe53cf0SGnoCiYeH } 14252da9a59SGnoCiYeH TtyDriver { 14352da9a59SGnoCiYeH driver_name: Default::default(), 14452da9a59SGnoCiYeH name: node_name, 14552da9a59SGnoCiYeH name_base: node_name_base, 14652da9a59SGnoCiYeH major, 14752da9a59SGnoCiYeH minor_start, 14852da9a59SGnoCiYeH device_count: count, 14952da9a59SGnoCiYeH tty_driver_type, 15052da9a59SGnoCiYeH tty_driver_sub_type: Default::default(), 15152da9a59SGnoCiYeH init_termios: default_termios, 15252da9a59SGnoCiYeH flags: TtyDriverFlag::empty(), 153*dfe53cf0SGnoCiYeH other_pty_driver: Default::default(), 15452da9a59SGnoCiYeH driver_funcs, 15552da9a59SGnoCiYeH ttys: SpinLock::new(HashMap::new()), 15652da9a59SGnoCiYeH saved_termios: Vec::with_capacity(count as usize), 157*dfe53cf0SGnoCiYeH ports: RwLock::new(ports), 15852da9a59SGnoCiYeH } 15952da9a59SGnoCiYeH } 160a03c4f9dSLoGin 16152da9a59SGnoCiYeH pub fn tty_line_name(&self, index: usize) -> String { 16252da9a59SGnoCiYeH if self 16352da9a59SGnoCiYeH .flags 16452da9a59SGnoCiYeH .contains(TtyDriverFlag::TTY_DRIVER_UNNUMBERED_NODE) 16552da9a59SGnoCiYeH { 166b5b571e0SLoGin return self.name.to_string(); 16752da9a59SGnoCiYeH } else { 16852da9a59SGnoCiYeH return format!("{}{}", self.name, index + self.name_base); 16952da9a59SGnoCiYeH } 17052da9a59SGnoCiYeH } 17152da9a59SGnoCiYeH 17252da9a59SGnoCiYeH pub fn add_tty(&self, tty_core: Arc<TtyCore>) { 17352da9a59SGnoCiYeH self.ttys.lock().insert(tty_core.core().index(), tty_core); 17452da9a59SGnoCiYeH } 17552da9a59SGnoCiYeH 17652da9a59SGnoCiYeH #[inline] 17752da9a59SGnoCiYeH pub fn driver_funcs(&self) -> Arc<dyn TtyOperation> { 17852da9a59SGnoCiYeH self.driver_funcs.clone() 17952da9a59SGnoCiYeH } 18052da9a59SGnoCiYeH 18152da9a59SGnoCiYeH #[inline] 182*dfe53cf0SGnoCiYeH pub fn init_termios(&self) -> Termios { 183*dfe53cf0SGnoCiYeH self.init_termios 184*dfe53cf0SGnoCiYeH } 185*dfe53cf0SGnoCiYeH 186*dfe53cf0SGnoCiYeH #[inline] 187*dfe53cf0SGnoCiYeH pub fn init_termios_mut(&mut self) -> &mut Termios { 188*dfe53cf0SGnoCiYeH &mut self.init_termios 189*dfe53cf0SGnoCiYeH } 190*dfe53cf0SGnoCiYeH 191*dfe53cf0SGnoCiYeH #[inline] 192*dfe53cf0SGnoCiYeH pub fn other_pty_driver(&self) -> Option<Arc<TtyDriver>> { 193*dfe53cf0SGnoCiYeH self.other_pty_driver.read().upgrade() 194*dfe53cf0SGnoCiYeH } 195*dfe53cf0SGnoCiYeH 196*dfe53cf0SGnoCiYeH pub fn set_other_pty_driver(&self, driver: Weak<TtyDriver>) { 197*dfe53cf0SGnoCiYeH *self.other_pty_driver.write() = driver 198*dfe53cf0SGnoCiYeH } 199*dfe53cf0SGnoCiYeH 200*dfe53cf0SGnoCiYeH #[inline] 201*dfe53cf0SGnoCiYeH pub fn set_subtype(&mut self, tp: TtyDriverSubType) { 202*dfe53cf0SGnoCiYeH self.tty_driver_sub_type = tp; 203*dfe53cf0SGnoCiYeH } 204*dfe53cf0SGnoCiYeH 205*dfe53cf0SGnoCiYeH #[inline] 206*dfe53cf0SGnoCiYeH pub fn ttys(&self) -> SpinLockGuard<HashMap<usize, Arc<TtyCore>>> { 207*dfe53cf0SGnoCiYeH self.ttys.lock() 208*dfe53cf0SGnoCiYeH } 209*dfe53cf0SGnoCiYeH 210*dfe53cf0SGnoCiYeH #[inline] 211*dfe53cf0SGnoCiYeH pub fn saved_termios(&self) -> &Vec<Termios> { 212*dfe53cf0SGnoCiYeH &self.saved_termios 213*dfe53cf0SGnoCiYeH } 214*dfe53cf0SGnoCiYeH 215*dfe53cf0SGnoCiYeH #[inline] 21652da9a59SGnoCiYeH pub fn flags(&self) -> TtyDriverFlag { 21752da9a59SGnoCiYeH self.flags 21852da9a59SGnoCiYeH } 21952da9a59SGnoCiYeH 22052da9a59SGnoCiYeH #[inline] 221*dfe53cf0SGnoCiYeH fn lookup_tty(&self, index: usize) -> Option<Arc<TtyCore>> { 222*dfe53cf0SGnoCiYeH let ret = self 223*dfe53cf0SGnoCiYeH .driver_funcs() 224*dfe53cf0SGnoCiYeH .lookup(index, TtyDriverPrivateData::Unused); 225*dfe53cf0SGnoCiYeH if let Err(SystemError::ENOSYS) = ret { 22652da9a59SGnoCiYeH let device_guard = self.ttys.lock(); 227b5b571e0SLoGin return device_guard.get(&index).cloned(); 22852da9a59SGnoCiYeH } 229*dfe53cf0SGnoCiYeH ret.ok() 230*dfe53cf0SGnoCiYeH } 23152da9a59SGnoCiYeH 23252da9a59SGnoCiYeH fn standard_install(&self, tty_core: Arc<TtyCore>) -> Result<(), SystemError> { 23352da9a59SGnoCiYeH let tty = tty_core.core(); 234*dfe53cf0SGnoCiYeH tty.init_termios(); 23552da9a59SGnoCiYeH // TODO:设置termios波特率? 23652da9a59SGnoCiYeH 23752da9a59SGnoCiYeH tty.add_count(); 23852da9a59SGnoCiYeH 239*dfe53cf0SGnoCiYeH self.ttys.lock().insert(tty.index(), tty_core); 24052da9a59SGnoCiYeH 24152da9a59SGnoCiYeH Ok(()) 24252da9a59SGnoCiYeH } 24352da9a59SGnoCiYeH 24452da9a59SGnoCiYeH fn driver_install_tty(driver: Arc<TtyDriver>, tty: Arc<TtyCore>) -> Result<(), SystemError> { 24552da9a59SGnoCiYeH let res = tty.install(driver.clone(), tty.clone()); 24652da9a59SGnoCiYeH 247b5b571e0SLoGin if let Err(err) = res { 24852da9a59SGnoCiYeH if err == SystemError::ENOSYS { 24952da9a59SGnoCiYeH return driver.standard_install(tty); 25052da9a59SGnoCiYeH } else { 25152da9a59SGnoCiYeH return Err(err); 25252da9a59SGnoCiYeH } 25352da9a59SGnoCiYeH } 25452da9a59SGnoCiYeH 25552da9a59SGnoCiYeH driver.add_tty(tty); 25652da9a59SGnoCiYeH 25752da9a59SGnoCiYeH Ok(()) 25852da9a59SGnoCiYeH } 25952da9a59SGnoCiYeH 260*dfe53cf0SGnoCiYeH pub fn init_tty_device( 261*dfe53cf0SGnoCiYeH driver: Arc<TtyDriver>, 262*dfe53cf0SGnoCiYeH index: usize, 263*dfe53cf0SGnoCiYeH ) -> Result<Arc<TtyCore>, SystemError> { 26452da9a59SGnoCiYeH let tty = TtyCore::new(driver.clone(), index); 26552da9a59SGnoCiYeH 26652da9a59SGnoCiYeH Self::driver_install_tty(driver.clone(), tty.clone())?; 26752da9a59SGnoCiYeH 26852da9a59SGnoCiYeH let core = tty.core(); 26952da9a59SGnoCiYeH 27052da9a59SGnoCiYeH if core.port().is_none() { 271*dfe53cf0SGnoCiYeH let ports = driver.ports.read(); 272*dfe53cf0SGnoCiYeH ports[core.index()].setup_internal_tty(Arc::downgrade(&tty)); 273*dfe53cf0SGnoCiYeH tty.set_port(ports[core.index()].clone()); 27452da9a59SGnoCiYeH } 27552da9a59SGnoCiYeH 27652da9a59SGnoCiYeH TtyLdiscManager::ldisc_setup(tty.clone(), None)?; 27752da9a59SGnoCiYeH 27852da9a59SGnoCiYeH Ok(tty) 27952da9a59SGnoCiYeH } 28052da9a59SGnoCiYeH 28152da9a59SGnoCiYeH /// ## 通过设备号找到对应驱动并且初始化Tty 282*dfe53cf0SGnoCiYeH pub fn open_tty(index: usize, driver: Arc<TtyDriver>) -> Result<Arc<TtyCore>, SystemError> { 283*dfe53cf0SGnoCiYeH let tty = match driver.lookup_tty(index) { 28452da9a59SGnoCiYeH Some(tty) => { 28552da9a59SGnoCiYeH // TODO: 暂时这么写,因为还没写TtyPort 28652da9a59SGnoCiYeH if tty.core().port().is_none() { 28752da9a59SGnoCiYeH kwarn!("{} port is None", tty.core().name()); 288b5b571e0SLoGin } else if tty.core().port().unwrap().state() == TtyPortState::KOPENED { 28952da9a59SGnoCiYeH return Err(SystemError::EBUSY); 29052da9a59SGnoCiYeH } 29152da9a59SGnoCiYeH 29252da9a59SGnoCiYeH tty.reopen()?; 29352da9a59SGnoCiYeH tty 29452da9a59SGnoCiYeH } 29552da9a59SGnoCiYeH None => Self::init_tty_device(driver, index)?, 29652da9a59SGnoCiYeH }; 29752da9a59SGnoCiYeH 29852da9a59SGnoCiYeH return Ok(tty); 29952da9a59SGnoCiYeH } 30052da9a59SGnoCiYeH 30152da9a59SGnoCiYeH pub fn tty_driver_type(&self) -> TtyDriverType { 30252da9a59SGnoCiYeH self.tty_driver_type 30352da9a59SGnoCiYeH } 30452da9a59SGnoCiYeH 30552da9a59SGnoCiYeH pub fn tty_driver_sub_type(&self) -> TtyDriverSubType { 30652da9a59SGnoCiYeH self.tty_driver_sub_type 30752da9a59SGnoCiYeH } 30852da9a59SGnoCiYeH } 30952da9a59SGnoCiYeH 31052da9a59SGnoCiYeH impl KObject for TtyDriver { 31152da9a59SGnoCiYeH fn as_any_ref(&self) -> &dyn core::any::Any { 31252da9a59SGnoCiYeH todo!() 31352da9a59SGnoCiYeH } 31452da9a59SGnoCiYeH 31552da9a59SGnoCiYeH fn set_inode(&self, _inode: Option<alloc::sync::Arc<crate::filesystem::kernfs::KernFSInode>>) { 31652da9a59SGnoCiYeH todo!() 31752da9a59SGnoCiYeH } 31852da9a59SGnoCiYeH 31952da9a59SGnoCiYeH fn inode(&self) -> Option<alloc::sync::Arc<crate::filesystem::kernfs::KernFSInode>> { 32052da9a59SGnoCiYeH todo!() 32152da9a59SGnoCiYeH } 32252da9a59SGnoCiYeH 32352da9a59SGnoCiYeH fn parent(&self) -> Option<alloc::sync::Weak<dyn KObject>> { 32452da9a59SGnoCiYeH todo!() 32552da9a59SGnoCiYeH } 32652da9a59SGnoCiYeH 32752da9a59SGnoCiYeH fn set_parent(&self, _parent: Option<alloc::sync::Weak<dyn KObject>>) { 32852da9a59SGnoCiYeH todo!() 32952da9a59SGnoCiYeH } 33052da9a59SGnoCiYeH 33152da9a59SGnoCiYeH fn kset(&self) -> Option<alloc::sync::Arc<crate::driver::base::kset::KSet>> { 33252da9a59SGnoCiYeH todo!() 33352da9a59SGnoCiYeH } 33452da9a59SGnoCiYeH 33552da9a59SGnoCiYeH fn set_kset(&self, _kset: Option<alloc::sync::Arc<crate::driver::base::kset::KSet>>) { 33652da9a59SGnoCiYeH todo!() 33752da9a59SGnoCiYeH } 33852da9a59SGnoCiYeH 33952da9a59SGnoCiYeH fn kobj_type(&self) -> Option<&'static dyn crate::driver::base::kobject::KObjType> { 34052da9a59SGnoCiYeH todo!() 34152da9a59SGnoCiYeH } 34252da9a59SGnoCiYeH 34352da9a59SGnoCiYeH fn set_kobj_type(&self, _ktype: Option<&'static dyn crate::driver::base::kobject::KObjType>) { 34452da9a59SGnoCiYeH todo!() 34552da9a59SGnoCiYeH } 34652da9a59SGnoCiYeH 34752da9a59SGnoCiYeH fn name(&self) -> alloc::string::String { 34852da9a59SGnoCiYeH todo!() 34952da9a59SGnoCiYeH } 35052da9a59SGnoCiYeH 35152da9a59SGnoCiYeH fn set_name(&self, _name: alloc::string::String) { 35252da9a59SGnoCiYeH todo!() 35352da9a59SGnoCiYeH } 35452da9a59SGnoCiYeH 35552da9a59SGnoCiYeH fn kobj_state( 35652da9a59SGnoCiYeH &self, 35752da9a59SGnoCiYeH ) -> crate::libs::rwlock::RwLockReadGuard<crate::driver::base::kobject::KObjectState> { 35852da9a59SGnoCiYeH todo!() 35952da9a59SGnoCiYeH } 36052da9a59SGnoCiYeH 36152da9a59SGnoCiYeH fn kobj_state_mut( 36252da9a59SGnoCiYeH &self, 36352da9a59SGnoCiYeH ) -> crate::libs::rwlock::RwLockWriteGuard<crate::driver::base::kobject::KObjectState> { 36452da9a59SGnoCiYeH todo!() 36552da9a59SGnoCiYeH } 36652da9a59SGnoCiYeH 36752da9a59SGnoCiYeH fn set_kobj_state(&self, _state: crate::driver::base::kobject::KObjectState) { 36852da9a59SGnoCiYeH todo!() 36952da9a59SGnoCiYeH } 37052da9a59SGnoCiYeH } 37152da9a59SGnoCiYeH 37252da9a59SGnoCiYeH impl Driver for TtyDriver { 37352da9a59SGnoCiYeH fn id_table(&self) -> Option<crate::driver::base::device::IdTable> { 37452da9a59SGnoCiYeH todo!() 37552da9a59SGnoCiYeH } 37652da9a59SGnoCiYeH 37752da9a59SGnoCiYeH fn devices( 37852da9a59SGnoCiYeH &self, 37952da9a59SGnoCiYeH ) -> alloc::vec::Vec<alloc::sync::Arc<dyn crate::driver::base::device::Device>> { 38052da9a59SGnoCiYeH todo!() 38152da9a59SGnoCiYeH } 38252da9a59SGnoCiYeH 38352da9a59SGnoCiYeH fn add_device(&self, _device: alloc::sync::Arc<dyn crate::driver::base::device::Device>) { 38452da9a59SGnoCiYeH todo!() 38552da9a59SGnoCiYeH } 38652da9a59SGnoCiYeH 38752da9a59SGnoCiYeH fn delete_device(&self, _device: &alloc::sync::Arc<dyn crate::driver::base::device::Device>) { 38852da9a59SGnoCiYeH todo!() 38952da9a59SGnoCiYeH } 39052da9a59SGnoCiYeH 39152da9a59SGnoCiYeH fn set_bus(&self, _bus: Option<alloc::sync::Weak<dyn crate::driver::base::device::bus::Bus>>) { 39252da9a59SGnoCiYeH todo!() 39352da9a59SGnoCiYeH } 39452da9a59SGnoCiYeH } 39552da9a59SGnoCiYeH 39652da9a59SGnoCiYeH pub trait TtyOperation: Sync + Send + Debug { 39752da9a59SGnoCiYeH fn install(&self, _driver: Arc<TtyDriver>, _tty: Arc<TtyCore>) -> Result<(), SystemError> { 39852da9a59SGnoCiYeH return Err(SystemError::ENOSYS); 39952da9a59SGnoCiYeH } 40052da9a59SGnoCiYeH 40152da9a59SGnoCiYeH fn open(&self, tty: &TtyCoreData) -> Result<(), SystemError>; 40252da9a59SGnoCiYeH 40352da9a59SGnoCiYeH /// ## 获取可写字符数 40452da9a59SGnoCiYeH fn write_room(&self, _tty: &TtyCoreData) -> usize { 40552da9a59SGnoCiYeH // 默认 40652da9a59SGnoCiYeH 2048 40752da9a59SGnoCiYeH } 40852da9a59SGnoCiYeH 40952da9a59SGnoCiYeH fn write(&self, tty: &TtyCoreData, buf: &[u8], nr: usize) -> Result<usize, SystemError>; 41052da9a59SGnoCiYeH 41152da9a59SGnoCiYeH fn flush_chars(&self, tty: &TtyCoreData); 41252da9a59SGnoCiYeH 413*dfe53cf0SGnoCiYeH fn put_char(&self, _tty: &TtyCoreData, _ch: u8) -> Result<(), SystemError> { 414*dfe53cf0SGnoCiYeH Err(SystemError::ENOSYS) 415*dfe53cf0SGnoCiYeH } 41652da9a59SGnoCiYeH 41752da9a59SGnoCiYeH fn start(&self, _tty: &TtyCoreData) -> Result<(), SystemError> { 41852da9a59SGnoCiYeH Err(SystemError::ENOSYS) 41952da9a59SGnoCiYeH } 42052da9a59SGnoCiYeH 42152da9a59SGnoCiYeH fn stop(&self, _tty: &TtyCoreData) -> Result<(), SystemError> { 42252da9a59SGnoCiYeH Err(SystemError::ENOSYS) 42352da9a59SGnoCiYeH } 42452da9a59SGnoCiYeH 42552da9a59SGnoCiYeH fn flush_buffer(&self, _tty: &TtyCoreData) -> Result<(), SystemError> { 42652da9a59SGnoCiYeH Err(SystemError::ENOSYS) 42752da9a59SGnoCiYeH } 42852da9a59SGnoCiYeH 42952da9a59SGnoCiYeH fn ioctl(&self, tty: Arc<TtyCore>, cmd: u32, arg: usize) -> Result<(), SystemError>; 43052da9a59SGnoCiYeH 43152da9a59SGnoCiYeH fn chars_in_buffer(&self) -> usize { 43252da9a59SGnoCiYeH 0 43352da9a59SGnoCiYeH } 43452da9a59SGnoCiYeH 43552da9a59SGnoCiYeH fn set_termios(&self, _tty: Arc<TtyCore>, _old_termios: Termios) -> Result<(), SystemError> { 43652da9a59SGnoCiYeH Err(SystemError::ENOSYS) 43752da9a59SGnoCiYeH } 438*dfe53cf0SGnoCiYeH 439*dfe53cf0SGnoCiYeH fn lookup( 440*dfe53cf0SGnoCiYeH &self, 441*dfe53cf0SGnoCiYeH _index: usize, 442*dfe53cf0SGnoCiYeH _priv_data: TtyDriverPrivateData, 443*dfe53cf0SGnoCiYeH ) -> Result<Arc<TtyCore>, SystemError> { 444*dfe53cf0SGnoCiYeH Err(SystemError::ENOSYS) 445*dfe53cf0SGnoCiYeH } 446*dfe53cf0SGnoCiYeH 447*dfe53cf0SGnoCiYeH fn close(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>; 44852da9a59SGnoCiYeH } 44952da9a59SGnoCiYeH 45052da9a59SGnoCiYeH #[allow(dead_code)] 45152da9a59SGnoCiYeH #[derive(Debug, PartialEq, Copy, Clone)] 45252da9a59SGnoCiYeH pub enum TtyDriverType { 45352da9a59SGnoCiYeH System, 45452da9a59SGnoCiYeH Console, 45552da9a59SGnoCiYeH Serial, 45652da9a59SGnoCiYeH Pty, 45752da9a59SGnoCiYeH Scc, 45852da9a59SGnoCiYeH Syscons, 45952da9a59SGnoCiYeH } 46052da9a59SGnoCiYeH 46152da9a59SGnoCiYeH #[allow(dead_code)] 46252da9a59SGnoCiYeH #[derive(Debug, PartialEq, Copy, Clone)] 46352da9a59SGnoCiYeH pub enum TtyDriverSubType { 46452da9a59SGnoCiYeH Undefined, 46552da9a59SGnoCiYeH Tty, 46652da9a59SGnoCiYeH Console, 46752da9a59SGnoCiYeH Syscons, 46852da9a59SGnoCiYeH Sysptmx, 46952da9a59SGnoCiYeH PtyMaster, 47052da9a59SGnoCiYeH PtySlave, 47152da9a59SGnoCiYeH SerialNormal, 47252da9a59SGnoCiYeH } 47352da9a59SGnoCiYeH 47452da9a59SGnoCiYeH impl Default for TtyDriverSubType { 47552da9a59SGnoCiYeH fn default() -> Self { 47652da9a59SGnoCiYeH Self::Undefined 47752da9a59SGnoCiYeH } 47852da9a59SGnoCiYeH } 479a03c4f9dSLoGin 480a03c4f9dSLoGin bitflags! { 48152da9a59SGnoCiYeH pub struct TtyDriverFlag: u32 { 48252da9a59SGnoCiYeH /// 表示 tty 驱动程序已安装 48352da9a59SGnoCiYeH const TTY_DRIVER_INSTALLED = 0x0001; 48452da9a59SGnoCiYeH /// 请求 tty 层在最后一个进程关闭设备时重置 termios 设置 48552da9a59SGnoCiYeH const TTY_DRIVER_RESET_TERMIOS = 0x0002; 48652da9a59SGnoCiYeH /// 表示驱动程序将保证在设置了该标志的 tty 上不设置任何特殊字符处理标志(原模式) 48752da9a59SGnoCiYeH const TTY_DRIVER_REAL_RAW = 0x0004; 488a03c4f9dSLoGin 48952da9a59SGnoCiYeH /// 以下四个标志位为内存分配相关,目前设计无需使用 49052da9a59SGnoCiYeH const TTY_DRIVER_DYNAMIC_DEV = 0x0008; 49152da9a59SGnoCiYeH const TTY_DRIVER_DEVPTS_MEM = 0x0010; 49252da9a59SGnoCiYeH const TTY_DRIVER_HARDWARE_BREAK = 0x0020; 49352da9a59SGnoCiYeH const TTY_DRIVER_DYNAMIC_ALLOC = 0x0040; 49452da9a59SGnoCiYeH 49552da9a59SGnoCiYeH /// 表示不创建带有编号的 /dev 节点。 49652da9a59SGnoCiYeH /// 例如,创建 /dev/ttyprintk 而不是 /dev/ttyprintk0。仅在为单个 tty 设备分配驱动程序时适用。 49752da9a59SGnoCiYeH const TTY_DRIVER_UNNUMBERED_NODE = 0x0080; 498a03c4f9dSLoGin } 499a03c4f9dSLoGin } 500