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 <[email protected]> --- 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 7b15a107fed4..84d18bbfafc5 100644 --- a/samples/rust/rust_driver_auxiliary.rs +++ b/samples/rust/rust_driver_auxiliary.rs @@ -52,14 +52,15 @@ fn probe<'bound>( } } -struct Data { +struct Data<'bound> { index: u32, + parent: &'bound 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!( @@ -85,14 +86,20 @@ fn probe<'bound>( 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, + }, )?, }) } @@ -100,13 +107,11 @@ fn probe<'bound>( 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(), @@ -114,7 +119,7 @@ fn connect(adev: &auxiliary::Device<Bound>) -> Result { ); dev_info!( - dev, + pdev, "Connected to auxiliary device with index {}.\n", data.index ); -- 2.54.0
