Re: [PATCH 3/8] media/v4l2-core: struct struct v4l2_ext_controls param which

From: Hans Verkuil
Date: Fri Aug 21 2015 - 06:12:09 EST


On 08/21/2015 11:29 AM, Ricardo Ribalda Delgado wrote:
> Support for new field which on v4l2_ext_controls, used to get the
> default value of one or more controls.
>
> Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@xxxxxxxxx>
> ---
> drivers/media/v4l2-core/v4l2-ctrls.c | 35 ++++++++++++++++++++++++++++++-----
> 1 file changed, 30 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> index b6b7dcc1b77d..23a69f637f6d 100644
> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> @@ -1489,6 +1489,17 @@ static int new_to_user(struct v4l2_ext_control *c,
> return ptr_to_user(c, ctrl, ctrl->p_new);
> }
>
> +/* Helper function: copy the initial control value back to the caller */
> +static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
> +{
> + int idx;
> +
> + for (idx = 0; idx < ctrl->elems; idx++)
> + ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
> +
> + return ptr_to_user(c, ctrl, ctrl->p_new);
> +}
> +
> /* Helper function: copy the caller-provider value to the given control value */
> static int user_to_ptr(struct v4l2_ext_control *c,
> struct v4l2_ctrl *ctrl,
> @@ -2708,7 +2719,9 @@ static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
>
> cs->error_idx = i;
>
> - if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
> + if (cs->ctrl_class &&
> + cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
> + V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
> return -EINVAL;
>
> /* Old-style private controls are not allowed for
> @@ -2787,7 +2800,7 @@ static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
> whether there are any controls at all. */
> static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
> {
> - if (ctrl_class == 0)
> + if (ctrl_class == 0 || ctrl_class == V4L2_CTRL_WHICH_DEF_VAL)
> return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
> return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
> }
> @@ -2801,10 +2814,14 @@ int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs
> struct v4l2_ctrl_helper *helpers = helper;
> int ret;
> int i, j;
> + bool def_value = false;
>
> cs->error_idx = cs->count;
> cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
>
> + if (cs->which == V4L2_CTRL_WHICH_DEF_VAL)
> + def_value = true;
> +

Ah, this is confusing. First assigning to ctrl_class, then checking 'which'.

I would add a patch after patch 2/8 that replaces all occurrences of ctrl_class in
by 'which'. It's only used in v4l2-core, the saa7164 driver and in Documentation.
The old 'ctrl_class' shouldn't be used in the kernel anymore.

It is probably a good idea to put #ifndef __KERNEL__ around the ctrl_class field in
the header. That way it isn't visible in the kernel at all.

I would also rename V4L2_CTRL_ID2CLASS to V4L2_CTRL_ID2WHICH (and keep the old define
as #define V4L2_CTRL_ID2CLASS V4L2_CTRL_ID2WHICH under #ifndef __KERNEL__).

> if (hdl == NULL)
> return -EINVAL;
>
> @@ -2827,9 +2844,11 @@ int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs
>
> for (i = 0; !ret && i < cs->count; i++) {
> int (*ctrl_to_user)(struct v4l2_ext_control *c,
> - struct v4l2_ctrl *ctrl) = cur_to_user;
> + struct v4l2_ctrl *ctrl);
> struct v4l2_ctrl *master;
>
> + ctrl_to_user = def_value ? def_to_user : cur_to_user;
> +
> if (helpers[i].mref == NULL)
> continue;
>
> @@ -2839,8 +2858,9 @@ int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs
> v4l2_ctrl_lock(master);
>
> /* g_volatile_ctrl will update the new control values */
> - if ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
> - (master->has_volatiles && !is_cur_manual(master))) {
> + if (!def_value &&
> + ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
> + (master->has_volatiles && !is_cur_manual(master)))) {
> for (j = 0; j < master->ncontrols; j++)
> cur_to_new(master->cluster[j]);
> ret = call_op(master, g_volatile_ctrl);
> @@ -3062,6 +3082,11 @@ static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
> int ret;
>
> cs->error_idx = cs->count;
> +
> + /* Default value cannot be changed */
> + if (cs->which == V4L2_CTRL_WHICH_DEF_VAL)
> + return -EINVAL;
> +
> cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
>
> if (hdl == NULL)
>

Regards,

Hans
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/