1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use hwio::{Io, Pio};
use crate::{
Error,
Timeout,
timeout
};
pub struct Pmc<T: Timeout> {
data: Pio<u8>,
cmd: Pio<u8>,
timeout: T,
}
impl<T: Timeout> Pmc<T> {
pub unsafe fn new(base: u16, timeout: T) -> Self {
Self {
data: Pio::new(base),
cmd: Pio::new(base + 4),
timeout,
}
}
pub unsafe fn can_read(&mut self) -> bool {
self.cmd.read() & 1 == 1
}
pub unsafe fn can_write(&mut self) -> bool {
self.cmd.read() & 2 == 0
}
pub unsafe fn command(&mut self, data: u8) -> Result<(), Error> {
if self.can_write() {
self.cmd.write(data);
Ok(())
} else {
Err(Error::WouldBlock)
}
}
pub unsafe fn read(&mut self) -> Result<u8, Error> {
if self.can_read() {
Ok(self.data.read())
} else {
Err(Error::WouldBlock)
}
}
pub unsafe fn write(&mut self, data: u8) -> Result<(), Error> {
if self.can_write() {
self.data.write(data);
Ok(())
} else {
Err(Error::WouldBlock)
}
}
pub unsafe fn acpi_read(&mut self, addr: u8) -> Result<u8, Error> {
self.timeout.reset();
timeout!(self.timeout, self.command(0x80))?;
timeout!(self.timeout, self.write(addr))?;
timeout!(self.timeout, self.read())
}
}