Skip to content

Commit 082e656

Browse files
Hans Verkuilpopcornmix
authored andcommitted
v4l2-ctrls: add support for dynamically allocated arrays.
Implement support for dynamically allocated arrays. Most of the changes concern keeping track of the number of elements of the array and the number of elements allocated for the array and reallocating memory if needed. Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> (cherry picked from commit fd5d45e6561f6f8c406b81aeddecaa11f0bd15af)
1 parent c0d1b36 commit 082e656

5 files changed

Lines changed: 272 additions & 71 deletions

File tree

‎drivers/media/v4l2-core/v4l2-ctrls-api.c‎

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,62 +97,75 @@ static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
9797
return ptr_to_user(c, ctrl, ctrl->p_new);
9898
}
9999

100-
/* Helper function: copy the caller-provider value to the given control value */
101-
static int user_to_ptr(struct v4l2_ext_control *c,
102-
struct v4l2_ctrl *ctrl,
103-
union v4l2_ctrl_ptr ptr)
100+
/* Helper function: copy the caller-provider value as the new control value */
101+
static int user_to_new(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
104102
{
105103
int ret;
106104
u32 size;
107105

108-
ctrl->is_new = 1;
106+
ctrl->is_new = 0;
107+
if (ctrl->is_dyn_array &&
108+
c->size > ctrl->p_dyn_alloc_elems * ctrl->elem_size) {
109+
void *old = ctrl->p_dyn;
110+
void *tmp = kvzalloc(2 * c->size, GFP_KERNEL);
111+
112+
if (!tmp)
113+
return -ENOMEM;
114+
memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size);
115+
memcpy(tmp + c->size, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size);
116+
ctrl->p_new.p = tmp;
117+
ctrl->p_cur.p = tmp + c->size;
118+
ctrl->p_dyn = tmp;
119+
ctrl->p_dyn_alloc_elems = c->size / ctrl->elem_size;
120+
kvfree(old);
121+
}
122+
109123
if (ctrl->is_ptr && !ctrl->is_string) {
124+
unsigned int elems = c->size / ctrl->elem_size;
110125
unsigned int idx;
111126

112-
ret = copy_from_user(ptr.p, c->ptr, c->size) ? -EFAULT : 0;
113-
if (ret || !ctrl->is_array)
114-
return ret;
115-
for (idx = c->size / ctrl->elem_size; idx < ctrl->elems; idx++)
116-
ctrl->type_ops->init(ctrl, idx, ptr);
127+
if (copy_from_user(ctrl->p_new.p, c->ptr, c->size))
128+
return -EFAULT;
129+
ctrl->is_new = 1;
130+
if (ctrl->is_dyn_array)
131+
ctrl->new_elems = elems;
132+
else if (ctrl->is_array)
133+
for (idx = elems; idx < ctrl->elems; idx++)
134+
ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
117135
return 0;
118136
}
119137

120138
switch (ctrl->type) {
121139
case V4L2_CTRL_TYPE_INTEGER64:
122-
*ptr.p_s64 = c->value64;
140+
*ctrl->p_new.p_s64 = c->value64;
123141
break;
124142
case V4L2_CTRL_TYPE_STRING:
125143
size = c->size;
126144
if (size == 0)
127145
return -ERANGE;
128146
if (size > ctrl->maximum + 1)
129147
size = ctrl->maximum + 1;
130-
ret = copy_from_user(ptr.p_char, c->string, size) ? -EFAULT : 0;
148+
ret = copy_from_user(ctrl->p_new.p_char, c->string, size) ? -EFAULT : 0;
131149
if (!ret) {
132-
char last = ptr.p_char[size - 1];
150+
char last = ctrl->p_new.p_char[size - 1];
133151

134-
ptr.p_char[size - 1] = 0;
152+
ctrl->p_new.p_char[size - 1] = 0;
135153
/*
136154
* If the string was longer than ctrl->maximum,
137155
* then return an error.
138156
*/
139-
if (strlen(ptr.p_char) == ctrl->maximum && last)
157+
if (strlen(ctrl->p_new.p_char) == ctrl->maximum && last)
140158
return -ERANGE;
141159
}
142160
return ret;
143161
default:
144-
*ptr.p_s32 = c->value;
162+
*ctrl->p_new.p_s32 = c->value;
145163
break;
146164
}
165+
ctrl->is_new = 1;
147166
return 0;
148167
}
149168

