2021-01-02 20:56:36 +03:00
|
|
|
/*
|
2021-06-23 14:19:24 +03:00
|
|
|
* Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
|
2021-04-15 12:39:48 +03:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-01-02 20:56:36 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-02 20:56:36 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-27 12:24:50 +03:00
|
|
|
#include <Kernel/Bus/VirtIO/ConsolePort.h>
|
|
|
|
#include <Kernel/Bus/VirtIO/Device.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/RingBuffer.h>
|
2021-01-02 20:56:36 +03:00
|
|
|
|
2021-08-27 12:18:13 +03:00
|
|
|
namespace Kernel::VirtIO {
|
|
|
|
class Console
|
|
|
|
: public VirtIO::Device
|
|
|
|
, public RefCounted<Console> {
|
|
|
|
friend VirtIO::ConsolePort;
|
2021-01-02 20:56:36 +03:00
|
|
|
|
|
|
|
public:
|
2021-09-23 10:20:54 +03:00
|
|
|
static NonnullRefPtr<Console> must_create(PCI::DeviceIdentifier const&);
|
2021-08-27 12:18:13 +03:00
|
|
|
virtual ~Console() override = default;
|
2021-01-02 20:56:36 +03:00
|
|
|
|
2021-08-27 11:41:27 +03:00
|
|
|
virtual StringView purpose() const override { return class_name(); }
|
2021-06-23 14:19:24 +03:00
|
|
|
|
|
|
|
unsigned device_id() const
|
|
|
|
{
|
|
|
|
return m_device_id;
|
|
|
|
}
|
2021-06-05 07:52:35 +03:00
|
|
|
|
2021-09-04 08:42:31 +03:00
|
|
|
virtual void initialize() override;
|
|
|
|
|
2021-01-02 20:56:36 +03:00
|
|
|
private:
|
2021-10-03 01:24:00 +03:00
|
|
|
virtual StringView class_name() const override { return "VirtIOConsole"sv; }
|
2021-09-23 10:20:54 +03:00
|
|
|
explicit Console(PCI::DeviceIdentifier const&);
|
2021-06-23 14:19:24 +03:00
|
|
|
enum class ControlEvent : u16 {
|
|
|
|
DeviceReady = 0,
|
|
|
|
DeviceAdd = 1,
|
|
|
|
PortReady = 3,
|
|
|
|
ConsolePort = 4,
|
|
|
|
PortOpen = 6,
|
|
|
|
};
|
|
|
|
struct [[gnu::packed]] ControlMessage {
|
|
|
|
u32 id;
|
|
|
|
u16 event;
|
|
|
|
u16 value;
|
|
|
|
|
|
|
|
enum class Status : u16 {
|
|
|
|
Success = 1,
|
|
|
|
Failure = 0
|
|
|
|
};
|
2021-01-02 20:56:36 +03:00
|
|
|
|
2021-06-23 14:19:24 +03:00
|
|
|
enum class PortStatus : u16 {
|
|
|
|
Open = 1,
|
|
|
|
Close = 0
|
|
|
|
};
|
|
|
|
};
|
2021-01-02 20:56:36 +03:00
|
|
|
|
2021-06-23 14:19:24 +03:00
|
|
|
constexpr static u16 CONTROL_RECEIVEQ = 2;
|
|
|
|
constexpr static u16 CONTROL_TRANSMITQ = 3;
|
|
|
|
constexpr static size_t CONTROL_MESSAGE_SIZE = sizeof(ControlMessage);
|
|
|
|
constexpr static size_t CONTROL_BUFFER_SIZE = CONTROL_MESSAGE_SIZE * 32;
|
2021-01-02 20:56:36 +03:00
|
|
|
|
2021-04-15 12:12:06 +03:00
|
|
|
virtual bool handle_device_config_change() override;
|
2021-04-15 12:39:48 +03:00
|
|
|
virtual void handle_queue_update(u16 queue_index) override;
|
2021-01-02 20:56:36 +03:00
|
|
|
|
2021-08-27 12:18:13 +03:00
|
|
|
Vector<RefPtr<ConsolePort>> m_ports;
|
2021-06-23 14:19:24 +03:00
|
|
|
void setup_multiport();
|
|
|
|
void process_control_message(ControlMessage message);
|
|
|
|
void write_control_message(ControlMessage message);
|
|
|
|
void send_open_control_message(unsigned port_number, bool open);
|
|
|
|
|
|
|
|
unsigned m_device_id;
|
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
OwnPtr<Memory::RingBuffer> m_control_transmit_buffer;
|
|
|
|
OwnPtr<Memory::RingBuffer> m_control_receive_buffer;
|
2021-06-23 14:19:24 +03:00
|
|
|
|
|
|
|
WaitQueue m_control_wait_queue;
|
2021-04-15 12:39:48 +03:00
|
|
|
|
|
|
|
static unsigned next_device_id;
|
2021-01-02 20:56:36 +03:00
|
|
|
};
|
|
|
|
}
|