Commit 861f92cb authored by Ricardo Ribalda's avatar Ricardo Ribalda Committed by Mauro Carvalho Chehab

media: v4l2-ioctl: Fix check_ext_ctrls

Drivers that do not use the ctrl-framework use this function instead.

Fix the following issues:

- Do not check for multiple classes when getting the DEF_VAL.
- Return -EINVAL for request_api calls
- Default value cannot be changed, return EINVAL as soon as possible.
- Return the right error_idx
[If an error is found when validating the list of controls passed with
VIDIOC_G_EXT_CTRLS, then error_idx shall be set to ctrls->count to
indicate to userspace that no actual hardware was touched.
It would have been much nicer of course if error_idx could point to the
control index that failed the validation, but sadly that's not how the
API was designed.]

Fixes v4l2-compliance:
Control ioctls (Input 0):
        warn: v4l2-test-controls.cpp(834): error_idx should be equal to count
        warn: v4l2-test-controls.cpp(855): error_idx should be equal to count
		fail: v4l2-test-controls.cpp(813): doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls)
	test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL
Buffer ioctls (Input 0):
		fail: v4l2-test-buffers.cpp(1994): ret != EINVAL && ret != EBADR && ret != ENOTTY
	test Requests: FAIL

Cc: stable@vger.kernel.org
Fixes: 6fa6f831 ("media: v4l2-ctrls: add core request support")
Suggested-by: default avatarHans Verkuil <hverkuil-cisco@xs4all.nl>
Reviewed-by: default avatarHans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: default avatarRicardo Ribalda <ribalda@chromium.org>
Signed-off-by: default avatarLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+huawei@kernel.org>
parent ae0334e0
...@@ -869,7 +869,7 @@ static void v4l_print_default(const void *arg, bool write_only) ...@@ -869,7 +869,7 @@ static void v4l_print_default(const void *arg, bool write_only)
pr_cont("driver-specific ioctl\n"); pr_cont("driver-specific ioctl\n");
} }
static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv) static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl)
{ {
__u32 i; __u32 i;
...@@ -878,23 +878,41 @@ static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv) ...@@ -878,23 +878,41 @@ static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
for (i = 0; i < c->count; i++) for (i = 0; i < c->count; i++)
c->controls[i].reserved2[0] = 0; c->controls[i].reserved2[0] = 0;
/* V4L2_CID_PRIVATE_BASE cannot be used as control class switch (c->which) {
when using extended controls. case V4L2_CID_PRIVATE_BASE:
Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL /*
is it allowed for backwards compatibility. * V4L2_CID_PRIVATE_BASE cannot be used as control class
* when using extended controls.
* Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
* is it allowed for backwards compatibility.
*/ */
if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE) if (ioctl == VIDIOC_G_CTRL || ioctl == VIDIOC_S_CTRL)
return 0; return false;
if (!c->which) break;
return 1; case V4L2_CTRL_WHICH_DEF_VAL:
/* Default value cannot be changed */
if (ioctl == VIDIOC_S_EXT_CTRLS ||
ioctl == VIDIOC_TRY_EXT_CTRLS) {
c->error_idx = c->count;
return false;
}
return true;
case V4L2_CTRL_WHICH_CUR_VAL:
return true;
case V4L2_CTRL_WHICH_REQUEST_VAL:
c->error_idx = c->count;
return false;
}
/* Check that all controls are from the same control class. */ /* Check that all controls are from the same control class. */
for (i = 0; i < c->count; i++) { for (i = 0; i < c->count; i++) {
if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) { if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
c->error_idx = i; c->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i :
return 0; c->count;
return false;
} }
} }
return 1; return true;
} }
static int check_fmt(struct file *file, enum v4l2_buf_type type) static int check_fmt(struct file *file, enum v4l2_buf_type type)
...@@ -2189,7 +2207,7 @@ static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops, ...@@ -2189,7 +2207,7 @@ static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
ctrls.controls = &ctrl; ctrls.controls = &ctrl;
ctrl.id = p->id; ctrl.id = p->id;
ctrl.value = p->value; ctrl.value = p->value;
if (check_ext_ctrls(&ctrls, 1)) { if (check_ext_ctrls(&ctrls, VIDIOC_G_CTRL)) {
int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls); int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
if (ret == 0) if (ret == 0)
...@@ -2223,7 +2241,7 @@ static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops, ...@@ -2223,7 +2241,7 @@ static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
ctrls.controls = &ctrl; ctrls.controls = &ctrl;
ctrl.id = p->id; ctrl.id = p->id;
ctrl.value = p->value; ctrl.value = p->value;
if (check_ext_ctrls(&ctrls, 1)) if (check_ext_ctrls(&ctrls, VIDIOC_S_CTRL))
return ops->vidioc_s_ext_ctrls(file, fh, &ctrls); return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
return -EINVAL; return -EINVAL;
} }
...@@ -2245,8 +2263,8 @@ static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops, ...@@ -2245,8 +2263,8 @@ static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
vfd, vfd->v4l2_dev->mdev, p); vfd, vfd->v4l2_dev->mdev, p);
if (ops->vidioc_g_ext_ctrls == NULL) if (ops->vidioc_g_ext_ctrls == NULL)
return -ENOTTY; return -ENOTTY;
return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) : return check_ext_ctrls(p, VIDIOC_G_EXT_CTRLS) ?
-EINVAL; ops->vidioc_g_ext_ctrls(file, fh, p) : -EINVAL;
} }
static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops, static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
...@@ -2266,8 +2284,8 @@ static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops, ...@@ -2266,8 +2284,8 @@ static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
vfd, vfd->v4l2_dev->mdev, p); vfd, vfd->v4l2_dev->mdev, p);
if (ops->vidioc_s_ext_ctrls == NULL) if (ops->vidioc_s_ext_ctrls == NULL)
return -ENOTTY; return -ENOTTY;
return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) : return check_ext_ctrls(p, VIDIOC_S_EXT_CTRLS) ?
-EINVAL; ops->vidioc_s_ext_ctrls(file, fh, p) : -EINVAL;
} }
static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops, static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
...@@ -2287,8 +2305,8 @@ static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops, ...@@ -2287,8 +2305,8 @@ static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
vfd, vfd->v4l2_dev->mdev, p); vfd, vfd->v4l2_dev->mdev, p);
if (ops->vidioc_try_ext_ctrls == NULL) if (ops->vidioc_try_ext_ctrls == NULL)
return -ENOTTY; return -ENOTTY;
return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) : return check_ext_ctrls(p, VIDIOC_TRY_EXT_CTRLS) ?
-EINVAL; ops->vidioc_try_ext_ctrls(file, fh, p) : -EINVAL;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment