Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

John Hubbard and committed by
Danilo Krummrich
1b8ac376 6783d3b0

+37 -26
+26 -9
rust/kernel/pci.rs
··· 133 133 134 134 /// Equivalent to C's `PCI_DEVICE` macro. 135 135 /// 136 - /// Create a new `pci::DeviceId` from a vendor and device ID number. 137 - pub const fn from_id(vendor: u32, device: u32) -> Self { 136 + /// Create a new `pci::DeviceId` from a vendor and device ID. 137 + pub const fn from_id(vendor: Vendor, device: u32) -> Self { 138 138 Self(bindings::pci_device_id { 139 - vendor, 139 + vendor: vendor.as_raw() as u32, 140 140 device, 141 141 subvendor: DeviceId::PCI_ANY_ID, 142 142 subdevice: DeviceId::PCI_ANY_ID, ··· 234 234 /// <MyDriver as pci::Driver>::IdInfo, 235 235 /// [ 236 236 /// ( 237 - /// pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as u32), 237 + /// pci::DeviceId::from_id(pci::Vendor::REDHAT, bindings::PCI_ANY_ID as u32), 238 238 /// (), 239 239 /// ) 240 240 /// ] ··· 415 415 } 416 416 417 417 impl Device { 418 - /// Returns the PCI vendor ID. 418 + /// Returns the PCI vendor ID as [`Vendor`]. 419 + /// 420 + /// # Examples 421 + /// 422 + /// ``` 423 + /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; 424 + /// fn log_device_info(pdev: &pci::Device<Core>) -> Result { 425 + /// // Get an instance of `Vendor`. 426 + /// let vendor = pdev.vendor_id(); 427 + /// dev_info!( 428 + /// pdev.as_ref(), 429 + /// "Device: Vendor={}, Device=0x{:x}\n", 430 + /// vendor, 431 + /// pdev.device_id() 432 + /// ); 433 + /// Ok(()) 434 + /// } 435 + /// ``` 419 436 #[inline] 420 - pub fn vendor_id(&self) -> u16 { 421 - // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a 422 - // `struct pci_dev`. 423 - unsafe { (*self.as_raw()).vendor } 437 + pub fn vendor_id(&self) -> Vendor { 438 + // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. 439 + let vendor_id = unsafe { (*self.as_raw()).vendor }; 440 + Vendor::from_raw(vendor_id) 424 441 } 425 442 426 443 /// Returns the PCI device ID.
-1
rust/kernel/pci/id.rs
··· 136 136 /// Once constructed, a `Vendor` contains a valid PCI Vendor ID. 137 137 impl Vendor { 138 138 /// Create a Vendor from a raw 16-bit vendor ID. 139 - #[expect(dead_code)] 140 139 #[inline] 141 140 pub(super) fn from_raw(vendor_id: u16) -> Self { 142 141 Self(vendor_id)
+1 -5
samples/rust/rust_dma.rs
··· 5 5 //! To make this driver probe, QEMU must be run with `-device pci-testdev`. 6 6 7 7 use kernel::{ 8 - bindings, 9 8 device::Core, 10 9 dma::{CoherentAllocation, Device, DmaMask}, 11 10 pci, ··· 44 45 PCI_TABLE, 45 46 MODULE_PCI_TABLE, 46 47 <DmaSampleDriver as pci::Driver>::IdInfo, 47 - [( 48 - pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, 0x5), 49 - () 50 - )] 48 + [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())] 51 49 ); 52 50 53 51 impl pci::Driver for DmaSampleDriver {
+5 -7
samples/rust/rust_driver_auxiliary.rs
··· 5 5 //! To make this driver probe, QEMU must be run with `-device pci-testdev`. 6 6 7 7 use kernel::{ 8 - auxiliary, bindings, c_str, device::Core, driver, error::Error, pci, prelude::*, InPlaceModule, 8 + auxiliary, c_str, device::Core, driver, error::Error, pci, prelude::*, InPlaceModule, 9 9 }; 10 10 11 11 use pin_init::PinInit; ··· 50 50 PCI_TABLE, 51 51 MODULE_PCI_TABLE, 52 52 <ParentDriver as pci::Driver>::IdInfo, 53 - [( 54 - pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, 0x5), 55 - () 56 - )] 53 + [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())] 57 54 ); 58 55 59 56 impl pci::Driver for ParentDriver { ··· 78 81 let parent = adev.parent().ok_or(EINVAL)?; 79 82 let pdev: &pci::Device = parent.try_into()?; 80 83 84 + let vendor = pdev.vendor_id(); 81 85 dev_info!( 82 86 adev.as_ref(), 83 - "Connect auxiliary {} with parent: VendorID={:#x}, DeviceID={:#x}\n", 87 + "Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n", 84 88 adev.id(), 85 - pdev.vendor_id(), 89 + vendor, 86 90 pdev.device_id() 87 91 ); 88 92
+5 -4
samples/rust/rust_driver_pci.rs
··· 4 4 //! 5 5 //! To make this driver probe, QEMU must be run with `-device pci-testdev`. 6 6 7 - use kernel::{bindings, c_str, device::Core, devres::Devres, pci, prelude::*, sync::aref::ARef}; 7 + use kernel::{c_str, device::Core, devres::Devres, pci, prelude::*, sync::aref::ARef}; 8 8 9 9 struct Regs; 10 10 ··· 38 38 MODULE_PCI_TABLE, 39 39 <SampleDriver as pci::Driver>::IdInfo, 40 40 [( 41 - pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, 0x5), 41 + pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), 42 42 TestIndex::NO_EVENTFD 43 43 )] 44 44 ); ··· 66 66 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE; 67 67 68 68 fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>>> { 69 + let vendor = pdev.vendor_id(); 69 70 dev_dbg!( 70 71 pdev.as_ref(), 71 - "Probe Rust PCI driver sample (PCI ID: 0x{:x}, 0x{:x}).\n", 72 - pdev.vendor_id(), 72 + "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n", 73 + vendor, 73 74 pdev.device_id() 74 75 ); 75 76