Skip to main content

starnix_modules_magma/
init.rs

1// Copyright 2023 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 crate::MagmaFile;
6use starnix_core::device::DeviceOps;
7use starnix_core::task::{CurrentTask, Kernel};
8use starnix_core::vfs::{FileOps, NamespaceNode};
9
10use starnix_uapi::device_id::DeviceId;
11use starnix_uapi::errors::Errno;
12use starnix_uapi::open_flags::OpenFlags;
13
14#[derive(Clone)]
15struct MagmaDeviceBuilder {
16    supported_vendors: Vec<u16>,
17}
18
19impl DeviceOps for MagmaDeviceBuilder {
20    fn open(
21        &self,
22        current_task: &CurrentTask,
23        id: DeviceId,
24        node: &NamespaceNode,
25        flags: OpenFlags,
26    ) -> Result<Box<dyn FileOps>, Errno> {
27        MagmaFile::new_file(
28            current_task,
29            id,
30            &node.entry.node,
31            flags,
32            self.supported_vendors.clone(),
33        )
34    }
35}
36
37pub fn magma_device_init(kernel: &Kernel, supported_vendors: Vec<u16>) {
38    let registry = &kernel.device_registry;
39    let builder = MagmaDeviceBuilder { supported_vendors };
40
41    registry
42        .register_dyn_device(kernel, "magma0".into(), registry.objects.starnix_class(), builder)
43        .expect("can register magma0");
44}