From patchwork Wed Aug 5 10:48:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 9209 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D0861BD86F for ; Wed, 5 Aug 2020 10:49:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A9B3A6055E; Wed, 5 Aug 2020 12:49:16 +0200 (CEST) Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8D94F60555 for ; Wed, 5 Aug 2020 12:49:12 +0200 (CEST) X-Halon-ID: 4bd20296-d709-11ea-a39b-005056917f90 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac52a8.dip0.t-ipconnect.de [84.172.82.168]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id 4bd20296-d709-11ea-a39b-005056917f90; Wed, 05 Aug 2020 12:49:11 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Wed, 5 Aug 2020 12:48:53 +0200 Message-Id: <20200805104900.2172763-3-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200805104900.2172763-1-niklas.soderlund@ragnatech.se> References: <20200805104900.2172763-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v8 2/9] libcamera: sysfs: Add helper to lookup device firmware node path X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" A system's firmware description is recorded differently in sysfs depending if the system uses DT or ACPI. Add a helper to abstract this, allowing users not to care which of the two are used. For DT-based systems, the path is the full name of the DT node that represents the device. For ACPI-based systems, the path is the absolute namespace path to the ACPI object that represents the device. In both cases, the path is guaranteed to be unique and persistent as long as the system firmware is not modified. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- * Changes since v7 - Rename function to firmwareNodePath() - Rename argument to device - Return firmware path instead of passing storage as argument - Update documentation - Keep leading slash for DT-based paths - Use File::exists() --- include/libcamera/internal/sysfs.h | 2 ++ src/libcamera/sysfs.cpp | 57 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/libcamera/internal/sysfs.h b/include/libcamera/internal/sysfs.h index 247a376ab7e0e8ee..bc6c1620b55d43a2 100644 --- a/include/libcamera/internal/sysfs.h +++ b/include/libcamera/internal/sysfs.h @@ -15,6 +15,8 @@ namespace sysfs { std::string charDevPath(const std::string &deviceNode); +std::string firmwareNodePath(const std::string &device); + } /* namespace sysfs */ } /* namespace libcamera */ diff --git a/src/libcamera/sysfs.cpp b/src/libcamera/sysfs.cpp index 398df2c2be114908..39fe0c468f5e703a 100644 --- a/src/libcamera/sysfs.cpp +++ b/src/libcamera/sysfs.cpp @@ -7,10 +7,12 @@ #include "libcamera/internal/sysfs.h" +#include #include #include #include +#include "libcamera/internal/file.h" #include "libcamera/internal/log.h" /** @@ -47,6 +49,61 @@ std::string charDevPath(const std::string &deviceNode) return dev.str(); } +/** + * \brief Retrieve the path of the firmware node for a device + * \param[in] device Path in sysfs to search + * + * Physical devices in a system are described by the system firmware. Depending + * on the type of platform, devices are identified using different naming + * schemes. The Linux kernel abstract those differences with "firmware nodes". + * This function retrieves the firmware node path corresponding to the + * \a device. + * + * For DT-based systems, the path is the full name of the DT node that + * represents the device. For ACPI-based systems, the path is the absolute + * namespace path to the ACPI object that represents the device. In both cases, + * the path is guaranteed to be unique and persistent as long as the system + * firmware is not modified. + * + * \return The firmware node path on success or an empty string on failure + */ +std::string firmwareNodePath(const std::string &device) +{ + std::string fwPath, node; + + /* Lookup for DT-based systems */ + node = device + "/of_node"; + if (File::exists(node)) { + char *ofPath = realpath(node.c_str(), nullptr); + if (!ofPath) + return {}; + + fwPath = ofPath; + free(ofPath); + + static const std::string dropStr = "/sys/firmware/devicetree"; + if (fwPath.find(dropStr) == 0) + fwPath.erase(0, dropStr.length()); + + return fwPath; + } + + /* Lookup for ACPI-based systems */ + node = device + "/firmware_node/path"; + if (File::exists(node)) { + std::ifstream file(node); + if (!file.is_open()) + return {}; + + std::getline(file, fwPath); + file.close(); + + return fwPath; + } + + return {}; +} + } /* namespace sysfs */ } /* namespace libcamera */