From patchwork Wed Dec 30 18:01:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 10780 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 F0B0CC0F1C for ; Wed, 30 Dec 2020 18:01:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1742462007; Wed, 30 Dec 2020 19:01:15 +0100 (CET) Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1E440615B2 for ; Wed, 30 Dec 2020 19:01:12 +0100 (CET) X-Originating-IP: 2.224.242.101 Received: from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id A45E020003; Wed, 30 Dec 2020 18:01:11 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Wed, 30 Dec 2020 19:01:16 +0100 Message-Id: <20201230180120.78407-3-jacopo@jmondi.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201230180120.78407-1-jacopo@jmondi.org> References: <20201230180120.78407-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 2/6] libcamera: camera_sensor: Validate driver support 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" The CameraSensor class requires the sensor driver to report information through V4L2 controls and through the V4L2 selection API, and uses that information to register Camera properties and to construct CameraSensorInfo class instances to provide them to the IPA. Currently, validation of the kernel support happens each time a feature is requested, with slighly similar debug/error messages output to the user in case a feature is not supported. Rationalize this by: - Validate the sensor driver requirements in a single function - Expand the debug output when a property gets defaulted to a value - Be more verbose when constructing CameraSensorInfo is not possible Reviewed-by: Paul Elder Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- include/libcamera/internal/camera_sensor.h | 1 + src/libcamera/camera_sensor.cpp | 117 ++++++++++++++++++--- 2 files changed, 102 insertions(+), 16 deletions(-) diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h index f80d836161a5..aee10aa6e3c7 100644 --- a/include/libcamera/internal/camera_sensor.h +++ b/include/libcamera/internal/camera_sensor.h @@ -69,6 +69,7 @@ protected: private: int generateId(); + int validateSensorDriver(); int initProperties(); const MediaEntity *entity_; diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index e786821d4ba2..a1f1256bd6f4 100644 --- a/src/libcamera/camera_sensor.cpp +++ b/src/libcamera/camera_sensor.cpp @@ -207,6 +207,10 @@ int CameraSensor::init() */ resolution_ = sizes_.back(); + ret = validateSensorDriver(); + if (ret) + return ret; + ret = initProperties(); if (ret) return ret; @@ -214,6 +218,86 @@ int CameraSensor::init() return 0; } +int CameraSensor::validateSensorDriver() +{ + /* + * Make sure the sensor driver supports the mandatory controls + * required by the CameraSensor class. + * - V4L2_CID_PIXEL_RATE is used to calculate the sensor timings + * - V4L2_CID_HBLANK is used to calculate the line length + */ + const std::vector mandatoryControls{ + V4L2_CID_PIXEL_RATE, + V4L2_CID_HBLANK, + }; + + ControlList ctrls = subdev_->getControls(mandatoryControls); + if (ctrls.empty()) { + LOG(CameraSensor, Error) + << "Mandatory V4L2 controls not available"; + LOG(CameraSensor, Error) + << "The sensor kernel driver needs to be fixed"; + LOG(CameraSensor, Error) + << "See Documentation/sensor_driver_requirements.rst in the libcamera sources for more information"; + return -EINVAL; + } + + int err = 0; + /* + * Optional controls are used to register optional sensor + * properties. If not present, some values will be defaulted. + */ + const std::vector optionalControls{ + V4L2_CID_CAMERA_ORIENTATION, + V4L2_CID_CAMERA_SENSOR_ROTATION, + }; + + ctrls = subdev_->getControls(optionalControls); + if (ctrls.empty()) { + LOG(CameraSensor, Info) + << "Optional V4L2 controls not supported"; + err = -EINVAL; + } + + /* + * Make sure the required selection targets are supported. + * + * Failures in reading any of the targets are not deemed to be fatal, + * but some properties and features, like constructing a + * CameraSensorInfo for the IPA module, won't be supported. + */ + Rectangle rect; + int ret = subdev_->getSelection(pad_, V4L2_SEL_TGT_CROP_BOUNDS, &rect); + if (ret) { + LOG(CameraSensor, Info) + << "Failed to retrieve the readable pixel area size"; + err = -EINVAL; + } + + ret = subdev_->getSelection(pad_, V4L2_SEL_TGT_CROP_DEFAULT, &rect); + if (ret) { + LOG(CameraSensor, Info) + << "Failed to retrieve the active pixel area size"; + err = -EINVAL; + } + + ret = subdev_->getSelection(pad_, V4L2_SEL_TGT_CROP, &rect); + if (ret) { + LOG(CameraSensor, Info) + << "Failed to retreive the sensor crop rectangle"; + err = -EINVAL; + } + + if (err) { + LOG(CameraSensor, Info) + << "The sensor kernel driver needs to be fixed"; + LOG(CameraSensor, Info) + << "See Documentation/sensor_driver_requirements.rst in the libcamera sources for more information"; + } + + return 0; +} + int CameraSensor::initProperties() { /* @@ -278,21 +362,29 @@ int CameraSensor::initProperties() } } else { propertyValue = properties::CameraLocationFront; + LOG(CameraSensor, Debug) + << "Location property defaulted to 'Front Camera'"; } properties_.set(properties::Location, propertyValue); /* Camera Rotation: default is 0 degrees. */ const auto &rotationControl = controls.find(V4L2_CID_CAMERA_SENSOR_ROTATION); - if (rotationControl != controls.end()) + if (rotationControl != controls.end()) { propertyValue = rotationControl->second.def().get(); - else + } else { propertyValue = 0; + LOG(CameraSensor, Debug) + << "Rotation property defaulted to 0 degrees"; + } properties_.set(properties::Rotation, propertyValue); Rectangle bounds; ret = subdev_->getSelection(pad_, V4L2_SEL_TGT_CROP_BOUNDS, &bounds); if (!ret) properties_.set(properties::PixelArraySize, bounds.size()); + else + LOG(CameraSensor, Debug) + << "PixelArraySize property not registered"; Rectangle crop; ret = subdev_->getSelection(pad_, V4L2_SEL_TGT_CROP_DEFAULT, &crop); @@ -306,6 +398,9 @@ int CameraSensor::initProperties() crop.x -= bounds.x; crop.y -= bounds.y; properties_.set(properties::PixelArrayActiveAreas, { crop }); + } else { + LOG(CameraSensor, Debug) + << "PixelArrayActiveAreas property not registered"; } /* Color filter array pattern, register only for RAW sensors. */ @@ -566,10 +661,7 @@ int CameraSensor::sensorInfo(CameraSensorInfo *info) const Rectangle rect; int ret = subdev_->getSelection(pad_, V4L2_SEL_TGT_CROP_DEFAULT, &rect); if (ret) { - LOG(CameraSensor, Error) - << "Failed to construct camera sensor info: " - << "the camera sensor does not report the active area"; - + LOG(CameraSensor, Error) << "Failed to get the active area"; return ret; } info->activeAreaSize = { rect.width, rect.height }; @@ -577,9 +669,7 @@ int CameraSensor::sensorInfo(CameraSensorInfo *info) const /* It's mandatory for the subdevice to report its crop rectangle. */ ret = subdev_->getSelection(pad_, V4L2_SEL_TGT_CROP, &info->analogCrop); if (ret) { - LOG(CameraSensor, Error) - << "Failed to construct camera sensor info: " - << "the camera sensor does not report the crop rectangle"; + LOG(CameraSensor, Error) << "Failed to get the crop rectangle"; return ret; } @@ -601,16 +691,11 @@ int CameraSensor::sensorInfo(CameraSensorInfo *info) const info->bitsPerPixel = format.bitsPerPixel(); info->outputSize = format.size; - /* - * Retrieve the pixel rate and the line length through V4L2 controls. - * Support for the V4L2_CID_PIXEL_RATE and V4L2_CID_HBLANK controls is - * mandatory. - */ + /* Retrieve the pixel rate and the line length through V4L2 controls. */ ControlList ctrls = subdev_->getControls({ V4L2_CID_PIXEL_RATE, V4L2_CID_HBLANK }); if (ctrls.empty()) { - LOG(CameraSensor, Error) - << "Failed to retrieve camera info controls"; + LOG(CameraSensor, Error) << "Failed to retrieve camera controls"; return -EINVAL; }