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