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