Move PS/2 driver to userspace

This commit is contained in:
Jeremy Soller 2016-09-19 17:19:49 -06:00
parent c957c2a105
commit 0b3be623fc
14 changed files with 178 additions and 173 deletions

View file

@ -5,6 +5,7 @@ version = "0.1.0"
[dependencies] [dependencies]
bitflags = "*" bitflags = "*"
hole_list_allocator = { path = "../../alloc/hole_list_allocator" } hole_list_allocator = { path = "../../alloc/hole_list_allocator" }
io = { path = "../../drivers/io" }
ransid = { git = "https://github.com/redox-os/ransid.git", branch = "new_api" } ransid = { git = "https://github.com/redox-os/ransid.git", branch = "new_api" }
spin = "*" spin = "*"

View file

@ -1,11 +1,9 @@
use paging::ActivePageTable; use paging::ActivePageTable;
pub mod display; pub mod display;
pub mod ps2;
pub mod serial; pub mod serial;
pub unsafe fn init(active_table: &mut ActivePageTable){ pub unsafe fn init(active_table: &mut ActivePageTable){
serial::init(); serial::init();
display::init(active_table); display::init(active_table);
ps2::init();
} }

View file

@ -1,9 +0,0 @@
/// I/O functions
pub use self::io::*;
pub use self::mmio::*;
pub use self::pio::*;
mod io;
mod mmio;
mod pio;

View file

@ -15,6 +15,7 @@ extern crate hole_list_allocator as allocator;
#[macro_use] #[macro_use]
extern crate bitflags; extern crate bitflags;
extern crate io;
extern crate ransid; extern crate ransid;
extern crate spin; extern crate spin;
pub extern crate x86; pub extern crate x86;
@ -198,9 +199,6 @@ pub mod gdt;
/// Interrupt descriptor table /// Interrupt descriptor table
pub mod idt; pub mod idt;
/// IO Handling
pub mod io;
/// Interrupt instructions /// Interrupt instructions
pub mod interrupt; pub mod interrupt;

3
drivers/io/Cargo.toml Normal file
View file

@ -0,0 +1,3 @@
[package]
name = "io"
version = "0.1.0"

14
drivers/io/src/lib.rs Normal file
View file

@ -0,0 +1,14 @@
//! I/O functions
#![feature(asm)]
#![feature(const_fn)]
#![feature(core_intrinsics)]
#![no_std]
pub use self::io::*;
pub use self::mmio::*;
pub use self::pio::*;
mod io;
mod mmio;
mod pio;

View file

@ -1,5 +1,4 @@
use core::marker::PhantomData; use core::marker::PhantomData;
use x86::io;
use super::io::Io; use super::io::Io;
@ -27,13 +26,19 @@ impl Io for Pio<u8> {
/// Read /// Read
#[inline(always)] #[inline(always)]
fn read(&self) -> u8 { fn read(&self) -> u8 {
unsafe { io::inb(self.port) } let value: u8;
unsafe {
asm!("in $0, $1" : "={al}"(value) : "{dx}"(self.port) : "memory" : "intel", "volatile");
}
value
} }
/// Write /// Write
#[inline(always)] #[inline(always)]
fn write(&mut self, value: u8) { fn write(&mut self, value: u8) {
unsafe { io::outb(self.port, value) } unsafe {
asm!("out $1, $0" : : "{al}"(value), "{dx}"(self.port) : "memory" : "intel", "volatile");
}
} }
} }
@ -44,13 +49,19 @@ impl Io for Pio<u16> {
/// Read /// Read
#[inline(always)] #[inline(always)]
fn read(&self) -> u16 { fn read(&self) -> u16 {
unsafe { io::inw(self.port) } let value: u16;
unsafe {
asm!("in $0, $1" : "={ax}"(value) : "{dx}"(self.port) : "memory" : "intel", "volatile");
}
value
} }
/// Write /// Write
#[inline(always)] #[inline(always)]
fn write(&mut self, value: u16) { fn write(&mut self, value: u16) {
unsafe { io::outw(self.port, value) } unsafe {
asm!("out $1, $0" : : "{ax}"(value), "{dx}"(self.port) : "memory" : "intel", "volatile");
}
} }
} }
@ -61,12 +72,18 @@ impl Io for Pio<u32> {
/// Read /// Read
#[inline(always)] #[inline(always)]
fn read(&self) -> u32 { fn read(&self) -> u32 {
unsafe { io::inl(self.port) } let value: u32;
unsafe {
asm!("in $0, $1" : "={eax}"(value) : "{dx}"(self.port) : "memory" : "intel", "volatile");
}
value
} }
/// Write /// Write
#[inline(always)] #[inline(always)]
fn write(&mut self, value: u32) { fn write(&mut self, value: u32) {
unsafe { io::outl(self.port, value) } unsafe {
asm!("out $1, $0" : : "{eax}"(value), "{dx}"(self.port) : "memory" : "intel", "volatile");
}
} }
} }

