diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-27 17:16:13 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-27 17:16:13 +0200 |
| commit | 1121cb81d66b704c7528356f450acffba4c0f62f (patch) | |
| tree | f35048dd6099aceb307a9861831ab34a89310f15 | |
| parent | dca8353027526b9e522ad8be169d9763e447b08b (diff) | |
| download | kernel-1121cb81d66b704c7528356f450acffba4c0f62f.tar.xz kernel-1121cb81d66b704c7528356f450acffba4c0f62f.zip | |
kapi: fix device resources
| -rw-r--r-- | kapi/kapi/devices/resource.hpp | 65 |
1 files changed, 46 insertions, 19 deletions
diff --git a/kapi/kapi/devices/resource.hpp b/kapi/kapi/devices/resource.hpp index 1b491225..6a682594 100644 --- a/kapi/kapi/devices/resource.hpp +++ b/kapi/kapi/devices/resource.hpp @@ -2,7 +2,9 @@ #define TEACHOS_KAPI_DEVICES_RESOURCE_HPP #include <kapi/memory.hpp> +#include <kapi/system.hpp> +#include <kstd/format.hpp> #include <kstd/units.hpp> #include <cstddef> @@ -14,8 +16,6 @@ namespace kapi::devices //! The type of a resource. enum struct resource_type { - //! An invalid resource. - invalid, //! A range of the memory-mapped I/O address space. mmio, //! A range of I/O ports. @@ -26,10 +26,45 @@ namespace kapi::devices dma, }; - struct invalid_resource +} // namespace kapi::devices + +template<> +struct kstd::formatter<kapi::devices::resource_type> +{ + constexpr auto parse(format_parse_context & context) -> format_parse_context::iterator { - constexpr auto static type = resource_type::invalid; - }; + auto it = context.begin(); + if (it != context.end() && *it != '}') + { + bits::format::error("Invalid specifier for resource_type."); + } + return it; + } + + auto format(kapi::devices::resource_type type, format_context & context) const -> void + { + switch (type) + { + case kapi::devices::resource_type::mmio: + context.push("mmio"); + break; + case kapi::devices::resource_type::port: + context.push("port"); + break; + case kapi::devices::resource_type::irq: + context.push("irq"); + break; + case kapi::devices::resource_type::dma: + context.push("dma"); + break; + default: + context.push("<unknown>"); + } + } +}; + +namespace kapi::devices +{ //! A range of the memory-mapped I/O address space. struct mmio_range @@ -111,8 +146,6 @@ namespace kapi::devices switch (lhs.type) { - case resource_type::invalid: - return true; case resource_type::mmio: return lhs.m_value.range == rhs.m_value.range; case resource_type::port: @@ -126,11 +159,6 @@ namespace kapi::devices return false; } - constexpr auto friend operator==(resource const & lhs, invalid_resource const &) noexcept -> bool - { - return lhs.type == resource_type::invalid; - } - constexpr auto friend operator==(resource const & lhs, mmio_range const & rhs) noexcept -> bool { return lhs.type == resource_type::mmio && lhs.m_value.range == rhs; @@ -155,6 +183,11 @@ namespace kapi::devices template<resource_type Type> [[nodiscard]] constexpr auto get() const { + if (Type != this->type) + { + system::panic("[OS:DEV] Invalid resource access! {} != {}", Type, type); + } + if constexpr (Type == resource_type::mmio) { return m_value.range; @@ -171,10 +204,6 @@ namespace kapi::devices { return m_value.channel; } - else - { - return m_value.invalid; - } } [[nodiscard]] constexpr auto unsafe_get() const @@ -182,13 +211,11 @@ namespace kapi::devices return m_value; } - //! The exact type of this resource. - resource_type type{resource_type::invalid}; + resource_type type; private: union { - invalid_resource invalid{}; mmio_range range; io_port port; interrupt_line irq; |
