|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Rust debugfs device sample |
| 4 | +
|
| 5 | +#![allow(missing_docs)] |
| 6 | + |
| 7 | +use kernel::{ |
| 8 | + c_str, debugfs, file, |
| 9 | + file::File, |
| 10 | + io_buffer::IoBufferWriter, |
| 11 | + prelude::*, |
| 12 | + sync::{Arc, SpinLock}, |
| 13 | + types::Mode, |
| 14 | +}; |
| 15 | + |
| 16 | +struct SampleFile; |
| 17 | + |
| 18 | +#[vtable] |
| 19 | +impl file::Operations for SampleFile { |
| 20 | + fn open(_data: &(), _file: &File) -> Result { |
| 21 | + Ok(()) |
| 22 | + } |
| 23 | + |
| 24 | + fn read( |
| 25 | + _data: (), |
| 26 | + _file: &File, |
| 27 | + writer: &mut impl IoBufferWriter, |
| 28 | + offset: u64, |
| 29 | + ) -> Result<usize> { |
| 30 | + let data = b"Sample debugfs file implementing file::Operations\n"; |
| 31 | + let offset = offset as usize; |
| 32 | + |
| 33 | + if offset > data.len() { |
| 34 | + return Ok(0); |
| 35 | + } |
| 36 | + |
| 37 | + let len = core::cmp::min(writer.len(), data.len() - offset); |
| 38 | + writer.write_slice(&data[offset..(offset + len)])?; |
| 39 | + Ok(len) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[pin_data] |
| 44 | +struct IncAttribute { |
| 45 | + #[pin] |
| 46 | + data: SpinLock<i64>, |
| 47 | +} |
| 48 | + |
| 49 | +impl debugfs::attr::Attribute<i64> for IncAttribute { |
| 50 | + fn get(&self) -> Result<i64> { |
| 51 | + let mut guard = self.data.lock(); |
| 52 | + let ret = *guard; |
| 53 | + *guard = ret + 1; |
| 54 | + Ok(ret) |
| 55 | + } |
| 56 | + |
| 57 | + fn set(&self, val: i64) -> Result { |
| 58 | + let mut guard = self.data.lock(); |
| 59 | + *guard = val; |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +debugfs::attribute_signed!(IncAttribute, "%#d\n"); |
| 65 | + |
| 66 | +struct RustDebugfs { |
| 67 | + _sample_file: debugfs::PinnedRegistration, |
| 68 | + _inc_attribute: debugfs::PinnedRegistration<Arc<IncAttribute>>, |
| 69 | + _symlink: debugfs::Registration<()>, |
| 70 | +} |
| 71 | +impl kernel::Module for RustDebugfs { |
| 72 | + fn init(_module: &'static ThisModule) -> Result<Self> { |
| 73 | + let dir = Arc::try_new(debugfs::Registration::register_dir( |
| 74 | + c_str!("rust_samples"), |
| 75 | + None, |
| 76 | + )?)?; |
| 77 | + |
| 78 | + let sample_file = debugfs::Registration::register_file::<SampleFile>( |
| 79 | + c_str!("sample"), |
| 80 | + Mode::from_int(0444), |
| 81 | + (), |
| 82 | + Some(dir.clone()), |
| 83 | + )?; |
| 84 | + |
| 85 | + let symlink = debugfs::Registration::register_symlink( |
| 86 | + c_str!("sample_symlink"), |
| 87 | + Some(dir.clone()), |
| 88 | + c_str!("sample"), |
| 89 | + )?; |
| 90 | + |
| 91 | + let attribute = Arc::pin_init(pin_init!(IncAttribute { |
| 92 | + data <- kernel::new_spinlock!(0x42), |
| 93 | + }))?; |
| 94 | + let inc_attribute = attribute.register( |
| 95 | + c_str!("inc_attribute"), |
| 96 | + Mode::from_int(0666), |
| 97 | + Some(dir.clone()), |
| 98 | + )?; |
| 99 | + |
| 100 | + Ok(Self { |
| 101 | + _sample_file: sample_file, |
| 102 | + _inc_attribute: inc_attribute, |
| 103 | + _symlink: symlink, |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +module! { |
| 109 | + type: RustDebugfs, |
| 110 | + name: "rust_debugfs", |
| 111 | + author : "Fabien Parent <[email protected]>", |
| 112 | + description: "Rust debugfs sample", |
| 113 | + license: "GPL", |
| 114 | +} |
0 commit comments