xref: /DragonOS/kernel/src/driver/open_firmware/device_node.rs (revision f5b2038871d3441e1c7f32439ff422957e7ab828)
1 use crate::{
2     driver::base::{
3         kobject::{KObjType, KObject, KObjectState},
4         kset::KSet,
5     },
6     filesystem::{kernfs::KernFSInode, sysfs::BinAttribute},
7     libs::rwlock::{RwLockReadGuard, RwLockWriteGuard},
8     libs::spinlock::SpinLock,
9 };
10 use alloc::{
11     string::String,
12     sync::{Arc, Weak},
13     vec::Vec,
14 };
15 use core::fmt::Debug;
16 
17 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/of.h#51
18 #[allow(dead_code)]
19 #[derive(Debug)]
20 pub struct DeviceNode {
21     full_name: Option<&'static str>,
22     full_name_allocated: Option<String>,
23     inner: SpinLock<InnerDeviceNode>,
24 }
25 
26 #[allow(dead_code)]
27 #[derive(Debug)]
28 struct InnerDeviceNode {
29     properties: Vec<Property>,
30     parent: Weak<DeviceNode>,
31     children: Vec<Arc<DeviceNode>>,
32     sibling: Option<Weak<DeviceNode>>,
33     private_data: Option<Arc<dyn DeviceNodePrivateData>>,
34 }
35 
36 #[allow(dead_code)]
37 impl DeviceNode {
38     pub fn new(
39         full_name: Option<&'static str>,
40         full_name_allocated: Option<String>,
41     ) -> Option<Arc<Self>> {
42         if full_name.is_none() && full_name_allocated.is_none() {
43             return None;
44         }
45 
46         let x = DeviceNode {
47             full_name,
48             full_name_allocated,
49             inner: SpinLock::new(InnerDeviceNode {
50                 properties: Vec::new(),
51                 parent: Weak::new(),
52                 children: Vec::new(),
53                 sibling: None,
54                 private_data: None,
55             }),
56         };
57 
58         return Some(Arc::new(x));
59     }
60 
61     pub fn add_property(&self, prop: Property) {
62         self.inner.lock().properties.push(prop);
63     }
64 
65     pub fn properties(&self) -> Vec<Property> {
66         self.inner.lock().properties.clone()
67     }
68 
69     pub fn parent(&self) -> Option<Arc<DeviceNode>> {
70         self.inner.lock().parent.upgrade()
71     }
72 
73     pub fn set_parent(&self, parent: Arc<DeviceNode>) {
74         self.inner.lock().parent = Arc::downgrade(&parent);
75     }
76 
77     pub fn children(&self) -> Vec<Arc<DeviceNode>> {
78         self.inner.lock().children.clone()
79     }
80 
81     pub fn add_child(&self, child: Arc<DeviceNode>) {
82         self.inner.lock().children.push(child);
83     }
84 
85     pub fn sibling(&self) -> Option<Arc<DeviceNode>> {
86         self.inner.lock().sibling.as_ref().and_then(|s| s.upgrade())
87     }
88 
89     pub fn set_sibling(&self, sibling: Arc<DeviceNode>) {
90         self.inner.lock().sibling = Some(Arc::downgrade(&sibling));
91     }
92 
93     pub fn private_data(&self) -> Option<Arc<dyn DeviceNodePrivateData>> {
94         self.inner.lock().private_data.clone()
95     }
96 
97     pub fn set_private_data(&self, data: Arc<dyn DeviceNodePrivateData>) {
98         self.inner.lock().private_data = Some(data);
99     }
100 }
101 
102 pub trait DeviceNodePrivateData: Send + Sync + Debug {}
103 
104 impl KObject for DeviceNode {
105     fn as_any_ref(&self) -> &dyn core::any::Any {
106         self
107     }
108 
109     fn set_inode(&self, _inode: Option<Arc<KernFSInode>>) {
110         todo!()
111     }
112 
113     fn inode(&self) -> Option<Arc<KernFSInode>> {
114         todo!()
115     }
116 
117     fn parent(&self) -> Option<Weak<dyn KObject>> {
118         todo!()
119     }
120 
121     fn set_parent(&self, _parent: Option<Weak<dyn KObject>>) {
122         todo!()
123     }
124 
125     fn kset(&self) -> Option<Arc<KSet>> {
126         todo!()
127     }
128 
129     fn set_kset(&self, _kset: Option<Arc<KSet>>) {
130         todo!()
131     }
132 
133     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
134         todo!()
135     }
136 
137     fn set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>) {
138         todo!()
139     }
140 
141     fn name(&self) -> String {
142         todo!()
143     }
144 
145     fn set_name(&self, _name: String) {}
146 
147     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
148         todo!()
149     }
150 
151     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
152         todo!()
153     }
154 
155     fn set_kobj_state(&self, _state: KObjectState) {
156         todo!()
157     }
158 }
159 
160 #[allow(dead_code)]
161 #[derive(Debug, Clone)]
162 pub struct Property {
163     name: String,
164     value: Vec<u8>,
165     bin_attr: Option<Arc<dyn BinAttribute>>,
166 }
167 
168 impl Property {
169     #[allow(dead_code)]
170     pub const fn new(name: String, value: Vec<u8>, battr: Option<Arc<dyn BinAttribute>>) -> Self {
171         Property {
172             name,
173             value,
174             bin_attr: battr,
175         }
176     }
177 }
178