[PATCH 8/8] samples: rust: pci: reset pci-testdev in unbind()

From: Danilo Krummrich
Date: Sat Jun 21 2025 - 15:53:41 EST


Reset the pci-testdev when the driver is unbound from its device.

Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
samples/rust/rust_driver_pci.rs | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
index 15147e4401b2..062a242f8874 100644
--- a/samples/rust/rust_driver_pci.rs
+++ b/samples/rust/rust_driver_pci.rs
@@ -18,7 +18,7 @@ impl Regs {

type Bar0 = pci::Bar<{ Regs::END }>;

-#[derive(Debug)]
+#[derive(Copy, Clone, Debug)]
struct TestIndex(u8);

impl TestIndex {
@@ -28,6 +28,7 @@ impl TestIndex {
struct SampleDriver {
pdev: ARef<pci::Device>,
bar: Devres<Bar0>,
+ index: TestIndex,
}

kernel::pci_device_table!(
@@ -79,6 +80,7 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
Self {
pdev: pdev.into(),
bar,
+ index: *info,
},
GFP_KERNEL,
)?;
@@ -92,6 +94,13 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>

Ok(drvdata.into())
}
+
+ fn unbind(pdev: &pci::Device<Core>, this: Pin<&Self>) {
+ if let Ok(bar) = this.bar.access(pdev.as_ref()) {
+ // Reset pci-testdev by writing a new test index.
+ bar.write8(this.index.0, Regs::TEST);
+ }
+ }
}

impl Drop for SampleDriver {
--
2.49.0