[v2,4/7] media: uvcvideo: Refactor uvc_ctrl_mappings_uvcXX

Message ID 20230105-uvc-gcc5-v2-4-2ba6c660d6f5@chromium.org
State New
Headers
Series media: uvcvideo: Hot fixes for top of next/uvc + std menus |

Commit Message

Ricardo Ribalda Jan. 5, 2023, 1:52 p.m. UTC
  Convert the array of structs into an array of pointers, that way the
mappings can be reused.

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/usb/uvc/uvc_ctrl.c | 81 +++++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 42 deletions(-)
  

Comments

Laurent Pinchart Jan. 6, 2023, 8:45 p.m. UTC | #1
Hi Ricardo,

Thank you for the patch.

On Thu, Jan 05, 2023 at 02:52:55PM +0100, Ricardo Ribalda wrote:
> Convert the array of structs into an array of pointers, that way the
> mappings can be reused.
> 
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
>  drivers/media/usb/uvc/uvc_ctrl.c | 81 +++++++++++++++++++---------------------
>  1 file changed, 39 insertions(+), 42 deletions(-)
> 
> diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
> index 2ebe5cc18ad9..9af64f7a23d3 100644
> --- a/drivers/media/usb/uvc/uvc_ctrl.c
> +++ b/drivers/media/usb/uvc/uvc_ctrl.c
> @@ -723,34 +723,40 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
>  	},
>  };
>  
> -static const struct uvc_control_mapping uvc_ctrl_mappings_uvc11[] = {
> -	{
> -		.id		= V4L2_CID_POWER_LINE_FREQUENCY,
> -		.entity		= UVC_GUID_UVC_PROCESSING,
> -		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
> -		.size		= 2,
> -		.offset		= 0,
> -		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
> -		.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
> -		.menu_info	= power_line_frequency_controls,
> -		.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
> -					  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
> -	},
> +static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
> +	.id		= V4L2_CID_POWER_LINE_FREQUENCY,
> +	.entity		= UVC_GUID_UVC_PROCESSING,
> +	.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
> +	.size		= 2,
> +	.offset		= 0,
> +	.v4l2_type	= V4L2_CTRL_TYPE_MENU,
> +	.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
> +	.menu_info	= power_line_frequency_controls,
> +	.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
> +				  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
>  };
>  
> -static const struct uvc_control_mapping uvc_ctrl_mappings_uvc15[] = {
> -	{
> -		.id		= V4L2_CID_POWER_LINE_FREQUENCY,
> -		.entity		= UVC_GUID_UVC_PROCESSING,
> -		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
> -		.size		= 2,
> -		.offset		= 0,
> -		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
> -		.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
> -		.menu_info	= power_line_frequency_controls,
> -		.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
> -					  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
> -	},
> +static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc11[] = {
> +	&uvc_ctrl_power_line_mapping_uvc11,
> +	NULL, /* Sentinel */
> +};
> +
> +static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
> +	.id		= V4L2_CID_POWER_LINE_FREQUENCY,
> +	.entity		= UVC_GUID_UVC_PROCESSING,
> +	.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
> +	.size		= 2,
> +	.offset		= 0,
> +	.v4l2_type	= V4L2_CTRL_TYPE_MENU,
> +	.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
> +	.menu_info	= power_line_frequency_controls,
> +	.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
> +				  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
> +};
> +
> +static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc15[] = {
> +	&uvc_ctrl_power_line_mapping_uvc15,
> +	NULL, /* Sentinel */
>  };
>  
>  /* ------------------------------------------------------------------------
> @@ -2506,8 +2512,7 @@ static void uvc_ctrl_prune_entity(struct uvc_device *dev,
>  static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
>  			       struct uvc_control *ctrl)
>  {
> -	const struct uvc_control_mapping *mappings;
> -	unsigned int num_mappings;
> +	const struct uvc_control_mapping **mappings;
>  	unsigned int i;
>  
>  	/*
> @@ -2574,21 +2579,13 @@ static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
>  	}
>  
>  	/* Finally process version-specific mappings. */
> -	if (chain->dev->uvc_version < 0x0150) {
> -		mappings = uvc_ctrl_mappings_uvc11;
> -		num_mappings = ARRAY_SIZE(uvc_ctrl_mappings_uvc11);
> -	} else {
> -		mappings = uvc_ctrl_mappings_uvc15;
> -		num_mappings = ARRAY_SIZE(uvc_ctrl_mappings_uvc15);
> -	}
> -
> -	for (i = 0; i < num_mappings; ++i) {
> -		const struct uvc_control_mapping *mapping = &mappings[i];
> +	mappings = (chain->dev->uvc_version < 0x0150) ? uvc_ctrl_mappings_uvc11

No need for parentheses.

> +						      : uvc_ctrl_mappings_uvc15;
>  
> -		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
> -		    ctrl->info.selector == mapping->selector)
> -			__uvc_ctrl_add_mapping(chain, ctrl, mapping);
> -	}
> +	for (i = 0; mappings[i]; ++i)

