// SPDX-License-Identifier: GPL-2.0 // Copyright (C) 2025 Greg Kroah-Hartman // Copyright (C) 2025 The Linux Foundation //! Sample debugfs rust module that emulates soc_info to try to see just how well the api can //! work... use core::fmt; use kernel::c_str; use kernel::debugfs::{Dir, Scope}; use kernel::prelude::*; module! { type: SocInfo, name: "rust_soc_info", authors: ["Greg Kroah-Hartman"], description: "Rust soc_info sample driver", license: "GPL", } fn foundry_print(foundry: &u32, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "Foundry: {}", foundry) } // Fake "hardware SOC info object that ideally would read from the hardware to get the info. // For now just use some fake data #[derive(Debug)] struct HwSocInfo { id: u32, ver: u32, raw_id: u32, foundry: u32, name: &'static CStr, } impl HwSocInfo { fn new() -> Self { Self { id: 123, ver: 456, raw_id: 789, foundry: 0, name: c_str!("hw_soc name"), } } } struct SocInfo { _debug_dir: Dir, _hw_soc_info: Pin>>, } impl kernel::Module for SocInfo { fn init(_this: &'static ThisModule) -> Result { // Read from the hardware and get our structure information let hw_soc_info = HwSocInfo::new(); // Create the root directory let root = Dir::new(c_str!("rust_soc_info")); let scope = KBox::pin_init( // Create directory scope, that contains some data and a bunch of files exporting this // data. root.scope(hw_soc_info, c_str!("hw_soc_info"), |hw_soc_info, dir| { dir.read_only_file(c_str!("id"), &hw_soc_info.id); dir.read_only_file(c_str!("ver"), &hw_soc_info.ver); dir.read_only_file(c_str!("raw_id"), &hw_soc_info.raw_id); dir.read_only_file(c_str!("name"), &hw_soc_info.name); dir.read_callback_file(c_str!("foundry"), &hw_soc_info.foundry, &foundry_print); }), GFP_KERNEL, )?; let soc_info: &HwSocInfo = &scope; // Print the contents of `soc_info` that were moved into `scope`. pr_info!("HwSocInfo: {:?}\n", soc_info); Ok(Self { _debug_dir: root, _hw_soc_info: scope, }) } }