redox_hwio-0.1.6/.cargo_vcs_info.json0000644000000001360000000000100132070ustar { "git": { "sha1": "acadac68ce44188533f0e7c5ad85ee3cf731beb9" }, "path_in_vcs": "" }redox_hwio-0.1.6/.gitignore000064400000000000000000000000221046102023000137610ustar 00000000000000Cargo.lock target redox_hwio-0.1.6/Cargo.toml0000644000000015420000000000100112070ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "redox_hwio" version = "0.1.6" authors = ["Jeremy Soller "] description = "Rust Hardware MMIO and PIO" readme = "README.md" license = "MIT" repository = "https://gitlab.redox-os.org/redox-os/hwio" [lib] name = "hwio" [dependencies.lazy_static] version = "1.4.0" optional = true [features] default = ["std"] stable = [] std = ["lazy_static"] redox_hwio-0.1.6/Cargo.toml.orig000064400000000000000000000006011046102023000146630ustar 00000000000000[package] name = "redox_hwio" version = "0.1.6" edition = "2018" description = "Rust Hardware MMIO and PIO" license = "MIT" authors = ["Jeremy Soller "] repository = "https://gitlab.redox-os.org/redox-os/hwio" [lib] name = "hwio" [dependencies] lazy_static = { version = "1.4.0", optional = true } [features] default = ["std"] stable = [] std = ["lazy_static"] redox_hwio-0.1.6/LICENSE000064400000000000000000000020561046102023000130070ustar 00000000000000MIT License Copyright (c) 2019 Jeremy Soller Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. redox_hwio-0.1.6/README.md000064400000000000000000000000431046102023000132530ustar 00000000000000# hwio Rust Hardware MMIO and PIO redox_hwio-0.1.6/src/io.rs000064400000000000000000000026661046102023000135550ustar 00000000000000// SPDX-License-Identifier: MIT use core::cmp::PartialEq; use core::ops::{BitAnd, BitOr, Not}; pub trait Io { type Value: Copy + PartialEq + BitAnd + BitOr + Not; fn read(&self) -> Self::Value; fn write(&mut self, value: Self::Value); #[inline(always)] fn readf(&self, flags: Self::Value) -> bool { (self.read() & flags) as Self::Value == flags } #[inline(always)] fn writef(&mut self, flags: Self::Value, value: bool) { let tmp: Self::Value = match value { true => self.read() | flags, false => self.read() & !flags, }; self.write(tmp); } } pub struct ReadOnly { inner: I } impl ReadOnly { pub const fn new(inner: I) -> ReadOnly { ReadOnly { inner } } #[inline(always)] pub fn read(&self) -> I::Value { self.inner.read() } #[inline(always)] pub fn readf(&self, flags: I::Value) -> bool { self.inner.readf(flags) } } pub struct WriteOnly { inner: I } impl WriteOnly { pub const fn new(inner: I) -> WriteOnly { WriteOnly { inner } } #[inline(always)] pub fn write(&mut self, value: I::Value) { self.inner.write(value) } #[inline(always)] pub fn writef(&mut self, flags: I::Value, value: bool) { self.inner.writef(flags, value) } } redox_hwio-0.1.6/src/lib.rs000064400000000000000000000002531046102023000137020ustar 00000000000000// SPDX-License-Identifier: MIT #![cfg_attr(not(feature = "std"), no_std)] pub use self::io::*; pub use self::mmio::*; pub use self::pio::*; mod io; mod mmio; mod pio; redox_hwio-0.1.6/src/mmio.rs000064400000000000000000000015411046102023000140760ustar 00000000000000// SPDX-License-Identifier: MIT use core::mem::MaybeUninit; use core::ops::{BitAnd, BitOr, Not}; use core::ptr; use super::io::Io; #[repr(packed)] pub struct Mmio { value: T, } #[allow(clippy::new_without_default)] impl Mmio { /// Create a new Mmio without initializing #[deprecated] #[allow(clippy::uninit_assumed_init)] pub fn new() -> Self { Mmio { value: unsafe { MaybeUninit::uninit().assume_init() } } } } impl Io for Mmio where T: Copy + PartialEq + BitAnd + BitOr + Not { type Value = T; fn read(&self) -> T { let raw = ptr::addr_of!(self.value); unsafe { raw.read_volatile() } } fn write(&mut self, value: T) { let raw = ptr::addr_of_mut!(self.value); unsafe { raw.write_volatile(value) }; } } redox_hwio-0.1.6/src/pio.rs000064400000000000000000000115401046102023000137240ustar 00000000000000// SPDX-License-Identifier: MIT #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] use core::arch::asm; use core::marker::PhantomData; use super::io::Io; #[cfg(all(feature = "std", 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"))))] lazy_static::lazy_static! { static ref FILE: Mutex = Mutex::new( OpenOptions::new() .read(true) .write(true) .open("/dev/port") .expect("failed to open /dev/port") ); } #[cfg(all(feature = "std", 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"))))] #[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(); } /// Generic PIO #[derive(Copy, Clone)] pub struct Pio { port: u16, value: PhantomData, } impl Pio { /// Create a PIO from a given port pub const fn new(port: u16) -> Self { Pio:: { port, value: PhantomData, } } } /// Read/Write for byte PIO impl Io for Pio { type Value = u8; #[cfg(all(feature = "std", 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"))] #[inline(always)] fn read(&self) -> u8 { let value: u8; unsafe { asm!("in al, dx", out("al") value, in("dx") self.port, options(nostack)); } value } #[cfg(all(feature = "std", 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"))] #[inline(always)] fn write(&mut self, value: u8) { unsafe { asm!("out dx, al", in("al") value, in("dx") self.port, options(nostack)); } } } /// Read/Write for word PIO impl Io for Pio { type Value = u16; #[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))] #[inline(always)] fn read(&self) -> u16 { let mut buf = [0, 0]; port_read(self.port, &mut buf); buf[0] as u16 | (buf[1] as u16) << 8 } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] fn read(&self) -> u16 { let value: u16; unsafe { asm!("in ax, dx", out("ax") value, in("dx") self.port, options(nostack)); } value } #[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))] #[inline(always)] fn write(&mut self, value: u16) { let buf = [ value as u8, (value >> 8) as u8 ]; port_write(self.port, &buf); } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] fn write(&mut self, value: u16) { unsafe { asm!("out dx, ax", in("ax") value, in("dx") self.port, options(nostack)); } } } /// Read/Write for doubleword PIO impl Io for Pio { type Value = u32; #[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))] #[inline(always)] fn read(&self) -> u32 { let mut buf = [0, 0, 0, 0]; port_read(self.port, &mut buf); buf[0] as u32 | (buf[1] as u32) << 8 | (buf[2] as u32) << 16 | (buf[3] as u32) << 24 } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] fn read(&self) -> u32 { let value: u32; unsafe { asm!("in eax, dx", out("eax") value, in("dx") self.port, options(nostack)); } value } #[cfg(all(feature = "std", not(any(target_arch = "x86", target_arch = "x86_64"))))] #[inline(always)] fn write(&mut self, value: u32) { let buf = [ value as u8, (value >> 8) as u8, (value >> 16) as u8, (value >> 24) as u8 ]; port_write(self.port, &buf); } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] fn write(&mut self, value: u32) { unsafe { asm!("out dx, eax", in("eax") value, in("dx") self.port, options(nostack)); } } }