1*c566df45SLoGin use core::{ 2*c566df45SLoGin ffi::{c_uint, c_void}, 3*c566df45SLoGin mem::MaybeUninit, 4*c566df45SLoGin sync::atomic::AtomicBool, 5*c566df45SLoGin }; 6*c566df45SLoGin 7*c566df45SLoGin use alloc::{ 8*c566df45SLoGin string::{String, ToString}, 9*c566df45SLoGin sync::{Arc, Weak}, 10*c566df45SLoGin vec::Vec, 11*c566df45SLoGin }; 12*c566df45SLoGin use system_error::SystemError; 13*c566df45SLoGin use unified_init::macros::unified_init; 14*c566df45SLoGin 15*c566df45SLoGin use crate::{ 16*c566df45SLoGin arch::MMArch, 17*c566df45SLoGin driver::{ 18*c566df45SLoGin base::{ 19*c566df45SLoGin class::Class, 20*c566df45SLoGin device::{ 21*c566df45SLoGin bus::Bus, device_manager, driver::Driver, Device, DeviceState, DeviceType, IdTable, 22*c566df45SLoGin }, 23*c566df45SLoGin kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 24*c566df45SLoGin kset::KSet, 25*c566df45SLoGin platform::{ 26*c566df45SLoGin platform_device::{platform_device_manager, PlatformDevice}, 27*c566df45SLoGin platform_driver::{platform_driver_manager, PlatformDriver}, 28*c566df45SLoGin CompatibleTable, 29*c566df45SLoGin }, 30*c566df45SLoGin }, 31*c566df45SLoGin tty::serial::serial8250::send_to_default_serial8250_port, 32*c566df45SLoGin video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual}, 33*c566df45SLoGin }, 34*c566df45SLoGin filesystem::{ 35*c566df45SLoGin kernfs::KernFSInode, 36*c566df45SLoGin sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport}, 37*c566df45SLoGin vfs::syscall::ModeType, 38*c566df45SLoGin }, 39*c566df45SLoGin include::bindings::bindings::{ 40*c566df45SLoGin multiboot2_get_Framebuffer_info, multiboot2_iter, multiboot_tag_framebuffer_info_t, 41*c566df45SLoGin }, 42*c566df45SLoGin init::{boot_params, initcall::INITCALL_DEVICE}, 43*c566df45SLoGin libs::{ 44*c566df45SLoGin align::page_align_up, 45*c566df45SLoGin once::Once, 46*c566df45SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 47*c566df45SLoGin spinlock::SpinLock, 48*c566df45SLoGin }, 49*c566df45SLoGin mm::{ 50*c566df45SLoGin allocator::page_frame::PageFrameCount, no_init::pseudo_map_phys, MemoryManagementArch, 51*c566df45SLoGin PhysAddr, VirtAddr, 52*c566df45SLoGin }, 53*c566df45SLoGin }; 54*c566df45SLoGin 55*c566df45SLoGin use super::base::{ 56*c566df45SLoGin fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType, 57*c566df45SLoGin FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo, 58*c566df45SLoGin FrameBufferOps, 59*c566df45SLoGin }; 60*c566df45SLoGin 61*c566df45SLoGin /// 当前机器上面是否有vesa帧缓冲区 62*c566df45SLoGin static HAS_VESA_FB: AtomicBool = AtomicBool::new(false); 63*c566df45SLoGin 64*c566df45SLoGin lazy_static! { 65*c566df45SLoGin static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo { 66*c566df45SLoGin id: FixedScreenInfo::name2id("VESA VGA"), 67*c566df45SLoGin fb_type: FbType::PackedPixels, 68*c566df45SLoGin accel: FbAccel::None, 69*c566df45SLoGin ..Default::default() 70*c566df45SLoGin }); 71*c566df45SLoGin static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo { 72*c566df45SLoGin activate: FbActivateFlags::FB_ACTIVATE_NOW, 73*c566df45SLoGin height: None, 74*c566df45SLoGin width: None, 75*c566df45SLoGin right_margin: 32, 76*c566df45SLoGin upper_margin: 16, 77*c566df45SLoGin lower_margin: 4, 78*c566df45SLoGin vsync_len: 4, 79*c566df45SLoGin vmode: FbVModeFlags::FB_VMODE_NONINTERLACED, 80*c566df45SLoGin 81*c566df45SLoGin ..Default::default() 82*c566df45SLoGin }); 83*c566df45SLoGin } 84*c566df45SLoGin 85*c566df45SLoGin #[derive(Debug)] 86*c566df45SLoGin #[cast_to([sync] Device)] 87*c566df45SLoGin #[cast_to([sync] PlatformDevice)] 88*c566df45SLoGin pub struct VesaFb { 89*c566df45SLoGin inner: SpinLock<InnerVesaFb>, 90*c566df45SLoGin kobj_state: LockedKObjectState, 91*c566df45SLoGin } 92*c566df45SLoGin 93*c566df45SLoGin impl VesaFb { 94*c566df45SLoGin pub const NAME: &'static str = "vesa_vga"; 95*c566df45SLoGin pub fn new() -> Self { 96*c566df45SLoGin return Self { 97*c566df45SLoGin inner: SpinLock::new(InnerVesaFb { 98*c566df45SLoGin bus: None, 99*c566df45SLoGin class: None, 100*c566df45SLoGin driver: None, 101*c566df45SLoGin kern_inode: None, 102*c566df45SLoGin parent: None, 103*c566df45SLoGin kset: None, 104*c566df45SLoGin kobj_type: None, 105*c566df45SLoGin device_state: DeviceState::NotInitialized, 106*c566df45SLoGin pdev_id: 0, 107*c566df45SLoGin pdev_id_auto: false, 108*c566df45SLoGin fb_id: FbId::INIT, 109*c566df45SLoGin fb_device: None, 110*c566df45SLoGin fb_state: FbState::Suspended, 111*c566df45SLoGin }), 112*c566df45SLoGin kobj_state: LockedKObjectState::new(None), 113*c566df45SLoGin }; 114*c566df45SLoGin } 115*c566df45SLoGin } 116*c566df45SLoGin 117*c566df45SLoGin #[derive(Debug)] 118*c566df45SLoGin struct InnerVesaFb { 119*c566df45SLoGin bus: Option<Weak<dyn Bus>>, 120*c566df45SLoGin class: Option<Arc<dyn Class>>, 121*c566df45SLoGin driver: Option<Weak<dyn Driver>>, 122*c566df45SLoGin kern_inode: Option<Arc<KernFSInode>>, 123*c566df45SLoGin parent: Option<Weak<dyn KObject>>, 124*c566df45SLoGin kset: Option<Arc<KSet>>, 125*c566df45SLoGin kobj_type: Option<&'static dyn KObjType>, 126*c566df45SLoGin device_state: DeviceState, 127*c566df45SLoGin pdev_id: i32, 128*c566df45SLoGin pdev_id_auto: bool, 129*c566df45SLoGin fb_id: FbId, 130*c566df45SLoGin fb_device: Option<Arc<FbDevice>>, 131*c566df45SLoGin fb_state: FbState, 132*c566df45SLoGin } 133*c566df45SLoGin 134*c566df45SLoGin impl FrameBuffer for VesaFb { 135*c566df45SLoGin fn fb_id(&self) -> FbId { 136*c566df45SLoGin self.inner.lock().fb_id 137*c566df45SLoGin } 138*c566df45SLoGin 139*c566df45SLoGin fn set_fb_id(&self, id: FbId) { 140*c566df45SLoGin self.inner.lock().fb_id = id; 141*c566df45SLoGin } 142*c566df45SLoGin } 143*c566df45SLoGin 144*c566df45SLoGin impl PlatformDevice for VesaFb { 145*c566df45SLoGin fn pdev_name(&self) -> &str { 146*c566df45SLoGin Self::NAME 147*c566df45SLoGin } 148*c566df45SLoGin 149*c566df45SLoGin fn set_pdev_id(&self, id: i32) { 150*c566df45SLoGin self.inner.lock().pdev_id = id; 151*c566df45SLoGin } 152*c566df45SLoGin 153*c566df45SLoGin fn set_pdev_id_auto(&self, id_auto: bool) { 154*c566df45SLoGin self.inner.lock().pdev_id_auto = id_auto; 155*c566df45SLoGin } 156*c566df45SLoGin 157*c566df45SLoGin fn compatible_table(&self) -> CompatibleTable { 158*c566df45SLoGin todo!() 159*c566df45SLoGin } 160*c566df45SLoGin 161*c566df45SLoGin fn is_initialized(&self) -> bool { 162*c566df45SLoGin self.inner.lock().device_state == DeviceState::Initialized 163*c566df45SLoGin } 164*c566df45SLoGin 165*c566df45SLoGin fn set_state(&self, set_state: DeviceState) { 166*c566df45SLoGin self.inner.lock().device_state = set_state; 167*c566df45SLoGin } 168*c566df45SLoGin } 169*c566df45SLoGin 170*c566df45SLoGin impl Device for VesaFb { 171*c566df45SLoGin fn dev_type(&self) -> DeviceType { 172*c566df45SLoGin DeviceType::Char 173*c566df45SLoGin } 174*c566df45SLoGin 175*c566df45SLoGin fn id_table(&self) -> IdTable { 176*c566df45SLoGin IdTable::new(self.name(), None) 177*c566df45SLoGin } 178*c566df45SLoGin 179*c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 180*c566df45SLoGin self.inner.lock().bus.clone() 181*c566df45SLoGin } 182*c566df45SLoGin 183*c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 184*c566df45SLoGin self.inner.lock().bus = bus; 185*c566df45SLoGin } 186*c566df45SLoGin 187*c566df45SLoGin fn set_class(&self, class: Option<Arc<dyn Class>>) { 188*c566df45SLoGin self.inner.lock().class = class; 189*c566df45SLoGin } 190*c566df45SLoGin 191*c566df45SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 192*c566df45SLoGin self.inner.lock().driver.clone()?.upgrade() 193*c566df45SLoGin } 194*c566df45SLoGin 195*c566df45SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 196*c566df45SLoGin self.inner.lock().driver = driver; 197*c566df45SLoGin } 198*c566df45SLoGin 199*c566df45SLoGin fn is_dead(&self) -> bool { 200*c566df45SLoGin false 201*c566df45SLoGin } 202*c566df45SLoGin 203*c566df45SLoGin fn can_match(&self) -> bool { 204*c566df45SLoGin true 205*c566df45SLoGin } 206*c566df45SLoGin 207*c566df45SLoGin fn set_can_match(&self, _can_match: bool) {} 208*c566df45SLoGin 209*c566df45SLoGin fn state_synced(&self) -> bool { 210*c566df45SLoGin true 211*c566df45SLoGin } 212*c566df45SLoGin } 213*c566df45SLoGin 214*c566df45SLoGin impl KObject for VesaFb { 215*c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 216*c566df45SLoGin self 217*c566df45SLoGin } 218*c566df45SLoGin 219*c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 220*c566df45SLoGin self.inner.lock().kern_inode = inode; 221*c566df45SLoGin } 222*c566df45SLoGin 223*c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 224*c566df45SLoGin self.inner.lock().kern_inode.clone() 225*c566df45SLoGin } 226*c566df45SLoGin 227*c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 228*c566df45SLoGin self.inner.lock().parent.clone() 229*c566df45SLoGin } 230*c566df45SLoGin 231*c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 232*c566df45SLoGin self.inner.lock().parent = parent; 233*c566df45SLoGin } 234*c566df45SLoGin 235*c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 236*c566df45SLoGin self.inner.lock().kset.clone() 237*c566df45SLoGin } 238*c566df45SLoGin 239*c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 240*c566df45SLoGin self.inner.lock().kset = kset; 241*c566df45SLoGin } 242*c566df45SLoGin 243*c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 244*c566df45SLoGin self.inner.lock().kobj_type 245*c566df45SLoGin } 246*c566df45SLoGin 247*c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 248*c566df45SLoGin self.inner.lock().kobj_type = ktype; 249*c566df45SLoGin } 250*c566df45SLoGin 251*c566df45SLoGin fn name(&self) -> String { 252*c566df45SLoGin Self::NAME.to_string() 253*c566df45SLoGin } 254*c566df45SLoGin 255*c566df45SLoGin fn set_name(&self, _name: String) { 256*c566df45SLoGin // do nothing 257*c566df45SLoGin } 258*c566df45SLoGin 259*c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 260*c566df45SLoGin self.kobj_state.read() 261*c566df45SLoGin } 262*c566df45SLoGin 263*c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 264*c566df45SLoGin self.kobj_state.write() 265*c566df45SLoGin } 266*c566df45SLoGin 267*c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 268*c566df45SLoGin *self.kobj_state.write() = state; 269*c566df45SLoGin } 270*c566df45SLoGin } 271*c566df45SLoGin 272*c566df45SLoGin impl FrameBufferOps for VesaFb { 273*c566df45SLoGin fn fb_open(&self, _user: bool) { 274*c566df45SLoGin todo!() 275*c566df45SLoGin } 276*c566df45SLoGin 277*c566df45SLoGin fn fb_release(&self, _user: bool) { 278*c566df45SLoGin todo!() 279*c566df45SLoGin } 280*c566df45SLoGin 281*c566df45SLoGin fn fb_set_color_register( 282*c566df45SLoGin &self, 283*c566df45SLoGin _regno: u16, 284*c566df45SLoGin _red: u16, 285*c566df45SLoGin _green: u16, 286*c566df45SLoGin _blue: u16, 287*c566df45SLoGin ) -> Result<(), SystemError> { 288*c566df45SLoGin todo!() 289*c566df45SLoGin } 290*c566df45SLoGin 291*c566df45SLoGin fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> { 292*c566df45SLoGin todo!() 293*c566df45SLoGin } 294*c566df45SLoGin 295*c566df45SLoGin fn fb_destroy(&self) { 296*c566df45SLoGin todo!() 297*c566df45SLoGin } 298*c566df45SLoGin } 299*c566df45SLoGin 300*c566df45SLoGin impl FrameBufferInfo for VesaFb { 301*c566df45SLoGin fn fb_device(&self) -> Option<Arc<FbDevice>> { 302*c566df45SLoGin self.inner.lock().fb_device.clone() 303*c566df45SLoGin } 304*c566df45SLoGin 305*c566df45SLoGin fn set_fb_device(&self, device: Option<Arc<FbDevice>>) { 306*c566df45SLoGin self.inner.lock().fb_device = device; 307*c566df45SLoGin } 308*c566df45SLoGin 309*c566df45SLoGin fn screen_size(&self) -> usize { 310*c566df45SLoGin todo!() 311*c566df45SLoGin } 312*c566df45SLoGin 313*c566df45SLoGin fn current_fb_var(&self) -> FbVarScreenInfo { 314*c566df45SLoGin VESAFB_DEFINED.read().clone() 315*c566df45SLoGin } 316*c566df45SLoGin 317*c566df45SLoGin fn current_fb_fix(&self) -> FixedScreenInfo { 318*c566df45SLoGin VESAFB_FIX_INFO.read().clone() 319*c566df45SLoGin } 320*c566df45SLoGin 321*c566df45SLoGin fn video_mode(&self) -> Option<&FbVideoMode> { 322*c566df45SLoGin todo!() 323*c566df45SLoGin } 324*c566df45SLoGin 325*c566df45SLoGin fn state(&self) -> FbState { 326*c566df45SLoGin self.inner.lock().fb_state 327*c566df45SLoGin } 328*c566df45SLoGin } 329*c566df45SLoGin 330*c566df45SLoGin #[derive(Debug)] 331*c566df45SLoGin #[cast_to([sync] PlatformDriver)] 332*c566df45SLoGin struct VesaFbDriver { 333*c566df45SLoGin inner: SpinLock<InnerVesaFbDriver>, 334*c566df45SLoGin kobj_state: LockedKObjectState, 335*c566df45SLoGin } 336*c566df45SLoGin 337*c566df45SLoGin impl VesaFbDriver { 338*c566df45SLoGin pub fn new() -> Arc<Self> { 339*c566df45SLoGin let r = Arc::new(Self { 340*c566df45SLoGin inner: SpinLock::new(InnerVesaFbDriver { 341*c566df45SLoGin ktype: None, 342*c566df45SLoGin kset: None, 343*c566df45SLoGin parent: None, 344*c566df45SLoGin kernfs_inode: None, 345*c566df45SLoGin devices: Vec::new(), 346*c566df45SLoGin bus: None, 347*c566df45SLoGin self_ref: Weak::new(), 348*c566df45SLoGin }), 349*c566df45SLoGin kobj_state: LockedKObjectState::new(None), 350*c566df45SLoGin }); 351*c566df45SLoGin 352*c566df45SLoGin r.inner.lock().self_ref = Arc::downgrade(&r); 353*c566df45SLoGin 354*c566df45SLoGin return r; 355*c566df45SLoGin } 356*c566df45SLoGin } 357*c566df45SLoGin 358*c566df45SLoGin #[derive(Debug)] 359*c566df45SLoGin struct InnerVesaFbDriver { 360*c566df45SLoGin ktype: Option<&'static dyn KObjType>, 361*c566df45SLoGin kset: Option<Arc<KSet>>, 362*c566df45SLoGin parent: Option<Weak<dyn KObject>>, 363*c566df45SLoGin kernfs_inode: Option<Arc<KernFSInode>>, 364*c566df45SLoGin devices: Vec<Arc<dyn Device>>, 365*c566df45SLoGin bus: Option<Weak<dyn Bus>>, 366*c566df45SLoGin 367*c566df45SLoGin self_ref: Weak<VesaFbDriver>, 368*c566df45SLoGin } 369*c566df45SLoGin 370*c566df45SLoGin impl VesaFbDriver { 371*c566df45SLoGin const NAME: &'static str = "vesa-framebuffer"; 372*c566df45SLoGin } 373*c566df45SLoGin 374*c566df45SLoGin impl PlatformDriver for VesaFbDriver { 375*c566df45SLoGin fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 376*c566df45SLoGin let device = device 377*c566df45SLoGin .clone() 378*c566df45SLoGin .arc_any() 379*c566df45SLoGin .downcast::<VesaFb>() 380*c566df45SLoGin .map_err(|_| SystemError::EINVAL)?; 381*c566df45SLoGin 382*c566df45SLoGin device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone())); 383*c566df45SLoGin 384*c566df45SLoGin return Ok(()); 385*c566df45SLoGin } 386*c566df45SLoGin 387*c566df45SLoGin fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 388*c566df45SLoGin todo!() 389*c566df45SLoGin } 390*c566df45SLoGin 391*c566df45SLoGin fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 392*c566df45SLoGin // do nothing 393*c566df45SLoGin return Ok(()); 394*c566df45SLoGin } 395*c566df45SLoGin 396*c566df45SLoGin fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 397*c566df45SLoGin // do nothing 398*c566df45SLoGin return Ok(()); 399*c566df45SLoGin } 400*c566df45SLoGin 401*c566df45SLoGin fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 402*c566df45SLoGin todo!() 403*c566df45SLoGin } 404*c566df45SLoGin } 405*c566df45SLoGin 406*c566df45SLoGin impl Driver for VesaFbDriver { 407*c566df45SLoGin fn id_table(&self) -> Option<IdTable> { 408*c566df45SLoGin Some(IdTable::new(VesaFb::NAME.to_string(), None)) 409*c566df45SLoGin } 410*c566df45SLoGin 411*c566df45SLoGin fn devices(&self) -> Vec<Arc<dyn Device>> { 412*c566df45SLoGin self.inner.lock().devices.clone() 413*c566df45SLoGin } 414*c566df45SLoGin 415*c566df45SLoGin fn add_device(&self, device: Arc<dyn Device>) { 416*c566df45SLoGin let mut guard = self.inner.lock(); 417*c566df45SLoGin // check if the device is already in the list 418*c566df45SLoGin if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) { 419*c566df45SLoGin return; 420*c566df45SLoGin } 421*c566df45SLoGin 422*c566df45SLoGin guard.devices.push(device); 423*c566df45SLoGin } 424*c566df45SLoGin 425*c566df45SLoGin fn delete_device(&self, device: &Arc<dyn Device>) { 426*c566df45SLoGin let mut guard = self.inner.lock(); 427*c566df45SLoGin guard.devices.retain(|dev| !Arc::ptr_eq(dev, device)); 428*c566df45SLoGin } 429*c566df45SLoGin 430*c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 431*c566df45SLoGin self.inner.lock().bus = bus; 432*c566df45SLoGin } 433*c566df45SLoGin 434*c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 435*c566df45SLoGin self.inner.lock().bus.clone() 436*c566df45SLoGin } 437*c566df45SLoGin 438*c566df45SLoGin fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 439*c566df45SLoGin return &[&VesaFbAnonAttributeGroup]; 440*c566df45SLoGin } 441*c566df45SLoGin } 442*c566df45SLoGin 443*c566df45SLoGin impl KObject for VesaFbDriver { 444*c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 445*c566df45SLoGin self 446*c566df45SLoGin } 447*c566df45SLoGin 448*c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 449*c566df45SLoGin self.inner.lock().kernfs_inode = inode; 450*c566df45SLoGin } 451*c566df45SLoGin 452*c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 453*c566df45SLoGin self.inner.lock().kernfs_inode.clone() 454*c566df45SLoGin } 455*c566df45SLoGin 456*c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 457*c566df45SLoGin self.inner.lock().parent.clone() 458*c566df45SLoGin } 459*c566df45SLoGin 460*c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 461*c566df45SLoGin self.inner.lock().parent = parent; 462*c566df45SLoGin } 463*c566df45SLoGin 464*c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 465*c566df45SLoGin self.inner.lock().kset.clone() 466*c566df45SLoGin } 467*c566df45SLoGin 468*c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 469*c566df45SLoGin self.inner.lock().kset = kset; 470*c566df45SLoGin } 471*c566df45SLoGin 472*c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 473*c566df45SLoGin self.inner.lock().ktype 474*c566df45SLoGin } 475*c566df45SLoGin 476*c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 477*c566df45SLoGin self.inner.lock().ktype = ktype; 478*c566df45SLoGin } 479*c566df45SLoGin 480*c566df45SLoGin fn name(&self) -> String { 481*c566df45SLoGin Self::NAME.to_string() 482*c566df45SLoGin } 483*c566df45SLoGin 484*c566df45SLoGin fn set_name(&self, _name: String) { 485*c566df45SLoGin // do nothing 486*c566df45SLoGin } 487*c566df45SLoGin 488*c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 489*c566df45SLoGin self.kobj_state.read() 490*c566df45SLoGin } 491*c566df45SLoGin 492*c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 493*c566df45SLoGin self.kobj_state.write() 494*c566df45SLoGin } 495*c566df45SLoGin 496*c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 497*c566df45SLoGin *self.kobj_state.write() = state; 498*c566df45SLoGin } 499*c566df45SLoGin } 500*c566df45SLoGin 501*c566df45SLoGin #[derive(Debug)] 502*c566df45SLoGin struct VesaFbAnonAttributeGroup; 503*c566df45SLoGin 504*c566df45SLoGin impl AttributeGroup for VesaFbAnonAttributeGroup { 505*c566df45SLoGin fn name(&self) -> Option<&str> { 506*c566df45SLoGin None 507*c566df45SLoGin } 508*c566df45SLoGin 509*c566df45SLoGin fn attrs(&self) -> &[&'static dyn Attribute] { 510*c566df45SLoGin &[&AnonAttrPhysAddr as &'static dyn Attribute] 511*c566df45SLoGin } 512*c566df45SLoGin 513*c566df45SLoGin fn is_visible( 514*c566df45SLoGin &self, 515*c566df45SLoGin _kobj: Arc<dyn KObject>, 516*c566df45SLoGin attr: &'static dyn Attribute, 517*c566df45SLoGin ) -> Option<ModeType> { 518*c566df45SLoGin Some(attr.mode()) 519*c566df45SLoGin } 520*c566df45SLoGin } 521*c566df45SLoGin 522*c566df45SLoGin #[derive(Debug)] 523*c566df45SLoGin struct AnonAttrPhysAddr; 524*c566df45SLoGin 525*c566df45SLoGin impl Attribute for AnonAttrPhysAddr { 526*c566df45SLoGin fn name(&self) -> &str { 527*c566df45SLoGin "smem_start" 528*c566df45SLoGin } 529*c566df45SLoGin 530*c566df45SLoGin fn mode(&self) -> ModeType { 531*c566df45SLoGin ModeType::S_IRUGO 532*c566df45SLoGin } 533*c566df45SLoGin 534*c566df45SLoGin fn support(&self) -> SysFSOpsSupport { 535*c566df45SLoGin SysFSOpsSupport::SHOW 536*c566df45SLoGin } 537*c566df45SLoGin 538*c566df45SLoGin fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 539*c566df45SLoGin sysfs_emit_str( 540*c566df45SLoGin buf, 541*c566df45SLoGin format!( 542*c566df45SLoGin "0x{:x}\n", 543*c566df45SLoGin VESAFB_FIX_INFO 544*c566df45SLoGin .read() 545*c566df45SLoGin .smem_start 546*c566df45SLoGin .unwrap_or(PhysAddr::new(0)) 547*c566df45SLoGin .data() 548*c566df45SLoGin ) 549*c566df45SLoGin .as_str(), 550*c566df45SLoGin ) 551*c566df45SLoGin } 552*c566df45SLoGin } 553*c566df45SLoGin 554*c566df45SLoGin #[unified_init(INITCALL_DEVICE)] 555*c566df45SLoGin pub fn vesa_fb_driver_init() -> Result<(), SystemError> { 556*c566df45SLoGin let driver = VesaFbDriver::new(); 557*c566df45SLoGin 558*c566df45SLoGin platform_driver_manager().register(driver)?; 559*c566df45SLoGin 560*c566df45SLoGin return Ok(()); 561*c566df45SLoGin } 562*c566df45SLoGin 563*c566df45SLoGin /// 在内存管理初始化之前,初始化vesafb 564*c566df45SLoGin pub fn vesafb_early_init() -> Result<VirtAddr, SystemError> { 565*c566df45SLoGin let mut _reserved: u32 = 0; 566*c566df45SLoGin 567*c566df45SLoGin let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit(); 568*c566df45SLoGin //从multiboot2中读取帧缓冲区信息至fb_info 569*c566df45SLoGin 570*c566df45SLoGin // todo: 换成rust的,并且检测是否成功获取 571*c566df45SLoGin unsafe { 572*c566df45SLoGin multiboot2_iter( 573*c566df45SLoGin Some(multiboot2_get_Framebuffer_info), 574*c566df45SLoGin fb_info.as_mut_ptr() as usize as *mut c_void, 575*c566df45SLoGin &mut _reserved as *mut c_uint, 576*c566df45SLoGin ) 577*c566df45SLoGin }; 578*c566df45SLoGin unsafe { fb_info.assume_init() }; 579*c566df45SLoGin let fb_info: multiboot_tag_framebuffer_info_t = unsafe { core::mem::transmute(fb_info) }; 580*c566df45SLoGin 581*c566df45SLoGin // todo: 判断是否有vesa帧缓冲区,这里暂时直接设置true 582*c566df45SLoGin HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst); 583*c566df45SLoGin 584*c566df45SLoGin let width = fb_info.framebuffer_width; 585*c566df45SLoGin let height = fb_info.framebuffer_height; 586*c566df45SLoGin 587*c566df45SLoGin let mut boot_params_guard = boot_params().write(); 588*c566df45SLoGin let boottime_screen_info = &mut boot_params_guard.screen_info; 589*c566df45SLoGin 590*c566df45SLoGin boottime_screen_info.is_vga = true; 591*c566df45SLoGin 592*c566df45SLoGin boottime_screen_info.lfb_base = PhysAddr::new(fb_info.framebuffer_addr as usize); 593*c566df45SLoGin 594*c566df45SLoGin if fb_info.framebuffer_type == 2 { 595*c566df45SLoGin //当type=2时,width与height用字符数表示,故depth=8 596*c566df45SLoGin boottime_screen_info.origin_video_cols = width as u8; 597*c566df45SLoGin boottime_screen_info.origin_video_lines = height as u8; 598*c566df45SLoGin boottime_screen_info.video_type = BootTimeVideoType::Mda; 599*c566df45SLoGin boottime_screen_info.lfb_depth = 8; 600*c566df45SLoGin } else { 601*c566df45SLoGin //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数 602*c566df45SLoGin boottime_screen_info.lfb_width = width; 603*c566df45SLoGin boottime_screen_info.lfb_height = height; 604*c566df45SLoGin boottime_screen_info.video_type = BootTimeVideoType::Vlfb; 605*c566df45SLoGin boottime_screen_info.lfb_depth = fb_info.framebuffer_bpp as u8; 606*c566df45SLoGin } 607*c566df45SLoGin 608*c566df45SLoGin boottime_screen_info.lfb_size = 609*c566df45SLoGin (width * height * ((fb_info.framebuffer_bpp as u32 + 7) / 8)) as usize; 610*c566df45SLoGin 611*c566df45SLoGin let buf_vaddr = VirtAddr::new(0xffff800003200000); 612*c566df45SLoGin boottime_screen_info.lfb_virt_base = Some(buf_vaddr); 613*c566df45SLoGin 614*c566df45SLoGin let init_text = "Video driver to map.\n\0"; 615*c566df45SLoGin send_to_default_serial8250_port(init_text.as_bytes()); 616*c566df45SLoGin 617*c566df45SLoGin // 地址映射 618*c566df45SLoGin let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize); 619*c566df45SLoGin let count = 620*c566df45SLoGin PageFrameCount::new(page_align_up(boottime_screen_info.lfb_size) / MMArch::PAGE_SIZE); 621*c566df45SLoGin unsafe { pseudo_map_phys(buf_vaddr, paddr, count) }; 622*c566df45SLoGin return Ok(buf_vaddr); 623*c566df45SLoGin } 624*c566df45SLoGin 625*c566df45SLoGin #[unified_init(INITCALL_DEVICE)] 626*c566df45SLoGin fn vesa_fb_device_init() -> Result<(), SystemError> { 627*c566df45SLoGin // 如果没有vesa帧缓冲区,直接返回 628*c566df45SLoGin if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) { 629*c566df45SLoGin return Ok(()); 630*c566df45SLoGin } 631*c566df45SLoGin 632*c566df45SLoGin static INIT: Once = Once::new(); 633*c566df45SLoGin INIT.call_once(|| { 634*c566df45SLoGin kinfo!("vesa fb device init"); 635*c566df45SLoGin 636*c566df45SLoGin let mut fix_info_guard = VESAFB_FIX_INFO.write_irqsave(); 637*c566df45SLoGin let mut var_info_guard = VESAFB_DEFINED.write_irqsave(); 638*c566df45SLoGin 639*c566df45SLoGin let boot_params_guard = boot_params().read(); 640*c566df45SLoGin let boottime_screen_info = &boot_params_guard.screen_info; 641*c566df45SLoGin 642*c566df45SLoGin fix_info_guard.smem_start = Some(boottime_screen_info.lfb_base); 643*c566df45SLoGin fix_info_guard.smem_len = boottime_screen_info.lfb_size; 644*c566df45SLoGin 645*c566df45SLoGin if boottime_screen_info.video_type == BootTimeVideoType::Mda { 646*c566df45SLoGin fix_info_guard.visual = FbVisual::Mono10; 647*c566df45SLoGin var_info_guard.bits_per_pixel = 8; 648*c566df45SLoGin fix_info_guard.line_length = (boottime_screen_info.origin_video_cols as u32) 649*c566df45SLoGin * (var_info_guard.bits_per_pixel / 8); 650*c566df45SLoGin var_info_guard.xres_virtual = boottime_screen_info.origin_video_cols as u32; 651*c566df45SLoGin var_info_guard.yres_virtual = boottime_screen_info.origin_video_lines as u32; 652*c566df45SLoGin } else { 653*c566df45SLoGin fix_info_guard.visual = FbVisual::TrueColor; 654*c566df45SLoGin var_info_guard.bits_per_pixel = boottime_screen_info.lfb_depth as u32; 655*c566df45SLoGin fix_info_guard.line_length = 656*c566df45SLoGin (boottime_screen_info.lfb_width as u32) * (var_info_guard.bits_per_pixel / 8); 657*c566df45SLoGin var_info_guard.xres_virtual = boottime_screen_info.lfb_width as u32; 658*c566df45SLoGin var_info_guard.yres_virtual = boottime_screen_info.lfb_height as u32; 659*c566df45SLoGin } 660*c566df45SLoGin 661*c566df45SLoGin drop(var_info_guard); 662*c566df45SLoGin drop(fix_info_guard); 663*c566df45SLoGin 664*c566df45SLoGin let device = Arc::new(VesaFb::new()); 665*c566df45SLoGin device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>)); 666*c566df45SLoGin 667*c566df45SLoGin platform_device_manager() 668*c566df45SLoGin .device_add(device.clone() as Arc<dyn PlatformDevice>) 669*c566df45SLoGin .expect("vesa_fb_device_init: platform_device_manager().device_add failed"); 670*c566df45SLoGin 671*c566df45SLoGin frame_buffer_manager() 672*c566df45SLoGin .register_fb(device.clone() as Arc<dyn FrameBuffer>) 673*c566df45SLoGin .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed"); 674*c566df45SLoGin 675*c566df45SLoGin // 设置vesa fb的状态为运行中 676*c566df45SLoGin device.inner.lock().fb_state = FbState::Running; 677*c566df45SLoGin }); 678*c566df45SLoGin 679*c566df45SLoGin return Ok(()); 680*c566df45SLoGin } 681