Skip to content

Commit

Permalink
Untie accessor from the actual PciHeader type
Browse files Browse the repository at this point in the history
I don't know why we didn't do this in the first place - there's no reason
to not do it like this.
  • Loading branch information
IsaacWoods committed Oct 16, 2020
1 parent e70b983 commit e1201d7
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ impl PciAddress {

impl fmt::Display for PciAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:02x}-{:02x}:{:02x}.{}",
self.segment(),
self.bus(),
self.device(),
self.function()
)
write!(f, "{:02x}-{:02x}:{:02x}.{}", self.segment(), self.bus(), self.device(), self.function())
}
}

Expand Down Expand Up @@ -133,32 +126,27 @@ pub trait ConfigRegionAccess: Send {
/// | | | line | line |
/// +--------------+--------------+--------------+--------------+
/// ```
pub struct PciHeader<A>(PciAddress, PhantomData<A>)
where
A: ConfigRegionAccess;

impl<A> PciHeader<A>
where
A: ConfigRegionAccess,
{
pub fn new(address: PciAddress) -> PciHeader<A> {
PciHeader(address, PhantomData)
pub struct PciHeader(PciAddress);

impl PciHeader {
pub fn new(address: PciAddress) -> PciHeader {
PciHeader(address)
}

pub fn id(&self, access: &A) -> (VendorId, DeviceId) {
pub fn id(&self, access: &impl ConfigRegionAccess) -> (VendorId, DeviceId) {
let id = unsafe { access.read(self.0, 0x00) };
(id.get_bits(0..16) as u16, id.get_bits(16..32) as u16)
}

pub fn header_type(&self, access: &A) -> u8 {
pub fn header_type(&self, access: &impl ConfigRegionAccess) -> u8 {
/*
* Read bits 0..=6 of the Header Type. Bit 7 dictates whether the device has multiple functions and so
* isn't read here.
*/
unsafe { access.read(self.0, 0x0c) }.get_bits(16..23) as u8
}

pub fn has_multiple_functions(&self, access: &A) -> bool {
pub fn has_multiple_functions(&self, access: &impl ConfigRegionAccess) -> bool {
/*
* Reads bit 7 of the Header Type, which is 1 if the device has multiple functions.
*/
Expand All @@ -167,7 +155,7 @@ where

pub fn revision_and_class(
&self,
access: &A,
access: &impl ConfigRegionAccess,
) -> (DeviceRevision, BaseClass, SubClass, Interface) {
let field = unsafe { access.read(self.0, 0x08) };
(
Expand Down

0 comments on commit e1201d7

Please sign in to comment.