Skip to content

Commit 63744fd

Browse files
committed
serial: implement receive FIFO flush via FCR
The FIFO Control Register (FCR) controls the behavior of the receive and transmit FIFO buffers of the device. The current implementation does not emulate this register, as FIFO buffers are always enabled. However, there are two bits in this register that control flushing of said FIFOS. The transmission FIFO is already flushed by the current implementation on every write, but the receive FIFO is not. This is problematic, as some driver implementations (e.g. FreeBSD's) rely on being able to clear this buffer via the corresponding bit. Implement the correct behavior when a driver sets this bit by clearing `in_buffer`. Since there is no more data in the receive FIFO, the data-ready bit in the Line Status Register (LSR) must be cleared as well, in case it was set. Fixes: rust-vmm#83 Signed-off-by: Carlos López <[email protected]>
1 parent f65498e commit 63744fd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

vm-superio/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
# Upcoming version
4+
5+
## Changed
6+
7+
- Implemented receive FIFO flushing via the FCR register for the `Serial`
8+
device ([#83](https://github.com/rust-vmm/vm-superio/issues/83)).
9+
310
# v0.8.0
411

512
## Changed

vm-superio/src/serial.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::Trigger;
2323
const DATA_OFFSET: u8 = 0;
2424
const IER_OFFSET: u8 = 1;
2525
const IIR_OFFSET: u8 = 2;
26+
const FCR_OFFSET: u8 = IIR_OFFSET;
2627
const LCR_OFFSET: u8 = 3;
2728
const MCR_OFFSET: u8 = 4;
2829
const LSR_OFFSET: u8 = 5;
@@ -48,6 +49,8 @@ const IIR_NONE_BIT: u8 = 0b0000_0001;
4849
const IIR_THR_EMPTY_BIT: u8 = 0b0000_0010;
4950
const IIR_RDA_BIT: u8 = 0b0000_0100;
5051

52+
const FCR_FLUSH_IN_BIT: u8 = 0b0000_0010;
53+
5154
const LCR_DLAB_BIT: u8 = 0b1000_0000;
5255

5356
const LSR_DATA_READY_BIT: u8 = 0b0000_0001;
@@ -611,7 +614,14 @@ impl<T: Trigger, EV: SerialEvents, W: Write> Serial<T, EV, W> {
611614
LCR_OFFSET => self.line_control = value,
612615
MCR_OFFSET => self.modem_control = value,
613616
SCR_OFFSET => self.scratch = value,
614-
// We are not interested in writing to other offsets (such as FCR offset).
617+
FCR_OFFSET => {
618+
// Clear the receive FIFO
619+
if value & FCR_FLUSH_IN_BIT != 0 {
620+
self.in_buffer.clear();
621+
self.clear_lsr_rda_bit();
622+
self.events.in_buffer_empty();
623+
}
624+
}
615625
_ => {}
616626
}
617627
Ok(())

0 commit comments

Comments
 (0)