Skip to contentSkip to conversation

media: uvcvideo: Use a count variable for meta_formats

PATCH New Hans de Goede 3 participants 5 messages Jul 8, 2025 – Jul 11, 2025

Subsystems (2)
lore
Share & tools
lore

Hans de Goede

author maintainer
#0 lore raw

cover media: uvcvideo: Use a count variable for meta_formats

Hi,

I still was not entirely happy with how the meta_formats handling code looked, especially not with uvc_meta_v4l2_enum_formats().

So here is a small patch / attempt to clean it up a bit.

Ricardo, assuming you're ok with the suggested changes can you give this a test please ?

My main UVC test cam only supports standard UVC metadata which is not helpful for testing (1), I did give this patch a spin with that camera but that does not say much.

Regards,

Hans

1) It is a microsoft HD camera (but quite old) so I wonder if maybe it does have the metadata but not the extended control for enabling it. How would I test this ?

Hans de Goede (1):
media: uvcvideo: Use a count variable for meta_formats instead of 0
terminating

 drivers/media/usb/uvc/uvc_metadata.c | 21 +++++++++------------
 drivers/media/usb/uvc/uvcvideo.h     |  4 ++--
 2 files changed, 11 insertions(+), 14 deletions(-)
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 1/1 media: uvcvideo: Use a count variable for meta_formats instead of 0 terminating

The code dealing with the 0 terminated meta_formats array is a bit klunky especially for the uvc_meta_v4l2_enum_formats() case.

Instead of 0 terminating add an unsigned int nmeta_formats member to struct uvc_device and use that. This leads to slightly cleaner code.

---
 drivers/media/usb/uvc/uvc_metadata.c | 21 +++++++++------------
 drivers/media/usb/uvc/uvcvideo.h     |  4 ++--
 2 files changed, 11 insertions(+), 14 deletions(-)
