]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: pci: add DeviceId::from_class_and_vendor() method
authorJohn Hubbard <jhubbard@nvidia.com>
Fri, 29 Aug 2025 22:36:29 +0000 (15:36 -0700)
committerDanilo Krummrich <dakr@kernel.org>
Mon, 1 Sep 2025 18:09:55 +0000 (20:09 +0200)
Add a new method to create PCI DeviceIds that match both a specific
vendor and PCI class. This is more targeted than the existing
from_class() method as it filters on both vendor and class criteria.

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-4-jhubbard@nvidia.com
[ Minor doc-comment improvements. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/pci.rs

index 4f7ef82492ae581993e25380f5086ae0dc382b2b..5d95081346e61d45b61e3b92a2a99c61a6df8aac 100644 (file)
@@ -162,6 +162,29 @@ impl DeviceId {
             override_only: 0,
         })
     }
+
+    /// Create a new [`DeviceId`] from a class number, mask, and specific vendor.
+    ///
+    /// This is more targeted than [`DeviceId::from_class`]: in addition to matching by [`Vendor`],
+    /// it also matches the PCI [`Class`] (up to the entire 24 bits, depending on the
+    /// [`ClassMask`]).
+    #[inline]
+    pub const fn from_class_and_vendor(
+        class: Class,
+        class_mask: ClassMask,
+        vendor: Vendor,
+    ) -> Self {
+        Self(bindings::pci_device_id {
+            vendor: vendor.as_raw() as u32,
+            device: DeviceId::PCI_ANY_ID,
+            subvendor: DeviceId::PCI_ANY_ID,
+            subdevice: DeviceId::PCI_ANY_ID,
+            class: class.as_raw(),
+            class_mask: class_mask.as_raw(),
+            driver_data: 0,
+            override_only: 0,
+        })
+    }
 }
 
 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_id` and does not add