public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: pci: add HeaderType enum and header_type() helper
@ 2026-01-03 14:38 SeungJong Ha via B4 Relay
  2026-01-03 15:17 ` Danilo Krummrich
  0 siblings, 1 reply; 6+ messages in thread
From: SeungJong Ha via B4 Relay @ 2026-01-03 14:38 UTC (permalink / raw)
  To: Danilo Krummrich, Bjorn Helgaas, Krzysztof Wilczyński,
	Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross
  Cc: linux-kernel, linux-pci, rust-for-linux, SeungJong Ha

From: SeungJong Ha <engineer.jjhama@gmail.com>

Add the HeaderType enum to represent PCI configuration space header
types (Normal and Bridge). Also implement the header_type() method in
the Device struct to allow Rust PCI drivers to identify the device
type safely.

This is required for drivers to handle type-specific configuration
registers correctly.

Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
Hello,

This is my first patch to the Linux kernel, specifically targeting the 
Rust PCI subsystem. 

This patch introduces the HeaderType enum to represent PCI configuration 
space header types (Normal and Bridge) and implements the header_type() 
method in the Device struct.

I have followed the patch submission checklist, but as this is my first 
contribution, please let me know if I have missed any conventions or 
if there are areas for improvement.

Thank you for your time and review!

Best regards,
SeungJong Ha
---
 rust/kernel/pci.rs        | 10 ++++++++++
 rust/kernel/pci/header.rs | 31 +++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 82e128431..90a0eb54f 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -31,10 +31,12 @@
     },
 };
 
+mod header;
 mod id;
 mod io;
 mod irq;
 
+pub use self::header::HeaderType;
 pub use self::id::{
     Class,
     ClassMask,
@@ -373,6 +375,14 @@ pub fn revision_id(&self) -> u8 {
         unsafe { (*self.as_raw()).revision }
     }
 
+    /// Returns the PCI header type.
+    #[inline]
+    pub fn header_type(&self) -> HeaderType {
+        // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
+        // `struct pci_dev`.
+        HeaderType::from(unsafe { (*self.as_raw()).hdr_type })
+    }
+
     /// Returns the PCI bus device/function.
     #[inline]
     pub fn dev_id(&self) -> u16 {
diff --git a/rust/kernel/pci/header.rs b/rust/kernel/pci/header.rs
new file mode 100644
index 000000000..032efeb16
--- /dev/null
+++ b/rust/kernel/pci/header.rs
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! PCI device header type definitions.
+//!
+//! This module contains PCI header type definitions
+
+use kernel::bindings;
+
+/// PCI device header types.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum HeaderType {
+    /// Normal PCI device header (Type 0)
+    NormalDevice,
+    /// PCI-to-PCI bridge header (Type 1)
+    PciToPciBridge,
+    /// CardBus bridge header (Type 2)
+    CardBusBridge,
+    /// Unknown or unsupported header type
+    Unknown,
+}
+
+impl From<u8> for HeaderType {
+    fn from(value: u8) -> Self {
+        match u32::from(value) & bindings::PCI_HEADER_TYPE_MASK {
+            bindings::PCI_HEADER_TYPE_NORMAL => HeaderType::NormalDevice,
+            bindings::PCI_HEADER_TYPE_BRIDGE => HeaderType::PciToPciBridge,
+            bindings::PCI_HEADER_TYPE_CARDBUS => HeaderType::CardBusBridge,
+            _ => HeaderType::Unknown,
+        }
+    }
+}

---
base-commit: f8f9c1f4d0c7a64600e2ca312dec824a0bc2f1da
change-id: 20260103-add-rust-pci-header-type-346249c1ec14

Best regards,
-- 
SeungJong Ha <engineer.jjhama@gmail.com>



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-01-04 14:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-03 14:38 [PATCH] rust: pci: add HeaderType enum and header_type() helper SeungJong Ha via B4 Relay
2026-01-03 15:17 ` Danilo Krummrich
2026-01-03 15:39   ` 하승종
2026-01-04 12:31     ` Miguel Ojeda
2026-01-04 13:40     ` Danilo Krummrich
2026-01-04 14:23       ` 하승종

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox