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