From patchwork Wed Apr 21 04:23:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12020 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 E3377BDB16 for ; Wed, 21 Apr 2021 04:24:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6B0FC68848; Wed, 21 Apr 2021 06:24:01 +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="kvXPIsgM"; dkim-atps=neutral Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 53A2468839 for ; Wed, 21 Apr 2021 06:24:00 +0200 (CEST) Received: by mail-pg1-x52a.google.com with SMTP id p12so28545825pgj.10 for ; Tue, 20 Apr 2021 21:24:00 -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=v7vOsb8DbMt3IBCLrf+iB6KOuge3VtWhyJKcdnF4+ec=; b=kvXPIsgM1Uuxj5tzi9tiFm+TbqRGqzk1VabmBVMAqEOYOWbbHZ9Xul9LSEZkKoV1DV pnuc8NYdQYe1sLydZ3MVTvMA4xI554Fmq6GXmVLnCHu9ArEfvERAljwOmvw+pV9CpCfL CDPQq2rPmorCQhnBp+EAgSV7Pf7yElTt/n4qg= 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=v7vOsb8DbMt3IBCLrf+iB6KOuge3VtWhyJKcdnF4+ec=; b=DxIpXjf0ucJdGi6Ql1KGIaYnQ8ppZpSz15ogbBlE4ypU/V7v5363alskN61MPfMF2l 93YRl4uHmZdyv2kHSure1r6OkfdSrN73TMt/wvl8Paf6Ta1kTkemoEKEIzE3Sy+AjsUH 62tfrg80Evq+pAVUmgy7TSbFCctBCpv0Z/rQWAdbwmkjL5tQpfDJmA4HrEMFhWzWjz6i gMCYmxvQdllc4dH50iNorNJ3t1KhJgA0Faz8P2m8BBP1AF0ladvFgZpsGPekIFxk3K4+ wn8C8aTkug+6v2x5qKLZP7QtQT8gpnEEPfu9tjNy3Ag5q1wJOHMh6f4l7RvHxMnXvlWU IUsA== X-Gm-Message-State: AOAM533q9xD0TZmKN9nXFYyvltv7V54e5csvjNg6HEvElVFuk8DooQqa 7fNkVS//jty3ZTiNOWhepjbCFjHaYPnxfw== X-Google-Smtp-Source: ABdhPJwWtjwSTNk8tuAsQNm301roTJGBuYP+e8iVCMNV1R1+umgHv8G7PX/8PFCKm5bz8SW27k9W+w== X-Received: by 2002:a17:90a:b947:: with SMTP id f7mr8513650pjw.166.1618979038777; Tue, 20 Apr 2021 21:23:58 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:7aaa:cbeb:5d87:4ab6]) by smtp.gmail.com with ESMTPSA id z18sm461316pfa.39.2021.04.20.21.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Apr 2021 21:23:58 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Wed, 21 Apr 2021 13:23:42 +0900 Message-Id: <20210421042346.312854-4-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog In-Reply-To: <20210421042346.312854-1-hiroh@chromium.org> References: <20210421042346.312854-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/7] 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. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_controls.h | 1 + include/libcamera/internal/v4l2_device.h | 3 + src/libcamera/v4l2_controls.cpp | 10 ++- src/libcamera/v4l2_device.cpp | 90 ++++++++++++++++++++-- 4 files changed, 98 insertions(+), 6 deletions(-) diff --git a/include/libcamera/internal/v4l2_controls.h b/include/libcamera/internal/v4l2_controls.h index 0851b8dd..c42f2529 100644 --- a/include/libcamera/internal/v4l2_controls.h +++ b/include/libcamera/internal/v4l2_controls.h @@ -24,6 +24,7 @@ class V4L2ControlInfo : public ControlInfo { public: V4L2ControlInfo(const struct v4l2_query_ext_ctrl &ctrl); + V4L2ControlInfo(const ControlInfo &ctrl); }; } /* namespace libcamera */ diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index 4cce3840..2436a83c 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -53,6 +53,9 @@ protected: int fd() const { return fd_; } private: + bool createV4L2ControlInfoForMenu(const v4l2_query_ext_ctrl &ctrl, + V4L2ControlInfo &v4l2CtrlInfo); + void listControls(); void updateControls(ControlList *ctrls, const std::vector &v4l2Ctrls); diff --git a/src/libcamera/v4l2_controls.cpp b/src/libcamera/v4l2_controls.cpp index 3f8ec6ca..2c965cfa 100644 --- a/src/libcamera/v4l2_controls.cpp +++ b/src/libcamera/v4l2_controls.cpp @@ -73,9 +73,12 @@ ControlType v4l2_ctrl_type(const struct v4l2_query_ext_ctrl &ctrl) return ControlTypeInteger64; case V4L2_CTRL_TYPE_MENU: + case V4L2_CTRL_TYPE_INTEGER_MENU: + return ControlTypeMenu; + 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. @@ -148,4 +151,9 @@ V4L2ControlInfo::V4L2ControlInfo(const struct v4l2_query_ext_ctrl &ctrl) } } +V4L2ControlInfo::V4L2ControlInfo(const ControlInfo &ctrl) + : ControlInfo(ctrl) +{ +} + } /* namespace libcamera */ diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index 8f29cd7f..585e527b 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -459,6 +459,47 @@ int V4L2Device::ioctl(unsigned long request, void *argp) * \return The device node path */ +bool V4L2Device::createV4L2ControlInfoForMenu(const v4l2_query_ext_ctrl &ctrl, + V4L2ControlInfo &v4l2CtrlInfo) +{ + ASSERT(ctrl.type == V4L2_CTRL_TYPE_MENU || + ctrl.type == V4L2_CTRL_TYPE_INTEGER_MENU); + const bool isName = ctrl.type == V4L2_CTRL_TYPE_MENU; + + std::vector values; + v4l2_querymenu menu; + memset(&menu, 0, sizeof(menu)); + menu.id = ctrl.id; + for (menu.index = ctrl.minimum; + static_cast(menu.index) <= ctrl.maximum; menu.index++) { + if (ioctl(VIDIOC_QUERYMENU, &menu) != 0) + continue; + ControlMenu ctrlMenu; + ctrlMenu.index = static_cast(menu.index); + ctrlMenu.isName = isName; + if (isName) { + strcpy(ctrlMenu.name, + reinterpret_cast(menu.name)); + } else { + ctrlMenu.value = menu.value; + } + + values.push_back(ControlValue(ctrlMenu)); + } + + if (values.empty()) { + LOG(V4L2, Error) + << "No applicable value: " << utils::hex(ctrl.id); + return false; + } + + v4l2CtrlInfo = V4L2ControlInfo( + ControlInfo(libcamera::Span( + values.data(), values.size()))); + + return true; +} + /** * \fn V4L2Device::fd() * \brief Retrieve the V4L2 device file descriptor @@ -474,7 +515,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; @@ -485,15 +525,20 @@ void V4L2Device::listControls() ctrl.flags & V4L2_CTRL_FLAG_DISABLED) continue; + V4L2ControlInfo v4l2CtrlInfo(ControlInfo(0)); switch (ctrl.type) { case V4L2_CTRL_TYPE_INTEGER: case V4L2_CTRL_TYPE_BOOLEAN: - case V4L2_CTRL_TYPE_MENU: case V4L2_CTRL_TYPE_BUTTON: case V4L2_CTRL_TYPE_INTEGER64: case V4L2_CTRL_TYPE_BITMASK: - case V4L2_CTRL_TYPE_INTEGER_MENU: case V4L2_CTRL_TYPE_U8: + v4l2CtrlInfo = V4L2ControlInfo(ctrl); + break; + case V4L2_CTRL_TYPE_MENU: + case V4L2_CTRL_TYPE_INTEGER_MENU: + if (!createV4L2ControlInfoForMenu(ctrl, v4l2CtrlInfo)) + continue; break; /* \todo Support other control types. */ default: @@ -503,10 +548,13 @@ void V4L2Device::listControls() continue; } + LOG(V4L2, Debug) << "Control: " << ctrl.name + << " (" << utils::hex(ctrl.id) << ")"; + controlIds_.emplace_back(std::make_unique(ctrl)); controlInfo_.emplace(ctrl.id, ctrl); - ctrls.emplace(controlIds_.back().get(), V4L2ControlInfo(ctrl)); + ctrls.emplace(controlIds_.back().get(), std::move(v4l2CtrlInfo)); } controls_ = std::move(ctrls); @@ -539,7 +587,39 @@ void V4L2Device::updateControls(ControlList *ctrls, case ControlTypeInteger64: newValue.set(v4l2Ctrl.value64); break; - + case ControlTypeMenu: + switch (v4l2Ctrl.id) { + case V4L2_CID_TEST_PATTERN: { + ControlMenu menu; + menu.index = v4l2Ctrl.value; + menu.isName = true; + + bool found = false; + for (const ControlValue &validValue : it->second.values()) { + const auto &ctrlMenu = validValue.get(); + if (!ctrlMenu.isName) + continue; + if (menu.index == ctrlMenu.index) { + strcpy(menu.name, ctrlMenu.name); + found = true; + break; + } + } + + if (!found) { + LOG(V4L2, Error) + << "Matched value is not found" + << ", index=" << menu.index; + continue; + } + + newValue.set(menu); + break; + } + default: + LOG(V4L2, Error) << "Unknown id: " << v4l2Ctrl.id; + break; + } case ControlTypeByte: /* * No action required, the VIDIOC_[GS]_EXT_CTRLS ioctl