From patchwork Tue Aug 4 16:13:51 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: 9185 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 417EEBD87A for ; Tue, 4 Aug 2020 16:14:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id ECCF160555; Tue, 4 Aug 2020 18:14:24 +0200 (CEST) Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5C29660547 for ; Tue, 4 Aug 2020 18:14:21 +0200 (CEST) X-Halon-ID: 8d526a04-d66d-11ea-b48b-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac52a8.dip0.t-ipconnect.de [84.172.82.168]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id 8d526a04-d66d-11ea-b48b-0050569116f7; Tue, 04 Aug 2020 18:14:20 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Tue, 4 Aug 2020 18:13:51 +0200 Message-Id: <20200804161358.1628962-3-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200804161358.1628962-1-niklas.soderlund@ragnatech.se> References: <20200804161358.1628962-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 2/9] libcamera: sysfs: Add method to lookup firmware ID 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 systems firmware description is recorded differently in sysfs depending if the system uses OF or ACPI. Add a helper to abstract this, allowing users not to care which of the two are used. For OF-based systems the ID is the full path of the device in the device tree description. For ACPI-based systems the ID is the ACPI firmware nodes path. Both ID sources are guaranteed to be unique and persistent as long as the firmware of the system is not changed. Signed-off-by: Niklas Söderlund --- 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 72f436205d8d30e8..ef8ec55909d85bf9 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 &devicePath); +int firmwareId(const std::string &path, std::string *id); + } /* namespace sysfs */ } /* namespace libcamera */ diff --git a/src/libcamera/sysfs.cpp b/src/libcamera/sysfs.cpp index 3b2920663e9c3bcc..98be4df9d38e6fa8 100644 --- a/src/libcamera/sysfs.cpp +++ b/src/libcamera/sysfs.cpp @@ -7,9 +7,11 @@ #include "libcamera/internal/sysfs.h" +#include #include #include +#include "libcamera/internal/file.h" #include "libcamera/internal/log.h" /** @@ -43,6 +45,61 @@ std::string charDevPath(const std::string &devicePath) return dev.str(); } +/** + * \brief Try to read a device firmware ID from sysfs + * \param[in] path Path in sysfs to search + * \param[out] id Location to store ID if found + * + * A systems firmware description is recorded differently in sysfs depending if + * the system uses OF or ACPI. Add a helper to abstract this, allowing users not + * to care which of the two are used. + * + * For OF-based systems the ID is the full path of the device in the device tree + * description. For ACPI-based systems the ID is the ACPI firmware nodes path. + * Both ID sources are guaranteed to be unique and persistent as long as the + * firmware of the system is not changed. + * + * \return 0 on success or a negative error code otherwise + * \retval -EINVAL Error when looking up firmware ID + * \retval -ENODEV No firmware ID available for \a path + */ +int firmwareId(const std::string &path, std::string *id) +{ + ASSERT(id); + + /* ID lookup for OF-based systems */ + File ofFile(path + "/of_node"); + if (ofFile.exists()) { + char *ofPath = realpath(ofFile.fileName().c_str(), nullptr); + if (!ofPath) + return -EINVAL; + + *id = ofPath; + free(ofPath); + + static const std::string dropStr = "/sys/firmware/devicetree/"; + if (id->find(dropStr) == 0) + id->erase(0, dropStr.length()); + + return 0; + } + + /* ID lookup for ACPI-based systems */ + File acpiFile(path + "/firmware_node/path"); + if (acpiFile.exists()) { + std::ifstream file(acpiFile.fileName()); + if (!file.is_open()) + return -EINVAL; + + std::getline(file, *id); + file.close(); + + return 0; + } + + return -ENODEV; +} + } /* namespace sysfs */ } /* namespace libcamera */