1 use core::sync::atomic::AtomicBool; 2 3 use alloc::{ 4 string::{String, ToString}, 5 sync::{Arc, Weak}, 6 vec::Vec, 7 }; 8 use log::{info, warn}; 9 use system_error::SystemError; 10 use unified_init::macros::unified_init; 11 12 use crate::{ 13 driver::{ 14 base::{ 15 class::Class, 16 device::{ 17 bus::Bus, device_manager, driver::Driver, Device, DeviceCommonData, DeviceState, 18 DeviceType, IdTable, 19 }, 20 kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState}, 21 kset::KSet, 22 platform::{ 23 platform_device::{platform_device_manager, PlatformDevice}, 24 platform_driver::{platform_driver_manager, PlatformDriver}, 25 }, 26 }, 27 serial::serial8250::send_to_default_serial8250_port, 28 video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual, FRAME_BUFFER_SET}, 29 }, 30 filesystem::{ 31 kernfs::KernFSInode, 32 sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport}, 33 vfs::syscall::ModeType, 34 }, 35 init::{boot::boot_callbacks, boot_params, initcall::INITCALL_DEVICE}, 36 libs::{ 37 once::Once, 38 rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 39 spinlock::{SpinLock, SpinLockGuard}, 40 }, 41 mm::{early_ioremap::EarlyIoRemap, PhysAddr, VirtAddr}, 42 }; 43 44 use super::base::{ 45 fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType, 46 FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo, 47 FrameBufferInfoData, FrameBufferOps, 48 }; 49 50 /// 当前机器上面是否有vesa帧缓冲区 51 static HAS_VESA_FB: AtomicBool = AtomicBool::new(false); 52 53 lazy_static! { 54 static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo { 55 id: FixedScreenInfo::name2id("VESA VGA"), 56 fb_type: FbType::PackedPixels, 57 accel: FbAccel::None, 58 ..Default::default() 59 }); 60 static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo { 61 activate: FbActivateFlags::FB_ACTIVATE_NOW, 62 height: None, 63 width: None, 64 right_margin: 32, 65 upper_margin: 16, 66 lower_margin: 4, 67 vsync_len: 4, 68 vmode: FbVModeFlags::FB_VMODE_NONINTERLACED, 69 70 ..Default::default() 71 }); 72 } 73 74 #[derive(Debug)] 75 #[cast_to([sync] Device)] 76 #[cast_to([sync] PlatformDevice)] 77 pub struct VesaFb { 78 inner: SpinLock<InnerVesaFb>, 79 kobj_state: LockedKObjectState, 80 fb_data: RwLock<FrameBufferInfoData>, 81 } 82 83 impl VesaFb { 84 pub const NAME: &'static str = "vesa_vga"; 85 pub fn new() -> Self { 86 let mut fb_info_data = FrameBufferInfoData::new(); 87 fb_info_data.pesudo_palette.resize(256, 0); 88 return Self { 89 inner: SpinLock::new(InnerVesaFb { 90 kobject_common: KObjectCommonData::default(), 91 device_common: DeviceCommonData::default(), 92 device_state: DeviceState::NotInitialized, 93 pdev_id: 0, 94 pdev_id_auto: false, 95 fb_id: FbId::INIT, 96 fb_device: None, 97 fb_state: FbState::Suspended, 98 }), 99 kobj_state: LockedKObjectState::new(None), 100 fb_data: RwLock::new(fb_info_data), 101 }; 102 } 103 104 fn inner(&self) -> SpinLockGuard<InnerVesaFb> { 105 self.inner.lock() 106 } 107 } 108 109 #[derive(Debug)] 110 struct InnerVesaFb { 111 kobject_common: KObjectCommonData, 112 device_common: DeviceCommonData, 113 device_state: DeviceState, 114 pdev_id: i32, 115 pdev_id_auto: bool, 116 fb_id: FbId, 117 fb_device: Option<Arc<FbDevice>>, 118 fb_state: FbState, 119 } 120 121 impl FrameBuffer for VesaFb { 122 fn fb_id(&self) -> FbId { 123 self.inner.lock().fb_id 124 } 125 126 fn set_fb_id(&self, id: FbId) { 127 self.inner.lock().fb_id = id; 128 } 129 } 130 131 impl PlatformDevice for VesaFb { 132 fn pdev_name(&self) -> &str { 133 Self::NAME 134 } 135 136 fn set_pdev_id(&self, id: i32) { 137 self.inner.lock().pdev_id = id; 138 } 139 140 fn set_pdev_id_auto(&self, id_auto: bool) { 141 self.inner.lock().pdev_id_auto = id_auto; 142 } 143 144 fn is_initialized(&self) -> bool { 145 self.inner.lock().device_state == DeviceState::Initialized 146 } 147 148 fn set_state(&self, set_state: DeviceState) { 149 self.inner.lock().device_state = set_state; 150 } 151 } 152 153 impl Device for VesaFb { 154 fn dev_type(&self) -> DeviceType { 155 DeviceType::Char 156 } 157 158 fn id_table(&self) -> IdTable { 159 IdTable::new(self.name(), None) 160 } 161 162 fn bus(&self) -> Option<Weak<dyn Bus>> { 163 self.inner().device_common.bus.clone() 164 } 165 166 fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 167 self.inner().device_common.bus = bus; 168 } 169 170 fn set_class(&self, class: Option<Weak<dyn Class>>) { 171 self.inner().device_common.class = class; 172 } 173 174 fn class(&self) -> Option<Arc<dyn Class>> { 175 let mut guard = self.inner(); 176 177 let r = guard.device_common.class.clone()?.upgrade(); 178 if r.is_none() { 179 // 为了让弱引用失效 180 guard.device_common.class = None; 181 } 182 183 return r; 184 } 185 186 fn driver(&self) -> Option<Arc<dyn Driver>> { 187 self.inner().device_common.driver.clone()?.upgrade() 188 } 189 190 fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 191 self.inner().device_common.driver = driver; 192 } 193 194 fn is_dead(&self) -> bool { 195 false 196 } 197 198 fn can_match(&self) -> bool { 199 true 200 } 201 202 fn set_can_match(&self, _can_match: bool) {} 203 204 fn state_synced(&self) -> bool { 205 true 206 } 207 208 fn dev_parent(&self) -> Option<Weak<dyn Device>> { 209 self.inner().device_common.get_parent_weak_or_clear() 210 } 211 212 fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) { 213 self.inner().device_common.parent = dev_parent; 214 } 215 } 216 217 impl KObject for VesaFb { 218 fn as_any_ref(&self) -> &dyn core::any::Any { 219 self 220 } 221 222 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 223 self.inner().kobject_common.kern_inode = inode; 224 } 225 226 fn inode(&self) -> Option<Arc<KernFSInode>> { 227 self.inner().kobject_common.kern_inode.clone() 228 } 229 230 fn parent(&self) -> Option<Weak<dyn KObject>> { 231 self.inner().kobject_common.parent.clone() 232 } 233 234 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 235 self.inner().kobject_common.parent = parent; 236 } 237 238 fn kset(&self) -> Option<Arc<KSet>> { 239 self.inner().kobject_common.kset.clone() 240 } 241 242 fn set_kset(&self, kset: Option<Arc<KSet>>) { 243 self.inner().kobject_common.kset = kset; 244 } 245 246 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 247 self.inner().kobject_common.kobj_type 248 } 249 250 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 251 self.inner().kobject_common.kobj_type = ktype; 252 } 253 254 fn name(&self) -> String { 255 Self::NAME.to_string() 256 } 257 258 fn set_name(&self, _name: String) { 259 // do nothing 260 } 261 262 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 263 self.kobj_state.read() 264 } 265 266 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 267 self.kobj_state.write() 268 } 269 270 fn set_kobj_state(&self, state: KObjectState) { 271 *self.kobj_state.write() = state; 272 } 273 } 274 275 impl FrameBufferOps for VesaFb { 276 fn fb_open(&self, _user: bool) { 277 todo!() 278 } 279 280 fn fb_release(&self, _user: bool) { 281 todo!() 282 } 283 284 fn fb_set_color_register( 285 &self, 286 regno: u16, 287 mut red: u16, 288 mut green: u16, 289 mut blue: u16, 290 ) -> Result<(), SystemError> { 291 let mut fb_data = self.framebuffer_info_data().write(); 292 let var = self.current_fb_var(); 293 if regno as usize >= fb_data.pesudo_palette.len() { 294 return Err(SystemError::E2BIG); 295 } 296 297 if var.bits_per_pixel == 8 { 298 todo!("vesa_setpalette todo"); 299 } else if regno < 16 { 300 match var.bits_per_pixel { 301 16 => { 302 if var.red.offset == 10 { 303 // RGB 1:5:5:5 304 fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1) 305 | ((green as u32 & 0xf800) >> 6) 306 | ((blue as u32 & 0xf800) >> 11); 307 } else { 308 fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800) 309 | ((green as u32 & 0xfc00) >> 5) 310 | ((blue as u32 & 0xf800) >> 11); 311 } 312 } 313 24 | 32 => { 314 red >>= 8; 315 green >>= 8; 316 blue >>= 8; 317 fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset) 318 | ((green as u32) << var.green.offset) 319 | ((blue as u32) << var.blue.offset); 320 } 321 _ => {} 322 } 323 } 324 325 Ok(()) 326 } 327 328 fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> { 329 todo!() 330 } 331 332 fn fb_destroy(&self) { 333 todo!() 334 } 335 336 fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> { 337 let bp = boot_params().read(); 338 339 let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 340 let size = self.current_fb_fix().smem_len; 341 drop(bp); 342 if pos >= size { 343 return Ok(0); 344 } 345 346 let pos = pos as i64; 347 let size = size as i64; 348 349 let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 350 351 let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) }; 352 buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]); 353 354 return Ok(len); 355 } 356 357 fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> { 358 let bp = boot_params().read(); 359 360 let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 361 let size = self.current_fb_fix().smem_len; 362 363 if pos >= size { 364 return Ok(0); 365 } 366 367 let pos = pos as i64; 368 let size = size as i64; 369 370 let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 371 372 let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) }; 373 slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]); 374 375 return Ok(len); 376 } 377 378 fn fb_image_blit(&self, image: &super::base::FbImage) { 379 self.generic_imageblit(image); 380 } 381 382 /// ## 填充矩形 383 fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> { 384 // warn!("rect {rect:?}"); 385 386 let boot_param = boot_params().read(); 387 let screen_base = boot_param 388 .screen_info 389 .lfb_virt_base 390 .ok_or(SystemError::ENODEV)?; 391 392 let fg = if self.current_fb_fix().visual == FbVisual::TrueColor 393 || self.current_fb_fix().visual == FbVisual::DirectColor 394 { 395 self.fb_data.read().pesudo_palette[rect.color as usize] 396 } else { 397 rect.color 398 }; 399 400 let bpp = self.current_fb_var().bits_per_pixel; 401 // 每行像素数 402 let line_offset = self.current_fb_var().xres; 403 match bpp { 404 32 => { 405 let base = screen_base.as_ptr::<u32>(); 406 407 for y in rect.dy..(rect.dy + rect.height) { 408 for x in rect.dx..(rect.dx + rect.width) { 409 unsafe { *base.add((y * line_offset + x) as usize) = fg }; 410 } 411 } 412 } 413 16 => { 414 let base = screen_base.as_ptr::<u16>(); 415 416 for y in rect.dy..(rect.dy + rect.height) { 417 for x in rect.dx..(rect.dx + rect.width) { 418 unsafe { *base.add((y * line_offset + x) as usize) = 0x0000 }; 419 } 420 } 421 } 422 24 => { 423 let base = screen_base.as_ptr::<[u8; 3]>(); 424 425 for y in rect.dy..(rect.dy + rect.height) { 426 for x in rect.dx..(rect.dx + rect.width) { 427 unsafe { *base.add((y * line_offset + x) as usize) = [0, 0, 0] }; 428 } 429 } 430 } 431 _ => { 432 send_to_default_serial8250_port( 433 format!("unsupported bit depth:{}!\n\0", bpp).as_bytes(), 434 ); 435 todo!() 436 } 437 } 438 439 Ok(()) 440 } 441 442 #[inline(never)] 443 fn fb_copyarea(&self, data: super::base::CopyAreaData) { 444 let bp = boot_params().read(); 445 let base = bp.screen_info.lfb_virt_base.unwrap(); 446 let var = self.current_fb_var(); 447 448 // 原区域或者目标区域全在屏幕外,则直接返回 449 if data.sx > var.xres as i32 450 || data.sy > var.yres as i32 451 || data.dx > var.xres as i32 452 || data.dy > var.yres as i32 453 || (data.sx + data.width as i32) < 0 454 || (data.sy + data.height as i32) < 0 455 || (data.dx + data.width as i32) < 0 456 || (data.dy + data.height as i32) < 0 457 { 458 return; 459 } 460 461 // 求两个矩形可视范围交集 462 let (s_visiable_x, s_w) = if data.sx < 0 { 463 (0, (data.width - ((-data.sx) as u32)).min(var.xres)) 464 } else { 465 let w = if data.sx as u32 + data.width > var.xres { 466 var.xres - data.sx as u32 467 } else { 468 data.width 469 }; 470 471 (data.sx, w) 472 }; 473 let (s_visiable_y, s_h) = if data.sy < 0 { 474 (0, (data.height - ((-data.sy) as u32).min(var.yres))) 475 } else { 476 let h = if data.sy as u32 + data.height > var.yres { 477 var.yres - data.sy as u32 478 } else { 479 data.height 480 }; 481 482 (data.sy, h) 483 }; 484 485 let (d_visiable_x, d_w) = if data.dx < 0 { 486 (0, (data.width - ((-data.dx) as u32)).min(var.xres)) 487 } else { 488 let w = if data.dx as u32 + data.width > var.xres { 489 var.xres - data.dx as u32 490 } else { 491 data.width 492 }; 493 494 (data.dx, w) 495 }; 496 let (d_visiable_y, d_h) = if data.dy < 0 { 497 (0, (data.height - ((-data.dy) as u32).min(var.yres))) 498 } else { 499 let h = if data.dy as u32 + data.height > var.yres { 500 var.yres - data.dy as u32 501 } else { 502 data.height 503 }; 504 505 (data.dy, h) 506 }; 507 508 // 可视范围无交集 509 if !(d_h + s_h > data.height && s_w + d_w > data.width) { 510 return; 511 } 512 513 // 可视区域左上角相对于矩形的坐标 514 let s_relative_x = s_visiable_x - data.sx; 515 let s_relative_y = s_visiable_y - data.sy; 516 let d_relative_x = d_visiable_x - data.dx; 517 let d_relative_y = d_visiable_y - data.dy; 518 519 let visiable_x = s_relative_x.max(d_relative_x); 520 let visiable_y = s_relative_y.max(d_relative_y); 521 let visiable_h = d_h + s_h - data.height; 522 let visiable_w = d_w + s_w - data.width; 523 524 let s_real_x = (visiable_x + data.sx) as u32; 525 let s_real_y = (visiable_y + data.sy) as u32; 526 let d_real_x = (visiable_x + data.dx) as u32; 527 let d_real_y = (visiable_y + data.dy) as u32; 528 529 let bytes_per_pixel = var.bits_per_pixel >> 3; 530 let bytes_per_line = var.xres * bytes_per_pixel; 531 532 let src = 533 base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize); 534 535 let dst = 536 base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize); 537 538 let size = (visiable_h * visiable_w) as usize; 539 540 match bytes_per_pixel { 541 4 => { 542 // 32bpp 543 let mut dst = dst.as_ptr::<u32>(); 544 let mut src = src.as_ptr::<u32>(); 545 let line_offset = var.xres as usize; 546 547 if s_real_x > d_real_x { 548 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 549 unsafe { 550 for _ in 0..visiable_h { 551 core::ptr::copy(src, dst, visiable_w as usize); 552 src = src.add(line_offset); 553 dst = dst.add(visiable_w as usize); 554 } 555 } 556 } else { 557 let mut tmp: Vec<u32> = vec![0; size]; 558 let mut tmp_ptr = tmp.as_mut_ptr(); 559 560 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 561 unsafe { 562 for _ in 0..visiable_h { 563 core::ptr::copy(src, tmp_ptr, visiable_w as usize); 564 src = src.add(line_offset); 565 tmp_ptr = tmp_ptr.add(visiable_w as usize); 566 } 567 568 tmp_ptr = tmp_ptr.sub(size); 569 for _ in 0..visiable_h { 570 core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 571 dst = dst.add(line_offset); 572 tmp_ptr = tmp_ptr.add(visiable_w as usize); 573 } 574 } 575 } 576 } 577 2 => { 578 let mut dst = dst.as_ptr::<u16>(); 579 let mut src = src.as_ptr::<u16>(); 580 let line_offset = var.xres as usize; 581 582 if s_real_x > d_real_x { 583 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 584 unsafe { 585 for _ in 0..visiable_h { 586 core::ptr::copy(src, dst, visiable_w as usize); 587 src = src.add(line_offset); 588 dst = dst.add(visiable_w as usize); 589 } 590 } 591 } else { 592 let mut tmp: Vec<u16> = vec![0; size]; 593 let mut tmp_ptr = tmp.as_mut_ptr(); 594 595 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 596 unsafe { 597 for _ in 0..visiable_h { 598 core::ptr::copy(src, tmp_ptr, visiable_w as usize); 599 src = src.add(line_offset); 600 tmp_ptr = tmp_ptr.add(visiable_w as usize); 601 } 602 603 tmp_ptr = tmp_ptr.sub(size); 604 for _ in 0..visiable_h { 605 core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 606 dst = dst.add(line_offset); 607 tmp_ptr = tmp_ptr.add(visiable_w as usize); 608 } 609 } 610 } 611 } 612 3 => { 613 let mut dst = dst.as_ptr::<[u8; 3]>(); 614 let mut src = src.as_ptr::<[u8; 3]>(); 615 let line_offset = var.xres as usize; 616 617 if s_real_x > d_real_x { 618 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 619 unsafe { 620 for _ in 0..visiable_h { 621 core::ptr::copy(src, dst, visiable_w as usize); 622 src = src.add(line_offset); 623 dst = dst.add(visiable_w as usize); 624 } 625 } 626 } else { 627 let mut tmp: Vec<u32> = vec![0; size]; 628 let mut tmp_ptr = tmp.as_mut_ptr() as *mut [u8; 3]; 629 630 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 631 unsafe { 632 for _ in 0..visiable_h { 633 core::ptr::copy(src, tmp_ptr, visiable_w as usize); 634 src = src.add(line_offset); 635 tmp_ptr = tmp_ptr.add(visiable_w as usize); 636 } 637 638 tmp_ptr = tmp_ptr.sub(size); 639 for _ in 0..visiable_h { 640 core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 641 dst = dst.add(line_offset); 642 tmp_ptr = tmp_ptr.add(visiable_w as usize); 643 } 644 } 645 } 646 } 647 648 _ => { 649 send_to_default_serial8250_port( 650 format!("bytes_per_pixel:{}\n\0", bytes_per_pixel).as_bytes(), 651 ); 652 todo!() 653 } 654 } 655 } 656 } 657 658 impl FrameBufferInfo for VesaFb { 659 fn fb_device(&self) -> Option<Arc<FbDevice>> { 660 self.inner.lock().fb_device.clone() 661 } 662 663 fn set_fb_device(&self, device: Option<Arc<FbDevice>>) { 664 self.inner.lock().fb_device = device; 665 } 666 667 fn screen_size(&self) -> usize { 668 todo!() 669 } 670 671 fn current_fb_var(&self) -> FbVarScreenInfo { 672 *VESAFB_DEFINED.read() 673 } 674 675 fn current_fb_fix(&self) -> FixedScreenInfo { 676 *VESAFB_FIX_INFO.read() 677 } 678 679 fn video_mode(&self) -> Option<&FbVideoMode> { 680 todo!() 681 } 682 683 fn state(&self) -> FbState { 684 self.inner.lock().fb_state 685 } 686 687 fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> { 688 &self.fb_data 689 } 690 } 691 692 #[derive(Debug)] 693 #[cast_to([sync] PlatformDriver)] 694 struct VesaFbDriver { 695 inner: SpinLock<InnerVesaFbDriver>, 696 kobj_state: LockedKObjectState, 697 } 698 699 impl VesaFbDriver { 700 pub fn new() -> Arc<Self> { 701 let r = Arc::new(Self { 702 inner: SpinLock::new(InnerVesaFbDriver { 703 ktype: None, 704 kset: None, 705 parent: None, 706 kernfs_inode: None, 707 devices: Vec::new(), 708 bus: None, 709 self_ref: Weak::new(), 710 }), 711 kobj_state: LockedKObjectState::new(None), 712 }); 713 714 r.inner.lock().self_ref = Arc::downgrade(&r); 715 716 return r; 717 } 718 } 719 720 #[derive(Debug)] 721 struct InnerVesaFbDriver { 722 ktype: Option<&'static dyn KObjType>, 723 kset: Option<Arc<KSet>>, 724 parent: Option<Weak<dyn KObject>>, 725 kernfs_inode: Option<Arc<KernFSInode>>, 726 devices: Vec<Arc<dyn Device>>, 727 bus: Option<Weak<dyn Bus>>, 728 729 self_ref: Weak<VesaFbDriver>, 730 } 731 732 impl VesaFbDriver { 733 const NAME: &'static str = "vesa-framebuffer"; 734 } 735 736 impl PlatformDriver for VesaFbDriver { 737 fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 738 let device = device 739 .clone() 740 .arc_any() 741 .downcast::<VesaFb>() 742 .map_err(|_| SystemError::EINVAL)?; 743 744 device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone())); 745 746 return Ok(()); 747 } 748 749 fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 750 todo!() 751 } 752 753 fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 754 // do nothing 755 return Ok(()); 756 } 757 758 fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 759 // do nothing 760 return Ok(()); 761 } 762 763 fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 764 todo!() 765 } 766 } 767 768 impl Driver for VesaFbDriver { 769 fn id_table(&self) -> Option<IdTable> { 770 Some(IdTable::new(VesaFb::NAME.to_string(), None)) 771 } 772 773 fn devices(&self) -> Vec<Arc<dyn Device>> { 774 self.inner.lock().devices.clone() 775 } 776 777 fn add_device(&self, device: Arc<dyn Device>) { 778 let mut guard = self.inner.lock(); 779 // check if the device is already in the list 780 if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) { 781 return; 782 } 783 784 guard.devices.push(device); 785 } 786 787 fn delete_device(&self, device: &Arc<dyn Device>) { 788 let mut guard = self.inner.lock(); 789 guard.devices.retain(|dev| !Arc::ptr_eq(dev, device)); 790 } 791 792 fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 793 self.inner.lock().bus = bus; 794 } 795 796 fn bus(&self) -> Option<Weak<dyn Bus>> { 797 self.inner.lock().bus.clone() 798 } 799 800 fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 801 return &[&VesaFbAnonAttributeGroup]; 802 } 803 } 804 805 impl KObject for VesaFbDriver { 806 fn as_any_ref(&self) -> &dyn core::any::Any { 807 self 808 } 809 810 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 811 self.inner.lock().kernfs_inode = inode; 812 } 813 814 fn inode(&self) -> Option<Arc<KernFSInode>> { 815 self.inner.lock().kernfs_inode.clone() 816 } 817 818 fn parent(&self) -> Option<Weak<dyn KObject>> { 819 self.inner.lock().parent.clone() 820 } 821 822 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 823 self.inner.lock().parent = parent; 824 } 825 826 fn kset(&self) -> Option<Arc<KSet>> { 827 self.inner.lock().kset.clone() 828 } 829 830 fn set_kset(&self, kset: Option<Arc<KSet>>) { 831 self.inner.lock().kset = kset; 832 } 833 834 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 835 self.inner.lock().ktype 836 } 837 838 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 839 self.inner.lock().ktype = ktype; 840 } 841 842 fn name(&self) -> String { 843 Self::NAME.to_string() 844 } 845 846 fn set_name(&self, _name: String) { 847 // do nothing 848 } 849 850 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 851 self.kobj_state.read() 852 } 853 854 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 855 self.kobj_state.write() 856 } 857 858 fn set_kobj_state(&self, state: KObjectState) { 859 *self.kobj_state.write() = state; 860 } 861 } 862 863 #[derive(Debug)] 864 struct VesaFbAnonAttributeGroup; 865 866 impl AttributeGroup for VesaFbAnonAttributeGroup { 867 fn name(&self) -> Option<&str> { 868 None 869 } 870 871 fn attrs(&self) -> &[&'static dyn Attribute] { 872 &[&AnonAttrPhysAddr as &'static dyn Attribute] 873 } 874 875 fn is_visible( 876 &self, 877 _kobj: Arc<dyn KObject>, 878 attr: &'static dyn Attribute, 879 ) -> Option<ModeType> { 880 Some(attr.mode()) 881 } 882 } 883 884 #[derive(Debug)] 885 struct AnonAttrPhysAddr; 886 887 impl Attribute for AnonAttrPhysAddr { 888 fn name(&self) -> &str { 889 "smem_start" 890 } 891 892 fn mode(&self) -> ModeType { 893 ModeType::S_IRUGO 894 } 895 896 fn support(&self) -> SysFSOpsSupport { 897 SysFSOpsSupport::ATTR_SHOW 898 } 899 900 fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 901 sysfs_emit_str( 902 buf, 903 format!( 904 "0x{:x}\n", 905 VESAFB_FIX_INFO 906 .read() 907 .smem_start 908 .unwrap_or(PhysAddr::new(0)) 909 .data() 910 ) 911 .as_str(), 912 ) 913 } 914 } 915 916 #[unified_init(INITCALL_DEVICE)] 917 pub fn vesa_fb_driver_init() -> Result<(), SystemError> { 918 let driver = VesaFbDriver::new(); 919 920 platform_driver_manager().register(driver)?; 921 922 return Ok(()); 923 } 924 925 /// 在内存管理初始化之前,初始化vesafb 926 pub fn vesafb_early_init() -> Result<(), SystemError> { 927 let mut boot_params_guard = boot_params().write(); 928 boot_callbacks().early_init_framebuffer_info(&mut boot_params_guard.screen_info)?; 929 930 HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst); 931 932 return Ok(()); 933 } 934 935 pub fn vesafb_early_map(paddr: PhysAddr, size: usize) -> Result<VirtAddr, SystemError> { 936 let (buf_vaddr, _) = EarlyIoRemap::map(paddr, size, false)?; 937 938 return Ok(buf_vaddr); 939 } 940 941 #[unified_init(INITCALL_DEVICE)] 942 fn vesa_fb_device_init() -> Result<(), SystemError> { 943 // 如果没有vesa帧缓冲区,直接返回 944 if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) { 945 return Ok(()); 946 } 947 948 static INIT: Once = Once::new(); 949 INIT.call_once(|| { 950 info!("vesa fb device init"); 951 let device = Arc::new(VesaFb::new()); 952 953 let mut fb_fix = VESAFB_FIX_INFO.write_irqsave(); 954 let mut fb_var = VESAFB_DEFINED.write_irqsave(); 955 956 let boot_params_guard = boot_params().read(); 957 let boottime_screen_info = &boot_params_guard.screen_info; 958 959 fb_fix.smem_start = Some(boottime_screen_info.lfb_base); 960 fb_fix.smem_len = boottime_screen_info.lfb_size; 961 962 if boottime_screen_info.video_type == BootTimeVideoType::Mda { 963 fb_fix.visual = FbVisual::Mono10; 964 fb_var.bits_per_pixel = 8; 965 fb_fix.line_length = 966 (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8); 967 fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32; 968 fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32; 969 } else { 970 fb_fix.visual = FbVisual::TrueColor; 971 fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32; 972 fb_fix.line_length = 973 (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8); 974 fb_var.xres_virtual = boottime_screen_info.lfb_width as u32; 975 fb_var.yres_virtual = boottime_screen_info.lfb_height as u32; 976 fb_var.xres = boottime_screen_info.lfb_width as u32; 977 fb_var.yres = boottime_screen_info.lfb_height as u32; 978 } 979 980 fb_var.red.length = boottime_screen_info.red_size as u32; 981 fb_var.green.length = boottime_screen_info.green_size as u32; 982 fb_var.blue.length = boottime_screen_info.blue_size as u32; 983 984 fb_var.red.offset = boottime_screen_info.red_pos as u32; 985 fb_var.green.offset = boottime_screen_info.green_pos as u32; 986 fb_var.blue.offset = boottime_screen_info.blue_pos as u32; 987 988 // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面 989 fb_var.red.offset = 16; 990 fb_var.green.offset = 8; 991 fb_var.blue.offset = 0; 992 993 if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 { 994 fb_var.red.length = fb_var.bits_per_pixel; 995 fb_var.green.length = fb_var.bits_per_pixel; 996 fb_var.blue.length = fb_var.bits_per_pixel; 997 } 998 999 device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>)); 1000 1001 platform_device_manager() 1002 .device_add(device.clone() as Arc<dyn PlatformDevice>) 1003 .expect("vesa_fb_device_init: platform_device_manager().device_add failed"); 1004 1005 frame_buffer_manager() 1006 .register_fb(device.clone() as Arc<dyn FrameBuffer>) 1007 .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed"); 1008 1009 // 加入全局fb表 1010 let mut guard = FRAME_BUFFER_SET.write(); 1011 if guard.get(device.fb_id().data() as usize).unwrap().is_some() { 1012 warn!( 1013 "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET", 1014 device.fb_id() 1015 ); 1016 } 1017 guard[device.fb_id().data() as usize] = Some(device.clone()); 1018 1019 // 设置vesa fb的状态为运行中 1020 device.inner.lock().fb_state = FbState::Running; 1021 }); 1022 1023 return Ok(()); 1024 } 1025