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