1+ /* Platform Device
2+ *
3+ * Author: Pascal Bauer <[email protected] > 4+ *
5+ * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <[email protected] > 6+ * SPDX-License-Identifier: Apache-2.0
7+ */
8+
9+ #pragma once
10+
11+ #include < filesystem>
12+ #include < villas/kernel/devices/device.hpp>
13+ #include < villas/kernel/devices/utils.hpp>
14+
15+ class PlatformDevice : public Device {
16+ private:
17+ static constexpr char PROBE_DEFAULT[] = " /sys/bus/platform/drivers_probe" ;
18+ static constexpr char OVERRIDE_DEFAULT[] = " driver_override" ;
19+
20+ public:
21+ const std::filesystem::path path;
22+ const std::filesystem::path probe_path;
23+ const std::filesystem::path override_path;
24+
25+ public:
26+ PlatformDevice (const std::filesystem::path path)
27+ : PlatformDevice(path, std::filesystem::path(PROBE_DEFAULT),
28+ path / std::filesystem::path(OVERRIDE_DEFAULT)){};
29+ PlatformDevice (const std::filesystem::path path,
30+ const std::filesystem::path probe_path,
31+ const std::filesystem::path override_path)
32+ : path(path), probe_path(probe_path), override_path(override_path){};
33+
34+ std::string name () const override {
35+ size_t pos = path.u8string ().rfind (' /' );
36+ return path.u8string ().substr (pos + 1 );
37+ }
38+
39+ std::optional<Driver> driver () const override {
40+ std::filesystem::path driver_symlink =
41+ this ->path / std::filesystem::path (" driver" );
42+
43+ if (!std::filesystem::is_symlink (driver_symlink))
44+ return std::nullopt ;
45+
46+ std::filesystem::path driver_path =
47+ std::filesystem::canonical (driver_symlink);
48+ return Driver (driver_path);
49+ };
50+
51+ int iommu_group () const override {
52+ std::filesystem::path symlink =
53+ std::filesystem::path (this ->path .u8string () + " /iommu_group" );
54+
55+ std::filesystem::path link;
56+ link = std::filesystem::read_symlink (symlink);
57+
58+ std::string delimiter = " iommu_groups/" ;
59+ int pos = link.u8string ().find (delimiter);
60+ int iommu_group =
61+ std::stoi (link.u8string ().substr (pos + delimiter.length ()));
62+ return iommu_group;
63+ }
64+
65+ void probe () const override {
66+ write_to_file (this ->name (), this ->probe_path );
67+ };
68+
69+ virtual std::filesystem::path get_override_path () const override {
70+ return this ->override_path ;
71+ };
72+ };
0 commit comments