From c56c437b140ed9c9e91d82f4ca705cdb021a9343 Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Tue, 8 Jul 2025 12:46:22 +0200 Subject: [PATCH] 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. Signed-off-by: Hans de Goede Reviewed-by: Ricardo Ribalda Tested-by: Ricardo Ribalda # Camera with MSXU_CONTROL_METADATA Link: https://lore.kernel.org/r/20250708104622.73237-2-hansg@kernel.org Signed-off-by: Hans Verkuil --- drivers/media/usb/uvc/uvc_metadata.c | 21 +++++++++------------ drivers/media/usb/uvc/uvcvideo.h | 4 ++-- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_metadata.c b/drivers/media/usb/uvc/uvc_metadata.c index 12972527ab8d5..229e08ff323ee 100644 --- a/drivers/media/usb/uvc/uvc_metadata.c +++ b/drivers/media/usb/uvc/uvc_metadata.c @@ -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; } diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index b34c1914ff394..757254fc4fe93 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -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.47.2