Kernel: Add a put_char(char) method to SerialDevice

This can be used to print a single char to the serial port the
SerialDevice instance handles.
This commit is contained in:
Idan Horowitz 2021-04-23 17:25:40 +03:00 committed by Andreas Kling
parent c75ca4ea8f
commit a5699a141d
Notes: sideshowbarker 2024-07-18 17:57:09 +09:00
2 changed files with 17 additions and 1 deletions

View file

@ -56,11 +56,24 @@ KResultOr<size_t> SerialDevice::write(FileDescription&, u64, const UserOrKernelB
return buffer.read_buffered<128>(size, [&](u8 const* data, size_t data_size) {
for (size_t i = 0; i < data_size; i++)
IO::out8(m_base_addr, data[i]);
put_char(data[i]);
return data_size;
});
}
void SerialDevice::put_char(char ch)
{
while ((get_line_status() & EmptyTransmitterHoldingRegister) == 0)
;
if (ch == '\n' && !m_last_put_char_was_carriage_return)
IO::out8(m_base_addr, '\r');
IO::out8(m_base_addr, ch);
m_last_put_char_was_carriage_return = (ch == '\r');
}
String SerialDevice::device_name() const
{
return String::formatted("ttyS{}", minor() - 64);

View file

@ -27,6 +27,8 @@ public:
virtual bool can_write(const FileDescription&, size_t) const override;
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
void put_char(char);
enum InterruptEnable {
LowPowerMode = 0x01 << 5,
SleepMode = 0x01 << 4,
@ -129,6 +131,7 @@ private:
WordLength m_word_length { EightBits };
bool m_break_enable { false };
u8 m_modem_control { 0 };
bool m_last_put_char_was_carriage_return { false };
};
}