Skip to main content

starnix_modules_rtc/
device.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use super::file::RtcFile;
6use starnix_core::device::DeviceOps;
7use starnix_core::task::CurrentTask;
8use starnix_core::vfs::{FileOps, NamespaceNode};
9use starnix_logging::log_debug;
10
11use starnix_uapi::device_id::DeviceId;
12use starnix_uapi::errors::Errno;
13use starnix_uapi::open_flags::OpenFlags;
14
15#[derive(Clone)]
16struct RtcDevice;
17
18impl DeviceOps for RtcDevice {
19    fn open(
20        &self,
21        current_task: &CurrentTask,
22        _id: DeviceId,
23        _node: &NamespaceNode,
24        _flags: OpenFlags,
25    ) -> Result<Box<dyn FileOps>, Errno> {
26        RtcFile::new_file(current_task)
27    }
28}
29
30/// Initialize a RTC device.
31///
32/// It will be available at `/dev/rtc0`.
33pub fn rtc_device_init(current_task: &CurrentTask) -> Result<(), Errno> {
34    let kernel = current_task.kernel();
35    let registry = &kernel.device_registry;
36    registry
37        .register_dyn_device(
38            current_task.kernel(),
39            "rtc0".into(),
40            registry.objects.rtc_class(),
41            RtcDevice,
42        )
43        .inspect(|dev| log_debug!("registered RTC device: {dev:?}"))
44        .map(|_| ())
45}