Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

samples: rust: auxiliary: illustrate driver interaction

Illustrate how a parent driver of an auxiliary driver can take advantage
of the device context guarantees given by the auxiliary bus and
subsequently safely derive its device private data.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+19 -3
+19 -3
samples/rust/rust_driver_auxiliary.rs
··· 5 5 //! To make this driver probe, QEMU must be run with `-device pci-testdev`. 6 6 7 7 use kernel::{ 8 - auxiliary, c_str, device::Core, devres::Devres, driver, error::Error, pci, prelude::*, 8 + auxiliary, c_str, 9 + device::{Bound, Core}, 10 + devres::Devres, 11 + driver, 12 + error::Error, 13 + pci, 14 + prelude::*, 9 15 InPlaceModule, 10 16 }; 11 17 18 + use core::any::TypeId; 12 19 use pin_init::PinInit; 13 20 14 21 const MODULE_NAME: &CStr = <LocalModule as kernel::ModuleMetadata>::NAME; ··· 50 43 51 44 #[pin_data] 52 45 struct ParentDriver { 46 + private: TypeId, 53 47 #[pin] 54 48 _reg0: Devres<auxiliary::Registration>, 55 49 #[pin] ··· 71 63 72 64 fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> { 73 65 try_pin_init!(Self { 66 + private: TypeId::of::<Self>(), 74 67 _reg0 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 0, MODULE_NAME), 75 68 _reg1 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 1, MODULE_NAME), 76 69 }) ··· 79 70 } 80 71 81 72 impl ParentDriver { 82 - fn connect(adev: &auxiliary::Device) -> Result { 73 + fn connect(adev: &auxiliary::Device<Bound>) -> Result { 83 74 let dev = adev.parent(); 84 - let pdev: &pci::Device = dev.try_into()?; 75 + let pdev: &pci::Device<Bound> = dev.try_into()?; 76 + let drvdata = dev.drvdata::<Self>()?; 85 77 86 78 dev_info!( 87 79 dev, ··· 90 80 adev.id(), 91 81 pdev.vendor_id(), 92 82 pdev.device_id() 83 + ); 84 + 85 + dev_info!( 86 + dev, 87 + "We have access to the private data of {:?}.\n", 88 + drvdata.private 93 89 ); 94 90 95 91 Ok(())