mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
add platform device
Signed-off-by: Pascal Bauer <pascal.bauer@rwth-aachen.de>
This commit is contained in:
parent
dca4d4c66a
commit
9d6b81b15a
2 changed files with 106 additions and 0 deletions
52
common/include/villas/kernel/devices/platform_device.hpp
Normal file
52
common/include/villas/kernel/devices/platform_device.hpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
/* Platform Device
|
||||
*
|
||||
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <villas/kernel/devices/device.hpp>
|
||||
#include <villas/kernel/devices/driver.hpp>
|
||||
#include <villas/kernel/devices/utils.hpp>
|
||||
|
||||
namespace villas {
|
||||
namespace kernel {
|
||||
namespace devices {
|
||||
|
||||
class PlatformDevice : public Device {
|
||||
private:
|
||||
static constexpr char PROBE_DEFAULT[] = "/sys/bus/platform/drivers_probe";
|
||||
static constexpr char OVERRIDE_DEFAULT[] = "driver_override";
|
||||
|
||||
private:
|
||||
const std::filesystem::path m_path;
|
||||
const std::filesystem::path m_probe_path;
|
||||
const std::filesystem::path m_override_path;
|
||||
|
||||
public:
|
||||
PlatformDevice(const std::filesystem::path path)
|
||||
: PlatformDevice(path, std::filesystem::path(PROBE_DEFAULT),
|
||||
path / std::filesystem::path(OVERRIDE_DEFAULT)){};
|
||||
|
||||
PlatformDevice(const std::filesystem::path path,
|
||||
const std::filesystem::path probe_path,
|
||||
const std::filesystem::path override_path)
|
||||
: m_path(path), m_probe_path(probe_path),
|
||||
m_override_path(override_path){};
|
||||
|
||||
// Implement device interface
|
||||
std::optional<std::unique_ptr<Driver>> driver() const override;
|
||||
std::optional<int> iommu_group() const override;
|
||||
std::string name() const override;
|
||||
std::filesystem::path override_path() const override;
|
||||
std::filesystem::path path() const override;
|
||||
void probe() const override;
|
||||
};
|
||||
|
||||
} // namespace devices
|
||||
} // namespace kernel
|
||||
} // namespace villas
|
54
common/lib/kernel/devices/platform_device.cpp
Normal file
54
common/lib/kernel/devices/platform_device.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
/* Platform Device
|
||||
*
|
||||
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <villas/kernel/devices/platform_device.hpp>
|
||||
#include <villas/kernel/devices/generic_driver.hpp>
|
||||
|
||||
using villas::kernel::devices::Driver, villas::kernel::devices::GenericDriver;
|
||||
using villas::kernel::devices::PlatformDevice;
|
||||
using villas::kernel::devices::utils::write_to_file;
|
||||
|
||||
std::optional<std::unique_ptr<Driver>> PlatformDevice::driver() const {
|
||||
std::filesystem::path driver_symlink =
|
||||
this->m_path / std::filesystem::path("driver");
|
||||
|
||||
if (!std::filesystem::is_symlink(driver_symlink))
|
||||
return std::nullopt;
|
||||
|
||||
std::filesystem::path driver_path =
|
||||
std::filesystem::canonical(driver_symlink);
|
||||
return std::make_optional(std::make_unique<GenericDriver>(driver_path));
|
||||
}
|
||||
|
||||
std::optional<int> PlatformDevice::iommu_group() const {
|
||||
std::filesystem::path symlink =
|
||||
std::filesystem::path(this->m_path.u8string() + "/iommu_group");
|
||||
|
||||
std::filesystem::path link;
|
||||
link = std::filesystem::read_symlink(symlink);
|
||||
|
||||
std::string delimiter = "iommu_groups/";
|
||||
int pos = link.u8string().find(delimiter);
|
||||
int iommu_group = std::stoi(link.u8string().substr(pos + delimiter.length()));
|
||||
return std::make_optional<int>(iommu_group);
|
||||
}
|
||||
|
||||
std::filesystem::path PlatformDevice::path() const { return this->m_path; };
|
||||
|
||||
void PlatformDevice::probe() const {
|
||||
write_to_file(this->name(), this->m_probe_path);
|
||||
}
|
||||
|
||||
std::filesystem::path PlatformDevice::override_path() const {
|
||||
return this->m_override_path;
|
||||
}
|
||||
|
||||
std::string PlatformDevice::name() const {
|
||||
size_t pos = this->m_path.u8string().rfind('/');
|
||||
return this->m_path.u8string().substr(pos + 1);
|
||||
}
|
Loading…
Add table
Reference in a new issue