]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
rust: device: add device name method
authorTimur Tabi <ttabi@nvidia.com>
Thu, 19 Mar 2026 21:26:53 +0000 (16:26 -0500)
committerDanilo Krummrich <dakr@kernel.org>
Tue, 24 Mar 2026 22:51:23 +0000 (23:51 +0100)
Add a name() method to the `Device` type, which returns a CStr that
contains the device name.

Signed-off-by: Timur Tabi <ttabi@nvidia.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Tested-by: John Hubbard <jhubbard@nvidia.com>
Tested-by: Eliot Courtney <ecourtney@nvidia.com>
Link: https://patch.msgid.link/20260319212658.2541610-2-ttabi@nvidia.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/helpers/device.c
rust/kernel/device.rs

index a8ab931a9bd1233c613a9127fbb3a93a3bab7951..3be4ee5907841a52058bfe527ace5ff0bd475b39 100644 (file)
@@ -25,3 +25,8 @@ __rust_helper void rust_helper_dev_set_drvdata(struct device *dev, void *data)
 {
        dev_set_drvdata(dev, data);
 }
+
+__rust_helper const char *rust_helper_dev_name(const struct device *dev)
+{
+       return dev_name(dev);
+}
index 379058eca2edf9bc2633913b91ff3cad7895345a..6d5396a43ebeacf4cce5cfea79c3966056b32b95 100644 (file)
@@ -489,6 +489,17 @@ impl<Ctx: DeviceContext> Device<Ctx> {
         // 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.
+    #[inline]
+    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