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