View file

@ -2,5 +2,8 @@
name = "ps2d" name = "ps2d"
version = "0.1.0" version = "0.1.0"
[dependencies.syscall] [dependencies]
path = "../../syscall/" bitflags = "*"
io = { path = "../io/" }
spin = "*"
syscall = { path = "../../syscall/" }

View file

@ -1,5 +1,3 @@
use core::cmp;
use io::{Io, Pio, ReadOnly, WriteOnly}; use io::{Io, Pio, ReadOnly, WriteOnly};
pub unsafe fn init() { pub unsafe fn init() {
@ -79,86 +77,6 @@ enum MouseCommandData {
SetSampleRate = 0xF3, SetSampleRate = 0xF3,
} }
bitflags! {
flags MousePacketFlags: u8 {
const LEFT_BUTTON = 1,
const RIGHT_BUTTON = 1 << 1,
const MIDDLE_BUTTON = 1 << 2,
const ALWAYS_ON = 1 << 3,
const X_SIGN = 1 << 4,
const Y_SIGN = 1 << 5,
const X_OVERFLOW = 1 << 6,
const Y_OVERFLOW = 1 << 7
}
}
pub struct Ps2Mouse {
data: ReadOnly<Pio<u8>>,
mouse: [u8; 4],
mouse_i: usize,
mouse_extra: bool,
mouse_x: usize,
mouse_y: usize
}
impl Ps2Mouse {
fn new(mouse_extra: bool) -> Self {
Ps2Mouse {
data: ReadOnly::new(Pio::new(0x60)),
mouse: [0; 4],
mouse_i: 0,
mouse_extra: mouse_extra,
mouse_x: 0,
mouse_y: 0
}
}
pub fn on_irq(&mut self) {
self.mouse[self.mouse_i] = self.data.read();
self.mouse_i += 1;
let flags = MousePacketFlags::from_bits_truncate(self.mouse[0]);
if ! flags.contains(ALWAYS_ON) {
println!("MOUSE MISALIGN {:X}", self.mouse[0]);
self.mouse = [0; 4];
self.mouse_i = 0;
} else if self.mouse_i >= self.mouse.len() || (!self.mouse_extra && self.mouse_i >= 3) {
if ! flags.contains(X_OVERFLOW) && ! flags.contains(Y_OVERFLOW) {
let mut dx = self.mouse[1] as isize;
if flags.contains(X_SIGN) {
dx -= 0x100;
}
let mut dy = self.mouse[2] as isize;
if flags.contains(Y_SIGN) {
dy -= 0x100;
}
let _extra = if self.mouse_extra {
self.mouse[3]
} else {
0
};
//print!("MOUSE {:?}, {}, {}, {}\n", flags, dx, dy, extra);
if let Some(ref mut display) = *super::display::DISPLAY.lock() {
self.mouse_x = cmp::max(0, cmp::min(display.width as isize - 1, self.mouse_x as isize + dx)) as usize;
self.mouse_y = cmp::max(0, cmp::min(display.height as isize - 1, self.mouse_y as isize - dy)) as usize;
let offset = self.mouse_y * display.width + self.mouse_x;
display.onscreen[offset as usize] = 0xFF0000;
}
} else {
println!("MOUSE OVERFLOW {:X} {:X} {:X} {:X}", self.mouse[0], self.mouse[1], self.mouse[2], self.mouse[3]);
}
self.mouse = [0; 4];
self.mouse_i = 0;
}
}
}
pub struct Ps2 { pub struct Ps2 {
data: Pio<u8>, data: Pio<u8>,
status: ReadOnly<Pio<u8>>, status: ReadOnly<Pio<u8>>,
@ -166,7 +84,7 @@ pub struct Ps2 {
} }
impl Ps2 { impl Ps2 {
const fn new() -> Self { pub fn new() -> Self {
Ps2 { Ps2 {
data: Pio::new(0x60), data: Pio::new(0x60),
status: ReadOnly::new(Pio::new(0x64)), status: ReadOnly::new(Pio::new(0x64)),
@ -244,7 +162,7 @@ impl Ps2 {
self.read() self.read()
} }
fn init(&mut self) { pub fn init(&mut self) -> bool {
// Disable devices // Disable devices
self.command(Command::DisableFirst); self.command(Command::DisableFirst);
self.command(Command::DisableSecond); self.command(Command::DisableSecond);
@ -294,18 +212,6 @@ impl Ps2 {
let mouse_id = self.read(); let mouse_id = self.read();
let mouse_extra = mouse_id == 3; let mouse_extra = mouse_id == 3;
// Enable extra buttons, TODO
/*
if self.mouse_extra {
print!("SAMPLE 200 {:X}\n", self.mouse_command_data(MouseCommandData::SetSampleRate, 200));
print!("SAMPLE 200 {:X}\n", self.mouse_command_data(MouseCommandData::SetSampleRate, 200));
print!("SAMPLE 80 {:X}\n", self.mouse_command_data(MouseCommandData::SetSampleRate, 80));
print!("GET ID {:X}\n", self.mouse_command(MouseCommand::GetDeviceId));
let mouse_id = self.read();
print!("MOUSE ID: {:X} == 0x04\n", mouse_id);
}
*/
// Set sample rate to maximum // Set sample rate to maximum
assert_eq!(self.mouse_command_data(MouseCommandData::SetSampleRate, 200), 0xFA); assert_eq!(self.mouse_command_data(MouseCommandData::SetSampleRate, 200), 0xFA);
@ -325,5 +231,7 @@ impl Ps2 {
} }
self.flush_read(); self.flush_read();
mouse_extra
} }
} }

View file

@ -0,0 +1,34 @@
use std::fs::File;
use std::io::{Read, Write};
use std::mem;
use std::thread;
use keymap;
pub fn keyboard() {
let mut file = File::open("irq:1").expect("ps2d: failed to open irq:1");
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("ps2d: failed to read irq:1") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
let (scancode, pressed) = if data >= 0x80 {
(data - 0x80, false)
} else {
(data, true)
};
if pressed {
print!("{}", keymap::get_char(scancode));
}
file.write(&irqs).expect("ps2d: failed to write irq:1");
} else {
thread::yield_now();
}
}
}

View file

@ -1,77 +1,42 @@
#![feature(asm)] #![feature(asm)]
#[macro_use]
extern crate bitflags;
extern crate io;
extern crate syscall; extern crate syscall;
use std::fs::File;
use std::io::{Read, Write};
use std::mem;
use std::thread; use std::thread;
use syscall::iopl; use syscall::iopl;
mod controller;
mod keyboard;
mod keymap; mod keymap;
mod mouse;
fn keyboard() {
let mut file = File::open("irq:1").expect("pskbd: failed to open irq:1");
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("pskbd: failed to read irq:1") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
let (scancode, pressed) = if data >= 0x80 {
(data - 0x80, false)
} else {
(data, true)
};
println!("pskbd: IRQ {}: {:X}: {:X}: {}: {}", unsafe { *(irqs.as_ptr() as *const usize) }, data, scancode, keymap::get_char(scancode), pressed);
file.write(&irqs).expect("pskbd: failed to write irq:1");
} else {
thread::yield_now();
}
}
}
fn mouse() {
let mut file = File::open("irq:12").expect("psmsd: failed to open irq:12");
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("psmsd: failed to read irq:12") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
println!("psmsd: IRQ {}: {:X}", unsafe { *(irqs.as_ptr() as *const usize) }, data);
file.write(&irqs).expect("psmsd: failed to write irq:12");
} else {
thread::yield_now();
}
}
}
fn main() { fn main() {
thread::spawn(|| {
unsafe { unsafe {
iopl(3).expect("pskbd: failed to get I/O permission"); iopl(3).expect("ps2d: failed to get I/O permission");
asm!("cli" :::: "intel", "volatile"); asm!("cli" :::: "intel", "volatile");
} }
keyboard(); let extra_packet = controller::Ps2::new().init();
});
thread::spawn(|| { thread::spawn(|| {
unsafe { unsafe {
iopl(3).expect("psmsd: failed to get I/O permission"); iopl(3).expect("ps2d: failed to get I/O permission");
asm!("cli" :::: "intel", "volatile"); asm!("cli" :::: "intel", "volatile");
} }
mouse(); keyboard::keyboard();
});
thread::spawn(move || {
unsafe {
iopl(3).expect("ps2d: failed to get I/O permission");
asm!("cli" :::: "intel", "volatile");
}
mouse::mouse(extra_packet);
}); });
} }

