xref: /DragonOS/kernel/src/driver/base/kobject.rs (revision 7eda31b2f07c6ef41dc0d2bd13051f0fce5e5976)
106d5e247SLoGin use core::{any::Any, fmt::Debug, hash::Hash, ops::Deref};
206d5e247SLoGin 
306d5e247SLoGin use alloc::{
406d5e247SLoGin     string::String,
506d5e247SLoGin     sync::{Arc, Weak},
606d5e247SLoGin };
706d5e247SLoGin use intertrait::CastFromSync;
806d5e247SLoGin 
906d5e247SLoGin use crate::{
1006d5e247SLoGin     filesystem::{
1106d5e247SLoGin         kernfs::KernFSInode,
1206d5e247SLoGin         sysfs::{sysfs_instance, Attribute, AttributeGroup, SysFSOps, SysFSOpsSupport},
1306d5e247SLoGin     },
1406d5e247SLoGin     kerror,
1506d5e247SLoGin     libs::{
1606d5e247SLoGin         casting::DowncastArc,
1706d5e247SLoGin         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
1806d5e247SLoGin     },
1906d5e247SLoGin     syscall::SystemError,
2006d5e247SLoGin };
2106d5e247SLoGin 
2206d5e247SLoGin use super::kset::KSet;
2306d5e247SLoGin 
2406d5e247SLoGin pub trait KObject: Any + Send + Sync + Debug + CastFromSync {
2506d5e247SLoGin     fn as_any_ref(&self) -> &dyn core::any::Any;
2606d5e247SLoGin 
2706d5e247SLoGin     /// 设置当前kobject对应的sysfs inode(类型为KernFSInode)
2806d5e247SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>);
2906d5e247SLoGin 
3006d5e247SLoGin     /// 获取当前kobject对应的sysfs inode(类型为KernFSInode)
3106d5e247SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>>;
3206d5e247SLoGin 
3306d5e247SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>>;
3406d5e247SLoGin 
3506d5e247SLoGin     /// 设置当前kobject的parent kobject(不一定与kset相同)
3606d5e247SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>);
3706d5e247SLoGin 
3806d5e247SLoGin     /// 当前kobject属于哪个kset
3906d5e247SLoGin     fn kset(&self) -> Option<Arc<KSet>>;
4006d5e247SLoGin 
4106d5e247SLoGin     /// 设置当前kobject所属的kset
4206d5e247SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>);
4306d5e247SLoGin 
4406d5e247SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType>;
4506d5e247SLoGin 
46a03c4f9dSLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>);
47a03c4f9dSLoGin 
4806d5e247SLoGin     fn name(&self) -> String;
4906d5e247SLoGin 
5006d5e247SLoGin     fn set_name(&self, name: String);
5106d5e247SLoGin 
5206d5e247SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState>;
5306d5e247SLoGin 
5406d5e247SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>;
5506d5e247SLoGin 
5606d5e247SLoGin     fn set_kobj_state(&self, state: KObjectState);
5706d5e247SLoGin }
5806d5e247SLoGin 
5906d5e247SLoGin impl dyn KObject {
6006d5e247SLoGin     /// 更新kobject的状态
6106d5e247SLoGin     pub fn update_kobj_state(&self, insert: Option<KObjectState>, remove: Option<KObjectState>) {
6206d5e247SLoGin         let insert = insert.unwrap_or(KObjectState::empty());
6306d5e247SLoGin         let remove = remove.unwrap_or(KObjectState::empty());
6406d5e247SLoGin         let mut state = self.kobj_state_mut();
6506d5e247SLoGin         *state = (*state | insert) & !remove;
6606d5e247SLoGin     }
6706d5e247SLoGin }
6806d5e247SLoGin 
6906d5e247SLoGin impl DowncastArc for dyn KObject {
7006d5e247SLoGin     fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any> {
7106d5e247SLoGin         self
7206d5e247SLoGin     }
7306d5e247SLoGin }
7406d5e247SLoGin 
75a03c4f9dSLoGin pub trait KObjType: Debug + Send + Sync {
7606d5e247SLoGin     fn release(&self, _kobj: Arc<dyn KObject>) {}
7706d5e247SLoGin     fn sysfs_ops(&self) -> Option<&dyn SysFSOps>;
7806d5e247SLoGin 
7906d5e247SLoGin     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]>;
8006d5e247SLoGin }
8106d5e247SLoGin 
8206d5e247SLoGin bitflags! {
8306d5e247SLoGin     pub struct KObjectState: u32 {
8406d5e247SLoGin         const IN_SYSFS = 1 << 0;
8506d5e247SLoGin         const ADD_UEVENT_SENT = 1 << 1;
8606d5e247SLoGin         const REMOVE_UEVENT_SENT = 1 << 2;
8706d5e247SLoGin         const INITIALIZED = 1 << 3;
8806d5e247SLoGin     }
8906d5e247SLoGin 
9006d5e247SLoGin }
9106d5e247SLoGin 
9206d5e247SLoGin #[derive(Debug)]
9306d5e247SLoGin pub struct LockedKObjectState(RwLock<KObjectState>);
9406d5e247SLoGin 
9506d5e247SLoGin impl LockedKObjectState {
96a03c4f9dSLoGin     pub fn new(state: Option<KObjectState>) -> LockedKObjectState {
97a03c4f9dSLoGin         let state = state.unwrap_or(KObjectState::empty());
9806d5e247SLoGin         LockedKObjectState(RwLock::new(state))
9906d5e247SLoGin     }
10006d5e247SLoGin }
10106d5e247SLoGin 
10206d5e247SLoGin impl Deref for LockedKObjectState {
10306d5e247SLoGin     type Target = RwLock<KObjectState>;
10406d5e247SLoGin 
10506d5e247SLoGin     fn deref(&self) -> &Self::Target {
10606d5e247SLoGin         &self.0
10706d5e247SLoGin     }
10806d5e247SLoGin }
10906d5e247SLoGin 
11006d5e247SLoGin pub trait KObjectAttribute: Attribute {
11106d5e247SLoGin     fn support(&self) -> SysFSOpsSupport;
11206d5e247SLoGin 
11306d5e247SLoGin     fn show(&self, kobj: &dyn KObject, buf: &mut [u8]) -> Result<usize, SystemError>;
11406d5e247SLoGin     fn store(&self, kobj: &dyn KObject, buf: &[u8]) -> Result<usize, SystemError>;
11506d5e247SLoGin }
11606d5e247SLoGin 
11706d5e247SLoGin #[derive(Debug)]
11806d5e247SLoGin pub struct KObjectSysFSOps;
11906d5e247SLoGin 
12006d5e247SLoGin impl SysFSOps for KObjectSysFSOps {
12106d5e247SLoGin     fn support(&self, attr: &dyn Attribute) -> SysFSOpsSupport {
12206d5e247SLoGin         return attr.support();
12306d5e247SLoGin     }
12406d5e247SLoGin 
12506d5e247SLoGin     fn show(
12606d5e247SLoGin         &self,
12706d5e247SLoGin         kobj: Arc<dyn KObject>,
12806d5e247SLoGin         attr: &dyn Attribute,
12906d5e247SLoGin         buf: &mut [u8],
13006d5e247SLoGin     ) -> Result<usize, SystemError> {
13106d5e247SLoGin         let r = attr.show(kobj, buf).map_err(|e| {
13206d5e247SLoGin             if e == SystemError::EOPNOTSUPP_OR_ENOTSUP {
13306d5e247SLoGin                 SystemError::EIO
13406d5e247SLoGin             } else {
13506d5e247SLoGin                 e
13606d5e247SLoGin             }
13706d5e247SLoGin         });
13806d5e247SLoGin 
13906d5e247SLoGin         return r;
14006d5e247SLoGin     }
14106d5e247SLoGin 
14206d5e247SLoGin     fn store(
14306d5e247SLoGin         &self,
14406d5e247SLoGin         kobj: Arc<dyn KObject>,
14506d5e247SLoGin         attr: &dyn Attribute,
14606d5e247SLoGin         buf: &[u8],
14706d5e247SLoGin     ) -> Result<usize, SystemError> {
14806d5e247SLoGin         let r = attr.store(kobj, buf).map_err(|e| {
14906d5e247SLoGin             if e == SystemError::EOPNOTSUPP_OR_ENOTSUP {
15006d5e247SLoGin                 SystemError::EIO
15106d5e247SLoGin             } else {
15206d5e247SLoGin                 e
15306d5e247SLoGin             }
15406d5e247SLoGin         });
15506d5e247SLoGin 
15606d5e247SLoGin         return r;
15706d5e247SLoGin     }
15806d5e247SLoGin }
15906d5e247SLoGin 
16006d5e247SLoGin #[derive(Debug)]
16106d5e247SLoGin pub struct KObjectManager;
16206d5e247SLoGin 
16306d5e247SLoGin impl KObjectManager {
164*7eda31b2SLoGin     #[allow(dead_code)]
165*7eda31b2SLoGin     pub fn init_and_add_kobj(
166*7eda31b2SLoGin         kobj: Arc<dyn KObject>,
167*7eda31b2SLoGin         join_kset: Option<Arc<KSet>>,
168*7eda31b2SLoGin     ) -> Result<(), SystemError> {
169*7eda31b2SLoGin         Self::kobj_init(&kobj);
170*7eda31b2SLoGin         Self::add_kobj(kobj, join_kset)
171*7eda31b2SLoGin     }
172*7eda31b2SLoGin 
173*7eda31b2SLoGin     #[allow(dead_code)]
174*7eda31b2SLoGin     pub fn kobj_init(kobj: &Arc<dyn KObject>) {
175*7eda31b2SLoGin         kobj.set_kobj_type(Some(&DynamicKObjKType));
176*7eda31b2SLoGin     }
177*7eda31b2SLoGin 
17806d5e247SLoGin     pub fn add_kobj(
17906d5e247SLoGin         kobj: Arc<dyn KObject>,
18006d5e247SLoGin         join_kset: Option<Arc<KSet>>,
18106d5e247SLoGin     ) -> Result<(), SystemError> {
18206d5e247SLoGin         if join_kset.is_some() {
18306d5e247SLoGin             let kset = join_kset.unwrap();
18406d5e247SLoGin             kset.join(&kobj);
18506d5e247SLoGin             // 如果kobject没有parent,那么就将这个kset作为parent
18606d5e247SLoGin             if kobj.parent().is_none() {
18706d5e247SLoGin                 kobj.set_parent(Some(Arc::downgrade(&(kset as Arc<dyn KObject>))));
18806d5e247SLoGin             }
18906d5e247SLoGin         }
19006d5e247SLoGin 
19106d5e247SLoGin         let r = Self::create_dir(kobj.clone());
19206d5e247SLoGin 
19306d5e247SLoGin         if let Err(e) = r {
19406d5e247SLoGin             // https://opengrok.ringotek.cn/xref/linux-6.1.9/lib/kobject.c?r=&mo=10426&fi=394#224
19506d5e247SLoGin             if let Some(kset) = kobj.kset() {
19606d5e247SLoGin                 kset.leave(&kobj);
19706d5e247SLoGin             }
19806d5e247SLoGin             kobj.set_parent(None);
19906d5e247SLoGin             if e == SystemError::EEXIST {
20006d5e247SLoGin                 kerror!("KObjectManager::add_kobj() failed with error: {e:?}, kobj:{kobj:?}");
20106d5e247SLoGin             }
20206d5e247SLoGin 
20306d5e247SLoGin             return Err(e);
20406d5e247SLoGin         }
20506d5e247SLoGin 
20606d5e247SLoGin         kobj.update_kobj_state(Some(KObjectState::IN_SYSFS), None);
20706d5e247SLoGin         return Ok(());
20806d5e247SLoGin     }
20906d5e247SLoGin 
21006d5e247SLoGin     fn create_dir(kobj: Arc<dyn KObject>) -> Result<(), SystemError> {
21106d5e247SLoGin         // create dir in sysfs
21206d5e247SLoGin         sysfs_instance().create_dir(kobj.clone())?;
21306d5e247SLoGin 
21406d5e247SLoGin         // create default attributes in sysfs
21506d5e247SLoGin         if let Some(ktype) = kobj.kobj_type() {
21606d5e247SLoGin             let groups = ktype.attribute_groups();
21706d5e247SLoGin             if let Some(groups) = groups {
21806d5e247SLoGin                 let r = sysfs_instance().create_groups(&kobj, groups);
21906d5e247SLoGin                 if let Err(e) = r {
22006d5e247SLoGin                     sysfs_instance().remove_dir(&kobj);
22106d5e247SLoGin                     return Err(e);
22206d5e247SLoGin                 }
22306d5e247SLoGin             }
22406d5e247SLoGin         }
22506d5e247SLoGin 
22606d5e247SLoGin         return Ok(());
22706d5e247SLoGin     }
22806d5e247SLoGin }
229*7eda31b2SLoGin 
230*7eda31b2SLoGin /// 动态创建的kobject对象的ktype
231*7eda31b2SLoGin #[derive(Debug)]
232*7eda31b2SLoGin pub struct DynamicKObjKType;
233*7eda31b2SLoGin 
234*7eda31b2SLoGin impl KObjType for DynamicKObjKType {
235*7eda31b2SLoGin     fn release(&self, kobj: Arc<dyn KObject>) {
236*7eda31b2SLoGin         kdebug!("DynamicKObjKType::release() kobj:{:?}", kobj.name());
237*7eda31b2SLoGin     }
238*7eda31b2SLoGin 
239*7eda31b2SLoGin     fn sysfs_ops(&self) -> Option<&dyn SysFSOps> {
240*7eda31b2SLoGin         Some(&KObjectSysFSOps)
241*7eda31b2SLoGin     }
242*7eda31b2SLoGin 
243*7eda31b2SLoGin     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
244*7eda31b2SLoGin         None
245*7eda31b2SLoGin     }
246*7eda31b2SLoGin }
247