1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// SPDX-License-Identifier: GPL-2.0
//! Rust configfs sample.
use kernel::alloc::flags;
use kernel::c_str;
use kernel::configfs;
use kernel::configfs_attrs;
use kernel::new_mutex;
use kernel::page::PAGE_SIZE;
use kernel::prelude::*;
use kernel::sync::Mutex;
module! {
type: RustConfigfs,
name: "rust_configfs",
author: "Rust for Linux Contributors",
description: "Rust configfs sample",
license: "GPL",
}
#[pin_data]
struct RustConfigfs {
#[pin]
config: configfs::Subsystem<Configuration>,
}
#[pin_data]
struct Configuration {
message: &'static CStr,
#[pin]
bar: Mutex<(KBox<[u8; PAGE_SIZE]>, usize)>,
}
impl Configuration {
fn new() -> impl PinInit<Self, Error> {
try_pin_init!(Self {
message: c_str!("Hello World\n"),
bar <- new_mutex!((KBox::new([0; PAGE_SIZE], flags::GFP_KERNEL)?, 0)),
})
}
}
impl kernel::InPlaceModule for RustConfigfs {
fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
pr_info!("Rust configfs sample (init)\n");
// Define a subsystem with the data type `Configuration`, two
// attributes, `message` and `bar` and child group type `Child`. `mkdir`
// in the directory representing this subsystem will create directories
// backed by the `Child` type.
let item_type = configfs_attrs! {
container: configfs::Subsystem<Configuration>,
data: Configuration,
child: Child,
attributes: [
message: 0,
bar: 1,
],
};
try_pin_init!(Self {
config <- configfs::Subsystem::new(
c_str!("rust_configfs"), item_type, Configuration::new()
),
})
}
}
#[vtable]
impl configfs::GroupOperations for Configuration {
type Child = Child;
fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<Child>, Error>> {
// Define a group with data type `Child`, one attribute `baz` and child
// group type `GrandChild`. `mkdir` in the directory representing this
// group will create directories backed by the `GrandChild` type.
let tpe = configfs_attrs! {
container: configfs::Group<Child>,
data: Child,
child: GrandChild,
attributes: [
baz: 0,
],
};
Ok(configfs::Group::new(name.try_into()?, tpe, Child::new()))
}
}
#[vtable]
impl configfs::AttributeOperations<0> for Configuration {
type Data = Configuration;
fn show(container: &Configuration, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show message\n");
let data = container.message;
page[0..data.len()].copy_from_slice(data);
Ok(data.len())
}
}
#[vtable]
impl configfs::AttributeOperations<1> for Configuration {
type Data = Configuration;
fn show(container: &Configuration, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show bar\n");
let guard = container.bar.lock();
let data = guard.0.as_slice();
let len = guard.1;
page[0..len].copy_from_slice(&data[0..len]);
Ok(len)
}
fn store(container: &Configuration, page: &[u8]) -> Result {
pr_info!("Store bar\n");
let mut guard = container.bar.lock();
guard.0[0..page.len()].copy_from_slice(page);
guard.1 = page.len();
Ok(())
}
}
// `pin_data` cannot handle structs without braces.
#[pin_data]
struct Child {}
impl Child {
fn new() -> impl PinInit<Self, Error> {
try_pin_init!(Self {})
}
}
#[vtable]
impl configfs::GroupOperations for Child {
type Child = GrandChild;
fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<GrandChild>, Error>> {
// Define a group with data type `GrandChild`, one attribute `gc`. As no
// child type is specified, it will not be possible to create subgroups
// in this group, and `mkdir`in the directory representing this group
// will return an error.
let tpe = configfs_attrs! {
container: configfs::Group<GrandChild>,
data: GrandChild,
attributes: [
gc: 0,
],
};
Ok(configfs::Group::new(
name.try_into()?,
tpe,
GrandChild::new(),
))
}
}
#[vtable]
impl configfs::AttributeOperations<0> for Child {
type Data = Child;
fn show(_container: &Child, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show baz\n");
let data = c"Hello Baz\n".to_bytes();
page[0..data.len()].copy_from_slice(data);
Ok(data.len())
}
}
// `pin_data` cannot handle structs without braces.
#[pin_data]
struct GrandChild {}
impl GrandChild {
fn new() -> impl PinInit<Self, Error> {
try_pin_init!(Self {})
}
}
#[vtable]
impl configfs::AttributeOperations<0> for GrandChild {
type Data = GrandChild;
fn show(_container: &GrandChild, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
pr_info!("Show grand child\n");
let data = c"Hello GC\n".to_bytes();
page[0..data.len()].copy_from_slice(data);
Ok(data.len())
}
}
|