Skip to content

Commit 8837a47

Browse files
committed
drivers: video: sw_generator: refactor fmt/frmival selection
This addresses a minor bug where the format where all fields are 0 could be passed to video_set_fmt() by mistake and be accepted unchanged by video_sw_generator_set_fmt(), which would match against fmts[] terminator. This also makes use of IN_RANGE() and applies it to the function video_sw_generator_enum_frmival(). Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent 5ab18e6 commit 8837a47

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

drivers/video/video_sw_generator.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <zephyr/drivers/video.h>
1111
#include <zephyr/drivers/video-controls.h>
1212
#include <zephyr/logging/log.h>
13+
#include <zephyr/sys/util.h>
14+
#include <zephyr/sys/byteorder.h>
1315

1416
LOG_MODULE_REGISTER(video_sw_generator, CONFIG_VIDEO_LOG_LEVEL);
1517

@@ -64,21 +66,21 @@ static int video_sw_generator_set_fmt(const struct device *dev, enum video_endpo
6466
struct video_format *fmt)
6567
{
6668
struct video_sw_generator_data *data = dev->data;
67-
int i = 0;
69+
int i;
6870

6971
if (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL) {
7072
return -EINVAL;
7173
}
7274

73-
for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
74-
if (fmt->pixelformat == fmts[i].pixelformat && fmt->width >= fmts[i].width_min &&
75-
fmt->width <= fmts[i].width_max && fmt->height >= fmts[i].height_min &&
76-
fmt->height <= fmts[i].height_max) {
75+
for (i = 0; fmts[i].pixelformat != 0; ++i) {
76+
if (fmt->pixelformat == fmts[i].pixelformat &&
77+
IN_RANGE(fmt->width, fmts[i].width_min, fmts[i].width_max) &&
78+
IN_RANGE(fmt->height, fmts[i].height_min, fmts[i].height_max)) {
7779
break;
7880
}
7981
}
8082

81-
if (i == ARRAY_SIZE(fmts)) {
83+
if (fmts[i].pixelformat == 0) {
8284
LOG_ERR("Unsupported pixel format or resolution");
8385
return -ENOTSUP;
8486
}
@@ -313,18 +315,20 @@ static int video_sw_generator_enum_frmival(const struct device *dev, enum video_
313315
{
314316
int i = 0;
315317

316-
if (ep != VIDEO_EP_OUT || fie->index) {
318+
if (ep != VIDEO_EP_OUT || fie->index > 0) {
317319
return -EINVAL;
318320
}
319321

320-
while (fmts[i].pixelformat && (fmts[i].pixelformat != fie->format->pixelformat)) {
321-
i++;
322+
for (i = 0; fmts[i].pixelformat != 0; ++i) {
323+
if (fie->format->pixelformat == fmts[i].pixelformat &&
324+
IN_RANGE(fie->format->width, fmts[i].width_min, fmts[i].width_max) &&
325+
IN_RANGE(fie->format->height, fmts[i].height_min, fmts[i].height_max)) {
326+
break;
327+
}
322328
}
323329

324-
if ((i == ARRAY_SIZE(fmts)) || (fie->format->width > fmts[i].width_max) ||
325-
(fie->format->width < fmts[i].width_min) ||
326-
(fie->format->height > fmts[i].height_max) ||
327-
(fie->format->height < fmts[i].height_min)) {
330+
if (fmts[i].pixelformat == 0) {
331+
LOG_ERR("Nothing matching the requested format was found");
328332
return -EINVAL;
329333
}
330334

0 commit comments

Comments
 (0)