Struct Device

Source
pub struct Device { /* private fields */ }
Expand description

Representation of a PCI device

This struct provides low-level methods for directly reading and writing values from the PCI configuration space for this device, as well as higer-level methods for accessing standard fields.

If you know the BDF of the device you want to access, you can directly create a Device instance and use it to interact with the device:

let bdf = Bdf::new(0, 1, 0);
let dev = Device::new(bdf);

// Check vendor and device ID:
let vid = dev.vendor_id();
let did = dev.device_id();
if vid == 0x1234 && did == 0x5678 {
    // Found the device we were looking for!
}

Alternatively, you can use the iter function to enumerate all PCI devices in the system. This method automatically filters out non-existent devices, and it returns an iterator which can be chained like any other Rust iterator:

let dev = pci::iter().find(|d| d.vendor_id() == 0x1234 && d.device_id() == 0x5678);
if dev.is_none() {
    // No such device found in the system.
}

Implementations§

Source§

impl Device

Source

pub const fn new(bdf: Bdf) -> Self

Constructs a new Device instance from a given BDF identifier.

Source

pub fn read8(&self, offset: u16) -> u8

Reads an 8-bit value from this device’s PCI configuration space.

Source

pub fn write8(&self, offset: u16, val: u8)

Writes an 8-bit value to this device’s PCI configuration space.

Source

pub fn read16(&self, offset: u16) -> u16

Reads a 16-bit value from this device’s PCI configuration space.

Source

pub fn write16(&self, offset: u16, val: u16)

Writes a 16-bit value to this device’s PCI configuration space.

Source

pub fn read32(&self, offset: u16) -> u32

Reads a 32-bit value from this device’s PCI configuration space.

Source

pub fn write32(&self, offset: u16, val: u32)

Writes a 32-bit value to this device’s PCI configuration space.

Source

pub fn vendor_id(&self) -> u16

Reads and returns the PCI vendor ID.

Source

pub fn device_id(&self) -> u16

Reads and returns the PCI device ID.

Source

pub fn command(&self) -> CommandVal

Reads the command register of this device.

Source

pub fn set_command(&self, value: CommandVal)

Sets the command register of this device.

Source

pub fn status(&self) -> StatusVal

Reads the status register of this device.

Source

pub fn reset_status(&self, value: StatusVal)

Reset fields within status register of this device.

The PCI status register implements “write 1 to reset” behavior for certain fields. This method may be used to reset such fields.

Source

pub fn header_type(&self) -> u8

Reads the header type of this device.

Source

pub fn bar(&self, index: usize) -> Option<u32>

Source

pub fn set_bar(&self, index: usize, val: u32)

Source

pub fn bar_addr(&self, index: u8) -> Option<usize>

Decodes the BAR at index and returns its memory address.

  • Returns None if index is out of range, the BAR is an I/O BAR, or the BAR type is unsupported.
  • For 64-bit memory BARs, this will read the high dword from the next BAR and combine them.
Source

pub fn cap_ptr(&self) -> Option<u8>

Returns the offset of the first capability pointer, if present.

Source

pub fn capabilities(&self) -> CapIter<'_>

Iterate over this device’s capabilities list.

Source

pub fn int_line(&self) -> Option<u8>

Returns the interrupt line assigned to this device, if applicable.

Trait Implementations§

Source§

impl Clone for Device

Source§

fn clone(&self) -> Device

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Device

Source§

fn eq(&self, other: &Device) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Device

Source§

impl Eq for Device

Source§

impl StructuralPartialEq for Device

Auto Trait Implementations§

§

impl Freeze for Device

§

impl RefUnwindSafe for Device

§

impl Send for Device

§

impl Sync for Device

§

impl Unpin for Device

§

impl UnwindSafe for Device

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.