150-
/* Helper function: copy the caller-provider value as the new control value */
151-
static int user_to_new(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
152-
{
153-
return user_to_ptr(c, ctrl, ctrl->p_new);
154-
}
155-
156169
/*
157170
* VIDIOC_G/TRY/S_EXT_CTRLS implementation
158171
*/
@@ -254,7 +267,31 @@ static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
254267
have_clusters = true;
255268
if (ctrl->cluster[0] != ctrl)
256269
ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
257-
if (ctrl->is_ptr && !ctrl->is_string) {
270+
if (ctrl->is_dyn_array) {
271+
unsigned int max_size = ctrl->dims[0] * ctrl->elem_size;
272+
unsigned int tot_size = ctrl->elem_size;
273+
274+
if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL)
275+
tot_size *= ref->p_req_elems;
276+
else
277+
tot_size *= ctrl->elems;
278+
279+
c->size = ctrl->elem_size * (c->size / ctrl->elem_size);
280+
if (get) {
281+
if (c->size < tot_size) {
282+
c->size = tot_size;
283+
return -ENOSPC;
284+
}
285+
c->size = tot_size;
286+
} else {
287+
if (c->size > max_size) {
288+
c->size = max_size;
289+
return -ENOSPC;
290+
}
291+
if (!c->size)
292+
return -EFAULT;
293+
}
294+
} else if (ctrl->is_ptr && !ctrl->is_string) {
258295
unsigned int tot_size = ctrl->elems * ctrl->elem_size;
259296

260297
if (c->size < tot_size) {
@@ -346,7 +383,7 @@ static int class_check(struct v4l2_ctrl_handler *hdl, u32 which)
346383
*
347384
* Note that v4l2_g_ext_ctrls_common() with 'which' set to
348385
* V4L2_CTRL_WHICH_REQUEST_VAL is only called if the request was
349-
* completed, and in that case valid_p_req is true for all controls.
386+
* completed, and in that case p_req_valid is true for all controls.
350387
*/
351388
int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl,
352389
struct v4l2_ext_controls *cs,
@@ -430,7 +467,9 @@ int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl,
430467

431468
if (is_default)
432469
ret = def_to_user(cs->controls + idx, ref->ctrl);
433-
else if (is_request && ref->valid_p_req)
470+
else if (is_request && ref->p_req_dyn_enomem)
471+
ret = -ENOMEM;
472+
else if (is_request && ref->p_req_valid)
434473
ret = req_to_user(cs->controls + idx, ref);
435474
else if (is_volatile)
436475
ret = new_to_user(cs->controls + idx, ref->ctrl);
@@ -457,6 +496,17 @@ int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
457496
}
458497
EXPORT_SYMBOL(v4l2_g_ext_ctrls);
459498

499+
/* Validate a new control */
500+
static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
501+
{
502+
unsigned int idx;
503+
int err = 0;
504+
505+
for (idx = 0; !err && idx < ctrl->new_elems; idx++)
506+
err = ctrl->type_ops->validate(ctrl, idx, p_new);
507+
return err;
508+
}
509+
460510
/* Validate controls. */
461511
static int validate_ctrls(struct v4l2_ext_controls *cs,
462512
struct v4l2_ctrl_helper *helpers,
@@ -872,6 +922,9 @@ int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
872922
/* It's a driver bug if this happens. */
873923
if (WARN_ON(ctrl->type != type))
874924
return -EINVAL;
925+
/* Setting dynamic arrays is not (yet?) supported. */
926+
if (WARN_ON(ctrl->is_dyn_array))
927+
return -EINVAL;
875928
memcpy(ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size);
876929
return set_ctrl(NULL, ctrl, 0);
877930
}

0 commit comments

Comments
 (0)