Missing curly braces.

Let's add a local mapping variable:

	for (i = 0; mappings[i]; ++i) {
		const struct uvc_control_mapping *mapping = &mappings[i];

		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
		    ctrl->info.selector == mapping->selector)
			__uvc_ctrl_add_mapping(chain, ctrl, mapping);
	}

I'll address this when applying.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +		if (uvc_entity_match_guid(ctrl->entity, mappings[i]->entity) &&
> +		    ctrl->info.selector == mappings[i]->selector)
> +			__uvc_ctrl_add_mapping(chain, ctrl, mappings[i]);
>  }
>  
>  /*
>
  

Patch

diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index 2ebe5cc18ad9..9af64f7a23d3 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -723,34 +723,40 @@  static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
 	},
 };
 
-static const struct uvc_control_mapping uvc_ctrl_mappings_uvc11[] = {
-	{
-		.id		= V4L2_CID_POWER_LINE_FREQUENCY,
-		.entity		= UVC_GUID_UVC_PROCESSING,
-		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
-		.size		= 2,
-		.offset		= 0,
-		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
-		.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
-		.menu_info	= power_line_frequency_controls,
-		.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
-					  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
-	},
+static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
+	.id		= V4L2_CID_POWER_LINE_FREQUENCY,
+	.entity		= UVC_GUID_UVC_PROCESSING,
+	.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
+	.size		= 2,
+	.offset		= 0,
+	.v4l2_type	= V4L2_CTRL_TYPE_MENU,
+	.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
+	.menu_info	= power_line_frequency_controls,
+	.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
+				  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
 };
 
-static const struct uvc_control_mapping uvc_ctrl_mappings_uvc15[] = {
-	{
-		.id		= V4L2_CID_POWER_LINE_FREQUENCY,
-		.entity		= UVC_GUID_UVC_PROCESSING,
-		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
-		.size		= 2,
-		.offset		= 0,
-		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
-		.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
-		.menu_info	= power_line_frequency_controls,
-		.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
-					  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
-	},
+static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc11[] = {
+	&uvc_ctrl_power_line_mapping_uvc11,
+	NULL, /* Sentinel */
+};
+
+static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
+	.id		= V4L2_CID_POWER_LINE_FREQUENCY,
+	.entity		= UVC_GUID_UVC_PROCESSING,
+	.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
+	.size		= 2,
+	.offset		= 0,
+	.v4l2_type	= V4L2_CTRL_TYPE_MENU,
+	.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
+	.menu_info	= power_line_frequency_controls,
+	.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
+				  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
+};
+
+static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc15[] = {
+	&uvc_ctrl_power_line_mapping_uvc15,
+	NULL, /* Sentinel */
 };
 
 /* ------------------------------------------------------------------------
@@ -2506,8 +2512,7 @@  static void uvc_ctrl_prune_entity(struct uvc_device *dev,
 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
 			       struct uvc_control *ctrl)
 {
-	const struct uvc_control_mapping *mappings;
-	unsigned int num_mappings;
+	const struct uvc_control_mapping **mappings;
 	unsigned int i;
 
 	/*
@@ -2574,21 +2579,13 @@  static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
 	}
 
 	/* Finally process version-specific mappings. */
-	if (chain->dev->uvc_version < 0x0150) {
-		mappings = uvc_ctrl_mappings_uvc11;
-		num_mappings = ARRAY_SIZE(uvc_ctrl_mappings_uvc11);
-	} else {
-		mappings = uvc_ctrl_mappings_uvc15;
-		num_mappings = ARRAY_SIZE(uvc_ctrl_mappings_uvc15);
-	}
-
-	for (i = 0; i < num_mappings; ++i) {
-		const struct uvc_control_mapping *mapping = &mappings[i];
+	mappings = (chain->dev->uvc_version < 0x0150) ? uvc_ctrl_mappings_uvc11
+						      : uvc_ctrl_mappings_uvc15;
 
-		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
-		    ctrl->info.selector == mapping->selector)
-			__uvc_ctrl_add_mapping(chain, ctrl, mapping);
-	}
+	for (i = 0; mappings[i]; ++i)
+		if (uvc_entity_match_guid(ctrl->entity, mappings[i]->entity) &&
+		    ctrl->info.selector == mappings[i]->selector)
+			__uvc_ctrl_add_mapping(chain, ctrl, mappings[i]);
 }
 
 /*