1 use core::sync::atomic::AtomicI32; 2 3 use alloc::sync::{Arc, Weak}; 4 5 use crate::{ 6 driver::tty::tty_device::TTY_DEVICES, 7 filesystem::{ 8 devfs::{devfs_register, DevFS, DeviceINode}, 9 vfs::{core::generate_inode_id, file::FileMode, FileType, IndexNode, Metadata, PollStatus}, 10 }, 11 include::bindings::bindings::{vfs_file_operations_t, vfs_file_t, vfs_index_node_t}, 12 libs::{keyboard_parser::TypeOneFSM, rwlock::RwLock, spinlock::SpinLock}, 13 syscall::SystemError, 14 time::TimeSpec, 15 }; 16 17 #[derive(Debug)] 18 pub struct LockedPS2KeyBoardInode(RwLock<PS2KeyBoardInode>, AtomicI32); // self.1 用来记录有多少个文件打开了这个inode 19 20 lazy_static! { 21 static ref PS2_KEYBOARD_FSM: SpinLock<TypeOneFSM> = { 22 let tty0 = TTY_DEVICES 23 .read() 24 .get("tty0") 25 .expect("Initializing PS2_KEYBOARD_FSM: Cannot found TTY0!") 26 .clone(); 27 SpinLock::new(TypeOneFSM::new(tty0)) 28 }; 29 } 30 31 #[derive(Debug)] 32 pub struct PS2KeyBoardInode { 33 /// uuid 暂时不知道有什么用(x 34 // uuid: Uuid, 35 /// 指向自身的弱引用 36 self_ref: Weak<LockedPS2KeyBoardInode>, 37 /// 指向inode所在的文件系统对象的指针 38 fs: Weak<DevFS>, 39 /// INode 元数据 40 metadata: Metadata, 41 /// 键盘操作函数 42 f_ops: vfs_file_operations_t, 43 } 44 45 impl LockedPS2KeyBoardInode { 46 pub fn new(f_ops: &vfs_file_operations_t) -> Arc<Self> { 47 let inode = PS2KeyBoardInode { 48 // uuid: Uuid::new_v5(), 49 self_ref: Weak::default(), 50 fs: Weak::default(), 51 f_ops: f_ops.clone(), // 从引用复制一遍获取所有权 52 metadata: Metadata { 53 dev_id: 1, 54 inode_id: generate_inode_id(), 55 size: 0, 56 blk_size: 0, 57 blocks: 0, 58 atime: TimeSpec::default(), 59 mtime: TimeSpec::default(), 60 ctime: TimeSpec::default(), 61 file_type: FileType::CharDevice, // 文件夹,block设备,char设备 62 mode: 0o666, 63 nlinks: 1, 64 uid: 0, 65 gid: 0, 66 raw_dev: 0, // 这里用来作为device number 67 }, 68 }; 69 70 let result = Arc::new(LockedPS2KeyBoardInode( 71 RwLock::new(inode), 72 AtomicI32::new(0), 73 )); 74 result.0.write().self_ref = Arc::downgrade(&result); 75 76 return result; 77 } 78 } 79 80 impl DeviceINode for LockedPS2KeyBoardInode { 81 fn set_fs(&self, fs: Weak<DevFS>) { 82 self.0.write().fs = fs; 83 } 84 } 85 86 #[no_mangle] // 不重命名 87 pub extern "C" fn ps2_keyboard_register(f_ops: &vfs_file_operations_t) { 88 devfs_register("ps2_keyboard", LockedPS2KeyBoardInode::new(f_ops)) 89 .expect("Failed to register ps/2 keyboard"); 90 } 91 92 impl IndexNode for LockedPS2KeyBoardInode { 93 fn read_at( 94 &self, 95 _offset: usize, 96 len: usize, 97 buf: &mut [u8], 98 _data: &mut crate::filesystem::vfs::FilePrivateData, 99 ) -> Result<usize, SystemError> { 100 let guard = self.0.read(); 101 let func = guard.f_ops.read.unwrap(); 102 let r = unsafe { 103 func( 104 0 as *mut vfs_file_t, 105 &mut buf[0..len] as *mut [u8] as *mut i8, 106 len as i64, 107 0 as *mut i64, 108 ) 109 }; 110 return Ok(r as usize); 111 } 112 113 fn write_at( 114 &self, 115 _offset: usize, 116 _len: usize, 117 _buf: &[u8], 118 _data: &mut crate::filesystem::vfs::FilePrivateData, 119 ) -> Result<usize, SystemError> { 120 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 121 } 122 123 fn open( 124 &self, 125 _data: &mut crate::filesystem::vfs::FilePrivateData, 126 _mode: &FileMode, 127 ) -> Result<(), SystemError> { 128 let prev_ref_count = self.1.fetch_add(1, core::sync::atomic::Ordering::SeqCst); 129 if prev_ref_count == 0 { 130 // 第一次打开,需要初始化 131 let guard = self.0.write(); 132 let func = guard.f_ops.open.unwrap(); 133 let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) }; 134 } 135 return Ok(()); 136 } 137 138 fn close( 139 &self, 140 _data: &mut crate::filesystem::vfs::FilePrivateData, 141 ) -> Result<(), SystemError> { 142 let prev_ref_count = self.1.fetch_sub(1, core::sync::atomic::Ordering::SeqCst); 143 if prev_ref_count == 1 { 144 // 最后一次关闭,需要释放 145 let guard = self.0.write(); 146 let func = guard.f_ops.close.unwrap(); 147 let _ = unsafe { func(0 as *mut vfs_index_node_t, 0 as *mut vfs_file_t) }; 148 } 149 return Ok(()); 150 } 151 152 fn poll(&self) -> Result<PollStatus, SystemError> { 153 return Ok(PollStatus::READ); 154 } 155 156 fn metadata(&self) -> Result<Metadata, SystemError> { 157 return Ok(self.0.read().metadata.clone()); 158 } 159 160 fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 161 let mut inode = self.0.write(); 162 inode.metadata.atime = metadata.atime; 163 inode.metadata.mtime = metadata.mtime; 164 inode.metadata.ctime = metadata.ctime; 165 inode.metadata.mode = metadata.mode; 166 inode.metadata.uid = metadata.uid; 167 inode.metadata.gid = metadata.gid; 168 169 return Ok(()); 170 } 171 172 fn fs(&self) -> alloc::sync::Arc<dyn crate::filesystem::vfs::FileSystem> { 173 return self.0.read().fs.upgrade().unwrap(); 174 } 175 176 fn as_any_ref(&self) -> &dyn core::any::Any { 177 self 178 } 179 180 fn list(&self) -> Result<alloc::vec::Vec<alloc::string::String>, SystemError> { 181 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 182 } 183 } 184 185 #[allow(dead_code)] 186 #[no_mangle] 187 /// for test 188 pub extern "C" fn ps2_keyboard_parse_keycode(input: u8) { 189 PS2_KEYBOARD_FSM.lock().parse(input); 190 } 191