summaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorJohn Hubbard <jhubbard@nvidia.com>2025-08-29 15:36:31 -0700
committerDanilo Krummrich <dakr@kernel.org>2025-09-01 20:16:36 +0200
commit1b8ac37677d307cd0fc10f6bf9bceae2c282bdb4 (patch)
tree6f68463b09b7a8038a5bd15a695ba517e8ea476e /rust/kernel
parent6783d3b08595e932938a244e97d92cda0c0833a1 (diff)
rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
Change Device::vendor_id() to return a Vendor type, and change DeviceId::from_id() to accept a Vendor type. Use the new pci::Vendor in the various Rust for Linux callers who were previously using bindings::PCI_VENDOR_ID_*. Doing so also allows removing "use kernel::bindings" entirely from most of the affected files here. Also, mark vendor_id() as inline. Cc: Danilo Krummrich <dakr@kernel.org> Cc: Elle Rhumsaa <elle@weathered-steel.dev> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: John Hubbard <jhubbard@nvidia.com> Link: https://lore.kernel.org/r/20250829223632.144030-6-jhubbard@nvidia.com [ Replace "as a validated vendor" with "as [`Vendor`]". - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/pci.rs35
-rw-r--r--rust/kernel/pci/id.rs1
2 files changed, 26 insertions, 10 deletions
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 5d95081346e6..391baf95929a 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -133,10 +133,10 @@ impl DeviceId {
/// Equivalent to C's `PCI_DEVICE` macro.
///
- /// Create a new `pci::DeviceId` from a vendor and device ID number.
- pub const fn from_id(vendor: u32, device: u32) -> Self {
+ /// Create a new `pci::DeviceId` from a vendor and device ID.
+ pub const fn from_id(vendor: Vendor, device: u32) -> Self {
Self(bindings::pci_device_id {
- vendor,
+ vendor: vendor.as_raw() as u32,
device,
subvendor: DeviceId::PCI_ANY_ID,
subdevice: DeviceId::PCI_ANY_ID,
@@ -234,7 +234,7 @@ macro_rules! pci_device_table {
/// <MyDriver as pci::Driver>::IdInfo,
/// [
/// (
-/// pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as u32),
+/// pci::DeviceId::from_id(pci::Vendor::REDHAT, bindings::PCI_ANY_ID as u32),
/// (),
/// )
/// ]
@@ -415,12 +415,29 @@ impl<Ctx: device::DeviceContext> Device<Ctx> {
}
impl Device {
- /// Returns the PCI vendor ID.
+ /// Returns the PCI vendor ID as [`Vendor`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*};
+ /// fn log_device_info(pdev: &pci::Device<Core>) -> Result {
+ /// // Get an instance of `Vendor`.
+ /// let vendor = pdev.vendor_id();
+ /// dev_info!(
+ /// pdev.as_ref(),
+ /// "Device: Vendor={}, Device=0x{:x}\n",
+ /// vendor,
+ /// pdev.device_id()
+ /// );
+ /// Ok(())
+ /// }
+ /// ```
#[inline]
- pub fn vendor_id(&self) -> u16 {
- // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
- // `struct pci_dev`.
- unsafe { (*self.as_raw()).vendor }
+ pub fn vendor_id(&self) -> Vendor {
+ // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+ let vendor_id = unsafe { (*self.as_raw()).vendor };
+ Vendor::from_raw(vendor_id)
}
/// Returns the PCI device ID.
diff --git a/rust/kernel/pci/id.rs b/rust/kernel/pci/id.rs
index f56a024f80aa..8ee1dc5c3057 100644
--- a/rust/kernel/pci/id.rs
+++ b/rust/kernel/pci/id.rs
@@ -136,7 +136,6 @@ macro_rules! define_all_pci_vendors {
/// Once constructed, a `Vendor` contains a valid PCI Vendor ID.
impl Vendor {
/// Create a Vendor from a raw 16-bit vendor ID.
- #[expect(dead_code)]
#[inline]
pub(super) fn from_raw(vendor_id: u16) -> Self {
Self(vendor_id)