From patchwork Tue Aug 27 16:42:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 21047 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 D3FA6C32C2 for ; Tue, 27 Aug 2024 16:43:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6EA3163460; Tue, 27 Aug 2024 18:43:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="aTtXNIaK"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1F4726345F for ; Tue, 27 Aug 2024 18:43:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724776993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xk3NH8Mo97mDm9v1otdkRM2eP3Jyn3uw2XyLBc9Unqs=; b=aTtXNIaKXdt+PriHo7z8vM7UTZmuSl00KkURcyuA8HQkj1twC7HhKULZKsis4hdbf/gA9A 4hFOa5q505/QJ7emXR6xONtj+Ao+20/4HzeLpOcl8FchHYbQpcpcSci4ESYeiV+twID3si HTiX0MZ+b8V6O4DclTFL0A4GrO8mfos= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-124-9NqRetDGPji5s5HEa27-xA-1; Tue, 27 Aug 2024 12:43:11 -0400 X-MC-Unique: 9NqRetDGPji5s5HEa27-xA-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E32191955BE7 for ; Tue, 27 Aug 2024 16:43:10 +0000 (UTC) Received: from localhost.localdomain (unknown [10.39.192.136]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5D2691955D8B; Tue, 27 Aug 2024 16:43:09 +0000 (UTC) From: Hans de Goede To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Maxime Ripard , Hans de Goede Subject: [PATCH v2 3/3] uvcvideo: Implement acquireDevice() + releaseDevice() Date: Tue, 27 Aug 2024 18:42:55 +0200 Message-ID: <20240827164255.314432-4-hdegoede@redhat.com> In-Reply-To: <20240827164255.314432-1-hdegoede@redhat.com> References: <20240827164255.314432-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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" The uvcvideo pipeline handler always keeps the uvcvideo /dev/video# node for a pipeline open after enumerating the camera. This is a problem for uvcvideo, as keeping the /dev/video# node open stops the underlying USB device and the USB bus controller from being able to enter runtime-suspend causing significant unnecessary power-usage. Implement acquireDevice() + releaseDevice(), openening /dev/video# on acquire and closing it on release to fix this. And make validate do a local video_->open() + close() around validate() when not open yet. To keep validate() working on unacquired cameras. Link: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2669 Signed-off-by: Hans de Goede Reviewed-by: Laurent Pinchart --- Changes in v2: - Minor commit msg + code improvements --- src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp index 8a7409fc..584f5230 100644 --- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -48,6 +49,7 @@ public: const std::string &id() const { return id_; } + Mutex openLock_; std::unique_ptr video_; Stream stream_; std::map> formats_; @@ -93,6 +95,9 @@ private: const ControlValue &value); int processControls(UVCCameraData *data, Request *request); + bool acquireDevice(Camera *camera) override; + void releaseDevice(Camera *camera) override; + UVCCameraData *cameraData(Camera *camera) { return static_cast(camera->_d()); @@ -107,6 +112,7 @@ UVCCameraConfiguration::UVCCameraConfiguration(UVCCameraData *data) CameraConfiguration::Status UVCCameraConfiguration::validate() { Status status = Valid; + bool opened = false; if (config_.empty()) return Invalid; @@ -158,7 +164,23 @@ CameraConfiguration::Status UVCCameraConfiguration::validate() format.fourcc = data_->video_->toV4L2PixelFormat(cfg.pixelFormat); format.size = cfg.size; + /* + * For power-consumption reasons video_ is closed when the camera + * is not acquired. Open it here if necessary. + */ + MutexLocker locker(data_->openLock_); + + if (!data_->video_->isOpen()) { + int ret = data_->video_->open(); + if (ret) + return Invalid; + + opened = true; + } + int ret = data_->video_->tryFormat(&format); + if (opened) + data_->video_->close(); if (ret) return Invalid; @@ -411,6 +433,23 @@ bool PipelineHandlerUVC::match(DeviceEnumerator *enumerator) return true; } +bool PipelineHandlerUVC::acquireDevice(Camera *camera) +{ + UVCCameraData *data = cameraData(camera); + + MutexLocker locker(data->openLock_); + + return data->video_->open() == 0; +} + +void PipelineHandlerUVC::releaseDevice(Camera *camera) +{ + UVCCameraData *data = cameraData(camera); + + MutexLocker locker(data->openLock_); + data->video_->close(); +} + int UVCCameraData::init(MediaDevice *media) { int ret; @@ -512,6 +551,12 @@ int UVCCameraData::init(MediaDevice *media) controlInfo_ = ControlInfoMap(std::move(ctrls), controls::controls); + /* + * Close to allow camera to go into runtime-suspend, video_ + * will be re-opened from acquireDevice() and validate(). + */ + video_->close(); + return 0; }