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