Skip to content

Commit 9a445b6

Browse files
author
Danilo Krummrich
committed
samples: rust: add Rust platform sample driver
Add a sample Rust platform driver illustrating the usage of the platform bus abstractions. This driver probes through either a match of device / driver name or a match within the OF ID table. Reviewed-by: Rob Herring (Arm) <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]>
1 parent 21b896b commit 9a445b6

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7037,6 +7037,7 @@ F: rust/kernel/device_id.rs
70377037
F: rust/kernel/devres.rs
70387038
F: rust/kernel/driver.rs
70397039
F: rust/kernel/platform.rs
7040+
F: samples/rust/rust_driver_platform.rs
70407041

70417042
DRIVERS FOR OMAP ADAPTIVE VOLTAGE SCALING (AVS)
70427043
M: Nishanth Menon <[email protected]>

drivers/of/unittest-data/tests-platform.dtsi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
reg = <0x100>;
3434
};
3535
};
36+
37+
test-device@2 {
38+
compatible = "test,rust-device";
39+
reg = <0x2>;
40+
};
3641
};
3742
};
3843
};

samples/rust/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ config SAMPLE_RUST_DRIVER_PCI
4141

4242
If unsure, say N.
4343

44+
config SAMPLE_RUST_DRIVER_PLATFORM
45+
tristate "Platform Driver"
46+
help
47+
This option builds the Rust Platform driver sample.
48+
49+
To compile this as a module, choose M here:
50+
the module will be called rust_driver_platform.
51+
52+
If unsure, say N.
53+
4454
config SAMPLE_RUST_HOSTPROGS
4555
bool "Host programs"
4656
help

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ccflags-y += -I$(src) # needed for trace events
44
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
55
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
66
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o
7+
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
78

89
rust_print-y := rust_print_main.o rust_print_events.o
910

samples/rust/rust_driver_platform.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust Platform driver sample.
4+
5+
use kernel::{c_str, of, platform, prelude::*};
6+
7+
struct SampleDriver {
8+
pdev: platform::Device,
9+
}
10+
11+
struct Info(u32);
12+
13+
kernel::of_device_table!(
14+
OF_TABLE,
15+
MODULE_OF_TABLE,
16+
<SampleDriver as platform::Driver>::IdInfo,
17+
[(of::DeviceId::new(c_str!("test,rust-device")), Info(42))]
18+
);
19+
20+
impl platform::Driver for SampleDriver {
21+
type IdInfo = Info;
22+
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
23+
24+
fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin<KBox<Self>>> {
25+
dev_dbg!(pdev.as_ref(), "Probe Rust Platform driver sample.\n");
26+
27+
if let Some(info) = info {
28+
dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0);
29+
}
30+
31+
let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;
32+
33+
Ok(drvdata.into())
34+
}
35+
}
36+
37+
impl Drop for SampleDriver {
38+
fn drop(&mut self) {
39+
dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\n");
40+
}
41+
}
42+
43+
kernel::module_platform_driver! {
44+
type: SampleDriver,
45+
name: "rust_driver_platform",
46+
author: "Danilo Krummrich",
47+
description: "Rust Platform driver",
48+
license: "GPL v2",
49+
}

0 commit comments

Comments
 (0)