On Fri, 12 Dec 2025 14:49:46 -0600
Timur Tabi <[email protected]> wrote:

> Add a name() method to the PCI `Device` type, which returns a CStr
> that contains the device name, typically the BDF address.
> 
> Signed-off-by: Timur Tabi <[email protected]>
> ---
>  rust/helpers/pci.c |  5 +++++
>  rust/kernel/pci.rs | 37 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 42 insertions(+)
> 
> diff --git a/rust/helpers/pci.c b/rust/helpers/pci.c
> index bf8173979c5e..411bc743fcc2 100644
> --- a/rust/helpers/pci.c
> +++ b/rust/helpers/pci.c
> @@ -2,6 +2,11 @@
>  
>  #include <linux/pci.h>
>  
> +const char *rust_helper_pci_name(const struct pci_dev *pdev)
> +{
> +     return pci_name(pdev);
> +}
> +

You're adding a helper function while having a Rust reimplementation of
the added function. Please pick one approach and stick with it.

Best,
Gary

>  u16 rust_helper_pci_dev_id(struct pci_dev *dev)
>  {
>       return PCI_DEVID(dev->bus->number, dev->devfn);
> diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
> index 82e128431f08..d0c0c2f6aa32 100644
> --- a/rust/kernel/pci.rs
> +++ b/rust/kernel/pci.rs
> @@ -427,6 +427,43 @@ pub fn pci_class(&self) -> Class {
>          // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
>          Class::from_raw(unsafe { (*self.as_raw()).class })
>      }
> +
> +    /// Returns the PCI device name.
> +    ///
> +    /// This returns the device 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)
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// # use kernel::{c_str, debugfs::Dir, device::Core, pci, prelude::*};
> +    /// fn create_debugfs(pdev: &pci::Device<Core>) -> Result {
> +    ///     let dir = Dir::new(pdev.name());
> +    ///     Ok(())
> +    /// }
> +    /// ```
> +    #[inline]
> +    pub fn name(&self) -> &CStr {
> +        // SAFETY: By its type invariant `self.as_raw` is always a valid 
> pointer to a
> +        // `struct pci_dev`, which contains a `struct device dev` member.
> +        unsafe {
> +            let pci_dev = self.as_raw();
> +            let dev = addr_of_mut!((*pci_dev).dev);
> +
> +            // If init_name is set, use it; otherwise use the kobject name
> +            let init_name = (*dev).init_name;
> +            let name_ptr = if !init_name.is_null() {
> +                init_name
> +            } else {
> +                (*dev).kobj.name
> +            };
> +
> +            CStr::from_char_ptr(name_ptr)
> +        }
> +    }
>  }
>  
>  impl Device<device::Core> {

Reply via email to