Show a patch.

GET /api/1.1/patches/21076/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 21076,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/21076/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21076/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20240830111207.46455-2-hdegoede@redhat.com>",
    "date": "2024-08-30T11:12:05",
    "name": "[v3,1/3] pipeline_handler: Add acquireDevice() function to mirror existing releaseDevice()",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "a0f7ba3297741a2a5dbc22b419dcaca1e3086a64",
    "submitter": {
        "id": 102,
        "url": "https://patchwork.libcamera.org/api/1.1/people/102/?format=api",
        "name": "Hans de Goede",
        "email": "hdegoede@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21076/mbox/",
    "series": [
        {
            "id": 4549,
            "url": "https://patchwork.libcamera.org/api/1.1/series/4549/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4549",
            "date": "2024-08-30T11:12:04",
            "name": "Fix uvcvideo pipelinehandler keeping /dev/video# open",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/4549/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21076/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21076/checks/",
    "tags": {},
    "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 22292C324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 30 Aug 2024 11:12:26 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BE7606345A;\n\tFri, 30 Aug 2024 13:12:25 +0200 (CEST)",
            "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B6D2961E4A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 30 Aug 2024 13:12:23 +0200 (CEST)",
            "from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com\n\t(ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63])\n\tby relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n\tcipher=TLS_AES_256_GCM_SHA384) id us-mta-588-APu_f6ujNW6Njdk_g-1y1w-1;\n\tFri, 30 Aug 2024 07:12:18 -0400",
            "from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com\n\t(mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com\n\t[10.30.177.15])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTPS id 658971955F4A; Fri, 30 Aug 2024 11:12:17 +0000 (UTC)",
            "from localhost.localdomain (unknown [10.39.193.217])\n\tby mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTP id 982931955F1B; Fri, 30 Aug 2024 11:12:15 +0000 (UTC)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"JDtAbYfI\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1725016342;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=2icJPm/+cr4coLeQO81/v2T2vP41Ot7pV5eyZfqXWcg=;\n\tb=JDtAbYfIceffbImoAzfBYP1rXl3uzo2oKB7YnyDTwB8vxKk39Ur6rWCSmRVRrJeJonfoI5\n\tVxjLzA0xl4nq91rMtD7UURRKpRALxMVyIxnvYF/HFmq+nHOBpVnDuzKbpK72A9Fz6Tc3zw\n\ttUuKw2xpjaVViL9LUpCJrcThc5S3Dr4=",
        "X-MC-Unique": "APu_f6ujNW6Njdk_g-1y1w-1",
        "From": "Hans de Goede <hdegoede@redhat.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Milan Zamazal <mzamazal@redhat.com>, Maxime Ripard <mripard@redhat.com>, \n\tHans de Goede <hdegoede@redhat.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tHarvey Yang <chenghaoyang@chromium.org>",
        "Subject": "[PATCH v3 1/3] pipeline_handler: Add acquireDevice() function to\n\tmirror existing releaseDevice()",
        "Date": "Fri, 30 Aug 2024 13:12:05 +0200",
        "Message-ID": "<20240830111207.46455-2-hdegoede@redhat.com>",
        "In-Reply-To": "<20240830111207.46455-1-hdegoede@redhat.com>",
        "References": "<20240830111207.46455-1-hdegoede@redhat.com>",
        "MIME-Version": "1.0",
        "X-Scanned-By": "MIMEDefang 3.0 on 10.30.177.15",
        "X-Mimecast-Spam-Score": "0",
        "X-Mimecast-Originator": "redhat.com",
        "Content-Transfer-Encoding": "8bit",
        "Content-Type": "text/plain; charset=\"US-ASCII\"; x-default=true",
        "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>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "libcamera always keeps all /dev/video# and /dev/v4l2_subdev# nodes\nfor a pipeline open after enumerating the camera.\n\nThis is a problem for the uvcvideo pipeline handler. Keeping /dev/video#\nopen stops the UVC USB device from being able to enter runtime-suspend\ncausing significant unnecessary power-usage.\n\nAdd a stub acquireDevice() function to the PipelineHandler class which\npipeline handlers can override.\n\nThe uvcvideo pipeline handler will use this to delay opening /dev/video#\nuntil the device is acquired. This is a special case because the kernel\nuvcvideo driver powers on the USB device as soon as /dev/video# is opened.\nThis behavior should *not* be copied by other pipeline handlers.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Harvey Yang <chenghaoyang@chromium.org>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\nChanges in v3:\n- s/method/function/ in commit msg\n- Add Reviewed-by-s\n\nChanges in v2:\n- Add a note to both the doxygen documentation as well as to the commit\n  message that opening/closing /dev/video# from acquire()/release()\n  as done by the uvcvideo pipeline handler is an exception and that this\n  behavior should not be copied by other pipeline handlers\n- Other doxygen doc fixes / improvements\n- Only unlock media devices on acquireDevice() failure if useCount_ == 0\n---\n include/libcamera/internal/pipeline_handler.h |  3 +-\n src/libcamera/camera.cpp                      |  2 +-\n src/libcamera/pipeline_handler.cpp            | 48 +++++++++++++++----\n 3 files changed, 43 insertions(+), 10 deletions(-)",
    "diff": "diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h\nindex cad5812f..597f7c3e 100644\n--- a/include/libcamera/internal/pipeline_handler.h\n+++ b/include/libcamera/internal/pipeline_handler.h\n@@ -45,7 +45,7 @@ public:\n \tMediaDevice *acquireMediaDevice(DeviceEnumerator *enumerator,\n \t\t\t\t\tconst DeviceMatch &dm);\n \n-\tbool acquire();\n+\tbool acquire(Camera *camera);\n \tvoid release(Camera *camera);\n \n \tvirtual std::unique_ptr<CameraConfiguration> generateConfiguration(Camera *camera,\n@@ -79,6 +79,7 @@ protected:\n \tvirtual int queueRequestDevice(Camera *camera, Request *request) = 0;\n \tvirtual void stopDevice(Camera *camera) = 0;\n \n+\tvirtual bool acquireDevice(Camera *camera);\n \tvirtual void releaseDevice(Camera *camera);\n \n \tCameraManager *manager_;\ndiff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\nindex 382a68f7..4e393f89 100644\n--- a/src/libcamera/camera.cpp\n+++ b/src/libcamera/camera.cpp\n@@ -995,7 +995,7 @@ int Camera::acquire()\n \tif (ret < 0)\n \t\treturn ret == -EACCES ? -EBUSY : ret;\n \n-\tif (!d->pipe_->acquire()) {\n+\tif (!d->pipe_->acquire(this)) {\n \t\tLOG(Camera, Info)\n \t\t\t<< \"Pipeline handler in use by another process\";\n \t\treturn -EBUSY;\ndiff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp\nindex 1fc22d6a..861815cb 100644\n--- a/src/libcamera/pipeline_handler.cpp\n+++ b/src/libcamera/pipeline_handler.cpp\n@@ -163,20 +163,24 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator,\n  * has already acquired it\n  * \\sa release()\n  */\n-bool PipelineHandler::acquire()\n+bool PipelineHandler::acquire(Camera *camera)\n {\n \tMutexLocker locker(lock_);\n \n-\tif (useCount_) {\n-\t\t++useCount_;\n-\t\treturn true;\n+\tif (useCount_ == 0) {\n+\t\tfor (std::shared_ptr<MediaDevice> &media : mediaDevices_) {\n+\t\t\tif (!media->lock()) {\n+\t\t\t\tunlockMediaDevices();\n+\t\t\t\treturn false;\n+\t\t\t}\n+\t\t}\n \t}\n \n-\tfor (std::shared_ptr<MediaDevice> &media : mediaDevices_) {\n-\t\tif (!media->lock()) {\n+\tif (!acquireDevice(camera)) {\n+\t\tif (useCount_ == 0)\n \t\t\tunlockMediaDevices();\n-\t\t\treturn false;\n-\t\t}\n+\n+\t\treturn false;\n \t}\n \n \t++useCount_;\n@@ -213,12 +217,40 @@ void PipelineHandler::release(Camera *camera)\n \t--useCount_;\n }\n \n+/**\n+ * \\brief Acquire resources associated with this camera\n+ * \\param[in] camera The camera for which to acquire resources\n+ *\n+ * Pipeline handlers may override this in order to get resources such as opening\n+ * devices and allocating buffers when a camera is acquired.\n+ *\n+ * This is used by the uvcvideo pipeline handler to delay opening /dev/video#\n+ * until the camera is acquired to avoid excess power consumption. The delayed\n+ * opening of /dev/video# is a special case because the kernel uvcvideo driver\n+ * powers on the USB device as soon as /dev/video# is opened. This behavior\n+ * should *not* be copied by other pipeline handlers.\n+ *\n+ * \\return True on success, false on failure\n+ * \\sa releaseDevice()\n+ */\n+bool PipelineHandler::acquireDevice([[maybe_unused]] Camera *camera)\n+{\n+\treturn true;\n+}\n+\n /**\n  * \\brief Release resources associated with this camera\n  * \\param[in] camera The camera for which to release resources\n  *\n  * Pipeline handlers may override this in order to perform cleanup operations\n  * when a camera is released, such as freeing memory.\n+ *\n+ * This is called once for every camera that is released. If there are resources\n+ * shared by multiple cameras then the pipeline handler must take care to not\n+ * release them until releaseDevice() has been called for all previously\n+ * acquired cameras.\n+ *\n+ * \\sa acquireDevice()\n  */\n void PipelineHandler::releaseDevice([[maybe_unused]] Camera *camera)\n {\n",
    "prefixes": [
        "v3",
        "1/3"
    ]
}