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