Add a name() method to the `Device` type, which returns a CStr that contains the device name, typically the BDF address for PCI devices.
Signed-off-by: Timur Tabi <[email protected]> --- rust/helpers/device.c | 5 +++++ rust/kernel/device.rs | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/rust/helpers/device.c b/rust/helpers/device.c index 9a4316bafedf..4609b62f6a06 100644 --- a/rust/helpers/device.c +++ b/rust/helpers/device.c @@ -25,3 +25,8 @@ void rust_helper_dev_set_drvdata(struct device *dev, void *data) { dev_set_drvdata(dev, data); } + +const char *rust_helper_dev_name(const struct device *dev) +{ + return dev_name(dev); +} diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index c79be2e2bfe3..636c522a8374 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -483,6 +483,22 @@ pub fn fwnode(&self) -> Option<&property::FwNode> { // defined as a `#[repr(transparent)]` wrapper around `fwnode_handle`. Some(unsafe { &*fwnode_handle.cast() }) } + + /// Returns the name of the device. + /// + /// This is the kobject name of the device, or its initial name if the kobject is not yet + /// available. + /// + /// For PCI devices, the name in the format "DDDD:BB:DD.F" where: + /// - DDDD is the PCI domain (4 hex digits) + /// - BB is the bus number (2 hex digits) + /// - DD is the device number (2 hex digits) + /// - F is the function number (1 hex digit) + pub fn name(&self) -> &CStr { + // SAFETY: By its type invariant `self.as_raw()` is a valid pointer to a `struct device`. + // The returned string is valid for the lifetime of the device. + unsafe { CStr::from_char_ptr(bindings::dev_name(self.as_raw())) } + } } // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic -- 2.52.0
