1 use alloc::sync::Arc; 2 use log::warn; 3 use system_error::SystemError; 4 5 use crate::{ 6 driver::base::kobject::KObject, 7 filesystem::{ 8 sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport}, 9 vfs::syscall::ModeType, 10 }, 11 }; 12 13 use super::fbmem::FbDevice; 14 15 /// 为FbDevice实现的sysfs属性组 16 #[derive(Debug)] 17 pub struct FbDeviceAttrGroup; 18 19 impl AttributeGroup for FbDeviceAttrGroup { name(&self) -> Option<&str>20 fn name(&self) -> Option<&str> { 21 None 22 } 23 attrs(&self) -> &[&'static dyn Attribute]24 fn attrs(&self) -> &[&'static dyn Attribute] { 25 &[ 26 &AttrBitsPerPixel, 27 &AttrBlank, 28 &AttrMode, 29 &AttrModes, 30 &AttrName, 31 &AttrPan, 32 &AttrRotate, 33 &AttrState, 34 &AttrStride, 35 &AttrVirtualSize, 36 ] 37 } 38 is_visible( &self, _kobj: alloc::sync::Arc<dyn KObject>, _attr: &'static dyn Attribute, ) -> Option<ModeType>39 fn is_visible( 40 &self, 41 _kobj: alloc::sync::Arc<dyn KObject>, 42 _attr: &'static dyn Attribute, 43 ) -> Option<ModeType> { 44 None 45 } 46 } 47 48 #[derive(Debug)] 49 struct AttrName; 50 51 impl Attribute for AttrName { name(&self) -> &str52 fn name(&self) -> &str { 53 "name" 54 } 55 mode(&self) -> ModeType56 fn mode(&self) -> ModeType { 57 ModeType::S_IRUGO 58 } 59 support(&self) -> SysFSOpsSupport60 fn support(&self) -> SysFSOpsSupport { 61 SysFSOpsSupport::ATTR_SHOW 62 } 63 show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>64 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 65 let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap(); 66 let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?; 67 let name = fb.name(); 68 return sysfs_emit_str(buf, &format!("{}\n", name)); 69 } 70 } 71 72 #[derive(Debug)] 73 struct AttrBitsPerPixel; 74 75 impl Attribute for AttrBitsPerPixel { name(&self) -> &str76 fn name(&self) -> &str { 77 "bits_per_pixel" 78 } 79 mode(&self) -> ModeType80 fn mode(&self) -> ModeType { 81 ModeType::S_IRUGO | ModeType::S_IWUSR 82 } 83 support(&self) -> SysFSOpsSupport84 fn support(&self) -> SysFSOpsSupport { 85 SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE 86 } 87 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>88 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 89 warn!("attr bits_per_pixel store not implemented"); 90 return Err(SystemError::ENOSYS); 91 } 92 show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>93 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 94 let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap(); 95 let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?; 96 let bits_per_pixel = fb.current_fb_var().bits_per_pixel; 97 return sysfs_emit_str(buf, &format!("{}\n", bits_per_pixel)); 98 } 99 } 100 101 /// 用于清空屏幕的属性 102 #[derive(Debug)] 103 struct AttrBlank; 104 105 impl Attribute for AttrBlank { name(&self) -> &str106 fn name(&self) -> &str { 107 "blank" 108 } 109 mode(&self) -> ModeType110 fn mode(&self) -> ModeType { 111 ModeType::S_IWUSR 112 } 113 support(&self) -> SysFSOpsSupport114 fn support(&self) -> SysFSOpsSupport { 115 SysFSOpsSupport::ATTR_STORE 116 } 117 118 // todo: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#309 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>119 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 120 warn!("attr blank store not implemented"); 121 return Err(SystemError::ENOSYS); 122 } 123 } 124 125 #[derive(Debug)] 126 struct AttrMode; 127 128 impl Attribute for AttrMode { name(&self) -> &str129 fn name(&self) -> &str { 130 "mode" 131 } 132 mode(&self) -> ModeType133 fn mode(&self) -> ModeType { 134 ModeType::S_IRUGO | ModeType::S_IWUSR 135 } 136 support(&self) -> SysFSOpsSupport137 fn support(&self) -> SysFSOpsSupport { 138 SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE 139 } 140 141 /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#166 show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>142 fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> { 143 todo!("AttrMode::show") 144 } 145 146 /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#135 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>147 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 148 todo!("AttrMode::store") 149 } 150 } 151 152 #[derive(Debug)] 153 struct AttrModes; 154 155 impl Attribute for AttrModes { name(&self) -> &str156 fn name(&self) -> &str { 157 "modes" 158 } 159 mode(&self) -> ModeType160 fn mode(&self) -> ModeType { 161 ModeType::S_IRUGO | ModeType::S_IWUSR 162 } 163 support(&self) -> SysFSOpsSupport164 fn support(&self) -> SysFSOpsSupport { 165 SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE 166 } 167 168 /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#206 show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>169 fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> { 170 todo!("AttrMode::show") 171 } 172 173 /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#177 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>174 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 175 todo!("AttrMode::store") 176 } 177 } 178 179 #[derive(Debug)] 180 struct AttrPan; 181 182 impl Attribute for AttrPan { name(&self) -> &str183 fn name(&self) -> &str { 184 "pan" 185 } 186 mode(&self) -> ModeType187 fn mode(&self) -> ModeType { 188 ModeType::S_IRUGO | ModeType::S_IWUSR 189 } 190 support(&self) -> SysFSOpsSupport191 fn support(&self) -> SysFSOpsSupport { 192 SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE 193 } 194 show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>195 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 196 let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap(); 197 let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?; 198 let var_info = fb.current_fb_var(); 199 return sysfs_emit_str(buf, &format!("{},{}\n", var_info.xoffset, var_info.yoffset)); 200 } 201 202 /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#365 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>203 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 204 todo!("AttrPan::store") 205 } 206 } 207 208 #[derive(Debug)] 209 struct AttrVirtualSize; 210 211 impl Attribute for AttrVirtualSize { name(&self) -> &str212 fn name(&self) -> &str { 213 "virtual_size" 214 } 215 mode(&self) -> ModeType216 fn mode(&self) -> ModeType { 217 ModeType::S_IRUGO | ModeType::S_IWUSR 218 } 219 support(&self) -> SysFSOpsSupport220 fn support(&self) -> SysFSOpsSupport { 221 SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE 222 } 223 show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>224 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 225 let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap(); 226 let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?; 227 let var_info = fb.current_fb_var(); 228 return sysfs_emit_str( 229 buf, 230 &format!("{},{}\n", var_info.xres_virtual, var_info.yres_virtual), 231 ); 232 } 233 234 /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#273 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>235 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 236 todo!("AttrVirtualSize::store") 237 } 238 } 239 240 #[derive(Debug)] 241 struct AttrStride; 242 243 impl Attribute for AttrStride { name(&self) -> &str244 fn name(&self) -> &str { 245 "stride" 246 } 247 mode(&self) -> ModeType248 fn mode(&self) -> ModeType { 249 ModeType::S_IRUGO 250 } 251 support(&self) -> SysFSOpsSupport252 fn support(&self) -> SysFSOpsSupport { 253 SysFSOpsSupport::ATTR_SHOW 254 } 255 show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>256 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 257 let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap(); 258 let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?; 259 let fix_info = fb.current_fb_fix(); 260 return sysfs_emit_str(buf, &format!("{}\n", fix_info.line_length)); 261 } 262 } 263 264 #[derive(Debug)] 265 struct AttrRotate; 266 267 impl Attribute for AttrRotate { name(&self) -> &str268 fn name(&self) -> &str { 269 "rotate" 270 } 271 mode(&self) -> ModeType272 fn mode(&self) -> ModeType { 273 ModeType::S_IRUGO | ModeType::S_IWUSR 274 } 275 support(&self) -> SysFSOpsSupport276 fn support(&self) -> SysFSOpsSupport { 277 SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE 278 } 279 show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>280 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 281 let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap(); 282 let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?; 283 let var_info = fb.current_fb_var(); 284 285 return sysfs_emit_str(buf, &format!("{}\n", var_info.rotate_angle)); 286 } 287 288 /// todo https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#246 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>289 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 290 todo!("AttrRotate::store") 291 } 292 } 293 294 #[derive(Debug)] 295 struct AttrState; 296 297 impl Attribute for AttrState { name(&self) -> &str298 fn name(&self) -> &str { 299 "state" 300 } 301 mode(&self) -> ModeType302 fn mode(&self) -> ModeType { 303 ModeType::S_IRUGO | ModeType::S_IWUSR 304 } 305 support(&self) -> SysFSOpsSupport306 fn support(&self) -> SysFSOpsSupport { 307 SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE 308 } 309 show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>310 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 311 let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap(); 312 let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?; 313 314 return sysfs_emit_str(buf, &format!("{}\n", fb.state() as u8)); 315 } 316 317 /// todo https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#406 store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>318 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> { 319 todo!("AttrState::store") 320 } 321 } 322