Skip to content

Commit

Permalink
samples: rust: add debugfs samples
Browse files Browse the repository at this point in the history
Add some basic debugfs sample module to show how to use the debugfs API.
The sample module is showing:
 * How to create a directory
 * How to define and register a debugfs file by implementing
   file::Operations
 * How to use the attribute macro to expose simple numerical debugfs
   values. This is the equivalent of the C macro
   DEFINE_DEBUGFS_ATTRIBUTE{,_SIGNED}

Signed-off-by: Fabien Parent <[email protected]>
  • Loading branch information
Fabo committed Dec 16, 2023
1 parent a6ba5e0 commit e05f067
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
10 changes: 10 additions & 0 deletions samples/rust/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ menuconfig SAMPLES_RUST

if SAMPLES_RUST

config SAMPLE_RUST_DEBUGFS
tristate "Debugfs"
help
This option builds the Rust debugfs module sample.

To compile this as a module, choose M here:
the module will be called rust_debugfs.

If unsure, say N.

config SAMPLE_RUST_MINIMAL
tristate "Minimal"
help
Expand Down
1 change: 1 addition & 0 deletions samples/rust/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0

obj-$(CONFIG_SAMPLE_RUST_DEBUGFS) += rust_debugfs.o
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o

Expand Down
114 changes: 114 additions & 0 deletions samples/rust/rust_debugfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// SPDX-License-Identifier: GPL-2.0

//! Rust debugfs device sample
#![allow(missing_docs)]

use kernel::{
c_str, debugfs, file,
file::File,
io_buffer::IoBufferWriter,
prelude::*,
sync::{Arc, SpinLock},
types::Mode,
};

struct SampleFile;

#[vtable]
impl file::Operations for SampleFile {
fn open(_data: &(), _file: &File) -> Result {
Ok(())
}

fn read(
_data: (),
_file: &File,
writer: &mut impl IoBufferWriter,
offset: u64,
) -> Result<usize> {
let data = b"Sample debugfs file implementing file::Operations\n";
let offset = offset as usize;

if offset > data.len() {
return Ok(0);
}

let len = core::cmp::min(writer.len(), data.len() - offset);
writer.write_slice(&data[offset..(offset + len)])?;
Ok(len)
}
}

#[pin_data]
struct IncAttribute {
#[pin]
data: SpinLock<i64>,
}

impl debugfs::attr::Attribute<i64> for IncAttribute {
fn get(&self) -> Result<i64> {
let mut guard = self.data.lock();
let ret = *guard;
*guard = ret + 1;
Ok(ret)
}

fn set(&self, val: i64) -> Result {
let mut guard = self.data.lock();
*guard = val;
Ok(())
}
}

debugfs::attribute_signed!(IncAttribute, "%#d\n");

struct RustDebugfs {
_sample_file: debugfs::PinnedRegistration,
_inc_attribute: debugfs::PinnedRegistration<Arc<IncAttribute>>,
_symlink: debugfs::Registration<()>,
}
impl kernel::Module for RustDebugfs {
fn init(_module: &'static ThisModule) -> Result<Self> {
let dir = Arc::try_new(debugfs::Registration::register_dir(
c_str!("rust_samples"),
None,
)?)?;

let sample_file = debugfs::Registration::register_file::<SampleFile>(
c_str!("sample"),
Mode::from_int(0444),
(),
Some(dir.clone()),
)?;

let symlink = debugfs::Registration::register_symlink(
c_str!("sample_symlink"),
Some(dir.clone()),
c_str!("sample"),
)?;

let attribute = Arc::pin_init(pin_init!(IncAttribute {
data <- kernel::new_spinlock!(0x42),
}))?;
let inc_attribute = attribute.register(
c_str!("inc_attribute"),
Mode::from_int(0666),
Some(dir.clone()),
)?;

Ok(Self {
_sample_file: sample_file,
_inc_attribute: inc_attribute,
_symlink: symlink,
})
}
}

module! {
type: RustDebugfs,
name: "rust_debugfs",
author: "Fabien Parent <[email protected]>",
description: "Rust debugfs sample",
license: "GPL",
}

0 comments on commit e05f067

Please sign in to comment.