164aea4b3SGou Ngai use core::sync::atomic::AtomicI32; 264aea4b3SGou Ngai 3004e86ffSlogin use alloc::sync::{Arc, Weak}; 4004e86ffSlogin 5004e86ffSlogin use crate::{ 620e3152eSlogin driver::tty::tty_device::TTY_DEVICES, 7004e86ffSlogin filesystem::{ 8004e86ffSlogin devfs::{devfs_register, DevFS, DeviceINode}, 9*6b4e7a29SLoGin vfs::{ 10*6b4e7a29SLoGin core::generate_inode_id, file::FileMode, syscall::ModeType, FileType, IndexNode, 11*6b4e7a29SLoGin Metadata, PollStatus, 12*6b4e7a29SLoGin }, 13004e86ffSlogin }, 14676b8ef6SMork include::bindings::bindings::{vfs_file_operations_t, vfs_file_t, vfs_index_node_t}, 155fb12ce4SGou Ngai libs::{keyboard_parser::TypeOneFSM, rwlock::RwLock, spinlock::SpinLock}, 165fb12ce4SGou Ngai syscall::SystemError, 175fb12ce4SGou Ngai time::TimeSpec, 18004e86ffSlogin }; 19004e86ffSlogin 20004e86ffSlogin #[derive(Debug)] 2164aea4b3SGou Ngai pub struct LockedPS2KeyBoardInode(RwLock<PS2KeyBoardInode>, AtomicI32); // self.1 用来记录有多少个文件打开了这个inode 22004e86ffSlogin 235fb12ce4SGou Ngai lazy_static! { 2420e3152eSlogin static ref PS2_KEYBOARD_FSM: SpinLock<TypeOneFSM> = { 2520e3152eSlogin let tty0 = TTY_DEVICES 2620e3152eSlogin .read() 2720e3152eSlogin .get("tty0") 2820e3152eSlogin .expect("Initializing PS2_KEYBOARD_FSM: Cannot found TTY0!") 2920e3152eSlogin .clone(); 3020e3152eSlogin SpinLock::new(TypeOneFSM::new(tty0)) 3120e3152eSlogin }; 325fb12ce4SGou Ngai } 335fb12ce4SGou Ngai 34004e86ffSlogin #[derive(Debug)] 35004e86ffSlogin pub struct PS2KeyBoardInode { 36004e86ffSlogin /// uuid 暂时不知道有什么用(x 37004e86ffSlogin // uuid: Uuid, 38004e86ffSlogin /// 指向自身的弱引用 39004e86ffSlogin self_ref: Weak<LockedPS2KeyBoardInode>, 40004e86ffSlogin /// 指向inode所在的文件系统对象的指针 41004e86ffSlogin fs: Weak<DevFS>, 42004e86ffSlogin /// INode 元数据 43004e86ffSlogin metadata: Metadata, 44004e86ffSlogin /// 键盘操作函数 45004e86ffSlogin f_ops: vfs_file_operations_t, 46004e86ffSlogin } 47004e86ffSlogin 48004e86ffSlogin impl LockedPS2KeyBoardInode { 49004e86ffSlogin pub fn new(f_ops: &vfs_file_operations_t) -> Arc<Self> { 50004e86ffSlogin let inode = PS2KeyBoardInode { 51004e86ffSlogin // uuid: Uuid::new_v5(), 52004e86ffSlogin self_ref: Weak::default(), 53004e86ffSlogin fs: Weak::default(), 54004e86ffSlogin f_ops: f_ops.clone(), // 从引用复制一遍获取所有权 55004e86ffSlogin metadata: Metadata { 56004e86ffSlogin dev_id: 1, 57004e86ffSlogin inode_id: generate_inode_id(), 58004e86ffSlogin size: 0, 59004e86ffSlogin blk_size: 0, 60004e86ffSlogin blocks: 0, 61004e86ffSlogin atime: TimeSpec::default(), 62004e86ffSlogin mtime: TimeSpec::default(), 63004e86ffSlogin ctime: TimeSpec::default(), 64004e86ffSlogin file_type: FileType::CharDevice, // 文件夹,block设备,char设备 65*6b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o666), 66004e86ffSlogin nlinks: 1, 67004e86ffSlogin uid: 0, 68004e86ffSlogin gid: 0, 69004e86ffSlogin raw_dev: 0, // 这里用来作为device number 70004e86ffSlogin }, 71004e86ffSlogin }; 72004e86ffSlogin 7364aea4b3SGou Ngai let result = Arc::new(LockedPS2KeyBoardInode( 7464aea4b3SGou Ngai RwLock::new(inode), 7564aea4b3SGou Ngai AtomicI32::new(0), 7664aea4b3SGou Ngai )); 7764aea4b3SGou Ngai result.0.write().self_ref = Arc::downgrade(&result); 78004e86ffSlogin 79004e86ffSlogin return result; 80004e86ffSlogin } 81004e86ffSlogin } 82004e86ffSlogin 83004e86ffSlogin impl DeviceINode for LockedPS2KeyBoardInode { 84004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 8564aea4b3SGou Ngai self.0.write().fs = fs; 86004e86ffSlogin } 87004e86ffSlogin } 88004e86ffSlogin 89004e86ffSlogin #[no_mangle] // 不重命名 90004e86ffSlogin pub extern "C" fn ps2_keyboard_register(f_ops: &vfs_file_operations_t) { 91004e86ffSlogin devfs_register("ps2_keyboard", LockedPS2KeyBoardInode::new(f_ops)) 92004e86ffSlogin .expect("Failed to register ps/2 keyboard"); 93004e86ffSlogin } 94004e86ffSlogin 95004e86ffSlogin impl IndexNode for LockedPS2KeyBoardInode { 96004e86ffSlogin fn read_at( 97004e86ffSlogin &self, 98004e86ffSlogin _offset: usize, 99004e86ffSlogin len: usize, 100004e86ffSlogin buf: &mut [u8], 101004e86ffSlogin _data: &mut crate::filesystem::vfs::FilePrivateData, 102676b8ef6SMork ) -> Result<usize, SystemError> { 10364aea4b3SGou Ngai let guard = self.0.read(); 104004e86ffSlogin let func = guard.f_ops.read.unwrap(); 105004e86ffSlogin let r = unsafe { 106004e86ffSlogin func( 107004e86ffSlogin 0 as *mut vfs_file_t, 108004e86ffSlogin &mut buf[0..len] as *mut [u8] as *mut i8, 109004e86ffSlogin len as i64, 110004e86ffSlogin 0 as *mut i64, 111004e86ffSlogin ) 112004e86ffSlogin }; 113004e86ffSlogin return Ok(r as usize); 114004e86ffSlogin } 115004e86ffSlogin 116004e86ffSlogin fn write_at( 117004e86ffSlogin &self, 118004e86ffSlogin _offset: usize, 119004e86ffSlogin _len: usize, 120004e86ffSlogin _buf: &[u8], 121004e86ffSlogin _data: &mut crate::filesystem::vfs::FilePrivateData, 122676b8ef6SMork ) -> Result<usize, SystemError> { 12379a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 124004e86ffSlogin } 125004e86ffSlogin 1260d48c3c9Slogin fn open( 1270d48c3c9Slogin &self, 1280d48c3c9Slogin _data: &mut crate::filesystem::vfs::FilePrivateData, 1290d48c3c9Slogin _mode: &FileMode, 130676b8ef6SMork ) -> Result<(), SystemError> { 13164aea4b3SGou Ngai let prev_ref_count = self.1.fetch_add(1, core::sync::atomic::Ordering::SeqCst); 13264aea4b3SGou Ngai if prev_ref_count == 0 { 13364aea4b3SGou Ngai // 第一次打开,需要初始化 13464aea4b3SGou Ngai let guard = self.0.write(); 135004e86ffSlogin let func = guard.f_ops.open.unwrap(); 136004e86ffSlogin let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) }; 13764aea4b3SGou Ngai } 138004e86ffSlogin return Ok(()); 139004e86ffSlogin } 140004e86ffSlogin 1415fb12ce4SGou Ngai fn close( 1425fb12ce4SGou Ngai &self, 1435fb12ce4SGou Ngai _data: &mut crate::filesystem::vfs::FilePrivateData, 1445fb12ce4SGou Ngai ) -> Result<(), SystemError> { 14564aea4b3SGou Ngai let prev_ref_count = self.1.fetch_sub(1, core::sync::atomic::Ordering::SeqCst); 14664aea4b3SGou Ngai if prev_ref_count == 1 { 14764aea4b3SGou Ngai // 最后一次关闭,需要释放 14864aea4b3SGou Ngai let guard = self.0.write(); 149004e86ffSlogin let func = guard.f_ops.close.unwrap(); 150004e86ffSlogin let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) }; 15164aea4b3SGou Ngai } 152004e86ffSlogin return Ok(()); 153004e86ffSlogin } 154004e86ffSlogin 155676b8ef6SMork fn poll(&self) -> Result<PollStatus, SystemError> { 156cde5492fSlogin return Ok(PollStatus::READ); 157004e86ffSlogin } 158004e86ffSlogin 159676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 16064aea4b3SGou Ngai return Ok(self.0.read().metadata.clone()); 161004e86ffSlogin } 162004e86ffSlogin 163676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 16464aea4b3SGou Ngai let mut inode = self.0.write(); 165004e86ffSlogin inode.metadata.atime = metadata.atime; 166004e86ffSlogin inode.metadata.mtime = metadata.mtime; 167004e86ffSlogin inode.metadata.ctime = metadata.ctime; 168004e86ffSlogin inode.metadata.mode = metadata.mode; 169004e86ffSlogin inode.metadata.uid = metadata.uid; 170004e86ffSlogin inode.metadata.gid = metadata.gid; 171004e86ffSlogin 172004e86ffSlogin return Ok(()); 173004e86ffSlogin } 174004e86ffSlogin 175004e86ffSlogin fn fs(&self) -> alloc::sync::Arc<dyn crate::filesystem::vfs::FileSystem> { 17664aea4b3SGou Ngai return self.0.read().fs.upgrade().unwrap(); 177004e86ffSlogin } 178004e86ffSlogin 179004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 180004e86ffSlogin self 181004e86ffSlogin } 182004e86ffSlogin 183676b8ef6SMork fn list(&self) -> Result<alloc::vec::Vec<alloc::string::String>, SystemError> { 18479a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 185004e86ffSlogin } 186004e86ffSlogin } 1875fb12ce4SGou Ngai 1885fb12ce4SGou Ngai #[allow(dead_code)] 1895fb12ce4SGou Ngai #[no_mangle] 1905fb12ce4SGou Ngai /// for test 1915fb12ce4SGou Ngai pub extern "C" fn ps2_keyboard_parse_keycode(input: u8) { 1925fb12ce4SGou Ngai PS2_KEYBOARD_FSM.lock().parse(input); 1935fb12ce4SGou Ngai } 194