73
drivers/ps2d/src/mouse.rs Normal file
View file

@ -0,0 +1,73 @@
use std::fs::File;
use std::io::{Read, Write};
use std::mem;
use std::thread;
bitflags! {
flags MousePacketFlags: u8 {
const LEFT_BUTTON = 1,
const RIGHT_BUTTON = 1 << 1,
const MIDDLE_BUTTON = 1 << 2,
const ALWAYS_ON = 1 << 3,
const X_SIGN = 1 << 4,
const Y_SIGN = 1 << 5,
const X_OVERFLOW = 1 << 6,
const Y_OVERFLOW = 1 << 7
}
}
pub fn mouse(extra_packet: bool) {
let mut file = File::open("irq:12").expect("ps2d: failed to open irq:12");
let mut packets = [0; 4];
let mut packet_i = 0;
loop {
let mut irqs = [0; 8];
if file.read(&mut irqs).expect("ps2d: failed to read irq:12") >= mem::size_of::<usize>() {
let data: u8;
unsafe {
asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
}
packets[packet_i] = data;
packet_i += 1;
let flags = MousePacketFlags::from_bits_truncate(packets[0]);
if ! flags.contains(ALWAYS_ON) {
println!("MOUSE MISALIGN {:X}", packets[0]);
packets = [0; 4];
packet_i = 0;
} else if packet_i >= packets.len() || (!extra_packet && packet_i >= 3) {
if ! flags.contains(X_OVERFLOW) && ! flags.contains(Y_OVERFLOW) {
let mut dx = packets[1] as isize;
if flags.contains(X_SIGN) {
dx -= 0x100;
}
let mut dy = packets[2] as isize;
if flags.contains(Y_SIGN) {
dy -= 0x100;
}
let extra = if extra_packet {
packets[3]
} else {
0
};
print!("ps2d: IRQ {:?}, {}, {}, {}\n", flags, dx, dy, extra);
} else {
println!("ps2d: overflow {:X} {:X} {:X} {:X}", packets[0], packets[1], packets[2], packets[3]);
}
packets = [0; 4];
packet_i = 0;
}
file.write(&irqs).expect("ps2d: failed to write irq:12");
} else {
thread::yield_now();
}
}
}