diff --git a/common/include/villas/kernel/devices/generic_driver.hpp b/common/include/villas/kernel/devices/generic_driver.hpp new file mode 100644 index 000000000..20d0c613e --- /dev/null +++ b/common/include/villas/kernel/devices/generic_driver.hpp @@ -0,0 +1,52 @@ +/* GenericDriver - Works for standard Linux drivers + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include + +namespace villas { +namespace kernel { +namespace devices { + +class GenericDriver : public Driver { +private: + static constexpr char BIND_DEFAULT[] = "bind"; + static constexpr char UNBIND_DEFAULT[] = "unbind"; + +public: + const std::filesystem::path path; + +private: + const std::filesystem::path bind_path; + const std::filesystem::path unbind_path; + +public: + GenericDriver(const std::filesystem::path path) + : GenericDriver(path, path / std::filesystem::path(BIND_DEFAULT), + path / std::filesystem::path(UNBIND_DEFAULT)){}; + + GenericDriver(const std::filesystem::path path, + const std::filesystem::path bind_path, + const std::filesystem::path unbind_path) + : path(path), bind_path(bind_path), unbind_path(unbind_path){}; + +public: + virtual void attach(const Device &device) const = 0; + virtual void bind(const Device &device) const = 0; + virtual std::string name() const = 0; + virtual void override(const Device &device) const = 0; + virtual void unbind(const Device &device) const = 0; +}; + +} // namespace devices +} // namespace kernel +} // namespace villas \ No newline at end of file diff --git a/common/lib/kernel/devices/generic_driver.cpp b/common/lib/kernel/devices/generic_driver.cpp new file mode 100644 index 000000000..9e0de6d98 --- /dev/null +++ b/common/lib/kernel/devices/generic_driver.cpp @@ -0,0 +1,40 @@ +/* GenericDriver + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include + +using villas::kernel::devices::Device, villas::kernel::devices::GenericDriver; +using villas::kernel::devices::utils::write_to_file; + +void GenericDriver::attach(const Device &device) const { + if (device.driver().has_value()) { + device.driver().value()->unbind(device); + } + this->override(device); + device.probe(); +} + +void GenericDriver::bind(const Device &device) const { + write_to_file(device.name(), this->bind_path); +} + +std::string GenericDriver::name() const { + size_t pos = path.u8string().rfind('/'); + return path.u8string().substr(pos + 1); +} + +void GenericDriver::override(const Device &device) const { + write_to_file(this->name(), device.override_path()); +} + +void GenericDriver::unbind(const Device &device) const { + write_to_file(device.name(), this->unbind_path); +}