Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 16 | #include "sysfs.hpp" |
| 17 | |
William A. Kennington III | f9aff80 | 2021-05-05 12:06:24 -0700 | [diff] [blame] | 18 | #include <fmt/format.h> |
| 19 | |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 20 | #include <algorithm> |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 21 | #include <cerrno> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 22 | #include <cstdlib> |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 23 | #include <filesystem> |
Brad Bishop | 68c43b2 | 2017-08-28 16:24:00 -0400 | [diff] [blame] | 24 | #include <fstream> |
William A. Kennington III | f9aff80 | 2021-05-05 12:06:24 -0700 | [diff] [blame] | 25 | #include <string> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 26 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 27 | using namespace std::string_literals; |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 28 | namespace fs = std::filesystem; |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 29 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 30 | namespace sysfs |
| 31 | { |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 32 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 33 | static const auto emptyString = ""s; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 34 | static constexpr auto ofRoot = "/sys/firmware/devicetree/base"; |
| 35 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 36 | std::string findPhandleMatch(const std::string& iochanneldir, |
| 37 | const std::string& phandledir) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 38 | { |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 39 | // TODO: At the moment this method only supports device trees |
| 40 | // with iio-hwmon nodes with a single sensor. Typically |
| 41 | // device trees are defined with all the iio sensors in a |
| 42 | // single iio-hwmon node so it would be nice to add support |
| 43 | // for lists of phandles (with variable sized entries) via |
| 44 | // libfdt or something like that, so that users are not |
| 45 | // forced into implementing unusual looking device trees |
| 46 | // with multiple iio-hwmon nodes - one for each sensor. |
| 47 | |
| 48 | fs::path ioChannelsPath{iochanneldir}; |
| 49 | ioChannelsPath /= "io-channels"; |
| 50 | |
| 51 | if (!fs::exists(ioChannelsPath)) |
| 52 | { |
| 53 | return emptyString; |
| 54 | } |
| 55 | |
| 56 | uint32_t ioChannelsValue; |
| 57 | std::ifstream ioChannelsFile(ioChannelsPath); |
| 58 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 59 | ioChannelsFile.read(reinterpret_cast<char*>(&ioChannelsValue), |
| 60 | sizeof(ioChannelsValue)); |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 61 | |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 62 | for (const auto& ofInst : fs::recursive_directory_iterator(phandledir)) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 63 | { |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 64 | auto path = ofInst.path(); |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 65 | if ("phandle" != path.filename()) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 66 | { |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 67 | continue; |
| 68 | } |
| 69 | std::ifstream pHandleFile(path); |
| 70 | uint32_t pHandleValue; |
Brandon Wyman | 4eb9858 | 2017-05-24 14:24:00 -0500 | [diff] [blame] | 71 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 72 | pHandleFile.read(reinterpret_cast<char*>(&pHandleValue), |
| 73 | sizeof(pHandleValue)); |
Brandon Wyman | 4eb9858 | 2017-05-24 14:24:00 -0500 | [diff] [blame] | 74 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 75 | if (ioChannelsValue == pHandleValue) |
| 76 | { |
| 77 | return path; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 81 | return emptyString; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 84 | std::string findCalloutPath(const std::string& instancePath) |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 85 | { |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 86 | // Follow the hwmon instance (/sys/class/hwmon/hwmon<N>) |
| 87 | // /sys/devices symlink. |
| 88 | fs::path devPath{instancePath}; |
| 89 | devPath /= "device"; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 90 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 91 | try |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 92 | { |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 93 | devPath = fs::canonical(devPath); |
| 94 | } |
| 95 | catch (const std::system_error& e) |
| 96 | { |
| 97 | return emptyString; |
| 98 | } |
| 99 | |
| 100 | // See if the device is backed by the iio-hwmon driver. |
| 101 | fs::path p{devPath}; |
| 102 | p /= "driver"; |
| 103 | p = fs::canonical(p); |
| 104 | |
| 105 | if (p.filename() != "iio_hwmon") |
| 106 | { |
| 107 | // Not backed by iio-hwmon. The device pointed to |
| 108 | // is the callout device. |
| 109 | return devPath; |
| 110 | } |
| 111 | |
| 112 | // Find the DT path to the iio-hwmon platform device. |
| 113 | fs::path ofDevPath{devPath}; |
| 114 | ofDevPath /= "of_node"; |
| 115 | |
| 116 | try |
| 117 | { |
| 118 | ofDevPath = fs::canonical(ofDevPath); |
| 119 | } |
| 120 | catch (const std::system_error& e) |
| 121 | { |
| 122 | return emptyString; |
| 123 | } |
| 124 | |
| 125 | // Search /sys/bus/iio/devices for the phandle in io-channels. |
| 126 | // If a match is found, use the corresponding /sys/devices |
| 127 | // iio device as the callout device. |
| 128 | static constexpr auto iioDevices = "/sys/bus/iio/devices"; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 129 | for (const auto& iioDev : fs::recursive_directory_iterator(iioDevices)) |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 130 | { |
| 131 | p = iioDev.path(); |
| 132 | p /= "of_node"; |
| 133 | |
| 134 | try |
| 135 | { |
| 136 | p = fs::canonical(p); |
| 137 | } |
| 138 | catch (const std::system_error& e) |
| 139 | { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | auto match = findPhandleMatch(ofDevPath, p); |
| 144 | auto n = match.rfind('/'); |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 145 | if (n != std::string::npos) |
| 146 | { |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 147 | // This is the iio device referred to by io-channels. |
| 148 | // Remove iio:device<N>. |
| 149 | try |
| 150 | { |
| 151 | return fs::canonical(iioDev).parent_path(); |
| 152 | } |
| 153 | catch (const std::system_error& e) |
| 154 | { |
| 155 | return emptyString; |
| 156 | } |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 160 | return emptyString; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Matt Spinler | 5c014d2 | 2019-04-16 09:13:14 -0500 | [diff] [blame] | 163 | std::string findHwmonFromOFPath(const std::string& ofNode) |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 164 | { |
| 165 | static constexpr auto hwmonRoot = "/sys/class/hwmon"; |
| 166 | |
Matt Spinler | 5c014d2 | 2019-04-16 09:13:14 -0500 | [diff] [blame] | 167 | auto fullOfPath = fs::path(ofRoot) / fs::path(ofNode).relative_path(); |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 168 | |
| 169 | for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot)) |
| 170 | { |
| 171 | auto path = hwmonInst.path(); |
| 172 | path /= "of_node"; |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 173 | |
| 174 | try |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 175 | { |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 176 | path = fs::canonical(path); |
| 177 | } |
| 178 | catch (const std::system_error& e) |
| 179 | { |
| 180 | // realpath may encounter ENOENT (Hwmon |
| 181 | // instances have a nasty habit of |
| 182 | // going away without warning). |
| 183 | continue; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 186 | if (path == fullOfPath) |
| 187 | { |
| 188 | return hwmonInst.path(); |
| 189 | } |
| 190 | |
| 191 | // Try to find HWMON instance via phandle values. |
| 192 | // Used for IIO device drivers. |
| 193 | auto matchpath = findPhandleMatch(path, fullOfPath); |
| 194 | if (!matchpath.empty()) |
| 195 | { |
| 196 | return hwmonInst.path(); |
| 197 | } |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 200 | return emptyString; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Matt Spinler | 5c014d2 | 2019-04-16 09:13:14 -0500 | [diff] [blame] | 203 | std::string findHwmonFromDevPath(const std::string& devPath) |
Matt Spinler | 626df17 | 2018-03-05 12:03:55 -0600 | [diff] [blame] | 204 | { |
| 205 | fs::path p{"/sys"}; |
Matt Spinler | 5c014d2 | 2019-04-16 09:13:14 -0500 | [diff] [blame] | 206 | p /= fs::path(devPath).relative_path(); |
Matt Spinler | 626df17 | 2018-03-05 12:03:55 -0600 | [diff] [blame] | 207 | p /= "hwmon"; |
| 208 | |
| 209 | try |
| 210 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 211 | // This path is also used as a filesystem path to an environment |
| 212 | // file, and that has issues with ':'s in the path so they've |
| 213 | // been converted to '--'s. Convert them back now. |
Matt Spinler | 626df17 | 2018-03-05 12:03:55 -0600 | [diff] [blame] | 214 | size_t pos = 0; |
| 215 | std::string path = p; |
| 216 | while ((pos = path.find("--")) != std::string::npos) |
| 217 | { |
| 218 | path.replace(pos, 2, ":"); |
| 219 | } |
| 220 | |
Patrick Venture | 0956017 | 2018-10-13 13:25:15 -0700 | [diff] [blame] | 221 | auto dir_iter = fs::directory_iterator(path); |
| 222 | auto hwmonInst = std::find_if( |
| 223 | dir_iter, end(dir_iter), [](const fs::directory_entry& d) { |
| 224 | return (d.path().filename().string().find("hwmon") != |
| 225 | std::string::npos); |
| 226 | }); |
| 227 | if (hwmonInst != end(dir_iter)) |
Matt Spinler | 626df17 | 2018-03-05 12:03:55 -0600 | [diff] [blame] | 228 | { |
Patrick Venture | 0956017 | 2018-10-13 13:25:15 -0700 | [diff] [blame] | 229 | return hwmonInst->path(); |
Matt Spinler | 626df17 | 2018-03-05 12:03:55 -0600 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | catch (const std::exception& e) |
| 233 | { |
William A. Kennington III | f9aff80 | 2021-05-05 12:06:24 -0700 | [diff] [blame] | 234 | fmt::print(stderr, |
| 235 | "Unable to find hwmon directory from the dev path: {}\n", |
| 236 | devPath.c_str()); |
Matt Spinler | 626df17 | 2018-03-05 12:03:55 -0600 | [diff] [blame] | 237 | } |
| 238 | return emptyString; |
| 239 | } |
| 240 | |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 241 | } // namespace sysfs |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 242 | |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 243 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |