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