rust/kernel/pci/id.rs

Source file repositories/reference/linux-study-clean/rust/kernel/pci/id.rs

File Facts

System
Linux kernel
Corpus path
rust/kernel/pci/id.rs
Extension
.rs
Size
39341 bytes
Lines
581
Domain
Rust Kernel Layer
Bucket
Rust API Membrane
Inferred role
Rust Kernel Layer: implementation source
Status
source implementation candidate

Why This File Exists

Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0

//! PCI device identifiers and related types.
//!
//! This module contains PCI class codes, Vendor IDs, and supporting types.

use crate::{
    bindings,
    fmt,
    prelude::*, //
};

/// PCI device class codes.
///
/// Each entry contains the full 24-bit PCI class code (base class in bits
/// 23-16, subclass in bits 15-8, programming interface in bits 7-0).
///
/// # Examples
///
/// ```
/// # use kernel::{device::Core, pci::{self, Class}, prelude::*};
/// fn probe_device(pdev: &pci::Device<Core<'_>>) -> Result {
///     let pci_class = pdev.pci_class();
///     dev_info!(
///         pdev,
///         "Detected PCI class: {}\n",
///         pci_class
///     );
///     Ok(())
/// }
/// ```
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct Class(u32);

/// PCI class mask constants for matching [`Class`] codes.
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClassMask {
    /// Match the full 24-bit class code.
    Full = 0xffffff,
    /// Match the upper 16 bits of the class code (base class and subclass only)
    ClassSubclass = 0xffff00,
}

macro_rules! define_all_pci_classes {
    (
        $($variant:ident = $binding:expr,)+
    ) => {
        impl Class {
            $(
                #[allow(missing_docs)]
                pub const $variant: Self = Self(Self::to_24bit_class($binding));
            )+
        }

        impl fmt::Display for Class {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                match self {
                    $(
                        &Self::$variant => write!(f, stringify!($variant)),
                    )+
                    _ => <Self as fmt::Debug>::fmt(self, f),
                }
            }
        }
    };
}

/// Once constructed, a [`Class`] contains a valid PCI class code.
impl Class {
    /// Create a [`Class`] from a raw 24-bit class code.
    #[inline]
    pub(super) fn from_raw(class_code: u32) -> Self {
        Self(class_code)
    }

    /// Get the raw 24-bit class code value.
    #[inline]
    pub const fn as_raw(self) -> u32 {
        self.0
    }

    // Converts a PCI class constant to 24-bit format.
    //
    // Many device drivers use only the upper 16 bits (base class and subclass),
    // but some use the full 24 bits. In order to support both cases, store the
    // class code as a 24-bit value, where 16-bit values are shifted up 8 bits.
    const fn to_24bit_class(val: u32) -> u32 {
        if val > 0xFFFF {

Annotation

Implementation Notes