From patchwork Thu Jun 10 07:50:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12538 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 C0A66BD78E for ; Thu, 10 Jun 2021 07:50:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 84F5F6893B; Thu, 10 Jun 2021 09:50:44 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Hifbk8Sg"; dkim-atps=neutral Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 137D66892C for ; Thu, 10 Jun 2021 09:50:43 +0200 (CEST) Received: by mail-pl1-x62f.google.com with SMTP id 69so544549plc.5 for ; Thu, 10 Jun 2021 00:50:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=j5NZZ4iXvafGwsvGWf4VPksAYgE7O1U8gS5PYWmf/To=; b=Hifbk8Sg5H13aBfwgVGJbOazl0dO0Q+V8lnXnmbUzVv8w3cuM6lXmzx9aG38gQtp8O K4ykIUVdtEs80ZzgEzv58lsNrO0rbRWcDrU0MwxK9a1vOK2/I6IEkzoUdtd2ap/rctaN HIq+T0UmO7eJP/FIEvvXxCZxO8zdMmMRx974Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=j5NZZ4iXvafGwsvGWf4VPksAYgE7O1U8gS5PYWmf/To=; b=mPQ+ddOfPxV+Oh2nUiaUd96EyYmppW0fu8BngPugWxAYVR4kQZYGzve48CalzDPawi u6Wfmv937cru2thputc/VgfXsoXQBidLwNBvDONhg2SS028c6l++KU8tMh/0NJ9eRY95 /NdnHJROvgwcCQHzIqAvWBDTiOliTAvicZnWVX2l2EUEOkLmQY6knB73H8k6pKGgMcOp NXpySGSIpkxmHRRyMd94wzq3Y97onbikW3/MdG0EHrk77oce/RkCTI2xK8vRkQXPDcdf bG5vznXxwmLceQ3iK3X/aRATpHkPSxhQD+zMP91Waj8ot3EzA3eorr4Rg5eQPuMQmD7v cCFA== X-Gm-Message-State: AOAM533iUKOSpauMBJtEzPA6IL9cl/1hb7njgr9mLb1ncNOlVba+HGNy fSGUpL+kT7P9AlNHDcYNEX3FzUgiBxaBIQ== X-Google-Smtp-Source: ABdhPJz34NJJrhdNYei7z2tfdr5lM82BckRdYpZwC0sSnAXhOeSRY7HnIn7BlPZVpr9JAtUnyRKKBQ== X-Received: by 2002:a17:902:8c91:b029:ef:aa9a:af35 with SMTP id t17-20020a1709028c91b02900efaa9aaf35mr3549104plo.24.1623311441423; Thu, 10 Jun 2021 00:50:41 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:98e0:b356:1c8a:25d4]) by smtp.gmail.com with ESMTPSA id d66sm1565161pfa.32.2021.06.10.00.50.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:41 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:22 +0900 Message-Id: <20210610075027.523672-6-hiroh@chromium.org> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog In-Reply-To: <20210610075027.523672-1-hiroh@chromium.org> References: <20210610075027.523672-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 05/10] libcamera: MediaDevice: Manage fd by ScopedFD 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" Manages a file descriptor owned by MediaDevice for a media device node by ScopedFD. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/media_device.h | 3 +- src/libcamera/media_device.cpp | 39 ++++++++++------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index c3292508..7722df06 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -14,6 +14,7 @@ #include +#include #include #include "libcamera/internal/log.h" @@ -82,7 +83,7 @@ private: unsigned int version_; unsigned int hwRevision_; - int fd_; + ScopedFD fd_; bool valid_; bool acquired_; bool lockOwner_; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 9ec84e56..1050bf6d 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -63,15 +63,14 @@ LOG_DEFINE_CATEGORY(MediaDevice) * populate() before the media graph can be queried. */ MediaDevice::MediaDevice(const std::string &deviceNode) - : deviceNode_(deviceNode), fd_(-1), valid_(false), acquired_(false), + : deviceNode_(deviceNode), valid_(false), acquired_(false), lockOwner_(false) { } MediaDevice::~MediaDevice() { - if (fd_ != -1) - ::close(fd_); + fd_.reset(); clear(); } @@ -143,14 +142,14 @@ void MediaDevice::release() */ bool MediaDevice::lock() { - if (fd_ == -1) + if (!fd_.isValid()) return false; /* Do not allow nested locking in the same libcamera instance. */ if (lockOwner_) return false; - if (lockf(fd_, F_TLOCK, 0)) + if (lockf(fd_.get(), F_TLOCK, 0)) return false; lockOwner_ = true; @@ -169,7 +168,7 @@ bool MediaDevice::lock() */ void MediaDevice::unlock() { - if (fd_ == -1) + if (!fd_.isValid()) return; if (!lockOwner_) @@ -177,7 +176,7 @@ void MediaDevice::unlock() lockOwner_ = false; - lockf(fd_, F_ULOCK, 0); + lockf(fd_.get(), F_ULOCK, 0); } /** @@ -220,7 +219,7 @@ int MediaDevice::populate() return ret; struct media_device_info info = {}; - ret = ioctl(fd_, MEDIA_IOC_DEVICE_INFO, &info); + ret = ioctl(fd_.get(), MEDIA_IOC_DEVICE_INFO, &info); if (ret) { ret = -errno; LOG(MediaDevice, Error) @@ -243,7 +242,7 @@ int MediaDevice::populate() topology.ptr_links = reinterpret_cast(links); topology.ptr_pads = reinterpret_cast(pads); - ret = ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology); + ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &topology); if (ret < 0) { ret = -errno; LOG(MediaDevice, Error) @@ -481,20 +480,18 @@ int MediaDevice::disableLinks() */ int MediaDevice::open() { - if (fd_ != -1) { + if (fd_.isValid()) { LOG(MediaDevice, Error) << "MediaDevice already open"; return -EBUSY; } - int ret = ::open(deviceNode_.c_str(), O_RDWR); - if (ret < 0) { - ret = -errno; + fd_ = ScopedFD(::open(deviceNode_.c_str(), O_RDWR)); + if (!fd_.isValid()) { LOG(MediaDevice, Error) << "Failed to open media device at " - << deviceNode_ << ": " << strerror(-ret); - return ret; + << deviceNode_ << ": " << strerror(errno); + return -errno; } - fd_ = ret; return 0; } @@ -514,11 +511,7 @@ int MediaDevice::open() */ void MediaDevice::close() { - if (fd_ == -1) - return; - - ::close(fd_); - fd_ = -1; + fd_.reset(); } /** @@ -763,7 +756,7 @@ void MediaDevice::fixupEntityFlags(struct media_v2_entity *entity) struct media_entity_desc desc = {}; desc.id = entity->id; - int ret = ioctl(fd_, MEDIA_IOC_ENUM_ENTITIES, &desc); + int ret = ioctl(fd_.get(), MEDIA_IOC_ENUM_ENTITIES, &desc); if (ret < 0) { ret = -errno; LOG(MediaDevice, Debug) @@ -806,7 +799,7 @@ int MediaDevice::setupLink(const MediaLink *link, unsigned int flags) linkDesc.flags = flags; - int ret = ioctl(fd_, MEDIA_IOC_SETUP_LINK, &linkDesc); + int ret = ioctl(fd_.get(), MEDIA_IOC_SETUP_LINK, &linkDesc); if (ret) { ret = -errno; LOG(MediaDevice, Error)