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
impl Device
Sourcepub const fn new(bdf: Bdf) -> Self
pub const fn new(bdf: Bdf) -> Self
Constructs a new Device
instance from a given BDF identifier.
Sourcepub fn read8(&self, offset: u16) -> u8
pub fn read8(&self, offset: u16) -> u8
Reads an 8-bit value from this device’s PCI configuration space.
Sourcepub fn write8(&self, offset: u16, val: u8)
pub fn write8(&self, offset: u16, val: u8)
Writes an 8-bit value to this device’s PCI configuration space.
Sourcepub fn read16(&self, offset: u16) -> u16
pub fn read16(&self, offset: u16) -> u16
Reads a 16-bit value from this device’s PCI configuration space.
Sourcepub fn write16(&self, offset: u16, val: u16)
pub fn write16(&self, offset: u16, val: u16)
Writes a 16-bit value to this device’s PCI configuration space.
Sourcepub fn read32(&self, offset: u16) -> u32
pub fn read32(&self, offset: u16) -> u32
Reads a 32-bit value from this device’s PCI configuration space.
Sourcepub fn write32(&self, offset: u16, val: u32)
pub fn write32(&self, offset: u16, val: u32)
Writes a 32-bit value to this device’s PCI configuration space.
Sourcepub fn command(&self) -> CommandVal
pub fn command(&self) -> CommandVal
Reads the command register of this device.
Sourcepub fn set_command(&self, value: CommandVal)
pub fn set_command(&self, value: CommandVal)
Sets the command register of this device.
Sourcepub fn reset_status(&self, value: StatusVal)
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.
Sourcepub fn header_type(&self) -> u8
pub fn header_type(&self) -> u8
Reads the header type of this device.
pub fn bar(&self, index: usize) -> Option<u32>
pub fn set_bar(&self, index: usize, val: u32)
Sourcepub fn bar_addr(&self, index: u8) -> Option<usize>
pub fn bar_addr(&self, index: u8) -> Option<usize>
Decodes the BAR at index
and returns its memory address.
- Returns
None
ifindex
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.
Sourcepub fn cap_ptr(&self) -> Option<u8>
pub fn cap_ptr(&self) -> Option<u8>
Returns the offset of the first capability pointer, if present.
Sourcepub fn capabilities(&self) -> CapIter<'_> ⓘ
pub fn capabilities(&self) -> CapIter<'_> ⓘ
Iterate over this device’s capabilities list.