Skip to content

Commit 998e765

Browse files
Hans Verkuilmchehab
authored andcommitted
[media] v4l2-ctrls: prepare for array support
Add core support for N-dimensional arrays. Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
1 parent 20d88ee commit 998e765

2 files changed

Lines changed: 44 additions & 22 deletions

File tree

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

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
11611161
v4l2_event_queue_fh(sev->fh, &ev);
11621162
}
11631163

1164-
static bool std_equal(const struct v4l2_ctrl *ctrl,
1164+
static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx,
11651165
union v4l2_ctrl_ptr ptr1,
11661166
union v4l2_ctrl_ptr ptr2)
11671167
{
@@ -1180,7 +1180,7 @@ static bool std_equal(const struct v4l2_ctrl *ctrl,
11801180
}
11811181
}
11821182

1183-
static void std_init(const struct v4l2_ctrl *ctrl,
1183+
static void std_init(const struct v4l2_ctrl *ctrl, u32 idx,
11841184
union v4l2_ctrl_ptr ptr)
11851185
{
11861186
switch (ctrl->type) {
@@ -1207,6 +1207,14 @@ static void std_log(const struct v4l2_ctrl *ctrl)
12071207
{
12081208
union v4l2_ctrl_ptr ptr = ctrl->p_cur;
12091209

1210+
if (ctrl->is_array) {
1211+
unsigned i;
1212+
1213+
for (i = 0; i < ctrl->nr_of_dims; i++)
1214+
pr_cont("[%u]", ctrl->dims[i]);
1215+
pr_cont(" ");
1216+
}
1217+
12101218
switch (ctrl->type) {
12111219
case V4L2_CTRL_TYPE_INTEGER:
12121220
pr_cont("%d", *ptr.p_s32);
@@ -1249,7 +1257,7 @@ static void std_log(const struct v4l2_ctrl *ctrl)
12491257
})
12501258

12511259
/* Validate a new control */
1252-
static int std_validate(const struct v4l2_ctrl *ctrl,
1260+
static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx,
12531261
union v4l2_ctrl_ptr ptr)
12541262
{
12551263
size_t len;
@@ -1473,8 +1481,8 @@ static int cluster_changed(struct v4l2_ctrl *master)
14731481

14741482
if (ctrl == NULL)
14751483
continue;
1476-
ctrl->has_changed = !ctrl->type_ops->equal(ctrl,
1477-
ctrl->p_cur, ctrl->p_new);
1484+
ctrl->has_changed = !ctrl->type_ops->equal(ctrl, 0,
1485+
ctrl->p_cur, ctrl->p_new);
14781486
changed |= ctrl->has_changed;
14791487
}
14801488
return changed;
@@ -1531,15 +1539,15 @@ static int validate_new(const struct v4l2_ctrl *ctrl,
15311539
case V4L2_CTRL_TYPE_BUTTON:
15321540
case V4L2_CTRL_TYPE_CTRL_CLASS:
15331541
ptr.p_s32 = &c->value;
1534-
return ctrl->type_ops->validate(ctrl, ptr);
1542+
return ctrl->type_ops->validate(ctrl, 0, ptr);
15351543

15361544
case V4L2_CTRL_TYPE_INTEGER64:
15371545
ptr.p_s64 = &c->value64;
1538-
return ctrl->type_ops->validate(ctrl, ptr);
1546+
return ctrl->type_ops->validate(ctrl, 0, ptr);
15391547

15401548
default:
15411549
ptr.p = c->ptr;
1542-
return ctrl->type_ops->validate(ctrl, ptr);
1550+
return ctrl->type_ops->validate(ctrl, 0, ptr);
15431551
}
15441552
}
15451553

@@ -1767,6 +1775,8 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
17671775
unsigned sz_extra;
17681776
unsigned nr_of_dims = 0;
17691777
unsigned elems = 1;
1778+
bool is_array;
1779+
unsigned tot_ctrl_size;
17701780
void *data;
17711781
int err;
17721782

@@ -1779,17 +1789,19 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
17791789
if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
17801790
break;
17811791
}
1792+
is_array = nr_of_dims > 0;
17821793

17831794
if (type == V4L2_CTRL_TYPE_INTEGER64)
17841795
elem_size = sizeof(s64);
17851796
else if (type == V4L2_CTRL_TYPE_STRING)
17861797
elem_size = max + 1;
17871798
else if (type < V4L2_CTRL_COMPOUND_TYPES)
17881799
elem_size = sizeof(s32);
1800+
tot_ctrl_size = elem_size * elems;
17891801

17901802
/* Sanity checks */
1791-
if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
1792-
elem_size == 0 ||
1803+
if (id == 0 || name == NULL || !elem_size ||
1804+
id >= V4L2_CID_PRIVATE_BASE ||
17931805
(type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
17941806
(type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
17951807
handler_set_err(hdl, -ERANGE);
@@ -1804,6 +1816,12 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
18041816
handler_set_err(hdl, -ERANGE);
18051817
return NULL;
18061818
}
1819+
if (is_array &&
1820+
(type == V4L2_CTRL_TYPE_BUTTON ||
1821+
type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
1822+
handler_set_err(hdl, -EINVAL);
1823+
return NULL;
1824+
}
18071825

18081826
sz_extra = 0;
18091827
if (type == V4L2_CTRL_TYPE_BUTTON)
@@ -1812,8 +1830,9 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
18121830
flags |= V4L2_CTRL_FLAG_READ_ONLY;
18131831
else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
18141832
type == V4L2_CTRL_TYPE_STRING ||
1815-
type >= V4L2_CTRL_COMPOUND_TYPES)
1816-
sz_extra += 2 * elem_size;
1833+
type >= V4L2_CTRL_COMPOUND_TYPES ||
1834+
is_array)
1835+
sz_extra += 2 * tot_ctrl_size;
18171836

18181837
ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
18191838
if (ctrl == NULL) {
@@ -1834,9 +1853,10 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
18341853
ctrl->maximum = max;
18351854
ctrl->step = step;
18361855
ctrl->default_value = def;
1837-
ctrl->is_string = type == V4L2_CTRL_TYPE_STRING;
1838-
ctrl->is_ptr = type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
1856+
ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
1857+
ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
18391858
ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
1859+
ctrl->is_array = is_array;
18401860
ctrl->elems = elems;
18411861
ctrl->nr_of_dims = nr_of_dims;
18421862
if (nr_of_dims)
@@ -1852,13 +1872,13 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
18521872

18531873
if (!ctrl->is_int) {
18541874
ctrl->p_new.p = data;
1855-
ctrl->p_cur.p = data + elem_size;
1875+
ctrl->p_cur.p = data + tot_ctrl_size;
18561876
} else {
18571877
ctrl->p_new.p = &ctrl->val;
18581878
ctrl->p_cur.p = &ctrl->cur.val;
18591879
}
1860-
ctrl->type_ops->init(ctrl, ctrl->p_cur);
1861-
ctrl->type_ops->init(ctrl, ctrl->p_new);
1880+
ctrl->type_ops->init(ctrl, 0, ctrl->p_cur);
1881+
ctrl->type_ops->init(ctrl, 0, ctrl->p_new);
18621882

18631883
if (handler_new_ref(hdl, ctrl)) {
18641884
kfree(ctrl);
@@ -2764,7 +2784,7 @@ s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
27642784
struct v4l2_ext_control c;
27652785

27662786
/* It's a driver bug if this happens. */
2767-
WARN_ON(ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
2787+
WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
27682788
c.value = 0;
27692789
get_ctrl(ctrl, &c);
27702790
return c.value;
@@ -3074,7 +3094,7 @@ int v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
30743094
struct v4l2_ext_control c;
30753095

30763096
/* It's a driver bug if this happens. */
3077-
WARN_ON(ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
3097+
WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
30783098
c.value64 = val;
30793099
return set_ctrl_lock(NULL, ctrl, &c);
30803100
}

‎include/media/v4l2-ctrls.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ struct v4l2_ctrl_ops {
7474
* @validate: validate the value. Return 0 on success and a negative value otherwise.
7575
*/
7676
struct v4l2_ctrl_type_ops {
77-
bool (*equal)(const struct v4l2_ctrl *ctrl,
77+
bool (*equal)(const struct v4l2_ctrl *ctrl, u32 idx,
7878
union v4l2_ctrl_ptr ptr1,
7979
union v4l2_ctrl_ptr ptr2);
80-
void (*init)(const struct v4l2_ctrl *ctrl,
80+
void (*init)(const struct v4l2_ctrl *ctrl, u32 idx,
8181
union v4l2_ctrl_ptr ptr);
8282
void (*log)(const struct v4l2_ctrl *ctrl);
83-
int (*validate)(const struct v4l2_ctrl *ctrl,
83+
int (*validate)(const struct v4l2_ctrl *ctrl, u32 idx,
8484
union v4l2_ctrl_ptr ptr);
8585
};
8686

@@ -111,6 +111,7 @@ typedef void (*v4l2_ctrl_notify_fnc)(struct v4l2_ctrl *ctrl, void *priv);
111111
* @is_ptr: If set, then this control is an array and/or has type >= V4L2_CTRL_COMPOUND_TYPES
112112
* and/or has type V4L2_CTRL_TYPE_STRING. In other words, struct
113113
* v4l2_ext_control uses field p to point to the data.
114+
* @is_array: If set, then this control contains an N-dimensional array.
114115
* @has_volatiles: If set, then one or more members of the cluster are volatile.
115116
* Drivers should never touch this flag.
116117
* @call_notify: If set, then call the handler's notify function whenever the
@@ -169,6 +170,7 @@ struct v4l2_ctrl {
169170
unsigned int is_int:1;
170171
unsigned int is_string:1;
171172
unsigned int is_ptr:1;
173+
unsigned int is_array:1;
172174
unsigned int has_volatiles:1;
173175
unsigned int call_notify:1;
174176
unsigned int manual_mode_value:8;

0 commit comments

Comments
 (0)