[{"id":11880,"web_url":"https://patchwork.libcamera.org/comment/11880/","msgid":"<20200805132717.GB6751@pendragon.ideasonboard.com>","date":"2020-08-05T13:27:17","subject":"Re: [libcamera-devel] [PATCH v8 2/9] libcamera: sysfs: Add helper\n\tto lookup device firmware node path","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Wed, Aug 05, 2020 at 12:48:53PM +0200, Niklas Söderlund wrote:\n> A system's firmware description is recorded differently in sysfs\n> depending if the system uses DT or ACPI. Add a helper to abstract\n> this, allowing users not to care which of the two are used.\n> \n> For DT-based systems, the path is the full name of the DT node that\n> represents the device. For ACPI-based systems, the path is the absolute\n> namespace path to the ACPI object that represents the device. In both\n> cases, the path is guaranteed to be unique and persistent as long as the\n> system firmware is not modified.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n> * Changes since v7\n> - Rename function to firmwareNodePath()\n> - Rename argument to device\n> - Return firmware path instead of passing storage as argument\n> - Update documentation\n> - Keep leading slash for DT-based paths\n> - Use File::exists()\n> ---\n>  include/libcamera/internal/sysfs.h |  2 ++\n>  src/libcamera/sysfs.cpp            | 57 ++++++++++++++++++++++++++++++\n>  2 files changed, 59 insertions(+)\n> \n> diff --git a/include/libcamera/internal/sysfs.h b/include/libcamera/internal/sysfs.h\n> index 247a376ab7e0e8ee..bc6c1620b55d43a2 100644\n> --- a/include/libcamera/internal/sysfs.h\n> +++ b/include/libcamera/internal/sysfs.h\n> @@ -15,6 +15,8 @@ namespace sysfs {\n>  \n>  std::string charDevPath(const std::string &deviceNode);\n>  \n> +std::string firmwareNodePath(const std::string &device);\n> +\n>  } /* namespace sysfs */\n>  \n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/sysfs.cpp b/src/libcamera/sysfs.cpp\n> index 398df2c2be114908..39fe0c468f5e703a 100644\n> --- a/src/libcamera/sysfs.cpp\n> +++ b/src/libcamera/sysfs.cpp\n> @@ -7,10 +7,12 @@\n>  \n>  #include \"libcamera/internal/sysfs.h\"\n>  \n> +#include <fstream>\n>  #include <sstream>\n>  #include <sys/stat.h>\n>  #include <sys/sysmacros.h>\n>  \n> +#include \"libcamera/internal/file.h\"\n>  #include \"libcamera/internal/log.h\"\n>  \n>  /**\n> @@ -47,6 +49,61 @@ std::string charDevPath(const std::string &deviceNode)\n>  \treturn dev.str();\n>  }\n>  \n> +/**\n> + * \\brief Retrieve the path of the firmware node for a device\n> + * \\param[in] device Path in sysfs to search\n> + *\n> + * Physical devices in a system are described by the system firmware. Depending\n> + * on the type of platform, devices are identified using different naming\n> + * schemes. The Linux kernel abstract those differences with \"firmware nodes\".\n> + * This function retrieves the firmware node path corresponding to the\n> + * \\a device.\n> + *\n> + * For DT-based systems, the path is the full name of the DT node that\n> + * represents the device. For ACPI-based systems, the path is the absolute\n> + * namespace path to the ACPI object that represents the device. In both cases,\n> + * the path is guaranteed to be unique and persistent as long as the system\n> + * firmware is not modified.\n> + *\n> + * \\return The firmware node path on success or an empty string on failure\n> + */\n> +std::string firmwareNodePath(const std::string &device)\n> +{\n> +\tstd::string fwPath, node;\n> +\n> +\t/* Lookup for DT-based systems */\n> +\tnode = device + \"/of_node\";\n> +\tif (File::exists(node)) {\n> +\t\tchar *ofPath = realpath(node.c_str(), nullptr);\n> +\t\tif (!ofPath)\n> +\t\t\treturn {};\n> +\n> +\t\tfwPath = ofPath;\n> +\t\tfree(ofPath);\n> +\n> +\t\tstatic const std::string dropStr = \"/sys/firmware/devicetree\";\n> +\t\tif (fwPath.find(dropStr) == 0)\n> +\t\t\tfwPath.erase(0, dropStr.length());\n> +\n> +\t\treturn fwPath;\n\nMaybe a bit more efficient\n\n\t\tstatic const char prefix[] = \"/sys/firmware/devicetree\";\n\t\tif (strncmp(ofPath, prefix, strlen(prefix)) == 0)\n\t\t\tfwPath = ofPath + strlen(prefix);\n\t\telse\n\t\t\tfwPath = ofPath\n\n\t\tfree(ofPath);\n\n\t\treturn fwPath;\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\t}\n> +\n> +\t/* Lookup for ACPI-based systems */\n> +\tnode = device + \"/firmware_node/path\";\n> +\tif (File::exists(node)) {\n> +\t\tstd::ifstream file(node);\n> +\t\tif (!file.is_open())\n> +\t\t\treturn {};\n> +\n> +\t\tstd::getline(file, fwPath);\n> +\t\tfile.close();\n> +\n> +\t\treturn fwPath;\n> +\t}\n> +\n> +\treturn {};\n> +}\n> +\n>  } /* namespace sysfs */\n>  \n>  } /* namespace libcamera */","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 23B0EBD86F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  5 Aug 2020 13:27:32 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AD7FC60599;\n\tWed,  5 Aug 2020 15:27:31 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D346E6039D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  5 Aug 2020 15:27:29 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5A6BE2C0;\n\tWed,  5 Aug 2020 15:27:29 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"TwYFlHQf\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1596634049;\n\tbh=Xb7rURitUsRjk1G+wfP9Cv7n8oSKMmerXa/Ov1Et3jg=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=TwYFlHQf4K0SCt5kyfNVksvXE8b6UXCDr9G/o+yPdLpdkzjqik7xUvbuwTH5MVpzh\n\tTZws5jf3bje46B0P5bOOea0e99BGJ9REri9Zjmh1DrOsDXWxLb4+KWKKLnErWu8Lq/\n\tC1AM77uvoKgIkYoop9vMBa9nikn8ma1YGa+BLUNo=","Date":"Wed, 5 Aug 2020 16:27:17 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Message-ID":"<20200805132717.GB6751@pendragon.ideasonboard.com>","References":"<20200805104900.2172763-1-niklas.soderlund@ragnatech.se>\n\t<20200805104900.2172763-3-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200805104900.2172763-3-niklas.soderlund@ragnatech.se>","Subject":"Re: [libcamera-devel] [PATCH v8 2/9] libcamera: sysfs: Add helper\n\tto lookup device firmware node path","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]