2023-03-18 09:10:33 +01:00
|
|
|
#include "fs/devices/DeviceRegistry.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "fs/devices/ConsoleDevice.h"
|
2023-04-21 18:18:15 +02:00
|
|
|
#include "fs/devices/FramebufferDevice.h"
|
2023-03-18 09:10:33 +01:00
|
|
|
#include "fs/devices/NullDevice.h"
|
2023-03-30 21:19:16 +02:00
|
|
|
#include "fs/devices/ZeroDevice.h"
|
2023-03-18 09:10:33 +01:00
|
|
|
#include <luna/Vector.h>
|
|
|
|
|
|
|
|
struct DeviceDescriptor
|
|
|
|
{
|
2023-05-09 18:31:27 +02:00
|
|
|
SharedPtr<Device> device;
|
2023-03-18 09:10:33 +01:00
|
|
|
u32 major;
|
|
|
|
u32 minor;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<DeviceDescriptor> g_available_devices = {};
|
|
|
|
|
|
|
|
namespace DeviceRegistry
|
|
|
|
{
|
2023-05-09 18:31:27 +02:00
|
|
|
Result<SharedPtr<Device>> fetch_special_device(u32 major, u32 minor)
|
2023-03-18 09:10:33 +01:00
|
|
|
{
|
|
|
|
for (const auto& descriptor : g_available_devices)
|
|
|
|
{
|
2023-05-09 18:31:27 +02:00
|
|
|
if (descriptor.major == major && descriptor.minor == minor) return descriptor.device;
|
2023-03-18 09:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err(ENODEV);
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:31:27 +02:00
|
|
|
Result<void> register_special_device(u32 major, u32 minor, SharedPtr<Device> device)
|
2023-03-18 09:10:33 +01:00
|
|
|
{
|
|
|
|
for (const auto& descriptor : g_available_devices)
|
|
|
|
{
|
|
|
|
if (descriptor.major == major && descriptor.minor == minor) return err(EEXIST);
|
|
|
|
}
|
|
|
|
|
2023-04-14 21:10:38 +02:00
|
|
|
kdbgln("DeviceRegistry: registered new device type %u:%u", major, minor);
|
2023-03-18 09:10:33 +01:00
|
|
|
|
2023-05-09 18:31:27 +02:00
|
|
|
return g_available_devices.try_append(DeviceDescriptor { .device = device, .major = major, .minor = minor });
|
2023-03-18 09:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> init()
|
|
|
|
{
|
2023-05-09 18:31:27 +02:00
|
|
|
NullDevice::create();
|
|
|
|
ZeroDevice::create();
|
|
|
|
ConsoleDevice::create();
|
|
|
|
FramebufferDevice::create();
|
2023-03-18 09:10:33 +01:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|