xref: /DragonOS/kernel/src/driver/video/fbdev/base/fbmem.rs (revision 08a2ee408498b0db4c76c57b149f1cf047758f3c)
1 use alloc::{
2     string::ToString,
3     sync::{Arc, Weak},
4 };
5 
6 use crate::{
7     driver::base::{
8         class::{class_manager, Class},
9         device::sys_dev_char_kset,
10         kobject::KObject,
11         subsys::SubSysPrivate,
12     },
13     syscall::SystemError,
14 };
15 
16 use super::fbcon::fb_console_init;
17 
18 /// `/sys/class/graphics` 的 class 实例
19 static mut CLASS_GRAPHICS_INSTANCE: Option<Arc<GraphicsClass>> = None;
20 
21 /// 获取 `/sys/class/graphics` 的 class 实例
22 #[inline(always)]
23 #[allow(dead_code)]
24 pub fn sys_class_graphics_instance() -> Option<&'static Arc<GraphicsClass>> {
25     unsafe { CLASS_GRAPHICS_INSTANCE.as_ref() }
26 }
27 
28 /// 初始化帧缓冲区子系统
29 pub fn fbmem_init() -> Result<(), SystemError> {
30     let graphics_class = GraphicsClass::new();
31     class_manager().class_register(&(graphics_class.clone() as Arc<dyn Class>))?;
32 
33     unsafe {
34         CLASS_GRAPHICS_INSTANCE = Some(graphics_class);
35     }
36 
37     fb_console_init()?;
38     return Ok(());
39 }
40 
41 /// `/sys/class/graphics` 类
42 #[derive(Debug)]
43 pub struct GraphicsClass {
44     subsystem: SubSysPrivate,
45 }
46 
47 impl GraphicsClass {
48     const NAME: &'static str = "graphics";
49     pub fn new() -> Arc<Self> {
50         let r = Self {
51             subsystem: SubSysPrivate::new(Self::NAME.to_string(), None, None, &[]),
52         };
53         let r = Arc::new(r);
54         r.subsystem()
55             .set_class(Some(Arc::downgrade(&r) as Weak<dyn Class>));
56 
57         return r;
58     }
59 }
60 
61 impl Class for GraphicsClass {
62     fn name(&self) -> &'static str {
63         return Self::NAME;
64     }
65 
66     fn dev_kobj(&self) -> Option<Arc<dyn KObject>> {
67         Some(sys_dev_char_kset() as Arc<dyn KObject>)
68     }
69 
70     fn set_dev_kobj(&self, _kobj: Arc<dyn KObject>) {
71         unimplemented!("GraphicsClass::set_dev_kobj");
72     }
73 
74     fn subsystem(&self) -> &SubSysPrivate {
75         return &self.subsystem;
76     }
77 }
78