146e234aeSLoGin use core::{ffi::c_void, sync::atomic::AtomicI32}; 264aea4b3SGou Ngai 3004e86ffSlogin use alloc::sync::{Arc, Weak}; 4004e86ffSlogin 5004e86ffSlogin use crate::{ 6*02343d0bSLoGin driver::{ 7*02343d0bSLoGin base::device::device_number::{DeviceNumber, Major}, 8*02343d0bSLoGin tty::tty_device::TTY_DEVICES, 9*02343d0bSLoGin }, 10004e86ffSlogin filesystem::{ 11004e86ffSlogin devfs::{devfs_register, DevFS, DeviceINode}, 126b4e7a29SLoGin vfs::{ 136b4e7a29SLoGin core::generate_inode_id, file::FileMode, syscall::ModeType, FileType, IndexNode, 1440609970SGnoCiYeH Metadata, 156b4e7a29SLoGin }, 16004e86ffSlogin }, 1746e234aeSLoGin include::bindings::bindings::vfs_file_operations_t, 185fb12ce4SGou Ngai libs::{keyboard_parser::TypeOneFSM, rwlock::RwLock, spinlock::SpinLock}, 195fb12ce4SGou Ngai time::TimeSpec, 20004e86ffSlogin }; 2191e9d4abSLoGin use system_error::SystemError; 22004e86ffSlogin #[derive(Debug)] 2364aea4b3SGou Ngai pub struct LockedPS2KeyBoardInode(RwLock<PS2KeyBoardInode>, AtomicI32); // self.1 用来记录有多少个文件打开了这个inode 24004e86ffSlogin 255fb12ce4SGou Ngai lazy_static! { 2620e3152eSlogin static ref PS2_KEYBOARD_FSM: SpinLock<TypeOneFSM> = { 2720e3152eSlogin let tty0 = TTY_DEVICES 2820e3152eSlogin .read() 2920e3152eSlogin .get("tty0") 3020e3152eSlogin .expect("Initializing PS2_KEYBOARD_FSM: Cannot found TTY0!") 3120e3152eSlogin .clone(); 3220e3152eSlogin SpinLock::new(TypeOneFSM::new(tty0)) 3320e3152eSlogin }; 345fb12ce4SGou Ngai } 355fb12ce4SGou Ngai 36004e86ffSlogin #[derive(Debug)] 37004e86ffSlogin pub struct PS2KeyBoardInode { 38004e86ffSlogin /// uuid 暂时不知道有什么用(x 39004e86ffSlogin // uuid: Uuid, 40004e86ffSlogin /// 指向自身的弱引用 41004e86ffSlogin self_ref: Weak<LockedPS2KeyBoardInode>, 42004e86ffSlogin /// 指向inode所在的文件系统对象的指针 43004e86ffSlogin fs: Weak<DevFS>, 44004e86ffSlogin /// INode 元数据 45004e86ffSlogin metadata: Metadata, 46004e86ffSlogin /// 键盘操作函数 47004e86ffSlogin f_ops: vfs_file_operations_t, 48004e86ffSlogin } 49004e86ffSlogin 50004e86ffSlogin impl LockedPS2KeyBoardInode { 51004e86ffSlogin pub fn new(f_ops: &vfs_file_operations_t) -> Arc<Self> { 52004e86ffSlogin let inode = PS2KeyBoardInode { 53004e86ffSlogin // uuid: Uuid::new_v5(), 54004e86ffSlogin self_ref: Weak::default(), 55004e86ffSlogin fs: Weak::default(), 56004e86ffSlogin f_ops: f_ops.clone(), // 从引用复制一遍获取所有权 57004e86ffSlogin metadata: Metadata { 58004e86ffSlogin dev_id: 1, 59004e86ffSlogin inode_id: generate_inode_id(), 60004e86ffSlogin size: 0, 61004e86ffSlogin blk_size: 0, 62004e86ffSlogin blocks: 0, 63004e86ffSlogin atime: TimeSpec::default(), 64004e86ffSlogin mtime: TimeSpec::default(), 65004e86ffSlogin ctime: TimeSpec::default(), 66004e86ffSlogin file_type: FileType::CharDevice, // 文件夹,block设备,char设备 676b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o666), 68004e86ffSlogin nlinks: 1, 69004e86ffSlogin uid: 0, 70004e86ffSlogin gid: 0, 71*02343d0bSLoGin raw_dev: DeviceNumber::new(Major::INPUT_MAJOR, 0), // 这里用来作为device number 72004e86ffSlogin }, 73004e86ffSlogin }; 74004e86ffSlogin 7564aea4b3SGou Ngai let result = Arc::new(LockedPS2KeyBoardInode( 7664aea4b3SGou Ngai RwLock::new(inode), 7764aea4b3SGou Ngai AtomicI32::new(0), 7864aea4b3SGou Ngai )); 7964aea4b3SGou Ngai result.0.write().self_ref = Arc::downgrade(&result); 80004e86ffSlogin 81004e86ffSlogin return result; 82004e86ffSlogin } 83004e86ffSlogin } 84004e86ffSlogin 85004e86ffSlogin impl DeviceINode for LockedPS2KeyBoardInode { 86004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 8764aea4b3SGou Ngai self.0.write().fs = fs; 88004e86ffSlogin } 89004e86ffSlogin } 90004e86ffSlogin 91004e86ffSlogin #[no_mangle] // 不重命名 92004e86ffSlogin pub extern "C" fn ps2_keyboard_register(f_ops: &vfs_file_operations_t) { 93004e86ffSlogin devfs_register("ps2_keyboard", LockedPS2KeyBoardInode::new(f_ops)) 94004e86ffSlogin .expect("Failed to register ps/2 keyboard"); 95004e86ffSlogin } 96004e86ffSlogin 97004e86ffSlogin impl IndexNode for LockedPS2KeyBoardInode { 98004e86ffSlogin fn read_at( 99004e86ffSlogin &self, 100004e86ffSlogin _offset: usize, 101004e86ffSlogin len: usize, 102004e86ffSlogin buf: &mut [u8], 103004e86ffSlogin _data: &mut crate::filesystem::vfs::FilePrivateData, 104676b8ef6SMork ) -> Result<usize, SystemError> { 10564aea4b3SGou Ngai let guard = self.0.read(); 106004e86ffSlogin let func = guard.f_ops.read.unwrap(); 107004e86ffSlogin let r = unsafe { 108004e86ffSlogin func( 10946e234aeSLoGin 0 as *mut c_void, 110004e86ffSlogin &mut buf[0..len] as *mut [u8] as *mut i8, 111004e86ffSlogin len as i64, 112004e86ffSlogin 0 as *mut i64, 113004e86ffSlogin ) 114004e86ffSlogin }; 115004e86ffSlogin return Ok(r as usize); 116004e86ffSlogin } 117004e86ffSlogin 118004e86ffSlogin fn write_at( 119004e86ffSlogin &self, 120004e86ffSlogin _offset: usize, 121004e86ffSlogin _len: usize, 122004e86ffSlogin _buf: &[u8], 123004e86ffSlogin _data: &mut crate::filesystem::vfs::FilePrivateData, 124676b8ef6SMork ) -> Result<usize, SystemError> { 12579a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 126004e86ffSlogin } 127004e86ffSlogin 1280d48c3c9Slogin fn open( 1290d48c3c9Slogin &self, 1300d48c3c9Slogin _data: &mut crate::filesystem::vfs::FilePrivateData, 1310d48c3c9Slogin _mode: &FileMode, 132676b8ef6SMork ) -> Result<(), SystemError> { 13364aea4b3SGou Ngai let prev_ref_count = self.1.fetch_add(1, core::sync::atomic::Ordering::SeqCst); 13464aea4b3SGou Ngai if prev_ref_count == 0 { 13564aea4b3SGou Ngai // 第一次打开,需要初始化 13664aea4b3SGou Ngai let guard = self.0.write(); 137004e86ffSlogin let func = guard.f_ops.open.unwrap(); 13846e234aeSLoGin let _ = unsafe { func(0 as *mut c_void, 0 as *mut c_void) }; 13964aea4b3SGou Ngai } 140004e86ffSlogin return Ok(()); 141004e86ffSlogin } 142004e86ffSlogin 1435fb12ce4SGou Ngai fn close( 1445fb12ce4SGou Ngai &self, 1455fb12ce4SGou Ngai _data: &mut crate::filesystem::vfs::FilePrivateData, 1465fb12ce4SGou Ngai ) -> Result<(), SystemError> { 14764aea4b3SGou Ngai let prev_ref_count = self.1.fetch_sub(1, core::sync::atomic::Ordering::SeqCst); 14864aea4b3SGou Ngai if prev_ref_count == 1 { 14964aea4b3SGou Ngai // 最后一次关闭,需要释放 15064aea4b3SGou Ngai let guard = self.0.write(); 151004e86ffSlogin let func = guard.f_ops.close.unwrap(); 15246e234aeSLoGin let _ = unsafe { func(0 as *mut c_void, 0 as *mut c_void) }; 15364aea4b3SGou Ngai } 154004e86ffSlogin return Ok(()); 155004e86ffSlogin } 156004e86ffSlogin 157676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 15864aea4b3SGou Ngai return Ok(self.0.read().metadata.clone()); 159004e86ffSlogin } 160004e86ffSlogin 161676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 16264aea4b3SGou Ngai let mut inode = self.0.write(); 163004e86ffSlogin inode.metadata.atime = metadata.atime; 164004e86ffSlogin inode.metadata.mtime = metadata.mtime; 165004e86ffSlogin inode.metadata.ctime = metadata.ctime; 166004e86ffSlogin inode.metadata.mode = metadata.mode; 167004e86ffSlogin inode.metadata.uid = metadata.uid; 168004e86ffSlogin inode.metadata.gid = metadata.gid; 169004e86ffSlogin 170004e86ffSlogin return Ok(()); 171004e86ffSlogin } 172004e86ffSlogin 173004e86ffSlogin fn fs(&self) -> alloc::sync::Arc<dyn crate::filesystem::vfs::FileSystem> { 17464aea4b3SGou Ngai return self.0.read().fs.upgrade().unwrap(); 175004e86ffSlogin } 176004e86ffSlogin 177004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 178004e86ffSlogin self 179004e86ffSlogin } 180004e86ffSlogin 181676b8ef6SMork fn list(&self) -> Result<alloc::vec::Vec<alloc::string::String>, SystemError> { 18279a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 183004e86ffSlogin } 184004e86ffSlogin } 1855fb12ce4SGou Ngai 1865fb12ce4SGou Ngai #[allow(dead_code)] 1875fb12ce4SGou Ngai #[no_mangle] 1885fb12ce4SGou Ngai /// for test 1895fb12ce4SGou Ngai pub extern "C" fn ps2_keyboard_parse_keycode(input: u8) { 1905fb12ce4SGou Ngai PS2_KEYBOARD_FSM.lock().parse(input); 1915fb12ce4SGou Ngai } 192