1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * V4L2 controls framework core implementation.
5 * Copyright (C) 2010-2021 Hans Verkuil <hverkuil-cisco@xs4all.nl>
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 static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx,
69 union v4l2_ctrl_ptr ptr1,
70 union v4l2_ctrl_ptr ptr2)
73 case V4L2_CTRL_TYPE_BUTTON:
75 case V4L2_CTRL_TYPE_STRING:
76 idx *= ctrl->elem_size;
77 /* strings are always 0-terminated */
78 return !strcmp(ptr1.p_char + idx, ptr2.p_char + idx);
79 case V4L2_CTRL_TYPE_INTEGER64:
80 return ptr1.p_s64[idx] == ptr2.p_s64[idx];
81 case V4L2_CTRL_TYPE_U8:
82 return ptr1.p_u8[idx] == ptr2.p_u8[idx];
83 case V4L2_CTRL_TYPE_U16:
84 return ptr1.p_u16[idx] == ptr2.p_u16[idx];
85 case V4L2_CTRL_TYPE_U32:
86 return ptr1.p_u32[idx] == ptr2.p_u32[idx];
89 return ptr1.p_s32[idx] == ptr2.p_s32[idx];
90 idx *= ctrl->elem_size;
91 return !memcmp(ptr1.p_const + idx, ptr2.p_const + idx,
96 /* Default intra MPEG-2 quantisation coefficients, from the specification. */
97 static const u8 mpeg2_intra_quant_matrix[64] = {
98 8, 16, 16, 19, 16, 19, 22, 22,
99 22, 22, 22, 22, 26, 24, 26, 27,
100 27, 27, 26, 26, 26, 26, 27, 27,
101 27, 29, 29, 29, 34, 34, 34, 29,
102 29, 29, 27, 27, 29, 29, 32, 32,
103 34, 34, 37, 38, 37, 35, 35, 34,
104 35, 38, 38, 40, 40, 40, 48, 48,
105 46, 46, 56, 56, 58, 69, 69, 83
108 static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx,
109 union v4l2_ctrl_ptr ptr)
111 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence;
112 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture;
113 struct v4l2_ctrl_mpeg2_quantisation *p_mpeg2_quant;
114 struct v4l2_ctrl_vp8_frame *p_vp8_frame;
115 struct v4l2_ctrl_fwht_params *p_fwht_params;
116 void *p = ptr.p + idx * ctrl->elem_size;
118 if (ctrl->p_def.p_const)
119 memcpy(p, ctrl->p_def.p_const, ctrl->elem_size);
121 memset(p, 0, ctrl->elem_size);
123 switch ((u32)ctrl->type) {
124 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
125 p_mpeg2_sequence = p;
128 p_mpeg2_sequence->chroma_format = 1;
130 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
133 /* interlaced top field */
134 p_mpeg2_picture->picture_structure = V4L2_MPEG2_PIC_TOP_FIELD;
135 p_mpeg2_picture->picture_coding_type =
136 V4L2_MPEG2_PIC_CODING_TYPE_I;
138 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
141 memcpy(p_mpeg2_quant->intra_quantiser_matrix,
142 mpeg2_intra_quant_matrix,
143 ARRAY_SIZE(mpeg2_intra_quant_matrix));
145 * The default non-intra MPEG-2 quantisation
146 * coefficients are all 16, as per the specification.
148 memset(p_mpeg2_quant->non_intra_quantiser_matrix, 16,
149 sizeof(p_mpeg2_quant->non_intra_quantiser_matrix));
151 case V4L2_CTRL_TYPE_VP8_FRAME:
153 p_vp8_frame->num_dct_parts = 1;
155 case V4L2_CTRL_TYPE_FWHT_PARAMS:
157 p_fwht_params->version = V4L2_FWHT_VERSION;
158 p_fwht_params->width = 1280;
159 p_fwht_params->height = 720;
160 p_fwht_params->flags = V4L2_FWHT_FL_PIXENC_YUV |
161 (2 << V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET);
166 static void std_init(const struct v4l2_ctrl *ctrl, u32 idx,
167 union v4l2_ctrl_ptr ptr)
169 switch (ctrl->type) {
170 case V4L2_CTRL_TYPE_STRING:
171 idx *= ctrl->elem_size;
172 memset(ptr.p_char + idx, ' ', ctrl->minimum);
173 ptr.p_char[idx + ctrl->minimum] = '\0';
175 case V4L2_CTRL_TYPE_INTEGER64:
176 ptr.p_s64[idx] = ctrl->default_value;
178 case V4L2_CTRL_TYPE_INTEGER:
179 case V4L2_CTRL_TYPE_INTEGER_MENU:
180 case V4L2_CTRL_TYPE_MENU:
181 case V4L2_CTRL_TYPE_BITMASK:
182 case V4L2_CTRL_TYPE_BOOLEAN:
183 ptr.p_s32[idx] = ctrl->default_value;
185 case V4L2_CTRL_TYPE_BUTTON:
186 case V4L2_CTRL_TYPE_CTRL_CLASS:
189 case V4L2_CTRL_TYPE_U8:
190 ptr.p_u8[idx] = ctrl->default_value;
192 case V4L2_CTRL_TYPE_U16:
193 ptr.p_u16[idx] = ctrl->default_value;
195 case V4L2_CTRL_TYPE_U32:
196 ptr.p_u32[idx] = ctrl->default_value;
199 std_init_compound(ctrl, idx, ptr);
204 static void std_log(const struct v4l2_ctrl *ctrl)
206 union v4l2_ctrl_ptr ptr = ctrl->p_cur;
208 if (ctrl->is_array) {
211 for (i = 0; i < ctrl->nr_of_dims; i++)
212 pr_cont("[%u]", ctrl->dims[i]);
216 switch (ctrl->type) {
217 case V4L2_CTRL_TYPE_INTEGER:
218 pr_cont("%d", *ptr.p_s32);
220 case V4L2_CTRL_TYPE_BOOLEAN:
221 pr_cont("%s", *ptr.p_s32 ? "true" : "false");
223 case V4L2_CTRL_TYPE_MENU:
224 pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
226 case V4L2_CTRL_TYPE_INTEGER_MENU:
227 pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
229 case V4L2_CTRL_TYPE_BITMASK:
230 pr_cont("0x%08x", *ptr.p_s32);
232 case V4L2_CTRL_TYPE_INTEGER64:
233 pr_cont("%lld", *ptr.p_s64);
235 case V4L2_CTRL_TYPE_STRING:
236 pr_cont("%s", ptr.p_char);
238 case V4L2_CTRL_TYPE_U8:
239 pr_cont("%u", (unsigned)*ptr.p_u8);
241 case V4L2_CTRL_TYPE_U16:
242 pr_cont("%u", (unsigned)*ptr.p_u16);
244 case V4L2_CTRL_TYPE_U32:
245 pr_cont("%u", (unsigned)*ptr.p_u32);
247 case V4L2_CTRL_TYPE_H264_SPS:
250 case V4L2_CTRL_TYPE_H264_PPS:
253 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
254 pr_cont("H264_SCALING_MATRIX");
256 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
257 pr_cont("H264_SLICE_PARAMS");
259 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
260 pr_cont("H264_DECODE_PARAMS");
262 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
263 pr_cont("H264_PRED_WEIGHTS");
265 case V4L2_CTRL_TYPE_FWHT_PARAMS:
266 pr_cont("FWHT_PARAMS");
268 case V4L2_CTRL_TYPE_VP8_FRAME:
269 pr_cont("VP8_FRAME");
271 case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
272 pr_cont("HDR10_CLL_INFO");
274 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
275 pr_cont("HDR10_MASTERING_DISPLAY");
277 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
278 pr_cont("MPEG2_QUANTISATION");
280 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
281 pr_cont("MPEG2_SEQUENCE");
283 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
284 pr_cont("MPEG2_PICTURE");
287 pr_cont("unknown type %d", ctrl->type);
293 * Round towards the closest legal value. Be careful when we are
294 * close to the maximum range of the control type to prevent
297 #define ROUND_TO_RANGE(val, offset_type, ctrl) \
299 offset_type offset; \
300 if ((ctrl)->maximum >= 0 && \
301 val >= (ctrl)->maximum - (s32)((ctrl)->step / 2)) \
302 val = (ctrl)->maximum; \
304 val += (s32)((ctrl)->step / 2); \
305 val = clamp_t(typeof(val), val, \
306 (ctrl)->minimum, (ctrl)->maximum); \
307 offset = (val) - (ctrl)->minimum; \
308 offset = (ctrl)->step * (offset / (u32)(ctrl)->step); \
309 val = (ctrl)->minimum + offset; \
313 /* Validate a new control */
315 #define zero_padding(s) \
316 memset(&(s).padding, 0, sizeof((s).padding))
317 #define zero_reserved(s) \
318 memset(&(s).reserved, 0, sizeof((s).reserved))
321 * Compound controls validation requires setting unused fields/flags to zero
322 * in order to properly detect unchanged controls with std_equal's memcmp.
324 static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
325 union v4l2_ctrl_ptr ptr)
327 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence;
328 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture;
329 struct v4l2_ctrl_vp8_frame *p_vp8_frame;
330 struct v4l2_ctrl_fwht_params *p_fwht_params;
331 struct v4l2_ctrl_h264_sps *p_h264_sps;
332 struct v4l2_ctrl_h264_pps *p_h264_pps;
333 struct v4l2_ctrl_h264_pred_weights *p_h264_pred_weights;
334 struct v4l2_ctrl_h264_slice_params *p_h264_slice_params;
335 struct v4l2_ctrl_h264_decode_params *p_h264_dec_params;
336 struct v4l2_ctrl_hevc_sps *p_hevc_sps;
337 struct v4l2_ctrl_hevc_pps *p_hevc_pps;
338 struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params;
339 struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering;
340 struct v4l2_ctrl_hevc_decode_params *p_hevc_decode_params;
341 struct v4l2_area *area;
342 void *p = ptr.p + idx * ctrl->elem_size;
345 switch ((u32)ctrl->type) {
346 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
347 p_mpeg2_sequence = p;
349 switch (p_mpeg2_sequence->chroma_format) {
359 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
362 switch (p_mpeg2_picture->intra_dc_precision) {
365 case 2: /* 10 bits */
366 case 3: /* 11 bits */
372 switch (p_mpeg2_picture->picture_structure) {
373 case V4L2_MPEG2_PIC_TOP_FIELD:
374 case V4L2_MPEG2_PIC_BOTTOM_FIELD:
375 case V4L2_MPEG2_PIC_FRAME:
381 switch (p_mpeg2_picture->picture_coding_type) {
382 case V4L2_MPEG2_PIC_CODING_TYPE_I:
383 case V4L2_MPEG2_PIC_CODING_TYPE_P:
384 case V4L2_MPEG2_PIC_CODING_TYPE_B:
389 zero_reserved(*p_mpeg2_picture);
392 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
395 case V4L2_CTRL_TYPE_FWHT_PARAMS:
397 if (p_fwht_params->version < V4L2_FWHT_VERSION)
399 if (!p_fwht_params->width || !p_fwht_params->height)
403 case V4L2_CTRL_TYPE_H264_SPS:
406 /* Some syntax elements are only conditionally valid */
407 if (p_h264_sps->pic_order_cnt_type != 0) {
408 p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 = 0;
409 } else if (p_h264_sps->pic_order_cnt_type != 1) {
410 p_h264_sps->num_ref_frames_in_pic_order_cnt_cycle = 0;
411 p_h264_sps->offset_for_non_ref_pic = 0;
412 p_h264_sps->offset_for_top_to_bottom_field = 0;
413 memset(&p_h264_sps->offset_for_ref_frame, 0,
414 sizeof(p_h264_sps->offset_for_ref_frame));
417 if (!V4L2_H264_SPS_HAS_CHROMA_FORMAT(p_h264_sps)) {
418 p_h264_sps->chroma_format_idc = 1;
419 p_h264_sps->bit_depth_luma_minus8 = 0;
420 p_h264_sps->bit_depth_chroma_minus8 = 0;
423 ~V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS;
425 if (p_h264_sps->chroma_format_idc < 3)
427 ~V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE;
430 if (p_h264_sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)
432 ~V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD;
435 * Chroma 4:2:2 format require at least High 4:2:2 profile.
437 * The H264 specification and well-known parser implementations
438 * use profile-idc values directly, as that is clearer and
439 * less ambiguous. We do the same here.
441 if (p_h264_sps->profile_idc < 122 &&
442 p_h264_sps->chroma_format_idc > 1)
444 /* Chroma 4:4:4 format require at least High 4:2:2 profile */
445 if (p_h264_sps->profile_idc < 244 &&
446 p_h264_sps->chroma_format_idc > 2)
448 if (p_h264_sps->chroma_format_idc > 3)
451 if (p_h264_sps->bit_depth_luma_minus8 > 6)
453 if (p_h264_sps->bit_depth_chroma_minus8 > 6)
455 if (p_h264_sps->log2_max_frame_num_minus4 > 12)
457 if (p_h264_sps->pic_order_cnt_type > 2)
459 if (p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 > 12)
461 if (p_h264_sps->max_num_ref_frames > V4L2_H264_REF_LIST_LEN)
465 case V4L2_CTRL_TYPE_H264_PPS:
468 if (p_h264_pps->num_slice_groups_minus1 > 7)
470 if (p_h264_pps->num_ref_idx_l0_default_active_minus1 >
471 (V4L2_H264_REF_LIST_LEN - 1))
473 if (p_h264_pps->num_ref_idx_l1_default_active_minus1 >
474 (V4L2_H264_REF_LIST_LEN - 1))
476 if (p_h264_pps->weighted_bipred_idc > 2)
479 * pic_init_qp_minus26 shall be in the range of
480 * -(26 + QpBdOffset_y) to +25, inclusive,
481 * where QpBdOffset_y is 6 * bit_depth_luma_minus8
483 if (p_h264_pps->pic_init_qp_minus26 < -62 ||
484 p_h264_pps->pic_init_qp_minus26 > 25)
486 if (p_h264_pps->pic_init_qs_minus26 < -26 ||
487 p_h264_pps->pic_init_qs_minus26 > 25)
489 if (p_h264_pps->chroma_qp_index_offset < -12 ||
490 p_h264_pps->chroma_qp_index_offset > 12)
492 if (p_h264_pps->second_chroma_qp_index_offset < -12 ||
493 p_h264_pps->second_chroma_qp_index_offset > 12)
497 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
500 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
501 p_h264_pred_weights = p;
503 if (p_h264_pred_weights->luma_log2_weight_denom > 7)
505 if (p_h264_pred_weights->chroma_log2_weight_denom > 7)
509 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
510 p_h264_slice_params = p;
512 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
513 p_h264_slice_params->flags &=
514 ~V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED;
516 if (p_h264_slice_params->colour_plane_id > 2)
518 if (p_h264_slice_params->cabac_init_idc > 2)
520 if (p_h264_slice_params->disable_deblocking_filter_idc > 2)
522 if (p_h264_slice_params->slice_alpha_c0_offset_div2 < -6 ||
523 p_h264_slice_params->slice_alpha_c0_offset_div2 > 6)
525 if (p_h264_slice_params->slice_beta_offset_div2 < -6 ||
526 p_h264_slice_params->slice_beta_offset_div2 > 6)
529 if (p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_I ||
530 p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_SI)
531 p_h264_slice_params->num_ref_idx_l0_active_minus1 = 0;
532 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
533 p_h264_slice_params->num_ref_idx_l1_active_minus1 = 0;
535 if (p_h264_slice_params->num_ref_idx_l0_active_minus1 >
536 (V4L2_H264_REF_LIST_LEN - 1))
538 if (p_h264_slice_params->num_ref_idx_l1_active_minus1 >
539 (V4L2_H264_REF_LIST_LEN - 1))
541 zero_reserved(*p_h264_slice_params);
544 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
545 p_h264_dec_params = p;
547 if (p_h264_dec_params->nal_ref_idc > 3)
549 for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
550 struct v4l2_h264_dpb_entry *dpb_entry =
551 &p_h264_dec_params->dpb[i];
553 zero_reserved(*dpb_entry);
555 zero_reserved(*p_h264_dec_params);
558 case V4L2_CTRL_TYPE_VP8_FRAME:
561 switch (p_vp8_frame->num_dct_parts) {
570 zero_padding(p_vp8_frame->segment);
571 zero_padding(p_vp8_frame->lf);
572 zero_padding(p_vp8_frame->quant);
573 zero_padding(p_vp8_frame->entropy);
574 zero_padding(p_vp8_frame->coder_state);
577 case V4L2_CTRL_TYPE_HEVC_SPS:
580 if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) {
581 p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0;
582 p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;
583 p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0;
584 p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0;
587 if (!(p_hevc_sps->flags &
588 V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT))
589 p_hevc_sps->num_long_term_ref_pics_sps = 0;
592 case V4L2_CTRL_TYPE_HEVC_PPS:
595 if (!(p_hevc_pps->flags &
596 V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED))
597 p_hevc_pps->diff_cu_qp_delta_depth = 0;
599 if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
600 p_hevc_pps->num_tile_columns_minus1 = 0;
601 p_hevc_pps->num_tile_rows_minus1 = 0;
602 memset(&p_hevc_pps->column_width_minus1, 0,
603 sizeof(p_hevc_pps->column_width_minus1));
604 memset(&p_hevc_pps->row_height_minus1, 0,
605 sizeof(p_hevc_pps->row_height_minus1));
608 ~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED;
611 if (p_hevc_pps->flags &
612 V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) {
613 p_hevc_pps->pps_beta_offset_div2 = 0;
614 p_hevc_pps->pps_tc_offset_div2 = 0;
617 zero_padding(*p_hevc_pps);
620 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
621 p_hevc_decode_params = p;
623 if (p_hevc_decode_params->num_active_dpb_entries >
624 V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
627 for (i = 0; i < p_hevc_decode_params->num_active_dpb_entries;
629 struct v4l2_hevc_dpb_entry *dpb_entry =
630 &p_hevc_decode_params->dpb[i];
632 zero_padding(*dpb_entry);
636 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
637 p_hevc_slice_params = p;
639 zero_padding(p_hevc_slice_params->pred_weight_table);
640 zero_padding(*p_hevc_slice_params);
643 case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
646 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
647 p_hdr10_mastering = p;
649 for (i = 0; i < 3; ++i) {
650 if (p_hdr10_mastering->display_primaries_x[i] <
651 V4L2_HDR10_MASTERING_PRIMARIES_X_LOW ||
652 p_hdr10_mastering->display_primaries_x[i] >
653 V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH ||
654 p_hdr10_mastering->display_primaries_y[i] <
655 V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW ||
656 p_hdr10_mastering->display_primaries_y[i] >
657 V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH)
661 if (p_hdr10_mastering->white_point_x <
662 V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW ||
663 p_hdr10_mastering->white_point_x >
664 V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH ||
665 p_hdr10_mastering->white_point_y <
666 V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW ||
667 p_hdr10_mastering->white_point_y >
668 V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH)
671 if (p_hdr10_mastering->max_display_mastering_luminance <
672 V4L2_HDR10_MASTERING_MAX_LUMA_LOW ||
673 p_hdr10_mastering->max_display_mastering_luminance >
674 V4L2_HDR10_MASTERING_MAX_LUMA_HIGH ||
675 p_hdr10_mastering->min_display_mastering_luminance <
676 V4L2_HDR10_MASTERING_MIN_LUMA_LOW ||
677 p_hdr10_mastering->min_display_mastering_luminance >
678 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
681 /* The following restriction comes from ITU-T Rec. H.265 spec */
682 if (p_hdr10_mastering->max_display_mastering_luminance ==
683 V4L2_HDR10_MASTERING_MAX_LUMA_LOW &&
684 p_hdr10_mastering->min_display_mastering_luminance ==
685 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
690 case V4L2_CTRL_TYPE_AREA:
692 if (!area->width || !area->height)
703 static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx,
704 union v4l2_ctrl_ptr ptr)
710 switch ((u32)ctrl->type) {
711 case V4L2_CTRL_TYPE_INTEGER:
712 return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
713 case V4L2_CTRL_TYPE_INTEGER64:
715 * We can't use the ROUND_TO_RANGE define here due to
716 * the u64 divide that needs special care.
718 val = ptr.p_s64[idx];
719 if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
722 val += (s64)(ctrl->step / 2);
723 val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
724 offset = val - ctrl->minimum;
725 do_div(offset, ctrl->step);
726 ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
728 case V4L2_CTRL_TYPE_U8:
729 return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
730 case V4L2_CTRL_TYPE_U16:
731 return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
732 case V4L2_CTRL_TYPE_U32:
733 return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
735 case V4L2_CTRL_TYPE_BOOLEAN:
736 ptr.p_s32[idx] = !!ptr.p_s32[idx];
739 case V4L2_CTRL_TYPE_MENU:
740 case V4L2_CTRL_TYPE_INTEGER_MENU:
741 if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
743 if (ptr.p_s32[idx] < BITS_PER_LONG_LONG &&
744 (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx])))
746 if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
747 ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
751 case V4L2_CTRL_TYPE_BITMASK:
752 ptr.p_s32[idx] &= ctrl->maximum;
755 case V4L2_CTRL_TYPE_BUTTON:
756 case V4L2_CTRL_TYPE_CTRL_CLASS:
760 case V4L2_CTRL_TYPE_STRING:
761 idx *= ctrl->elem_size;
762 len = strlen(ptr.p_char + idx);
763 if (len < ctrl->minimum)
765 if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
770 return std_validate_compound(ctrl, idx, ptr);
774 static const struct v4l2_ctrl_type_ops std_type_ops = {
778 .validate = std_validate,
781 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
786 ctrl->call_notify = 0;
789 if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
791 ctrl->handler->notify = notify;
792 ctrl->handler->notify_priv = priv;
793 ctrl->call_notify = 1;
795 EXPORT_SYMBOL(v4l2_ctrl_notify);
797 /* Copy the one value to another. */
798 static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
799 union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to)
803 memcpy(to.p, from.p_const, ctrl->elems * ctrl->elem_size);
806 /* Copy the new value to the current value. */
807 void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
814 /* has_changed is set by cluster_changed */
815 changed = ctrl->has_changed;
817 ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur);
819 if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
820 /* Note: CH_FLAGS is only set for auto clusters. */
822 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
823 if (!is_cur_manual(ctrl->cluster[0])) {
824 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
825 if (ctrl->cluster[0]->has_volatiles)
826 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
830 if (changed || ch_flags) {
831 /* If a control was changed that was not one of the controls
832 modified by the application, then send the event to all. */
836 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
837 if (ctrl->call_notify && changed && ctrl->handler->notify)
838 ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
842 /* Copy the current value to the new value */
843 void cur_to_new(struct v4l2_ctrl *ctrl)
847 ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new);
850 /* Copy the new value to the request value */
851 void new_to_req(struct v4l2_ctrl_ref *ref)
855 ptr_to_ptr(ref->ctrl, ref->ctrl->p_new, ref->p_req);
856 ref->valid_p_req = true;
859 /* Copy the current value to the request value */
860 void cur_to_req(struct v4l2_ctrl_ref *ref)
864 ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->p_req);
865 ref->valid_p_req = true;
868 /* Copy the request value to the new value */
869 void req_to_new(struct v4l2_ctrl_ref *ref)
873 if (ref->valid_p_req)
874 ptr_to_ptr(ref->ctrl, ref->p_req, ref->ctrl->p_new);
876 ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->ctrl->p_new);
879 /* Control range checking */
880 int check_range(enum v4l2_ctrl_type type,
881 s64 min, s64 max, u64 step, s64 def)
884 case V4L2_CTRL_TYPE_BOOLEAN:
885 if (step != 1 || max > 1 || min < 0)
888 case V4L2_CTRL_TYPE_U8:
889 case V4L2_CTRL_TYPE_U16:
890 case V4L2_CTRL_TYPE_U32:
891 case V4L2_CTRL_TYPE_INTEGER:
892 case V4L2_CTRL_TYPE_INTEGER64:
893 if (step == 0 || min > max || def < min || def > max)
896 case V4L2_CTRL_TYPE_BITMASK:
897 if (step || min || !max || (def & ~max))
900 case V4L2_CTRL_TYPE_MENU:
901 case V4L2_CTRL_TYPE_INTEGER_MENU:
902 if (min > max || def < min || def > max)
904 /* Note: step == menu_skip_mask for menu controls.
905 So here we check if the default value is masked out. */
906 if (step && ((1 << def) & step))
909 case V4L2_CTRL_TYPE_STRING:
910 if (min > max || min < 0 || step < 1 || def)
918 /* Validate a new control */
919 int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
924 for (idx = 0; !err && idx < ctrl->elems; idx++)
925 err = ctrl->type_ops->validate(ctrl, idx, p_new);
929 /* Set the handler's error code if it wasn't set earlier already */
930 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
937 /* Initialize the handler */
938 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
939 unsigned nr_of_controls_hint,
940 struct lock_class_key *key, const char *name)
942 mutex_init(&hdl->_lock);
943 hdl->lock = &hdl->_lock;
944 lockdep_set_class_and_name(hdl->lock, key, name);
945 INIT_LIST_HEAD(&hdl->ctrls);
946 INIT_LIST_HEAD(&hdl->ctrl_refs);
947 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
948 hdl->buckets = kvmalloc_array(hdl->nr_of_buckets,
949 sizeof(hdl->buckets[0]),
950 GFP_KERNEL | __GFP_ZERO);
951 hdl->error = hdl->buckets ? 0 : -ENOMEM;
952 v4l2_ctrl_handler_init_request(hdl);
955 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
957 /* Free all controls and control refs */
958 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
960 struct v4l2_ctrl_ref *ref, *next_ref;
961 struct v4l2_ctrl *ctrl, *next_ctrl;
962 struct v4l2_subscribed_event *sev, *next_sev;
964 if (hdl == NULL || hdl->buckets == NULL)
967 v4l2_ctrl_handler_free_request(hdl);
969 mutex_lock(hdl->lock);
971 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
972 list_del(&ref->node);
975 /* Free all controls owned by the handler */
976 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
977 list_del(&ctrl->node);
978 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
979 list_del(&sev->node);
982 kvfree(hdl->buckets);
986 mutex_unlock(hdl->lock);
987 mutex_destroy(&hdl->_lock);
989 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
991 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
992 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
993 with applications that do not use the NEXT_CTRL flag.
995 We just find the n-th private user control. It's O(N), but that should not
996 be an issue in this particular case. */
997 static struct v4l2_ctrl_ref *find_private_ref(
998 struct v4l2_ctrl_handler *hdl, u32 id)
1000 struct v4l2_ctrl_ref *ref;
1002 id -= V4L2_CID_PRIVATE_BASE;
1003 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1004 /* Search for private user controls that are compatible with
1006 if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1007 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1008 if (!ref->ctrl->is_int)
1018 /* Find a control with the given ID. */
1019 struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1021 struct v4l2_ctrl_ref *ref;
1024 id &= V4L2_CTRL_ID_MASK;
1026 /* Old-style private controls need special handling */
1027 if (id >= V4L2_CID_PRIVATE_BASE)
1028 return find_private_ref(hdl, id);
1029 bucket = id % hdl->nr_of_buckets;
1031 /* Simple optimization: cache the last control found */
1032 if (hdl->cached && hdl->cached->ctrl->id == id)
1035 /* Not in cache, search the hash */
1036 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1037 while (ref && ref->ctrl->id != id)
1041 hdl->cached = ref; /* cache it! */
1045 /* Find a control with the given ID. Take the handler's lock first. */
1046 struct v4l2_ctrl_ref *find_ref_lock(struct v4l2_ctrl_handler *hdl, u32 id)
1048 struct v4l2_ctrl_ref *ref = NULL;
1051 mutex_lock(hdl->lock);
1052 ref = find_ref(hdl, id);
1053 mutex_unlock(hdl->lock);
1058 /* Find a control with the given ID. */
1059 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1061 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1063 return ref ? ref->ctrl : NULL;
1065 EXPORT_SYMBOL(v4l2_ctrl_find);
1067 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1068 int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1069 struct v4l2_ctrl *ctrl,
1070 struct v4l2_ctrl_ref **ctrl_ref,
1071 bool from_other_dev, bool allocate_req)
1073 struct v4l2_ctrl_ref *ref;
1074 struct v4l2_ctrl_ref *new_ref;
1076 u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
1077 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
1078 unsigned int size_extra_req = 0;
1084 * Automatically add the control class if it is not yet present and
1085 * the new control is not a compound control.
1087 if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
1088 id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1089 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1096 size_extra_req = ctrl->elems * ctrl->elem_size;
1097 new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
1099 return handler_set_err(hdl, -ENOMEM);
1100 new_ref->ctrl = ctrl;
1101 new_ref->from_other_dev = from_other_dev;
1103 new_ref->p_req.p = &new_ref[1];
1105 INIT_LIST_HEAD(&new_ref->node);
1107 mutex_lock(hdl->lock);
1109 /* Add immediately at the end of the list if the list is empty, or if
1110 the last element in the list has a lower ID.
1111 This ensures that when elements are added in ascending order the
1112 insertion is an O(1) operation. */
1113 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1114 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1115 goto insert_in_hash;
1118 /* Find insert position in sorted list */
1119 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1120 if (ref->ctrl->id < id)
1122 /* Don't add duplicates */
1123 if (ref->ctrl->id == id) {
1127 list_add(&new_ref->node, ref->node.prev);
1132 /* Insert the control node in the hash */
1133 new_ref->next = hdl->buckets[bucket];
1134 hdl->buckets[bucket] = new_ref;
1136 *ctrl_ref = new_ref;
1137 if (ctrl->handler == hdl) {
1138 /* By default each control starts in a cluster of its own.
1139 * new_ref->ctrl is basically a cluster array with one
1140 * element, so that's perfect to use as the cluster pointer.
1141 * But only do this for the handler that owns the control.
1143 ctrl->cluster = &new_ref->ctrl;
1144 ctrl->ncontrols = 1;
1148 mutex_unlock(hdl->lock);
1152 /* Add a new control */
1153 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1154 const struct v4l2_ctrl_ops *ops,
1155 const struct v4l2_ctrl_type_ops *type_ops,
1156 u32 id, const char *name, enum v4l2_ctrl_type type,
1157 s64 min, s64 max, u64 step, s64 def,
1158 const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
1159 u32 flags, const char * const *qmenu,
1160 const s64 *qmenu_int, const union v4l2_ctrl_ptr p_def,
1163 struct v4l2_ctrl *ctrl;
1165 unsigned nr_of_dims = 0;
1168 unsigned tot_ctrl_size;
1176 while (dims && dims[nr_of_dims]) {
1177 elems *= dims[nr_of_dims];
1179 if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
1182 is_array = nr_of_dims > 0;
1184 /* Prefill elem_size for all types handled by std_type_ops */
1185 switch ((u32)type) {
1186 case V4L2_CTRL_TYPE_INTEGER64:
1187 elem_size = sizeof(s64);
1189 case V4L2_CTRL_TYPE_STRING:
1190 elem_size = max + 1;
1192 case V4L2_CTRL_TYPE_U8:
1193 elem_size = sizeof(u8);
1195 case V4L2_CTRL_TYPE_U16:
1196 elem_size = sizeof(u16);
1198 case V4L2_CTRL_TYPE_U32:
1199 elem_size = sizeof(u32);
1201 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
1202 elem_size = sizeof(struct v4l2_ctrl_mpeg2_sequence);
1204 case V4L2_CTRL_TYPE_MPEG2_PICTURE:
1205 elem_size = sizeof(struct v4l2_ctrl_mpeg2_picture);
1207 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
1208 elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantisation);
1210 case V4L2_CTRL_TYPE_FWHT_PARAMS:
1211 elem_size = sizeof(struct v4l2_ctrl_fwht_params);
1213 case V4L2_CTRL_TYPE_H264_SPS:
1214 elem_size = sizeof(struct v4l2_ctrl_h264_sps);
1216 case V4L2_CTRL_TYPE_H264_PPS:
1217 elem_size = sizeof(struct v4l2_ctrl_h264_pps);
1219 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
1220 elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix);
1222 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
1223 elem_size = sizeof(struct v4l2_ctrl_h264_slice_params);
1225 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
1226 elem_size = sizeof(struct v4l2_ctrl_h264_decode_params);
1228 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
1229 elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights);
1231 case V4L2_CTRL_TYPE_VP8_FRAME:
1232 elem_size = sizeof(struct v4l2_ctrl_vp8_frame);
1234 case V4L2_CTRL_TYPE_HEVC_SPS:
1235 elem_size = sizeof(struct v4l2_ctrl_hevc_sps);
1237 case V4L2_CTRL_TYPE_HEVC_PPS:
1238 elem_size = sizeof(struct v4l2_ctrl_hevc_pps);
1240 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
1241 elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params);
1243 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
1244 elem_size = sizeof(struct v4l2_ctrl_hevc_decode_params);
1246 case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
1247 elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info);
1249 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
1250 elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display);
1252 case V4L2_CTRL_TYPE_AREA:
1253 elem_size = sizeof(struct v4l2_area);
1256 if (type < V4L2_CTRL_COMPOUND_TYPES)
1257 elem_size = sizeof(s32);
1260 tot_ctrl_size = elem_size * elems;
1263 if (id == 0 || name == NULL || !elem_size ||
1264 id >= V4L2_CID_PRIVATE_BASE ||
1265 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1266 (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
1267 handler_set_err(hdl, -ERANGE);
1270 err = check_range(type, min, max, step, def);
1272 handler_set_err(hdl, err);
1276 (type == V4L2_CTRL_TYPE_BUTTON ||
1277 type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
1278 handler_set_err(hdl, -EINVAL);
1283 if (type == V4L2_CTRL_TYPE_BUTTON)
1284 flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1285 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1286 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1287 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1288 else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
1289 type == V4L2_CTRL_TYPE_STRING ||
1290 type >= V4L2_CTRL_COMPOUND_TYPES ||
1292 sz_extra += 2 * tot_ctrl_size;
1294 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const)
1295 sz_extra += elem_size;
1297 ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1299 handler_set_err(hdl, -ENOMEM);
1303 INIT_LIST_HEAD(&ctrl->node);
1304 INIT_LIST_HEAD(&ctrl->ev_subs);
1305 ctrl->handler = hdl;
1307 ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
1311 ctrl->flags = flags;
1312 ctrl->minimum = min;
1313 ctrl->maximum = max;
1315 ctrl->default_value = def;
1316 ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
1317 ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
1318 ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
1319 ctrl->is_array = is_array;
1320 ctrl->elems = elems;
1321 ctrl->nr_of_dims = nr_of_dims;
1323 memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
1324 ctrl->elem_size = elem_size;
1325 if (type == V4L2_CTRL_TYPE_MENU)
1326 ctrl->qmenu = qmenu;
1327 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
1328 ctrl->qmenu_int = qmenu_int;
1330 ctrl->cur.val = ctrl->val = def;
1333 if (!ctrl->is_int) {
1334 ctrl->p_new.p = data;
1335 ctrl->p_cur.p = data + tot_ctrl_size;
1337 ctrl->p_new.p = &ctrl->val;
1338 ctrl->p_cur.p = &ctrl->cur.val;
1341 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) {
1342 ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size;
1343 memcpy(ctrl->p_def.p, p_def.p_const, elem_size);
1346 for (idx = 0; idx < elems; idx++) {
1347 ctrl->type_ops->init(ctrl, idx, ctrl->p_cur);
1348 ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
1351 if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
1355 mutex_lock(hdl->lock);
1356 list_add_tail(&ctrl->node, &hdl->ctrls);
1357 mutex_unlock(hdl->lock);
1361 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1362 const struct v4l2_ctrl_config *cfg, void *priv)
1365 struct v4l2_ctrl *ctrl;
1366 const char *name = cfg->name;
1367 const char * const *qmenu = cfg->qmenu;
1368 const s64 *qmenu_int = cfg->qmenu_int;
1369 enum v4l2_ctrl_type type = cfg->type;
1370 u32 flags = cfg->flags;
1373 u64 step = cfg->step;
1377 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1380 is_menu = (type == V4L2_CTRL_TYPE_MENU ||
1381 type == V4L2_CTRL_TYPE_INTEGER_MENU);
1385 WARN_ON(cfg->menu_skip_mask);
1386 if (type == V4L2_CTRL_TYPE_MENU && !qmenu) {
1387 qmenu = v4l2_ctrl_get_menu(cfg->id);
1388 } else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) {
1389 handler_set_err(hdl, -EINVAL);
1393 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
1395 is_menu ? cfg->menu_skip_mask : step, def,
1396 cfg->dims, cfg->elem_size,
1397 flags, qmenu, qmenu_int, cfg->p_def, priv);
1399 ctrl->is_private = cfg->is_private;
1402 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1404 /* Helper function for standard non-menu controls */
1405 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1406 const struct v4l2_ctrl_ops *ops,
1407 u32 id, s64 min, s64 max, u64 step, s64 def)
1410 enum v4l2_ctrl_type type;
1413 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1414 if (type == V4L2_CTRL_TYPE_MENU ||
1415 type == V4L2_CTRL_TYPE_INTEGER_MENU ||
1416 type >= V4L2_CTRL_COMPOUND_TYPES) {
1417 handler_set_err(hdl, -EINVAL);
1420 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
1421 min, max, step, def, NULL, 0,
1422 flags, NULL, NULL, ptr_null, NULL);
1424 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1426 /* Helper function for standard menu controls */
1427 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1428 const struct v4l2_ctrl_ops *ops,
1429 u32 id, u8 _max, u64 mask, u8 _def)
1431 const char * const *qmenu = NULL;
1432 const s64 *qmenu_int = NULL;
1433 unsigned int qmenu_int_len = 0;
1435 enum v4l2_ctrl_type type;
1442 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1444 if (type == V4L2_CTRL_TYPE_MENU)
1445 qmenu = v4l2_ctrl_get_menu(id);
1446 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
1447 qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
1449 if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) {
1450 handler_set_err(hdl, -EINVAL);
1453 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
1454 0, max, mask, def, NULL, 0,
1455 flags, qmenu, qmenu_int, ptr_null, NULL);
1457 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1459 /* Helper function for standard menu controls with driver defined menu */
1460 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
1461 const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
1462 u64 mask, u8 _def, const char * const *qmenu)
1464 enum v4l2_ctrl_type type;
1472 /* v4l2_ctrl_new_std_menu_items() should only be called for
1473 * standard controls without a standard menu.
1475 if (v4l2_ctrl_get_menu(id)) {
1476 handler_set_err(hdl, -EINVAL);
1480 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1481 if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
1482 handler_set_err(hdl, -EINVAL);
1485 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
1486 0, max, mask, def, NULL, 0,
1487 flags, qmenu, NULL, ptr_null, NULL);
1490 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
1492 /* Helper function for standard compound controls */
1493 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
1494 const struct v4l2_ctrl_ops *ops, u32 id,
1495 const union v4l2_ctrl_ptr p_def)
1498 enum v4l2_ctrl_type type;
1500 s64 min, max, step, def;
1502 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1503 if (type < V4L2_CTRL_COMPOUND_TYPES) {
1504 handler_set_err(hdl, -EINVAL);
1507 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
1508 min, max, step, def, NULL, 0,
1509 flags, NULL, NULL, p_def, NULL);
1511 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound);
1513 /* Helper function for standard integer menu controls */
1514 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
1515 const struct v4l2_ctrl_ops *ops,
1516 u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
1519 enum v4l2_ctrl_type type;
1526 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1527 if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
1528 handler_set_err(hdl, -EINVAL);
1531 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
1532 0, max, 0, def, NULL, 0,
1533 flags, NULL, qmenu_int, ptr_null, NULL);
1535 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
1537 /* Add the controls from another handler to our own. */
1538 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1539 struct v4l2_ctrl_handler *add,
1540 bool (*filter)(const struct v4l2_ctrl *ctrl),
1541 bool from_other_dev)
1543 struct v4l2_ctrl_ref *ref;
1546 /* Do nothing if either handler is NULL or if they are the same */
1547 if (!hdl || !add || hdl == add)
1551 mutex_lock(add->lock);
1552 list_for_each_entry(ref, &add->ctrl_refs, node) {
1553 struct v4l2_ctrl *ctrl = ref->ctrl;
1555 /* Skip handler-private controls. */
1556 if (ctrl->is_private)
1558 /* And control classes */
1559 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1561 /* Filter any unwanted controls */
1562 if (filter && !filter(ctrl))
1564 ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
1568 mutex_unlock(add->lock);
1571 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1573 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
1575 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
1577 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
1580 case V4L2_CID_AUDIO_MUTE:
1581 case V4L2_CID_AUDIO_VOLUME:
1582 case V4L2_CID_AUDIO_BALANCE:
1583 case V4L2_CID_AUDIO_BASS:
1584 case V4L2_CID_AUDIO_TREBLE:
1585 case V4L2_CID_AUDIO_LOUDNESS:
1592 EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
1594 /* Cluster controls */
1595 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1597 bool has_volatiles = false;
1600 /* The first control is the master control and it must not be NULL */
1601 if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
1604 for (i = 0; i < ncontrols; i++) {
1606 controls[i]->cluster = controls;
1607 controls[i]->ncontrols = ncontrols;
1608 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
1609 has_volatiles = true;
1612 controls[0]->has_volatiles = has_volatiles;
1614 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1616 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1617 u8 manual_val, bool set_volatile)
1619 struct v4l2_ctrl *master = controls[0];
1623 v4l2_ctrl_cluster(ncontrols, controls);
1624 WARN_ON(ncontrols <= 1);
1625 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
1626 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
1627 master->is_auto = true;
1628 master->has_volatiles = set_volatile;
1629 master->manual_mode_value = manual_val;
1630 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1632 if (!is_cur_manual(master))
1633 flag = V4L2_CTRL_FLAG_INACTIVE |
1634 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
1636 for (i = 1; i < ncontrols; i++)
1638 controls[i]->flags |= flag;
1640 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1643 * Obtain the current volatile values of an autocluster and mark them
1646 void update_from_auto_cluster(struct v4l2_ctrl *master)
1650 for (i = 1; i < master->ncontrols; i++)
1651 cur_to_new(master->cluster[i]);
1652 if (!call_op(master, g_volatile_ctrl))
1653 for (i = 1; i < master->ncontrols; i++)
1654 if (master->cluster[i])
1655 master->cluster[i]->is_new = 1;
1659 * Return non-zero if one or more of the controls in the cluster has a new
1660 * value that differs from the current value.
1662 static int cluster_changed(struct v4l2_ctrl *master)
1664 bool changed = false;
1668 for (i = 0; i < master->ncontrols; i++) {
1669 struct v4l2_ctrl *ctrl = master->cluster[i];
1670 bool ctrl_changed = false;
1675 if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE) {
1677 ctrl_changed = true;
1681 * Set has_changed to false to avoid generating
1682 * the event V4L2_EVENT_CTRL_CH_VALUE
1684 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
1685 ctrl->has_changed = false;
1689 for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++)
1690 ctrl_changed = !ctrl->type_ops->equal(ctrl, idx,
1691 ctrl->p_cur, ctrl->p_new);
1692 ctrl->has_changed = ctrl_changed;
1693 changed |= ctrl->has_changed;
1699 * Core function that calls try/s_ctrl and ensures that the new value is
1700 * copied to the current value on a set.
1701 * Must be called with ctrl->handler->lock held.
1703 int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
1704 bool set, u32 ch_flags)
1711 * Go through the cluster and either validate the new value or
1712 * (if no new value was set), copy the current value to the new
1713 * value, ensuring a consistent view for the control ops when
1716 for (i = 0; i < master->ncontrols; i++) {
1717 struct v4l2_ctrl *ctrl = master->cluster[i];
1722 if (!ctrl->is_new) {
1727 * Check again: it may have changed since the
1728 * previous check in try_or_set_ext_ctrls().
1730 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1734 ret = call_op(master, try_ctrl);
1736 /* Don't set if there is no change */
1737 if (ret || !set || !cluster_changed(master))
1739 ret = call_op(master, s_ctrl);
1743 /* If OK, then make the new values permanent. */
1744 update_flag = is_cur_manual(master) != is_new_manual(master);
1746 for (i = 0; i < master->ncontrols; i++) {
1748 * If we switch from auto to manual mode, and this cluster
1749 * contains volatile controls, then all non-master controls
1750 * have to be marked as changed. The 'new' value contains
1751 * the volatile value (obtained by update_from_auto_cluster),
1752 * which now has to become the current value.
1754 if (i && update_flag && is_new_manual(master) &&
1755 master->has_volatiles && master->cluster[i])
1756 master->cluster[i]->has_changed = true;
1758 new_to_cur(fh, master->cluster[i], ch_flags |
1759 ((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
1764 /* Activate/deactivate a control. */
1765 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1767 /* invert since the actual flag is called 'inactive' */
1768 bool inactive = !active;
1775 /* set V4L2_CTRL_FLAG_INACTIVE */
1776 old = test_and_set_bit(4, &ctrl->flags);
1778 /* clear V4L2_CTRL_FLAG_INACTIVE */
1779 old = test_and_clear_bit(4, &ctrl->flags);
1780 if (old != inactive)
1781 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1783 EXPORT_SYMBOL(v4l2_ctrl_activate);
1785 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1792 lockdep_assert_held(ctrl->handler->lock);
1795 /* set V4L2_CTRL_FLAG_GRABBED */
1796 old = test_and_set_bit(1, &ctrl->flags);
1798 /* clear V4L2_CTRL_FLAG_GRABBED */
1799 old = test_and_clear_bit(1, &ctrl->flags);
1801 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1803 EXPORT_SYMBOL(__v4l2_ctrl_grab);
1805 /* Call s_ctrl for all controls owned by the handler */
1806 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1808 struct v4l2_ctrl *ctrl;
1814 lockdep_assert_held(hdl->lock);
1816 list_for_each_entry(ctrl, &hdl->ctrls, node)
1819 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1820 struct v4l2_ctrl *master = ctrl->cluster[0];
1823 /* Skip if this control was already handled by a cluster. */
1824 /* Skip button controls and read-only controls. */
1825 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1826 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1829 for (i = 0; i < master->ncontrols; i++) {
1830 if (master->cluster[i]) {
1831 cur_to_new(master->cluster[i]);
1832 master->cluster[i]->is_new = 1;
1833 master->cluster[i]->done = true;
1836 ret = call_op(master, s_ctrl);
1843 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
1845 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1852 mutex_lock(hdl->lock);
1853 ret = __v4l2_ctrl_handler_setup(hdl);
1854 mutex_unlock(hdl->lock);
1858 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1860 /* Log the control name and value */
1861 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1862 const char *prefix, const char *colon)
1864 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1866 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1869 pr_info("%s%s%s: ", prefix, colon, ctrl->name);
1871 ctrl->type_ops->log(ctrl);
1873 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
1874 V4L2_CTRL_FLAG_GRABBED |
1875 V4L2_CTRL_FLAG_VOLATILE)) {
1876 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1877 pr_cont(" inactive");
1878 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
1879 pr_cont(" grabbed");
1880 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
1881 pr_cont(" volatile");
1886 /* Log all controls owned by the handler */
1887 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1890 struct v4l2_ctrl *ctrl;
1891 const char *colon = "";
1898 len = strlen(prefix);
1899 if (len && prefix[len - 1] != ' ')
1901 mutex_lock(hdl->lock);
1902 list_for_each_entry(ctrl, &hdl->ctrls, node)
1903 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1904 log_ctrl(ctrl, prefix, colon);
1905 mutex_unlock(hdl->lock);
1907 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1909 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,
1910 const struct v4l2_ctrl_ops *ctrl_ops,
1911 const struct v4l2_fwnode_device_properties *p)
1913 if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) {
1914 u32 orientation_ctrl;
1916 switch (p->orientation) {
1917 case V4L2_FWNODE_ORIENTATION_FRONT:
1918 orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT;
1920 case V4L2_FWNODE_ORIENTATION_BACK:
1921 orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK;
1923 case V4L2_FWNODE_ORIENTATION_EXTERNAL:
1924 orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL;
1929 if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops,
1930 V4L2_CID_CAMERA_ORIENTATION,
1931 V4L2_CAMERA_ORIENTATION_EXTERNAL, 0,
1936 if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) {
1937 if (!v4l2_ctrl_new_std(hdl, ctrl_ops,
1938 V4L2_CID_CAMERA_SENSOR_ROTATION,
1939 p->rotation, p->rotation, 1,
1946 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties);