Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pio: Add FreeBSD Support #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ name = "hwio"

[dependencies]
lazy_static = { version = "1.4.0", optional = true }
nix = { version = "0.25.0", optional = true }

[features]
default = ["std"]
stable = []
std = ["lazy_static"]
std = ["lazy_static", "nix"]
126 changes: 109 additions & 17 deletions src/pio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,35 @@ use core::marker::PhantomData;

use super::io::Io;

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", target_os = "freebsd"))]
use nix::ioctl_readwrite;
#[cfg(all(feature = "std", target_os = "freebsd"))]
use std::os::fd::AsRawFd;


#[cfg(all(feature = "std", target_os = "freebsd"))]
#[repr(C)]
pub struct IoDevPioReq {
access: u32,
port: u32,
width: u32,
val: u32,
}
#[cfg(all(feature = "std", target_os = "freebsd"))]
ioctl_readwrite!(iodev_rw, b'I', 0, IoDevPioReq);
#[cfg(all(feature = "std", target_os = "freebsd"))]
const IODEV_PIO_READ: u32 = 0;
#[cfg(all(feature = "std", target_os = "freebsd"))]
const IODEV_PIO_WRITE: u32 = 1;

#[cfg(all(feature = "std", any(target_os = "freebsd", not(any(target_arch = "x86", target_arch = "x86_64")))))]
use std::{
fs::{File, OpenOptions},
io::{Read, Seek, SeekFrom, Write},
sync::Mutex,
};

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", not(any(target_os = "freebsd", target_arch = "x86", target_arch = "x86_64"))))]
lazy_static::lazy_static! {
static ref FILE: Mutex<File> = Mutex::new(
OpenOptions::new()
Expand All @@ -25,22 +46,93 @@ lazy_static::lazy_static! {
);
}

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", target_os = "freebsd"))]
lazy_static::lazy_static! {
static ref FILE: Mutex<File> = Mutex::new(
OpenOptions::new()
.read(true)
.write(true)
.open("/dev/io")
.expect("failed to open /dev/io")
);
}

#[cfg(all(feature = "std", target_os = "linux", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[inline(always)]
pub fn port_read(port: u16, buf: &mut [u8]) {
let mut file = FILE.lock().unwrap();
file.seek(SeekFrom::Start(port as u64)).unwrap();
file.read_exact(buf).unwrap();
}

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", target_os = "linux", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[inline(always)]
pub fn port_write(port: u16, buf: &[u8]) {
let mut file = FILE.lock().unwrap();
file.seek(SeekFrom::Start(port as u64)).unwrap();
file.write_all(buf).unwrap();
}

#[cfg(all(feature = "std", target_os = "freebsd"))]
#[inline(always)]
pub fn port_read(port: u16, buf: &mut [u8]) {
let mut file = FILE.lock().unwrap();
let fd = file.as_raw_fd();

let mut req = IoDevPioReq {
access: IODEV_PIO_READ,
port: port as u32,
width: buf.len() as u32,
val: 0,
};
unsafe {
let _res = iodev_rw(fd, &mut req).unwrap();
}

match buf.len() {
1 => {
buf[0] = req.val as u8;
},
2 => {
let val = u16::to_le_bytes(req.val as u16);
buf[0] = val[0];
buf[1] = val[1];
},
4 => {
let val = u32::to_le_bytes(req.val);
buf[0] = val[0];
buf[1] = val[1];
buf[2] = val[2];
buf[3] = val[3];
},
_ => panic!("Unsupported"),
}
}

#[cfg(all(feature = "std", target_os = "freebsd"))]
#[inline(always)]
pub fn port_write(port: u16, buf: &[u8]) {
let mut file = FILE.lock().unwrap();
let fd = file.as_raw_fd();

let val = match buf.len() {
1 => buf[0] as u32,
2 => u16::from_le_bytes([buf[0], buf[1]]) as u32,
4 => u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]),
_ => panic!("Unsupported"),
};

