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;
10use starnix_sync::{FileOpsCore, LockEqualOrBefore, Locked};
11use starnix_uapi::device_type::DeviceType;
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        _locked: &mut Locked<FileOpsCore>,
22        current_task: &CurrentTask,
23        _id: DeviceType,
24        _node: &NamespaceNode,
25        _flags: OpenFlags,
26    ) -> Result<Box<dyn FileOps>, Errno> {
27        RtcFile::new_file(current_task)
28    }
29}
30
31/// Initialize a RTC device.
32///
33/// It will be available at `/dev/rtc0`.
34pub fn rtc_device_init<L>(locked: &mut Locked<L>, current_task: &CurrentTask) -> Result<(), Errno>
35where
36    L: LockEqualOrBefore<FileOpsCore>,
37{
38    let kernel = current_task.kernel();
39    let registry = &kernel.device_registry;
40    registry
41        .register_dyn_device(
42            locked,
43            current_task,
44            "rtc0".into(),
45            registry.objects.rtc_class(),
46            RtcDevice,
47        )
48        .inspect(|dev| log_debug!("registered RTC device: {dev:?}"))
49        .map(|_| ())
50}