From patchwork Mon Sep 23 06:02:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21325 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 F27FCC0F1B for ; Mon, 23 Sep 2024 06:06:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4306163508; Mon, 23 Sep 2024 08:06:04 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="kY1MCxxv"; dkim-atps=neutral Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1E3AA634F4 for ; Mon, 23 Sep 2024 08:06:02 +0200 (CEST) Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-7db12af2f31so3315999a12.1 for ; Sun, 22 Sep 2024 23:06:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1727071560; x=1727676360; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TQTkOyswq9G1VDrDGvJyIsuI03VF30js8vfGj2qnPNg=; b=kY1MCxxvXudYYiE4gYp/lE3NJi3My4GN0CHPHa2bHxg7MtTo6/97orZa8/YGQn6shb R7Kss9+h4GkUqaXa1qx01m/JSR3cl47cE7k09+R+u4fVfs0WZ9s0ailnsiUkI0cQTUAA voPTgNXF524xtMGDoJQKcil+zhWAVLVyERq6Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727071560; x=1727676360; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TQTkOyswq9G1VDrDGvJyIsuI03VF30js8vfGj2qnPNg=; b=U71DkCpeI+YgCtayoLBFOJqUDCut3Gitxspf+BF+XfE3GtoiYRDR0tVWLeGrjf97R7 NePENx8bxy5ZDmxwoJJhtUc4sr0uIEy79aHZjIaB73qGgQx9zW9rg2IyGJKr5+GdjbGO xv+URnKs7T68QVZUZErP3mgf7QmFT4is5zefjKDMiDzGsV4tkouzS6KLUX7x9Yn0D0Gd wjaxur7C30QJ5R+/6pwMwB2wplPLc6qfh3qA9oBEG6K/XUul4pk9TFXapK2HI44egXTD wmNaLP9GruAnS1mxaJ9XRFxj80yHvA/BhmOmS35iKg8eZK1bj1z84Kvz0MIZmaQ59XGz huqQ== X-Gm-Message-State: AOJu0Ywx3MrCwSVt9hHFEhu3fx21TW7xP5z1CVOsL+98qgcDc9iqtHL7 W8Q4DrOir2Rt92c9oEex6C7qOMhmVnXvrftd42UVwncZ81bbwApZfqYwWjFfrjiE16Z+fmSscJc = X-Google-Smtp-Source: AGHT+IFr4lD3Fl2RWt7jbt+P2l6J5YciwQsa2mvgPTKmAemipCkt85bgOKNXYUHr6qR5y7lJNN3S8g== X-Received: by 2002:a17:90a:d908:b0:2d8:83ce:d4c0 with SMTP id 98e67ed59e1d1-2dd7f40ad24mr13908919a91.13.1727071559826; Sun, 22 Sep 2024 23:05:59 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (208.158.221.35.bc.googleusercontent.com. [35.221.158.208]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2dd50bfc88asm6369920a91.0.2024.09.22.23.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Sep 2024 23:05:59 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH 1/1] libcamera: Add request API support for media controller device Date: Mon, 23 Sep 2024 06:02:35 +0000 Message-ID: <20240923060551.1849065-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog In-Reply-To: <20240923060551.1849065-1-chenghaoyang@google.com> References: <20240923060551.1849065-1-chenghaoyang@google.com> MIME-Version: 1.0 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" From: Han-Lin Chen This patch adds `allocateRequests` with MEDIA_IOC_REQUEST_ALLOC , `queueRequest` with MEDIA_REQUEST_IOC_QUEUE, and `reInitRequest` with MEDIA_REQUEST_IOC_REINIT. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang --- include/libcamera/internal/media_device.h | 4 ++ src/libcamera/media_device.cpp | 87 +++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index e412d3a0..8f54dd12 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -53,6 +53,10 @@ public: MediaLink *link(const MediaPad *source, const MediaPad *sink); int disableLinks(); + int allocateRequests(unsigned int count, std::vector &requests); + int queueRequest(int requestFd); + int reInitRequest(int requestFd); + Signal<> disconnected; protected: diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index bd054552..47b75809 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -836,4 +837,90 @@ int MediaDevice::setupLink(const MediaLink *link, unsigned int flags) return 0; } +/** + * \brief Allocate \a count requests with ioctl + * \param[in] count The number of requests to be allocated + * \param[out] requests The request file descriptors to be returned + * + * This function returns request file descriptors if the MediaDevice supports + * requests. The file descriptors can then be queued and re-inited afterwards. + * + * \sa queueRequest(int requestFd) + * \sa reInitRequest(int requestFd) + * + * \return 0 on success or a negative error code otherwise + */ +int MediaDevice::allocateRequests(unsigned int count, std::vector &requests) +{ + for (unsigned int i = 0; i < count; i++) { + int fd; + int ret = ioctl(fd_.get(), MEDIA_IOC_REQUEST_ALLOC, &fd); + if (ret) { + LOG(MediaDevice, Error) << "Allocate request failed " + << strerror(-ret); + return -EBUSY; + } + requests.emplace_back(fd); + } + + return 0; +} + +/** + * \brief Queue a request with ioctl + * \param[in] requestFd The request file descriptor + * + * This function queues a request that was allocated before. + * + * \sa allocateRequests(unsigned int count, std::vector &requests) + * \sa reInitRequest(int requestFd) + * + * \return 0 on success or a negative error code otherwise + */ +int MediaDevice::queueRequest(int requestFd) +{ + int ret = ioctl(requestFd, MEDIA_REQUEST_IOC_QUEUE, NULL); + if (ret) + LOG(MediaDevice, Error) << "QueueRequest fd " << requestFd + << "failed: " << strerror(-ret); + return ret; +} + +/** + * \brief Re-init a request with ioctl + * \param[in] requestFd The request file descriptor + * + * This function recycles a request that was allocated and queued before. It + * polls on \a requestFd to ensure the request is completed, and reinits the + * request. + * + * \sa allocateRequests(unsigned int count, std::vector &requests) + * \sa queueRequest(int requestFd) + * + * \return 0 on success or a negative error code otherwise + */ +int MediaDevice::reInitRequest(int requestFd) +{ + struct pollfd pfd; + + pfd.fd = requestFd; + pfd.events = POLLPRI; + + int ret = TEMP_FAILURE_RETRY(poll(&pfd, 1, 300)); + if (ret < 0) + LOG(MediaDevice, Error) << "The request " << requestFd + << " polled failed: " << strerror(-ret); + else if (ret == 0) + LOG(MediaDevice, Error) << "The request " << requestFd + << " polled timeout: " << strerror(-ret); + + ret = ::ioctl(requestFd, MEDIA_REQUEST_IOC_REINIT, NULL); + if (ret) + LOG(MediaDevice, Error) << "The request " << requestFd + << " is queued but not yet completed: " + << strerror(-ret); + + return ret; +} + } /* namespace libcamera */