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