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