Skip to content

Commit

Permalink
Only read or write config fields which are readable or writable.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Nov 27, 2024
1 parent ef316c6 commit 2b97721
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
39 changes: 26 additions & 13 deletions src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ pub mod mmio;
pub mod pci;
mod some;

use crate::{PhysAddr, Result, PAGE_SIZE};
use crate::{
volatile::{VolatileReadable, VolatileWritable},
PhysAddr, Result, PAGE_SIZE,
};
use bitflags::{bitflags, Flags};
use core::{fmt::Debug, ops::BitAnd};
use log::debug;
Expand Down Expand Up @@ -235,45 +238,55 @@ impl From<u8> for DeviceType {
/// Wrapper for Transport::read_config_space with an extra dummy parameter to force the correct type
/// to be inferred.
#[inline(always)]
pub(crate) fn read_help<T: Transport, V: FromBytes>(
transport: &T,
offset: usize,
_dummy_v: Option<V>,
) -> Result<V> {
pub(crate) fn read_help<T, V, R>(transport: &T, offset: usize, _dummy_r: Option<R>) -> Result<V>
where
T: Transport,
V: FromBytes,
*const R: VolatileReadable<V>,
{
transport.read_config_space(offset)
}

/// Wrapper for Transport::write_config_space with an extra dummy parameter to force the correct
/// type to be inferred.
#[inline(always)]
pub(crate) fn write_help<T: Transport, V: Immutable + IntoBytes>(
pub(crate) fn write_help<T, V, W>(
transport: &mut T,
offset: usize,
value: V,
_dummy_v: Option<V>,
) -> Result<()> {
_dummy_w: Option<W>,
) -> Result<()>
where
T: Transport,
V: Immutable + IntoBytes,
*mut W: VolatileWritable<V>,
{
transport.write_config_space(offset, value)
}

/// Reads the given field of the given struct from the device config space via the given transport.
macro_rules! read_config {
($transport:expr, $struct:ty, $field:ident) => {{
let dummy_struct: Option<$struct> = None;
let dummy_v = dummy_struct.map(|s| s.$field.0);
crate::transport::read_help(&$transport, core::mem::offset_of!($struct, $field), dummy_v)
let dummy_field = dummy_struct.map(|s| s.$field);
crate::transport::read_help(
&$transport,
core::mem::offset_of!($struct, $field),
dummy_field,
)
}};
}

/// Writes the given field of the given struct from the device config space via the given transport.
macro_rules! write_config {

Check warning on line 281 in src/transport/mod.rs

View workflow job for this annotation

GitHub Actions / build

unused macro definition: `write_config`

Check warning on line 281 in src/transport/mod.rs

View workflow job for this annotation

GitHub Actions / build

unused macro definition: `write_config`
($transport:expr, $struct:ty, $field:ident, $value:expr) => {{
let dummy_struct: Option<$struct> = None;
let dummy_v = dummy_struct.map(|s| s.$field.0);
let dummy_field = dummy_struct.map(|s| s.$field);
crate::transport::write_help(
&mut $transport,
core::mem::offset_of!($struct, $field),
$value,
dummy_v,
dummy_field,
)
}};
}
Expand Down
2 changes: 1 addition & 1 deletion src/volatile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct WriteOnly<T: Copy>(pub(crate) T);
/// An MMIO register which may be both read and written.
#[derive(Default)]
#[repr(transparent)]
pub struct Volatile<T: Copy>(pub(crate) T);
pub struct Volatile<T: Copy>(T);

impl<T: Copy> Volatile<T> {
/// Construct a new instance for testing.
Expand Down

0 comments on commit 2b97721

Please sign in to comment.