1c566df45SLoGin use core::intrinsics::unlikely; 2c566df45SLoGin 308a2ee40SLoGin use alloc::{ 4c566df45SLoGin string::{String, ToString}, 508a2ee40SLoGin sync::{Arc, Weak}, 602343d0bSLoGin vec::Vec, 708a2ee40SLoGin }; 8c566df45SLoGin 9*2eab6dd7S曾俊 use log::error; 1091e9d4abSLoGin use system_error::SystemError; 1191e9d4abSLoGin use unified_init::macros::unified_init; 1208a2ee40SLoGin 13c566df45SLoGin use crate::{ 14c566df45SLoGin driver::base::{ 1508a2ee40SLoGin class::{class_manager, Class}, 16c566df45SLoGin device::{ 17c566df45SLoGin bus::Bus, 18c566df45SLoGin device_manager, 19c566df45SLoGin device_number::{DeviceNumber, Major}, 20c566df45SLoGin driver::Driver, 21c566df45SLoGin sys_dev_char_kset, Device, DeviceType, IdTable, 22c566df45SLoGin }, 23c566df45SLoGin kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 24c566df45SLoGin kset::KSet, 2508a2ee40SLoGin subsys::SubSysPrivate, 26c566df45SLoGin }, 2702343d0bSLoGin filesystem::{ 2802343d0bSLoGin devfs::{devfs_register, DevFS, DeviceINode}, 2902343d0bSLoGin kernfs::KernFSInode, 3002343d0bSLoGin sysfs::AttributeGroup, 3102343d0bSLoGin vfs::{ 3202343d0bSLoGin file::FileMode, syscall::ModeType, FilePrivateData, FileSystem, FileType, IndexNode, 3302343d0bSLoGin Metadata, 3402343d0bSLoGin }, 3502343d0bSLoGin }, 36c566df45SLoGin init::initcall::INITCALL_SUBSYS, 37c566df45SLoGin libs::{ 38c566df45SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 3902343d0bSLoGin spinlock::{SpinLock, SpinLockGuard}, 40c566df45SLoGin }, 4108a2ee40SLoGin }; 4208a2ee40SLoGin 43c566df45SLoGin use super::{fbcon::fb_console_init, fbsysfs::FbDeviceAttrGroup, FbId, FrameBuffer}; 4408a2ee40SLoGin 4508a2ee40SLoGin /// `/sys/class/graphics` 的 class 实例 4608a2ee40SLoGin static mut CLASS_GRAPHICS_INSTANCE: Option<Arc<GraphicsClass>> = None; 4708a2ee40SLoGin 48c566df45SLoGin lazy_static! { 49c566df45SLoGin /// 帧缓冲区管理器 50c566df45SLoGin static ref FRAME_BUFFER_MANAGER: FrameBufferManager = FrameBufferManager::new(); 51c566df45SLoGin } 52c566df45SLoGin 5308a2ee40SLoGin /// 获取 `/sys/class/graphics` 的 class 实例 5408a2ee40SLoGin #[inline(always)] 5508a2ee40SLoGin #[allow(dead_code)] 5608a2ee40SLoGin pub fn sys_class_graphics_instance() -> Option<&'static Arc<GraphicsClass>> { 5708a2ee40SLoGin unsafe { CLASS_GRAPHICS_INSTANCE.as_ref() } 5808a2ee40SLoGin } 5908a2ee40SLoGin 60c566df45SLoGin #[inline(always)] 61c566df45SLoGin pub fn frame_buffer_manager() -> &'static FrameBufferManager { 62c566df45SLoGin &FRAME_BUFFER_MANAGER 63c566df45SLoGin } 64c566df45SLoGin 6508a2ee40SLoGin /// 初始化帧缓冲区子系统 66c566df45SLoGin #[unified_init(INITCALL_SUBSYS)] 6708a2ee40SLoGin pub fn fbmem_init() -> Result<(), SystemError> { 6808a2ee40SLoGin let graphics_class = GraphicsClass::new(); 6908a2ee40SLoGin class_manager().class_register(&(graphics_class.clone() as Arc<dyn Class>))?; 7008a2ee40SLoGin 7108a2ee40SLoGin unsafe { 7208a2ee40SLoGin CLASS_GRAPHICS_INSTANCE = Some(graphics_class); 7308a2ee40SLoGin } 7408a2ee40SLoGin 7508a2ee40SLoGin fb_console_init()?; 7608a2ee40SLoGin return Ok(()); 7708a2ee40SLoGin } 7808a2ee40SLoGin 7908a2ee40SLoGin /// `/sys/class/graphics` 类 8008a2ee40SLoGin #[derive(Debug)] 8108a2ee40SLoGin pub struct GraphicsClass { 8208a2ee40SLoGin subsystem: SubSysPrivate, 8308a2ee40SLoGin } 8408a2ee40SLoGin 8508a2ee40SLoGin impl GraphicsClass { 8608a2ee40SLoGin const NAME: &'static str = "graphics"; 8708a2ee40SLoGin pub fn new() -> Arc<Self> { 8808a2ee40SLoGin let r = Self { 8908a2ee40SLoGin subsystem: SubSysPrivate::new(Self::NAME.to_string(), None, None, &[]), 9008a2ee40SLoGin }; 9108a2ee40SLoGin let r = Arc::new(r); 9208a2ee40SLoGin r.subsystem() 9308a2ee40SLoGin .set_class(Some(Arc::downgrade(&r) as Weak<dyn Class>)); 9408a2ee40SLoGin 9508a2ee40SLoGin return r; 9608a2ee40SLoGin } 9708a2ee40SLoGin } 9808a2ee40SLoGin 9908a2ee40SLoGin impl Class for GraphicsClass { 10008a2ee40SLoGin fn name(&self) -> &'static str { 10108a2ee40SLoGin return Self::NAME; 10208a2ee40SLoGin } 10308a2ee40SLoGin 10408a2ee40SLoGin fn dev_kobj(&self) -> Option<Arc<dyn KObject>> { 10508a2ee40SLoGin Some(sys_dev_char_kset() as Arc<dyn KObject>) 10608a2ee40SLoGin } 10708a2ee40SLoGin 10808a2ee40SLoGin fn set_dev_kobj(&self, _kobj: Arc<dyn KObject>) { 10908a2ee40SLoGin unimplemented!("GraphicsClass::set_dev_kobj"); 11008a2ee40SLoGin } 11108a2ee40SLoGin 11208a2ee40SLoGin fn subsystem(&self) -> &SubSysPrivate { 11308a2ee40SLoGin return &self.subsystem; 11408a2ee40SLoGin } 11508a2ee40SLoGin } 116c566df45SLoGin 117c566df45SLoGin /// 帧缓冲区管理器 118c566df45SLoGin #[derive(Debug)] 119c566df45SLoGin pub struct FrameBufferManager { 120c566df45SLoGin inner: RwLock<InnerFrameBufferManager>, 121c566df45SLoGin } 122c566df45SLoGin 123c566df45SLoGin #[derive(Debug)] 124c566df45SLoGin struct InnerFrameBufferManager { 125c566df45SLoGin /// 已经注册的帧缓冲区 126c566df45SLoGin registered_fbs: [Option<Arc<dyn FrameBuffer>>; FrameBufferManager::FB_MAX], 127c566df45SLoGin } 128c566df45SLoGin 129c566df45SLoGin impl FrameBufferManager { 130c566df45SLoGin pub const FB_MAX: usize = 32; 131c566df45SLoGin pub fn new() -> Self { 132c566df45SLoGin Self { 133c566df45SLoGin inner: RwLock::new(InnerFrameBufferManager { 134c566df45SLoGin registered_fbs: Default::default(), 135c566df45SLoGin }), 136c566df45SLoGin } 137c566df45SLoGin } 138c566df45SLoGin 139c566df45SLoGin /// 注册一个帧缓冲区 140c566df45SLoGin /// 141c566df45SLoGin /// # 参数 142c566df45SLoGin /// 143c566df45SLoGin /// - fb: 帧缓冲区 144c566df45SLoGin pub fn register_fb(&self, fb: Arc<dyn FrameBuffer>) -> Result<FbId, SystemError> { 145c566df45SLoGin let id = self.generate_fb_id().expect("no more fb id"); 146c566df45SLoGin fb.set_fb_id(id); 147c566df45SLoGin let fb_device = FbDevice::new(Arc::downgrade(&fb) as Weak<dyn FrameBuffer>, id); 148c566df45SLoGin device_manager().device_default_initialize(&(fb_device.clone() as Arc<dyn Device>)); 149c566df45SLoGin fb_device.set_parent(Some(Arc::downgrade(&(fb.clone() as Arc<dyn KObject>)))); 150c566df45SLoGin 151c566df45SLoGin fb.set_fb_device(Some(fb_device.clone())); 152c566df45SLoGin 153c566df45SLoGin device_manager().add_device(fb_device.clone() as Arc<dyn Device>)?; 15402343d0bSLoGin // 添加到devfs 15502343d0bSLoGin devfs_register(&fb_device.name(), fb_device.clone()).map_err(|e| { 156*2eab6dd7S曾俊 error!( 15702343d0bSLoGin "register fb device '{}' to devfs failed: {:?}", 15802343d0bSLoGin fb_device.name(), 15902343d0bSLoGin e 16002343d0bSLoGin ); 16102343d0bSLoGin device_manager().remove(&(fb_device.clone() as Arc<dyn Device>)); 16202343d0bSLoGin e 16302343d0bSLoGin })?; 164c566df45SLoGin 165c566df45SLoGin // todo: 从Modedb中获取信息 166c566df45SLoGin // 参考: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbmem.c#1584 167c566df45SLoGin 168c566df45SLoGin let mut inner = self.inner.write(); 169c566df45SLoGin inner.registered_fbs[id.data() as usize] = Some(fb.clone() as Arc<dyn FrameBuffer>); 170c566df45SLoGin 171c566df45SLoGin // todo: 把fb跟fbcon关联起来 172c566df45SLoGin return Ok(id); 173c566df45SLoGin } 174c566df45SLoGin 175c566df45SLoGin /// 注销一个帧缓冲区 176c566df45SLoGin /// 177c566df45SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbmem.c#1726 178c566df45SLoGin #[allow(dead_code)] 179c566df45SLoGin pub fn unregister_fb(&self, _fb: Arc<dyn FrameBuffer>) -> Result<(), SystemError> { 180c566df45SLoGin todo!("unregister_fb") 181c566df45SLoGin } 182c566df45SLoGin 183c566df45SLoGin /// 根据id查找帧缓冲区 184c566df45SLoGin pub fn find_fb_by_id(&self, id: FbId) -> Result<Option<Arc<dyn FrameBuffer>>, SystemError> { 185c566df45SLoGin if unlikely(!id.is_valid()) { 186c566df45SLoGin return Err(SystemError::EINVAL); 187c566df45SLoGin } 188c566df45SLoGin 189c566df45SLoGin let inner = self.inner.read(); 190c566df45SLoGin return Ok(inner.registered_fbs[id.data() as usize].clone()); 191c566df45SLoGin } 192c566df45SLoGin 193c566df45SLoGin fn generate_fb_id(&self) -> Option<FbId> { 194c566df45SLoGin for i in 0..Self::FB_MAX { 195c566df45SLoGin if self.inner.read().registered_fbs[i].is_none() { 196c566df45SLoGin return Some(FbId::new(i as u32)); 197c566df45SLoGin } 198c566df45SLoGin } 199c566df45SLoGin return None; 200c566df45SLoGin } 201c566df45SLoGin } 202c566df45SLoGin 203c566df45SLoGin /// 抽象的帧缓冲区设备 204c566df45SLoGin /// 205c566df45SLoGin /// 对应于`/sys/class/graphics/fb(x)`目录下的设备, 其中`(x)`为帧缓冲区的id 206c566df45SLoGin /// 207c566df45SLoGin /// 该设备的父设备为真实的帧缓冲区设备 208c566df45SLoGin #[derive(Debug)] 209c566df45SLoGin #[cast_to([sync] Device)] 210c566df45SLoGin pub struct FbDevice { 211c566df45SLoGin inner: SpinLock<InnerFbDevice>, 212c566df45SLoGin kobj_state: LockedKObjectState, 213c566df45SLoGin } 214c566df45SLoGin 215c566df45SLoGin impl FbDevice { 216c566df45SLoGin pub const BASENAME: &'static str = "fb"; 217c566df45SLoGin fn new(fb: Weak<dyn FrameBuffer>, id: FbId) -> Arc<Self> { 21802343d0bSLoGin let r = Arc::new(Self { 219c566df45SLoGin inner: SpinLock::new(InnerFbDevice { 220c566df45SLoGin fb, 221c566df45SLoGin kern_inode: None, 222c566df45SLoGin parent: None, 223c566df45SLoGin kset: None, 224c566df45SLoGin ktype: None, 225c566df45SLoGin fb_id: id, 22602343d0bSLoGin device_inode_fs: None, 22702343d0bSLoGin devfs_metadata: Metadata::new( 22802343d0bSLoGin FileType::FramebufferDevice, 22902343d0bSLoGin ModeType::from_bits_truncate(0o666), 23002343d0bSLoGin ), 231c566df45SLoGin }), 232c566df45SLoGin kobj_state: LockedKObjectState::new(None), 23302343d0bSLoGin }); 23402343d0bSLoGin 23502343d0bSLoGin let mut inner_guard = r.inner.lock(); 23602343d0bSLoGin 23702343d0bSLoGin inner_guard.devfs_metadata.raw_dev = r.do_device_number(&inner_guard); 23802343d0bSLoGin drop(inner_guard); 23902343d0bSLoGin 24002343d0bSLoGin return r; 241c566df45SLoGin } 242c566df45SLoGin 243c566df45SLoGin pub fn framebuffer(&self) -> Option<Arc<dyn FrameBuffer>> { 244c566df45SLoGin self.inner.lock().fb.upgrade() 245c566df45SLoGin } 24602343d0bSLoGin 24702343d0bSLoGin /// 获取设备号 24802343d0bSLoGin pub fn device_number(&self) -> DeviceNumber { 24902343d0bSLoGin let inner_guard = self.inner.lock(); 25002343d0bSLoGin self.do_device_number(&inner_guard) 25102343d0bSLoGin } 25202343d0bSLoGin 25302343d0bSLoGin fn do_device_number(&self, inner_guard: &SpinLockGuard<'_, InnerFbDevice>) -> DeviceNumber { 25402343d0bSLoGin DeviceNumber::new(Major::FB_MAJOR, inner_guard.fb_id.data()) 25502343d0bSLoGin } 256c566df45SLoGin } 257c566df45SLoGin 258c566df45SLoGin #[derive(Debug)] 259c566df45SLoGin struct InnerFbDevice { 260c566df45SLoGin fb: Weak<dyn FrameBuffer>, 261c566df45SLoGin kern_inode: Option<Arc<KernFSInode>>, 262c566df45SLoGin parent: Option<Weak<dyn KObject>>, 263c566df45SLoGin kset: Option<Arc<KSet>>, 264c566df45SLoGin ktype: Option<&'static dyn KObjType>, 265c566df45SLoGin /// 帧缓冲区id 266c566df45SLoGin fb_id: FbId, 26702343d0bSLoGin 26802343d0bSLoGin /// device inode要求的字段 26902343d0bSLoGin device_inode_fs: Option<Weak<DevFS>>, 27002343d0bSLoGin devfs_metadata: Metadata, 271c566df45SLoGin } 272c566df45SLoGin 273c566df45SLoGin impl KObject for FbDevice { 274c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 275c566df45SLoGin self 276c566df45SLoGin } 277c566df45SLoGin 278c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 279c566df45SLoGin self.inner.lock().kern_inode = inode; 280c566df45SLoGin } 281c566df45SLoGin 282c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 283c566df45SLoGin self.inner.lock().kern_inode.clone() 284c566df45SLoGin } 285c566df45SLoGin 286c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 287c566df45SLoGin self.inner.lock().parent.clone() 288c566df45SLoGin } 289c566df45SLoGin 290c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 291c566df45SLoGin self.inner.lock().parent = parent; 292c566df45SLoGin } 293c566df45SLoGin 294c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 295c566df45SLoGin self.inner.lock().kset.clone() 296c566df45SLoGin } 297c566df45SLoGin 298c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 299c566df45SLoGin self.inner.lock().kset = kset; 300c566df45SLoGin } 301c566df45SLoGin 302c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 303c566df45SLoGin self.inner.lock().ktype 304c566df45SLoGin } 305c566df45SLoGin 306c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 307c566df45SLoGin self.inner.lock().ktype = ktype; 308c566df45SLoGin } 309c566df45SLoGin 310c566df45SLoGin fn name(&self) -> String { 311c566df45SLoGin format!("{}{}", Self::BASENAME, self.inner.lock().fb_id.data()) 312c566df45SLoGin } 313c566df45SLoGin 314c566df45SLoGin fn set_name(&self, _name: String) { 315c566df45SLoGin // do nothing 316c566df45SLoGin } 317c566df45SLoGin 318c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 319c566df45SLoGin self.kobj_state.read() 320c566df45SLoGin } 321c566df45SLoGin 322c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 323c566df45SLoGin self.kobj_state.write() 324c566df45SLoGin } 325c566df45SLoGin 326c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 327c566df45SLoGin *self.kobj_state.write() = state; 328c566df45SLoGin } 329c566df45SLoGin } 330c566df45SLoGin 331c566df45SLoGin impl Device for FbDevice { 332c566df45SLoGin fn dev_type(&self) -> DeviceType { 333c566df45SLoGin DeviceType::Char 334c566df45SLoGin } 335c566df45SLoGin 336c566df45SLoGin fn id_table(&self) -> IdTable { 33702343d0bSLoGin IdTable::new(Self::BASENAME.to_string(), Some(self.device_number())) 338c566df45SLoGin } 339c566df45SLoGin 340c566df45SLoGin fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) { 341c566df45SLoGin todo!() 342c566df45SLoGin } 343c566df45SLoGin 344c566df45SLoGin fn class(&self) -> Option<Arc<dyn Class>> { 345c566df45SLoGin sys_class_graphics_instance().map(|ins| ins.clone() as Arc<dyn Class>) 346c566df45SLoGin } 3474256da7fSLoGin fn set_class(&self, _class: Option<Weak<dyn Class>>) { 348c566df45SLoGin // do nothing 349c566df45SLoGin } 350c566df45SLoGin 351c566df45SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 352c566df45SLoGin None 353c566df45SLoGin } 354c566df45SLoGin 355c566df45SLoGin fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) { 356c566df45SLoGin // do nothing 357c566df45SLoGin } 358c566df45SLoGin 359c566df45SLoGin fn is_dead(&self) -> bool { 360c566df45SLoGin false 361c566df45SLoGin } 362c566df45SLoGin 363c566df45SLoGin fn can_match(&self) -> bool { 364c566df45SLoGin false 365c566df45SLoGin } 366c566df45SLoGin 367c566df45SLoGin fn set_can_match(&self, _can_match: bool) { 368c566df45SLoGin // do nothing 369c566df45SLoGin } 370c566df45SLoGin 371c566df45SLoGin fn state_synced(&self) -> bool { 372c566df45SLoGin true 373c566df45SLoGin } 374c566df45SLoGin 375c566df45SLoGin fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> { 376c566df45SLoGin Some(&[&FbDeviceAttrGroup]) 377c566df45SLoGin } 378c566df45SLoGin } 37902343d0bSLoGin 38002343d0bSLoGin impl DeviceINode for FbDevice { 38102343d0bSLoGin fn set_fs(&self, fs: Weak<DevFS>) { 38202343d0bSLoGin self.inner.lock().device_inode_fs = Some(fs); 38302343d0bSLoGin } 38402343d0bSLoGin } 38502343d0bSLoGin 38602343d0bSLoGin impl IndexNode for FbDevice { 387dfe53cf0SGnoCiYeH fn open( 388dfe53cf0SGnoCiYeH &self, 389dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 390dfe53cf0SGnoCiYeH _mode: &FileMode, 391dfe53cf0SGnoCiYeH ) -> Result<(), SystemError> { 39202343d0bSLoGin Ok(()) 39302343d0bSLoGin } 39402343d0bSLoGin 395dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 39602343d0bSLoGin Ok(()) 39702343d0bSLoGin } 39802343d0bSLoGin fn read_at( 39902343d0bSLoGin &self, 40002343d0bSLoGin offset: usize, 40102343d0bSLoGin len: usize, 40202343d0bSLoGin buf: &mut [u8], 403dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 40402343d0bSLoGin ) -> Result<usize, SystemError> { 40502343d0bSLoGin let fb = self.inner.lock().fb.upgrade().unwrap(); 40602343d0bSLoGin return fb.fb_read(&mut buf[0..len], offset); 40702343d0bSLoGin } 40802343d0bSLoGin 40902343d0bSLoGin fn write_at( 41002343d0bSLoGin &self, 41102343d0bSLoGin offset: usize, 41202343d0bSLoGin len: usize, 41302343d0bSLoGin buf: &[u8], 414dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 41502343d0bSLoGin ) -> Result<usize, SystemError> { 41602343d0bSLoGin let fb = self.inner.lock().fb.upgrade().unwrap(); 41702343d0bSLoGin return fb.fb_write(&buf[0..len], offset); 41802343d0bSLoGin } 41902343d0bSLoGin 42002343d0bSLoGin fn fs(&self) -> Arc<dyn FileSystem> { 42102343d0bSLoGin self.inner 42202343d0bSLoGin .lock() 42302343d0bSLoGin .device_inode_fs 42402343d0bSLoGin .as_ref() 42502343d0bSLoGin .unwrap() 42602343d0bSLoGin .upgrade() 42702343d0bSLoGin .unwrap() 42802343d0bSLoGin } 42902343d0bSLoGin 43002343d0bSLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 43102343d0bSLoGin self 43202343d0bSLoGin } 43302343d0bSLoGin 43402343d0bSLoGin fn list(&self) -> Result<Vec<String>, SystemError> { 43502343d0bSLoGin todo!() 43602343d0bSLoGin } 43702343d0bSLoGin 43802343d0bSLoGin fn metadata(&self) -> Result<Metadata, SystemError> { 43902343d0bSLoGin Ok(self.inner.lock().devfs_metadata.clone()) 44002343d0bSLoGin } 44102343d0bSLoGin 44202343d0bSLoGin fn resize(&self, _len: usize) -> Result<(), SystemError> { 44302343d0bSLoGin return Ok(()); 44402343d0bSLoGin } 44502343d0bSLoGin } 446