lkml.org 
[lkml]   [2026]   [Apr]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH 15/24] samples: rust: rust_driver_auxiliary: showcase lifetime-bound registration data
Date
Make the Data struct lifetime-parameterized, storing a reference to the
parent pci::Device<Bound>. This demonstrates that registration data can
hold device resources tied to the parent driver's lifetime.

In connect(), retrieve the parent PCI device from the registration data
rather than casting through adev.parent().

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
samples/rust/rust_driver_auxiliary.rs | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driver_auxiliary.rs
index 4ad619c5731e..010ec2201a69 100644
--- a/samples/rust/rust_driver_auxiliary.rs
+++ b/samples/rust/rust_driver_auxiliary.rs
@@ -51,14 +51,15 @@ fn probe(
}
}

-struct Data {
+struct Data<'a> {
index: u32,
+ parent: &'a pci::Device<Bound>,
}

#[allow(clippy::type_complexity)]
struct ParentDriver {
- _reg0: Devres<auxiliary::Registration<ForLt!(Data)>>,
- _reg1: Devres<auxiliary::Registration<ForLt!(Data)>>,
+ _reg0: Devres<auxiliary::Registration<ForLt!(Data<'_>)>>,
+ _reg1: Devres<auxiliary::Registration<ForLt!(Data<'_>)>>,
}

kernel::pci_device_table!(
@@ -83,14 +84,20 @@ fn probe(
AUXILIARY_NAME,
0,
MODULE_NAME,
- Data { index: 0 },
+ Data {
+ index: 0,
+ parent: pdev,
+ },
)?,
_reg1: auxiliary::Registration::new(
pdev.as_ref(),
AUXILIARY_NAME,
1,
MODULE_NAME,
- Data { index: 1 },
+ Data {
+ index: 1,
+ parent: pdev,
+ },
)?,
})
}
@@ -98,13 +105,11 @@ fn probe(

impl ParentDriver {
fn connect(adev: &auxiliary::Device<Bound>) -> Result {
- let dev = adev.parent();
- let pdev: &pci::Device<Bound> = dev.try_into()?;
-
- let data = adev.registration_data::<ForLt!(Data)>()?;
+ let data = adev.registration_data::<ForLt!(Data<'_>)>()?;
+ let pdev = data.parent;

dev_info!(
- dev,
+ pdev,
"Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n",
adev.id(),
pdev.vendor_id(),
@@ -112,7 +117,7 @@ fn connect(adev: &auxiliary::Device<Bound>) -> Result {
);

dev_info!(
- dev,
+ pdev,
"Connected to auxiliary device with index {}.\n",
data.index
);
--
2.54.0

\
 
 \ /
  Last update: 2026-04-28 00:16    [from the cache]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and my Meterkast|Read the blog