Message ID | 20250321214826.140946-2-dakr@kernel.org (mailing list archive) |
---|---|
State | Handled Elsewhere |
Delegated to: | Bjorn Helgaas |
Headers | show |
Series | Implement TryFrom<&Device> for bus specific devices | expand |
On Fri, Mar 21, 2025 at 10:47:53PM +0100, Danilo Krummrich wrote: > Implement bus_type_raw(), which returns a raw pointer to the device' > struct bus_type. > > This is useful for bus devices, to implement the following trait. > > impl TryFrom<&Device> for &pci::Device > > With this a caller can try to get the bus specific device from a generic > device in a safe way. try_from() will only succeed if the generic > device' bus type pointer matches the pointer of the bus' type. > > Reviewed-by: Alice Ryhl <aliceryhl@google.com> > Reviewed-by: Benno Lossin <benno.lossin@proton.me> > Signed-off-by: Danilo Krummrich <dakr@kernel.org> > --- > rust/kernel/device.rs | 10 ++++++++++ > 1 file changed, 10 insertions(+) Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 21b343a1dc4d..67a2fc46cf4c 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -65,6 +65,16 @@ pub(crate) fn as_raw(&self) -> *mut bindings::device { self.0.get() } + /// Returns a raw pointer to the device' bus type. + #[expect(unused)] + pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type { + // SAFETY: + // - By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. + // - `dev->bus` is a pointer to a `const struct bus_type`, which is only ever set at device + // creation. + unsafe { (*self.as_raw()).bus } + } + /// Convert a raw C `struct device` pointer to a `&'a Device`. /// /// # Safety