xref: /DragonOS/kernel/src/driver/base/platform/platform_device.rs (revision 06d5e247267cb65b84a80f219853ccd0f384b16e)
1 use alloc::{
2     string::{String, ToString},
3     sync::{Arc, Weak},
4 };
5 
6 use crate::{
7     driver::{
8         base::{
9             device::{
10                 bus::{Bus, BusState},
11                 Device, DeviceNumber, DevicePrivateData, DeviceType, IdTable,
12             },
13             kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
14             kset::KSet,
15         },
16         Driver,
17     },
18     filesystem::kernfs::KernFSInode,
19     libs::{
20         rwlock::{RwLockReadGuard, RwLockWriteGuard},
21         spinlock::SpinLock,
22     },
23 };
24 
25 use super::{super::device::DeviceState, CompatibleTable};
26 
27 /// @brief: 实现该trait的设备实例应挂载在platform总线上,
28 ///         同时应该实现Device trait
29 pub trait PlatformDevice: Device {
30     fn compatible_table(&self) -> CompatibleTable;
31     /// @brief: 判断设备是否初始化
32     /// @parameter: None
33     /// @return: 如果已经初始化,返回true,否则,返回false
34     fn is_initialized(&self) -> bool;
35 
36     /// @brief: 设置设备状态
37     /// @parameter set_state: 设备状态
38     /// @return: None
39     fn set_state(&self, set_state: DeviceState);
40 }
41 
42 #[derive(Debug)]
43 #[cast_to([sync] Device)]
44 pub struct PlatformBusDevice {
45     inner: SpinLock<InnerPlatformBusDevice>,
46     kobj_state: LockedKObjectState,
47 }
48 
49 impl PlatformBusDevice {
50     /// @brief: 创建一个加锁的platform总线实例
51     /// @parameter: None
52     /// @return: platform总线实例
53     pub fn new(
54         data: DevicePrivateData,
55         parent: Option<Weak<dyn KObject>>,
56     ) -> Arc<PlatformBusDevice> {
57         return Arc::new(PlatformBusDevice {
58             inner: SpinLock::new(InnerPlatformBusDevice::new(data, parent)),
59             kobj_state: LockedKObjectState::new(KObjectState::empty()),
60         });
61     }
62 
63     /// @brief: 获取总线的匹配表
64     /// @parameter: None
65     /// @return: platform总线匹配表
66     #[inline]
67     #[allow(dead_code)]
68     fn compatible_table(&self) -> CompatibleTable {
69         CompatibleTable::new(vec!["platform"])
70     }
71 
72     /// @brief: 判断总线是否初始化
73     /// @parameter: None
74     /// @return: 已初始化,返回true,否则,返回false
75     #[inline]
76     #[allow(dead_code)]
77     fn is_initialized(&self) -> bool {
78         let state = self.inner.lock().state;
79         match state {
80             BusState::Initialized => true,
81             _ => false,
82         }
83     }
84 
85     /// @brief: 设置总线状态
86     /// @parameter set_state: 总线状态BusState
87     /// @return: None
88     #[inline]
89     #[allow(dead_code)]
90     fn set_state(&self, set_state: BusState) {
91         let state = &mut self.inner.lock().state;
92         *state = set_state;
93     }
94 
95     /// @brief: 获取总线状态
96     /// @parameter: None
97     /// @return: 总线状态
98     #[inline]
99     #[allow(dead_code)]
100     fn get_state(&self) -> BusState {
101         let state = self.inner.lock().state;
102         return state;
103     }
104 
105     // /// @brief:
106     // /// @parameter: None
107     // /// @return: 总线状态
108     // #[inline]
109     // #[allow(dead_code)]
110     // fn set_driver(&self, driver: Option<Arc<LockedPlatformBusDriver>>) {
111     //     self.0.lock().driver = driver;
112     // }
113 }
114 
115 /// @brief: platform总线
116 #[allow(dead_code)]
117 #[derive(Debug, Clone)]
118 pub struct InnerPlatformBusDevice {
119     name: String,
120     data: DevicePrivateData,
121     state: BusState,                   // 总线状态
122     parent: Option<Weak<dyn KObject>>, // 总线的父对象
123 
124     kernfs_inode: Option<Arc<KernFSInode>>,
125     /// 当前设备挂载到的总线
126     bus: Option<Arc<dyn Bus>>,
127     /// 当前设备已经匹配的驱动
128     driver: Option<Arc<dyn Driver>>,
129 }
130 
131 /// @brief: platform方法集
132 impl InnerPlatformBusDevice {
133     /// @brief: 创建一个platform总线实例
134     /// @parameter: None
135     /// @return: platform总线实例
136     pub fn new(data: DevicePrivateData, parent: Option<Weak<dyn KObject>>) -> Self {
137         Self {
138             data,
139             name: "platform".to_string(),
140             state: BusState::NotInitialized,
141             parent,
142             kernfs_inode: None,
143             bus: None,
144             driver: None,
145         }
146     }
147 }
148 
149 impl KObject for PlatformBusDevice {
150     fn as_any_ref(&self) -> &dyn core::any::Any {
151         self
152     }
153 
154     fn parent(&self) -> Option<Weak<dyn KObject>> {
155         self.inner.lock().parent.clone()
156     }
157 
158     fn inode(&self) -> Option<Arc<KernFSInode>> {
159         self.inner.lock().kernfs_inode.clone()
160     }
161 
162     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
163         self.inner.lock().kernfs_inode = inode;
164     }
165 
166     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
167         None
168     }
169 
170     fn kset(&self) -> Option<Arc<KSet>> {
171         None
172     }
173 
174     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
175         self.kobj_state.read()
176     }
177 
178     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
179         self.kobj_state.write()
180     }
181 
182     fn set_kobj_state(&self, state: KObjectState) {
183         *self.kobj_state.write() = state;
184     }
185 
186     fn name(&self) -> String {
187         self.inner.lock().name.clone()
188     }
189 
190     fn set_name(&self, name: String) {
191         self.inner.lock().name = name;
192     }
193 
194     fn set_kset(&self, _kset: Option<Arc<KSet>>) {
195         todo!()
196     }
197 
198     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
199         self.inner.lock().parent = parent;
200     }
201 }
202 
203 /// @brief: 为Platform实现Device trait,platform总线也是一种设备,属于总线设备类型
204 impl Device for PlatformBusDevice {
205     #[inline]
206     #[allow(dead_code)]
207     fn dev_type(&self) -> DeviceType {
208         return DeviceType::Bus;
209     }
210 
211     #[inline]
212     #[allow(dead_code)]
213     fn id_table(&self) -> IdTable {
214         IdTable::new("platform".to_string(), DeviceNumber::new(0))
215     }
216 
217     fn bus(&self) -> Option<Arc<dyn Bus>> {
218         self.inner.lock().bus.clone()
219     }
220 
221     fn driver(&self) -> Option<Arc<dyn Driver>> {
222         self.inner.lock().driver.clone()
223     }
224 
225     #[inline]
226     fn is_dead(&self) -> bool {
227         false
228     }
229 
230     fn set_driver(&self, driver: Option<Arc<dyn Driver>>) {
231         self.inner.lock().driver = driver;
232     }
233 }
234