164aea4b3SGou Ngai use core::sync::atomic::AtomicI32; 264aea4b3SGou Ngai 3004e86ffSlogin use alloc::sync::{Arc, Weak}; 4004e86ffSlogin 5004e86ffSlogin use crate::{ 6004e86ffSlogin filesystem::{ 7004e86ffSlogin devfs::{devfs_register, DevFS, DeviceINode}, 80d48c3c9Slogin vfs::{core::generate_inode_id, file::FileMode, FileType, IndexNode, Metadata, PollStatus}, 9004e86ffSlogin }, 10676b8ef6SMork include::bindings::bindings::{vfs_file_operations_t, vfs_file_t, vfs_index_node_t}, 11*5fb12ce4SGou Ngai libs::{keyboard_parser::TypeOneFSM, rwlock::RwLock, spinlock::SpinLock}, 12*5fb12ce4SGou Ngai syscall::SystemError, 13*5fb12ce4SGou Ngai time::TimeSpec, 14004e86ffSlogin }; 15004e86ffSlogin 16004e86ffSlogin #[derive(Debug)] 1764aea4b3SGou Ngai pub struct LockedPS2KeyBoardInode(RwLock<PS2KeyBoardInode>, AtomicI32); // self.1 用来记录有多少个文件打开了这个inode 18004e86ffSlogin 19*5fb12ce4SGou Ngai lazy_static! { 20*5fb12ce4SGou Ngai static ref PS2_KEYBOARD_FSM: SpinLock<TypeOneFSM> = SpinLock::new(TypeOneFSM::new()); 21*5fb12ce4SGou Ngai } 22*5fb12ce4SGou Ngai 23004e86ffSlogin #[derive(Debug)] 24004e86ffSlogin pub struct PS2KeyBoardInode { 25004e86ffSlogin /// uuid 暂时不知道有什么用(x 26004e86ffSlogin // uuid: Uuid, 27004e86ffSlogin /// 指向自身的弱引用 28004e86ffSlogin self_ref: Weak<LockedPS2KeyBoardInode>, 29004e86ffSlogin /// 指向inode所在的文件系统对象的指针 30004e86ffSlogin fs: Weak<DevFS>, 31004e86ffSlogin /// INode 元数据 32004e86ffSlogin metadata: Metadata, 33004e86ffSlogin /// 键盘操作函数 34004e86ffSlogin f_ops: vfs_file_operations_t, 35004e86ffSlogin } 36004e86ffSlogin 37004e86ffSlogin impl LockedPS2KeyBoardInode { 38004e86ffSlogin pub fn new(f_ops: &vfs_file_operations_t) -> Arc<Self> { 39004e86ffSlogin let inode = PS2KeyBoardInode { 40004e86ffSlogin // uuid: Uuid::new_v5(), 41004e86ffSlogin self_ref: Weak::default(), 42004e86ffSlogin fs: Weak::default(), 43004e86ffSlogin f_ops: f_ops.clone(), // 从引用复制一遍获取所有权 44004e86ffSlogin metadata: Metadata { 45004e86ffSlogin dev_id: 1, 46004e86ffSlogin inode_id: generate_inode_id(), 47004e86ffSlogin size: 0, 48004e86ffSlogin blk_size: 0, 49004e86ffSlogin blocks: 0, 50004e86ffSlogin atime: TimeSpec::default(), 51004e86ffSlogin mtime: TimeSpec::default(), 52004e86ffSlogin ctime: TimeSpec::default(), 53004e86ffSlogin file_type: FileType::CharDevice, // 文件夹,block设备,char设备 54004e86ffSlogin mode: 0o666, 55004e86ffSlogin nlinks: 1, 56004e86ffSlogin uid: 0, 57004e86ffSlogin gid: 0, 58004e86ffSlogin raw_dev: 0, // 这里用来作为device number 59004e86ffSlogin }, 60004e86ffSlogin }; 61004e86ffSlogin 6264aea4b3SGou Ngai let result = Arc::new(LockedPS2KeyBoardInode( 6364aea4b3SGou Ngai RwLock::new(inode), 6464aea4b3SGou Ngai AtomicI32::new(0), 6564aea4b3SGou Ngai )); 6664aea4b3SGou Ngai result.0.write().self_ref = Arc::downgrade(&result); 67004e86ffSlogin 68004e86ffSlogin return result; 69004e86ffSlogin } 70004e86ffSlogin } 71004e86ffSlogin 72004e86ffSlogin impl DeviceINode for LockedPS2KeyBoardInode { 73004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 7464aea4b3SGou Ngai self.0.write().fs = fs; 75004e86ffSlogin } 76004e86ffSlogin } 77004e86ffSlogin 78004e86ffSlogin #[no_mangle] // 不重命名 79004e86ffSlogin pub extern "C" fn ps2_keyboard_register(f_ops: &vfs_file_operations_t) { 80004e86ffSlogin devfs_register("ps2_keyboard", LockedPS2KeyBoardInode::new(f_ops)) 81004e86ffSlogin .expect("Failed to register ps/2 keyboard"); 82004e86ffSlogin } 83004e86ffSlogin 84004e86ffSlogin impl IndexNode for LockedPS2KeyBoardInode { 85004e86ffSlogin fn read_at( 86004e86ffSlogin &self, 87004e86ffSlogin _offset: usize, 88004e86ffSlogin len: usize, 89004e86ffSlogin buf: &mut [u8], 90004e86ffSlogin _data: &mut crate::filesystem::vfs::FilePrivateData, 91676b8ef6SMork ) -> Result<usize, SystemError> { 9264aea4b3SGou Ngai let guard = self.0.read(); 93004e86ffSlogin let func = guard.f_ops.read.unwrap(); 94004e86ffSlogin let r = unsafe { 95004e86ffSlogin func( 96004e86ffSlogin 0 as *mut vfs_file_t, 97004e86ffSlogin &mut buf[0..len] as *mut [u8] as *mut i8, 98004e86ffSlogin len as i64, 99004e86ffSlogin 0 as *mut i64, 100004e86ffSlogin ) 101004e86ffSlogin }; 102004e86ffSlogin return Ok(r as usize); 103004e86ffSlogin } 104004e86ffSlogin 105004e86ffSlogin fn write_at( 106004e86ffSlogin &self, 107004e86ffSlogin _offset: usize, 108004e86ffSlogin _len: usize, 109004e86ffSlogin _buf: &[u8], 110004e86ffSlogin _data: &mut crate::filesystem::vfs::FilePrivateData, 111676b8ef6SMork ) -> Result<usize, SystemError> { 112676b8ef6SMork return Err(SystemError::ENOTSUP); 113004e86ffSlogin } 114004e86ffSlogin 1150d48c3c9Slogin fn open( 1160d48c3c9Slogin &self, 1170d48c3c9Slogin _data: &mut crate::filesystem::vfs::FilePrivateData, 1180d48c3c9Slogin _mode: &FileMode, 119676b8ef6SMork ) -> Result<(), SystemError> { 12064aea4b3SGou Ngai let prev_ref_count = self.1.fetch_add(1, core::sync::atomic::Ordering::SeqCst); 12164aea4b3SGou Ngai if prev_ref_count == 0 { 12264aea4b3SGou Ngai // 第一次打开,需要初始化 12364aea4b3SGou Ngai let guard = self.0.write(); 124004e86ffSlogin let func = guard.f_ops.open.unwrap(); 125004e86ffSlogin let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) }; 12664aea4b3SGou Ngai } 127004e86ffSlogin return Ok(()); 128004e86ffSlogin } 129004e86ffSlogin 130*5fb12ce4SGou Ngai fn close( 131*5fb12ce4SGou Ngai &self, 132*5fb12ce4SGou Ngai _data: &mut crate::filesystem::vfs::FilePrivateData, 133*5fb12ce4SGou Ngai ) -> Result<(), SystemError> { 13464aea4b3SGou Ngai let prev_ref_count = self.1.fetch_sub(1, core::sync::atomic::Ordering::SeqCst); 13564aea4b3SGou Ngai if prev_ref_count == 1 { 13664aea4b3SGou Ngai // 最后一次关闭,需要释放 13764aea4b3SGou Ngai let guard = self.0.write(); 138004e86ffSlogin let func = guard.f_ops.close.unwrap(); 139004e86ffSlogin let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) }; 14064aea4b3SGou Ngai } 141004e86ffSlogin return Ok(()); 142004e86ffSlogin } 143004e86ffSlogin 144676b8ef6SMork fn poll(&self) -> Result<PollStatus, SystemError> { 145004e86ffSlogin return Ok(PollStatus { 146004e86ffSlogin flags: PollStatus::READ_MASK, 147004e86ffSlogin }); 148004e86ffSlogin } 149004e86ffSlogin 150676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 15164aea4b3SGou Ngai return Ok(self.0.read().metadata.clone()); 152004e86ffSlogin } 153004e86ffSlogin 154676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 15564aea4b3SGou Ngai let mut inode = self.0.write(); 156004e86ffSlogin inode.metadata.atime = metadata.atime; 157004e86ffSlogin inode.metadata.mtime = metadata.mtime; 158004e86ffSlogin inode.metadata.ctime = metadata.ctime; 159004e86ffSlogin inode.metadata.mode = metadata.mode; 160004e86ffSlogin inode.metadata.uid = metadata.uid; 161004e86ffSlogin inode.metadata.gid = metadata.gid; 162004e86ffSlogin 163004e86ffSlogin return Ok(()); 164004e86ffSlogin } 165004e86ffSlogin 166004e86ffSlogin fn fs(&self) -> alloc::sync::Arc<dyn crate::filesystem::vfs::FileSystem> { 16764aea4b3SGou Ngai return self.0.read().fs.upgrade().unwrap(); 168004e86ffSlogin } 169004e86ffSlogin 170004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 171004e86ffSlogin self 172004e86ffSlogin } 173004e86ffSlogin 174676b8ef6SMork fn list(&self) -> Result<alloc::vec::Vec<alloc::string::String>, SystemError> { 175676b8ef6SMork return Err(SystemError::ENOTSUP); 176004e86ffSlogin } 177004e86ffSlogin } 178*5fb12ce4SGou Ngai 179*5fb12ce4SGou Ngai #[allow(dead_code)] 180*5fb12ce4SGou Ngai #[no_mangle] 181*5fb12ce4SGou Ngai /// for test 182*5fb12ce4SGou Ngai pub extern "C" fn ps2_keyboard_parse_keycode(input: u8) { 183*5fb12ce4SGou Ngai PS2_KEYBOARD_FSM.lock().parse(input); 184*5fb12ce4SGou Ngai } 185