2020-02-09 11:28:56 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-09 11:28:56 -03:00
|
|
|
*/
|
|
|
|
|
|
2022-09-02 09:59:21 -03:00
|
|
|
#include <AK/Platform.h>
|
2022-10-03 21:05:54 -03:00
|
|
|
#if ARCH(X86_64)
|
2022-10-04 07:46:11 -03:00
|
|
|
# include <Kernel/Arch/x86_64/BochsDebugOutput.h>
|
2022-09-02 09:59:21 -03:00
|
|
|
#endif
|
2021-09-16 16:23:25 -03:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2023-03-18 08:17:13 -03:00
|
|
|
#include <Kernel/Devices/Generic/ConsoleDevice.h>
|
2021-08-21 20:37:17 -03:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-06-22 12:40:16 -03:00
|
|
|
#include <Kernel/Sections.h>
|
2020-09-18 04:49:51 -03:00
|
|
|
#include <Kernel/kstdio.h>
|
2020-02-09 11:28:56 -03:00
|
|
|
|
2022-10-14 14:51:51 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2022-11-09 07:39:58 -03:00
|
|
|
Spinlock<LockRank::None> g_console_lock {};
|
2020-02-09 11:28:56 -03:00
|
|
|
|
2022-08-19 15:53:40 -03:00
|
|
|
UNMAP_AFTER_INIT NonnullLockRefPtr<ConsoleDevice> ConsoleDevice::must_create()
|
2020-08-24 22:35:19 -03:00
|
|
|
{
|
2021-09-16 16:23:25 -03:00
|
|
|
auto device_or_error = DeviceManagement::try_create_device<ConsoleDevice>();
|
|
|
|
|
VERIFY(!device_or_error.is_error());
|
|
|
|
|
return device_or_error.release_value();
|
2020-02-09 11:28:56 -03:00
|
|
|
}
|
|
|
|
|
|
2021-04-24 11:50:35 -03:00
|
|
|
UNMAP_AFTER_INIT ConsoleDevice::ConsoleDevice()
|
2020-02-09 11:28:56 -03:00
|
|
|
: CharacterDevice(5, 1)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:15:15 -03:00
|
|
|
UNMAP_AFTER_INIT ConsoleDevice::~ConsoleDevice() = default;
|
2020-02-09 11:28:56 -03:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
bool ConsoleDevice::can_read(Kernel::OpenFileDescription const&, u64) const
|
2020-02-09 11:28:56 -03:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<size_t> ConsoleDevice::read(OpenFileDescription&, u64, Kernel::UserOrKernelBuffer&, size_t)
|
2020-02-09 11:28:56 -03:00
|
|
|
{
|
|
|
|
|
// FIXME: Implement reading from the console.
|
|
|
|
|
// Maybe we could use a ring buffer for this device?
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
ErrorOr<size_t> ConsoleDevice::write(OpenFileDescription&, u64, Kernel::UserOrKernelBuffer const& data, size_t size)
|
2020-02-09 11:28:56 -03:00
|
|
|
{
|
|
|
|
|
if (!size)
|
|
|
|
|
return 0;
|
2020-09-12 00:11:07 -03:00
|
|
|
|
2021-09-01 03:44:55 -03:00
|
|
|
return data.read_buffered<256>(size, [&](ReadonlyBytes readonly_bytes) {
|
|
|
|
|
for (const auto& byte : readonly_bytes)
|
|
|
|
|
put_char(byte);
|
|
|
|
|
return readonly_bytes.size();
|
2020-09-12 00:11:07 -03:00
|
|
|
});
|
2020-02-09 11:28:56 -03:00
|
|
|
}
|
|
|
|
|
|
2021-04-24 11:50:35 -03:00
|
|
|
void ConsoleDevice::put_char(char ch)
|
2020-02-09 11:28:56 -03:00
|
|
|
{
|
2021-08-21 20:49:22 -03:00
|
|
|
Kernel::SpinlockLocker lock(g_console_lock);
|
2022-09-02 09:59:21 -03:00
|
|
|
dbgputchar(ch);
|
2020-02-09 11:28:56 -03:00
|
|
|
m_logbuffer.enqueue(ch);
|
|
|
|
|
}
|
2022-10-14 14:51:51 -03:00
|
|
|
|
|
|
|
|
}
|