/* * Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, Kyle Pereira * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include namespace Kernel::VirtIO { class Console; #define VIRTIO_CONSOLE_F_SIZE (1 << 0) #define VIRTIO_CONSOLE_F_MULTIPORT (1 << 1) #define VIRTIO_CONSOLE_F_EMERG_WRITE (1 << 2) class ConsolePort : public CharacterDevice { public: static ErrorOr> try_create(unsigned port, VirtIO::Console&); void handle_queue_update(Badge, u16 queue_index); void set_open(Badge, bool state) { m_open = state; } bool is_open() const { return m_open; } void init_receive_buffer(Badge); private: constexpr static size_t RINGBUFFER_SIZE = 2 * PAGE_SIZE; ConsolePort(unsigned port, VirtIO::Console& console, NonnullOwnPtr receive_buffer, NonnullOwnPtr transmit_buffer); virtual StringView class_name() const override { return "VirtIOConsolePort"sv; } virtual bool can_read(OpenFileDescription const&, u64) const override; virtual ErrorOr read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override; virtual bool can_write(OpenFileDescription const&, u64) const override; virtual ErrorOr write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override; virtual ErrorOr> open(int options) override; static unsigned next_device_id; u16 m_receive_queue {}; u16 m_transmit_queue {}; NonnullOwnPtr m_receive_buffer; NonnullOwnPtr m_transmit_buffer; VirtIO::Console& m_console; unsigned m_port; bool m_open { false }; Atomic m_receive_buffer_exhausted; }; }