1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * V4L2 controls framework core implementation.
8 #include <linux/export.h>
10 #include <linux/slab.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-event.h>
13 #include <media/v4l2-fwnode.h>
15 #include "v4l2-ctrls-priv.h"
17 static const union v4l2_ctrl_ptr ptr_null;
19 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl,
22 memset(ev, 0, sizeof(*ev));
23 ev->type = V4L2_EVENT_CTRL;
25 ev->u.ctrl.changes = changes;
26 ev->u.ctrl.type = ctrl->type;
27 ev->u.ctrl.flags = user_flags(ctrl);
29 ev->u.ctrl.value64 = 0;
31 ev->u.ctrl.value64 = *ctrl->p_cur.p_s64;
32 ev->u.ctrl.minimum = ctrl->minimum;
33 ev->u.ctrl.maximum = ctrl->maximum;
34 if (ctrl->type == V4L2_CTRL_TYPE_MENU
35 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
38 ev->u.ctrl.step = ctrl->step;
39 ev->u.ctrl.default_value = ctrl->default_value;
42 void send_initial_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl)
45 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
47 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
48 changes |= V4L2_EVENT_CTRL_CH_VALUE;
49 fill_event(&ev, ctrl, changes);
50 v4l2_event_queue_fh(fh, &ev);
53 void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
56 struct v4l2_subscribed_event *sev;
58 if (list_empty(&ctrl->ev_subs))
60 fill_event(&ev, ctrl, changes);
62 list_for_each_entry(sev, &ctrl->ev_subs, node)
64 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
65 v4l2_event_queue_fh(sev->fh, &ev);
68 bool v4l2_ctrl_type_op_equal(const struct v4l2_ctrl *ctrl,
69 union v4l2_ctrl_ptr ptr1, union v4l2_ctrl_ptr ptr2)
74 case V4L2_CTRL_TYPE_BUTTON:
76 case V4L2_CTRL_TYPE_STRING:
77 for (i = 0; i < ctrl->elems; i++) {
78 unsigned int idx = i * ctrl->elem_size;
80 /* strings are always 0-terminated */
81 if (strcmp(ptr1.p_char + idx, ptr2.p_char + idx))
86 return !memcmp(ptr1.p_const, ptr2.p_const,
87 ctrl->elems * ctrl->elem_size);
90 EXPORT_SYMBOL(v4l2_ctrl_type_op_equal);
92 /* Default intra MPEG-2 quantisation coefficients, from the specification. */
93 static const u8 mpeg2_intra_quant_matrix[64] = {
94 8, 16, 16, 19, 16, 19, 22, 22,
95 22, 22, 22, 22, 26, 24, 26, 27,
96 27, 27, 26, 26, 26, 26, 27, 27,
97 27, 29, 29, 29, 34, 34, 34, 29,
98 29, 29, 27, 27, 29, 29, 32, 32,
99 34, 34, 37, 38, 37, 35, 35, 34,
100 35, 38, 38, 40, 40, 40, 48, 48,
101 46, 46, 56, 56, 58, 69, 69, 83
104 static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx,
105 union v4l2_ctrl_ptr ptr)
107 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence;
108 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture;
109 struct v4l2_ctrl_mpeg2_quantisation *p_mpeg2_quant;
110 struct v4l2_ctrl_vp8_frame *p_vp8_frame;
111 struct v4l2_ctrl_vp9_frame *p_vp9_frame;
112 struct v4l2_ctrl_fwht_params *p_fwht_params;
113 struct v4l2_ctrl_h264_scaling_matrix *p_h264_scaling_matrix;
114 struct v4l2_ctrl_av1_sequence *p_av1_sequence;
115 void *p = ptr.p + idx * ctrl->elem_size;
117 if (ctrl->p_def.p_const)
118 memcpy(p, ctrl->p_def.p_const, ctrl->elem_size);
120 memset(p, 0, ctrl->elem_size);
122 switch ((u32)ctrl->type) {
123 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
124 p_mpeg2_sequence = p;
127 p_mpeg2_sequence->chroma_format = 1;
129 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
132 /* interlaced top field */
133 p_mpeg2_picture->picture_structure = V4L2_MPEG2_PIC_TOP_FIELD;
134 p_mpeg2_picture->picture_coding_type =
135 V4L2_MPEG2_PIC_CODING_TYPE_I;
137 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
140 memcpy(p_mpeg2_quant->intra_quantiser_matrix,
141 mpeg2_intra_quant_matrix,
142 ARRAY_SIZE(mpeg2_intra_quant_matrix));
144 * The default non-intra MPEG-2 quantisation
145 * coefficients are all 16, as per the specification.
147 memset(p_mpeg2_quant->non_intra_quantiser_matrix, 16,
148 sizeof(p_mpeg2_quant->non_intra_quantiser_matrix));
150 case V4L2_CTRL_TYPE_VP8_FRAME:
152 p_vp8_frame->num_dct_parts = 1;
154 case V4L2_CTRL_TYPE_VP9_FRAME:
156 p_vp9_frame->profile = 0;
157 p_vp9_frame->bit_depth = 8;
158 p_vp9_frame->flags |= V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING |
159 V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING;
161 case V4L2_CTRL_TYPE_AV1_SEQUENCE:
163 p_av1_sequence->bit_depth = 8;
165 case V4L2_CTRL_TYPE_FWHT_PARAMS:
167 p_fwht_params->version = V4L2_FWHT_VERSION;
168 p_fwht_params->width = 1280;
169 p_fwht_params->height = 720;
170 p_fwht_params->flags = V4L2_FWHT_FL_PIXENC_YUV |
171 (2 << V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET);
173 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
174 p_h264_scaling_matrix = p;
176 * The default (flat) H.264 scaling matrix when none are
177 * specified in the bitstream, this is according to formulas
178 * (7-8) and (7-9) of the specification.
180 memset(p_h264_scaling_matrix, 16, sizeof(*p_h264_scaling_matrix));
185 void v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx,
186 union v4l2_ctrl_ptr ptr)
189 u32 tot_elems = ctrl->elems;
190 u32 elems = tot_elems - from_idx;
192 if (from_idx >= tot_elems)
195 switch (ctrl->type) {
196 case V4L2_CTRL_TYPE_STRING:
197 for (i = from_idx; i < tot_elems; i++) {
198 unsigned int offset = i * ctrl->elem_size;
200 memset(ptr.p_char + offset, ' ', ctrl->minimum);
201 ptr.p_char[offset + ctrl->minimum] = '\0';
204 case V4L2_CTRL_TYPE_INTEGER64:
205 if (ctrl->default_value) {
206 for (i = from_idx; i < tot_elems; i++)
207 ptr.p_s64[i] = ctrl->default_value;
209 memset(ptr.p_s64 + from_idx, 0, elems * sizeof(s64));
212 case V4L2_CTRL_TYPE_INTEGER:
213 case V4L2_CTRL_TYPE_INTEGER_MENU:
214 case V4L2_CTRL_TYPE_MENU:
215 case V4L2_CTRL_TYPE_BITMASK:
216 case V4L2_CTRL_TYPE_BOOLEAN:
217 if (ctrl->default_value) {
218 for (i = from_idx; i < tot_elems; i++)
219 ptr.p_s32[i] = ctrl->default_value;
221 memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32));
224 case V4L2_CTRL_TYPE_BUTTON:
225 case V4L2_CTRL_TYPE_CTRL_CLASS:
226 memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32));
228 case V4L2_CTRL_TYPE_U8:
229 memset(ptr.p_u8 + from_idx, ctrl->default_value, elems);
231 case V4L2_CTRL_TYPE_U16:
232 if (ctrl->default_value) {
233 for (i = from_idx; i < tot_elems; i++)
234 ptr.p_u16[i] = ctrl->default_value;
236 memset(ptr.p_u16 + from_idx, 0, elems * sizeof(u16));
239 case V4L2_CTRL_TYPE_U32:
240 if (ctrl->default_value) {
241 for (i = from_idx; i < tot_elems; i++)
242 ptr.p_u32[i] = ctrl->default_value;
244 memset(ptr.p_u32 + from_idx, 0, elems * sizeof(u32));
248 for (i = from_idx; i < tot_elems; i++)
249 std_init_compound(ctrl, i, ptr);
253 EXPORT_SYMBOL(v4l2_ctrl_type_op_init);
255 void v4l2_ctrl_type_op_log(const struct v4l2_ctrl *ctrl)
257 union v4l2_ctrl_ptr ptr = ctrl->p_cur;
259 if (ctrl->is_array) {
262 for (i = 0; i < ctrl->nr_of_dims; i++)
263 pr_cont("[%u]", ctrl->dims[i]);
267 switch (ctrl->type) {
268 case V4L2_CTRL_TYPE_INTEGER:
269 pr_cont("%d", *ptr.p_s32);
271 case V4L2_CTRL_TYPE_BOOLEAN:
272 pr_cont("%s", *ptr.p_s32 ? "true" : "false");
274 case V4L2_CTRL_TYPE_MENU:
275 pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
277 case V4L2_CTRL_TYPE_INTEGER_MENU:
278 pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
280 case V4L2_CTRL_TYPE_BITMASK:
281 pr_cont("0x%08x", *ptr.p_s32);
283 case V4L2_CTRL_TYPE_INTEGER64:
284 pr_cont("%lld", *ptr.p_s64);
286 case V4L2_CTRL_TYPE_STRING:
287 pr_cont("%s", ptr.p_char);
289 case V4L2_CTRL_TYPE_U8:
290 pr_cont("%u", (unsigned)*ptr.p_u8);
292 case V4L2_CTRL_TYPE_U16:
293 pr_cont("%u", (unsigned)*ptr.p_u16);
295 case V4L2_CTRL_TYPE_U32:
296 pr_cont("%u", (unsigned)*ptr.p_u32);
298 case V4L2_CTRL_TYPE_H264_SPS:
301 case V4L2_CTRL_TYPE_H264_PPS:
304 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
305 pr_cont("H264_SCALING_MATRIX");
307 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
308 pr_cont("H264_SLICE_PARAMS");
310 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
311 pr_cont("H264_DECODE_PARAMS");
313 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
314 pr_cont("H264_PRED_WEIGHTS");
316 case V4L2_CTRL_TYPE_FWHT_PARAMS:
317 pr_cont("FWHT_PARAMS");
319 case V4L2_CTRL_TYPE_VP8_FRAME:
320 pr_cont("VP8_FRAME");
322 case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
323 pr_cont("HDR10_CLL_INFO");
325 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
326 pr_cont("HDR10_MASTERING_DISPLAY");
328 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
329 pr_cont("MPEG2_QUANTISATION");
331 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
332 pr_cont("MPEG2_SEQUENCE");
334 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
335 pr_cont("MPEG2_PICTURE");
337 case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
338 pr_cont("VP9_COMPRESSED_HDR");
340 case V4L2_CTRL_TYPE_VP9_FRAME:
341 pr_cont("VP9_FRAME");
343 case V4L2_CTRL_TYPE_HEVC_SPS:
346 case V4L2_CTRL_TYPE_HEVC_PPS:
349 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
350 pr_cont("HEVC_SLICE_PARAMS");
352 case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
353 pr_cont("HEVC_SCALING_MATRIX");
355 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
356 pr_cont("HEVC_DECODE_PARAMS");
358 case V4L2_CTRL_TYPE_AV1_SEQUENCE:
359 pr_cont("AV1_SEQUENCE");
361 case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
362 pr_cont("AV1_TILE_GROUP_ENTRY");
364 case V4L2_CTRL_TYPE_AV1_FRAME:
365 pr_cont("AV1_FRAME");
367 case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
368 pr_cont("AV1_FILM_GRAIN");
372 pr_cont("unknown type %d", ctrl->type);
376 EXPORT_SYMBOL(v4l2_ctrl_type_op_log);
379 * Round towards the closest legal value. Be careful when we are
380 * close to the maximum range of the control type to prevent
383 #define ROUND_TO_RANGE(val, offset_type, ctrl) \
385 offset_type offset; \
386 if ((ctrl)->maximum >= 0 && \
387 val >= (ctrl)->maximum - (s32)((ctrl)->step / 2)) \
388 val = (ctrl)->maximum; \
390 val += (s32)((ctrl)->step / 2); \
391 val = clamp_t(typeof(val), val, \
392 (ctrl)->minimum, (ctrl)->maximum); \
393 offset = (val) - (ctrl)->minimum; \
394 offset = (ctrl)->step * (offset / (u32)(ctrl)->step); \
395 val = (ctrl)->minimum + offset; \
399 /* Validate a new control */
401 #define zero_padding(s) \
402 memset(&(s).padding, 0, sizeof((s).padding))
403 #define zero_reserved(s) \
404 memset(&(s).reserved, 0, sizeof((s).reserved))
407 validate_vp9_lf_params(struct v4l2_vp9_loop_filter *lf)
411 if (lf->flags & ~(V4L2_VP9_LOOP_FILTER_FLAG_DELTA_ENABLED |
412 V4L2_VP9_LOOP_FILTER_FLAG_DELTA_UPDATE))
415 /* That all values are in the accepted range. */
416 if (lf->level > GENMASK(5, 0))
419 if (lf->sharpness > GENMASK(2, 0))
422 for (i = 0; i < ARRAY_SIZE(lf->ref_deltas); i++)
423 if (lf->ref_deltas[i] < -63 || lf->ref_deltas[i] > 63)
426 for (i = 0; i < ARRAY_SIZE(lf->mode_deltas); i++)
427 if (lf->mode_deltas[i] < -63 || lf->mode_deltas[i] > 63)
435 validate_vp9_quant_params(struct v4l2_vp9_quantization *quant)
437 if (quant->delta_q_y_dc < -15 || quant->delta_q_y_dc > 15 ||
438 quant->delta_q_uv_dc < -15 || quant->delta_q_uv_dc > 15 ||
439 quant->delta_q_uv_ac < -15 || quant->delta_q_uv_ac > 15)
442 zero_reserved(*quant);
447 validate_vp9_seg_params(struct v4l2_vp9_segmentation *seg)
451 if (seg->flags & ~(V4L2_VP9_SEGMENTATION_FLAG_ENABLED |
452 V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP |
453 V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE |
454 V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA |
455 V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE))
458 for (i = 0; i < ARRAY_SIZE(seg->feature_enabled); i++) {
459 if (seg->feature_enabled[i] &
460 ~V4L2_VP9_SEGMENT_FEATURE_ENABLED_MASK)
464 for (i = 0; i < ARRAY_SIZE(seg->feature_data); i++) {
465 static const int range[] = { 255, 63, 3, 0 };
467 for (j = 0; j < ARRAY_SIZE(seg->feature_data[j]); j++) {
468 if (seg->feature_data[i][j] < -range[j] ||
469 seg->feature_data[i][j] > range[j])
479 validate_vp9_compressed_hdr(struct v4l2_ctrl_vp9_compressed_hdr *hdr)
481 if (hdr->tx_mode > V4L2_VP9_TX_MODE_SELECT)
488 validate_vp9_frame(struct v4l2_ctrl_vp9_frame *frame)
492 /* Make sure we're not passed invalid flags. */
493 if (frame->flags & ~(V4L2_VP9_FRAME_FLAG_KEY_FRAME |
494 V4L2_VP9_FRAME_FLAG_SHOW_FRAME |
495 V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT |
496 V4L2_VP9_FRAME_FLAG_INTRA_ONLY |
497 V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV |
498 V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX |
499 V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE |
500 V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING |
501 V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING |
502 V4L2_VP9_FRAME_FLAG_COLOR_RANGE_FULL_SWING))
505 if (frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT &&
506 frame->flags & V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX)
509 if (frame->profile > V4L2_VP9_PROFILE_MAX)
512 if (frame->reset_frame_context > V4L2_VP9_RESET_FRAME_CTX_ALL)
515 if (frame->frame_context_idx >= V4L2_VP9_NUM_FRAME_CTX)
519 * Profiles 0 and 1 only support 8-bit depth, profiles 2 and 3 only 10
522 if ((frame->profile < 2 && frame->bit_depth != 8) ||
523 (frame->profile >= 2 &&
524 (frame->bit_depth != 10 && frame->bit_depth != 12)))
527 /* Profile 0 and 2 only accept YUV 4:2:0. */
528 if ((frame->profile == 0 || frame->profile == 2) &&
529 (!(frame->flags & V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING) ||
530 !(frame->flags & V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING)))
533 /* Profile 1 and 3 only accept YUV 4:2:2, 4:4:0 and 4:4:4. */
534 if ((frame->profile == 1 || frame->profile == 3) &&
535 ((frame->flags & V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING) &&
536 (frame->flags & V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING)))
539 if (frame->interpolation_filter > V4L2_VP9_INTERP_FILTER_SWITCHABLE)
543 * According to the spec, tile_cols_log2 shall be less than or equal
546 if (frame->tile_cols_log2 > 6)
549 if (frame->reference_mode > V4L2_VP9_REFERENCE_MODE_SELECT)
552 ret = validate_vp9_lf_params(&frame->lf);
556 ret = validate_vp9_quant_params(&frame->quant);
560 ret = validate_vp9_seg_params(&frame->seg);
564 zero_reserved(*frame);
568 static int validate_av1_quantization(struct v4l2_av1_quantization *q)
570 if (q->flags > GENMASK(2, 0))
573 if (q->delta_q_y_dc < -64 || q->delta_q_y_dc > 63 ||
574 q->delta_q_u_dc < -64 || q->delta_q_u_dc > 63 ||
575 q->delta_q_v_dc < -64 || q->delta_q_v_dc > 63 ||
576 q->delta_q_u_ac < -64 || q->delta_q_u_ac > 63 ||
577 q->delta_q_v_ac < -64 || q->delta_q_v_ac > 63 ||
578 q->delta_q_res > GENMASK(1, 0))
581 if (q->qm_y > GENMASK(3, 0) ||
582 q->qm_u > GENMASK(3, 0) ||
583 q->qm_v > GENMASK(3, 0))
589 static int validate_av1_segmentation(struct v4l2_av1_segmentation *s)
594 if (s->flags > GENMASK(4, 0))
597 for (i = 0; i < ARRAY_SIZE(s->feature_data); i++) {
598 static const int segmentation_feature_signed[] = { 1, 1, 1, 1, 1, 0, 0, 0 };
599 static const int segmentation_feature_max[] = { 255, 63, 63, 63, 63, 7, 0, 0};
601 for (j = 0; j < ARRAY_SIZE(s->feature_data[j]); j++) {
602 s32 limit = segmentation_feature_max[j];
604 if (segmentation_feature_signed[j]) {
605 if (s->feature_data[i][j] < -limit ||
606 s->feature_data[i][j] > limit)
609 if (s->feature_data[i][j] < 0 || s->feature_data[i][j] > limit)
618 static int validate_av1_loop_filter(struct v4l2_av1_loop_filter *lf)
622 if (lf->flags > GENMASK(3, 0))
625 for (i = 0; i < ARRAY_SIZE(lf->level); i++) {
626 if (lf->level[i] > GENMASK(5, 0))
630 if (lf->sharpness > GENMASK(2, 0))
633 for (i = 0; i < ARRAY_SIZE(lf->ref_deltas); i++) {
634 if (lf->ref_deltas[i] < -64 || lf->ref_deltas[i] > 63)
638 for (i = 0; i < ARRAY_SIZE(lf->mode_deltas); i++) {
639 if (lf->mode_deltas[i] < -64 || lf->mode_deltas[i] > 63)
646 static int validate_av1_cdef(struct v4l2_av1_cdef *cdef)
650 if (cdef->damping_minus_3 > GENMASK(1, 0) ||
651 cdef->bits > GENMASK(1, 0))
654 for (i = 0; i < 1 << cdef->bits; i++) {
655 if (cdef->y_pri_strength[i] > GENMASK(3, 0) ||
656 cdef->y_sec_strength[i] > 4 ||
657 cdef->uv_pri_strength[i] > GENMASK(3, 0) ||
658 cdef->uv_sec_strength[i] > 4)
665 static int validate_av1_loop_restauration(struct v4l2_av1_loop_restoration *lr)
667 if (lr->lr_unit_shift > 3 || lr->lr_uv_shift > 1)
673 static int validate_av1_film_grain(struct v4l2_ctrl_av1_film_grain *fg)
677 if (fg->flags > GENMASK(4, 0))
680 if (fg->film_grain_params_ref_idx > GENMASK(2, 0) ||
681 fg->num_y_points > 14 ||
682 fg->num_cb_points > 10 ||
683 fg->num_cr_points > GENMASK(3, 0) ||
684 fg->grain_scaling_minus_8 > GENMASK(1, 0) ||
685 fg->ar_coeff_lag > GENMASK(1, 0) ||
686 fg->ar_coeff_shift_minus_6 > GENMASK(1, 0) ||
687 fg->grain_scale_shift > GENMASK(1, 0))
690 if (!(fg->flags & V4L2_AV1_FILM_GRAIN_FLAG_APPLY_GRAIN))
693 for (i = 1; i < fg->num_y_points; i++)
694 if (fg->point_y_value[i] <= fg->point_y_value[i - 1])
697 for (i = 1; i < fg->num_cb_points; i++)
698 if (fg->point_cb_value[i] <= fg->point_cb_value[i - 1])
701 for (i = 1; i < fg->num_cr_points; i++)
702 if (fg->point_cr_value[i] <= fg->point_cr_value[i - 1])
708 static int validate_av1_frame(struct v4l2_ctrl_av1_frame *f)
712 ret = validate_av1_quantization(&f->quantization);
715 ret = validate_av1_segmentation(&f->segmentation);
718 ret = validate_av1_loop_filter(&f->loop_filter);
721 ret = validate_av1_cdef(&f->cdef);
724 ret = validate_av1_loop_restauration(&f->loop_restoration);
729 ~(V4L2_AV1_FRAME_FLAG_SHOW_FRAME |
730 V4L2_AV1_FRAME_FLAG_SHOWABLE_FRAME |
731 V4L2_AV1_FRAME_FLAG_ERROR_RESILIENT_MODE |
732 V4L2_AV1_FRAME_FLAG_DISABLE_CDF_UPDATE |
733 V4L2_AV1_FRAME_FLAG_ALLOW_SCREEN_CONTENT_TOOLS |
734 V4L2_AV1_FRAME_FLAG_FORCE_INTEGER_MV |
735 V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC |
736 V4L2_AV1_FRAME_FLAG_USE_SUPERRES |
737 V4L2_AV1_FRAME_FLAG_ALLOW_HIGH_PRECISION_MV |
738 V4L2_AV1_FRAME_FLAG_IS_MOTION_MODE_SWITCHABLE |
739 V4L2_AV1_FRAME_FLAG_USE_REF_FRAME_MVS |
740 V4L2_AV1_FRAME_FLAG_DISABLE_FRAME_END_UPDATE_CDF |
741 V4L2_AV1_FRAME_FLAG_ALLOW_WARPED_MOTION |
742 V4L2_AV1_FRAME_FLAG_REFERENCE_SELECT |
743 V4L2_AV1_FRAME_FLAG_REDUCED_TX_SET |
744 V4L2_AV1_FRAME_FLAG_SKIP_MODE_ALLOWED |
745 V4L2_AV1_FRAME_FLAG_SKIP_MODE_PRESENT |
746 V4L2_AV1_FRAME_FLAG_FRAME_SIZE_OVERRIDE |
747 V4L2_AV1_FRAME_FLAG_BUFFER_REMOVAL_TIME_PRESENT |
748 V4L2_AV1_FRAME_FLAG_FRAME_REFS_SHORT_SIGNALING))
751 if (f->superres_denom > GENMASK(2, 0) + 9)
757 static int validate_av1_sequence(struct v4l2_ctrl_av1_sequence *s)
760 ~(V4L2_AV1_SEQUENCE_FLAG_STILL_PICTURE |
761 V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK |
762 V4L2_AV1_SEQUENCE_FLAG_ENABLE_FILTER_INTRA |
763 V4L2_AV1_SEQUENCE_FLAG_ENABLE_INTRA_EDGE_FILTER |
764 V4L2_AV1_SEQUENCE_FLAG_ENABLE_INTERINTRA_COMPOUND |
765 V4L2_AV1_SEQUENCE_FLAG_ENABLE_MASKED_COMPOUND |
766 V4L2_AV1_SEQUENCE_FLAG_ENABLE_WARPED_MOTION |
767 V4L2_AV1_SEQUENCE_FLAG_ENABLE_DUAL_FILTER |
768 V4L2_AV1_SEQUENCE_FLAG_ENABLE_ORDER_HINT |
769 V4L2_AV1_SEQUENCE_FLAG_ENABLE_JNT_COMP |
770 V4L2_AV1_SEQUENCE_FLAG_ENABLE_REF_FRAME_MVS |
771 V4L2_AV1_SEQUENCE_FLAG_ENABLE_SUPERRES |
772 V4L2_AV1_SEQUENCE_FLAG_ENABLE_CDEF |
773 V4L2_AV1_SEQUENCE_FLAG_ENABLE_RESTORATION |
774 V4L2_AV1_SEQUENCE_FLAG_MONO_CHROME |
775 V4L2_AV1_SEQUENCE_FLAG_COLOR_RANGE |
776 V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X |
777 V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y |
778 V4L2_AV1_SEQUENCE_FLAG_FILM_GRAIN_PARAMS_PRESENT |
779 V4L2_AV1_SEQUENCE_FLAG_SEPARATE_UV_DELTA_Q))
782 if (s->seq_profile == 1 && s->flags & V4L2_AV1_SEQUENCE_FLAG_MONO_CHROME)
786 if (s->seq_profile > 2)
794 * Compound controls validation requires setting unused fields/flags to zero
795 * in order to properly detect unchanged controls with v4l2_ctrl_type_op_equal's
798 static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
799 union v4l2_ctrl_ptr ptr)
801 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence;
802 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture;
803 struct v4l2_ctrl_vp8_frame *p_vp8_frame;
804 struct v4l2_ctrl_fwht_params *p_fwht_params;
805 struct v4l2_ctrl_h264_sps *p_h264_sps;
806 struct v4l2_ctrl_h264_pps *p_h264_pps;
807 struct v4l2_ctrl_h264_pred_weights *p_h264_pred_weights;
808 struct v4l2_ctrl_h264_slice_params *p_h264_slice_params;
809 struct v4l2_ctrl_h264_decode_params *p_h264_dec_params;
810 struct v4l2_ctrl_hevc_sps *p_hevc_sps;
811 struct v4l2_ctrl_hevc_pps *p_hevc_pps;
812 struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering;
813 struct v4l2_ctrl_hevc_decode_params *p_hevc_decode_params;
814 struct v4l2_area *area;
815 void *p = ptr.p + idx * ctrl->elem_size;
818 switch ((u32)ctrl->type) {
819 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
820 p_mpeg2_sequence = p;
822 switch (p_mpeg2_sequence->chroma_format) {
832 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
835 switch (p_mpeg2_picture->intra_dc_precision) {
838 case 2: /* 10 bits */
839 case 3: /* 11 bits */
845 switch (p_mpeg2_picture->picture_structure) {
846 case V4L2_MPEG2_PIC_TOP_FIELD:
847 case V4L2_MPEG2_PIC_BOTTOM_FIELD:
848 case V4L2_MPEG2_PIC_FRAME:
854 switch (p_mpeg2_picture->picture_coding_type) {
855 case V4L2_MPEG2_PIC_CODING_TYPE_I:
856 case V4L2_MPEG2_PIC_CODING_TYPE_P:
857 case V4L2_MPEG2_PIC_CODING_TYPE_B:
862 zero_reserved(*p_mpeg2_picture);
865 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
868 case V4L2_CTRL_TYPE_FWHT_PARAMS:
870 if (p_fwht_params->version < V4L2_FWHT_VERSION)
872 if (!p_fwht_params->width || !p_fwht_params->height)
876 case V4L2_CTRL_TYPE_H264_SPS:
879 /* Some syntax elements are only conditionally valid */
880 if (p_h264_sps->pic_order_cnt_type != 0) {
881 p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 = 0;
882 } else if (p_h264_sps->pic_order_cnt_type != 1) {
883 p_h264_sps->num_ref_frames_in_pic_order_cnt_cycle = 0;
884 p_h264_sps->offset_for_non_ref_pic = 0;
885 p_h264_sps->offset_for_top_to_bottom_field = 0;
886 memset(&p_h264_sps->offset_for_ref_frame, 0,
887 sizeof(p_h264_sps->offset_for_ref_frame));
890 if (!V4L2_H264_SPS_HAS_CHROMA_FORMAT(p_h264_sps)) {
891 p_h264_sps->chroma_format_idc = 1;
892 p_h264_sps->bit_depth_luma_minus8 = 0;
893 p_h264_sps->bit_depth_chroma_minus8 = 0;
896 ~V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS;
898 if (p_h264_sps->chroma_format_idc < 3)
900 ~V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE;
903 if (p_h264_sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)
905 ~V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD;
908 * Chroma 4:2:2 format require at least High 4:2:2 profile.
910 * The H264 specification and well-known parser implementations
911 * use profile-idc values directly, as that is clearer and
912 * less ambiguous. We do the same here.
914 if (p_h264_sps->profile_idc < 122 &&
915 p_h264_sps->chroma_format_idc > 1)
917 /* Chroma 4:4:4 format require at least High 4:2:2 profile */
918 if (p_h264_sps->profile_idc < 244 &&
919 p_h264_sps->chroma_format_idc > 2)
921 if (p_h264_sps->chroma_format_idc > 3)
924 if (p_h264_sps->bit_depth_luma_minus8 > 6)
926 if (p_h264_sps->bit_depth_chroma_minus8 > 6)
928 if (p_h264_sps->log2_max_frame_num_minus4 > 12)
930 if (p_h264_sps->pic_order_cnt_type > 2)
932 if (p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 > 12)
934 if (p_h264_sps->max_num_ref_frames > V4L2_H264_REF_LIST_LEN)
938 case V4L2_CTRL_TYPE_H264_PPS:
941 if (p_h264_pps->num_slice_groups_minus1 > 7)
943 if (p_h264_pps->num_ref_idx_l0_default_active_minus1 >
944 (V4L2_H264_REF_LIST_LEN - 1))
946 if (p_h264_pps->num_ref_idx_l1_default_active_minus1 >
947 (V4L2_H264_REF_LIST_LEN - 1))
949 if (p_h264_pps->weighted_bipred_idc > 2)
952 * pic_init_qp_minus26 shall be in the range of
953 * -(26 + QpBdOffset_y) to +25, inclusive,
954 * where QpBdOffset_y is 6 * bit_depth_luma_minus8
956 if (p_h264_pps->pic_init_qp_minus26 < -62 ||
957 p_h264_pps->pic_init_qp_minus26 > 25)
959 if (p_h264_pps->pic_init_qs_minus26 < -26 ||
960 p_h264_pps->pic_init_qs_minus26 > 25)
962 if (p_h264_pps->chroma_qp_index_offset < -12 ||
963 p_h264_pps->chroma_qp_index_offset > 12)
965 if (p_h264_pps->second_chroma_qp_index_offset < -12 ||
966 p_h264_pps->second_chroma_qp_index_offset > 12)
970 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
973 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
974 p_h264_pred_weights = p;
976 if (p_h264_pred_weights->luma_log2_weight_denom > 7)
978 if (p_h264_pred_weights->chroma_log2_weight_denom > 7)
982 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
983 p_h264_slice_params = p;
985 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
986 p_h264_slice_params->flags &=
987 ~V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED;
989 if (p_h264_slice_params->colour_plane_id > 2)
991 if (p_h264_slice_params->cabac_init_idc > 2)
993 if (p_h264_slice_params->disable_deblocking_filter_idc > 2)
995 if (p_h264_slice_params->slice_alpha_c0_offset_div2 < -6 ||
996 p_h264_slice_params->slice_alpha_c0_offset_div2 > 6)
998 if (p_h264_slice_params->slice_beta_offset_div2 < -6 ||
999 p_h264_slice_params->slice_beta_offset_div2 > 6)
1002 if (p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_I ||
1003 p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_SI)
1004 p_h264_slice_params->num_ref_idx_l0_active_minus1 = 0;
1005 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
1006 p_h264_slice_params->num_ref_idx_l1_active_minus1 = 0;
1008 if (p_h264_slice_params->num_ref_idx_l0_active_minus1 >
1009 (V4L2_H264_REF_LIST_LEN - 1))
1011 if (p_h264_slice_params->num_ref_idx_l1_active_minus1 >
1012 (V4L2_H264_REF_LIST_LEN - 1))
1014 zero_reserved(*p_h264_slice_params);
1017 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
1018 p_h264_dec_params = p;
1020 if (p_h264_dec_params->nal_ref_idc > 3)
1022 for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
1023 struct v4l2_h264_dpb_entry *dpb_entry =
1024 &p_h264_dec_params->dpb[i];
1026 zero_reserved(*dpb_entry);
1028 zero_reserved(*p_h264_dec_params);
1031 case V4L2_CTRL_TYPE_VP8_FRAME:
1034 switch (p_vp8_frame->num_dct_parts) {
1043 zero_padding(p_vp8_frame->segment);
1044 zero_padding(p_vp8_frame->lf);
1045 zero_padding(p_vp8_frame->quant);
1046 zero_padding(p_vp8_frame->entropy);
1047 zero_padding(p_vp8_frame->coder_state);
1050 case V4L2_CTRL_TYPE_HEVC_SPS:
1053 if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) {
1054 p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0;
1055 p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;
1056 p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0;
1057 p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0;
1060 if (!(p_hevc_sps->flags &
1061 V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT))
1062 p_hevc_sps->num_long_term_ref_pics_sps = 0;
1065 case V4L2_CTRL_TYPE_HEVC_PPS:
1068 if (!(p_hevc_pps->flags &
1069 V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED))
1070 p_hevc_pps->diff_cu_qp_delta_depth = 0;
1072 if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
1073 p_hevc_pps->num_tile_columns_minus1 = 0;
1074 p_hevc_pps->num_tile_rows_minus1 = 0;
1075 memset(&p_hevc_pps->column_width_minus1, 0,
1076 sizeof(p_hevc_pps->column_width_minus1));
1077 memset(&p_hevc_pps->row_height_minus1, 0,
1078 sizeof(p_hevc_pps->row_height_minus1));
1080 p_hevc_pps->flags &=
1081 ~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED;
1084 if (p_hevc_pps->flags &
1085 V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) {
1086 p_hevc_pps->pps_beta_offset_div2 = 0;
1087 p_hevc_pps->pps_tc_offset_div2 = 0;
1091 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
1092 p_hevc_decode_params = p;
1094 if (p_hevc_decode_params->num_active_dpb_entries >
1095 V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
1099 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
1102 case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
1105 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
1106 p_hdr10_mastering = p;
1108 for (i = 0; i < 3; ++i) {
1109 if (p_hdr10_mastering->display_primaries_x[i] <
1110 V4L2_HDR10_MASTERING_PRIMARIES_X_LOW ||
1111 p_hdr10_mastering->display_primaries_x[i] >
1112 V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH ||
1113 p_hdr10_mastering->display_primaries_y[i] <
1114 V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW ||
1115 p_hdr10_mastering->display_primaries_y[i] >
1116 V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH)
1120 if (p_hdr10_mastering->white_point_x <
1121 V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW ||
1122 p_hdr10_mastering->white_point_x >
1123 V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH ||
1124 p_hdr10_mastering->white_point_y <
1125 V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW ||
1126 p_hdr10_mastering->white_point_y >
1127 V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH)
1130 if (p_hdr10_mastering->max_display_mastering_luminance <
1131 V4L2_HDR10_MASTERING_MAX_LUMA_LOW ||
1132 p_hdr10_mastering->max_display_mastering_luminance >
1133 V4L2_HDR10_MASTERING_MAX_LUMA_HIGH ||
1134 p_hdr10_mastering->min_display_mastering_luminance <
1135 V4L2_HDR10_MASTERING_MIN_LUMA_LOW ||
1136 p_hdr10_mastering->min_display_mastering_luminance >
1137 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
1140 /* The following restriction comes from ITU-T Rec. H.265 spec */
1141 if (p_hdr10_mastering->max_display_mastering_luminance ==
1142 V4L2_HDR10_MASTERING_MAX_LUMA_LOW &&
1143 p_hdr10_mastering->min_display_mastering_luminance ==
1144 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
1149 case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
1152 case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
1153 return validate_vp9_compressed_hdr(p);
1155 case V4L2_CTRL_TYPE_VP9_FRAME:
1156 return validate_vp9_frame(p);
1157 case V4L2_CTRL_TYPE_AV1_FRAME:
1158 return validate_av1_frame(p);
1159 case V4L2_CTRL_TYPE_AV1_SEQUENCE:
1160 return validate_av1_sequence(p);
1161 case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
1163 case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
1164 return validate_av1_film_grain(p);
1166 case V4L2_CTRL_TYPE_AREA:
1168 if (!area->width || !area->height)
1179 static int std_validate_elem(const struct v4l2_ctrl *ctrl, u32 idx,
1180 union v4l2_ctrl_ptr ptr)
1186 switch ((u32)ctrl->type) {
1187 case V4L2_CTRL_TYPE_INTEGER:
1188 return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
1189 case V4L2_CTRL_TYPE_INTEGER64:
1191 * We can't use the ROUND_TO_RANGE define here due to
1192 * the u64 divide that needs special care.
1194 val = ptr.p_s64[idx];
1195 if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
1196 val = ctrl->maximum;
1198 val += (s64)(ctrl->step / 2);
1199 val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
1200 offset = val - ctrl->minimum;
1201 do_div(offset, ctrl->step);
1202 ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
1204 case V4L2_CTRL_TYPE_U8:
1205 return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
1206 case V4L2_CTRL_TYPE_U16:
1207 return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
1208 case V4L2_CTRL_TYPE_U32:
1209 return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
1211 case V4L2_CTRL_TYPE_BOOLEAN:
1212 ptr.p_s32[idx] = !!ptr.p_s32[idx];
1215 case V4L2_CTRL_TYPE_MENU:
1216 case V4L2_CTRL_TYPE_INTEGER_MENU:
1217 if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
1219 if (ptr.p_s32[idx] < BITS_PER_LONG_LONG &&
1220 (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx])))
1222 if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1223 ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
1227 case V4L2_CTRL_TYPE_BITMASK:
1228 ptr.p_s32[idx] &= ctrl->maximum;
1231 case V4L2_CTRL_TYPE_BUTTON:
1232 case V4L2_CTRL_TYPE_CTRL_CLASS:
1236 case V4L2_CTRL_TYPE_STRING:
1237 idx *= ctrl->elem_size;
1238 len = strlen(ptr.p_char + idx);
1239 if (len < ctrl->minimum)
1241 if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
1246 return std_validate_compound(ctrl, idx, ptr);
1250 int v4l2_ctrl_type_op_validate(const struct v4l2_ctrl *ctrl,
1251 union v4l2_ctrl_ptr ptr)
1256 switch ((u32)ctrl->type) {
1257 case V4L2_CTRL_TYPE_U8:
1258 if (ctrl->maximum == 0xff && ctrl->minimum == 0 && ctrl->step == 1)
1261 case V4L2_CTRL_TYPE_U16:
1262 if (ctrl->maximum == 0xffff && ctrl->minimum == 0 && ctrl->step == 1)
1265 case V4L2_CTRL_TYPE_U32:
1266 if (ctrl->maximum == 0xffffffff && ctrl->minimum == 0 && ctrl->step == 1)
1270 case V4L2_CTRL_TYPE_BUTTON:
1271 case V4L2_CTRL_TYPE_CTRL_CLASS:
1272 memset(ptr.p_s32, 0, ctrl->new_elems * sizeof(s32));
1276 for (i = 0; !ret && i < ctrl->new_elems; i++)
1277 ret = std_validate_elem(ctrl, i, ptr);
1280 EXPORT_SYMBOL(v4l2_ctrl_type_op_validate);
1282 static const struct v4l2_ctrl_type_ops std_type_ops = {
1283 .equal = v4l2_ctrl_type_op_equal,
1284 .init = v4l2_ctrl_type_op_init,
1285 .log = v4l2_ctrl_type_op_log,
1286 .validate = v4l2_ctrl_type_op_validate,
1289 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
1294 ctrl->call_notify = 0;
1297 if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
1299 ctrl->handler->notify = notify;
1300 ctrl->handler->notify_priv = priv;
1301 ctrl->call_notify = 1;
1303 EXPORT_SYMBOL(v4l2_ctrl_notify);
1305 /* Copy the one value to another. */
1306 static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
1307 union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to,
1312 memcpy(to.p, from.p_const, elems * ctrl->elem_size);
1315 /* Copy the new value to the current value. */
1316 void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
1323 /* has_changed is set by cluster_changed */
1324 changed = ctrl->has_changed;
1326 if (ctrl->is_dyn_array)
1327 ctrl->elems = ctrl->new_elems;
1328 ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur, ctrl->elems);
1331 if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
1332 /* Note: CH_FLAGS is only set for auto clusters. */
1334 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
1335 if (!is_cur_manual(ctrl->cluster[0])) {
1336 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1337 if (ctrl->cluster[0]->has_volatiles)
1338 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1342 if (changed || ch_flags) {
1343 /* If a control was changed that was not one of the controls
1344 modified by the application, then send the event to all. */
1347 send_event(fh, ctrl,
1348 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
1349 if (ctrl->call_notify && changed && ctrl->handler->notify)
1350 ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
1354 /* Copy the current value to the new value */
1355 void cur_to_new(struct v4l2_ctrl *ctrl)
1359 if (ctrl->is_dyn_array)
1360 ctrl->new_elems = ctrl->elems;
1361 ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems);
1364 static bool req_alloc_array(struct v4l2_ctrl_ref *ref, u32 elems)
1368 if (elems == ref->p_req_array_alloc_elems)
1370 if (ref->ctrl->is_dyn_array &&
1371 elems < ref->p_req_array_alloc_elems)
1374 tmp = kvmalloc(elems * ref->ctrl->elem_size, GFP_KERNEL);
1377 ref->p_req_array_enomem = true;
1380 ref->p_req_array_enomem = false;
1381 kvfree(ref->p_req.p);
1383 ref->p_req_array_alloc_elems = elems;
1387 /* Copy the new value to the request value */
1388 void new_to_req(struct v4l2_ctrl_ref *ref)
1390 struct v4l2_ctrl *ctrl;
1396 if (ctrl->is_array && !req_alloc_array(ref, ctrl->new_elems))
1399 ref->p_req_elems = ctrl->new_elems;
1400 ptr_to_ptr(ctrl, ctrl->p_new, ref->p_req, ref->p_req_elems);
1401 ref->p_req_valid = true;
1404 /* Copy the current value to the request value */
1405 void cur_to_req(struct v4l2_ctrl_ref *ref)
1407 struct v4l2_ctrl *ctrl;
1413 if (ctrl->is_array && !req_alloc_array(ref, ctrl->elems))
1416 ref->p_req_elems = ctrl->elems;
1417 ptr_to_ptr(ctrl, ctrl->p_cur, ref->p_req, ctrl->elems);
1418 ref->p_req_valid = true;
1421 /* Copy the request value to the new value */
1422 int req_to_new(struct v4l2_ctrl_ref *ref)
1424 struct v4l2_ctrl *ctrl;
1432 * This control was never set in the request, so just use the current
1435 if (!ref->p_req_valid) {
1436 if (ctrl->is_dyn_array)
1437 ctrl->new_elems = ctrl->elems;
1438 ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems);
1442 /* Not an array, so just copy the request value */
1443 if (!ctrl->is_array) {
1444 ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems);
1448 /* Sanity check, should never happen */
1449 if (WARN_ON(!ref->p_req_array_alloc_elems))
1452 if (!ctrl->is_dyn_array &&
1453 ref->p_req_elems != ctrl->p_array_alloc_elems)
1457 * Check if the number of elements in the request is more than the
1458 * elements in ctrl->p_array. If so, attempt to realloc ctrl->p_array.
1459 * Note that p_array is allocated with twice the number of elements
1460 * in the dynamic array since it has to store both the current and
1461 * new value of such a control.
1463 if (ref->p_req_elems > ctrl->p_array_alloc_elems) {
1464 unsigned int sz = ref->p_req_elems * ctrl->elem_size;
1465 void *old = ctrl->p_array;
1466 void *tmp = kvzalloc(2 * sz, GFP_KERNEL);
1470 memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size);
1471 memcpy(tmp + sz, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size);
1472 ctrl->p_new.p = tmp;
1473 ctrl->p_cur.p = tmp + sz;
1474 ctrl->p_array = tmp;
1475 ctrl->p_array_alloc_elems = ref->p_req_elems;
1479 ctrl->new_elems = ref->p_req_elems;
1480 ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems);
1484 /* Control range checking */
1485 int check_range(enum v4l2_ctrl_type type,
1486 s64 min, s64 max, u64 step, s64 def)
1489 case V4L2_CTRL_TYPE_BOOLEAN:
1490 if (step != 1 || max > 1 || min < 0)
1493 case V4L2_CTRL_TYPE_U8:
1494 case V4L2_CTRL_TYPE_U16:
1495 case V4L2_CTRL_TYPE_U32:
1496 case V4L2_CTRL_TYPE_INTEGER:
1497 case V4L2_CTRL_TYPE_INTEGER64:
1498 if (step == 0 || min > max || def < min || def > max)
1501 case V4L2_CTRL_TYPE_BITMASK:
1502 if (step || min || !max || (def & ~max))
1505 case V4L2_CTRL_TYPE_MENU:
1506 case V4L2_CTRL_TYPE_INTEGER_MENU:
1507 if (min > max || def < min || def > max ||
1508 min < 0 || (step && max >= BITS_PER_LONG_LONG))
1510 /* Note: step == menu_skip_mask for menu controls.
1511 So here we check if the default value is masked out. */
1512 if (def < BITS_PER_LONG_LONG && (step & BIT_ULL(def)))
1515 case V4L2_CTRL_TYPE_STRING:
1516 if (min > max || min < 0 || step < 1 || def)
1524 /* Set the handler's error code if it wasn't set earlier already */
1525 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1527 if (hdl->error == 0)
1532 /* Initialize the handler */
1533 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
1534 unsigned nr_of_controls_hint,
1535 struct lock_class_key *key, const char *name)
1537 mutex_init(&hdl->_lock);
1538 hdl->lock = &hdl->_lock;
1539 lockdep_set_class_and_name(hdl->lock, key, name);
1540 INIT_LIST_HEAD(&hdl->ctrls);
1541 INIT_LIST_HEAD(&hdl->ctrl_refs);
1542 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
1543 hdl->buckets = kvcalloc(hdl->nr_of_buckets, sizeof(hdl->buckets[0]),
1545 hdl->error = hdl->buckets ? 0 : -ENOMEM;
1546 v4l2_ctrl_handler_init_request(hdl);
1549 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
1551 /* Free all controls and control refs */
1552 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1554 struct v4l2_ctrl_ref *ref, *next_ref;
1555 struct v4l2_ctrl *ctrl, *next_ctrl;
1556 struct v4l2_subscribed_event *sev, *next_sev;
1558 if (hdl == NULL || hdl->buckets == NULL)
1561 v4l2_ctrl_handler_free_request(hdl);
1563 mutex_lock(hdl->lock);
1564 /* Free all nodes */
1565 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1566 list_del(&ref->node);
1567 if (ref->p_req_array_alloc_elems)
1568 kvfree(ref->p_req.p);
1571 /* Free all controls owned by the handler */
1572 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1573 list_del(&ctrl->node);
1574 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1575 list_del(&sev->node);
1576 kvfree(ctrl->p_array);
1579 kvfree(hdl->buckets);
1580 hdl->buckets = NULL;
1583 mutex_unlock(hdl->lock);
1584 mutex_destroy(&hdl->_lock);
1586 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1588 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1589 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1590 with applications that do not use the NEXT_CTRL flag.
1592 We just find the n-th private user control. It's O(N), but that should not
1593 be an issue in this particular case. */
1594 static struct v4l2_ctrl_ref *find_private_ref(
1595 struct v4l2_ctrl_handler *hdl, u32 id)
1597 struct v4l2_ctrl_ref *ref;
1599 id -= V4L2_CID_PRIVATE_BASE;
1600 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1601 /* Search for private user controls that are compatible with
1603 if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1604 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1605 if (!ref->ctrl->is_int)
1615 /* Find a control with the given ID. */
1616 struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1618 struct v4l2_ctrl_ref *ref;
1621 id &= V4L2_CTRL_ID_MASK;
1623 /* Old-style private controls need special handling */
1624 if (id >= V4L2_CID_PRIVATE_BASE)
1625 return find_private_ref(hdl, id);
1626 bucket = id % hdl->nr_of_buckets;
1628 /* Simple optimization: cache the last control found */
1629 if (hdl->cached && hdl->cached->ctrl->id == id)
1632 /* Not in cache, search the hash */
1633 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1634 while (ref && ref->ctrl->id != id)
1638 hdl->cached = ref; /* cache it! */
1642 /* Find a control with the given ID. Take the handler's lock first. */
1643 struct v4l2_ctrl_ref *find_ref_lock(struct v4l2_ctrl_handler *hdl, u32 id)
1645 struct v4l2_ctrl_ref *ref = NULL;
1648 mutex_lock(hdl->lock);
1649 ref = find_ref(hdl, id);
1650 mutex_unlock(hdl->lock);
1655 /* Find a control with the given ID. */
1656 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1658 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1660 return ref ? ref->ctrl : NULL;
1662 EXPORT_SYMBOL(v4l2_ctrl_find);
1664 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1665 int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1666 struct v4l2_ctrl *ctrl,
1667 struct v4l2_ctrl_ref **ctrl_ref,
1668 bool from_other_dev, bool allocate_req)
1670 struct v4l2_ctrl_ref *ref;
1671 struct v4l2_ctrl_ref *new_ref;
1673 u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
1674 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
1675 unsigned int size_extra_req = 0;
1681 * Automatically add the control class if it is not yet present and
1682 * the new control is not a compound control.
1684 if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
1685 id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1686 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1692 if (allocate_req && !ctrl->is_array)
1693 size_extra_req = ctrl->elems * ctrl->elem_size;
1694 new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
1696 return handler_set_err(hdl, -ENOMEM);
1697 new_ref->ctrl = ctrl;
1698 new_ref->from_other_dev = from_other_dev;
1700 new_ref->p_req.p = &new_ref[1];
1702 INIT_LIST_HEAD(&new_ref->node);
1704 mutex_lock(hdl->lock);
1706 /* Add immediately at the end of the list if the list is empty, or if
1707 the last element in the list has a lower ID.
1708 This ensures that when elements are added in ascending order the
1709 insertion is an O(1) operation. */
1710 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1711 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1712 goto insert_in_hash;
1715 /* Find insert position in sorted list */
1716 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1717 if (ref->ctrl->id < id)
1719 /* Don't add duplicates */
1720 if (ref->ctrl->id == id) {
1724 list_add(&new_ref->node, ref->node.prev);
1729 /* Insert the control node in the hash */
1730 new_ref->next = hdl->buckets[bucket];
1731 hdl->buckets[bucket] = new_ref;
1733 *ctrl_ref = new_ref;
1734 if (ctrl->handler == hdl) {
1735 /* By default each control starts in a cluster of its own.
1736 * new_ref->ctrl is basically a cluster array with one
1737 * element, so that's perfect to use as the cluster pointer.
1738 * But only do this for the handler that owns the control.
1740 ctrl->cluster = &new_ref->ctrl;
1741 ctrl->ncontrols = 1;
1745 mutex_unlock(hdl->lock);
1749 /* Add a new control */
1750 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1751 const struct v4l2_ctrl_ops *ops,
1752 const struct v4l2_ctrl_type_ops *type_ops,
1753 u32 id, const char *name, enum v4l2_ctrl_type type,
1754 s64 min, s64 max, u64 step, s64 def,
1755 const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
1756 u32 flags, const char * const *qmenu,
1757 const s64 *qmenu_int, const union v4l2_ctrl_ptr p_def,
1760 struct v4l2_ctrl *ctrl;
1762 unsigned nr_of_dims = 0;
1765 unsigned tot_ctrl_size;
1772 while (dims && dims[nr_of_dims]) {
1773 elems *= dims[nr_of_dims];
1775 if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
1778 is_array = nr_of_dims > 0;
1780 /* Prefill elem_size for all types handled by std_type_ops */
1781 switch ((u32)type) {
1782 case V4L2_CTRL_TYPE_INTEGER64:
1783 elem_size = sizeof(s64);
1785 case V4L2_CTRL_TYPE_STRING:
1786 elem_size = max + 1;
1788 case V4L2_CTRL_TYPE_U8:
1789 elem_size = sizeof(u8);
1791 case V4L2_CTRL_TYPE_U16:
1792 elem_size = sizeof(u16);
1794 case V4L2_CTRL_TYPE_U32:
1795 elem_size = sizeof(u32);
1797 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
1798 elem_size = sizeof(struct v4l2_ctrl_mpeg2_sequence);
1800 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
1801 elem_size = sizeof(struct v4l2_ctrl_mpeg2_picture);
1803 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
1804 elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantisation);
1806 case V4L2_CTRL_TYPE_FWHT_PARAMS:
1807 elem_size = sizeof(struct v4l2_ctrl_fwht_params);
1809 case V4L2_CTRL_TYPE_H264_SPS:
1810 elem_size = sizeof(struct v4l2_ctrl_h264_sps);
1812 case V4L2_CTRL_TYPE_H264_PPS:
1813 elem_size = sizeof(struct v4l2_ctrl_h264_pps);
1815 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
1816 elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix);
1818 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
1819 elem_size = sizeof(struct v4l2_ctrl_h264_slice_params);
1821 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
1822 elem_size = sizeof(struct v4l2_ctrl_h264_decode_params);
1824 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
1825 elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights);
1827 case V4L2_CTRL_TYPE_VP8_FRAME:
1828 elem_size = sizeof(struct v4l2_ctrl_vp8_frame);
1830 case V4L2_CTRL_TYPE_HEVC_SPS:
1831 elem_size = sizeof(struct v4l2_ctrl_hevc_sps);
1833 case V4L2_CTRL_TYPE_HEVC_PPS:
1834 elem_size = sizeof(struct v4l2_ctrl_hevc_pps);
1836 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
1837 elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params);
1839 case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
1840 elem_size = sizeof(struct v4l2_ctrl_hevc_scaling_matrix);
1842 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
1843 elem_size = sizeof(struct v4l2_ctrl_hevc_decode_params);
1845 case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
1846 elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info);
1848 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
1849 elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display);
1851 case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
1852 elem_size = sizeof(struct v4l2_ctrl_vp9_compressed_hdr);
1854 case V4L2_CTRL_TYPE_VP9_FRAME:
1855 elem_size = sizeof(struct v4l2_ctrl_vp9_frame);
1857 case V4L2_CTRL_TYPE_AV1_SEQUENCE:
1858 elem_size = sizeof(struct v4l2_ctrl_av1_sequence);
1860 case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
1861 elem_size = sizeof(struct v4l2_ctrl_av1_tile_group_entry);
1863 case V4L2_CTRL_TYPE_AV1_FRAME:
1864 elem_size = sizeof(struct v4l2_ctrl_av1_frame);
1866 case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
1867 elem_size = sizeof(struct v4l2_ctrl_av1_film_grain);
1869 case V4L2_CTRL_TYPE_AREA:
1870 elem_size = sizeof(struct v4l2_area);
1873 if (type < V4L2_CTRL_COMPOUND_TYPES)
1874 elem_size = sizeof(s32);
1879 if (id == 0 || name == NULL || !elem_size ||
1880 id >= V4L2_CID_PRIVATE_BASE ||
1881 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1882 (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
1883 handler_set_err(hdl, -ERANGE);
1886 err = check_range(type, min, max, step, def);
1888 handler_set_err(hdl, err);
1892 (type == V4L2_CTRL_TYPE_BUTTON ||
1893 type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
1894 handler_set_err(hdl, -EINVAL);
1897 if (flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY) {
1899 * For now only support this for one-dimensional arrays only.
1901 * This can be relaxed in the future, but this will
1902 * require more effort.
1904 if (nr_of_dims != 1) {
1905 handler_set_err(hdl, -EINVAL);
1908 /* Start with just 1 element */
1912 tot_ctrl_size = elem_size * elems;
1914 if (type == V4L2_CTRL_TYPE_BUTTON)
1915 flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1916 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1917 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1918 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1919 else if (!is_array &&
1920 (type == V4L2_CTRL_TYPE_INTEGER64 ||
1921 type == V4L2_CTRL_TYPE_STRING ||
1922 type >= V4L2_CTRL_COMPOUND_TYPES))
1923 sz_extra += 2 * tot_ctrl_size;
1925 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const)
1926 sz_extra += elem_size;
1928 ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1930 handler_set_err(hdl, -ENOMEM);
1934 INIT_LIST_HEAD(&ctrl->node);
1935 INIT_LIST_HEAD(&ctrl->ev_subs);
1936 ctrl->handler = hdl;
1938 ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
1942 ctrl->flags = flags;
1943 ctrl->minimum = min;
1944 ctrl->maximum = max;
1946 ctrl->default_value = def;
1947 ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
1948 ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
1949 ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
1950 ctrl->is_array = is_array;
1951 ctrl->is_dyn_array = !!(flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY);
1952 ctrl->elems = elems;
1953 ctrl->new_elems = elems;
1954 ctrl->nr_of_dims = nr_of_dims;
1956 memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
1957 ctrl->elem_size = elem_size;
1958 if (type == V4L2_CTRL_TYPE_MENU)
1959 ctrl->qmenu = qmenu;
1960 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
1961 ctrl->qmenu_int = qmenu_int;
1963 ctrl->cur.val = ctrl->val = def;
1966 if (ctrl->is_array) {
1967 ctrl->p_array_alloc_elems = elems;
1968 ctrl->p_array = kvzalloc(2 * elems * elem_size, GFP_KERNEL);
1969 if (!ctrl->p_array) {
1973 data = ctrl->p_array;
1976 if (!ctrl->is_int) {
1977 ctrl->p_new.p = data;
1978 ctrl->p_cur.p = data + tot_ctrl_size;
1980 ctrl->p_new.p = &ctrl->val;
1981 ctrl->p_cur.p = &ctrl->cur.val;
1984 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) {
1986 ctrl->p_def.p = &ctrl[1];
1988 ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size;
1989 memcpy(ctrl->p_def.p, p_def.p_const, elem_size);
1992 ctrl->type_ops->init(ctrl, 0, ctrl->p_cur);
1995 if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
1996 kvfree(ctrl->p_array);
2000 mutex_lock(hdl->lock);
2001 list_add_tail(&ctrl->node, &hdl->ctrls);
2002 mutex_unlock(hdl->lock);
2006 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
2007 const struct v4l2_ctrl_config *cfg, void *priv)
2010 struct v4l2_ctrl *ctrl;
2011 const char *name = cfg->name;
2012 const char * const *qmenu = cfg->qmenu;
2013 const s64 *qmenu_int = cfg->qmenu_int;
2014 enum v4l2_ctrl_type type = cfg->type;
2015 u32 flags = cfg->flags;
2018 u64 step = cfg->step;
2022 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
2025 is_menu = (type == V4L2_CTRL_TYPE_MENU ||
2026 type == V4L2_CTRL_TYPE_INTEGER_MENU);
2030 WARN_ON(cfg->menu_skip_mask);
2031 if (type == V4L2_CTRL_TYPE_MENU && !qmenu) {
2032 qmenu = v4l2_ctrl_get_menu(cfg->id);
2033 } else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) {
2034 handler_set_err(hdl, -EINVAL);
2038 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
2040 is_menu ? cfg->menu_skip_mask : step, def,
2041 cfg->dims, cfg->elem_size,
2042 flags, qmenu, qmenu_int, cfg->p_def, priv);
2044 ctrl->is_private = cfg->is_private;
2047 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
2049 /* Helper function for standard non-menu controls */
2050 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
2051 const struct v4l2_ctrl_ops *ops,
2052 u32 id, s64 min, s64 max, u64 step, s64 def)
2055 enum v4l2_ctrl_type type;
2058 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2059 if (type == V4L2_CTRL_TYPE_MENU ||
2060 type == V4L2_CTRL_TYPE_INTEGER_MENU ||
2061 type >= V4L2_CTRL_COMPOUND_TYPES) {
2062 handler_set_err(hdl, -EINVAL);
2065 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2066 min, max, step, def, NULL, 0,
2067 flags, NULL, NULL, ptr_null, NULL);
2069 EXPORT_SYMBOL(v4l2_ctrl_new_std);
2071 /* Helper function for standard menu controls */
2072 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
2073 const struct v4l2_ctrl_ops *ops,
2074 u32 id, u8 _max, u64 mask, u8 _def)
2076 const char * const *qmenu = NULL;
2077 const s64 *qmenu_int = NULL;
2078 unsigned int qmenu_int_len = 0;
2080 enum v4l2_ctrl_type type;
2087 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2089 if (type == V4L2_CTRL_TYPE_MENU)
2090 qmenu = v4l2_ctrl_get_menu(id);
2091 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2092 qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
2094 if ((!qmenu && !qmenu_int) || (qmenu_int && max >= qmenu_int_len)) {
2095 handler_set_err(hdl, -EINVAL);
2098 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2099 0, max, mask, def, NULL, 0,
2100 flags, qmenu, qmenu_int, ptr_null, NULL);
2102 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
2104 /* Helper function for standard menu controls with driver defined menu */
2105 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
2106 const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
2107 u64 mask, u8 _def, const char * const *qmenu)
2109 enum v4l2_ctrl_type type;
2117 /* v4l2_ctrl_new_std_menu_items() should only be called for
2118 * standard controls without a standard menu.
2120 if (v4l2_ctrl_get_menu(id)) {
2121 handler_set_err(hdl, -EINVAL);
2125 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2126 if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
2127 handler_set_err(hdl, -EINVAL);
2130 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2131 0, max, mask, def, NULL, 0,
2132 flags, qmenu, NULL, ptr_null, NULL);
2135 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
2137 /* Helper function for standard compound controls */
2138 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
2139 const struct v4l2_ctrl_ops *ops, u32 id,
2140 const union v4l2_ctrl_ptr p_def)
2143 enum v4l2_ctrl_type type;
2145 s64 min, max, step, def;
2147 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2148 if (type < V4L2_CTRL_COMPOUND_TYPES) {
2149 handler_set_err(hdl, -EINVAL);
2152 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2153 min, max, step, def, NULL, 0,
2154 flags, NULL, NULL, p_def, NULL);
2156 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound);
2158 /* Helper function for standard integer menu controls */
2159 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
2160 const struct v4l2_ctrl_ops *ops,
2161 u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
2164 enum v4l2_ctrl_type type;
2171 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2172 if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
2173 handler_set_err(hdl, -EINVAL);
2176 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2177 0, max, 0, def, NULL, 0,
2178 flags, NULL, qmenu_int, ptr_null, NULL);
2180 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
2182 /* Add the controls from another handler to our own. */
2183 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
2184 struct v4l2_ctrl_handler *add,
2185 bool (*filter)(const struct v4l2_ctrl *ctrl),
2186 bool from_other_dev)
2188 struct v4l2_ctrl_ref *ref;
2191 /* Do nothing if either handler is NULL or if they are the same */
2192 if (!hdl || !add || hdl == add)
2196 mutex_lock(add->lock);
2197 list_for_each_entry(ref, &add->ctrl_refs, node) {
2198 struct v4l2_ctrl *ctrl = ref->ctrl;
2200 /* Skip handler-private controls. */
2201 if (ctrl->is_private)
2203 /* And control classes */
2204 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2206 /* Filter any unwanted controls */
2207 if (filter && !filter(ctrl))
2209 ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
2213 mutex_unlock(add->lock);
2216 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
2218 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
2220 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
2222 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
2225 case V4L2_CID_AUDIO_MUTE:
2226 case V4L2_CID_AUDIO_VOLUME:
2227 case V4L2_CID_AUDIO_BALANCE:
2228 case V4L2_CID_AUDIO_BASS:
2229 case V4L2_CID_AUDIO_TREBLE:
2230 case V4L2_CID_AUDIO_LOUDNESS:
2237 EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
2239 /* Cluster controls */
2240 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
2242 bool has_volatiles = false;
2245 /* The first control is the master control and it must not be NULL */
2246 if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
2249 for (i = 0; i < ncontrols; i++) {
2251 controls[i]->cluster = controls;
2252 controls[i]->ncontrols = ncontrols;
2253 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
2254 has_volatiles = true;
2257 controls[0]->has_volatiles = has_volatiles;
2259 EXPORT_SYMBOL(v4l2_ctrl_cluster);
2261 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
2262 u8 manual_val, bool set_volatile)
2264 struct v4l2_ctrl *master = controls[0];
2268 v4l2_ctrl_cluster(ncontrols, controls);
2269 WARN_ON(ncontrols <= 1);
2270 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
2271 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
2272 master->is_auto = true;
2273 master->has_volatiles = set_volatile;
2274 master->manual_mode_value = manual_val;
2275 master->flags |= V4L2_CTRL_FLAG_UPDATE;
2277 if (!is_cur_manual(master))
2278 flag = V4L2_CTRL_FLAG_INACTIVE |
2279 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
2281 for (i = 1; i < ncontrols; i++)
2283 controls[i]->flags |= flag;
2285 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
2288 * Obtain the current volatile values of an autocluster and mark them
2291 void update_from_auto_cluster(struct v4l2_ctrl *master)
2295 for (i = 1; i < master->ncontrols; i++)
2296 cur_to_new(master->cluster[i]);
2297 if (!call_op(master, g_volatile_ctrl))
2298 for (i = 1; i < master->ncontrols; i++)
2299 if (master->cluster[i])
2300 master->cluster[i]->is_new = 1;
2304 * Return non-zero if one or more of the controls in the cluster has a new
2305 * value that differs from the current value.
2307 static int cluster_changed(struct v4l2_ctrl *master)
2309 bool changed = false;
2312 for (i = 0; i < master->ncontrols; i++) {
2313 struct v4l2_ctrl *ctrl = master->cluster[i];
2314 bool ctrl_changed = false;
2319 if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE) {
2321 ctrl_changed = true;
2325 * Set has_changed to false to avoid generating
2326 * the event V4L2_EVENT_CTRL_CH_VALUE
2328 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
2329 ctrl->has_changed = false;
2333 if (ctrl->elems != ctrl->new_elems)
2334 ctrl_changed = true;
2336 ctrl_changed = !ctrl->type_ops->equal(ctrl,
2337 ctrl->p_cur, ctrl->p_new);
2338 ctrl->has_changed = ctrl_changed;
2339 changed |= ctrl->has_changed;
2345 * Core function that calls try/s_ctrl and ensures that the new value is
2346 * copied to the current value on a set.
2347 * Must be called with ctrl->handler->lock held.
2349 int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
2350 bool set, u32 ch_flags)
2357 * Go through the cluster and either validate the new value or
2358 * (if no new value was set), copy the current value to the new
2359 * value, ensuring a consistent view for the control ops when
2362 for (i = 0; i < master->ncontrols; i++) {
2363 struct v4l2_ctrl *ctrl = master->cluster[i];
2368 if (!ctrl->is_new) {
2373 * Check again: it may have changed since the
2374 * previous check in try_or_set_ext_ctrls().
2376 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2380 ret = call_op(master, try_ctrl);
2382 /* Don't set if there is no change */
2383 if (ret || !set || !cluster_changed(master))
2385 ret = call_op(master, s_ctrl);
2389 /* If OK, then make the new values permanent. */
2390 update_flag = is_cur_manual(master) != is_new_manual(master);
2392 for (i = 0; i < master->ncontrols; i++) {
2394 * If we switch from auto to manual mode, and this cluster
2395 * contains volatile controls, then all non-master controls
2396 * have to be marked as changed. The 'new' value contains
2397 * the volatile value (obtained by update_from_auto_cluster),
2398 * which now has to become the current value.
2400 if (i && update_flag && is_new_manual(master) &&
2401 master->has_volatiles && master->cluster[i])
2402 master->cluster[i]->has_changed = true;
2404 new_to_cur(fh, master->cluster[i], ch_flags |
2405 ((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
2410 /* Activate/deactivate a control. */
2411 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
2413 /* invert since the actual flag is called 'inactive' */
2414 bool inactive = !active;
2421 /* set V4L2_CTRL_FLAG_INACTIVE */
2422 old = test_and_set_bit(4, &ctrl->flags);
2424 /* clear V4L2_CTRL_FLAG_INACTIVE */
2425 old = test_and_clear_bit(4, &ctrl->flags);
2426 if (old != inactive)
2427 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2429 EXPORT_SYMBOL(v4l2_ctrl_activate);
2431 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
2438 lockdep_assert_held(ctrl->handler->lock);
2441 /* set V4L2_CTRL_FLAG_GRABBED */
2442 old = test_and_set_bit(1, &ctrl->flags);
2444 /* clear V4L2_CTRL_FLAG_GRABBED */
2445 old = test_and_clear_bit(1, &ctrl->flags);
2447 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2449 EXPORT_SYMBOL(__v4l2_ctrl_grab);
2451 /* Call s_ctrl for all controls owned by the handler */
2452 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2454 struct v4l2_ctrl *ctrl;
2460 lockdep_assert_held(hdl->lock);
2462 list_for_each_entry(ctrl, &hdl->ctrls, node)
2465 list_for_each_entry(ctrl, &hdl->ctrls, node) {
2466 struct v4l2_ctrl *master = ctrl->cluster[0];
2469 /* Skip if this control was already handled by a cluster. */
2470 /* Skip button controls and read-only controls. */
2471 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
2472 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
2475 for (i = 0; i < master->ncontrols; i++) {
2476 if (master->cluster[i]) {
2477 cur_to_new(master->cluster[i]);
2478 master->cluster[i]->is_new = 1;
2479 master->cluster[i]->done = true;
2482 ret = call_op(master, s_ctrl);
2489 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
2491 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2498 mutex_lock(hdl->lock);
2499 ret = __v4l2_ctrl_handler_setup(hdl);
2500 mutex_unlock(hdl->lock);
2504 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
2506 /* Log the control name and value */
2507 static void log_ctrl(const struct v4l2_ctrl_handler *hdl,
2508 struct v4l2_ctrl *ctrl,
2509 const char *prefix, const char *colon)
2511 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
2513 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2516 pr_info("%s%s%s: ", prefix, colon, ctrl->name);
2518 if (ctrl->handler != hdl)
2519 v4l2_ctrl_lock(ctrl);
2520 ctrl->type_ops->log(ctrl);
2521 if (ctrl->handler != hdl)
2522 v4l2_ctrl_unlock(ctrl);
2524 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
2525 V4L2_CTRL_FLAG_GRABBED |
2526 V4L2_CTRL_FLAG_VOLATILE)) {
2527 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
2528 pr_cont(" inactive");
2529 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
2530 pr_cont(" grabbed");
2531 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
2532 pr_cont(" volatile");
2537 /* Log all controls owned by the handler */
2538 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
2541 struct v4l2_ctrl_ref *ref;
2542 const char *colon = "";
2549 len = strlen(prefix);
2550 if (len && prefix[len - 1] != ' ')
2552 mutex_lock(hdl->lock);
2553 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2554 if (ref->from_other_dev ||
2555 (ref->ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
2557 log_ctrl(hdl, ref->ctrl, prefix, colon);
2559 mutex_unlock(hdl->lock);
2561 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
2563 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,
2564 const struct v4l2_ctrl_ops *ctrl_ops,
2565 const struct v4l2_fwnode_device_properties *p)
2567 if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) {
2568 u32 orientation_ctrl;
2570 switch (p->orientation) {
2571 case V4L2_FWNODE_ORIENTATION_FRONT:
2572 orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT;
2574 case V4L2_FWNODE_ORIENTATION_BACK:
2575 orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK;
2577 case V4L2_FWNODE_ORIENTATION_EXTERNAL:
2578 orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL;
2583 if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops,
2584 V4L2_CID_CAMERA_ORIENTATION,
2585 V4L2_CAMERA_ORIENTATION_EXTERNAL, 0,
2590 if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) {
2591 if (!v4l2_ctrl_new_std(hdl, ctrl_ops,
2592 V4L2_CID_CAMERA_SENSOR_ROTATION,
2593 p->rotation, p->rotation, 1,
2600 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties);