diff mbox series

[v3,1/2] media: v4l2-subdev: Provide const-aware subdev state accessors

Message ID 20240507161907.27747-2-laurent.pinchart+renesas@ideasonboard.com (mailing list archive)
State Superseded
Delegated to: Kieran Bingham
Headers show
Series media: v4l2-subdev: Support const-awareness in state accessors | expand

Commit Message

Laurent Pinchart May 7, 2024, 4:19 p.m. UTC
It would be useful to mark instances of v4l2_subdev_state structures as
const when code needs to access them read-only. This isn't currently
possible, as the v4l2_subdev_state_get_*() accessor functions take a
non-const pointer to the state.

Use _Generic() to provide two different versions of the accessors, for
const and non-const states respectively. The former returns a const
pointer to the requested format, rectangle or interval, implementing
const-correctness. The latter returns a non-const pointer, preserving
the current behaviour for drivers.

Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
---
Changes since v2:

- Specify types explicitly in _Generic() expression
- Fix cast of value to const pointer

Changes since v1:

- Wrap the accessors with _Generic() using a single macro instead of
  adding a _Generic() statement in each of them.
---
 include/media/v4l2-subdev.h | 40 +++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 13 deletions(-)

Comments

Sakari Ailus May 8, 2024, 6:44 p.m. UTC | #1
Hi Laurent,

Thanks for the update.

This is starting to look very nice. A few comments below...

On Tue, May 07, 2024 at 07:19:06PM +0300, Laurent Pinchart wrote:
> It would be useful to mark instances of v4l2_subdev_state structures as
> const when code needs to access them read-only. This isn't currently
> possible, as the v4l2_subdev_state_get_*() accessor functions take a
> non-const pointer to the state.
> 
> Use _Generic() to provide two different versions of the accessors, for
> const and non-const states respectively. The former returns a const
> pointer to the requested format, rectangle or interval, implementing
> const-correctness. The latter returns a non-const pointer, preserving
> the current behaviour for drivers.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
> Changes since v2:
> 
> - Specify types explicitly in _Generic() expression
> - Fix cast of value to const pointer
> 
> Changes since v1:
> 
> - Wrap the accessors with _Generic() using a single macro instead of
>   adding a _Generic() statement in each of them.
> ---
>  include/media/v4l2-subdev.h | 40 +++++++++++++++++++++++++------------
>  1 file changed, 27 insertions(+), 13 deletions(-)
> 
> diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
> index e30c463d90e5..cd3e9e2ebe4d 100644
> --- a/include/media/v4l2-subdev.h
> +++ b/include/media/v4l2-subdev.h
> @@ -1326,6 +1326,16 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
>  #define __v4l2_subdev_state_gen_call(NAME, _1, ARG, ...)	\
>  	__v4l2_subdev_state_get_ ## NAME ## ARG
>  
> +/*
> + * A macro to constify the return value of the state accessors when the state
> + * parameter is const.
> + */
> +#define __v4l2_subdev_state_constify_call(state, value)				\

This function just changes the return value constness. How about calling it
e.g. __v4l2_subdev_state_constify_ret instead?

Either way,

Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>

