xref: /DragonOS/kernel/src/driver/rtc/rtc_cmos.rs (revision 1ea2daad8121b77ed704e6d7c3a09f478147441d)
1 use alloc::{
2     string::{String, ToString},
3     sync::{Arc, Weak},
4     vec::Vec,
5 };
6 use intertrait::cast::CastArc;
7 use system_error::SystemError;
8 use unified_init::macros::unified_init;
9 
10 use super::{
11     class::rtc_register_device,
12     sysfs::{rtc_general_device_create, RtcGeneralDevice},
13     RtcClassOps, RtcDevice, RtcTime,
14 };
15 use crate::{
16     driver::base::{
17         device::{
18             bus::Bus,
19             driver::{Driver, DriverCommonData},
20             Device, IdTable,
21         },
22         kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
23         kset::KSet,
24         platform::{
25             platform_device::PlatformDevice,
26             platform_driver::{platform_driver_manager, PlatformDriver},
27         },
28     },
29     filesystem::kernfs::KernFSInode,
30     init::initcall::INITCALL_DEVICE,
31     libs::{
32         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
33         spinlock::{SpinLock, SpinLockGuard},
34     },
35 };
36 
37 static CMOS_RTC_GENERAL_DEVICE: RwLock<Option<Arc<RtcGeneralDevice>>> = RwLock::new(None);
38 
39 #[derive(Debug)]
40 #[cast_to([sync] Driver, PlatformDriver)]
41 struct CmosPlatformDriver {
42     inner: SpinLock<InnerCmosPlatformDriver>,
43     locked_kobjstate: LockedKObjectState,
44 }
45 
46 impl CmosPlatformDriver {
47     const NAME: &str = "rtc_cmos";
48 
49     fn new() -> Arc<Self> {
50         Arc::new(CmosPlatformDriver {
51             inner: SpinLock::new(InnerCmosPlatformDriver {
52                 driver_common: DriverCommonData::default(),
53                 kobject_common: KObjectCommonData::default(),
54             }),
55             locked_kobjstate: LockedKObjectState::new(None),
56         })
57     }
58 
59     fn inner(&self) -> SpinLockGuard<InnerCmosPlatformDriver> {
60         self.inner.lock()
61     }
62 }
63 
64 #[derive(Debug)]
65 struct InnerCmosPlatformDriver {
66     driver_common: DriverCommonData,
67     kobject_common: KObjectCommonData,
68 }
69 
70 impl PlatformDriver for CmosPlatformDriver {
71     fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
72         let dev = device
73             .clone()
74             .arc_any()
75             .cast::<dyn RtcDevice>()
76             .map_err(|_| SystemError::ENODEV)?;
77 
78         if dev.id_table() != self.id_table().unwrap() {
79             return Err(SystemError::ENODEV);
80         }
81 
82         if CMOS_RTC_GENERAL_DEVICE.read().is_some() {
83             return Err(SystemError::EBUSY);
84         }
85 
86         let mut guard = CMOS_RTC_GENERAL_DEVICE.write();
87 
88         // 再次检查
89         if guard.is_some() {
90             return Err(SystemError::EBUSY);
91         }
92 
93         let general_rtc_device: Arc<RtcGeneralDevice> = rtc_general_device_create(&dev, None);
94         guard.replace(general_rtc_device.clone());
95 
96         general_rtc_device.set_class_ops(&CmosRtcClassOps);
97         drop(guard);
98 
99         rtc_register_device(&general_rtc_device)
100             .expect("cmos_rtc: register general rtc device failed");
101 
102         return Ok(());
103     }
104 
105     fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
106         // todo: remove
107         Err(SystemError::ENOSYS)
108     }
109 
110     fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
111         unimplemented!("cmos platform driver shutdown")
112     }
113 
114     fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
115         todo!("cmos platform driver suspend")
116     }
117 
118     fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
119         todo!("cmos platform driver resume")
120     }
121 }
122 
123 impl Driver for CmosPlatformDriver {
124     fn id_table(&self) -> Option<IdTable> {
125         Some(IdTable::new(Self::NAME.to_string(), None))
126     }
127 
128     fn devices(&self) -> Vec<Arc<dyn Device>> {
129         self.inner().driver_common.devices.clone()
130     }
131 
132     fn add_device(&self, device: Arc<dyn Device>) {
133         self.inner().driver_common.push_device(device);
134     }
135 
136     fn delete_device(&self, device: &Arc<dyn Device>) {
137         self.inner().driver_common.delete_device(device);
138     }
139 
140     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
141         self.inner().driver_common.bus = bus;
142     }
143 
144     fn bus(&self) -> Option<Weak<dyn Bus>> {
145         self.inner().driver_common.bus.clone()
146     }
147 }
148 
149 impl KObject for CmosPlatformDriver {
150     fn as_any_ref(&self) -> &dyn core::any::Any {
151         self
152     }
153 
154     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
155         self.inner().kobject_common.kern_inode = inode;
156     }
157 
158     fn inode(&self) -> Option<Arc<KernFSInode>> {
159         self.inner().kobject_common.kern_inode.clone()
160     }
161 
162     fn parent(&self) -> Option<Weak<dyn KObject>> {
163         self.inner().kobject_common.parent.clone()
164     }
165 
166     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
167         self.inner().kobject_common.parent = parent;
168     }
169 
170     fn kset(&self) -> Option<Arc<KSet>> {
171         self.inner().kobject_common.kset.clone()
172     }
173 
174     fn set_kset(&self, kset: Option<Arc<KSet>>) {
175         self.inner().kobject_common.kset = kset;
176     }
177 
178     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
179         self.inner().kobject_common.kobj_type
180     }
181 
182     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
183         self.inner().kobject_common.kobj_type = ktype;
184     }
185 
186     fn name(&self) -> String {
187         Self::NAME.to_string()
188     }
189 
190     fn set_name(&self, _name: String) {
191         // do nothing
192     }
193 
194     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
195         self.locked_kobjstate.read()
196     }
197 
198     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
199         self.locked_kobjstate.write()
200     }
201 
202     fn set_kobj_state(&self, state: KObjectState) {
203         *self.locked_kobjstate.write() = state;
204     }
205 }
206 
207 #[unified_init(INITCALL_DEVICE)]
208 pub fn cmos_rtc_driver_init() -> Result<(), SystemError> {
209     let driver = CmosPlatformDriver::new();
210 
211     platform_driver_manager().register(driver)?;
212 
213     return Ok(());
214 }
215 
216 #[derive(Debug)]
217 struct CmosRtcClassOps;
218 
219 impl RtcClassOps for CmosRtcClassOps {
220     fn read_time(&self, dev: &Arc<dyn RtcDevice>) -> Result<RtcTime, SystemError> {
221         dev.class_ops().read_time(dev)
222     }
223 
224     fn set_time(&self, dev: &Arc<dyn RtcDevice>, time: &RtcTime) -> Result<(), SystemError> {
225         dev.class_ops().set_time(dev, time)
226     }
227 }
228