120e3152eSlogin use alloc::{ 220e3152eSlogin string::{String, ToString}, 320e3152eSlogin sync::{Arc, Weak}, 420e3152eSlogin }; 591e9d4abSLoGin use system_error::SystemError; 6c566df45SLoGin use unified_init::macros::unified_init; 70d48c3c9Slogin 80d48c3c9Slogin use crate::{ 952da9a59SGnoCiYeH arch::ipc::signal::Signal, 1052da9a59SGnoCiYeH driver::{ 1152da9a59SGnoCiYeH base::{ 1252da9a59SGnoCiYeH char::CharDevice, 1352da9a59SGnoCiYeH device::{ 1452da9a59SGnoCiYeH bus::Bus, 1552da9a59SGnoCiYeH device_number::{DeviceNumber, Major}, 1652da9a59SGnoCiYeH device_register, 1752da9a59SGnoCiYeH driver::Driver, 1852da9a59SGnoCiYeH Device, DeviceKObjType, DeviceType, IdTable, 1952da9a59SGnoCiYeH }, 2052da9a59SGnoCiYeH kobject::{KObject, LockedKObjectState}, 2152da9a59SGnoCiYeH kset::KSet, 2252da9a59SGnoCiYeH }, 2352da9a59SGnoCiYeH serial::serial_init, 2452da9a59SGnoCiYeH }, 250d48c3c9Slogin filesystem::{ 2620e3152eSlogin devfs::{devfs_register, DevFS, DeviceINode}, 2752da9a59SGnoCiYeH kernfs::KernFSInode, 2852da9a59SGnoCiYeH vfs::{file::FileMode, syscall::ModeType, FilePrivateData, FileType, IndexNode, Metadata}, 290d48c3c9Slogin }, 30c566df45SLoGin init::initcall::INITCALL_DEVICE, 3152da9a59SGnoCiYeH libs::rwlock::RwLock, 3252da9a59SGnoCiYeH mm::VirtAddr, 33*52bcb59eSGnoCiYeH net::event_poll::{EPollItem, EventPoll}, 3452da9a59SGnoCiYeH process::ProcessManager, 35*52bcb59eSGnoCiYeH syscall::user_access::{UserBufferReader, UserBufferWriter}, 360d48c3c9Slogin }; 370d48c3c9Slogin 3852da9a59SGnoCiYeH use super::{ 3952da9a59SGnoCiYeH termios::WindowSize, 4052da9a59SGnoCiYeH tty_core::{TtyCore, TtyFlag, TtyIoctlCmd}, 4152da9a59SGnoCiYeH tty_driver::{TtyDriver, TtyDriverSubType, TtyDriverType, TtyOperation}, 4252da9a59SGnoCiYeH tty_job_control::TtyJobCtrlManager, 4352da9a59SGnoCiYeH virtual_terminal::vty_init, 4452da9a59SGnoCiYeH }; 450d48c3c9Slogin 4652da9a59SGnoCiYeH #[derive(Debug)] 4752da9a59SGnoCiYeH pub struct InnerTtyDevice { 4852da9a59SGnoCiYeH /// 当前设备所述的kset 4952da9a59SGnoCiYeH kset: Option<Arc<KSet>>, 5052da9a59SGnoCiYeH parent_kobj: Option<Weak<dyn KObject>>, 5152da9a59SGnoCiYeH /// 当前设备所述的总线 5252da9a59SGnoCiYeH bus: Option<Weak<dyn Bus>>, 5352da9a59SGnoCiYeH inode: Option<Arc<KernFSInode>>, 5452da9a59SGnoCiYeH driver: Option<Weak<dyn Driver>>, 5552da9a59SGnoCiYeH can_match: bool, 5652da9a59SGnoCiYeH 5752da9a59SGnoCiYeH metadata: Metadata, 5820e3152eSlogin } 5920e3152eSlogin 6052da9a59SGnoCiYeH impl InnerTtyDevice { 6152da9a59SGnoCiYeH pub fn new() -> Self { 6252da9a59SGnoCiYeH Self { 6352da9a59SGnoCiYeH kset: None, 6452da9a59SGnoCiYeH parent_kobj: None, 6552da9a59SGnoCiYeH bus: None, 6652da9a59SGnoCiYeH inode: None, 6752da9a59SGnoCiYeH driver: None, 6852da9a59SGnoCiYeH can_match: false, 6952da9a59SGnoCiYeH metadata: Metadata::new(FileType::CharDevice, ModeType::from_bits_truncate(0o755)), 7052da9a59SGnoCiYeH } 7152da9a59SGnoCiYeH } 7252da9a59SGnoCiYeH } 7352da9a59SGnoCiYeH 740d48c3c9Slogin #[derive(Debug)] 7552da9a59SGnoCiYeH #[cast_to([sync] Device)] 760d48c3c9Slogin pub struct TtyDevice { 7752da9a59SGnoCiYeH name: &'static str, 7852da9a59SGnoCiYeH id_table: IdTable, 7952da9a59SGnoCiYeH inner: RwLock<InnerTtyDevice>, 8052da9a59SGnoCiYeH kobj_state: LockedKObjectState, 8120e3152eSlogin /// TTY所属的文件系统 8220e3152eSlogin fs: RwLock<Weak<DevFS>>, 830d48c3c9Slogin } 840d48c3c9Slogin 850d48c3c9Slogin impl TtyDevice { 8652da9a59SGnoCiYeH pub fn new(name: &'static str, id_table: IdTable) -> Arc<TtyDevice> { 8752da9a59SGnoCiYeH let dev_num = id_table.device_number(); 8852da9a59SGnoCiYeH let dev = TtyDevice { 8952da9a59SGnoCiYeH name, 9052da9a59SGnoCiYeH id_table, 9152da9a59SGnoCiYeH inner: RwLock::new(InnerTtyDevice::new()), 9252da9a59SGnoCiYeH kobj_state: LockedKObjectState::new(None), 930d48c3c9Slogin fs: RwLock::new(Weak::default()), 9452da9a59SGnoCiYeH }; 9552da9a59SGnoCiYeH 9652da9a59SGnoCiYeH dev.inner.write().metadata.raw_dev = dev_num; 9752da9a59SGnoCiYeH 9852da9a59SGnoCiYeH Arc::new(dev) 9952da9a59SGnoCiYeH } 1000d48c3c9Slogin } 1010d48c3c9Slogin 10252da9a59SGnoCiYeH impl IndexNode for TtyDevice { 10352da9a59SGnoCiYeH fn open( 1040d48c3c9Slogin &self, 10552da9a59SGnoCiYeH data: &mut crate::filesystem::vfs::FilePrivateData, 10652da9a59SGnoCiYeH mode: &crate::filesystem::vfs::file::FileMode, 10752da9a59SGnoCiYeH ) -> Result<(), SystemError> { 10852da9a59SGnoCiYeH let dev_num = self.metadata()?.raw_dev; 10952da9a59SGnoCiYeH 11052da9a59SGnoCiYeH let tty = TtyDriver::open_tty(dev_num)?; 11152da9a59SGnoCiYeH 11252da9a59SGnoCiYeH // 设置privdata 11352da9a59SGnoCiYeH *data = FilePrivateData::Tty(TtyFilePrivateData { 11452da9a59SGnoCiYeH tty: tty.clone(), 11552da9a59SGnoCiYeH mode: *mode, 11652da9a59SGnoCiYeH }); 11752da9a59SGnoCiYeH 11852da9a59SGnoCiYeH let ret = tty.open(tty.core()); 11952da9a59SGnoCiYeH if ret.is_err() { 12052da9a59SGnoCiYeH let err = ret.unwrap_err(); 12152da9a59SGnoCiYeH if err == SystemError::ENOSYS { 12252da9a59SGnoCiYeH return Err(SystemError::ENODEV); 1230d48c3c9Slogin } 12452da9a59SGnoCiYeH return Err(err); 12552da9a59SGnoCiYeH } 12652da9a59SGnoCiYeH 12752da9a59SGnoCiYeH let driver = tty.core().driver(); 12852da9a59SGnoCiYeH // 考虑noctty(当前tty) 12952da9a59SGnoCiYeH if !(mode.contains(FileMode::O_NOCTTY) && dev_num == DeviceNumber::new(Major::TTY_MAJOR, 0) 13052da9a59SGnoCiYeH || dev_num == DeviceNumber::new(Major::TTYAUX_MAJOR, 1) 13152da9a59SGnoCiYeH || (driver.tty_driver_type() == TtyDriverType::Pty 13252da9a59SGnoCiYeH && driver.tty_driver_sub_type() == TtyDriverSubType::PtyMaster)) 13352da9a59SGnoCiYeH { 13452da9a59SGnoCiYeH let pcb = ProcessManager::current_pcb(); 135*52bcb59eSGnoCiYeH let pcb_tty = pcb.sig_info_irqsave().tty(); 13652da9a59SGnoCiYeH if pcb_tty.is_none() && tty.core().contorl_info_irqsave().session.is_none() { 13752da9a59SGnoCiYeH TtyJobCtrlManager::proc_set_tty(tty); 13852da9a59SGnoCiYeH } 13952da9a59SGnoCiYeH } 14052da9a59SGnoCiYeH 14152da9a59SGnoCiYeH Ok(()) 14252da9a59SGnoCiYeH } 14352da9a59SGnoCiYeH 14452da9a59SGnoCiYeH fn read_at( 14552da9a59SGnoCiYeH &self, 14652da9a59SGnoCiYeH _offset: usize, 14752da9a59SGnoCiYeH len: usize, 14852da9a59SGnoCiYeH buf: &mut [u8], 14952da9a59SGnoCiYeH data: &mut crate::filesystem::vfs::FilePrivateData, 15052da9a59SGnoCiYeH ) -> Result<usize, system_error::SystemError> { 15152da9a59SGnoCiYeH let (tty, mode) = if let FilePrivateData::Tty(tty_priv) = data { 15252da9a59SGnoCiYeH (tty_priv.tty.clone(), tty_priv.mode) 15352da9a59SGnoCiYeH } else { 154676b8ef6SMork return Err(SystemError::EIO); 15552da9a59SGnoCiYeH }; 15652da9a59SGnoCiYeH 15752da9a59SGnoCiYeH let ld = tty.ldisc(); 15852da9a59SGnoCiYeH let mut offset = 0; 15952da9a59SGnoCiYeH let mut cookie = false; 16052da9a59SGnoCiYeH loop { 16152da9a59SGnoCiYeH let mut size = if len > buf.len() { buf.len() } else { len }; 16252da9a59SGnoCiYeH size = ld.read(tty.clone(), buf, size, &mut cookie, offset, mode)?; 16352da9a59SGnoCiYeH // 没有更多数据 16452da9a59SGnoCiYeH if size == 0 { 16552da9a59SGnoCiYeH break; 1660d48c3c9Slogin } 16720e3152eSlogin 16852da9a59SGnoCiYeH offset += size; 16952da9a59SGnoCiYeH 17052da9a59SGnoCiYeH // 缓冲区写满 17152da9a59SGnoCiYeH if offset >= len { 17252da9a59SGnoCiYeH break; 17320e3152eSlogin } 17420e3152eSlogin 17552da9a59SGnoCiYeH // 没有更多数据 17652da9a59SGnoCiYeH if !cookie { 17752da9a59SGnoCiYeH break; 17820e3152eSlogin } 17920e3152eSlogin } 18020e3152eSlogin 18152da9a59SGnoCiYeH return Ok(offset); 18220e3152eSlogin } 18320e3152eSlogin 18452da9a59SGnoCiYeH fn write_at( 18552da9a59SGnoCiYeH &self, 18652da9a59SGnoCiYeH _offset: usize, 18752da9a59SGnoCiYeH len: usize, 18852da9a59SGnoCiYeH buf: &[u8], 18952da9a59SGnoCiYeH data: &mut crate::filesystem::vfs::FilePrivateData, 19052da9a59SGnoCiYeH ) -> Result<usize, system_error::SystemError> { 19152da9a59SGnoCiYeH let mut count = len; 19252da9a59SGnoCiYeH let (tty, mode) = if let FilePrivateData::Tty(tty_priv) = data { 19352da9a59SGnoCiYeH (tty_priv.tty.clone(), tty_priv.mode) 19452da9a59SGnoCiYeH } else { 19552da9a59SGnoCiYeH return Err(SystemError::EIO); 19652da9a59SGnoCiYeH }; 19752da9a59SGnoCiYeH 19852da9a59SGnoCiYeH let ld = tty.ldisc(); 19952da9a59SGnoCiYeH let core = tty.core(); 20052da9a59SGnoCiYeH let mut chunk = 2048; 20152da9a59SGnoCiYeH if core.flags().contains(TtyFlag::NO_WRITE_SPLIT) { 20252da9a59SGnoCiYeH chunk = 65536; 20352da9a59SGnoCiYeH } 20452da9a59SGnoCiYeH chunk = chunk.min(count); 20552da9a59SGnoCiYeH 20652da9a59SGnoCiYeH let pcb = ProcessManager::current_pcb(); 20752da9a59SGnoCiYeH let mut written = 0; 20852da9a59SGnoCiYeH loop { 20952da9a59SGnoCiYeH // 至少需要写多少 21052da9a59SGnoCiYeH let size = chunk.min(count); 21152da9a59SGnoCiYeH 21252da9a59SGnoCiYeH // 将数据从buf拷贝到writebuf 21352da9a59SGnoCiYeH 214*52bcb59eSGnoCiYeH let ret = ld.write(tty.clone(), &buf[written..], size, mode)?; 21552da9a59SGnoCiYeH 21652da9a59SGnoCiYeH written += ret; 21752da9a59SGnoCiYeH count -= ret; 21852da9a59SGnoCiYeH 21952da9a59SGnoCiYeH if count == 0 { 22052da9a59SGnoCiYeH break; 22152da9a59SGnoCiYeH } 22252da9a59SGnoCiYeH 223*52bcb59eSGnoCiYeH if pcb.sig_info_irqsave().sig_pending().has_pending() { 22452da9a59SGnoCiYeH return Err(SystemError::ERESTARTSYS); 22520e3152eSlogin } 22620e3152eSlogin } 22752da9a59SGnoCiYeH 22852da9a59SGnoCiYeH if written > 0 { 22952da9a59SGnoCiYeH // todo: 更新时间 23052da9a59SGnoCiYeH } 23152da9a59SGnoCiYeH 23252da9a59SGnoCiYeH Ok(written) 23352da9a59SGnoCiYeH } 23452da9a59SGnoCiYeH 23552da9a59SGnoCiYeH fn fs(&self) -> Arc<dyn crate::filesystem::vfs::FileSystem> { 23652da9a59SGnoCiYeH todo!() 23752da9a59SGnoCiYeH } 23852da9a59SGnoCiYeH 23952da9a59SGnoCiYeH fn as_any_ref(&self) -> &dyn core::any::Any { 24052da9a59SGnoCiYeH todo!() 24152da9a59SGnoCiYeH } 24252da9a59SGnoCiYeH 24352da9a59SGnoCiYeH fn list(&self) -> Result<alloc::vec::Vec<alloc::string::String>, system_error::SystemError> { 24452da9a59SGnoCiYeH todo!() 24552da9a59SGnoCiYeH } 24652da9a59SGnoCiYeH 24752da9a59SGnoCiYeH fn metadata(&self) -> Result<crate::filesystem::vfs::Metadata, SystemError> { 24852da9a59SGnoCiYeH Ok(self.inner.read().metadata.clone()) 24952da9a59SGnoCiYeH } 25052da9a59SGnoCiYeH 25152da9a59SGnoCiYeH fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 25252da9a59SGnoCiYeH Ok(()) 25352da9a59SGnoCiYeH } 25452da9a59SGnoCiYeH 25552da9a59SGnoCiYeH fn resize(&self, _len: usize) -> Result<(), SystemError> { 25652da9a59SGnoCiYeH Ok(()) 25752da9a59SGnoCiYeH } 25852da9a59SGnoCiYeH 25952da9a59SGnoCiYeH fn ioctl(&self, cmd: u32, arg: usize, data: &FilePrivateData) -> Result<usize, SystemError> { 26052da9a59SGnoCiYeH let (tty, _) = if let FilePrivateData::Tty(tty_priv) = data { 26152da9a59SGnoCiYeH (tty_priv.tty.clone(), tty_priv.mode) 26252da9a59SGnoCiYeH } else { 26352da9a59SGnoCiYeH return Err(SystemError::EIO); 26452da9a59SGnoCiYeH }; 26552da9a59SGnoCiYeH 26652da9a59SGnoCiYeH match cmd { 26752da9a59SGnoCiYeH TtyIoctlCmd::TIOCSETD 26852da9a59SGnoCiYeH | TtyIoctlCmd::TIOCSBRK 26952da9a59SGnoCiYeH | TtyIoctlCmd::TIOCCBRK 27052da9a59SGnoCiYeH | TtyIoctlCmd::TCSBRK 27152da9a59SGnoCiYeH | TtyIoctlCmd::TCSBRKP => { 27252da9a59SGnoCiYeH TtyJobCtrlManager::tty_check_change(tty.clone(), Signal::SIGTTOU)?; 27352da9a59SGnoCiYeH if cmd != TtyIoctlCmd::TIOCCBRK { 27452da9a59SGnoCiYeH todo!() 27552da9a59SGnoCiYeH } 27652da9a59SGnoCiYeH } 277*52bcb59eSGnoCiYeH EventPoll::ADD_EPOLLITEM => { 278*52bcb59eSGnoCiYeH let _ = UserBufferReader::new( 279*52bcb59eSGnoCiYeH arg as *const Arc<EPollItem>, 280*52bcb59eSGnoCiYeH core::mem::size_of::<Arc<EPollItem>>(), 281*52bcb59eSGnoCiYeH false, 282*52bcb59eSGnoCiYeH )?; 283*52bcb59eSGnoCiYeH let epitem = unsafe { &*(arg as *const Arc<EPollItem>) }; 284*52bcb59eSGnoCiYeH 285*52bcb59eSGnoCiYeH let core = tty.core(); 286*52bcb59eSGnoCiYeH 287*52bcb59eSGnoCiYeH core.add_epitem(epitem.clone()); 288*52bcb59eSGnoCiYeH 289*52bcb59eSGnoCiYeH return Ok(0); 290*52bcb59eSGnoCiYeH } 29152da9a59SGnoCiYeH _ => {} 29252da9a59SGnoCiYeH } 29352da9a59SGnoCiYeH 29452da9a59SGnoCiYeH match cmd { 29552da9a59SGnoCiYeH TtyIoctlCmd::TIOCGWINSZ => { 29652da9a59SGnoCiYeH let core = tty.core(); 29752da9a59SGnoCiYeH let winsize = *core.window_size(); 29852da9a59SGnoCiYeH 29952da9a59SGnoCiYeH let mut user_writer = UserBufferWriter::new( 30052da9a59SGnoCiYeH VirtAddr::new(arg).as_ptr::<WindowSize>(), 30152da9a59SGnoCiYeH core::mem::size_of::<WindowSize>(), 30252da9a59SGnoCiYeH true, 30352da9a59SGnoCiYeH )?; 30452da9a59SGnoCiYeH 30552da9a59SGnoCiYeH let err = user_writer.copy_one_to_user(&winsize, 0); 30652da9a59SGnoCiYeH if err.is_err() { 30752da9a59SGnoCiYeH return Err(SystemError::EFAULT); 30852da9a59SGnoCiYeH } 30952da9a59SGnoCiYeH return Ok(0); 31052da9a59SGnoCiYeH } 31152da9a59SGnoCiYeH _ => match TtyJobCtrlManager::job_ctrl_ioctl(tty.clone(), cmd, arg) { 31252da9a59SGnoCiYeH Ok(_) => { 31352da9a59SGnoCiYeH return Ok(0); 31452da9a59SGnoCiYeH } 31552da9a59SGnoCiYeH Err(e) => { 31652da9a59SGnoCiYeH if e != SystemError::ENOIOCTLCMD { 31752da9a59SGnoCiYeH return Err(e); 31852da9a59SGnoCiYeH } 31952da9a59SGnoCiYeH } 32052da9a59SGnoCiYeH }, 32152da9a59SGnoCiYeH } 32252da9a59SGnoCiYeH 32352da9a59SGnoCiYeH match tty.ioctl(tty.clone(), cmd, arg) { 32452da9a59SGnoCiYeH Ok(_) => { 32552da9a59SGnoCiYeH return Ok(0); 32652da9a59SGnoCiYeH } 32752da9a59SGnoCiYeH Err(e) => { 32852da9a59SGnoCiYeH if e != SystemError::ENOIOCTLCMD { 32952da9a59SGnoCiYeH return Err(e); 33052da9a59SGnoCiYeH } 33152da9a59SGnoCiYeH } 33252da9a59SGnoCiYeH } 33352da9a59SGnoCiYeH tty.ldisc().ioctl(tty, cmd, arg)?; 33452da9a59SGnoCiYeH 33552da9a59SGnoCiYeH Ok(0) 33620e3152eSlogin } 337*52bcb59eSGnoCiYeH 338*52bcb59eSGnoCiYeH fn poll(&self, private_data: &FilePrivateData) -> Result<usize, SystemError> { 339*52bcb59eSGnoCiYeH let (tty, _) = if let FilePrivateData::Tty(tty_priv) = private_data { 340*52bcb59eSGnoCiYeH (tty_priv.tty.clone(), tty_priv.mode) 341*52bcb59eSGnoCiYeH } else { 342*52bcb59eSGnoCiYeH return Err(SystemError::EIO); 343*52bcb59eSGnoCiYeH }; 344*52bcb59eSGnoCiYeH 345*52bcb59eSGnoCiYeH tty.ldisc().poll(tty) 346*52bcb59eSGnoCiYeH } 3470d48c3c9Slogin } 3480d48c3c9Slogin 3490d48c3c9Slogin impl DeviceINode for TtyDevice { 3500d48c3c9Slogin fn set_fs(&self, fs: alloc::sync::Weak<crate::filesystem::devfs::DevFS>) { 3510d48c3c9Slogin *self.fs.write() = fs; 3520d48c3c9Slogin } 3530d48c3c9Slogin } 3540d48c3c9Slogin 35552da9a59SGnoCiYeH impl KObject for TtyDevice { 3560d48c3c9Slogin fn as_any_ref(&self) -> &dyn core::any::Any { 3570d48c3c9Slogin self 3580d48c3c9Slogin } 3590d48c3c9Slogin 36052da9a59SGnoCiYeH fn set_inode(&self, inode: Option<Arc<crate::filesystem::kernfs::KernFSInode>>) { 36152da9a59SGnoCiYeH self.inner.write().inode = inode; 3620d48c3c9Slogin } 36320e3152eSlogin 36452da9a59SGnoCiYeH fn inode(&self) -> Option<Arc<crate::filesystem::kernfs::KernFSInode>> { 36552da9a59SGnoCiYeH self.inner.read().inode.clone() 36620e3152eSlogin } 36720e3152eSlogin 36852da9a59SGnoCiYeH fn parent(&self) -> Option<alloc::sync::Weak<dyn KObject>> { 36952da9a59SGnoCiYeH self.inner.read().parent_kobj.clone() 37052da9a59SGnoCiYeH } 37152da9a59SGnoCiYeH 37252da9a59SGnoCiYeH fn set_parent(&self, parent: Option<alloc::sync::Weak<dyn KObject>>) { 37352da9a59SGnoCiYeH self.inner.write().parent_kobj = parent 37452da9a59SGnoCiYeH } 37552da9a59SGnoCiYeH 37652da9a59SGnoCiYeH fn kset(&self) -> Option<Arc<crate::driver::base::kset::KSet>> { 37752da9a59SGnoCiYeH self.inner.read().kset.clone() 37852da9a59SGnoCiYeH } 37952da9a59SGnoCiYeH 38052da9a59SGnoCiYeH fn set_kset(&self, kset: Option<Arc<crate::driver::base::kset::KSet>>) { 38152da9a59SGnoCiYeH self.inner.write().kset = kset 38252da9a59SGnoCiYeH } 38352da9a59SGnoCiYeH 38452da9a59SGnoCiYeH fn kobj_type(&self) -> Option<&'static dyn crate::driver::base::kobject::KObjType> { 38552da9a59SGnoCiYeH Some(&DeviceKObjType) 38652da9a59SGnoCiYeH } 38752da9a59SGnoCiYeH 38852da9a59SGnoCiYeH fn set_kobj_type(&self, _ktype: Option<&'static dyn crate::driver::base::kobject::KObjType>) {} 38952da9a59SGnoCiYeH 39052da9a59SGnoCiYeH fn name(&self) -> alloc::string::String { 39152da9a59SGnoCiYeH self.name.to_string() 39252da9a59SGnoCiYeH } 39352da9a59SGnoCiYeH 39452da9a59SGnoCiYeH fn set_name(&self, _name: alloc::string::String) { 39552da9a59SGnoCiYeH // self.name = name 39652da9a59SGnoCiYeH } 39752da9a59SGnoCiYeH 39852da9a59SGnoCiYeH fn kobj_state( 39952da9a59SGnoCiYeH &self, 40052da9a59SGnoCiYeH ) -> crate::libs::rwlock::RwLockReadGuard<crate::driver::base::kobject::KObjectState> { 40152da9a59SGnoCiYeH self.kobj_state.read() 40252da9a59SGnoCiYeH } 40352da9a59SGnoCiYeH 40452da9a59SGnoCiYeH fn kobj_state_mut( 40552da9a59SGnoCiYeH &self, 40652da9a59SGnoCiYeH ) -> crate::libs::rwlock::RwLockWriteGuard<crate::driver::base::kobject::KObjectState> { 40752da9a59SGnoCiYeH self.kobj_state.write() 40852da9a59SGnoCiYeH } 40952da9a59SGnoCiYeH 41052da9a59SGnoCiYeH fn set_kobj_state(&self, state: crate::driver::base::kobject::KObjectState) { 41152da9a59SGnoCiYeH *self.kobj_state.write() = state 41252da9a59SGnoCiYeH } 41352da9a59SGnoCiYeH } 41452da9a59SGnoCiYeH 41552da9a59SGnoCiYeH impl Device for TtyDevice { 41652da9a59SGnoCiYeH fn dev_type(&self) -> crate::driver::base::device::DeviceType { 41752da9a59SGnoCiYeH DeviceType::Char 41852da9a59SGnoCiYeH } 41952da9a59SGnoCiYeH 42052da9a59SGnoCiYeH fn id_table(&self) -> crate::driver::base::device::IdTable { 42152da9a59SGnoCiYeH self.id_table.clone() 42252da9a59SGnoCiYeH } 42352da9a59SGnoCiYeH 42452da9a59SGnoCiYeH fn set_bus(&self, bus: Option<alloc::sync::Weak<dyn crate::driver::base::device::bus::Bus>>) { 42552da9a59SGnoCiYeH self.inner.write().bus = bus 42652da9a59SGnoCiYeH } 42752da9a59SGnoCiYeH 42852da9a59SGnoCiYeH fn set_class(&self, _class: Option<Arc<dyn crate::driver::base::class::Class>>) { 42952da9a59SGnoCiYeH todo!() 43052da9a59SGnoCiYeH } 43152da9a59SGnoCiYeH 43252da9a59SGnoCiYeH fn driver(&self) -> Option<Arc<dyn crate::driver::base::device::driver::Driver>> { 43352da9a59SGnoCiYeH self.inner.read().driver.clone()?.upgrade() 43452da9a59SGnoCiYeH } 43552da9a59SGnoCiYeH 43652da9a59SGnoCiYeH fn set_driver( 43752da9a59SGnoCiYeH &self, 43852da9a59SGnoCiYeH driver: Option<alloc::sync::Weak<dyn crate::driver::base::device::driver::Driver>>, 43952da9a59SGnoCiYeH ) { 44052da9a59SGnoCiYeH self.inner.write().driver = driver 44152da9a59SGnoCiYeH } 44252da9a59SGnoCiYeH 44352da9a59SGnoCiYeH fn is_dead(&self) -> bool { 44452da9a59SGnoCiYeH false 44552da9a59SGnoCiYeH } 44652da9a59SGnoCiYeH 44752da9a59SGnoCiYeH fn can_match(&self) -> bool { 44852da9a59SGnoCiYeH self.inner.read().can_match 44952da9a59SGnoCiYeH } 45052da9a59SGnoCiYeH 45152da9a59SGnoCiYeH fn set_can_match(&self, can_match: bool) { 45252da9a59SGnoCiYeH self.inner.write().can_match = can_match 45352da9a59SGnoCiYeH } 45452da9a59SGnoCiYeH 45552da9a59SGnoCiYeH fn state_synced(&self) -> bool { 45652da9a59SGnoCiYeH true 45752da9a59SGnoCiYeH } 45852da9a59SGnoCiYeH } 45952da9a59SGnoCiYeH 46052da9a59SGnoCiYeH impl CharDevice for TtyDevice { 46152da9a59SGnoCiYeH fn read(&self, _len: usize, _buf: &mut [u8]) -> Result<usize, SystemError> { 46252da9a59SGnoCiYeH todo!() 46352da9a59SGnoCiYeH } 46452da9a59SGnoCiYeH 46552da9a59SGnoCiYeH fn write(&self, _len: usize, _buf: &[u8]) -> Result<usize, SystemError> { 46652da9a59SGnoCiYeH todo!() 46720e3152eSlogin } 46820e3152eSlogin 46920e3152eSlogin fn sync(&self) -> Result<(), SystemError> { 47052da9a59SGnoCiYeH todo!() 47111110997Shanjiezhou } 47220e3152eSlogin } 47320e3152eSlogin 47452da9a59SGnoCiYeH #[derive(Debug, Clone)] 47552da9a59SGnoCiYeH pub struct TtyFilePrivateData { 47652da9a59SGnoCiYeH tty: Arc<TtyCore>, 47752da9a59SGnoCiYeH mode: FileMode, 47820e3152eSlogin } 47920e3152eSlogin 48052da9a59SGnoCiYeH /// 初始化tty设备和console子设备 481c566df45SLoGin #[unified_init(INITCALL_DEVICE)] 48252da9a59SGnoCiYeH #[inline(never)] 48320e3152eSlogin pub fn tty_init() -> Result<(), SystemError> { 48452da9a59SGnoCiYeH let tty = TtyDevice::new( 48552da9a59SGnoCiYeH "tty0", 48652da9a59SGnoCiYeH IdTable::new( 48752da9a59SGnoCiYeH String::from("tty0"), 48852da9a59SGnoCiYeH Some(DeviceNumber::new(Major::TTY_MAJOR, 0)), 48952da9a59SGnoCiYeH ), 49052da9a59SGnoCiYeH ); 49120e3152eSlogin 49252da9a59SGnoCiYeH let console = TtyDevice::new( 49352da9a59SGnoCiYeH "console", 49452da9a59SGnoCiYeH IdTable::new( 49552da9a59SGnoCiYeH String::from("console"), 49652da9a59SGnoCiYeH Some(DeviceNumber::new(Major::TTYAUX_MAJOR, 1)), 49752da9a59SGnoCiYeH ), 49852da9a59SGnoCiYeH ); 49920e3152eSlogin 50052da9a59SGnoCiYeH // 注册tty设备 50152da9a59SGnoCiYeH // CharDevOps::cdev_add( 50252da9a59SGnoCiYeH // tty.clone() as Arc<dyn CharDevice>, 50352da9a59SGnoCiYeH // IdTable::new( 50452da9a59SGnoCiYeH // String::from("tty0"), 50552da9a59SGnoCiYeH // Some(DeviceNumber::new(Major::TTYAUX_MAJOR, 0)), 50652da9a59SGnoCiYeH // ), 50752da9a59SGnoCiYeH // 1, 50852da9a59SGnoCiYeH // )?; 50920e3152eSlogin 51052da9a59SGnoCiYeH // CharDevOps::register_chardev_region(DeviceNumber::new(Major::TTYAUX_MAJOR, 0), 1, "/dev/tty")?; 51120e3152eSlogin 51252da9a59SGnoCiYeH // 注册console设备 51352da9a59SGnoCiYeH // CharDevOps::cdev_add( 51452da9a59SGnoCiYeH // console.clone() as Arc<dyn CharDevice>, 51552da9a59SGnoCiYeH // IdTable::new( 51652da9a59SGnoCiYeH // String::from("console"), 51752da9a59SGnoCiYeH // Some(DeviceNumber::new(Major::TTYAUX_MAJOR, 1)), 51852da9a59SGnoCiYeH // ), 51952da9a59SGnoCiYeH // 1, 52052da9a59SGnoCiYeH // )?; 52120e3152eSlogin 52252da9a59SGnoCiYeH // CharDevOps::register_chardev_region(DeviceNumber::new(Major::TTYAUX_MAJOR, 1), 1, "/dev/tty")?; 52352da9a59SGnoCiYeH 52452da9a59SGnoCiYeH // 将这两个设备注册到devfs,TODO:这里console设备应该与tty在一个设备group里面 52552da9a59SGnoCiYeH device_register(tty.clone())?; 52652da9a59SGnoCiYeH device_register(console.clone())?; 52752da9a59SGnoCiYeH devfs_register(tty.name, tty)?; 52852da9a59SGnoCiYeH devfs_register(console.name, console)?; 52920e3152eSlogin 530a03c4f9dSLoGin serial_init()?; 53152da9a59SGnoCiYeH return vty_init(); 5320d48c3c9Slogin } 533