diff --git a/kernel/src/fs/devices/ConsoleDevice.cpp b/kernel/src/fs/devices/ConsoleDevice.cpp index 2449e1c0..594261c2 100644 --- a/kernel/src/fs/devices/ConsoleDevice.cpp +++ b/kernel/src/fs/devices/ConsoleDevice.cpp @@ -1,6 +1,7 @@ #include "fs/devices/ConsoleDevice.h" #include "Log.h" #include "arch/Keyboard.h" +#include "fs/devices/DeviceRegistry.h" #include "memory/MemoryManager.h" #include "thread/Scheduler.h" #include "video/TextConsole.h" @@ -14,9 +15,10 @@ static Buffer g_console_input; static bool g_eof { false }; static bool g_echo { true }; -Result> ConsoleDevice::create() +Result ConsoleDevice::create() { - return (SharedPtr)TRY(make_shared()); + auto device = (SharedPtr)TRY(make_shared()); + return DeviceRegistry::register_special_device(DeviceRegistry::Console, 0, device); } Result ConsoleDevice::read(u8* buf, usize, usize length) const diff --git a/kernel/src/fs/devices/ConsoleDevice.h b/kernel/src/fs/devices/ConsoleDevice.h index d8cb8ce5..4a17f832 100644 --- a/kernel/src/fs/devices/ConsoleDevice.h +++ b/kernel/src/fs/devices/ConsoleDevice.h @@ -5,7 +5,7 @@ class ConsoleDevice : public Device { public: // Initializer for DeviceRegistry. - static Result> create(); + static Result create(); Result read(u8*, usize, usize) const override; diff --git a/kernel/src/fs/devices/DeviceRegistry.cpp b/kernel/src/fs/devices/DeviceRegistry.cpp index 67d5e6ed..b9d56179 100644 --- a/kernel/src/fs/devices/DeviceRegistry.cpp +++ b/kernel/src/fs/devices/DeviceRegistry.cpp @@ -8,7 +8,7 @@ struct DeviceDescriptor { - device_create_func_t initializer; + SharedPtr device; u32 major; u32 minor; }; @@ -17,17 +17,17 @@ Vector g_available_devices = {}; namespace DeviceRegistry { - Result> create_special_device(u32 major, u32 minor) + Result> fetch_special_device(u32 major, u32 minor) { for (const auto& descriptor : g_available_devices) { - if (descriptor.major == major && descriptor.minor == minor) return descriptor.initializer(); + if (descriptor.major == major && descriptor.minor == minor) return descriptor.device; } return err(ENODEV); } - Result register_special_device(u32 major, u32 minor, device_create_func_t initializer) + Result register_special_device(u32 major, u32 minor, SharedPtr device) { for (const auto& descriptor : g_available_devices) { @@ -36,17 +36,15 @@ namespace DeviceRegistry kdbgln("DeviceRegistry: registered new device type %u:%u", major, minor); - return g_available_devices.try_append( - DeviceDescriptor { .initializer = initializer, .major = major, .minor = minor }); + return g_available_devices.try_append(DeviceDescriptor { .device = device, .major = major, .minor = minor }); } Result init() { - // Hardcode default devices. - TRY(register_special_device(DeviceMajorTypes::Memory, 0, NullDevice::create)); - TRY(register_special_device(DeviceMajorTypes::Memory, 1, ZeroDevice::create)); - TRY(register_special_device(DeviceMajorTypes::Console, 0, ConsoleDevice::create)); - TRY(register_special_device(DeviceMajorTypes::Framebuffer, 0, FramebufferDevice::create)); + NullDevice::create(); + ZeroDevice::create(); + ConsoleDevice::create(); + FramebufferDevice::create(); return {}; } diff --git a/kernel/src/fs/devices/DeviceRegistry.h b/kernel/src/fs/devices/DeviceRegistry.h index b004246b..330c22ae 100644 --- a/kernel/src/fs/devices/DeviceRegistry.h +++ b/kernel/src/fs/devices/DeviceRegistry.h @@ -3,8 +3,6 @@ #include "fs/devices/Device.h" #include -typedef Result> (*device_create_func_t)(void); - namespace DeviceRegistry { enum DeviceMajorTypes : u32 @@ -15,9 +13,9 @@ namespace DeviceRegistry Framebuffer = 3, }; - Result> create_special_device(u32 major, u32 minor); + Result> fetch_special_device(u32 major, u32 minor); - Result register_special_device(u32 major, u32 minor, device_create_func_t initializer); + Result register_special_device(u32 major, u32 minor, SharedPtr device); Result init(); } diff --git a/kernel/src/fs/devices/FramebufferDevice.cpp b/kernel/src/fs/devices/FramebufferDevice.cpp index 852c8395..5a060cbe 100644 --- a/kernel/src/fs/devices/FramebufferDevice.cpp +++ b/kernel/src/fs/devices/FramebufferDevice.cpp @@ -3,9 +3,10 @@ #include #include -Result> FramebufferDevice::create() +Result FramebufferDevice::create() { - return (SharedPtr)TRY(make_shared()); + auto device = (SharedPtr)TRY(make_shared()); + return DeviceRegistry::register_special_device(DeviceRegistry::Framebuffer, 0, device); } Result FramebufferDevice::read(u8*, usize, usize) const diff --git a/kernel/src/fs/devices/FramebufferDevice.h b/kernel/src/fs/devices/FramebufferDevice.h index 6d293725..9e65d622 100644 --- a/kernel/src/fs/devices/FramebufferDevice.h +++ b/kernel/src/fs/devices/FramebufferDevice.h @@ -5,7 +5,7 @@ class FramebufferDevice : public Device { public: // Initializer for DeviceRegistry. - static Result> create(); + static Result create(); Result read(u8*, usize, usize) const override; diff --git a/kernel/src/fs/devices/NullDevice.cpp b/kernel/src/fs/devices/NullDevice.cpp index 9d3d0094..0fd8efee 100644 --- a/kernel/src/fs/devices/NullDevice.cpp +++ b/kernel/src/fs/devices/NullDevice.cpp @@ -1,6 +1,7 @@ #include "fs/devices/NullDevice.h" -Result> NullDevice::create() +Result NullDevice::create() { - return (SharedPtr)TRY(make_shared()); + auto device = (SharedPtr)TRY(make_shared()); + return DeviceRegistry::register_special_device(DeviceRegistry::Memory, 0, device); } diff --git a/kernel/src/fs/devices/NullDevice.h b/kernel/src/fs/devices/NullDevice.h index 358b46c2..319aeb7e 100644 --- a/kernel/src/fs/devices/NullDevice.h +++ b/kernel/src/fs/devices/NullDevice.h @@ -5,7 +5,7 @@ class NullDevice : public Device { public: // Initializer for DeviceRegistry. - static Result> create(); + static Result create(); Result read(u8*, usize, usize) const override { diff --git a/kernel/src/fs/devices/ZeroDevice.cpp b/kernel/src/fs/devices/ZeroDevice.cpp index f1a4c151..57403234 100644 --- a/kernel/src/fs/devices/ZeroDevice.cpp +++ b/kernel/src/fs/devices/ZeroDevice.cpp @@ -1,6 +1,7 @@ #include "fs/devices/ZeroDevice.h" -Result> ZeroDevice::create() +Result ZeroDevice::create() { - return (SharedPtr)TRY(make_shared()); + auto device = (SharedPtr)TRY(make_shared()); + return DeviceRegistry::register_special_device(DeviceRegistry::Memory, 1, device); } diff --git a/kernel/src/fs/devices/ZeroDevice.h b/kernel/src/fs/devices/ZeroDevice.h index 33da86d8..a2aa7b88 100644 --- a/kernel/src/fs/devices/ZeroDevice.h +++ b/kernel/src/fs/devices/ZeroDevice.h @@ -5,7 +5,7 @@ class ZeroDevice : public Device { public: // Initializer for DeviceRegistry. - static Result> create(); + static Result create(); Result read(u8* buf, usize, usize length) const override { diff --git a/kernel/src/fs/tmpfs/FileSystem.cpp b/kernel/src/fs/tmpfs/FileSystem.cpp index d441be44..a73077e8 100644 --- a/kernel/src/fs/tmpfs/FileSystem.cpp +++ b/kernel/src/fs/tmpfs/FileSystem.cpp @@ -39,7 +39,7 @@ namespace TmpFS Result> FileSystem::create_device_inode(u32 major, u32 minor) { - SharedPtr device = TRY(DeviceRegistry::create_special_device(major, minor)); + SharedPtr device = TRY(DeviceRegistry::fetch_special_device(major, minor)); SharedPtr inode = TRY(make_shared()); inode->set_fs(*this, {});