> +	_Generic(state,								\
> +		const struct v4l2_subdev_state *: (const typeof(*(value)) *)(value), \
> +		struct v4l2_subdev_state *: (value)				\
> +	)
> +
>  /**
>   * v4l2_subdev_state_get_format() - Get pointer to a stream format
>   * @state: subdevice state
> @@ -1340,16 +1350,17 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
>   */
>  /*
>   * Wrap v4l2_subdev_state_get_format(), allowing the function to be called with
> - * two or three arguments. The purpose of the __v4l2_subdev_state_get_format()
> + * two or three arguments. The purpose of the __v4l2_subdev_state_gen_call()
>   * macro below is to come up with the name of the function or macro to call,
>   * using the last two arguments (_stream and _pad). The selected function or
>   * macro is then called using the arguments specified by the caller. A similar
>   * arrangement is used for v4l2_subdev_state_crop() and
>   * v4l2_subdev_state_compose() below.

These function names are missing _get after the prefix.

Could you also add the interval function here, too?

The would fit well for a separate patch.

>   */
> -#define v4l2_subdev_state_get_format(state, pad, ...)			\
> -	__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
> -		(state, pad, ##__VA_ARGS__)
> +#define v4l2_subdev_state_get_format(state, pad, ...)				\
> +	__v4l2_subdev_state_constify_call(state,				\
> +		__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>  #define __v4l2_subdev_state_get_format_pad(state, pad)	\
>  	__v4l2_subdev_state_get_format(state, pad, 0)
>  struct v4l2_mbus_framefmt *
> @@ -1368,9 +1379,10 @@ __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
>   * For stream-unaware drivers the crop rectangle for the corresponding pad is
>   * returned. If the pad does not exist, NULL is returned.
>   */
> -#define v4l2_subdev_state_get_crop(state, pad, ...)			\
> -	__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
> -		(state, pad, ##__VA_ARGS__)
> +#define v4l2_subdev_state_get_crop(state, pad, ...)				\
> +	__v4l2_subdev_state_constify_call(state,				\
> +		__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>  #define __v4l2_subdev_state_get_crop_pad(state, pad)	\
>  	__v4l2_subdev_state_get_crop(state, pad, 0)
>  struct v4l2_rect *
> @@ -1389,9 +1401,10 @@ __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
>   * For stream-unaware drivers the compose rectangle for the corresponding pad is
>   * returned. If the pad does not exist, NULL is returned.
>   */
> -#define v4l2_subdev_state_get_compose(state, pad, ...)			\
> -	__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
> -		(state, pad, ##__VA_ARGS__)
> +#define v4l2_subdev_state_get_compose(state, pad, ...)				\
> +	__v4l2_subdev_state_constify_call(state,				\
> +		__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>  #define __v4l2_subdev_state_get_compose_pad(state, pad)	\
>  	__v4l2_subdev_state_get_compose(state, pad, 0)
>  struct v4l2_rect *
> @@ -1410,9 +1423,10 @@ __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
>   * For stream-unaware drivers the frame interval for the corresponding pad is
>   * returned. If the pad does not exist, NULL is returned.
>   */
> -#define v4l2_subdev_state_get_interval(state, pad, ...)			\
> -	__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
> -		(state, pad, ##__VA_ARGS__)
> +#define v4l2_subdev_state_get_interval(state, pad, ...)				\
> +	__v4l2_subdev_state_constify_call(state,				\
> +		__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>  #define __v4l2_subdev_state_get_interval_pad(state, pad)	\
>  	__v4l2_subdev_state_get_interval(state, pad, 0)
>  struct v4l2_fract *
Tomi Valkeinen May 8, 2024, 6:48 p.m. UTC | #2
On 08/05/2024 21:44, Sakari Ailus wrote:
> Hi Laurent,
> 
> Thanks for the update.
> 
> This is starting to look very nice. A few comments below...
> 
> On Tue, May 07, 2024 at 07:19:06PM +0300, Laurent Pinchart wrote:
>> It would be useful to mark instances of v4l2_subdev_state structures as
>> const when code needs to access them read-only. This isn't currently
>> possible, as the v4l2_subdev_state_get_*() accessor functions take a
>> non-const pointer to the state.
>>
>> Use _Generic() to provide two different versions of the accessors, for
>> const and non-const states respectively. The former returns a const
>> pointer to the requested format, rectangle or interval, implementing
>> const-correctness. The latter returns a non-const pointer, preserving
>> the current behaviour for drivers.
>>
>> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
>> ---
>> Changes since v2:
>>
>> - Specify types explicitly in _Generic() expression
>> - Fix cast of value to const pointer
>>
>> Changes since v1:
>>
>> - Wrap the accessors with _Generic() using a single macro instead of
>>    adding a _Generic() statement in each of them.
>> ---
>>   include/media/v4l2-subdev.h | 40 +++++++++++++++++++++++++------------
>>   1 file changed, 27 insertions(+), 13 deletions(-)
>>
>> diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
>> index e30c463d90e5..cd3e9e2ebe4d 100644
>> --- a/include/media/v4l2-subdev.h
>> +++ b/include/media/v4l2-subdev.h
>> @@ -1326,6 +1326,16 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
>>   #define __v4l2_subdev_state_gen_call(NAME, _1, ARG, ...)	\
>>   	__v4l2_subdev_state_get_ ## NAME ## ARG
>>   
>> +/*
>> + * A macro to constify the return value of the state accessors when the state
>> + * parameter is const.
>> + */
>> +#define __v4l2_subdev_state_constify_call(state, value)				\
> 
> This function just changes the return value constness. How about calling it
> e.g. __v4l2_subdev_state_constify_ret instead?
> 
> Either way,
> 
> Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>

I tested this today on my branches, but apparently forgot to send the 
R-b. So here's mine also:

Reviewed-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>

  Tomi

>> +	_Generic(state,								\
>> +		const struct v4l2_subdev_state *: (const typeof(*(value)) *)(value), \
>> +		struct v4l2_subdev_state *: (value)				\
>> +	)
>> +
>>   /**
>>    * v4l2_subdev_state_get_format() - Get pointer to a stream format
>>    * @state: subdevice state
>> @@ -1340,16 +1350,17 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
>>    */
>>   /*
>>    * Wrap v4l2_subdev_state_get_format(), allowing the function to be called with
>> - * two or three arguments. The purpose of the __v4l2_subdev_state_get_format()
>> + * two or three arguments. The purpose of the __v4l2_subdev_state_gen_call()
>>    * macro below is to come up with the name of the function or macro to call,
>>    * using the last two arguments (_stream and _pad). The selected function or
>>    * macro is then called using the arguments specified by the caller. A similar
>>    * arrangement is used for v4l2_subdev_state_crop() and
>>    * v4l2_subdev_state_compose() below.
> 
> These function names are missing _get after the prefix.
> 
> Could you also add the interval function here, too?
> 
> The would fit well for a separate patch.
> 
>>    */
>> -#define v4l2_subdev_state_get_format(state, pad, ...)			\
>> -	__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
>> -		(state, pad, ##__VA_ARGS__)
>> +#define v4l2_subdev_state_get_format(state, pad, ...)				\
>> +	__v4l2_subdev_state_constify_call(state,				\
>> +		__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
>> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>>   #define __v4l2_subdev_state_get_format_pad(state, pad)	\
>>   	__v4l2_subdev_state_get_format(state, pad, 0)
>>   struct v4l2_mbus_framefmt *
>> @@ -1368,9 +1379,10 @@ __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
>>    * For stream-unaware drivers the crop rectangle for the corresponding pad is
>>    * returned. If the pad does not exist, NULL is returned.
>>    */
>> -#define v4l2_subdev_state_get_crop(state, pad, ...)			\
>> -	__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
>> -		(state, pad, ##__VA_ARGS__)
>> +#define v4l2_subdev_state_get_crop(state, pad, ...)				\
>> +	__v4l2_subdev_state_constify_call(state,				\
>> +		__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
>> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>>   #define __v4l2_subdev_state_get_crop_pad(state, pad)	\
>>   	__v4l2_subdev_state_get_crop(state, pad, 0)
>>   struct v4l2_rect *
>> @@ -1389,9 +1401,10 @@ __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
>>    * For stream-unaware drivers the compose rectangle for the corresponding pad is
>>    * returned. If the pad does not exist, NULL is returned.
>>    */
>> -#define v4l2_subdev_state_get_compose(state, pad, ...)			\
>> -	__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
>> -		(state, pad, ##__VA_ARGS__)
>> +#define v4l2_subdev_state_get_compose(state, pad, ...)				\
>> +	__v4l2_subdev_state_constify_call(state,				\
>> +		__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
>> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>>   #define __v4l2_subdev_state_get_compose_pad(state, pad)	\
>>   	__v4l2_subdev_state_get_compose(state, pad, 0)
>>   struct v4l2_rect *
>> @@ -1410,9 +1423,10 @@ __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
>>    * For stream-unaware drivers the frame interval for the corresponding pad is
>>    * returned. If the pad does not exist, NULL is returned.
>>    */
>> -#define v4l2_subdev_state_get_interval(state, pad, ...)			\
>> -	__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
>> -		(state, pad, ##__VA_ARGS__)
>> +#define v4l2_subdev_state_get_interval(state, pad, ...)				\
>> +	__v4l2_subdev_state_constify_call(state,				\
>> +		__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
>> +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
>>   #define __v4l2_subdev_state_get_interval_pad(state, pad)	\
>>   	__v4l2_subdev_state_get_interval(state, pad, 0)
>>   struct v4l2_fract *
>
Laurent Pinchart May 8, 2024, 9:02 p.m. UTC | #3
On Wed, May 08, 2024 at 06:44:40PM +0000, Sakari Ailus wrote:
> Hi Laurent,
> 
> Thanks for the update.
> 
> This is starting to look very nice. A few comments below...

Thanks :-)

> On Tue, May 07, 2024 at 07:19:06PM +0300, Laurent Pinchart wrote:
> > It would be useful to mark instances of v4l2_subdev_state structures as
> > const when code needs to access them read-only. This isn't currently
> > possible, as the v4l2_subdev_state_get_*() accessor functions take a
> > non-const pointer to the state.
> > 
> > Use _Generic() to provide two different versions of the accessors, for
> > const and non-const states respectively. The former returns a const
> > pointer to the requested format, rectangle or interval, implementing
> > const-correctness. The latter returns a non-const pointer, preserving
> > the current behaviour for drivers.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > ---
> > Changes since v2:
> > 
> > - Specify types explicitly in _Generic() expression
> > - Fix cast of value to const pointer
> > 
> > Changes since v1:
> > 
> > - Wrap the accessors with _Generic() using a single macro instead of
> >   adding a _Generic() statement in each of them.
> > ---
> >  include/media/v4l2-subdev.h | 40 +++++++++++++++++++++++++------------
> >  1 file changed, 27 insertions(+), 13 deletions(-)
> > 
> > diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
> > index e30c463d90e5..cd3e9e2ebe4d 100644
> > --- a/include/media/v4l2-subdev.h
> > +++ b/include/media/v4l2-subdev.h
> > @@ -1326,6 +1326,16 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
> >  #define __v4l2_subdev_state_gen_call(NAME, _1, ARG, ...)	\
> >  	__v4l2_subdev_state_get_ ## NAME ## ARG
> >  
> > +/*
> > + * A macro to constify the return value of the state accessors when the state
> > + * parameter is const.
> > + */
> > +#define __v4l2_subdev_state_constify_call(state, value)				\
> 
> This function just changes the return value constness. How about calling it
> e.g. __v4l2_subdev_state_constify_ret instead?

Fine with me.

> Either way,
> 
> Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> 
> > +	_Generic(state,								\
> > +		const struct v4l2_subdev_state *: (const typeof(*(value)) *)(value), \
> > +		struct v4l2_subdev_state *: (value)				\
> > +	)
> > +
> >  /**
> >   * v4l2_subdev_state_get_format() - Get pointer to a stream format
> >   * @state: subdevice state
> > @@ -1340,16 +1350,17 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
> >   */
> >  /*
> >   * Wrap v4l2_subdev_state_get_format(), allowing the function to be called with
> > - * two or three arguments. The purpose of the __v4l2_subdev_state_get_format()
> > + * two or three arguments. The purpose of the __v4l2_subdev_state_gen_call()
> >   * macro below is to come up with the name of the function or macro to call,
> >   * using the last two arguments (_stream and _pad). The selected function or
> >   * macro is then called using the arguments specified by the caller. A similar
> >   * arrangement is used for v4l2_subdev_state_crop() and
> >   * v4l2_subdev_state_compose() below.
> 
> These function names are missing _get after the prefix.
> 
> Could you also add the interval function here, too?
> 
> The would fit well for a separate patch.

I'll drop the change above and will fix the whole paragraph in a
separate patch.

> >   */
> > -#define v4l2_subdev_state_get_format(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_format(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >  #define __v4l2_subdev_state_get_format_pad(state, pad)	\
> >  	__v4l2_subdev_state_get_format(state, pad, 0)
> >  struct v4l2_mbus_framefmt *
> > @@ -1368,9 +1379,10 @@ __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
> >   * For stream-unaware drivers the crop rectangle for the corresponding pad is
> >   * returned. If the pad does not exist, NULL is returned.
> >   */
> > -#define v4l2_subdev_state_get_crop(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_crop(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >  #define __v4l2_subdev_state_get_crop_pad(state, pad)	\
> >  	__v4l2_subdev_state_get_crop(state, pad, 0)
> >  struct v4l2_rect *
> > @@ -1389,9 +1401,10 @@ __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
> >   * For stream-unaware drivers the compose rectangle for the corresponding pad is
> >   * returned. If the pad does not exist, NULL is returned.
> >   */
> > -#define v4l2_subdev_state_get_compose(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_compose(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >  #define __v4l2_subdev_state_get_compose_pad(state, pad)	\
> >  	__v4l2_subdev_state_get_compose(state, pad, 0)
> >  struct v4l2_rect *
> > @@ -1410,9 +1423,10 @@ __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
> >   * For stream-unaware drivers the frame interval for the corresponding pad is
> >   * returned. If the pad does not exist, NULL is returned.
> >   */
> > -#define v4l2_subdev_state_get_interval(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_interval(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >  #define __v4l2_subdev_state_get_interval_pad(state, pad)	\
> >  	__v4l2_subdev_state_get_interval(state, pad, 0)
> >  struct v4l2_fract *
diff mbox series

Patch

diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
index e30c463d90e5..cd3e9e2ebe4d 100644
--- a/include/media/v4l2-subdev.h
+++ b/include/media/v4l2-subdev.h
@@ -1326,6 +1326,16 @@  void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
 #define __v4l2_subdev_state_gen_call(NAME, _1, ARG, ...)	\
 	__v4l2_subdev_state_get_ ## NAME ## ARG
 
+/*
+ * A macro to constify the return value of the state accessors when the state
+ * parameter is const.
+ */
+#define __v4l2_subdev_state_constify_call(state, value)				\
+	_Generic(state,								\
+		const struct v4l2_subdev_state *: (const typeof(*(value)) *)(value), \
+		struct v4l2_subdev_state *: (value)				\
+	)
+
 /**
  * v4l2_subdev_state_get_format() - Get pointer to a stream format
  * @state: subdevice state
@@ -1340,16 +1350,17 @@  void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
  */
 /*
  * Wrap v4l2_subdev_state_get_format(), allowing the function to be called with
- * two or three arguments. The purpose of the __v4l2_subdev_state_get_format()
+ * two or three arguments. The purpose of the __v4l2_subdev_state_gen_call()
  * macro below is to come up with the name of the function or macro to call,
  * using the last two arguments (_stream and _pad). The selected function or
  * macro is then called using the arguments specified by the caller. A similar
  * arrangement is used for v4l2_subdev_state_crop() and
  * v4l2_subdev_state_compose() below.
  */
-#define v4l2_subdev_state_get_format(state, pad, ...)			\
-	__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
-		(state, pad, ##__VA_ARGS__)
+#define v4l2_subdev_state_get_format(state, pad, ...)				\
+	__v4l2_subdev_state_constify_call(state,				\
+		__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
+			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
 #define __v4l2_subdev_state_get_format_pad(state, pad)	\
 	__v4l2_subdev_state_get_format(state, pad, 0)
 struct v4l2_mbus_framefmt *
@@ -1368,9 +1379,10 @@  __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
  * For stream-unaware drivers the crop rectangle for the corresponding pad is
  * returned. If the pad does not exist, NULL is returned.
  */
-#define v4l2_subdev_state_get_crop(state, pad, ...)			\
-	__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
-		(state, pad, ##__VA_ARGS__)
+#define v4l2_subdev_state_get_crop(state, pad, ...)				\
+	__v4l2_subdev_state_constify_call(state,				\
+		__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
+			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
 #define __v4l2_subdev_state_get_crop_pad(state, pad)	\
 	__v4l2_subdev_state_get_crop(state, pad, 0)
 struct v4l2_rect *
@@ -1389,9 +1401,10 @@  __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
  * For stream-unaware drivers the compose rectangle for the corresponding pad is
  * returned. If the pad does not exist, NULL is returned.
  */
-#define v4l2_subdev_state_get_compose(state, pad, ...)			\
-	__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
-		(state, pad, ##__VA_ARGS__)
+#define v4l2_subdev_state_get_compose(state, pad, ...)				\
+	__v4l2_subdev_state_constify_call(state,				\
+		__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
+			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
 #define __v4l2_subdev_state_get_compose_pad(state, pad)	\
 	__v4l2_subdev_state_get_compose(state, pad, 0)
 struct v4l2_rect *
@@ -1410,9 +1423,10 @@  __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
  * For stream-unaware drivers the frame interval for the corresponding pad is
  * returned. If the pad does not exist, NULL is returned.
  */
-#define v4l2_subdev_state_get_interval(state, pad, ...)			\
-	__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
-		(state, pad, ##__VA_ARGS__)
+#define v4l2_subdev_state_get_interval(state, pad, ...)				\
+	__v4l2_subdev_state_constify_call(state,				\
+		__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
+			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
 #define __v4l2_subdev_state_get_interval_pad(state, pad)	\
 	__v4l2_subdev_state_get_interval(state, pad, 0)
 struct v4l2_fract *