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