From patchwork Thu Jun 10 08:25:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12546 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 B9DC0BD78E for ; Thu, 10 Jun 2021 08:25:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7A4AF6892B; Thu, 10 Jun 2021 10:25:49 +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="MyeSSwbE"; dkim-atps=neutral Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 48AAF602C5 for ; Thu, 10 Jun 2021 10:25:48 +0200 (CEST) Received: by mail-pj1-x102c.google.com with SMTP id h16so3205220pjv.2 for ; Thu, 10 Jun 2021 01:25: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=aXPp9utSmFRdgbvjmka9zrFr9zhgWAhDypHo5gIOfiE=; b=MyeSSwbECIaZfTeiP+bZnOTTob9t5jEYG7+veB7lhmDGrtmYUqBVCO7/6eGz72D2Yl t5pVP8GdzOrYXl3lFH0VH4djk7kC6Tr5kDqqspWYko5wqmJRycgsajSikfSnJTr6vyFp 8fOs1Jf69E2iLIrt6vHu/ipL9EnMf8bzXhvZM= 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=aXPp9utSmFRdgbvjmka9zrFr9zhgWAhDypHo5gIOfiE=; b=O3yD7dG4ZKH9FboF1PfrO8cV9fYn734DPRIJcWZfjsk14M7+xW234J0krJ5Po1XQnQ FTpFPaieWlnsa9LcaAqHrElLCdeLLWJWJrlzBhF63nNku97Vx/HcHFITlCeDKPpeAZ7+ GSbu1k0aSx6+Ng0jRrSvJtgtneKm8Az8E/N3pddYPIjWapq9vqTszcAo+tY6e1AJFAJf 7hTQJa6SNRXdCy2CyCvBgRc8ErQIsJ0aRtBN2dI+4cY6+A0r7baUZ4BqabBgJpbHVusF SIovwehnzWBnKQeZ/ZtUuGAXMwooNNgKxVbVfjcEAZpY8suD39mkYYaNvINJJEMZqLeH US8g== X-Gm-Message-State: AOAM530g+Hc5blemwbUwVds4XkraXLFhjPUUNExvFM1+oNYGzYN/C7Ze VBbrKU95P37g+InZexi+2959jjlyuyyy/w== X-Google-Smtp-Source: ABdhPJxqI+lhoglcy0wuj0fd4qgzrKkiojLKuPTjLFvs/DESNQG2Z3JYioyjrPq1uZl0LBbja4Vl0g== X-Received: by 2002:a17:90a:4dc1:: with SMTP id r1mr2203353pjl.192.1623313546565; Thu, 10 Jun 2021 01:25:46 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:98e0:b356:1c8a:25d4]) by smtp.gmail.com with ESMTPSA id x3sm2087011pgx.8.2021.06.10.01.25.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 01:25:46 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 17:25:35 +0900 Message-Id: <20210610082539.529739-2-hiroh@chromium.org> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog In-Reply-To: <20210610082539.529739-1-hiroh@chromium.org> References: <20210610082539.529739-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v8 2/6] libcamera: V4L2Device: Support v4l2 menu control 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" This adds a support of v4l2 menu. The control info for v4l2 menu contains indices without names and 64bit values of querymenu. Signed-off-by: Hirokazu Honda Reviewed-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- include/libcamera/internal/v4l2_device.h | 5 + src/libcamera/v4l2_device.cpp | 186 +++++++++++++++-------- 2 files changed, 127 insertions(+), 64 deletions(-) diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index b82e2a14..c318e976 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -56,6 +56,11 @@ protected: int fd() const { return fd_; } private: + static ControlType v4l2CtrlType(uint32_t ctrlType); + static std::unique_ptr v4l2ControlId(const v4l2_query_ext_ctrl &ctrl); + ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl); + ControlInfo v4l2MenuControlInfo(const v4l2_query_ext_ctrl &ctrl); + void listControls(); void updateControls(ControlList *ctrls, Span v4l2Ctrls); diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index b39c6266..cda3769a 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -32,69 +32,6 @@ LOG_DEFINE_CATEGORY(V4L2) namespace { -ControlType v4l2CtrlType(uint32_t ctrlType) -{ - switch (ctrlType) { - case V4L2_CTRL_TYPE_U8: - return ControlTypeByte; - - case V4L2_CTRL_TYPE_BOOLEAN: - return ControlTypeBool; - - case V4L2_CTRL_TYPE_INTEGER: - return ControlTypeInteger32; - - case V4L2_CTRL_TYPE_INTEGER64: - return ControlTypeInteger64; - - case V4L2_CTRL_TYPE_MENU: - case V4L2_CTRL_TYPE_BUTTON: - case V4L2_CTRL_TYPE_BITMASK: - case V4L2_CTRL_TYPE_INTEGER_MENU: - /* - * More precise types may be needed, for now use a 32-bit - * integer type. - */ - return ControlTypeInteger32; - - default: - return ControlTypeNone; - } -} - -std::unique_ptr v4l2ControlId(const v4l2_query_ext_ctrl &ctrl) -{ - const size_t len = strnlen(ctrl.name, sizeof(ctrl.name)); - const std::string name(static_cast(ctrl.name), len); - const ControlType type = v4l2CtrlType(ctrl.type); - - return std::make_unique(ctrl.id, name, type); -} - -ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl) -{ - switch (ctrl.type) { - case V4L2_CTRL_TYPE_U8: - return ControlInfo(static_cast(ctrl.minimum), - static_cast(ctrl.maximum), - static_cast(ctrl.default_value)); - - case V4L2_CTRL_TYPE_BOOLEAN: - return ControlInfo(static_cast(ctrl.minimum), - static_cast(ctrl.maximum), - static_cast(ctrl.default_value)); - - case V4L2_CTRL_TYPE_INTEGER64: - return ControlInfo(static_cast(ctrl.minimum), - static_cast(ctrl.maximum), - static_cast(ctrl.default_value)); - - default: - return ControlInfo(static_cast(ctrl.minimum), - static_cast(ctrl.maximum), - static_cast(ctrl.default_value)); - } -} } /* namespace */ /** @@ -524,6 +461,121 @@ int V4L2Device::ioctl(unsigned long request, void *argp) * \return The V4L2 device file descriptor, -1 if the device node is not open */ +/** + * \brief Retrieve ControlType for V4L2 control type + * \param[in] ctrlType V4L2 control type + * + * \return ControlType for \a ctrlType + */ +ControlType V4L2Device::v4l2CtrlType(uint32_t ctrlType) +{ + switch (ctrlType) { + case V4L2_CTRL_TYPE_U8: + return ControlTypeByte; + + case V4L2_CTRL_TYPE_BOOLEAN: + return ControlTypeBool; + + case V4L2_CTRL_TYPE_INTEGER: + return ControlTypeInteger32; + + case V4L2_CTRL_TYPE_INTEGER64: + return ControlTypeInteger64; + + case V4L2_CTRL_TYPE_MENU: + case V4L2_CTRL_TYPE_BUTTON: + case V4L2_CTRL_TYPE_BITMASK: + case V4L2_CTRL_TYPE_INTEGER_MENU: + /* + * More precise types may be needed, for now use a 32-bit + * integer type. + */ + return ControlTypeInteger32; + + default: + return ControlTypeNone; + } +} + +/** + * \brief Create ControlId for v4l2_query_ext_ctrl + * \param[in] ctrl v4l2_query_ext_ctrl + * + * \return ControlId for \a ctrl + */ +std::unique_ptr V4L2Device::v4l2ControlId(const v4l2_query_ext_ctrl &ctrl) +{ + const size_t len = strnlen(ctrl.name, sizeof(ctrl.name)); + const std::string name(static_cast(ctrl.name), len); + const ControlType type = v4l2CtrlType(ctrl.type); + + return std::make_unique(ctrl.id, name, type); +} + +/** + * \brief Create ControlInfo from v4l2_query_ext_ctrl + * \param[in] ctrl v4l2_query_ext_ctrl + * + * \return ControlInfo from \a v4l2_query_ext_ctrl + */ +ControlInfo V4L2Device::v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl) +{ + switch (ctrl.type) { + case V4L2_CTRL_TYPE_U8: + return ControlInfo(static_cast(ctrl.minimum), + static_cast(ctrl.maximum), + static_cast(ctrl.default_value)); + + case V4L2_CTRL_TYPE_BOOLEAN: + return ControlInfo(static_cast(ctrl.minimum), + static_cast(ctrl.maximum), + static_cast(ctrl.default_value)); + + case V4L2_CTRL_TYPE_INTEGER64: + return ControlInfo(static_cast(ctrl.minimum), + static_cast(ctrl.maximum), + static_cast(ctrl.default_value)); + + case V4L2_CTRL_TYPE_INTEGER_MENU: + case V4L2_CTRL_TYPE_MENU: + return v4l2MenuControlInfo(ctrl); + + default: + return ControlInfo(static_cast(ctrl.minimum), + static_cast(ctrl.maximum), + static_cast(ctrl.default_value)); + } +} + +/** + * \brief Create ControlInfo for a V4L2 menu control + * \param[in] ctrl The v4l2_query_ext_ctrl that represents a menu + * + * The created ControlInfo contains indices acquired by VIDIOC_QUERYMENU. + * + * \return ControlInfo for a V4L2 menu control + */ +ControlInfo V4L2Device::v4l2MenuControlInfo(const struct v4l2_query_ext_ctrl &ctrl) +{ + std::vector indices; + struct v4l2_querymenu menu = {}; + menu.id = ctrl.id; + + if (ctrl.minimum < 0) + return ControlInfo(); + + for (int32_t index = ctrl.minimum; index <= ctrl.maximum; ++index) { + menu.index = index; + if (ioctl(VIDIOC_QUERYMENU, &menu) != 0) + continue; + + indices.push_back(index); + } + + return ControlInfo(indices, + ControlValue(static_cast(ctrl.default_value))); +} + /* * \brief List and store information about all controls supported by the * V4L2 device @@ -533,7 +585,6 @@ void V4L2Device::listControls() ControlInfoMap::Map ctrls; struct v4l2_query_ext_ctrl ctrl = {}; - /* \todo Add support for menu controls. */ while (1) { ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND; @@ -562,6 +613,9 @@ void V4L2Device::listControls() continue; } + LOG(V4L2, Debug) << "Control: " << ctrl.name + << " (" << utils::hex(ctrl.id) << ")"; + controlIds_.emplace_back(v4l2ControlId(ctrl)); controlInfo_.emplace(ctrl.id, ctrl); @@ -630,6 +684,10 @@ void V4L2Device::updateControls(ControlList *ctrls, value.set(v4l2Ctrl.value64); break; + case ControlTypeInteger32: + value.set(v4l2Ctrl.value); + break; + case ControlTypeByte: /* * No action required, the VIDIOC_[GS]_EXT_CTRLS ioctl