From 891ced4da2eb4765504b00fe7ff2646404574c55 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 4 Jan 2024 10:49:51 +0200 Subject: [PATCH] Kernel: Move PS2Device to its own file --- kernel/CMakeLists.txt | 1 + kernel/include/kernel/Input/PS2Controller.h | 37 +--------- kernel/include/kernel/Input/PS2Device.h | 43 +++++++++++ kernel/include/kernel/Input/PS2Keyboard.h | 8 +-- kernel/kernel/Input/PS2Controller.cpp | 68 ------------------ kernel/kernel/Input/PS2Device.cpp | 80 +++++++++++++++++++++ kernel/kernel/Input/PS2Keyboard.cpp | 4 -- 7 files changed, 126 insertions(+), 115 deletions(-) create mode 100644 kernel/include/kernel/Input/PS2Device.h create mode 100644 kernel/kernel/Input/PS2Device.cpp diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 556f98ea..8ab65a88 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -33,6 +33,7 @@ set(KERNEL_SOURCES kernel/FS/TmpFS/Inode.cpp kernel/FS/VirtualFileSystem.cpp kernel/Input/PS2Controller.cpp + kernel/Input/PS2Device.cpp kernel/Input/PS2Keyboard.cpp kernel/Input/PS2Keymap.cpp kernel/InterruptController.cpp diff --git a/kernel/include/kernel/Input/PS2Controller.h b/kernel/include/kernel/Input/PS2Controller.h index 07ea3db7..1c48a7d0 100644 --- a/kernel/include/kernel/Input/PS2Controller.h +++ b/kernel/include/kernel/Input/PS2Controller.h @@ -7,42 +7,7 @@ namespace Kernel::Input { - class PS2Controller; - - class PS2Device : public CharacterDevice, public Interruptable - { - public: - PS2Device(PS2Controller&); - virtual ~PS2Device() {} - - virtual void send_initialize() = 0; - - bool append_command_queue(uint8_t command); - bool append_command_queue(uint8_t command, uint8_t data); - virtual void handle_irq() final override; - - virtual void handle_byte(uint8_t) = 0; - virtual void handle_device_command_response(uint8_t) = 0; - - virtual BAN::StringView name() const override { return m_name; } - - protected: - void update(); - - private: - enum class State - { - Normal, - WaitingAck, - }; - - private: - const BAN::String m_name; - - PS2Controller& m_controller; - State m_state = State::Normal; - BAN::CircularQueue m_command_queue; - }; + class PS2Device; class PS2Controller { diff --git a/kernel/include/kernel/Input/PS2Device.h b/kernel/include/kernel/Input/PS2Device.h new file mode 100644 index 00000000..a5ce2796 --- /dev/null +++ b/kernel/include/kernel/Input/PS2Device.h @@ -0,0 +1,43 @@ +#include + +namespace Kernel::Input +{ + + class PS2Device : public CharacterDevice, public Interruptable + { + public: + PS2Device(PS2Controller&); + virtual ~PS2Device() {} + + virtual void send_initialize() = 0; + + bool append_command_queue(uint8_t command); + bool append_command_queue(uint8_t command, uint8_t data); + virtual void handle_irq() final override; + + virtual void handle_byte(uint8_t) = 0; + virtual void handle_device_command_response(uint8_t) = 0; + + virtual BAN::StringView name() const final override { return m_name; } + virtual dev_t rdev() const final override { return m_rdev; } + + protected: + void update(); + + private: + enum class State + { + Normal, + WaitingAck, + }; + + private: + const BAN::String m_name; + const dev_t m_rdev; + + PS2Controller& m_controller; + State m_state = State::Normal; + BAN::CircularQueue m_command_queue; + }; + +} \ No newline at end of file diff --git a/kernel/include/kernel/Input/PS2Keyboard.h b/kernel/include/kernel/Input/PS2Keyboard.h index 04e6681e..3ee2fa46 100644 --- a/kernel/include/kernel/Input/PS2Keyboard.h +++ b/kernel/include/kernel/Input/PS2Keyboard.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -41,15 +41,9 @@ namespace Kernel::Input Semaphore m_semaphore; - public: - virtual dev_t rdev() const override { return m_rdev; } - protected: virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; virtual bool has_data_impl() const override; - - private: - const dev_t m_rdev; }; } \ No newline at end of file diff --git a/kernel/kernel/Input/PS2Controller.cpp b/kernel/kernel/Input/PS2Controller.cpp index 6abc87ff..626adc34 100644 --- a/kernel/kernel/Input/PS2Controller.cpp +++ b/kernel/kernel/Input/PS2Controller.cpp @@ -248,72 +248,4 @@ namespace Kernel::Input return {}; } - PS2Device::PS2Device(PS2Controller& controller) - : CharacterDevice(0440, 0, 901) - , m_name(BAN::String::formatted("input{}", DevFileSystem::get().get_next_input_device())) - , m_controller(controller) - { } - - bool PS2Device::append_command_queue(uint8_t command) - { - if (m_command_queue.size() + 1 >= m_command_queue.capacity()) - return false; - m_command_queue.push(command); - return true; - } - - bool PS2Device::append_command_queue(uint8_t command, uint8_t data) - { - if (m_command_queue.size() + 2 >= m_command_queue.capacity()) - return false; - m_command_queue.push(command); - m_command_queue.push(data); - return true; - } - - void PS2Device::handle_irq() - { - uint8_t byte = IO::inb(PS2::IOPort::DATA); - - // NOTE: This implementation does not allow using commands - // that respond with more bytes than ACK - switch (m_state) - { - case State::WaitingAck: - { - switch (byte) - { - case PS2::Response::ACK: - m_command_queue.pop(); - m_state = State::Normal; - break; - case PS2::Response::RESEND: - m_state = State::Normal; - break; - default: - handle_device_command_response(byte); - break; - } - break; - } - case State::Normal: - { - handle_byte(byte); - break; - } - } - - update(); - } - - void PS2Device::update() - { - if (m_state == State::WaitingAck) - return; - if (m_command_queue.empty()) - return; - m_state = State::WaitingAck; - m_controller.send_byte(this, m_command_queue.front()); - } - } \ No newline at end of file diff --git a/kernel/kernel/Input/PS2Device.cpp b/kernel/kernel/Input/PS2Device.cpp new file mode 100644 index 00000000..ac20a8c1 --- /dev/null +++ b/kernel/kernel/Input/PS2Device.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +#include + +namespace Kernel::Input +{ + + PS2Device::PS2Device(PS2Controller& controller) + : CharacterDevice(0440, 0, 901) + , m_name(BAN::String::formatted("input{}", DevFileSystem::get().get_next_input_device())) + , m_rdev(makedev(DevFileSystem::get().get_next_dev(), 0)) + , m_controller(controller) + { } + + bool PS2Device::append_command_queue(uint8_t command) + { + if (m_command_queue.size() + 1 >= m_command_queue.capacity()) + return false; + m_command_queue.push(command); + return true; + } + + bool PS2Device::append_command_queue(uint8_t command, uint8_t data) + { + if (m_command_queue.size() + 2 >= m_command_queue.capacity()) + return false; + m_command_queue.push(command); + m_command_queue.push(data); + return true; + } + + void PS2Device::handle_irq() + { + uint8_t byte = IO::inb(PS2::IOPort::DATA); + + // NOTE: This implementation does not allow using commands + // that respond with more bytes than ACK + switch (m_state) + { + case State::WaitingAck: + { + switch (byte) + { + case PS2::Response::ACK: + m_command_queue.pop(); + m_state = State::Normal; + break; + case PS2::Response::RESEND: + m_state = State::Normal; + break; + default: + handle_device_command_response(byte); + break; + } + break; + } + case State::Normal: + { + handle_byte(byte); + break; + } + } + + update(); + } + + void PS2Device::update() + { + if (m_state == State::WaitingAck) + return; + if (m_command_queue.empty()) + return; + m_state = State::WaitingAck; + m_controller.send_byte(this, m_command_queue.front()); + } + +} \ No newline at end of file diff --git a/kernel/kernel/Input/PS2Keyboard.cpp b/kernel/kernel/Input/PS2Keyboard.cpp index a33e4f04..48dfc945 100644 --- a/kernel/kernel/Input/PS2Keyboard.cpp +++ b/kernel/kernel/Input/PS2Keyboard.cpp @@ -3,11 +3,8 @@ #include #include #include -#include #include -#include - #define SET_MASK(byte, mask, on_off) ((on_off) ? ((byte) | (mask)) : ((byte) & ~(mask))) #define TOGGLE_MASK(byte, mask) ((byte) ^ (mask)) @@ -24,7 +21,6 @@ namespace Kernel::Input PS2Keyboard::PS2Keyboard(PS2Controller& controller) : PS2Device(controller) - , m_rdev(makedev(DevFileSystem::get().get_next_dev(), 0)) { } void PS2Keyboard::send_initialize()