From patchwork Thu Jun 10 07:50:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12542 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 83314BD78E for ; Thu, 10 Jun 2021 07:50:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4090268937; Thu, 10 Jun 2021 09:50:51 +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="MSpbbfKJ"; dkim-atps=neutral Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DEDE268933 for ; Thu, 10 Jun 2021 09:50:48 +0200 (CEST) Received: by mail-pj1-x1030.google.com with SMTP id k22-20020a17090aef16b0290163512accedso5053830pjz.0 for ; Thu, 10 Jun 2021 00:50:48 -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=tRd9oNMvJz1cb4+9G5xPCdGe7j9zeJmXlD0Yo4r3yXg=; b=MSpbbfKJY6zmdfEyj4Wa8Kho+TqhQYSFpfNe3dtvzQDUPyNZO2gwiXqRyGdhtByGr2 kIhYMuGB+4WGBKSmBj6MRpoxFqra619WJUYG62htl2Vj4AVtDf+Klo2Ir15KZPwjQf9d Oid0WJBxWT5hMYPRjpC3wnB5nK0wJmhsA1PKE= 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=tRd9oNMvJz1cb4+9G5xPCdGe7j9zeJmXlD0Yo4r3yXg=; b=Vhbk9cFKn0gSj4EOs0bzn73BbJ+ChYhXOrqQEI2+2AnJUDMq6NULDScqkGeoIjvvqe N2DsojB/tsOjGdmalni9CU4N0jWFveyQSeQo6KzEUV/bwQJxcyx3ng514S+lLBEaHbxt JejrqM45DcXnkC/5hZkOwbKg5pGXUV/HFWUSzOg4iP9XyxZ9ZqK/QID5UXLO6CUfvf9r 8nIy/bB87hV4OWIH7qftaWfSp0N4AaeVnajadc54u017Jt44pHqCT16ky9g+pIXTEjm/ kDWx2pvH1/tywuO8Lmd/2iR/MBpdHTmUvnYdPeI3zgkzh4pCJ29zHdhRBxBxs7psd/aW 6HBA== X-Gm-Message-State: AOAM5305K8MPNDGTdZHqEJTEkKfkG0GgRdwkyEFpVQ3ptZ5/5HuNvg5g FsTs4H41wtisQ2G59GP+74Crwyy7huf4Jw== X-Google-Smtp-Source: ABdhPJwIAnbFLQLhjqQWg94WPvOdJYNx2zU6Mz5toiRZ17BkIDDxHXsddd1IwyU8Eey7QOqPWi3l0Q== X-Received: by 2002:a17:90a:f015:: with SMTP id bt21mr2045899pjb.37.1623311447263; Thu, 10 Jun 2021 00:50:47 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:46 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:26 +0900 Message-Id: <20210610075027.523672-10-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 09/10] libcamera: pipeline: raspberrypi: DmaHeaps: Use ScopedFD for a file descriptor 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 DmaHeaps for a dma handle by ScopedFD. Furthermore, DmaHeaps::alloc() creates a new file descriptor and the returned file descriptor is owned by a caller. This also clarifies it by changing the returned value to ScopedFD. Signed-off-by: Hirokazu Honda --- .../pipeline/raspberrypi/dma_heaps.cpp | 23 ++++++++----------- .../pipeline/raspberrypi/dma_heaps.h | 10 ++++---- .../pipeline/raspberrypi/raspberrypi.cpp | 7 ++++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp index 4d5dd6cb..d5b000b8 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp @@ -35,7 +35,6 @@ LOG_DECLARE_CATEGORY(RPI) namespace RPi { DmaHeap::DmaHeap() - : dmaHeapHandle_(-1) { for (const char *name : heapNames) { int ret = ::open(name, O_RDWR, 0); @@ -46,49 +45,47 @@ DmaHeap::DmaHeap() continue; } - dmaHeapHandle_ = ret; + dmaHeapHandle_ = ScopedFD(ret); break; } - if (dmaHeapHandle_ < 0) + if (!dmaHeapHandle_.isValid()) LOG(RPI, Error) << "Could not open any dmaHeap device"; } DmaHeap::~DmaHeap() { - if (dmaHeapHandle_ > -1) - ::close(dmaHeapHandle_); } -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size) +ScopedFD DmaHeap::alloc(const char *name, std::size_t size) { int ret; if (!name) - return FileDescriptor(); + return ScopedFD(); struct dma_heap_allocation_data alloc = {}; alloc.len = size; alloc.fd_flags = O_CLOEXEC | O_RDWR; - ret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc); + ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc); if (ret < 0) { LOG(RPI, Error) << "dmaHeap allocation failure for " << name; - return FileDescriptor(); + return ScopedFD(); } - ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name); + ScopedFD allocFd(alloc.fd); + ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name); if (ret < 0) { LOG(RPI, Error) << "dmaHeap naming failure for " << name; - ::close(alloc.fd); - return FileDescriptor(); + return ScopedFD(); } - return FileDescriptor(std::move(alloc.fd)); + return allocFd; } } /* namespace RPi */ diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h index 79f39c51..3a0765e6 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h @@ -7,7 +7,9 @@ #ifndef __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__ #define __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__ -#include +#include + +#include namespace libcamera { @@ -18,11 +20,11 @@ class DmaHeap public: DmaHeap(); ~DmaHeap(); - bool isValid() const { return dmaHeapHandle_ > -1; } - FileDescriptor alloc(const char *name, std::size_t size); + bool isValid() const { return dmaHeapHandle_.isValid(); } + ScopedFD alloc(const char *name, std::size_t size); private: - int dmaHeapHandle_; + ScopedFD dmaHeapHandle_; }; } /* namespace RPi */ diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index a65b4568..13faa96e 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -1263,10 +1263,13 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config) /* Allocate the lens shading table via dmaHeap and pass to the IPA. */ if (!lsTable_.isValid()) { - lsTable_ = dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize); - if (!lsTable_.isValid()) + ScopedFD scopedFD = + dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize); + if (!scopedFD.isValid()) return -ENOMEM; + lsTable_ = FileDescriptor(std::move(scopedFD)); + /* Allow the IPA to mmap the LS table via the file descriptor. */ /* * \todo Investigate if mapping the lens shading table buffer