Show a patch.

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

{
    "id": 21078,
    "url": "https://patchwork.libcamera.org/api/patches/21078/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21078/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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-4-hdegoede@redhat.com>",
    "date": "2024-08-30T11:12:07",
    "name": "[v3,3/3] uvcvideo: Implement acquireDevice() + releaseDevice()",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "23cb7b769205f6f8439255c8a04b7c9b72fdf337",
    "submitter": {
        "id": 102,
        "url": "https://patchwork.libcamera.org/api/people/102/?format=api",
        "name": "Hans de Goede",
        "email": "hdegoede@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21078/mbox/",
    "series": [
        {
            "id": 4549,
            "url": "https://patchwork.libcamera.org/api/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/21078/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21078/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 3DB3EC324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 30 Aug 2024 11:12:32 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C416461E4A;\n\tFri, 30 Aug 2024 13:12:31 +0200 (CEST)",
            "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1F4BB634CC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 30 Aug 2024 13:12:26 +0200 (CEST)",
            "from mx-prod-mc-04.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-271-G6paRzdgPvaCOOLvdQSQ3w-1;\n\tFri, 30 Aug 2024 07:12:22 -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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTPS id 7D0C21955D48; Fri, 30 Aug 2024 11:12:21 +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 D71651955F1B; Fri, 30 Aug 2024 11:12:19 +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=\"XMzQZjuI\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1725016344;\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=7f7aP+URRSUKGUpm+gZlI4fviFn04jD7EWpbA7gJNvk=;\n\tb=XMzQZjuIMAGXCMGfUVTIPz1cUOBbf20Vgr7zmN+IRgFYBKNID0jz2hb+5ZXwdOKsGaJENw\n\tn0RRuTTSf6oeVHUFaQvK6qgM700m+NnQOzfljpvveUfnIsA1B4VsJ6DP4ttaP8t7cPItu8\n\t1eJ6fS/R04Hup+/hmzNYclFo9ykf3bI=",
        "X-MC-Unique": "G6paRzdgPvaCOOLvdQSQ3w-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>",
        "Subject": "[PATCH v3 3/3] uvcvideo: Implement acquireDevice() + releaseDevice()",
        "Date": "Fri, 30 Aug 2024 13:12:07 +0200",
        "Message-ID": "<20240830111207.46455-4-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": "The uvcvideo pipeline handler always keeps the uvcvideo /dev/video# node\nfor a pipeline open after enumerating the camera.\n\nThis is a problem for uvcvideo, as keeping the /dev/video# node open stops\nthe underlying USB device and the USB bus controller from being able to\nenter runtime-suspend causing significant unnecessary power-usage.\n\nImplement acquireDevice() + releaseDevice(), openening /dev/video# on\nacquire and closing it on release to fix this.\n\nAnd make validate do a local video_->open() + close() around validate()\nwhen not open yet, to keep validate() working on unacquired cameras.\n\nBug: https://bugs.libcamera.org/show_bug.cgi?id=168\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\nChanges in v3:\n- Put MutexLocker locker(data_->openLock_); in a { } context\n  to reduce the time the lock is held to the minimum time necessary\n\nChanges in v2:\n- Minor commit msg + code improvements\n---\n src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 54 ++++++++++++++++++--\n 1 file changed, 51 insertions(+), 3 deletions(-)",
    "diff": "diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\nindex 8a7409fc..a26969e1 100644\n--- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n+++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n@@ -13,6 +13,7 @@\n #include <tuple>\n \n #include <libcamera/base/log.h>\n+#include <libcamera/base/mutex.h>\n #include <libcamera/base/utils.h>\n \n #include <libcamera/camera.h>\n@@ -48,6 +49,7 @@ public:\n \n \tconst std::string &id() const { return id_; }\n \n+\tMutex openLock_;\n \tstd::unique_ptr<V4L2VideoDevice> video_;\n \tStream stream_;\n \tstd::map<PixelFormat, std::vector<SizeRange>> formats_;\n@@ -93,6 +95,9 @@ private:\n \t\t\t   const ControlValue &value);\n \tint processControls(UVCCameraData *data, Request *request);\n \n+\tbool acquireDevice(Camera *camera) override;\n+\tvoid releaseDevice(Camera *camera) override;\n+\n \tUVCCameraData *cameraData(Camera *camera)\n \t{\n \t\treturn static_cast<UVCCameraData *>(camera->_d());\n@@ -158,9 +163,29 @@ CameraConfiguration::Status UVCCameraConfiguration::validate()\n \tformat.fourcc = data_->video_->toV4L2PixelFormat(cfg.pixelFormat);\n \tformat.size = cfg.size;\n \n-\tint ret = data_->video_->tryFormat(&format);\n-\tif (ret)\n-\t\treturn Invalid;\n+\t/*\n+\t * For power-consumption reasons video_ is closed when the camera\n+\t * is not acquired. Open it here if necessary.\n+\t */\n+\t{\n+\t\tbool opened = false;\n+\n+\t\tMutexLocker locker(data_->openLock_);\n+\n+\t\tif (!data_->video_->isOpen()) {\n+\t\t\tint ret = data_->video_->open();\n+\t\t\tif (ret)\n+\t\t\t\treturn Invalid;\n+\n+\t\t\topened = true;\n+\t\t}\n+\n+\t\tint ret = data_->video_->tryFormat(&format);\n+\t\tif (opened)\n+\t\t\tdata_->video_->close();\n+\t\tif (ret)\n+\t\t\treturn Invalid;\n+\t}\n \n \tcfg.stride = format.planes[0].bpl;\n \tcfg.frameSize = format.planes[0].size;\n@@ -411,6 +436,23 @@ bool PipelineHandlerUVC::match(DeviceEnumerator *enumerator)\n \treturn true;\n }\n \n+bool PipelineHandlerUVC::acquireDevice(Camera *camera)\n+{\n+\tUVCCameraData *data = cameraData(camera);\n+\n+\tMutexLocker locker(data->openLock_);\n+\n+\treturn data->video_->open() == 0;\n+}\n+\n+void PipelineHandlerUVC::releaseDevice(Camera *camera)\n+{\n+\tUVCCameraData *data = cameraData(camera);\n+\n+\tMutexLocker locker(data->openLock_);\n+\tdata->video_->close();\n+}\n+\n int UVCCameraData::init(MediaDevice *media)\n {\n \tint ret;\n@@ -512,6 +554,12 @@ int UVCCameraData::init(MediaDevice *media)\n \n \tcontrolInfo_ = ControlInfoMap(std::move(ctrls), controls::controls);\n \n+\t/*\n+\t * Close to allow camera to go into runtime-suspend, video_\n+\t * will be re-opened from acquireDevice() and validate().\n+\t */\n+\tvideo_->close();\n+\n \treturn 0;\n }\n \n",
    "prefixes": [
        "v3",
        "3/3"
    ]
}