{"id":1209,"url":"https://patchwork.libcamera.org/api/1.1/patches/1209/?format=json","web_url":"https://patchwork.libcamera.org/patch/1209/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190517005447.27171-5-niklas.soderlund@ragnatech.se>","date":"2019-05-17T00:54:40","name":"[libcamera-devel,v4,04/11] libcamera: media_device: Handle media device fd in acquire() and release()","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"633d8e06cd6b62fa54b047b60e99655021896358","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/1.1/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/1209/mbox/","series":[{"id":306,"url":"https://patchwork.libcamera.org/api/1.1/series/306/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=306","date":"2019-05-17T00:54:36","name":"libcamerea: Add support for exclusive access to cameras between processes","version":4,"mbox":"https://patchwork.libcamera.org/series/306/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/1209/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/1209/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net\n\t[195.74.38.227])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 992D461867\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 17 May 2019 02:55:11 +0200 (CEST)","from bismarck.berto.se (unknown [89.233.230.99])\n\tby bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA\n\tid 6b5ae3e8-783e-11e9-8d05-005056917f90;\n\tFri, 17 May 2019 02:55:09 +0200 (CEST)"],"X-Halon-ID":"6b5ae3e8-783e-11e9-8d05-005056917f90","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 17 May 2019 02:54:40 +0200","Message-Id":"<20190517005447.27171-5-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.21.0","In-Reply-To":"<20190517005447.27171-1-niklas.soderlund@ragnatech.se>","References":"<20190517005447.27171-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 04/11] libcamera: media_device: Handle\n\tmedia device fd in acquire() and release()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Fri, 17 May 2019 00:55:11 -0000"},"content":"To gain better control of when a file descriptor is open to the media\ndevice and reduce the work needed in pipeline handler implementations,\nhandle the file descriptor in acquire() and release().\n\nThis changes the current behavior where a file descriptor is only open\nwhen requested by the pipeline handler to that one is always open as\nlong a media device is acquired. This new behavior is desired to allow\nimplementing exclusive access to a pipeline handler between processes.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/include/media_device.h         |  2 +-\n src/libcamera/media_device.cpp               | 29 +++++++-------\n src/libcamera/pipeline/ipu3/ipu3.cpp         | 39 +++---------------\n src/libcamera/pipeline/rkisp1/rkisp1.cpp     | 42 +++++---------------\n test/media_device/media_device_link_test.cpp |  7 ----\n 5 files changed, 33 insertions(+), 86 deletions(-)","diff":"diff --git a/src/libcamera/include/media_device.h b/src/libcamera/include/media_device.h\nindex 9f038093b2b22fc7..883985055eb419dd 100644\n--- a/src/libcamera/include/media_device.h\n+++ b/src/libcamera/include/media_device.h\n@@ -27,7 +27,7 @@ public:\n \t~MediaDevice();\n \n \tbool acquire();\n-\tvoid release() { acquired_ = false; }\n+\tvoid release();\n \tbool busy() const { return acquired_; }\n \n \tint open();\ndiff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp\nindex eef348e35eb6a97f..b1cc37e840a9fa9d 100644\n--- a/src/libcamera/media_device.cpp\n+++ b/src/libcamera/media_device.cpp\n@@ -40,10 +40,9 @@ LOG_DEFINE_CATEGORY(MediaDevice)\n  * instance.\n  *\n  * The instance is created with an empty media graph. Before performing any\n- * other operation, it must be opened with the open() function and the media\n- * graph populated by calling populate(). Instances of MediaEntity, MediaPad and\n- * MediaLink are created to model the media graph, and stored in a map indexed\n- * by object id.\n+ * other operation, it must be populate by calling populate(). Instances of\n+ * MediaEntity, MediaPad and MediaLink are created to model the media graph,\n+ * and stored in a map indexed by object id.\n  *\n  * The graph is valid once successfully populated, as reported by the valid()\n  * function. It can be queried to list all entities(), or entities can be\n@@ -51,12 +50,6 @@ LOG_DEFINE_CATEGORY(MediaDevice)\n  * entity to entity through pads and links as exposed by the corresponding\n  * classes.\n  *\n- * An open media device will keep an open file handle for the underlying media\n- * controller device node. It can be closed at any time with a call to close().\n- * This will not invalidate the media graph and all cached media objects remain\n- * valid and can be accessed normally. The device can then be later reopened if\n- * needed to perform other operations that interact with the device node.\n- *\n  * Media device can be claimed for exclusive use with acquire(), released with\n  * release() and tested with busy(). This mechanism is aimed at pipeline\n  * managers to claim media devices they support during enumeration.\n@@ -66,8 +59,8 @@ LOG_DEFINE_CATEGORY(MediaDevice)\n  * \\brief Construct a MediaDevice\n  * \\param[in] deviceNode The media device node path\n  *\n- * Once constructed the media device is invalid, and must be opened and\n- * populated with open() and populate() before the media graph can be queried.\n+ * Once constructed the media device is invalid, and must be populated with\n+ * populate() before the media graph can be queried.\n  */\n MediaDevice::MediaDevice(const std::string &deviceNode)\n \t: deviceNode_(deviceNode), fd_(-1), valid_(false), acquired_(false)\n@@ -92,7 +85,8 @@ MediaDevice::~MediaDevice()\n  * busy() function.\n  *\n  * Once claimed the device shall be released by its user when not needed anymore\n- * by calling the release() function.\n+ * by calling the release() function. Acquiring the media device opens a file\n+ * descriptor to the device which is kept open until release() is called.\n  *\n  * Exclusive access is only guaranteed if all users of the media device abide by\n  * the device claiming mechanism, as it isn't enforced by the media device\n@@ -107,15 +101,22 @@ bool MediaDevice::acquire()\n \tif (acquired_)\n \t\treturn false;\n \n+\tif (open())\n+\t\treturn false;\n+\n \tacquired_ = true;\n \treturn true;\n }\n \n /**\n- * \\fn MediaDevice::release()\n  * \\brief Release a device previously claimed for exclusive use\n  * \\sa acquire(), busy()\n  */\n+void MediaDevice::release()\n+{\n+\tclose();\n+\tacquired_ = false;\n+}\n \n /**\n  * \\fn MediaDevice::busy()\ndiff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\nindex 75e878afad4e67a9..8a6a0e2721955d15 100644\n--- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n@@ -631,23 +631,10 @@ bool PipelineHandlerIPU3::match(DeviceEnumerator *enumerator)\n \t/*\n \t * Disable all links that are enabled by default on CIO2, as camera\n \t * creation enables all valid links it finds.\n-\t *\n-\t * Close the CIO2 media device after, as links are enabled and should\n-\t * not need to be changed after.\n \t */\n-\tif (cio2MediaDev_->open())\n+\tif (cio2MediaDev_->disableLinks())\n \t\treturn false;\n \n-\tif (cio2MediaDev_->disableLinks()) {\n-\t\tcio2MediaDev_->close();\n-\t\treturn false;\n-\t}\n-\n-\tif (imguMediaDev_->open()) {\n-\t\tcio2MediaDev_->close();\n-\t\treturn false;\n-\t}\n-\n \t/*\n \t * FIXME: enabled links in one ImgU instance interfere with capture\n \t * operations on the other one. This can be easily triggered by\n@@ -674,14 +661,10 @@ bool PipelineHandlerIPU3::match(DeviceEnumerator *enumerator)\n \t */\n \tret = imguMediaDev_->disableLinks();\n \tif (ret)\n-\t\tgoto error;\n+\t\treturn ret;\n \n \tret = registerCameras();\n \n-error:\n-\tcio2MediaDev_->close();\n-\timguMediaDev_->close();\n-\n \treturn ret == 0;\n }\n \n@@ -1139,29 +1122,19 @@ int ImgUDevice::enableLinks(bool enable)\n \tstd::string inputName = name_ + \" input\";\n \tint ret;\n \n-\t/* \\todo Establish rules to handle media devices open/close. */\n-\tret = media_->open();\n-\tif (ret)\n-\t\treturn ret;\n-\n \tret = linkSetup(inputName, 0, name_, PAD_INPUT, enable);\n \tif (ret)\n-\t\tgoto done;\n+\t\treturn ret;\n \n \tret = linkSetup(name_, PAD_OUTPUT, outputName, 0, enable);\n \tif (ret)\n-\t\tgoto done;\n+\t\treturn ret;\n \n \tret = linkSetup(name_, PAD_VF, viewfinderName, 0, enable);\n \tif (ret)\n-\t\tgoto done;\n+\t\treturn ret;\n \n-\tret = linkSetup(name_, PAD_STAT, statName, 0, enable);\n-\n-done:\n-\tmedia_->close();\n-\n-\treturn ret;\n+\treturn linkSetup(name_, PAD_STAT, statName, 0, enable);\n }\n \n /*------------------------------------------------------------------------------\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\nindex 134d3df4e1eb2b9b..b94d742dd6ec33a4 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n@@ -148,10 +148,6 @@ int PipelineHandlerRkISP1::configureStreams(Camera *camera,\n \t */\n \tconst MediaPad *pad = dphy_->entity()->getPadByIndex(0);\n \n-\tret = media_->open();\n-\tif (ret < 0)\n-\t\treturn ret;\n-\n \tfor (MediaLink *link : pad->links()) {\n \t\tbool enable = link->source()->entity() == sensor->entity();\n \n@@ -169,8 +165,6 @@ int PipelineHandlerRkISP1::configureStreams(Camera *camera,\n \t\t\tbreak;\n \t}\n \n-\tmedia_->close();\n-\n \tif (ret < 0)\n \t\treturn ret;\n \n@@ -352,7 +346,6 @@ int PipelineHandlerRkISP1::createCamera(MediaEntity *sensor)\n bool PipelineHandlerRkISP1::match(DeviceEnumerator *enumerator)\n {\n \tconst MediaPad *pad;\n-\tint ret;\n \n \tDeviceMatch dm(\"rkisp1\");\n \tdm.add(\"rkisp1-isp-subdev\");\n@@ -368,35 +361,27 @@ bool PipelineHandlerRkISP1::match(DeviceEnumerator *enumerator)\n \n \tmedia_->acquire();\n \n-\tret = media_->open();\n-\tif (ret < 0)\n-\t\treturn ret;\n-\n \t/* Create the V4L2 subdevices we will need. */\n \tdphy_ = V4L2Subdevice::fromEntityName(media_.get(),\n \t\t\t\t\t      \"rockchip-sy-mipi-dphy\");\n-\tret = dphy_->open();\n-\tif (ret < 0)\n-\t\tgoto done;\n+\tif (dphy_->open() < 0)\n+\t\treturn false;\n \n \tisp_ = V4L2Subdevice::fromEntityName(media_.get(), \"rkisp1-isp-subdev\");\n-\tret = isp_->open();\n-\tif (ret < 0)\n-\t\tgoto done;\n+\tif (isp_->open() < 0)\n+\t\treturn false;\n \n \t/* Locate and open the capture video node. */\n \tvideo_ = V4L2Device::fromEntityName(media_.get(), \"rkisp1_mainpath\");\n-\tret = video_->open();\n-\tif (ret < 0)\n-\t\tgoto done;\n+\tif (video_->open() < 0)\n+\t\treturn false;\n \n \tvideo_->bufferReady.connect(this, &PipelineHandlerRkISP1::bufferReady);\n \n \t/* Configure default links. */\n-\tret = initLinks();\n-\tif (ret < 0) {\n+\tif (initLinks() < 0) {\n \t\tLOG(RkISP1, Error) << \"Failed to setup links\";\n-\t\tgoto done;\n+\t\treturn false;\n \t}\n \n \t/*\n@@ -404,18 +389,13 @@ bool PipelineHandlerRkISP1::match(DeviceEnumerator *enumerator)\n \t * camera instance for each of them.\n \t */\n \tpad = dphy_->entity()->getPadByIndex(0);\n-\tif (!pad) {\n-\t\tret = -EINVAL;\n-\t\tgoto done;\n-\t}\n+\tif (!pad)\n+\t\treturn false;\n \n \tfor (MediaLink *link : pad->links())\n \t\tcreateCamera(link->source()->entity());\n \n-done:\n-\tmedia_->close();\n-\n-\treturn ret == 0;\n+\treturn true;\n }\n \n /* -----------------------------------------------------------------------------\ndiff --git a/test/media_device/media_device_link_test.cpp b/test/media_device/media_device_link_test.cpp\nindex 484d3691310f78a0..334bb44a6fc14371 100644\n--- a/test/media_device/media_device_link_test.cpp\n+++ b/test/media_device/media_device_link_test.cpp\n@@ -57,12 +57,6 @@ class MediaDeviceLinkTest : public Test\n \t\t\treturn TestSkip;\n \t\t}\n \n-\t\tif (dev_->open()) {\n-\t\t\tcerr << \"Failed to open media device at \"\n-\t\t\t     << dev_->deviceNode() << endl;\n-\t\t\treturn TestFail;\n-\t\t}\n-\n \t\treturn 0;\n \t}\n \n@@ -238,7 +232,6 @@ class MediaDeviceLinkTest : public Test\n \n \tvoid cleanup()\n \t{\n-\t\tdev_->close();\n \t\tdev_->release();\n \t}\n \n","prefixes":["libcamera-devel","v4","04/11"]}