let mut req = IoDevPioReq {
access: IODEV_PIO_WRITE,
port: port as u32,
width: buf.len() as u32,
val
};
unsafe {
let _res = iodev_rw(fd, &mut req).unwrap();
}
}

/// Generic PIO
#[derive(Copy, Clone)]
pub struct Pio<T> {
Expand All @@ -62,15 +154,15 @@ impl<T> Pio<T> {
impl Io for Pio<u8> {
type Value = u8;

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", any(target_os = "freebsd", not(any(target_arch = "x86", target_arch = "x86_64")))))]
#[inline(always)]
fn read(&self) -> u8 {
let mut buf = [0];
port_read(self.port, &mut buf);
buf[0]
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(not(target_os = "freebsd"), any(target_arch = "x86", target_arch = "x86_64")))]
#[inline(always)]
fn read(&self) -> u8 {
let value: u8;
Expand All @@ -80,14 +172,14 @@ impl Io for Pio<u8> {
value
}

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", any(target_os = "freebsd", not(any(target_arch = "x86", target_arch = "x86_64")))))]
#[inline(always)]
fn write(&mut self, value: u8) {
let buf = [value];
port_write(self.port, &buf);
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(not(target_os = "freebsd"), any(target_arch = "x86", target_arch = "x86_64")))]
#[inline(always)]
fn write(&mut self, value: u8) {
unsafe {
Expand All @@ -100,7 +192,7 @@ impl Io for Pio<u8> {
impl Io for Pio<u16> {
type Value = u16;

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", any(target_os = "freebsd", not(any(target_arch = "x86", target_arch = "x86_64")))))]
#[inline(always)]
fn read(&self) -> u16 {
let mut buf = [0, 0];
Expand All @@ -109,7 +201,7 @@ impl Io for Pio<u16> {
(buf[1] as u16) << 8
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(not(target_os = "freebsd"), any(target_arch = "x86", target_arch = "x86_64")))]
#[inline(always)]
fn read(&self) -> u16 {
let value: u16;
Expand All @@ -119,7 +211,7 @@ impl Io for Pio<u16> {
value
}

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", any(target_os = "freebsd", not(any(target_arch = "x86", target_arch = "x86_64")))))]
#[inline(always)]
fn write(&mut self, value: u16) {
let buf = [
Expand All @@ -129,7 +221,7 @@ impl Io for Pio<u16> {
port_write(self.port, &buf);
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(not(target_os = "freebsd"), any(target_arch = "x86", target_arch = "x86_64")))]
#[inline(always)]
fn write(&mut self, value: u16) {
unsafe {
Expand All @@ -138,11 +230,11 @@ impl Io for Pio<u16> {
}
}

/// Read/Write for doubleword PIO
// Read/Write for doubleword PIO
impl Io for Pio<u32> {
type Value = u32;

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", any(target_os = "freebsd", not(any(target_arch = "x86", target_arch = "x86_64")))))]
#[inline(always)]
fn read(&self) -> u32 {
let mut buf = [0, 0, 0, 0];
Expand All @@ -153,7 +245,7 @@ impl Io for Pio<u32> {
(buf[3] as u32) << 24
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(not(target_os = "freebsd"), any(target_arch = "x86", target_arch = "x86_64")))]
#[inline(always)]
fn read(&self) -> u32 {
let value: u32;
Expand All @@ -163,7 +255,7 @@ impl Io for Pio<u32> {
value
}

#[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))]
#[cfg(all(feature = "std", any(target_os = "freebsd", not(any(target_arch = "x86", target_arch = "x86_64")))))]
#[inline(always)]
fn write(&mut self, value: u32) {
let buf = [
Expand All @@ -175,7 +267,7 @@ impl Io for Pio<u32> {
port_write(self.port, &buf);
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(not(target_os = "freebsd"), any(target_arch = "x86", target_arch = "x86_64")))]
#[inline(always)]
fn write(&mut self, value: u32) {
unsafe {
Expand Down