1*da152319SLoGin use alloc::{ 2*da152319SLoGin string::String, 3*da152319SLoGin sync::{Arc, Weak}, 4*da152319SLoGin }; 5*da152319SLoGin use ida::IdAllocator; 6*da152319SLoGin use system_error::SystemError; 7*da152319SLoGin 8*da152319SLoGin use crate::{ 9*da152319SLoGin driver::base::{ 10*da152319SLoGin class::Class, 11*da152319SLoGin device::{ 12*da152319SLoGin bus::Bus, device_manager, driver::Driver, Device, DeviceCommonData, DeviceType, IdTable, 13*da152319SLoGin }, 14*da152319SLoGin kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState}, 15*da152319SLoGin kset::KSet, 16*da152319SLoGin }, 17*da152319SLoGin filesystem::{ 18*da152319SLoGin kernfs::KernFSInode, 19*da152319SLoGin sysfs::{ 20*da152319SLoGin file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport, SYSFS_ATTR_MODE_RO, 21*da152319SLoGin }, 22*da152319SLoGin vfs::syscall::ModeType, 23*da152319SLoGin }, 24*da152319SLoGin libs::{ 25*da152319SLoGin rwlock::{RwLockReadGuard, RwLockWriteGuard}, 26*da152319SLoGin spinlock::{SpinLock, SpinLockGuard}, 27*da152319SLoGin }, 28*da152319SLoGin }; 29*da152319SLoGin 30*da152319SLoGin use super::{ 31*da152319SLoGin class::sys_class_rtc_instance, 32*da152319SLoGin interface::rtc_read_time, 33*da152319SLoGin utils::{kobj2rtc_device, kobj2rtc_general_device}, 34*da152319SLoGin GeneralRtcPriority, RtcClassOps, RtcDevice, 35*da152319SLoGin }; 36*da152319SLoGin 37*da152319SLoGin static RTC_GENERAL_DEVICE_IDA: IdAllocator = IdAllocator::new(0, usize::MAX); 38*da152319SLoGin 39*da152319SLoGin pub(super) const RTC_HCTOSYS_DEVICE: &str = "rtc0"; 40*da152319SLoGin 41*da152319SLoGin #[derive(Debug)] 42*da152319SLoGin #[cast_to([sync] KObject, Device, RtcDevice)] 43*da152319SLoGin pub struct RtcGeneralDevice { 44*da152319SLoGin name: String, 45*da152319SLoGin id: usize, 46*da152319SLoGin inner: SpinLock<InnerRtcGeneralDevice>, 47*da152319SLoGin kobj_state: LockedKObjectState, 48*da152319SLoGin priority: GeneralRtcPriority, 49*da152319SLoGin } 50*da152319SLoGin 51*da152319SLoGin #[derive(Debug)] 52*da152319SLoGin struct InnerRtcGeneralDevice { 53*da152319SLoGin device_common: DeviceCommonData, 54*da152319SLoGin kobject_common: KObjectCommonData, 55*da152319SLoGin 56*da152319SLoGin class_ops: Option<&'static dyn RtcClassOps>, 57*da152319SLoGin /// 上一次调用`rtc_hctosys()`把时间同步到timekeeper的时候的返回值 58*da152319SLoGin hc2sysfs_result: Result<(), SystemError>, 59*da152319SLoGin } 60*da152319SLoGin 61*da152319SLoGin impl RtcGeneralDevice { 62*da152319SLoGin /// 创建一个新的通用RTC设备实例 63*da152319SLoGin /// 64*da152319SLoGin /// 注意,由于还需要进行其他的初始化操作,因此这个函数并不是公开的构造函数。 65*da152319SLoGin fn new(priority: GeneralRtcPriority) -> Arc<Self> { 66*da152319SLoGin let id = RTC_GENERAL_DEVICE_IDA.alloc().unwrap(); 67*da152319SLoGin let name = format!("rtc{}", id); 68*da152319SLoGin Arc::new(Self { 69*da152319SLoGin name, 70*da152319SLoGin id, 71*da152319SLoGin inner: SpinLock::new(InnerRtcGeneralDevice { 72*da152319SLoGin device_common: DeviceCommonData::default(), 73*da152319SLoGin kobject_common: KObjectCommonData::default(), 74*da152319SLoGin class_ops: None, 75*da152319SLoGin hc2sysfs_result: Err(SystemError::ENODEV), 76*da152319SLoGin }), 77*da152319SLoGin kobj_state: LockedKObjectState::new(None), 78*da152319SLoGin priority, 79*da152319SLoGin }) 80*da152319SLoGin } 81*da152319SLoGin 82*da152319SLoGin fn inner(&self) -> SpinLockGuard<InnerRtcGeneralDevice> { 83*da152319SLoGin self.inner.lock() 84*da152319SLoGin } 85*da152319SLoGin 86*da152319SLoGin pub fn set_class_ops(&self, class_ops: &'static dyn RtcClassOps) { 87*da152319SLoGin self.inner().class_ops = Some(class_ops); 88*da152319SLoGin } 89*da152319SLoGin 90*da152319SLoGin pub fn class_ops(&self) -> Option<&'static dyn RtcClassOps> { 91*da152319SLoGin self.inner().class_ops 92*da152319SLoGin } 93*da152319SLoGin 94*da152319SLoGin pub fn priority(&self) -> GeneralRtcPriority { 95*da152319SLoGin self.priority 96*da152319SLoGin } 97*da152319SLoGin 98*da152319SLoGin pub(super) fn set_hc2sys_result(&self, val: Result<(), SystemError>) { 99*da152319SLoGin self.inner().hc2sysfs_result = val; 100*da152319SLoGin } 101*da152319SLoGin 102*da152319SLoGin pub(super) fn hc2sysfs_result(&self) -> Result<(), SystemError> { 103*da152319SLoGin self.inner().hc2sysfs_result.clone() 104*da152319SLoGin } 105*da152319SLoGin } 106*da152319SLoGin 107*da152319SLoGin impl Drop for RtcGeneralDevice { 108*da152319SLoGin fn drop(&mut self) { 109*da152319SLoGin RTC_GENERAL_DEVICE_IDA.free(self.id); 110*da152319SLoGin } 111*da152319SLoGin } 112*da152319SLoGin 113*da152319SLoGin impl RtcDevice for RtcGeneralDevice { 114*da152319SLoGin fn class_ops(&self) -> &'static dyn super::RtcClassOps { 115*da152319SLoGin todo!() 116*da152319SLoGin } 117*da152319SLoGin } 118*da152319SLoGin 119*da152319SLoGin impl Device for RtcGeneralDevice { 120*da152319SLoGin fn dev_type(&self) -> DeviceType { 121*da152319SLoGin DeviceType::Rtc 122*da152319SLoGin } 123*da152319SLoGin 124*da152319SLoGin fn id_table(&self) -> IdTable { 125*da152319SLoGin IdTable::new(self.name.clone(), None) 126*da152319SLoGin } 127*da152319SLoGin 128*da152319SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 129*da152319SLoGin self.inner().device_common.bus = bus; 130*da152319SLoGin } 131*da152319SLoGin 132*da152319SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 133*da152319SLoGin self.inner().device_common.get_bus_weak_or_clear() 134*da152319SLoGin } 135*da152319SLoGin 136*da152319SLoGin fn set_class(&self, class: Option<Weak<dyn Class>>) { 137*da152319SLoGin self.inner().device_common.class = class; 138*da152319SLoGin } 139*da152319SLoGin 140*da152319SLoGin fn class(&self) -> Option<Arc<dyn Class>> { 141*da152319SLoGin self.inner() 142*da152319SLoGin .device_common 143*da152319SLoGin .get_class_weak_or_clear() 144*da152319SLoGin .and_then(|x| x.upgrade()) 145*da152319SLoGin } 146*da152319SLoGin 147*da152319SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 148*da152319SLoGin self.inner() 149*da152319SLoGin .device_common 150*da152319SLoGin .get_driver_weak_or_clear() 151*da152319SLoGin .and_then(|x| x.upgrade()) 152*da152319SLoGin } 153*da152319SLoGin 154*da152319SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 155*da152319SLoGin self.inner().device_common.driver = driver; 156*da152319SLoGin } 157*da152319SLoGin 158*da152319SLoGin fn is_dead(&self) -> bool { 159*da152319SLoGin false 160*da152319SLoGin } 161*da152319SLoGin 162*da152319SLoGin fn can_match(&self) -> bool { 163*da152319SLoGin false 164*da152319SLoGin } 165*da152319SLoGin 166*da152319SLoGin fn set_can_match(&self, _can_match: bool) { 167*da152319SLoGin // do nothing 168*da152319SLoGin } 169*da152319SLoGin 170*da152319SLoGin fn state_synced(&self) -> bool { 171*da152319SLoGin true 172*da152319SLoGin } 173*da152319SLoGin fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> { 174*da152319SLoGin Some(&[&RtcAttrGroup]) 175*da152319SLoGin } 176*da152319SLoGin } 177*da152319SLoGin 178*da152319SLoGin impl KObject for RtcGeneralDevice { 179*da152319SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 180*da152319SLoGin self 181*da152319SLoGin } 182*da152319SLoGin 183*da152319SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 184*da152319SLoGin self.inner().kobject_common.kern_inode = inode; 185*da152319SLoGin } 186*da152319SLoGin 187*da152319SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 188*da152319SLoGin self.inner().kobject_common.kern_inode.clone() 189*da152319SLoGin } 190*da152319SLoGin 191*da152319SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 192*da152319SLoGin self.inner().kobject_common.parent.clone() 193*da152319SLoGin } 194*da152319SLoGin 195*da152319SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 196*da152319SLoGin self.inner().kobject_common.parent = parent; 197*da152319SLoGin } 198*da152319SLoGin 199*da152319SLoGin fn kset(&self) -> Option<Arc<KSet>> { 200*da152319SLoGin self.inner().kobject_common.kset.clone() 201*da152319SLoGin } 202*da152319SLoGin 203*da152319SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 204*da152319SLoGin self.inner().kobject_common.kset = kset; 205*da152319SLoGin } 206*da152319SLoGin 207*da152319SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 208*da152319SLoGin self.inner().kobject_common.kobj_type 209*da152319SLoGin } 210*da152319SLoGin 211*da152319SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 212*da152319SLoGin self.inner().kobject_common.kobj_type = ktype; 213*da152319SLoGin } 214*da152319SLoGin 215*da152319SLoGin fn name(&self) -> String { 216*da152319SLoGin self.name.clone() 217*da152319SLoGin } 218*da152319SLoGin 219*da152319SLoGin fn set_name(&self, _name: String) { 220*da152319SLoGin // do nothing 221*da152319SLoGin } 222*da152319SLoGin 223*da152319SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 224*da152319SLoGin self.kobj_state.read() 225*da152319SLoGin } 226*da152319SLoGin 227*da152319SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 228*da152319SLoGin self.kobj_state.write() 229*da152319SLoGin } 230*da152319SLoGin 231*da152319SLoGin fn set_kobj_state(&self, state: KObjectState) { 232*da152319SLoGin *self.kobj_state_mut() = state; 233*da152319SLoGin } 234*da152319SLoGin } 235*da152319SLoGin 236*da152319SLoGin /// 237*da152319SLoGin /// 用于创建一个通用的RTC设备实例。 238*da152319SLoGin /// 239*da152319SLoGin /// ## 参数 240*da152319SLoGin /// 241*da152319SLoGin /// - `real_dev`: 一个对实际RTC设备的引用,这个设备将作为通用RTC设备的父设备。 242*da152319SLoGin pub fn rtc_general_device_create( 243*da152319SLoGin real_dev: &Arc<dyn RtcDevice>, 244*da152319SLoGin priority: Option<GeneralRtcPriority>, 245*da152319SLoGin ) -> Arc<RtcGeneralDevice> { 246*da152319SLoGin let dev = RtcGeneralDevice::new(priority.unwrap_or_default()); 247*da152319SLoGin device_manager().device_default_initialize(&(dev.clone() as Arc<dyn Device>)); 248*da152319SLoGin dev.set_parent(Some(Arc::downgrade(real_dev) as Weak<dyn KObject>)); 249*da152319SLoGin dev.set_class(Some(Arc::downgrade( 250*da152319SLoGin &(sys_class_rtc_instance().cloned().unwrap() as Arc<dyn Class>), 251*da152319SLoGin ))); 252*da152319SLoGin 253*da152319SLoGin return dev; 254*da152319SLoGin } 255*da152319SLoGin 256*da152319SLoGin #[derive(Debug)] 257*da152319SLoGin struct RtcAttrGroup; 258*da152319SLoGin 259*da152319SLoGin impl AttributeGroup for RtcAttrGroup { 260*da152319SLoGin fn name(&self) -> Option<&str> { 261*da152319SLoGin None 262*da152319SLoGin } 263*da152319SLoGin 264*da152319SLoGin fn attrs(&self) -> &[&'static dyn Attribute] { 265*da152319SLoGin &[&AttrName, &AttrDate, &AttrTime, &AttrHcToSys] 266*da152319SLoGin } 267*da152319SLoGin 268*da152319SLoGin fn is_visible( 269*da152319SLoGin &self, 270*da152319SLoGin _kobj: Arc<dyn KObject>, 271*da152319SLoGin attr: &'static dyn Attribute, 272*da152319SLoGin ) -> Option<ModeType> { 273*da152319SLoGin // todo: https://code.dragonos.org.cn/xref/linux-6.6.21/drivers/rtc/sysfs.c#280 274*da152319SLoGin 275*da152319SLoGin return Some(attr.mode()); 276*da152319SLoGin } 277*da152319SLoGin } 278*da152319SLoGin 279*da152319SLoGin #[derive(Debug)] 280*da152319SLoGin struct AttrName; 281*da152319SLoGin 282*da152319SLoGin impl Attribute for AttrName { 283*da152319SLoGin fn name(&self) -> &str { 284*da152319SLoGin "name" 285*da152319SLoGin } 286*da152319SLoGin 287*da152319SLoGin fn mode(&self) -> ModeType { 288*da152319SLoGin SYSFS_ATTR_MODE_RO 289*da152319SLoGin } 290*da152319SLoGin 291*da152319SLoGin fn support(&self) -> SysFSOpsSupport { 292*da152319SLoGin SysFSOpsSupport::ATTR_SHOW 293*da152319SLoGin } 294*da152319SLoGin 295*da152319SLoGin fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 296*da152319SLoGin let rtc_device = kobj 297*da152319SLoGin .parent() 298*da152319SLoGin .and_then(|x| x.upgrade()) 299*da152319SLoGin .ok_or(SystemError::ENODEV)?; 300*da152319SLoGin let rtc_device = kobj2rtc_device(rtc_device).ok_or(SystemError::EINVAL)?; 301*da152319SLoGin 302*da152319SLoGin let driver_name = rtc_device.driver().ok_or(SystemError::ENODEV)?.name(); 303*da152319SLoGin let device_name = rtc_device.name(); 304*da152319SLoGin sysfs_emit_str(buf, &format!("{} {}\n", driver_name, device_name)) 305*da152319SLoGin } 306*da152319SLoGin } 307*da152319SLoGin 308*da152319SLoGin #[derive(Debug)] 309*da152319SLoGin struct AttrDate; 310*da152319SLoGin 311*da152319SLoGin impl Attribute for AttrDate { 312*da152319SLoGin fn name(&self) -> &str { 313*da152319SLoGin "date" 314*da152319SLoGin } 315*da152319SLoGin 316*da152319SLoGin fn mode(&self) -> ModeType { 317*da152319SLoGin SYSFS_ATTR_MODE_RO 318*da152319SLoGin } 319*da152319SLoGin 320*da152319SLoGin fn support(&self) -> SysFSOpsSupport { 321*da152319SLoGin SysFSOpsSupport::ATTR_SHOW 322*da152319SLoGin } 323*da152319SLoGin 324*da152319SLoGin fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 325*da152319SLoGin let rtc_device: Arc<RtcGeneralDevice> = 326*da152319SLoGin kobj2rtc_general_device(kobj).ok_or(SystemError::EINVAL)?; 327*da152319SLoGin let time = rtc_read_time(&rtc_device)?; 328*da152319SLoGin sysfs_emit_str(buf, &time.date_string()) 329*da152319SLoGin } 330*da152319SLoGin } 331*da152319SLoGin 332*da152319SLoGin #[derive(Debug)] 333*da152319SLoGin struct AttrTime; 334*da152319SLoGin 335*da152319SLoGin impl Attribute for AttrTime { 336*da152319SLoGin fn name(&self) -> &str { 337*da152319SLoGin "time" 338*da152319SLoGin } 339*da152319SLoGin 340*da152319SLoGin fn mode(&self) -> ModeType { 341*da152319SLoGin SYSFS_ATTR_MODE_RO 342*da152319SLoGin } 343*da152319SLoGin 344*da152319SLoGin fn support(&self) -> SysFSOpsSupport { 345*da152319SLoGin SysFSOpsSupport::ATTR_SHOW 346*da152319SLoGin } 347*da152319SLoGin 348*da152319SLoGin fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 349*da152319SLoGin let rtc_device = kobj2rtc_general_device(kobj).ok_or(SystemError::EINVAL)?; 350*da152319SLoGin let time = rtc_read_time(&rtc_device)?; 351*da152319SLoGin sysfs_emit_str(buf, &time.time_string()) 352*da152319SLoGin } 353*da152319SLoGin } 354*da152319SLoGin 355*da152319SLoGin #[derive(Debug)] 356*da152319SLoGin struct AttrHcToSys; 357*da152319SLoGin 358*da152319SLoGin impl Attribute for AttrHcToSys { 359*da152319SLoGin fn name(&self) -> &str { 360*da152319SLoGin "hctosys" 361*da152319SLoGin } 362*da152319SLoGin 363*da152319SLoGin fn mode(&self) -> ModeType { 364*da152319SLoGin SYSFS_ATTR_MODE_RO 365*da152319SLoGin } 366*da152319SLoGin 367*da152319SLoGin fn support(&self) -> SysFSOpsSupport { 368*da152319SLoGin SysFSOpsSupport::ATTR_SHOW 369*da152319SLoGin } 370*da152319SLoGin 371*da152319SLoGin fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 372*da152319SLoGin let rtc_device = kobj2rtc_general_device(kobj).ok_or(SystemError::EINVAL)?; 373*da152319SLoGin if rtc_device.hc2sysfs_result().is_ok() && rtc_device.name().eq(RTC_HCTOSYS_DEVICE) { 374*da152319SLoGin return sysfs_emit_str(buf, "1\n"); 375*da152319SLoGin } 376*da152319SLoGin 377*da152319SLoGin return sysfs_emit_str(buf, "0\n"); 378*da152319SLoGin } 379*da152319SLoGin } 380