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