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, Kernel};
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(kernel: &Kernel) -> Result<(), Errno> {
34    let registry = &kernel.device_registry;
35    registry
36        .register_dyn_device(kernel, "rtc0".into(), registry.objects.rtc_class(), RtcDevice)
37        .inspect(|dev| log_debug!("registered RTC device: {dev:?}"))
38        .map(|_| ())
39}