media: Introduce V4L2 generic ISP support
Subsystems (5)
Jacopo Mondi
maintainerv9 cover media: Introduce V4L2 generic ISP support
Extensible parameters meta formats have been introduced in the Linux kernel v6.12 initially to support different revision of the RkISP1 ISP implemented in different SoC. In order to avoid breaking userspace everytime an ISP configuration block is added or modified in the uAPI these new formats, which are versionated and extensible by their definition have been introduced.
See for reference:e9d05e9d5db1 ("media: uapi: rkisp1-config: Add extensible params format") 6c53a7b68c5d ("media: rkisp1: Implement extensible params support")
The Amlogic C3 ISP driver followed shortly, introducing an extensible format for the ISP configuration:
6d406187ebc0 ("media: uapi: Add stats info and parameters buffer for C3 ISP")
with a very similar, if not identical, implementation of the routines to validate and handle the ISP configuration in the ISP driver in the c3-isp-params.c file.
fb2e135208f3 ("media: platform: Add C3 ISP driver")
With the recent upstreaming attempt of the Mali C55 ISP driver from Dan, a third user of extensible parameters is going to be itroduced in the kernel, duplicating again in the driver the procedure for validating and handling the ISP configuration blocks
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/
To avoid duplicating again the validation routines and common types definition, this series introduces v4l2-isp.c/.h for the kAPI and v4l2-isp.h for the uAPI and re-organize the RkISP1
and Amlogic C3 drivers to use the common types and the helper validation routines.
The v4l2-isp abstraction will be augmented to support statistcs as well.
If the here proposed approach is accepted, I propose to rebase the Mali C55 driver on top of this series, to use the new common types and helpers.
Tested on Mali C55 and RkISP1 with camshark
Thanks
j
---
Changes in v9:
- Modify the RkISP1 and C3 params drivers to enumerate their block info
with macros (and static_assert they're of the correct size)
- Modify documentation according to Laurent's suggestion
- Link to v8: https://lore.kernel.org/r/20251020-extensible-parameters-validation-v8-0-afba4ba7b42d@ideasonboard.com
Changes in v8:
- Address Michael comments and reword documentation
- Use the correct "metadata capture" for stats and "metadata output" for
parameters. I mixed up the two in the previous revisions.
- Link to v7: https://lore.kernel.org/r/20251014-extensible-parameters-validation-v7-0-6628bed5ca98@ideasonboard.com
Changes in v7:
- Moved version to the v4l2-isp uAPI
- Moved version check to the v4l2-isp.c helpers
- Link to v6: https://lore.kernel.org/r/20251007-extensible-parameters-validation-v6-0-5f719d9f39e5@ideasonboard.com
Changes in v6:
- Rename all symbols to v4l2_isp
- Changed the interface of the two buffer validation functions
- Reworked the rkisp1 and c3 porting accordingly
- Updated documentation
- I have moved v4l2_params_buffer_size() from uAPI because it was
convenient for linux but not required in userspace
- Link to v5: https://lore.kernel.org/r/20250915-extensible-parameters-validation-v5-0-e6db94468af3@ideasonboard.com
Changes in v5:
- Move everything to v4l2-isp prefix except from format documentation
which still is about 'extensible-parameters' (to be paired in future
with extensbile-stats)
- Simplify documentation and move it part to the driver-api
Documentation
- Remove 'group' and 'features' from the generic handlers definition and
adjust rkisp1 accordingly
- Link to v4: https://lore.kernel.org/r/20250820-extensible-parameters-validation-v4-0-30fe5a99cb1f@ideasonboard.com
Changes in v4:
- Fix the definition of V4L2_PARAMS_FL_PLATFORM_FLAGS
- Add __counted_by() attribute to the data[] flexible-array member of
v4l2_params_buffer
- Minor style change
- Link to v3: https://lore.kernel.org/r/20250819-extensible-parameters-validation-v3-0-9dc008348b30@ideasonboard.com
Changes in v3:
- Rebased on latest media-committers/next
- Take in Dan's suggestion in block size validation
- Documentation minor spelling fixes
- Link to v2: https://lore.kernel.org/r/20250710-extensible-parameters-validation-v2-0-7ec8918ec443@ideasonboard.com
Changes in v2:
- Make v4l2_params_buffer directly usable
- Centralize ENABLE/DISABLE flags definition and validation
- Take in Dan's v4l2_params_buffer_size()
- Allow blocks to only contain the header if they're going to be
disabled
- Documentation fixes as reported by Nicolas
- Link to v1: https://lore.kernel.org/r/20250708-extensible-parameters-validation-v1-0-9fc27c9c728c@ideasonboard.com
---
Jacopo Mondi (8):
media: uapi: Introduce V4L2 generic ISP types
media: uapi: Convert RkISP1 to V4L2 extensible params
media: uapi: Convert Amlogic C3 to V4L2 extensible params
media: Documentation: uapi: Add V4L2 ISP documentation
media: v4l2-core: Introduce v4l2-isp.c
media: rkisp1: Use v4l2-isp for validation
media: amlogic-c3: Use v4l2-isp for validation
media: Documentation: kapi: Add v4l2 generic ISP support
Documentation/driver-api/media/v4l2-core.rst | 1 + Documentation/driver-api/media/v4l2-isp.rst | 49 ++++++ .../userspace-api/media/v4l/meta-formats.rst | 1 + Documentation/userspace-api/media/v4l/v4l2-isp.rst | 67 +++++++++ MAINTAINERS | 10 ++ drivers/media/platform/amlogic/c3/isp/Kconfig | 1 + .../media/platform/amlogic/c3/isp/c3-isp-params.c | 165 ++++++--------------- drivers/media/platform/rockchip/rkisp1/Kconfig | 1 + .../media/platform/rockchip/rkisp1/rkisp1-params.c | 150 ++++++------------- drivers/media/v4l2-core/Kconfig | 4 + drivers/media/v4l2-core/Makefile | 1 + drivers/media/v4l2-core/v4l2-isp.c | 128 ++++++++++++++++ include/media/v4l2-isp.h | 91 ++++++++++++ include/uapi/linux/media/amlogic/c3-isp-config.h | 92 +++--------- include/uapi/linux/media/v4l2-isp.h | 102 +++++++++++++ include/uapi/linux/rkisp1-config.h | 107 +++---------- 16 files changed, 590 insertions(+), 380 deletions(-)
---
base-commit: b4fbb13db86adb0bae1d7f968b61ea8dc9635e33
change-id: 20250701-extensible-parameters-validation-c831f7f5cc0b
Best regards,
Signature · 1 line
Jacopo Mondi <[email protected]>
Jacopo Mondi
maintainerPATCH v9 1/8 media: uapi: Introduce V4L2 generic ISP types
Introduce v4l2-isp.h in the Linux kernel uAPI.
The header includes types for generic ISP configuration parameters and will be extended in the future with support for generic ISP statistics formats.
Generic ISP parameters support is provided by introducing two new types that represent an extensible and versioned buffer of ISP configuration parameters.
The v4l2_params_buffer represents the container for the ISP configuration data block. The generic type is defined with a 0-sized data member that the ISP driver implementations shall properly size according to their capabilities. The v4l2_params_block_header structure represents the header to be prepend to each ISP configuration block.
--- MAINTAINERS | 6 +++ include/uapi/linux/media/v4l2-isp.h | 102 ++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+)
MAINTAINERS +6 -0
@@ -26855,6 +26855,12 @@ F: drivers/media/i2c/vd55g1.c F: drivers/media/i2c/vd56g3.c F: drivers/media/i2c/vgxy61.c +V4L2 GENERIC ISP PARAMETERS AND STATISTIC FORMATS +M: Jacopo Mondi <[email protected]> +L: [email protected] +S: Maintained +F: include/uapi/linux/media/v4l2-isp.h + VF610 NAND DRIVER M: Stefan Agner <[email protected]> L: [email protected]
include/uapi/linux/media/v4l2-isp.h new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
2.51.0
Jacopo Mondi
maintainerPATCH v9 2/8 media: uapi: Convert RkISP1 to V4L2 extensible params
In reply to Jacopo MondiWith the introduction of common types for extensible parameters format, convert the rkisp1-config.h header to use the new types.
Factor out the documentation that is now part of the common header and only keep the driver-specific on in place.
The conversion to use common types doesn't impact userspace as the new types are either identical to the ones already existing in the RkISP1 uAPI or are 1-to-1 type convertible.
--- include/uapi/linux/rkisp1-config.h | 107 +++++++++---------------------------- 1 file changed, 24 insertions(+), 83 deletions(-)
include/uapi/linux/rkisp1-config.h +24 -83
@@ -7,8 +7,13 @@#ifndef _UAPI_RKISP1_CONFIG_H#define _UAPI_RKISP1_CONFIG_H +#ifdef __KERNEL__ +#include <linux/build_bug.h> +#endif /* __KERNEL__ */ #include <linux/types.h> +#include <linux/media/v4l2-isp.h> +/* Defect Pixel Cluster Detection */ #define RKISP1_CIF_ISP_MODULE_DPCC (1U << 0)/* Black Level Subtraction */
@@ -1158,79 +1163,26 @@ enum rkisp1_ext_params_block_type { RKISP1_EXT_PARAMS_BLOCK_TYPE_WDR, }; -#define RKISP1_EXT_PARAMS_FL_BLOCK_DISABLE (1U << 0) -#define RKISP1_EXT_PARAMS_FL_BLOCK_ENABLE (1U << 1) +/* For backward compatibility */ +#define RKISP1_EXT_PARAMS_FL_BLOCK_DISABLE V4L2_ISP_PARAMS_FL_BLOCK_DISABLE +#define RKISP1_EXT_PARAMS_FL_BLOCK_ENABLE V4L2_ISP_PARAMS_FL_BLOCK_ENABLE/* A bitmask of parameters blocks supported on the current hardware. */ #define RKISP1_CID_SUPPORTED_PARAMS_BLOCKS (V4L2_CID_USER_RKISP1_BASE + 0x01)/** -* struct rkisp1_ext_params_block_header - RkISP1 extensible parameters block -* header +* rkisp1_ext_params_block_header - RkISP1 extensible parameters block header * * This structure represents the common part of all the ISP configuration -* blocks. Each parameters block shall embed an instance of this structure type -* as its first member, followed by the block-specific configuration data. The -* driver inspects this common header to discern the block type and its size and -* properly handle the block content by casting it to the correct block-specific -* type. +* blocks and is identical to :c:type:`v4l2_isp_params_block_header`. * -* The @type field is one of the values enumerated by +* The type field is one of the values enumerated by * :c:type:`rkisp1_ext_params_block_type` and specifies how the data should be -* interpreted by the driver. The @size field specifies the size of the -* parameters block and is used by the driver for validation purposes. -* -* The @flags field is a bitmask of per-block flags RKISP1_EXT_PARAMS_FL_*. -* -* When userspace wants to configure and enable an ISP block it shall fully -* populate the block configuration and set the -* RKISP1_EXT_PARAMS_FL_BLOCK_ENABLE bit in the @flags field. -* -* When userspace simply wants to disable an ISP block the -* RKISP1_EXT_PARAMS_FL_BLOCK_DISABLE bit should be set in @flags field. The -* driver ignores the rest of the block configuration structure in this case. -* -* If a new configuration of an ISP block has to be applied userspace shall -* fully populate the ISP block configuration and omit setting the -* RKISP1_EXT_PARAMS_FL_BLOCK_ENABLE and RKISP1_EXT_PARAMS_FL_BLOCK_DISABLE bits -* in the @flags field. -* -* Setting both the RKISP1_EXT_PARAMS_FL_BLOCK_ENABLE and -* RKISP1_EXT_PARAMS_FL_BLOCK_DISABLE bits in the @flags field is not allowed -* and not accepted by the driver. -* -* Userspace is responsible for correctly populating the parameters block header -* fields (@type, @flags and @size) and the block-specific parameters. -* -* For example: +* interpreted by the driver. * -* .. code-block:: c -* -* void populate_bls(struct rkisp1_ext_params_block_header *block) { -* struct rkisp1_ext_params_bls_config *bls = -* (struct rkisp1_ext_params_bls_config *)block; -* -* bls->header.type = RKISP1_EXT_PARAMS_BLOCK_ID_BLS; -* bls->header.flags = RKISP1_EXT_PARAMS_FL_BLOCK_ENABLE; -* bls->header.size = sizeof(*bls); -* -* bls->config.enable_auto = 0; -* bls->config.fixed_val.r = blackLevelRed_; -* bls->config.fixed_val.gr = blackLevelGreenR_; -* bls->config.fixed_val.gb = blackLevelGreenB_; -* bls->config.fixed_val.b = blackLevelBlue_; -* } -* -* @type: The parameters block type, see -* :c:type:`rkisp1_ext_params_block_type` -* @flags: A bitmask of block flags -* @size: Size (in bytes) of the parameters block, including this header +* The flags field is a bitmask of per-block flags RKISP1_EXT_PARAMS_FL_*. */ -struct rkisp1_ext_params_block_header { -__u16 type; -__u16 flags; -__u32 size; -}; +#define rkisp1_ext_params_block_header v4l2_isp_params_block_header/** * struct rkisp1_ext_params_bls_config - RkISP1 extensible params BLS config
@@ -1588,27 +1540,14 @@ struct rkisp1_ext_params_wdr_config {* @RKISP1_EXT_PARAM_BUFFER_V1: First version of RkISP1 extensible parameters */ enum rksip1_ext_param_buffer_version { -RKISP1_EXT_PARAM_BUFFER_V1 =1 , +RKISP1_EXT_PARAM_BUFFER_V1 =V4L2_ISP_PARAMS_VERSION_V1 , };/** * struct rkisp1_ext_params_cfg - RkISP1 extensible parameters configuration * -* This struct contains the configuration parameters of the RkISP1 ISP -* algorithms, serialized by userspace into a data buffer. Each configuration -* parameter block is represented by a block-specific structure which contains a -* :c:type:`rkisp1_ext_params_block_header` entry as first member. Userspace -* populates the @data buffer with configuration parameters for the blocks that -* it intends to configure. As a consequence, the data buffer effective size -* changes according to the number of ISP blocks that userspace intends to -* configure and is set by userspace in the @data_size field. -* -* The parameters buffer is versioned by the @version field to allow modifying -* and extending its definition. Userspace shall populate the @version field to -* inform the driver about the version it intends to use. The driver will parse -* and handle the @data buffer according to the data layout specific to the -* indicated version and return an error if the desired version is not -* supported. +* This is the driver-specific implementation of +* :c:type:`v4l2_isp_params_buffer`. * * Currently the single RKISP1_EXT_PARAM_BUFFER_V1 version is supported. * When a new format version will be added, a mechanism for userspace to query
@@ -1624,11 +1563,6 @@ enum rksip1_ext_param_buffer_version {* the maximum value represents the blocks supported by the kernel driver, * independently of the device instance. * -* For each ISP block that userspace wants to configure, a block-specific -* structure is appended to the @data buffer, one after the other without gaps -* in between nor overlaps. Userspace shall populate the @data_size field with -* the effective size, in bytes, of the @data buffer. -* * The expected memory layout of the parameters buffer is:: * * +-------------------- struct rkisp1_ext_params_cfg -------------------+
@@ -1678,4 +1612,11 @@ struct rkisp1_ext_params_cfg { __u8 data[RKISP1_EXT_PARAMS_MAX_SIZE]; }; +#ifdef __KERNEL__ +/* Make sure the header is type-convertible to the generic v4l2 params one */ +static_assert ((sizeof (struct rkisp1_ext_params_cfg ) - +RKISP1_EXT_PARAMS_MAX_SIZE ) == +sizeof (struct v4l2_isp_params_buffer )); +#endif /* __KERNEL__ */ +#endif /* _UAPI_RKISP1_CONFIG_H */
Signature · 1 line
2.51.0
Jacopo Mondi
maintainerPATCH v9 3/8 media: uapi: Convert Amlogic C3 to V4L2 extensible params
In reply to Jacopo MondiWith the introduction of common types for extensible parameters format, convert the c3-isp-config.h header to use the new types.
Factor-out the documentation that is now part of the common header and only keep the driver-specific on in place.
The conversion to use common types doesn't impact userspace as the new types are either identical to the ones already existing in the C3 ISP uAPI or are 1-to-1 type convertible.
--- include/uapi/linux/media/amlogic/c3-isp-config.h | 92 +++++++----------------- 1 file changed, 24 insertions(+), 68 deletions(-)
include/uapi/linux/media/amlogic/c3-isp-config.h +24 -68
@@ -6,8 +6,13 @@#ifndef _UAPI_C3_ISP_CONFIG_H_#define _UAPI_C3_ISP_CONFIG_H_ +#ifdef __KERNEL__ +#include <linux/build_bug.h> +#endif /* __KERNEL__ */ #include <linux/types.h> +#include <linux/media/v4l2-isp.h> +/* * Frames are split into zones of almost equal width and height - a zone is a * rectangular tile of a frame. The metering blocks within the ISP collect
@@ -141,7 +146,7 @@ struct c3_isp_stats_info {* @C3_ISP_PARAMS_BUFFER_V0: First version of C3 ISP parameters block */ enum c3_isp_params_buffer_version { -C3_ISP_PARAMS_BUFFER_V0 , +C3_ISP_PARAMS_BUFFER_V0 =V4L2_ISP_PARAMS_VERSION_V0 , };/**
@@ -176,62 +181,23 @@ enum c3_isp_params_block_type { C3_ISP_PARAMS_BLOCK_SENTINEL }; -#define C3_ISP_PARAMS_BLOCK_FL_DISABLE (1U << 0) -#define C3_ISP_PARAMS_BLOCK_FL_ENABLE (1U << 1) +/* For backward compatibility */ +#define C3_ISP_PARAMS_BLOCK_FL_DISABLE V4L2_ISP_PARAMS_FL_BLOCK_DISABLE +#define C3_ISP_PARAMS_BLOCK_FL_ENABLE V4L2_ISP_PARAMS_FL_BLOCK_ENABLE/** * struct c3_isp_params_block_header - C3 ISP parameter block header * * This structure represents the common part of all the ISP configuration -* blocks. Each parameters block shall embed an instance of this structure type -* as its first member, followed by the block-specific configuration data. The -* driver inspects this common header to discern the block type and its size and -* properly handle the block content by casting it to the correct block-specific -* type. +* blocks and is identical to :c:type:`v4l2_isp_params_block_header`. * -* The @type field is one of the values enumerated by +* The type field is one of the values enumerated by * :c:type:`c3_isp_params_block_type` and specifies how the data should be -* interpreted by the driver. The @size field specifies the size of the -* parameters block and is used by the driver for validation purposes. The -* @flags field is a bitmask of per-block flags C3_ISP_PARAMS_FL*. -* -* When userspace wants to disable an ISP block the -* C3_ISP_PARAMS_BLOCK_FL_DISABLED bit should be set in the @flags field. In -* this case userspace may optionally omit the remainder of the configuration -* block, which will be ignored by the driver. -* -* When a new configuration of an ISP block needs to be applied userspace -* shall fully populate the ISP block and omit setting the -* C3_ISP_PARAMS_BLOCK_FL_DISABLED bit in the @flags field. -* -* Userspace is responsible for correctly populating the parameters block header -* fields (@type, @flags and @size) and the block-specific parameters. -* -* For example: -* -* .. code-block:: c +* interpreted by the driver. * -* void populate_pst_gamma(struct c3_isp_params_block_header *block) { -* struct c3_isp_params_pst_gamma *gamma = -* (struct c3_isp_params_pst_gamma *)block; -* -* gamma->header.type = C3_ISP_PARAMS_BLOCK_PST_GAMMA; -* gamma->header.flags = C3_ISP_PARAMS_BLOCK_FL_ENABLE; -* gamma->header.size = sizeof(*gamma); -* -* for (unsigned int i = 0; i < 129; i++) -* gamma->pst_gamma_lut[i] = i; -* } -* -* @type: The parameters block type from :c:type:`c3_isp_params_block_type` -* @flags: A bitmask of block flags -* @size: Size (in bytes) of the parameters block, including this header +* The flags field is a bitmask of per-block flags C3_ISP_PARAMS_FL_*. */ -struct c3_isp_params_block_header { -__u16 type; -__u16 flags; -__u32 size; -}; +#define c3_isp_params_block_header v4l2_isp_params_block_header/** * struct c3_isp_params_awb_gains - Gains for auto-white balance
@@ -498,26 +464,10 @@ struct c3_isp_params_blc {/** * struct c3_isp_params_cfg - C3 ISP configuration parameters * -* This struct contains the configuration parameters of the C3 ISP -* algorithms, serialized by userspace into an opaque data buffer. Each -* configuration parameter block is represented by a block-specific structure -* which contains a :c:type:`c3_isp_param_block_header` entry as first -* member. Userspace populates the @data buffer with configuration parameters -* for the blocks that it intends to configure. As a consequence, the data -* buffer effective size changes according to the number of ISP blocks that -* userspace intends to configure. -* -* The parameters buffer is versioned by the @version field to allow modifying -* and extending its definition. Userspace should populate the @version field to -* inform the driver about the version it intends to use. The driver will parse -* and handle the @data buffer according to the data layout specific to the -* indicated revision and return an error if the desired revision is not -* supported. -* -* For each ISP block that userspace wants to configure, a block-specific -* structure is appended to the @data buffer, one after the other without gaps -* in between nor overlaps. Userspace shall populate the @total_size field with -* the effective size, in bytes, of the @data buffer. +* This is the driver-specific implementation of +* :c:type:`v4l2_isp_params_buffer`. +* +* Currently only C3_ISP_PARAM_BUFFER_V0 is supported. * * The expected memory layout of the parameters buffer is:: *
@@ -561,4 +511,10 @@ struct c3_isp_params_cfg { __u8 data[C3_ISP_PARAMS_MAX_SIZE]; }; +#ifdef __KERNEL__ +/* Make sure the header is type-convertible to the generic v4l2 params one */ +static_assert ((sizeof (struct c3_isp_params_cfg ) -C3_ISP_PARAMS_MAX_SIZE ) == +sizeof (struct v4l2_isp_params_buffer )); +#endif /* __KERNEL__ */ +#endif
Signature · 1 line
2.51.0
Jacopo Mondi
maintainerPATCH v9 4/8 media: Documentation: uapi: Add V4L2 ISP documentation
In reply to Jacopo MondiAdd userspace documentation for V4L2 ISP generic parameters and statistics formats.
--- .../userspace-api/media/v4l/meta-formats.rst | 1 + Documentation/userspace-api/media/v4l/v4l2-isp.rst | 67 ++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 69 insertions(+)
Documentation/userspace-api/media/v4l/meta-formats.rst +1 -0
@@ -25,3 +25,4 @@ These formats are used for the :ref:`metadata` interface only. metafmt-vivid metafmt-vsp1-hgo metafmt-vsp1-hgt + v4l2-isp
Documentation/userspace-api/media/v4l/v4l2-isp.rst new file +67 -0
67 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -26859,6 +26859,7 @@ V4L2 GENERIC ISP PARAMETERS AND STATISTIC FORMATS M: Jacopo Mondi <[email protected]> L: [email protected] S: Maintained +F: Documentation/userspace-api/media/v4l/v4l2-isp.rst F: include/uapi/linux/media/v4l2-isp.h VF610 NAND DRIVER
Signature · 1 line
2.51.0
Jacopo Mondi
maintainerPATCH v9 5/8 media: v4l2-core: Introduce v4l2-isp.c
In reply to Jacopo MondiAdd to the V4L2 framework helper functions to support drivers when validating a buffer of V4L2 ISP parameters.
Driver shall use v4l2_isp_params_validate_buffer_size() to verify the size correctness of the data received from userspace, and after having copied the data to a kernel-only memory location, complete the validation by calling v4l2_isp_params_validate_buffer().
--- MAINTAINERS | 2 + drivers/media/v4l2-core/Kconfig | 4 ++ drivers/media/v4l2-core/Makefile | 1 + drivers/media/v4l2-core/v4l2-isp.c | 128 +++++++++++++++++++++++++++++++++++++ include/media/v4l2-isp.h | 91 ++++++++++++++++++++++++++ 5 files changed, 226 insertions(+)
MAINTAINERS +2 -0
@@ -26860,6 +26860,8 @@ M: Jacopo Mondi <[email protected]> L: [email protected] S: Maintained F: Documentation/userspace-api/media/v4l/v4l2-isp.rst +F: drivers/media/v4l2-core/v4l2-isp.c +F: include/media/v4l2-isp.h F: include/uapi/linux/media/v4l2-isp.h VF610 NAND DRIVER
drivers/media/v4l2-core/Kconfig +4 -0
@@ -82,3 +82,7 @@ config V4L2_CCI_I2C depends on I2C select REGMAP_I2C select V4L2_CCI + +config V4L2_ISP + tristate + depends on VIDEOBUF2_CORE
drivers/media/v4l2-core/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-$(CONFIG_V4L2_CCI) += v4l2-cci.o obj-$(CONFIG_V4L2_FLASH_LED_CLASS) += v4l2-flash-led-class.o obj-$(CONFIG_V4L2_FWNODE) += v4l2-fwnode.o obj-$(CONFIG_V4L2_H264) += v4l2-h264.o +obj-$(CONFIG_V4L2_ISP) += v4l2-isp.o obj-$(CONFIG_V4L2_JPEG_HELPER) += v4l2-jpeg.o obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o obj-$(CONFIG_V4L2_VP9) += v4l2-vp9.o
drivers/media/v4l2-core/v4l2-isp.c new file +128 -0
128 lines not on this page · raw message
include/media/v4l2-isp.h new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH v9 5/8] media: v4l2-core: Introduce v4l2-isp.c
Hi Jacopo,
While reviewing your code in the Mali PR I found one thing confusing in the v4l2-isp part. My apologies for not reviewing this before, I had too much to do.
Quoted 241 lines · Jacopo Mondi
Add to the V4L2 framework helper functions to support drivers when validating a buffer of V4L2 ISP parameters.
Driver shall use
v4l2_isp_params_validate_buffer_size()to verify the size correctness of the data received from userspace, and after having copied the data to a kernel-only memory location, complete the validation by callingv4l2_isp_params_validate_buffer().Reviewed-by Daniel Scally <[email protected]>Reviewed-by Laurent Pinchart <[email protected]>Reviewed-by Michael Riesch <[email protected]>Acked-by Sakari Ailus <[email protected]>Tested-by Lad Prabhakar <[email protected]>Signed-off-by Jacopo Mondi <[email protected]>--- MAINTAINERS | 2 + drivers/media/v4l2-core/Kconfig | 4 ++ drivers/media/v4l2-core/Makefile | 1 + drivers/media/v4l2-core/v4l2-isp.c | 128 +++++++++++++++++++++++++++++++++++++ include/media/v4l2-isp.h | 91 ++++++++++++++++++++++++++ 5 files changed, 226 insertions(+)MAINTAINERS +2
-0@@ -26860,6 +26860,8 @@ M: Jacopo Mondi <[email protected]> L: [email protected] S: Maintained F: Documentation/userspace-api/media/v4l/v4l2-isp.rst +F: drivers/media/v4l2-core/v4l2-isp.c +F: include/media/v4l2-isp.h F: include/uapi/linux/media/v4l2-isp.h VF610 NAND DRIVERdrivers/media/v4l2-core/Kconfig +4
-0@@ -82,3 +82,7 @@ config V4L2_CCI_I2C depends on I2C select REGMAP_I2C select V4L2_CCI + +config V4L2_ISP + tristate + depends on VIDEOBUF2_COREdrivers/media/v4l2-core/Makefile +1
-0@@ -29,6 +29,7 @@ obj-$(CONFIG_V4L2_CCI) += v4l2-cci.o obj-$(CONFIG_V4L2_FLASH_LED_CLASS) += v4l2-flash-led-class.o obj-$(CONFIG_V4L2_FWNODE) += v4l2-fwnode.o obj-$(CONFIG_V4L2_H264) += v4l2-h264.o +obj-$(CONFIG_V4L2_ISP) += v4l2-isp.o obj-$(CONFIG_V4L2_JPEG_HELPER) += v4l2-jpeg.o obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o obj-$(CONFIG_V4L2_VP9) += v4l2-vp9.odrivers/media/v4l2-core/v4l2-isp.c new file +128
-0@@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Video4Linux2 generic ISP parameters and statistics support +* +* Copyright (C) 2025 Ideas On Board Oy +* Author: Jacopo Mondi <[email protected]> +*/ + +#include <media/v4l2-isp.h> + +#include <linux/bitops.h> +#include <linux/device.h> + +#include <media/videobuf2-core.h> + +int v4l2_isp_params_validate_buffer_size (struct device *dev, +struct vb2_buffer *vb, +size_t max_size) +{ +size_t header_size = offsetof(struct v4l2_isp_params_buffer , data); +size_t payload_size =vb2_get_plane_payload (vb,0 ); + +/* Payload size can't be greater than the destination buffer size */ +if (payload_size > max_size) { +dev_dbg (dev,"Payload size is too large: %zu\n" , payload_size); +return -EINVAL ; + } + +/* Payload size can't be smaller than the header size */ +if (payload_size < header_size) { +dev_dbg (dev,"Payload size is too small: %zu\n" , payload_size); +return -EINVAL ; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (v4l2_isp_params_validate_buffer_size); + +int v4l2_isp_params_validate_buffer (struct device *dev,struct vb2_buffer *vb, +const struct v4l2_isp_params_buffer *buffer, +const struct v4l2_isp_params_block_info *info, +size_t num_blocks) +{ +size_t header_size = offsetof(struct v4l2_isp_params_buffer , data); +size_t payload_size =vb2_get_plane_payload (vb,0 ); +size_t block_offset =0 ; +size_t buffer_size; + +/* +* Currently only the first version of the V4L2 ISP parameters format is +* supported. We accept both V0 and V1 to support existing drivers +* compatible with V4L2 ISP that use either 0 or 1 as their "first +* version" identifiers. +*/ +if (buffer->version !=V4L2_ISP_PARAMS_VERSION_V0 && + buffer->version !=V4L2_ISP_PARAMS_VERSION_V1 ) { +dev_dbg (dev, +"Unsupported V4L2 ISP parameters format version: %u\n" , + buffer->version); +return -EINVAL ; + } + +/* Validate the size reported in the header */ + buffer_size = header_size + buffer->data_size; +if (buffer_size != payload_size) { +dev_dbg (dev,"Data size %zu and payload size %zu are different\n" , + buffer_size, payload_size); +return -EINVAL ; + } + +/* Walk the list of ISP configuration blocks and validate them. */ + buffer_size = buffer->data_size; +while (buffer_size >=sizeof (struct v4l2_isp_params_block_header )) { +const struct v4l2_isp_params_block_info *block_info; +const struct v4l2_isp_params_block_header *block; + + block = (const struct v4l2_isp_params_block_header *) + (buffer->data + block_offset); + +if (block->type >= num_blocks) { +dev_dbg (dev, +"Invalid block type %u at offset %zu\n" , + block->type, block_offset); +return -EINVAL ; + } + +if (block->size > buffer_size) { +dev_dbg (dev,"Premature end of parameters data\n" ); +return -EINVAL ; + } + +/* It's invalid to specify both ENABLE and DISABLE. */ +if ((block->flags & (V4L2_ISP_PARAMS_FL_BLOCK_ENABLE | +V4L2_ISP_PARAMS_FL_BLOCK_DISABLE )) == + (V4L2_ISP_PARAMS_FL_BLOCK_ENABLE | +V4L2_ISP_PARAMS_FL_BLOCK_DISABLE )) { +dev_dbg (dev,"Invalid block flags %x at offset %zu\n" , + block->flags, block_offset); +return -EINVAL ; + } + +/* +* Match the block reported size against the info provided +* one, but allow the block to only contain the header in +* case it is going to be disabled. +*/ + block_info = &info[block->type]; +if (block->size != block_info->size && + (!(block->flags &V4L2_ISP_PARAMS_FL_BLOCK_DISABLE ) || + block->size !=sizeof (*block))) { +dev_dbg (dev, +"Invalid block size %u (expected %zu) at offset %zu\n" , + block->size, block_info->size, block_offset); +return -EINVAL ; + } + + block_offset += block->size; + buffer_size -= block->size; + } + +if (buffer_size) { +dev_dbg (dev,"Unexpected data after the parameters buffer end\n" ); +return -EINVAL ; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (v4l2_isp_params_validate_buffer);include/media/v4l2-isp.h new file +42
-0@@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Video4Linux2 generic ISP parameters and statistics support +* +* Copyright (C) 2025 Ideas On Board Oy +* Author: Jacopo Mondi <[email protected]> +*/ + +#ifndef _V4L2_ISP_H_ +#define _V4L2_ISP_H_ + +#include <linux/media/v4l2-isp.h> + +struct device ; +struct vb2_buffer ; + +/** +* v4l2_isp_params_buffer_size - Calculate size of v4l2_isp_params_buffer +* @max_params_size: The total size of the ISP configuration blocks +* +* Users of the v4l2 extensible parameters will have differing sized data arrays +* depending on their specific parameter buffers. Drivers and userspace will +* need to be able to calculate the appropriate size of the struct to +* accommodate all ISP configuration blocks provided by the platform. +* This macro provides a convenient tool for the calculation. +*/ +#define v4l2_isp_params_buffer_size (max_params_size) \ + (offsetof(struct v4l2_isp_params_buffer, data) + (max_params_size)) + +/** +* v4l2_isp_params_validate_buffer_size - Validate a V4L2 ISP buffer sizes +* @dev: the driver's device pointer +* @vb: the videobuf2 buffer +* @max_size: the maximum allowed buffer size +* +* This function performs validation of the size of a V4L2 ISP parameters buffer +* before the driver can access the actual data buffer content. +* +* After the sizes validation, drivers should copy the buffer content to a +* kernel-only memory area to prevent userspace from modifying it, +* before completing validation using v4l2_isp_params_validate_buffer(). +*
include/media/v4l2-isp.h line 52
Quoted 10 lines
include/media/v4l2-isp.h new file +10
-0+* The @vb buffer as received from the vb2 .buf_prepare() operation is checked +* against @max_size and it's validated to be large enough to accommodate at +* least one ISP configuration block. +*/ +int v4l2_isp_params_validate_buffer_size (struct device *dev, +struct vb2_buffer *vb, +size_t max_size); + +/** +* struct v4l2_isp_params_block_info - V4L2 ISP per-block info
Shouldn't this be v4l2_isp_params_block_type_info? After all, this isn't info about a block but about a block type.
include/media/v4l2-isp.h line 56
Quoted 4 lines
include/media/v4l2-isp.h new file +4
-0+* @size: the block expected size +* +* The v4l2_isp_params_block_info collects information of the ISP configuration +* blocks for validation purposes. It currently only contains the expected
blocks -> block types
include/media/v4l2-isp.h line 59
Quoted 3 lines
include/media/v4l2-isp.h new file +3
-0+* block size. +* +* Drivers shall prepare a list of block info, indexed by block type, one for
a list of block type info
include/media/v4l2-isp.h line 60
Quoted 1 line
include/media/v4l2-isp.h new file +1
-0+* each supported ISP block and correctly populate them with the expected block
block -> block type
include/media/v4l2-isp.h line 72
Quoted 12 lines
include/media/v4l2-isp.h new file +12
-0+* size. +*/ +struct v4l2_isp_params_block_info { +size_t size; +}; + +/** +* v4l2_isp_params_validate_buffer - Validate a V4L2 ISP parameters buffer +* @dev: the driver's device pointer +* @vb: the videobuf2 buffer +* @buffer: the V4L2 ISP parameters buffer +* @info: the list of per-block validation info
@type_info: the array of per-block-type validation info
include/media/v4l2-isp.h line 73
Quoted 1 line
include/media/v4l2-isp.h new file +1
-0+* @num_blocks: the number of blocks
@num_block_types: the number of block types in the type_info array
Probably more places to correct.
It took me some time before I realized that this was about the block types, not about the blocks themselves.
Regards,
Hans
Quoted 19 lines
include/media/v4l2-isp.h new file +18
-0+* +* This function completes the validation of a V4L2 ISP parameters buffer, +* verifying each configuration block correctness before the driver can use +* them to program the hardware. +* +* Drivers should use this function after having validated the correctness of +* the vb2 buffer sizes by using the v4l2_isp_params_validate_buffer_size() +* helper first. Once the buffer size has been validated, drivers should +* perform a copy of the user provided buffer into a kernel-only memory buffer +* to prevent userspace from modifying its content after it has been submitted +* to the driver, and then call this function to complete validation. +*/ +int v4l2_isp_params_validate_buffer (struct device *dev,struct vb2_buffer *vb, +const struct v4l2_isp_params_buffer *buffer, +const struct v4l2_isp_params_block_info *info, +size_t num_blocks); + +#endif /* _V4L2_ISP_H_ */
Jacopo Mondi
maintainerPATCH v9 6/8 media: rkisp1: Use v4l2-isp for validation
In reply to Jacopo MondiConvert rkisp1-params.c to use the helpers defined in v4l2-isp.h to perform validation of a ISP parameters buffer.
--- drivers/media/platform/rockchip/rkisp1/Kconfig | 1 + .../media/platform/rockchip/rkisp1/rkisp1-params.c | 150 ++++++--------------- 2 files changed, 44 insertions(+), 107 deletions(-)
drivers/media/platform/rockchip/rkisp1/Kconfig +1 -0
@@ -10,6 +10,7 @@ config VIDEO_ROCKCHIP_ISP1 select VIDEOBUF2_VMALLOC select V4L2_FWNODE select GENERIC_PHY_MIPI_DPHY + select V4L2_ISP default n help Enable this to support the Image Signal Processing (ISP) module
drivers/media/platform/rockchip/rkisp1/rkisp1-params.c +43 -107
279 lines not on this page · raw message
Signature · 1 line
2.51.0
Jacopo Mondi
maintainerPATCH v9 7/8 media: amlogic-c3: Use v4l2-isp for validation
In reply to Jacopo MondiConvert c3-isp-params.c to use the helpers defined in v4l2-isp.h to perform validation of a ISP parameters buffer.
--- drivers/media/platform/amlogic/c3/isp/Kconfig | 1 + .../media/platform/amlogic/c3/isp/c3-isp-params.c | 165 ++++++--------------- 2 files changed, 44 insertions(+), 122 deletions(-)
drivers/media/platform/amlogic/c3/isp/Kconfig +1 -0
@@ -10,6 +10,7 @@ config VIDEO_C3_ISP select VIDEO_V4L2_SUBDEV_API select VIDEOBUF2_DMA_CONTIG select VIDEOBUF2_VMALLOC + select V4L2_ISP help Video4Linux2 driver for Amlogic C3 ISP pipeline. The C3 ISP is used for processing raw images and
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c +43 -122
216 lines not on this page · raw message
Signature · 1 line
2.51.0
Jacopo Mondi
maintainerPATCH v9 8/8 media: Documentation: kapi: Add v4l2 generic ISP support
In reply to Jacopo MondiAdd to the driver-api documentation the v4l2-isp.h types and helpers documentation.
--- Documentation/driver-api/media/v4l2-core.rst | 1 + Documentation/driver-api/media/v4l2-isp.rst | 49 ++++++++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 51 insertions(+)
Documentation/driver-api/media/v4l2-core.rst +1 -0
@@ -27,3 +27,4 @@ Video4Linux devices v4l2-common v4l2-tveeprom v4l2-jpeg + v4l2-isp
Documentation/driver-api/media/v4l2-isp.rst new file +49 -0
49 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -26859,6 +26859,7 @@ V4L2 GENERIC ISP PARAMETERS AND STATISTIC FORMATS M: Jacopo Mondi <[email protected]> L: [email protected] S: Maintained +F: Documentation/driver-api/media/v4l2-isp.rst F: Documentation/userspace-api/media/v4l/v4l2-isp.rst F: drivers/media/v4l2-core/v4l2-isp.c F: include/media/v4l2-isp.h
Signature · 1 line
2.51.0