drivers/media/usb/uvc/uvc_metadata.c +9 -12
index 12972527ab8d..229e08ff323e 100644
@@ -64,17 +64,16 @@ static int uvc_meta_v4l2_try_format(struct file *file, void *fh,
 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
 	struct uvc_device *dev = stream->dev;
 	struct v4l2_meta_format *fmt = &format->fmt.meta;
-	u32 fmeta = fmt->dataformat;
-	u32 i;
+	u32 fmeta = V4L2_META_FMT_UVC;
 
 	if (format->type != vfh->vdev->queue->type)
 		return -EINVAL;
 
-	for (i = 0; (fmeta != dev->meta_formats[i]) && dev->meta_formats[i];
-	     i++)
-		;
-	if (!dev->meta_formats[i])
-		fmeta = V4L2_META_FMT_UVC;
+	for (unsigned int i = 0; i < dev->nmeta_formats; i++)
+		if (dev->meta_formats[i] == fmt->dataformat) {
+			fmeta = fmt->dataformat;
+			break;
+		}
 
 	memset(fmt, 0, sizeof(*fmt));
 
@@ -119,14 +118,12 @@ static int uvc_meta_v4l2_enum_formats(struct file *file, void *fh,
 	struct v4l2_fh *vfh = file->private_data;
 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
 	struct uvc_device *dev = stream->dev;
-	u32 i;
+	u32 i = fdesc->index;
 
 	if (fdesc->type != vfh->vdev->queue->type)
 		return -EINVAL;
 
-	for (i = 0; (i < fdesc->index) && dev->meta_formats[i]; i++)
-		;
-	if (!dev->meta_formats[i])
+	if (i >= dev->nmeta_formats)
 		return -EINVAL;
 
 	memset(fdesc, 0, sizeof(*fdesc));
@@ -265,7 +262,7 @@ int uvc_meta_init(struct uvc_device *dev)
 		dev->meta_formats[i++] = V4L2_META_FMT_UVC_MSXU_1_5;
 
 	 /* IMPORTANT: for new meta-formats update UVC_MAX_META_DATA_FORMATS. */
-	dev->meta_formats[i++] = 0;
+	dev->nmeta_formats = i;
 
 	return 0;
 }
drivers/media/usb/uvc/uvcvideo.h +2 -2
index b34c1914ff39..757254fc4fe9 100644
@@ -588,8 +588,8 @@ struct uvc_device {
 
 	const struct uvc_device_info *info;
 
-	/* Zero-ended list of meta formats */
-	u32 meta_formats[UVC_MAX_META_DATA_FORMATS + 1];
+	u32 meta_formats[UVC_MAX_META_DATA_FORMATS];
+	unsigned int nmeta_formats;
 
 	atomic_t nmappings;
 
Signature · 1 line
2.49.0
Reviewed-by Tested-by on v1 1/1
#2 lore raw

Re: [PATCH] media: uvcvideo: Use a count variable for meta_formats instead of 0 terminating

Hi Hans

Indeed it looks nicer, thanks :)

Quoted 7 lines · Hans de Goede

The code dealing with the 0 terminated meta_formats array is a bit klunky especially for the uvc_meta_v4l2_enum_formats() case.

Instead of 0 terminating add an unsigned int nmeta_formats member to struct uvc_device and use that. This leads to slightly cleaner code.

MSXU_CONTROL_METADATA

Quoted 16 lines
---
 drivers/media/usb/uvc/uvc_metadata.c | 21 +++++++++------------
 drivers/media/usb/uvc/uvcvideo.h     |  4 ++--
 2 files changed, 11 insertions(+), 14 deletions(-)
drivers/media/usb/uvc/uvc_metadata.c +0 -2
index 12972527ab8d..229e08ff323e 100644
@@ -64,17 +64,16 @@ static int uvc_meta_v4l2_try_format(struct file *file, void *fh,
        struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
        struct uvc_device *dev = stream->dev;
        struct v4l2_meta_format *fmt = &format->fmt.meta;
-       u32 fmeta = fmt->dataformat;
-       u32 i;

drivers/media/usb/uvc/uvc_metadata.c static int uvc_meta_v4l2_try_format(struct file *file, void *fh, · line 77

Quoted 10 lines
drivers/media/usb/uvc/uvc_metadata.c +1 -5
+       u32 fmeta = V4L2_META_FMT_UVC;
 
        if (format->type != vfh->vdev->queue->type)
                return -EINVAL;
 
-       for (i = 0; (fmeta != dev->meta_formats[i]) && dev->meta_formats[i];
-            i++)
-               ;
-       if (!dev->meta_formats[i])
-               fmeta = V4L2_META_FMT_UVC;

Super nitpick:
I'd have placed this here: and not at the beginning of the function, to make clear my intentions.

fmeta = V4L2_META_FMT_UVC;

Quoted 52 lines
drivers/media/usb/uvc/uvc_metadata.c +8 -5
+       for (unsigned int i = 0; i < dev->nmeta_formats; i++)
+               if (dev->meta_formats[i] == fmt->dataformat) {
+                       fmeta = fmt->dataformat;
+                       break;
+               }
 
        memset(fmt, 0, sizeof(*fmt));
 
@@ -119,14 +118,12 @@ static int uvc_meta_v4l2_enum_formats(struct file *file, void *fh,
        struct v4l2_fh *vfh = file->private_data;
        struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
        struct uvc_device *dev = stream->dev;
-       u32 i;
+       u32 i = fdesc->index;
 
        if (fdesc->type != vfh->vdev->queue->type)
                return -EINVAL;
 
-       for (i = 0; (i < fdesc->index) && dev->meta_formats[i]; i++)
-               ;
-       if (!dev->meta_formats[i])
+       if (i >= dev->nmeta_formats)
                return -EINVAL;
 
        memset(fdesc, 0, sizeof(*fdesc));
@@ -265,7 +262,7 @@ int uvc_meta_init(struct uvc_device *dev)
                dev->meta_formats[i++] = V4L2_META_FMT_UVC_MSXU_1_5;
 
         /* IMPORTANT: for new meta-formats update UVC_MAX_META_DATA_FORMATS. */
-       dev->meta_formats[i++] = 0;
+       dev->nmeta_formats = i;
 
        return 0;
 }
drivers/media/usb/uvc/uvcvideo.h +2 -3
index b34c1914ff39..757254fc4fe9 100644
@@ -588,8 +588,8 @@ struct uvc_device {
 
        const struct uvc_device_info *info;
 
-       /* Zero-ended list of meta formats */
-       u32 meta_formats[UVC_MAX_META_DATA_FORMATS + 1];
+       u32 meta_formats[UVC_MAX_META_DATA_FORMATS];
+       unsigned int nmeta_formats;
 
        atomic_t nmappings;
 
--

2.49.0

Signature · 1 line
Ricardo Ribalda
author maintainer
#3 lore raw

Re: [PATCH] media: uvcvideo: Use a count variable for meta_formats instead of 0 terminating

Hi Ricardo,

Quoted 6 lines · Ricardo Ribalda

Hi Hans

Indeed it looks nicer, thanks :)

Quoted 1 line · Hans de Goede
Quoted 8 lines
Quoted 6 lines

The code dealing with the 0 terminated meta_formats array is a bit klunky especially for the uvc_meta_v4l2_enum_formats() case.

Instead of 0 terminating add an unsigned int nmeta_formats member to struct uvc_device and use that. This leads to slightly cleaner code.

Thank you. I've pushed this to uvc/for-next now.

Regards,

Hans

maintainer
#4 lore raw

Re: [PATCH] media: uvcvideo: Use a count variable for meta_formats instead of 0 terminating

In reply to Hans de Goede

Hi Hans,

A few comments here. I see you've included this in your pull request today, so I'll send patch on top. No need for any action on your side (except perhaps reviewing the patches :-)).

Quoted 27 lines · Hans de Goede

The code dealing with the 0 terminated meta_formats array is a bit klunky especially for the uvc_meta_v4l2_enum_formats() case.

Instead of 0 terminating add an unsigned int nmeta_formats member to struct uvc_device and use that. This leads to slightly cleaner code.

---
 drivers/media/usb/uvc/uvc_metadata.c | 21 +++++++++------------
 drivers/media/usb/uvc/uvcvideo.h     |  4 ++--
 2 files changed, 11 insertions(+), 14 deletions(-)
drivers/media/usb/uvc/uvc_metadata.c +1 -2
index 12972527ab8d..229e08ff323e 100644
@@ -64,17 +64,16 @@ static int uvc_meta_v4l2_try_format(struct file *file, void *fh,
 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
 	struct uvc_device *dev = stream->dev;
 	struct v4l2_meta_format *fmt = &format->fmt.meta;
-	u32 fmeta = fmt->dataformat;
-	u32 i;
+	u32 fmeta = V4L2_META_FMT_UVC;
 
 	if (format->type != vfh->vdev->queue->type)
 		return -EINVAL;
 

drivers/media/usb/uvc/uvc_metadata.c static int uvc_meta_v4l2_try_format(struct file *file, void *fh, · line 76

Quoted 10 lines
drivers/media/usb/uvc/uvc_metadata.c +5 -5
-	for (i = 0; (fmeta != dev->meta_formats[i]) && dev->meta_formats[i];
-	     i++)
-		;
-	if (!dev->meta_formats[i])
-		fmeta = V4L2_META_FMT_UVC;
+	for (unsigned int i = 0; i < dev->nmeta_formats; i++)
+		if (dev->meta_formats[i] == fmt->dataformat) {
+			fmeta = fmt->dataformat;
+			break;
+		}

Missing curcly braces for the for loop.

drivers/media/usb/uvc/uvc_metadata.c static int uvc_meta_v4l2_try_format(struct file *file, void *fh, · line 78

Quoted 2 lines
drivers/media/usb/uvc/uvc_metadata.c +0 -0
 	memset(fmt, 0, sizeof(*fmt));

We can actually drop the memset, as the V4L2 ioctl core already zeroes fmt->fmt.

Quoted 8 lines
drivers/media/usb/uvc/uvc_metadata.c +1 -1
@@ -119,14 +118,12 @@ static int uvc_meta_v4l2_enum_formats(struct file *file, void *fh,
 	struct v4l2_fh *vfh = file->private_data;
 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
 	struct uvc_device *dev = stream->dev;
-	u32 i;
+	u32 i = fdesc->index;
 

drivers/media/usb/uvc/uvc_metadata.c static int uvc_meta_v4l2_enum_formats(struct file *file, void *fh, · line 129

Quoted 10 lines
drivers/media/usb/uvc/uvc_metadata.c +1 -3
 	if (fdesc->type != vfh->vdev->queue->type)
 		return -EINVAL;
 
-	for (i = 0; (i < fdesc->index) && dev->meta_formats[i]; i++)
-		;
-	if (!dev->meta_formats[i])
+	if (i >= dev->nmeta_formats)
 		return -EINVAL;
 
 	memset(fdesc, 0, sizeof(*fdesc));

Same here, we can drop the memset, which will lead to simplifications in the function.

Quoted 24 lines
drivers/media/usb/uvc/uvc_metadata.c +1 -1
@@ -265,7 +262,7 @@ int uvc_meta_init(struct uvc_device *dev)
 		dev->meta_formats[i++] = V4L2_META_FMT_UVC_MSXU_1_5;
 
 	 /* IMPORTANT: for new meta-formats update UVC_MAX_META_DATA_FORMATS. */
-	dev->meta_formats[i++] = 0;
+	dev->nmeta_formats = i;
 
 	return 0;
 }
drivers/media/usb/uvc/uvcvideo.h +2 -2
index b34c1914ff39..757254fc4fe9 100644
@@ -588,8 +588,8 @@ struct uvc_device {
 
 	const struct uvc_device_info *info;
 
-	/* Zero-ended list of meta formats */
-	u32 meta_formats[UVC_MAX_META_DATA_FORMATS + 1];
+	u32 meta_formats[UVC_MAX_META_DATA_FORMATS];
+	unsigned int nmeta_formats;
 
 	atomic_t nmappings;
 
Signature · 3 lines
Regards,

Laurent Pinchart