xref: /linux/drivers/media/v4l2-core/v4l2-ctrls-core.c (revision afbe4bc252d90a6f8fad869b06d5430f615f22f9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * V4L2 controls framework core implementation.
4  *
5  * Copyright (C) 2010-2021  Hans Verkuil <hverkuil@kernel.org>
6  */
7 
8 #include <linux/export.h>
9 #include <linux/mm.h>
10 #include <linux/slab.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-event.h>
13 #include <media/v4l2-fwnode.h>
14 
15 #include "v4l2-ctrls-priv.h"
16 
17 static const union v4l2_ctrl_ptr ptr_null;
18 
19 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl,
20 		       u32 changes)
21 {
22 	memset(ev, 0, sizeof(*ev));
23 	ev->type = V4L2_EVENT_CTRL;
24 	ev->id = ctrl->id;
25 	ev->u.ctrl.changes = changes;
26 	ev->u.ctrl.type = ctrl->type;
27 	ev->u.ctrl.flags = user_flags(ctrl);
28 	if (ctrl->is_ptr)
29 		ev->u.ctrl.value64 = 0;
30 	else
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)
36 		ev->u.ctrl.step = 1;
37 	else
38 		ev->u.ctrl.step = ctrl->step;
39 	ev->u.ctrl.default_value = ctrl->default_value;
40 }
41 
42 void send_initial_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl)
43 {
44 	struct v4l2_event ev;
45 	u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
46 
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);
51 }
52 
53 void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
54 {
55 	struct v4l2_event ev;
56 	struct v4l2_subscribed_event *sev;
57 
58 	if (list_empty(&ctrl->ev_subs))
59 		return;
60 	fill_event(&ev, ctrl, changes);
61 
62 	list_for_each_entry(sev, &ctrl->ev_subs, node)
63 		if (sev->fh != fh ||
64 		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
65 			v4l2_event_queue_fh(sev->fh, &ev);
66 }
67 
68 bool v4l2_ctrl_type_op_equal(const struct v4l2_ctrl *ctrl,
69 			     union v4l2_ctrl_ptr ptr1, union v4l2_ctrl_ptr ptr2)
70 {
71 	unsigned int i;
72 
73 	switch (ctrl->type) {
74 	case V4L2_CTRL_TYPE_BUTTON:
75 		return false;
76 	case V4L2_CTRL_TYPE_STRING:
77 		for (i = 0; i < ctrl->elems; i++) {
78 			unsigned int idx = i * ctrl->elem_size;
79 
80 			/* strings are always 0-terminated */
81 			if (strcmp(ptr1.p_char + idx, ptr2.p_char + idx))
82 				return false;
83 		}
84 		return true;
85 	default:
86 		return !memcmp(ptr1.p_const, ptr2.p_const,
87 			       ctrl->elems * ctrl->elem_size);
88 	}
89 }
90 EXPORT_SYMBOL(v4l2_ctrl_type_op_equal);
91 
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
102 };
103 
104 static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx,
105 			      union v4l2_ctrl_ptr ptr)
106 {
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;
116 
117 	if (ctrl->p_def.p_const)
118 		memcpy(p, ctrl->p_def.p_const, ctrl->elem_size);
119 	else
120 		memset(p, 0, ctrl->elem_size);
121 
122 	switch ((u32)ctrl->type) {
123 	case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
124 		p_mpeg2_sequence = p;
125 
126 		/* 4:2:0 */
127 		p_mpeg2_sequence->chroma_format = 1;
128 		break;
129 	case V4L2_CTRL_TYPE_MPEG2_PICTURE:
130 		p_mpeg2_picture = p;
131 
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;
136 		break;
137 	case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
138 		p_mpeg2_quant = p;
139 
140 		memcpy(p_mpeg2_quant->intra_quantiser_matrix,
141 		       mpeg2_intra_quant_matrix,
142 		       ARRAY_SIZE(mpeg2_intra_quant_matrix));
143 		/*
144 		 * The default non-intra MPEG-2 quantisation
145 		 * coefficients are all 16, as per the specification.
146 		 */
147 		memset(p_mpeg2_quant->non_intra_quantiser_matrix, 16,
148 		       sizeof(p_mpeg2_quant->non_intra_quantiser_matrix));
149 		break;
150 	case V4L2_CTRL_TYPE_VP8_FRAME:
151 		p_vp8_frame = p;
152 		p_vp8_frame->num_dct_parts = 1;
153 		break;
154 	case V4L2_CTRL_TYPE_VP9_FRAME:
155 		p_vp9_frame = p;
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;
160 		break;
161 	case V4L2_CTRL_TYPE_AV1_SEQUENCE:
162 		p_av1_sequence = p;
163 		/*
164 		 * The initial profile is 0 which only allows YUV 420 subsampled
165 		 * data. Set the subsampling flags accordingly.
166 		 */
167 		p_av1_sequence->bit_depth = 8;
168 		p_av1_sequence->flags |= V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X |
169 					 V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y;
170 		break;
171 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
172 		p_fwht_params = p;
173 		p_fwht_params->version = V4L2_FWHT_VERSION;
174 		p_fwht_params->width = 1280;
175 		p_fwht_params->height = 720;
176 		p_fwht_params->flags = V4L2_FWHT_FL_PIXENC_YUV |
177 			(2 << V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET);
178 		break;
179 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
180 		p_h264_scaling_matrix = p;
181 		/*
182 		 * The default (flat) H.264 scaling matrix when none are
183 		 * specified in the bitstream, this is according to formulas
184 		 *  (7-8) and (7-9) of the specification.
185 		 */
186 		memset(p_h264_scaling_matrix, 16, sizeof(*p_h264_scaling_matrix));
187 		break;
188 	}
189 }
190 
191 static void std_min_compound(const struct v4l2_ctrl *ctrl, u32 idx,
192 			     union v4l2_ctrl_ptr ptr)
193 {
194 	void *p = ptr.p + idx * ctrl->elem_size;
195 
196 	if (ctrl->p_min.p_const)
197 		memcpy(p, ctrl->p_min.p_const, ctrl->elem_size);
198 	else
199 		memset(p, 0, ctrl->elem_size);
200 }
201 
202 static void std_max_compound(const struct v4l2_ctrl *ctrl, u32 idx,
203 			     union v4l2_ctrl_ptr ptr)
204 {
205 	void *p = ptr.p + idx * ctrl->elem_size;
206 
207 	if (ctrl->p_max.p_const)
208 		memcpy(p, ctrl->p_max.p_const, ctrl->elem_size);
209 	else
210 		memset(p, 0, ctrl->elem_size);
211 }
212 
213 static void __v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx,
214 				     u32 which, union v4l2_ctrl_ptr ptr)
215 {
216 	unsigned int i;
217 	u32 tot_elems = ctrl->elems;
218 	u32 elems = tot_elems - from_idx;
219 	s64 value;
220 
221 	switch (which) {
222 	case V4L2_CTRL_WHICH_DEF_VAL:
223 		value = ctrl->default_value;
224 		break;
225 	case V4L2_CTRL_WHICH_MAX_VAL:
226 		value = ctrl->maximum;
227 		break;
228 	case V4L2_CTRL_WHICH_MIN_VAL:
229 		value = ctrl->minimum;
230 		break;
231 	default:
232 		return;
233 	}
234 
235 	switch (ctrl->type) {
236 	case V4L2_CTRL_TYPE_STRING:
237 		if (which == V4L2_CTRL_WHICH_DEF_VAL)
238 			value = ctrl->minimum;
239 
240 		for (i = from_idx; i < tot_elems; i++) {
241 			unsigned int offset = i * ctrl->elem_size;
242 
243 			memset(ptr.p_char + offset, ' ', value);
244 			ptr.p_char[offset + value] = '\0';
245 		}
246 		break;
247 	case V4L2_CTRL_TYPE_INTEGER64:
248 		if (value) {
249 			for (i = from_idx; i < tot_elems; i++)
250 				ptr.p_s64[i] = value;
251 		} else {
252 			memset(ptr.p_s64 + from_idx, 0, elems * sizeof(s64));
253 		}
254 		break;
255 	case V4L2_CTRL_TYPE_INTEGER:
256 	case V4L2_CTRL_TYPE_INTEGER_MENU:
257 	case V4L2_CTRL_TYPE_MENU:
258 	case V4L2_CTRL_TYPE_BITMASK:
259 	case V4L2_CTRL_TYPE_BOOLEAN:
260 		if (value) {
261 			for (i = from_idx; i < tot_elems; i++)
262 				ptr.p_s32[i] = value;
263 		} else {
264 			memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32));
265 		}
266 		break;
267 	case V4L2_CTRL_TYPE_BUTTON:
268 	case V4L2_CTRL_TYPE_CTRL_CLASS:
269 		memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32));
270 		break;
271 	case V4L2_CTRL_TYPE_U8:
272 		memset(ptr.p_u8 + from_idx, value, elems);
273 		break;
274 	case V4L2_CTRL_TYPE_U16:
275 		if (value) {
276 			for (i = from_idx; i < tot_elems; i++)
277 				ptr.p_u16[i] = value;
278 		} else {
279 			memset(ptr.p_u16 + from_idx, 0, elems * sizeof(u16));
280 		}
281 		break;
282 	case V4L2_CTRL_TYPE_U32:
283 		if (value) {
284 			for (i = from_idx; i < tot_elems; i++)
285 				ptr.p_u32[i] = value;
286 		} else {
287 			memset(ptr.p_u32 + from_idx, 0, elems * sizeof(u32));
288 		}
289 		break;
290 	default:
291 		for (i = from_idx; i < tot_elems; i++) {
292 			switch (which) {
293 			case V4L2_CTRL_WHICH_DEF_VAL:
294 				std_init_compound(ctrl, i, ptr);
295 				break;
296 			case V4L2_CTRL_WHICH_MAX_VAL:
297 				std_max_compound(ctrl, i, ptr);
298 				break;
299 			case V4L2_CTRL_WHICH_MIN_VAL:
300 				std_min_compound(ctrl, i, ptr);
301 				break;
302 			}
303 		}
304 		break;
305 	}
306 }
307 
308 void v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx,
309 			    union v4l2_ctrl_ptr ptr)
310 {
311 	__v4l2_ctrl_type_op_init(ctrl, from_idx, V4L2_CTRL_WHICH_DEF_VAL, ptr);
312 }
313 EXPORT_SYMBOL(v4l2_ctrl_type_op_init);
314 
315 static void v4l2_ctrl_type_op_minimum(const struct v4l2_ctrl *ctrl,
316 				      u32 from_idx, union v4l2_ctrl_ptr ptr)
317 {
318 	__v4l2_ctrl_type_op_init(ctrl, from_idx, V4L2_CTRL_WHICH_MIN_VAL, ptr);
319 }
320 
321 static void v4l2_ctrl_type_op_maximum(const struct v4l2_ctrl *ctrl,
322 				      u32 from_idx, union v4l2_ctrl_ptr ptr)
323 {
324 	__v4l2_ctrl_type_op_init(ctrl, from_idx, V4L2_CTRL_WHICH_MAX_VAL, ptr);
325 }
326 
327 void v4l2_ctrl_type_op_log(const struct v4l2_ctrl *ctrl)
328 {
329 	union v4l2_ctrl_ptr ptr = ctrl->p_cur;
330 
331 	if (ctrl->is_array) {
332 		unsigned i;
333 
334 		for (i = 0; i < ctrl->nr_of_dims; i++)
335 			pr_cont("[%u]", ctrl->dims[i]);
336 		pr_cont(" ");
337 	}
338 
339 	switch (ctrl->type) {
340 	case V4L2_CTRL_TYPE_INTEGER:
341 		pr_cont("%d", *ptr.p_s32);
342 		break;
343 	case V4L2_CTRL_TYPE_BOOLEAN:
344 		pr_cont("%s", *ptr.p_s32 ? "true" : "false");
345 		break;
346 	case V4L2_CTRL_TYPE_MENU:
347 		pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
348 		break;
349 	case V4L2_CTRL_TYPE_INTEGER_MENU:
350 		pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
351 		break;
352 	case V4L2_CTRL_TYPE_BITMASK:
353 		pr_cont("0x%08x", *ptr.p_s32);
354 		break;
355 	case V4L2_CTRL_TYPE_INTEGER64:
356 		pr_cont("%lld", *ptr.p_s64);
357 		break;
358 	case V4L2_CTRL_TYPE_STRING:
359 		pr_cont("%s", ptr.p_char);
360 		break;
361 	case V4L2_CTRL_TYPE_U8:
362 		pr_cont("%u", (unsigned)*ptr.p_u8);
363 		break;
364 	case V4L2_CTRL_TYPE_U16:
365 		pr_cont("%u", (unsigned)*ptr.p_u16);
366 		break;
367 	case V4L2_CTRL_TYPE_U32:
368 		pr_cont("%u", (unsigned)*ptr.p_u32);
369 		break;
370 	case V4L2_CTRL_TYPE_AREA:
371 		pr_cont("%ux%u", ptr.p_area->width, ptr.p_area->height);
372 		break;
373 	case V4L2_CTRL_TYPE_H264_SPS:
374 		pr_cont("H264_SPS");
375 		break;
376 	case V4L2_CTRL_TYPE_H264_PPS:
377 		pr_cont("H264_PPS");
378 		break;
379 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
380 		pr_cont("H264_SCALING_MATRIX");
381 		break;
382 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
383 		pr_cont("H264_SLICE_PARAMS");
384 		break;
385 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
386 		pr_cont("H264_DECODE_PARAMS");
387 		break;
388 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
389 		pr_cont("H264_PRED_WEIGHTS");
390 		break;
391 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
392 		pr_cont("FWHT_PARAMS");
393 		break;
394 	case V4L2_CTRL_TYPE_VP8_FRAME:
395 		pr_cont("VP8_FRAME");
396 		break;
397 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
398 		pr_cont("HDR10_CLL_INFO");
399 		break;
400 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
401 		pr_cont("HDR10_MASTERING_DISPLAY");
402 		break;
403 	case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
404 		pr_cont("MPEG2_QUANTISATION");
405 		break;
406 	case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
407 		pr_cont("MPEG2_SEQUENCE");
408 		break;
409 	case V4L2_CTRL_TYPE_MPEG2_PICTURE:
410 		pr_cont("MPEG2_PICTURE");
411 		break;
412 	case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
413 		pr_cont("VP9_COMPRESSED_HDR");
414 		break;
415 	case V4L2_CTRL_TYPE_VP9_FRAME:
416 		pr_cont("VP9_FRAME");
417 		break;
418 	case V4L2_CTRL_TYPE_HEVC_SPS:
419 		pr_cont("HEVC_SPS");
420 		break;
421 	case V4L2_CTRL_TYPE_HEVC_PPS:
422 		pr_cont("HEVC_PPS");
423 		break;
424 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
425 		pr_cont("HEVC_SLICE_PARAMS");
426 		break;
427 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_ST_RPS:
428 		pr_cont("HEVC_EXT_SPS_ST_RPS");
429 		break;
430 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS:
431 		pr_cont("HEVC_EXT_SPS_LT_RPS");
432 		break;
433 	case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
434 		pr_cont("HEVC_SCALING_MATRIX");
435 		break;
436 	case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
437 		pr_cont("HEVC_DECODE_PARAMS");
438 		break;
439 	case V4L2_CTRL_TYPE_AV1_SEQUENCE:
440 		pr_cont("AV1_SEQUENCE");
441 		break;
442 	case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
443 		pr_cont("AV1_TILE_GROUP_ENTRY");
444 		break;
445 	case V4L2_CTRL_TYPE_AV1_FRAME:
446 		pr_cont("AV1_FRAME");
447 		break;
448 	case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
449 		pr_cont("AV1_FILM_GRAIN");
450 		break;
451 	case V4L2_CTRL_TYPE_RECT:
452 		pr_cont("(%d,%d)/%ux%u",
453 			ptr.p_rect->left, ptr.p_rect->top,
454 			ptr.p_rect->width, ptr.p_rect->height);
455 		break;
456 	default:
457 		pr_cont("unknown type %d", ctrl->type);
458 		break;
459 	}
460 }
461 EXPORT_SYMBOL(v4l2_ctrl_type_op_log);
462 
463 /*
464  * Round towards the closest legal value. Be careful when we are
465  * close to the maximum range of the control type to prevent
466  * wrap-arounds.
467  */
468 #define ROUND_TO_RANGE(val, offset_type, ctrl)			\
469 ({								\
470 	offset_type offset;					\
471 	if ((ctrl)->maximum >= 0 &&				\
472 	    val >= (ctrl)->maximum - (s32)((ctrl)->step / 2))	\
473 		val = (ctrl)->maximum;				\
474 	else							\
475 		val += (s32)((ctrl)->step / 2);			\
476 	val = clamp_t(typeof(val), val,				\
477 		      (ctrl)->minimum, (ctrl)->maximum);	\
478 	offset = (val) - (ctrl)->minimum;			\
479 	offset = (ctrl)->step * (offset / (u32)(ctrl)->step);	\
480 	val = (ctrl)->minimum + offset;				\
481 	0;							\
482 })
483 
484 /* Validate a new control */
485 
486 #define zero_padding(s) \
487 	memset(&(s).padding, 0, sizeof((s).padding))
488 #define zero_reserved(s) \
489 	memset(&(s).reserved, 0, sizeof((s).reserved))
490 
491 static int
492 validate_vp9_lf_params(struct v4l2_vp9_loop_filter *lf)
493 {
494 	unsigned int i;
495 
496 	if (lf->flags & ~(V4L2_VP9_LOOP_FILTER_FLAG_DELTA_ENABLED |
497 			  V4L2_VP9_LOOP_FILTER_FLAG_DELTA_UPDATE))
498 		return -EINVAL;
499 
500 	/* That all values are in the accepted range. */
501 	if (lf->level > GENMASK(5, 0))
502 		return -EINVAL;
503 
504 	if (lf->sharpness > GENMASK(2, 0))
505 		return -EINVAL;
506 
507 	for (i = 0; i < ARRAY_SIZE(lf->ref_deltas); i++)
508 		if (lf->ref_deltas[i] < -63 || lf->ref_deltas[i] > 63)
509 			return -EINVAL;
510 
511 	for (i = 0; i < ARRAY_SIZE(lf->mode_deltas); i++)
512 		if (lf->mode_deltas[i] < -63 || lf->mode_deltas[i] > 63)
513 			return -EINVAL;
514 
515 	zero_reserved(*lf);
516 	return 0;
517 }
518 
519 static int
520 validate_vp9_quant_params(struct v4l2_vp9_quantization *quant)
521 {
522 	if (quant->delta_q_y_dc < -15 || quant->delta_q_y_dc > 15 ||
523 	    quant->delta_q_uv_dc < -15 || quant->delta_q_uv_dc > 15 ||
524 	    quant->delta_q_uv_ac < -15 || quant->delta_q_uv_ac > 15)
525 		return -EINVAL;
526 
527 	zero_reserved(*quant);
528 	return 0;
529 }
530 
531 static int
532 validate_vp9_seg_params(struct v4l2_vp9_segmentation *seg)
533 {
534 	unsigned int i, j;
535 
536 	if (seg->flags & ~(V4L2_VP9_SEGMENTATION_FLAG_ENABLED |
537 			   V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP |
538 			   V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE |
539 			   V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA |
540 			   V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE))
541 		return -EINVAL;
542 
543 	for (i = 0; i < ARRAY_SIZE(seg->feature_enabled); i++) {
544 		if (seg->feature_enabled[i] &
545 		    ~V4L2_VP9_SEGMENT_FEATURE_ENABLED_MASK)
546 			return -EINVAL;
547 	}
548 
549 	for (i = 0; i < ARRAY_SIZE(seg->feature_data); i++) {
550 		static const int range[] = { 255, 63, 3, 0 };
551 
552 		for (j = 0; j < ARRAY_SIZE(seg->feature_data[j]); j++) {
553 			if (seg->feature_data[i][j] < -range[j] ||
554 			    seg->feature_data[i][j] > range[j])
555 				return -EINVAL;
556 		}
557 	}
558 
559 	zero_reserved(*seg);
560 	return 0;
561 }
562 
563 static int
564 validate_vp9_compressed_hdr(struct v4l2_ctrl_vp9_compressed_hdr *hdr)
565 {
566 	if (hdr->tx_mode > V4L2_VP9_TX_MODE_SELECT)
567 		return -EINVAL;
568 
569 	return 0;
570 }
571 
572 static int
573 validate_vp9_frame(struct v4l2_ctrl_vp9_frame *frame)
574 {
575 	int ret;
576 
577 	/* Make sure we're not passed invalid flags. */
578 	if (frame->flags & ~(V4L2_VP9_FRAME_FLAG_KEY_FRAME |
579 		  V4L2_VP9_FRAME_FLAG_SHOW_FRAME |
580 		  V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT |
581 		  V4L2_VP9_FRAME_FLAG_INTRA_ONLY |
582 		  V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV |
583 		  V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX |
584 		  V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE |
585 		  V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING |
586 		  V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING |
587 		  V4L2_VP9_FRAME_FLAG_COLOR_RANGE_FULL_SWING))
588 		return -EINVAL;
589 
590 	if (frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT &&
591 	    frame->flags & V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX)
592 		return -EINVAL;
593 
594 	if (frame->profile > V4L2_VP9_PROFILE_MAX)
595 		return -EINVAL;
596 
597 	if (frame->reset_frame_context > V4L2_VP9_RESET_FRAME_CTX_ALL)
598 		return -EINVAL;
599 
600 	if (frame->frame_context_idx >= V4L2_VP9_NUM_FRAME_CTX)
601 		return -EINVAL;
602 
603 	/*
604 	 * Profiles 0 and 1 only support 8-bit depth, profiles 2 and 3 only 10
605 	 * and 12 bit depths.
606 	 */
607 	if ((frame->profile < 2 && frame->bit_depth != 8) ||
608 	    (frame->profile >= 2 &&
609 	     (frame->bit_depth != 10 && frame->bit_depth != 12)))
610 		return -EINVAL;
611 
612 	/* Profile 0 and 2 only accept YUV 4:2:0. */
613 	if ((frame->profile == 0 || frame->profile == 2) &&
614 	    (!(frame->flags & V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING) ||
615 	     !(frame->flags & V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING)))
616 		return -EINVAL;
617 
618 	/* Profile 1 and 3 only accept YUV 4:2:2, 4:4:0 and 4:4:4. */
619 	if ((frame->profile == 1 || frame->profile == 3) &&
620 	    ((frame->flags & V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING) &&
621 	     (frame->flags & V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING)))
622 		return -EINVAL;
623 
624 	if (frame->interpolation_filter > V4L2_VP9_INTERP_FILTER_SWITCHABLE)
625 		return -EINVAL;
626 
627 	/*
628 	 * According to the spec, tile_cols_log2 shall be less than or equal
629 	 * to 6.
630 	 */
631 	if (frame->tile_cols_log2 > 6)
632 		return -EINVAL;
633 
634 	if (frame->reference_mode > V4L2_VP9_REFERENCE_MODE_SELECT)
635 		return -EINVAL;
636 
637 	ret = validate_vp9_lf_params(&frame->lf);
638 	if (ret)
639 		return ret;
640 
641 	ret = validate_vp9_quant_params(&frame->quant);
642 	if (ret)
643 		return ret;
644 
645 	ret = validate_vp9_seg_params(&frame->seg);
646 	if (ret)
647 		return ret;
648 
649 	zero_reserved(*frame);
650 	return 0;
651 }
652 
653 static int validate_av1_quantization(struct v4l2_av1_quantization *q)
654 {
655 	if (q->flags > GENMASK(2, 0))
656 		return -EINVAL;
657 
658 	if (q->delta_q_y_dc < -64 || q->delta_q_y_dc > 63 ||
659 	    q->delta_q_u_dc < -64 || q->delta_q_u_dc > 63 ||
660 	    q->delta_q_v_dc < -64 || q->delta_q_v_dc > 63 ||
661 	    q->delta_q_u_ac < -64 || q->delta_q_u_ac > 63 ||
662 	    q->delta_q_v_ac < -64 || q->delta_q_v_ac > 63 ||
663 	    q->delta_q_res > GENMASK(1, 0))
664 		return -EINVAL;
665 
666 	if (q->qm_y > GENMASK(3, 0) ||
667 	    q->qm_u > GENMASK(3, 0) ||
668 	    q->qm_v > GENMASK(3, 0))
669 		return -EINVAL;
670 
671 	return 0;
672 }
673 
674 static int validate_av1_segmentation(struct v4l2_av1_segmentation *s)
675 {
676 	u32 i;
677 	u32 j;
678 
679 	if (s->flags > GENMASK(4, 0))
680 		return -EINVAL;
681 
682 	for (i = 0; i < ARRAY_SIZE(s->feature_data); i++) {
683 		static const int segmentation_feature_signed[] = { 1, 1, 1, 1, 1, 0, 0, 0 };
684 		static const int segmentation_feature_max[] = { 255, 63, 63, 63, 63, 7, 0, 0};
685 
686 		for (j = 0; j < ARRAY_SIZE(s->feature_data[j]); j++) {
687 			s32 limit = segmentation_feature_max[j];
688 
689 			if (segmentation_feature_signed[j]) {
690 				if (s->feature_data[i][j] < -limit ||
691 				    s->feature_data[i][j] > limit)
692 					return -EINVAL;
693 			} else {
694 				if (s->feature_data[i][j] < 0 || s->feature_data[i][j] > limit)
695 					return -EINVAL;
696 			}
697 		}
698 	}
699 
700 	return 0;
701 }
702 
703 static int validate_av1_loop_filter(struct v4l2_av1_loop_filter *lf)
704 {
705 	u32 i;
706 
707 	if (lf->flags > GENMASK(3, 0))
708 		return -EINVAL;
709 
710 	for (i = 0; i < ARRAY_SIZE(lf->level); i++) {
711 		if (lf->level[i] > GENMASK(5, 0))
712 			return -EINVAL;
713 	}
714 
715 	if (lf->sharpness > GENMASK(2, 0))
716 		return -EINVAL;
717 
718 	for (i = 0; i < ARRAY_SIZE(lf->ref_deltas); i++) {
719 		if (lf->ref_deltas[i] < -64 || lf->ref_deltas[i] > 63)
720 			return -EINVAL;
721 	}
722 
723 	for (i = 0; i < ARRAY_SIZE(lf->mode_deltas); i++) {
724 		if (lf->mode_deltas[i] < -64 || lf->mode_deltas[i] > 63)
725 			return -EINVAL;
726 	}
727 
728 	return 0;
729 }
730 
731 static int validate_av1_cdef(struct v4l2_av1_cdef *cdef)
732 {
733 	u32 i;
734 
735 	if (cdef->damping_minus_3 > GENMASK(1, 0) ||
736 	    cdef->bits > GENMASK(1, 0))
737 		return -EINVAL;
738 
739 	for (i = 0; i < 1 << cdef->bits; i++) {
740 		if (cdef->y_pri_strength[i] > GENMASK(3, 0) ||
741 		    cdef->y_sec_strength[i] > 4 ||
742 		    cdef->uv_pri_strength[i] > GENMASK(3, 0) ||
743 		    cdef->uv_sec_strength[i] > 4)
744 			return -EINVAL;
745 	}
746 
747 	return 0;
748 }
749 
750 static int validate_av1_loop_restauration(struct v4l2_av1_loop_restoration *lr)
751 {
752 	if (lr->lr_unit_shift > 3 || lr->lr_uv_shift > 1)
753 		return -EINVAL;
754 
755 	return 0;
756 }
757 
758 static int validate_av1_film_grain(struct v4l2_ctrl_av1_film_grain *fg)
759 {
760 	u32 i;
761 
762 	if (fg->flags > GENMASK(4, 0))
763 		return -EINVAL;
764 
765 	if (fg->film_grain_params_ref_idx > GENMASK(2, 0) ||
766 	    fg->num_y_points > 14 ||
767 	    fg->num_cb_points > 10 ||
768 	    fg->num_cr_points > GENMASK(3, 0) ||
769 	    fg->grain_scaling_minus_8 > GENMASK(1, 0) ||
770 	    fg->ar_coeff_lag > GENMASK(1, 0) ||
771 	    fg->ar_coeff_shift_minus_6 > GENMASK(1, 0) ||
772 	    fg->grain_scale_shift > GENMASK(1, 0))
773 		return -EINVAL;
774 
775 	if (!(fg->flags & V4L2_AV1_FILM_GRAIN_FLAG_APPLY_GRAIN))
776 		return 0;
777 
778 	for (i = 1; i < fg->num_y_points; i++)
779 		if (fg->point_y_value[i] <= fg->point_y_value[i - 1])
780 			return -EINVAL;
781 
782 	for (i = 1; i < fg->num_cb_points; i++)
783 		if (fg->point_cb_value[i] <= fg->point_cb_value[i - 1])
784 			return -EINVAL;
785 
786 	for (i = 1; i < fg->num_cr_points; i++)
787 		if (fg->point_cr_value[i] <= fg->point_cr_value[i - 1])
788 			return -EINVAL;
789 
790 	return 0;
791 }
792 
793 static int validate_av1_frame(struct v4l2_ctrl_av1_frame *f)
794 {
795 	int ret = 0;
796 
797 	ret = validate_av1_quantization(&f->quantization);
798 	if (ret)
799 		return ret;
800 	ret = validate_av1_segmentation(&f->segmentation);
801 	if (ret)
802 		return ret;
803 	ret = validate_av1_loop_filter(&f->loop_filter);
804 	if (ret)
805 		return ret;
806 	ret = validate_av1_cdef(&f->cdef);
807 	if (ret)
808 		return ret;
809 	ret = validate_av1_loop_restauration(&f->loop_restoration);
810 	if (ret)
811 		return ret;
812 
813 	if (f->flags &
814 	~(V4L2_AV1_FRAME_FLAG_SHOW_FRAME |
815 	  V4L2_AV1_FRAME_FLAG_SHOWABLE_FRAME |
816 	  V4L2_AV1_FRAME_FLAG_ERROR_RESILIENT_MODE |
817 	  V4L2_AV1_FRAME_FLAG_DISABLE_CDF_UPDATE |
818 	  V4L2_AV1_FRAME_FLAG_ALLOW_SCREEN_CONTENT_TOOLS |
819 	  V4L2_AV1_FRAME_FLAG_FORCE_INTEGER_MV |
820 	  V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC |
821 	  V4L2_AV1_FRAME_FLAG_USE_SUPERRES |
822 	  V4L2_AV1_FRAME_FLAG_ALLOW_HIGH_PRECISION_MV |
823 	  V4L2_AV1_FRAME_FLAG_IS_MOTION_MODE_SWITCHABLE |
824 	  V4L2_AV1_FRAME_FLAG_USE_REF_FRAME_MVS |
825 	  V4L2_AV1_FRAME_FLAG_DISABLE_FRAME_END_UPDATE_CDF |
826 	  V4L2_AV1_FRAME_FLAG_ALLOW_WARPED_MOTION |
827 	  V4L2_AV1_FRAME_FLAG_REFERENCE_SELECT |
828 	  V4L2_AV1_FRAME_FLAG_REDUCED_TX_SET |
829 	  V4L2_AV1_FRAME_FLAG_SKIP_MODE_ALLOWED |
830 	  V4L2_AV1_FRAME_FLAG_SKIP_MODE_PRESENT |
831 	  V4L2_AV1_FRAME_FLAG_FRAME_SIZE_OVERRIDE |
832 	  V4L2_AV1_FRAME_FLAG_BUFFER_REMOVAL_TIME_PRESENT |
833 	  V4L2_AV1_FRAME_FLAG_FRAME_REFS_SHORT_SIGNALING))
834 		return -EINVAL;
835 
836 	if (f->superres_denom > GENMASK(2, 0) + 9)
837 		return -EINVAL;
838 
839 	return 0;
840 }
841 
842 /**
843  * validate_av1_sequence - validate AV1 sequence header fields
844  * @s: control struct from userspace
845  *
846  * Implements AV1 spec §5.5.2 color_config() checks that are
847  * possible with the current v4l2_ctrl_av1_sequence definition.
848  *
849  * TODO: extend validation once additional fields such as
850  *       color_primaries, transfer_characteristics,
851  *       matrix_coefficients, and chroma_sample_position
852  *       are added to the uAPI.
853  *
854  * Returns 0 if valid, -EINVAL otherwise.
855  */
856 static int validate_av1_sequence(struct v4l2_ctrl_av1_sequence *s)
857 {
858 	const bool mono  = s->flags & V4L2_AV1_SEQUENCE_FLAG_MONO_CHROME;
859 	const bool sx    = s->flags & V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X;
860 	const bool sy    = s->flags & V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y;
861 	const bool uv_dq = s->flags & V4L2_AV1_SEQUENCE_FLAG_SEPARATE_UV_DELTA_Q;
862 
863 	/* 1. Reject unknown flags */
864 	if (s->flags &
865 	    ~(V4L2_AV1_SEQUENCE_FLAG_STILL_PICTURE |
866 	      V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK |
867 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_FILTER_INTRA |
868 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_INTRA_EDGE_FILTER |
869 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_INTERINTRA_COMPOUND |
870 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_MASKED_COMPOUND |
871 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_WARPED_MOTION |
872 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_DUAL_FILTER |
873 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_ORDER_HINT |
874 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_JNT_COMP |
875 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_REF_FRAME_MVS |
876 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_SUPERRES |
877 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_CDEF |
878 	      V4L2_AV1_SEQUENCE_FLAG_ENABLE_RESTORATION |
879 	      V4L2_AV1_SEQUENCE_FLAG_MONO_CHROME |
880 	      V4L2_AV1_SEQUENCE_FLAG_COLOR_RANGE |
881 	      V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X |
882 	      V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y |
883 	      V4L2_AV1_SEQUENCE_FLAG_FILM_GRAIN_PARAMS_PRESENT |
884 	      V4L2_AV1_SEQUENCE_FLAG_SEPARATE_UV_DELTA_Q))
885 		return -EINVAL;
886 
887 	/* 2. Profile range */
888 	if (s->seq_profile > 2)
889 		return -EINVAL;
890 
891 	/* 3. Monochrome shortcut */
892 	if (mono) {
893 		/* Profile 1 forbids monochrome */
894 		if (s->seq_profile == 1)
895 			return -EINVAL;
896 
897 		/* Mono → subsampling must look like 4:0:0: sx=1, sy=1 */
898 		if (!sx || !sy)
899 			return -EINVAL;
900 
901 		/* separate_uv_delta_q must be 0 */
902 		if (uv_dq)
903 			return -EINVAL;
904 
905 		return 0;
906 	}
907 
908 	/* 4. Profile-specific rules */
909 	switch (s->seq_profile) {
910 	case 0:
911 		/* Profile 0: only 8/10-bit, subsampling=4:2:0 (sx=1, sy=1) */
912 		if (s->bit_depth != 8 && s->bit_depth != 10)
913 			return -EINVAL;
914 		if (!(sx && sy))
915 			return -EINVAL;
916 		break;
917 
918 	case 1:
919 		/* Profile 1: only 8/10-bit, subsampling=4:4:4 (sx=0, sy=0) */
920 		if (s->bit_depth != 8 && s->bit_depth != 10)
921 			return -EINVAL;
922 		if (sx || sy)
923 			return -EINVAL;
924 		break;
925 
926 	case 2:
927 		/* Profile 2: 8/10/12-bit allowed */
928 		if (s->bit_depth != 8 && s->bit_depth != 10 &&
929 		    s->bit_depth != 12)
930 			return -EINVAL;
931 
932 		if (s->bit_depth == 12) {
933 			if (!sx) {
934 				/* 4:4:4 → sy must be 0 */
935 				if (sy)
936 					return -EINVAL;
937 			} else {
938 				/* sx=1 → sy=0 (4:2:2) or sy=1 (4:2:0) */
939 				if (sy != 0 && sy != 1)
940 					return -EINVAL;
941 			}
942 		} else {
943 			/* 8/10-bit → only 4:2:2 allowed (sx=1, sy=0) */
944 			if (!(sx && !sy))
945 				return -EINVAL;
946 		}
947 		break;
948 	}
949 
950 	return 0;
951 }
952 
953 /*
954  * Compound controls validation requires setting unused fields/flags to zero
955  * in order to properly detect unchanged controls with v4l2_ctrl_type_op_equal's
956  * memcmp.
957  */
958 static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
959 				 union v4l2_ctrl_ptr ptr)
960 {
961 	struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence;
962 	struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture;
963 	struct v4l2_ctrl_vp8_frame *p_vp8_frame;
964 	struct v4l2_ctrl_fwht_params *p_fwht_params;
965 	struct v4l2_ctrl_h264_sps *p_h264_sps;
966 	struct v4l2_ctrl_h264_pps *p_h264_pps;
967 	struct v4l2_ctrl_h264_pred_weights *p_h264_pred_weights;
968 	struct v4l2_ctrl_h264_slice_params *p_h264_slice_params;
969 	struct v4l2_ctrl_h264_decode_params *p_h264_dec_params;
970 	struct v4l2_ctrl_hevc_ext_sps_lt_rps *p_hevc_lt_rps;
971 	struct v4l2_ctrl_hevc_ext_sps_st_rps *p_hevc_st_rps;
972 	struct v4l2_ctrl_hevc_sps *p_hevc_sps;
973 	struct v4l2_ctrl_hevc_pps *p_hevc_pps;
974 	struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params;
975 	struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering;
976 	struct v4l2_ctrl_hevc_decode_params *p_hevc_decode_params;
977 	struct v4l2_area *area;
978 	struct v4l2_rect *rect;
979 	void *p = ptr.p + idx * ctrl->elem_size;
980 	unsigned int i;
981 
982 	switch ((u32)ctrl->type) {
983 	case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
984 		p_mpeg2_sequence = p;
985 
986 		switch (p_mpeg2_sequence->chroma_format) {
987 		case 1: /* 4:2:0 */
988 		case 2: /* 4:2:2 */
989 		case 3: /* 4:4:4 */
990 			break;
991 		default:
992 			return -EINVAL;
993 		}
994 		break;
995 
996 	case V4L2_CTRL_TYPE_MPEG2_PICTURE:
997 		p_mpeg2_picture = p;
998 
999 		switch (p_mpeg2_picture->intra_dc_precision) {
1000 		case 0: /* 8 bits */
1001 		case 1: /* 9 bits */
1002 		case 2: /* 10 bits */
1003 		case 3: /* 11 bits */
1004 			break;
1005 		default:
1006 			return -EINVAL;
1007 		}
1008 
1009 		switch (p_mpeg2_picture->picture_structure) {
1010 		case V4L2_MPEG2_PIC_TOP_FIELD:
1011 		case V4L2_MPEG2_PIC_BOTTOM_FIELD:
1012 		case V4L2_MPEG2_PIC_FRAME:
1013 			break;
1014 		default:
1015 			return -EINVAL;
1016 		}
1017 
1018 		switch (p_mpeg2_picture->picture_coding_type) {
1019 		case V4L2_MPEG2_PIC_CODING_TYPE_I:
1020 		case V4L2_MPEG2_PIC_CODING_TYPE_P:
1021 		case V4L2_MPEG2_PIC_CODING_TYPE_B:
1022 			break;
1023 		default:
1024 			return -EINVAL;
1025 		}
1026 		zero_reserved(*p_mpeg2_picture);
1027 		break;
1028 
1029 	case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
1030 		break;
1031 
1032 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
1033 		p_fwht_params = p;
1034 		if (p_fwht_params->version < V4L2_FWHT_VERSION)
1035 			return -EINVAL;
1036 		if (!p_fwht_params->width || !p_fwht_params->height)
1037 			return -EINVAL;
1038 		break;
1039 
1040 	case V4L2_CTRL_TYPE_H264_SPS:
1041 		p_h264_sps = p;
1042 
1043 		/* Some syntax elements are only conditionally valid */
1044 		if (p_h264_sps->pic_order_cnt_type != 0) {
1045 			p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 = 0;
1046 		} else if (p_h264_sps->pic_order_cnt_type != 1) {
1047 			p_h264_sps->num_ref_frames_in_pic_order_cnt_cycle = 0;
1048 			p_h264_sps->offset_for_non_ref_pic = 0;
1049 			p_h264_sps->offset_for_top_to_bottom_field = 0;
1050 			memset(&p_h264_sps->offset_for_ref_frame, 0,
1051 			       sizeof(p_h264_sps->offset_for_ref_frame));
1052 		}
1053 
1054 		if (!V4L2_H264_SPS_HAS_CHROMA_FORMAT(p_h264_sps)) {
1055 			p_h264_sps->chroma_format_idc = 1;
1056 			p_h264_sps->bit_depth_luma_minus8 = 0;
1057 			p_h264_sps->bit_depth_chroma_minus8 = 0;
1058 
1059 			p_h264_sps->flags &=
1060 				~V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS;
1061 		}
1062 
1063 		if (p_h264_sps->chroma_format_idc < 3)
1064 			p_h264_sps->flags &=
1065 				~V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE;
1066 
1067 		if (p_h264_sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)
1068 			p_h264_sps->flags &=
1069 				~V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD;
1070 
1071 		/*
1072 		 * Chroma 4:2:2 format require at least High 4:2:2 profile.
1073 		 *
1074 		 * The H264 specification and well-known parser implementations
1075 		 * use profile-idc values directly, as that is clearer and
1076 		 * less ambiguous. We do the same here.
1077 		 */
1078 		if (p_h264_sps->profile_idc < 122 &&
1079 		    p_h264_sps->chroma_format_idc > 1)
1080 			return -EINVAL;
1081 		/* Chroma 4:4:4 format require at least High 4:2:2 profile */
1082 		if (p_h264_sps->profile_idc < 244 &&
1083 		    p_h264_sps->chroma_format_idc > 2)
1084 			return -EINVAL;
1085 		if (p_h264_sps->chroma_format_idc > 3)
1086 			return -EINVAL;
1087 
1088 		if (p_h264_sps->bit_depth_luma_minus8 > 6)
1089 			return -EINVAL;
1090 		if (p_h264_sps->bit_depth_chroma_minus8 > 6)
1091 			return -EINVAL;
1092 		if (p_h264_sps->log2_max_frame_num_minus4 > 12)
1093 			return -EINVAL;
1094 		if (p_h264_sps->pic_order_cnt_type > 2)
1095 			return -EINVAL;
1096 		if (p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 > 12)
1097 			return -EINVAL;
1098 		if (p_h264_sps->max_num_ref_frames > V4L2_H264_REF_LIST_LEN)
1099 			return -EINVAL;
1100 		break;
1101 
1102 	case V4L2_CTRL_TYPE_H264_PPS:
1103 		p_h264_pps = p;
1104 
1105 		if (p_h264_pps->num_slice_groups_minus1 > 7)
1106 			return -EINVAL;
1107 		if (p_h264_pps->num_ref_idx_l0_default_active_minus1 >
1108 		    (V4L2_H264_REF_LIST_LEN - 1))
1109 			return -EINVAL;
1110 		if (p_h264_pps->num_ref_idx_l1_default_active_minus1 >
1111 		    (V4L2_H264_REF_LIST_LEN - 1))
1112 			return -EINVAL;
1113 		if (p_h264_pps->weighted_bipred_idc > 2)
1114 			return -EINVAL;
1115 		/*
1116 		 * pic_init_qp_minus26 shall be in the range of
1117 		 * -(26 + QpBdOffset_y) to +25, inclusive,
1118 		 *  where QpBdOffset_y is 6 * bit_depth_luma_minus8
1119 		 */
1120 		if (p_h264_pps->pic_init_qp_minus26 < -62 ||
1121 		    p_h264_pps->pic_init_qp_minus26 > 25)
1122 			return -EINVAL;
1123 		if (p_h264_pps->pic_init_qs_minus26 < -26 ||
1124 		    p_h264_pps->pic_init_qs_minus26 > 25)
1125 			return -EINVAL;
1126 		if (p_h264_pps->chroma_qp_index_offset < -12 ||
1127 		    p_h264_pps->chroma_qp_index_offset > 12)
1128 			return -EINVAL;
1129 		if (p_h264_pps->second_chroma_qp_index_offset < -12 ||
1130 		    p_h264_pps->second_chroma_qp_index_offset > 12)
1131 			return -EINVAL;
1132 		break;
1133 
1134 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
1135 		break;
1136 
1137 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
1138 		p_h264_pred_weights = p;
1139 
1140 		if (p_h264_pred_weights->luma_log2_weight_denom > 7)
1141 			return -EINVAL;
1142 		if (p_h264_pred_weights->chroma_log2_weight_denom > 7)
1143 			return -EINVAL;
1144 		break;
1145 
1146 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
1147 		p_h264_slice_params = p;
1148 
1149 		if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
1150 			p_h264_slice_params->flags &=
1151 				~V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED;
1152 
1153 		if (p_h264_slice_params->colour_plane_id > 2)
1154 			return -EINVAL;
1155 		if (p_h264_slice_params->cabac_init_idc > 2)
1156 			return -EINVAL;
1157 		if (p_h264_slice_params->disable_deblocking_filter_idc > 2)
1158 			return -EINVAL;
1159 		if (p_h264_slice_params->slice_alpha_c0_offset_div2 < -6 ||
1160 		    p_h264_slice_params->slice_alpha_c0_offset_div2 > 6)
1161 			return -EINVAL;
1162 		if (p_h264_slice_params->slice_beta_offset_div2 < -6 ||
1163 		    p_h264_slice_params->slice_beta_offset_div2 > 6)
1164 			return -EINVAL;
1165 
1166 		if (p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_I ||
1167 		    p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_SI)
1168 			p_h264_slice_params->num_ref_idx_l0_active_minus1 = 0;
1169 		if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
1170 			p_h264_slice_params->num_ref_idx_l1_active_minus1 = 0;
1171 
1172 		if (p_h264_slice_params->num_ref_idx_l0_active_minus1 >
1173 		    (V4L2_H264_REF_LIST_LEN - 1))
1174 			return -EINVAL;
1175 		if (p_h264_slice_params->num_ref_idx_l1_active_minus1 >
1176 		    (V4L2_H264_REF_LIST_LEN - 1))
1177 			return -EINVAL;
1178 		zero_reserved(*p_h264_slice_params);
1179 		break;
1180 
1181 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
1182 		p_h264_dec_params = p;
1183 
1184 		if (p_h264_dec_params->nal_ref_idc > 3)
1185 			return -EINVAL;
1186 		for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
1187 			struct v4l2_h264_dpb_entry *dpb_entry =
1188 				&p_h264_dec_params->dpb[i];
1189 
1190 			zero_reserved(*dpb_entry);
1191 		}
1192 		zero_reserved(*p_h264_dec_params);
1193 		break;
1194 
1195 	case V4L2_CTRL_TYPE_VP8_FRAME:
1196 		p_vp8_frame = p;
1197 
1198 		switch (p_vp8_frame->num_dct_parts) {
1199 		case 1:
1200 		case 2:
1201 		case 4:
1202 		case 8:
1203 			break;
1204 		default:
1205 			return -EINVAL;
1206 		}
1207 		zero_padding(p_vp8_frame->segment);
1208 		zero_padding(p_vp8_frame->lf);
1209 		zero_padding(p_vp8_frame->quant);
1210 		zero_padding(p_vp8_frame->entropy);
1211 		zero_padding(p_vp8_frame->coder_state);
1212 		break;
1213 
1214 	case V4L2_CTRL_TYPE_HEVC_SPS:
1215 		p_hevc_sps = p;
1216 
1217 		if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) {
1218 			p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0;
1219 			p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;
1220 			p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0;
1221 			p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0;
1222 		}
1223 
1224 		if (!(p_hevc_sps->flags &
1225 		      V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT))
1226 			p_hevc_sps->num_long_term_ref_pics_sps = 0;
1227 		break;
1228 
1229 	case V4L2_CTRL_TYPE_HEVC_PPS:
1230 		p_hevc_pps = p;
1231 
1232 		if (!(p_hevc_pps->flags &
1233 		      V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED))
1234 			p_hevc_pps->diff_cu_qp_delta_depth = 0;
1235 
1236 		if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
1237 			p_hevc_pps->num_tile_columns_minus1 = 0;
1238 			p_hevc_pps->num_tile_rows_minus1 = 0;
1239 			memset(&p_hevc_pps->column_width_minus1, 0,
1240 			       sizeof(p_hevc_pps->column_width_minus1));
1241 			memset(&p_hevc_pps->row_height_minus1, 0,
1242 			       sizeof(p_hevc_pps->row_height_minus1));
1243 
1244 			p_hevc_pps->flags &=
1245 				~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED;
1246 		}
1247 
1248 		if (p_hevc_pps->flags &
1249 		    V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) {
1250 			p_hevc_pps->pps_beta_offset_div2 = 0;
1251 			p_hevc_pps->pps_tc_offset_div2 = 0;
1252 		}
1253 		break;
1254 
1255 	case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
1256 		p_hevc_decode_params = p;
1257 
1258 		if (p_hevc_decode_params->num_active_dpb_entries >
1259 		    V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
1260 			return -EINVAL;
1261 		break;
1262 
1263 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
1264 		p_hevc_slice_params = p;
1265 
1266 		if (p_hevc_slice_params->num_ref_idx_l0_active_minus1 >=
1267 		    V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
1268 			return -EINVAL;
1269 
1270 		if (p_hevc_slice_params->slice_type != V4L2_HEVC_SLICE_TYPE_B)
1271 			break;
1272 
1273 		if (p_hevc_slice_params->num_ref_idx_l1_active_minus1 >=
1274 		    V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
1275 			return -EINVAL;
1276 		break;
1277 
1278 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_ST_RPS:
1279 		p_hevc_st_rps = p;
1280 
1281 		if (p_hevc_st_rps->flags & ~V4L2_HEVC_EXT_SPS_ST_RPS_FLAG_INTER_REF_PIC_SET_PRED)
1282 			return -EINVAL;
1283 		break;
1284 
1285 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS:
1286 		p_hevc_lt_rps = p;
1287 
1288 		if (p_hevc_lt_rps->flags & ~V4L2_HEVC_EXT_SPS_LT_RPS_FLAG_USED_LT)
1289 			return -EINVAL;
1290 		break;
1291 
1292 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
1293 		break;
1294 
1295 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
1296 		p_hdr10_mastering = p;
1297 
1298 		for (i = 0; i < 3; ++i) {
1299 			if (p_hdr10_mastering->display_primaries_x[i] <
1300 				V4L2_HDR10_MASTERING_PRIMARIES_X_LOW ||
1301 			    p_hdr10_mastering->display_primaries_x[i] >
1302 				V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH ||
1303 			    p_hdr10_mastering->display_primaries_y[i] <
1304 				V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW ||
1305 			    p_hdr10_mastering->display_primaries_y[i] >
1306 				V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH)
1307 				return -EINVAL;
1308 		}
1309 
1310 		if (p_hdr10_mastering->white_point_x <
1311 			V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW ||
1312 		    p_hdr10_mastering->white_point_x >
1313 			V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH ||
1314 		    p_hdr10_mastering->white_point_y <
1315 			V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW ||
1316 		    p_hdr10_mastering->white_point_y >
1317 			V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH)
1318 			return -EINVAL;
1319 
1320 		if (p_hdr10_mastering->max_display_mastering_luminance <
1321 			V4L2_HDR10_MASTERING_MAX_LUMA_LOW ||
1322 		    p_hdr10_mastering->max_display_mastering_luminance >
1323 			V4L2_HDR10_MASTERING_MAX_LUMA_HIGH ||
1324 		    p_hdr10_mastering->min_display_mastering_luminance <
1325 			V4L2_HDR10_MASTERING_MIN_LUMA_LOW ||
1326 		    p_hdr10_mastering->min_display_mastering_luminance >
1327 			V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
1328 			return -EINVAL;
1329 
1330 		/* The following restriction comes from ITU-T Rec. H.265 spec */
1331 		if (p_hdr10_mastering->max_display_mastering_luminance ==
1332 			V4L2_HDR10_MASTERING_MAX_LUMA_LOW &&
1333 		    p_hdr10_mastering->min_display_mastering_luminance ==
1334 			V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
1335 			return -EINVAL;
1336 
1337 		break;
1338 
1339 	case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
1340 		break;
1341 
1342 	case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
1343 		return validate_vp9_compressed_hdr(p);
1344 
1345 	case V4L2_CTRL_TYPE_VP9_FRAME:
1346 		return validate_vp9_frame(p);
1347 	case V4L2_CTRL_TYPE_AV1_FRAME:
1348 		return validate_av1_frame(p);
1349 	case V4L2_CTRL_TYPE_AV1_SEQUENCE:
1350 		return validate_av1_sequence(p);
1351 	case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
1352 		break;
1353 	case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
1354 		return validate_av1_film_grain(p);
1355 
1356 	case V4L2_CTRL_TYPE_AREA:
1357 		area = p;
1358 		if (!area->width || !area->height)
1359 			return -EINVAL;
1360 		break;
1361 
1362 	case V4L2_CTRL_TYPE_RECT:
1363 		rect = p;
1364 		if (!rect->width || !rect->height)
1365 			return -EINVAL;
1366 		break;
1367 
1368 	default:
1369 		return -EINVAL;
1370 	}
1371 
1372 	return 0;
1373 }
1374 
1375 static int std_validate_elem(const struct v4l2_ctrl *ctrl, u32 idx,
1376 			     union v4l2_ctrl_ptr ptr)
1377 {
1378 	size_t len;
1379 	u64 offset;
1380 	s64 val;
1381 
1382 	switch ((u32)ctrl->type) {
1383 	case V4L2_CTRL_TYPE_INTEGER:
1384 		return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
1385 	case V4L2_CTRL_TYPE_INTEGER64:
1386 		/*
1387 		 * We can't use the ROUND_TO_RANGE define here due to
1388 		 * the u64 divide that needs special care.
1389 		 */
1390 		val = ptr.p_s64[idx];
1391 		if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
1392 			val = ctrl->maximum;
1393 		else
1394 			val += (s64)(ctrl->step / 2);
1395 		val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
1396 		offset = val - ctrl->minimum;
1397 		do_div(offset, ctrl->step);
1398 		ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
1399 		return 0;
1400 	case V4L2_CTRL_TYPE_U8:
1401 		return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
1402 	case V4L2_CTRL_TYPE_U16:
1403 		return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
1404 	case V4L2_CTRL_TYPE_U32:
1405 		return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
1406 
1407 	case V4L2_CTRL_TYPE_BOOLEAN:
1408 		ptr.p_s32[idx] = !!ptr.p_s32[idx];
1409 		return 0;
1410 
1411 	case V4L2_CTRL_TYPE_MENU:
1412 	case V4L2_CTRL_TYPE_INTEGER_MENU:
1413 		if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
1414 			return -ERANGE;
1415 		if (ptr.p_s32[idx] < BITS_PER_LONG_LONG &&
1416 		    (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx])))
1417 			return -EINVAL;
1418 		if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1419 		    ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
1420 			return -EINVAL;
1421 		return 0;
1422 
1423 	case V4L2_CTRL_TYPE_BITMASK:
1424 		ptr.p_s32[idx] &= ctrl->maximum;
1425 		return 0;
1426 
1427 	case V4L2_CTRL_TYPE_BUTTON:
1428 	case V4L2_CTRL_TYPE_CTRL_CLASS:
1429 		ptr.p_s32[idx] = 0;
1430 		return 0;
1431 
1432 	case V4L2_CTRL_TYPE_STRING:
1433 		idx *= ctrl->elem_size;
1434 		len = strlen(ptr.p_char + idx);
1435 		if (len < ctrl->minimum)
1436 			return -ERANGE;
1437 		if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
1438 			return -ERANGE;
1439 		return 0;
1440 
1441 	default:
1442 		return std_validate_compound(ctrl, idx, ptr);
1443 	}
1444 }
1445 
1446 int v4l2_ctrl_type_op_validate(const struct v4l2_ctrl *ctrl,
1447 			       union v4l2_ctrl_ptr ptr)
1448 {
1449 	unsigned int i;
1450 	int ret = 0;
1451 
1452 	switch ((u32)ctrl->type) {
1453 	case V4L2_CTRL_TYPE_U8:
1454 		if (ctrl->maximum == 0xff && ctrl->minimum == 0 && ctrl->step == 1)
1455 			return 0;
1456 		break;
1457 	case V4L2_CTRL_TYPE_U16:
1458 		if (ctrl->maximum == 0xffff && ctrl->minimum == 0 && ctrl->step == 1)
1459 			return 0;
1460 		break;
1461 	case V4L2_CTRL_TYPE_U32:
1462 		if (ctrl->maximum == 0xffffffff && ctrl->minimum == 0 && ctrl->step == 1)
1463 			return 0;
1464 		break;
1465 
1466 	case V4L2_CTRL_TYPE_BUTTON:
1467 	case V4L2_CTRL_TYPE_CTRL_CLASS:
1468 		memset(ptr.p_s32, 0, ctrl->new_elems * sizeof(s32));
1469 		return 0;
1470 	}
1471 
1472 	for (i = 0; !ret && i < ctrl->new_elems; i++)
1473 		ret = std_validate_elem(ctrl, i, ptr);
1474 	return ret;
1475 }
1476 EXPORT_SYMBOL(v4l2_ctrl_type_op_validate);
1477 
1478 static const struct v4l2_ctrl_type_ops std_type_ops = {
1479 	.equal = v4l2_ctrl_type_op_equal,
1480 	.init = v4l2_ctrl_type_op_init,
1481 	.minimum = v4l2_ctrl_type_op_minimum,
1482 	.maximum = v4l2_ctrl_type_op_maximum,
1483 	.log = v4l2_ctrl_type_op_log,
1484 	.validate = v4l2_ctrl_type_op_validate,
1485 };
1486 
1487 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
1488 {
1489 	if (!ctrl)
1490 		return;
1491 	if (!notify) {
1492 		ctrl->call_notify = 0;
1493 		return;
1494 	}
1495 	if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
1496 		return;
1497 	ctrl->handler->notify = notify;
1498 	ctrl->handler->notify_priv = priv;
1499 	ctrl->call_notify = 1;
1500 }
1501 EXPORT_SYMBOL(v4l2_ctrl_notify);
1502 
1503 /* Copy the one value to another. */
1504 static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
1505 		       union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to,
1506 		       unsigned int elems)
1507 {
1508 	if (ctrl == NULL)
1509 		return;
1510 	memcpy(to.p, from.p_const, elems * ctrl->elem_size);
1511 }
1512 
1513 /* Copy the new value to the current value. */
1514 void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
1515 {
1516 	bool changed;
1517 
1518 	if (ctrl == NULL)
1519 		return;
1520 
1521 	/* has_changed is set by cluster_changed */
1522 	changed = ctrl->has_changed;
1523 	if (changed) {
1524 		if (ctrl->is_dyn_array)
1525 			ctrl->elems = ctrl->new_elems;
1526 		ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur, ctrl->elems);
1527 	}
1528 
1529 	if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
1530 		/* Note: CH_FLAGS is only set for auto clusters. */
1531 		ctrl->flags &=
1532 			~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
1533 		if (!is_cur_manual(ctrl->cluster[0])) {
1534 			ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1535 			if (ctrl->cluster[0]->has_volatiles)
1536 				ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1537 		}
1538 		fh = NULL;
1539 	}
1540 	if (changed || ch_flags) {
1541 		/* If a control was changed that was not one of the controls
1542 		   modified by the application, then send the event to all. */
1543 		if (!ctrl->is_new)
1544 			fh = NULL;
1545 		send_event(fh, ctrl,
1546 			(changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
1547 		if (ctrl->call_notify && changed && ctrl->handler->notify)
1548 			ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
1549 	}
1550 }
1551 
1552 /* Copy the current value to the new value */
1553 void cur_to_new(struct v4l2_ctrl *ctrl)
1554 {
1555 	if (ctrl == NULL)
1556 		return;
1557 	if (ctrl->is_dyn_array)
1558 		ctrl->new_elems = ctrl->elems;
1559 	ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems);
1560 }
1561 
1562 static bool req_alloc_array(struct v4l2_ctrl_ref *ref, u32 elems)
1563 {
1564 	void *tmp;
1565 
1566 	if (elems == ref->p_req_array_alloc_elems)
1567 		return true;
1568 	if (ref->ctrl->is_dyn_array &&
1569 	    elems < ref->p_req_array_alloc_elems)
1570 		return true;
1571 
1572 	tmp = kvmalloc(elems * ref->ctrl->elem_size, GFP_KERNEL);
1573 
1574 	if (!tmp) {
1575 		ref->p_req_array_enomem = true;
1576 		return false;
1577 	}
1578 	ref->p_req_array_enomem = false;
1579 	kvfree(ref->p_req.p);
1580 	ref->p_req.p = tmp;
1581 	ref->p_req_array_alloc_elems = elems;
1582 	return true;
1583 }
1584 
1585 /* Copy the new value to the request value */
1586 void new_to_req(struct v4l2_ctrl_ref *ref)
1587 {
1588 	struct v4l2_ctrl *ctrl;
1589 
1590 	if (!ref)
1591 		return;
1592 
1593 	ctrl = ref->ctrl;
1594 	if (ctrl->is_array && !req_alloc_array(ref, ctrl->new_elems))
1595 		return;
1596 
1597 	ref->p_req_elems = ctrl->new_elems;
1598 	ptr_to_ptr(ctrl, ctrl->p_new, ref->p_req, ref->p_req_elems);
1599 	ref->p_req_valid = true;
1600 }
1601 
1602 /* Copy the current value to the request value */
1603 void cur_to_req(struct v4l2_ctrl_ref *ref)
1604 {
1605 	struct v4l2_ctrl *ctrl;
1606 
1607 	if (!ref)
1608 		return;
1609 
1610 	ctrl = ref->ctrl;
1611 	if (ctrl->is_array && !req_alloc_array(ref, ctrl->elems))
1612 		return;
1613 
1614 	ref->p_req_elems = ctrl->elems;
1615 	ptr_to_ptr(ctrl, ctrl->p_cur, ref->p_req, ctrl->elems);
1616 	ref->p_req_valid = true;
1617 }
1618 
1619 /* Copy the request value to the new value */
1620 int req_to_new(struct v4l2_ctrl_ref *ref)
1621 {
1622 	struct v4l2_ctrl *ctrl;
1623 
1624 	if (!ref)
1625 		return 0;
1626 
1627 	ctrl = ref->ctrl;
1628 
1629 	/*
1630 	 * This control was never set in the request, so just use the current
1631 	 * value.
1632 	 */
1633 	if (!ref->p_req_valid) {
1634 		if (ctrl->is_dyn_array)
1635 			ctrl->new_elems = ctrl->elems;
1636 		ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems);
1637 		return 0;
1638 	}
1639 
1640 	/* Not an array, so just copy the request value */
1641 	if (!ctrl->is_array) {
1642 		ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems);
1643 		return 0;
1644 	}
1645 
1646 	/* Sanity check, should never happen */
1647 	if (WARN_ON(!ref->p_req_array_alloc_elems))
1648 		return -ENOMEM;
1649 
1650 	if (!ctrl->is_dyn_array &&
1651 	    ref->p_req_elems != ctrl->p_array_alloc_elems)
1652 		return -ENOMEM;
1653 
1654 	/*
1655 	 * Check if the number of elements in the request is more than the
1656 	 * elements in ctrl->p_array. If so, attempt to realloc ctrl->p_array.
1657 	 * Note that p_array is allocated with twice the number of elements
1658 	 * in the dynamic array since it has to store both the current and
1659 	 * new value of such a control.
1660 	 */
1661 	if (ref->p_req_elems > ctrl->p_array_alloc_elems) {
1662 		unsigned int sz = ref->p_req_elems * ctrl->elem_size;
1663 		void *old = ctrl->p_array;
1664 		void *tmp = kvzalloc(2 * sz, GFP_KERNEL);
1665 
1666 		if (!tmp)
1667 			return -ENOMEM;
1668 		memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size);
1669 		memcpy(tmp + sz, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size);
1670 		ctrl->p_new.p = tmp;
1671 		ctrl->p_cur.p = tmp + sz;
1672 		ctrl->p_array = tmp;
1673 		ctrl->p_array_alloc_elems = ref->p_req_elems;
1674 		kvfree(old);
1675 	}
1676 
1677 	ctrl->new_elems = ref->p_req_elems;
1678 	ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems);
1679 	return 0;
1680 }
1681 
1682 /* Control range checking */
1683 int check_range(enum v4l2_ctrl_type type,
1684 		s64 min, s64 max, u64 step, s64 def)
1685 {
1686 	switch (type) {
1687 	case V4L2_CTRL_TYPE_BOOLEAN:
1688 		if (step != 1 || max > 1 || min < 0)
1689 			return -ERANGE;
1690 		fallthrough;
1691 	case V4L2_CTRL_TYPE_U8:
1692 	case V4L2_CTRL_TYPE_U16:
1693 	case V4L2_CTRL_TYPE_U32:
1694 	case V4L2_CTRL_TYPE_INTEGER:
1695 	case V4L2_CTRL_TYPE_INTEGER64:
1696 		if (step == 0 || min > max || def < min || def > max)
1697 			return -ERANGE;
1698 		return 0;
1699 	case V4L2_CTRL_TYPE_BITMASK:
1700 		if (step || min || !max || (def & ~max))
1701 			return -ERANGE;
1702 		return 0;
1703 	case V4L2_CTRL_TYPE_MENU:
1704 	case V4L2_CTRL_TYPE_INTEGER_MENU:
1705 		if (min > max || def < min || def > max ||
1706 		    min < 0 || (step && max >= BITS_PER_LONG_LONG))
1707 			return -ERANGE;
1708 		/* Note: step == menu_skip_mask for menu controls.
1709 		   So here we check if the default value is masked out. */
1710 		if (def < BITS_PER_LONG_LONG && (step & BIT_ULL(def)))
1711 			return -EINVAL;
1712 		return 0;
1713 	case V4L2_CTRL_TYPE_STRING:
1714 		if (min > max || min < 0 || step < 1 || def)
1715 			return -ERANGE;
1716 		return 0;
1717 	default:
1718 		return 0;
1719 	}
1720 }
1721 
1722 /* Set the handler's error code if it wasn't set earlier already */
1723 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1724 {
1725 	if (hdl->error == 0)
1726 		hdl->error = err;
1727 	return err;
1728 }
1729 
1730 /* Initialize the handler */
1731 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
1732 				 unsigned nr_of_controls_hint,
1733 				 struct lock_class_key *key, const char *name)
1734 {
1735 	mutex_init(&hdl->_lock);
1736 	hdl->lock = &hdl->_lock;
1737 	lockdep_set_class_and_name(hdl->lock, key, name);
1738 	INIT_LIST_HEAD(&hdl->ctrls);
1739 	INIT_LIST_HEAD(&hdl->ctrl_refs);
1740 	hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
1741 	hdl->buckets = kvzalloc_objs(hdl->buckets[0], hdl->nr_of_buckets);
1742 	hdl->error = hdl->buckets ? 0 : -ENOMEM;
1743 	v4l2_ctrl_handler_init_request(hdl);
1744 	return hdl->error;
1745 }
1746 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
1747 
1748 /* Free all controls and control refs */
1749 int v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1750 {
1751 	struct v4l2_ctrl_ref *ref, *next_ref;
1752 	struct v4l2_ctrl *ctrl, *next_ctrl;
1753 	struct v4l2_subscribed_event *sev, *next_sev;
1754 
1755 	if (!hdl)
1756 		return 0;
1757 
1758 	if (!hdl->buckets)
1759 		return hdl->error;
1760 
1761 	v4l2_ctrl_handler_free_request(hdl);
1762 
1763 	mutex_lock(hdl->lock);
1764 	/* Free all nodes */
1765 	list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1766 		list_del(&ref->node);
1767 		if (ref->p_req_array_alloc_elems)
1768 			kvfree(ref->p_req.p);
1769 		kfree(ref);
1770 	}
1771 	/* Free all controls owned by the handler */
1772 	list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1773 		list_del(&ctrl->node);
1774 		list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1775 			list_del(&sev->node);
1776 		kvfree(ctrl->p_array);
1777 		kvfree(ctrl);
1778 	}
1779 	kvfree(hdl->buckets);
1780 	hdl->buckets = NULL;
1781 	hdl->cached = NULL;
1782 	mutex_unlock(hdl->lock);
1783 	mutex_destroy(&hdl->_lock);
1784 
1785 	return hdl->error;
1786 }
1787 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1788 
1789 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1790    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1791    with applications that do not use the NEXT_CTRL flag.
1792 
1793    We just find the n-th private user control. It's O(N), but that should not
1794    be an issue in this particular case. */
1795 static struct v4l2_ctrl_ref *find_private_ref(
1796 		struct v4l2_ctrl_handler *hdl, u32 id)
1797 {
1798 	struct v4l2_ctrl_ref *ref;
1799 
1800 	id -= V4L2_CID_PRIVATE_BASE;
1801 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1802 		/* Search for private user controls that are compatible with
1803 		   VIDIOC_G/S_CTRL. */
1804 		if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1805 		    V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1806 			if (!ref->ctrl->is_int)
1807 				continue;
1808 			if (id == 0)
1809 				return ref;
1810 			id--;
1811 		}
1812 	}
1813 	return NULL;
1814 }
1815 
1816 /* Find a control with the given ID. */
1817 struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1818 {
1819 	struct v4l2_ctrl_ref *ref;
1820 	int bucket;
1821 
1822 	id &= V4L2_CTRL_ID_MASK;
1823 
1824 	/* Old-style private controls need special handling */
1825 	if (id >= V4L2_CID_PRIVATE_BASE)
1826 		return find_private_ref(hdl, id);
1827 	bucket = id % hdl->nr_of_buckets;
1828 
1829 	/* Simple optimization: cache the last control found */
1830 	if (hdl->cached && hdl->cached->ctrl->id == id)
1831 		return hdl->cached;
1832 
1833 	/* Not in cache, search the hash */
1834 	ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1835 	while (ref && ref->ctrl->id != id)
1836 		ref = ref->next;
1837 
1838 	if (ref)
1839 		hdl->cached = ref; /* cache it! */
1840 	return ref;
1841 }
1842 
1843 /* Find a control with the given ID. Take the handler's lock first. */
1844 struct v4l2_ctrl_ref *find_ref_lock(struct v4l2_ctrl_handler *hdl, u32 id)
1845 {
1846 	struct v4l2_ctrl_ref *ref = NULL;
1847 
1848 	if (hdl) {
1849 		mutex_lock(hdl->lock);
1850 		ref = find_ref(hdl, id);
1851 		mutex_unlock(hdl->lock);
1852 	}
1853 	return ref;
1854 }
1855 
1856 /* Find a control with the given ID. */
1857 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1858 {
1859 	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1860 
1861 	return ref ? ref->ctrl : NULL;
1862 }
1863 EXPORT_SYMBOL(v4l2_ctrl_find);
1864 
1865 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1866 int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1867 		    struct v4l2_ctrl *ctrl,
1868 		    struct v4l2_ctrl_ref **ctrl_ref,
1869 		    bool from_other_dev, bool allocate_req)
1870 {
1871 	struct v4l2_ctrl_ref *ref;
1872 	struct v4l2_ctrl_ref *new_ref;
1873 	u32 id = ctrl->id;
1874 	u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
1875 	int bucket = id % hdl->nr_of_buckets;	/* which bucket to use */
1876 	unsigned int size_extra_req = 0;
1877 
1878 	if (ctrl_ref)
1879 		*ctrl_ref = NULL;
1880 
1881 	/*
1882 	 * Automatically add the control class if it is not yet present and
1883 	 * the new control is not a compound control.
1884 	 */
1885 	if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
1886 	    id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1887 		if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1888 			return hdl->error;
1889 
1890 	if (hdl->error)
1891 		return hdl->error;
1892 
1893 	if (allocate_req && !ctrl->is_array)
1894 		size_extra_req = ctrl->elems * ctrl->elem_size;
1895 	new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
1896 	if (!new_ref)
1897 		return handler_set_err(hdl, -ENOMEM);
1898 	new_ref->ctrl = ctrl;
1899 	new_ref->from_other_dev = from_other_dev;
1900 	if (size_extra_req)
1901 		new_ref->p_req.p = &new_ref[1];
1902 
1903 	INIT_LIST_HEAD(&new_ref->node);
1904 
1905 	mutex_lock(hdl->lock);
1906 
1907 	/* Add immediately at the end of the list if the list is empty, or if
1908 	   the last element in the list has a lower ID.
1909 	   This ensures that when elements are added in ascending order the
1910 	   insertion is an O(1) operation. */
1911 	if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1912 		list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1913 		goto insert_in_hash;
1914 	}
1915 
1916 	/* Find insert position in sorted list */
1917 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1918 		if (ref->ctrl->id < id)
1919 			continue;
1920 		/* Don't add duplicates */
1921 		if (ref->ctrl->id == id) {
1922 			kfree(new_ref);
1923 			goto unlock;
1924 		}
1925 		list_add(&new_ref->node, ref->node.prev);
1926 		break;
1927 	}
1928 
1929 insert_in_hash:
1930 	/* Insert the control node in the hash */
1931 	new_ref->next = hdl->buckets[bucket];
1932 	hdl->buckets[bucket] = new_ref;
1933 	if (ctrl_ref)
1934 		*ctrl_ref = new_ref;
1935 	if (ctrl->handler == hdl) {
1936 		/* By default each control starts in a cluster of its own.
1937 		 * new_ref->ctrl is basically a cluster array with one
1938 		 * element, so that's perfect to use as the cluster pointer.
1939 		 * But only do this for the handler that owns the control.
1940 		 */
1941 		ctrl->cluster = &new_ref->ctrl;
1942 		ctrl->ncontrols = 1;
1943 	}
1944 
1945 unlock:
1946 	mutex_unlock(hdl->lock);
1947 	return 0;
1948 }
1949 
1950 /* Add a new control */
1951 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1952 			const struct v4l2_ctrl_ops *ops,
1953 			const struct v4l2_ctrl_type_ops *type_ops,
1954 			u32 id, const char *name, enum v4l2_ctrl_type type,
1955 			s64 min, s64 max, u64 step, s64 def,
1956 			const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
1957 			u32 flags, const char * const *qmenu,
1958 			const s64 *qmenu_int,
1959 			const union v4l2_ctrl_ptr p_def,
1960 			const union v4l2_ctrl_ptr p_min,
1961 			const union v4l2_ctrl_ptr p_max,
1962 			void *priv)
1963 {
1964 	struct v4l2_ctrl *ctrl;
1965 	unsigned sz_extra;
1966 	unsigned nr_of_dims = 0;
1967 	unsigned elems = 1;
1968 	bool is_array;
1969 	unsigned tot_ctrl_size;
1970 	void *data;
1971 	int err;
1972 
1973 	if (hdl->error)
1974 		return NULL;
1975 
1976 	while (dims && dims[nr_of_dims]) {
1977 		elems *= dims[nr_of_dims];
1978 		nr_of_dims++;
1979 		if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
1980 			break;
1981 	}
1982 	is_array = nr_of_dims > 0;
1983 
1984 	/* Prefill elem_size for all types handled by std_type_ops */
1985 	switch ((u32)type) {
1986 	case V4L2_CTRL_TYPE_INTEGER64:
1987 		elem_size = sizeof(s64);
1988 		break;
1989 	case V4L2_CTRL_TYPE_STRING:
1990 		elem_size = max + 1;
1991 		break;
1992 	case V4L2_CTRL_TYPE_U8:
1993 		elem_size = sizeof(u8);
1994 		break;
1995 	case V4L2_CTRL_TYPE_U16:
1996 		elem_size = sizeof(u16);
1997 		break;
1998 	case V4L2_CTRL_TYPE_U32:
1999 		elem_size = sizeof(u32);
2000 		break;
2001 	case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
2002 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_sequence);
2003 		break;
2004 	case V4L2_CTRL_TYPE_MPEG2_PICTURE:
2005 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_picture);
2006 		break;
2007 	case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
2008 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantisation);
2009 		break;
2010 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
2011 		elem_size = sizeof(struct v4l2_ctrl_fwht_params);
2012 		break;
2013 	case V4L2_CTRL_TYPE_H264_SPS:
2014 		elem_size = sizeof(struct v4l2_ctrl_h264_sps);
2015 		break;
2016 	case V4L2_CTRL_TYPE_H264_PPS:
2017 		elem_size = sizeof(struct v4l2_ctrl_h264_pps);
2018 		break;
2019 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
2020 		elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix);
2021 		break;
2022 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
2023 		elem_size = sizeof(struct v4l2_ctrl_h264_slice_params);
2024 		break;
2025 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
2026 		elem_size = sizeof(struct v4l2_ctrl_h264_decode_params);
2027 		break;
2028 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
2029 		elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights);
2030 		break;
2031 	case V4L2_CTRL_TYPE_VP8_FRAME:
2032 		elem_size = sizeof(struct v4l2_ctrl_vp8_frame);
2033 		break;
2034 	case V4L2_CTRL_TYPE_HEVC_SPS:
2035 		elem_size = sizeof(struct v4l2_ctrl_hevc_sps);
2036 		break;
2037 	case V4L2_CTRL_TYPE_HEVC_PPS:
2038 		elem_size = sizeof(struct v4l2_ctrl_hevc_pps);
2039 		break;
2040 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
2041 		elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params);
2042 		break;
2043 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_ST_RPS:
2044 		elem_size = sizeof(struct v4l2_ctrl_hevc_ext_sps_st_rps);
2045 		break;
2046 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS:
2047 		elem_size = sizeof(struct v4l2_ctrl_hevc_ext_sps_lt_rps);
2048 		break;
2049 	case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
2050 		elem_size = sizeof(struct v4l2_ctrl_hevc_scaling_matrix);
2051 		break;
2052 	case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
2053 		elem_size = sizeof(struct v4l2_ctrl_hevc_decode_params);
2054 		break;
2055 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
2056 		elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info);
2057 		break;
2058 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
2059 		elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display);
2060 		break;
2061 	case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
2062 		elem_size = sizeof(struct v4l2_ctrl_vp9_compressed_hdr);
2063 		break;
2064 	case V4L2_CTRL_TYPE_VP9_FRAME:
2065 		elem_size = sizeof(struct v4l2_ctrl_vp9_frame);
2066 		break;
2067 	case V4L2_CTRL_TYPE_AV1_SEQUENCE:
2068 		elem_size = sizeof(struct v4l2_ctrl_av1_sequence);
2069 		break;
2070 	case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
2071 		elem_size = sizeof(struct v4l2_ctrl_av1_tile_group_entry);
2072 		break;
2073 	case V4L2_CTRL_TYPE_AV1_FRAME:
2074 		elem_size = sizeof(struct v4l2_ctrl_av1_frame);
2075 		break;
2076 	case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
2077 		elem_size = sizeof(struct v4l2_ctrl_av1_film_grain);
2078 		break;
2079 	case V4L2_CTRL_TYPE_AREA:
2080 		elem_size = sizeof(struct v4l2_area);
2081 		break;
2082 	case V4L2_CTRL_TYPE_RECT:
2083 		elem_size = sizeof(struct v4l2_rect);
2084 		break;
2085 	default:
2086 		if (type < V4L2_CTRL_COMPOUND_TYPES)
2087 			elem_size = sizeof(s32);
2088 		break;
2089 	}
2090 
2091 	if (type < V4L2_CTRL_COMPOUND_TYPES &&
2092 	    type != V4L2_CTRL_TYPE_BUTTON &&
2093 	    type != V4L2_CTRL_TYPE_CTRL_CLASS &&
2094 	    type != V4L2_CTRL_TYPE_STRING)
2095 		flags |= V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX;
2096 
2097 	/* Sanity checks */
2098 	if (id == 0 || name == NULL || !elem_size ||
2099 	    id >= V4L2_CID_PRIVATE_BASE ||
2100 	    (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
2101 	    (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
2102 		handler_set_err(hdl, -ERANGE);
2103 		return NULL;
2104 	}
2105 
2106 	err = check_range(type, min, max, step, def);
2107 	if (err) {
2108 		handler_set_err(hdl, err);
2109 		return NULL;
2110 	}
2111 	if (is_array &&
2112 	    (type == V4L2_CTRL_TYPE_BUTTON ||
2113 	     type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
2114 		handler_set_err(hdl, -EINVAL);
2115 		return NULL;
2116 	}
2117 	if (flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY) {
2118 		/*
2119 		 * For now only support this for one-dimensional arrays only.
2120 		 *
2121 		 * This can be relaxed in the future, but this will
2122 		 * require more effort.
2123 		 */
2124 		if (nr_of_dims != 1) {
2125 			handler_set_err(hdl, -EINVAL);
2126 			return NULL;
2127 		}
2128 		/* Start with just 1 element */
2129 		elems = 1;
2130 	}
2131 
2132 	tot_ctrl_size = elem_size * elems;
2133 	sz_extra = 0;
2134 	if (type == V4L2_CTRL_TYPE_BUTTON)
2135 		flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
2136 			V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
2137 	else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
2138 		flags |= V4L2_CTRL_FLAG_READ_ONLY;
2139 	else if (!is_array &&
2140 		 (type == V4L2_CTRL_TYPE_INTEGER64 ||
2141 		  type == V4L2_CTRL_TYPE_STRING ||
2142 		  type >= V4L2_CTRL_COMPOUND_TYPES))
2143 		sz_extra += 2 * tot_ctrl_size;
2144 
2145 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const)
2146 		sz_extra += elem_size;
2147 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_min.p_const)
2148 		sz_extra += elem_size;
2149 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_max.p_const)
2150 		sz_extra += elem_size;
2151 
2152 	ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
2153 	if (ctrl == NULL) {
2154 		handler_set_err(hdl, -ENOMEM);
2155 		return NULL;
2156 	}
2157 
2158 	INIT_LIST_HEAD(&ctrl->node);
2159 	INIT_LIST_HEAD(&ctrl->ev_subs);
2160 	ctrl->handler = hdl;
2161 	ctrl->ops = ops;
2162 	ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
2163 	ctrl->id = id;
2164 	ctrl->name = name;
2165 	ctrl->type = type;
2166 	ctrl->flags = flags;
2167 	ctrl->minimum = min;
2168 	ctrl->maximum = max;
2169 	ctrl->step = step;
2170 	ctrl->default_value = def;
2171 	ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
2172 	ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
2173 	ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
2174 	ctrl->is_array = is_array;
2175 	ctrl->is_dyn_array = !!(flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY);
2176 	ctrl->elems = elems;
2177 	ctrl->new_elems = elems;
2178 	ctrl->nr_of_dims = nr_of_dims;
2179 	if (nr_of_dims)
2180 		memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
2181 	ctrl->elem_size = elem_size;
2182 	if (type == V4L2_CTRL_TYPE_MENU)
2183 		ctrl->qmenu = qmenu;
2184 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2185 		ctrl->qmenu_int = qmenu_int;
2186 	ctrl->priv = priv;
2187 	ctrl->cur.val = ctrl->val = def;
2188 	data = &ctrl[1];
2189 
2190 	if (ctrl->is_array) {
2191 		ctrl->p_array_alloc_elems = elems;
2192 		ctrl->p_array = kvzalloc(2 * elems * elem_size, GFP_KERNEL);
2193 		if (!ctrl->p_array) {
2194 			kvfree(ctrl);
2195 			return NULL;
2196 		}
2197 		data = ctrl->p_array;
2198 	}
2199 
2200 	if (!ctrl->is_int) {
2201 		ctrl->p_new.p = data;
2202 		ctrl->p_cur.p = data + tot_ctrl_size;
2203 	} else {
2204 		ctrl->p_new.p = &ctrl->val;
2205 		ctrl->p_cur.p = &ctrl->cur.val;
2206 	}
2207 
2208 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) {
2209 		if (ctrl->is_array)
2210 			ctrl->p_def.p = &ctrl[1];
2211 		else
2212 			ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size;
2213 		memcpy(ctrl->p_def.p, p_def.p_const, elem_size);
2214 	}
2215 
2216 	if (flags & V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX) {
2217 		void *ptr = ctrl->p_def.p;
2218 
2219 		if (p_min.p_const) {
2220 			ptr += elem_size;
2221 			ctrl->p_min.p = ptr;
2222 			memcpy(ctrl->p_min.p, p_min.p_const, elem_size);
2223 		}
2224 
2225 		if (p_max.p_const) {
2226 			ptr += elem_size;
2227 			ctrl->p_max.p = ptr;
2228 			memcpy(ctrl->p_max.p, p_max.p_const, elem_size);
2229 		}
2230 	}
2231 
2232 	ctrl->type_ops->init(ctrl, 0, ctrl->p_cur);
2233 	cur_to_new(ctrl);
2234 
2235 	if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
2236 		kvfree(ctrl->p_array);
2237 		kvfree(ctrl);
2238 		return NULL;
2239 	}
2240 	mutex_lock(hdl->lock);
2241 	list_add_tail(&ctrl->node, &hdl->ctrls);
2242 	mutex_unlock(hdl->lock);
2243 	return ctrl;
2244 }
2245 
2246 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
2247 			const struct v4l2_ctrl_config *cfg, void *priv)
2248 {
2249 	bool is_menu;
2250 	struct v4l2_ctrl *ctrl;
2251 	const char *name = cfg->name;
2252 	const char * const *qmenu = cfg->qmenu;
2253 	const s64 *qmenu_int = cfg->qmenu_int;
2254 	enum v4l2_ctrl_type type = cfg->type;
2255 	u32 flags = cfg->flags;
2256 	s64 min = cfg->min;
2257 	s64 max = cfg->max;
2258 	u64 step = cfg->step;
2259 	s64 def = cfg->def;
2260 
2261 	if (name == NULL)
2262 		v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
2263 								&def, &flags);
2264 
2265 	is_menu = (type == V4L2_CTRL_TYPE_MENU ||
2266 		   type == V4L2_CTRL_TYPE_INTEGER_MENU);
2267 	if (is_menu)
2268 		WARN_ON(step);
2269 	else
2270 		WARN_ON(cfg->menu_skip_mask);
2271 	if (type == V4L2_CTRL_TYPE_MENU && !qmenu) {
2272 		qmenu = v4l2_ctrl_get_menu(cfg->id);
2273 	} else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) {
2274 		handler_set_err(hdl, -EINVAL);
2275 		return NULL;
2276 	}
2277 
2278 	ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
2279 			type, min, max,
2280 			is_menu ? cfg->menu_skip_mask : step, def,
2281 			cfg->dims, cfg->elem_size,
2282 			flags, qmenu, qmenu_int, cfg->p_def, cfg->p_min,
2283 			cfg->p_max, priv);
2284 	if (ctrl)
2285 		ctrl->is_private = cfg->is_private;
2286 	return ctrl;
2287 }
2288 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
2289 
2290 /* Helper function for standard non-menu controls */
2291 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
2292 			const struct v4l2_ctrl_ops *ops,
2293 			u32 id, s64 min, s64 max, u64 step, s64 def)
2294 {
2295 	const char *name;
2296 	enum v4l2_ctrl_type type;
2297 	u32 flags;
2298 
2299 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2300 	if (type == V4L2_CTRL_TYPE_MENU ||
2301 	    type == V4L2_CTRL_TYPE_INTEGER_MENU ||
2302 	    type >= V4L2_CTRL_COMPOUND_TYPES) {
2303 		handler_set_err(hdl, -EINVAL);
2304 		return NULL;
2305 	}
2306 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2307 			     min, max, step, def, NULL, 0,
2308 			     flags, NULL, NULL, ptr_null, ptr_null,
2309 			     ptr_null, NULL);
2310 }
2311 EXPORT_SYMBOL(v4l2_ctrl_new_std);
2312 
2313 /* Helper function for standard menu controls */
2314 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
2315 			const struct v4l2_ctrl_ops *ops,
2316 			u32 id, u8 _max, u64 mask, u8 _def)
2317 {
2318 	const char * const *qmenu = NULL;
2319 	const s64 *qmenu_int = NULL;
2320 	unsigned int qmenu_int_len = 0;
2321 	const char *name;
2322 	enum v4l2_ctrl_type type;
2323 	s64 min;
2324 	s64 max = _max;
2325 	s64 def = _def;
2326 	u64 step;
2327 	u32 flags;
2328 
2329 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2330 
2331 	if (type == V4L2_CTRL_TYPE_MENU)
2332 		qmenu = v4l2_ctrl_get_menu(id);
2333 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2334 		qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
2335 
2336 	if ((!qmenu && !qmenu_int) || (qmenu_int && max >= qmenu_int_len)) {
2337 		handler_set_err(hdl, -EINVAL);
2338 		return NULL;
2339 	}
2340 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2341 			     0, max, mask, def, NULL, 0,
2342 			     flags, qmenu, qmenu_int, ptr_null, ptr_null,
2343 			     ptr_null, NULL);
2344 }
2345 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
2346 
2347 /* Helper function for standard menu controls with driver defined menu */
2348 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
2349 			const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
2350 			u64 mask, u8 _def, const char * const *qmenu)
2351 {
2352 	enum v4l2_ctrl_type type;
2353 	const char *name;
2354 	u32 flags;
2355 	u64 step;
2356 	s64 min;
2357 	s64 max = _max;
2358 	s64 def = _def;
2359 
2360 	/* v4l2_ctrl_new_std_menu_items() should only be called for
2361 	 * standard controls without a standard menu.
2362 	 */
2363 	if (v4l2_ctrl_get_menu(id)) {
2364 		handler_set_err(hdl, -EINVAL);
2365 		return NULL;
2366 	}
2367 
2368 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2369 	if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
2370 		handler_set_err(hdl, -EINVAL);
2371 		return NULL;
2372 	}
2373 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2374 			     0, max, mask, def, NULL, 0,
2375 			     flags, qmenu, NULL, ptr_null, ptr_null,
2376 			     ptr_null, NULL);
2377 
2378 }
2379 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
2380 
2381 /* Helper function for standard compound controls */
2382 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
2383 				const struct v4l2_ctrl_ops *ops, u32 id,
2384 				const union v4l2_ctrl_ptr p_def,
2385 				const union v4l2_ctrl_ptr p_min,
2386 				const union v4l2_ctrl_ptr p_max)
2387 {
2388 	const char *name;
2389 	enum v4l2_ctrl_type type;
2390 	u32 flags;
2391 	s64 min, max, step, def;
2392 
2393 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2394 	if (type < V4L2_CTRL_COMPOUND_TYPES) {
2395 		handler_set_err(hdl, -EINVAL);
2396 		return NULL;
2397 	}
2398 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2399 			     min, max, step, def, NULL, 0,
2400 			     flags, NULL, NULL, p_def, p_min, p_max, NULL);
2401 }
2402 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound);
2403 
2404 /* Helper function for standard integer menu controls */
2405 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
2406 			const struct v4l2_ctrl_ops *ops,
2407 			u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
2408 {
2409 	const char *name;
2410 	enum v4l2_ctrl_type type;
2411 	s64 min;
2412 	u64 step;
2413 	s64 max = _max;
2414 	s64 def = _def;
2415 	u32 flags;
2416 
2417 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2418 	if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
2419 		handler_set_err(hdl, -EINVAL);
2420 		return NULL;
2421 	}
2422 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2423 			     0, max, 0, def, NULL, 0,
2424 			     flags, NULL, qmenu_int, ptr_null, ptr_null,
2425 			     ptr_null, NULL);
2426 }
2427 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
2428 
2429 /* Add the controls from another handler to our own. */
2430 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
2431 			  struct v4l2_ctrl_handler *add,
2432 			  bool (*filter)(const struct v4l2_ctrl *ctrl),
2433 			  bool from_other_dev)
2434 {
2435 	struct v4l2_ctrl_ref *ref;
2436 	int ret = 0;
2437 
2438 	/* Do nothing if either handler is NULL or if they are the same */
2439 	if (!hdl || !add || hdl == add)
2440 		return 0;
2441 	if (hdl->error)
2442 		return hdl->error;
2443 	mutex_lock(add->lock);
2444 	list_for_each_entry(ref, &add->ctrl_refs, node) {
2445 		struct v4l2_ctrl *ctrl = ref->ctrl;
2446 
2447 		/* Skip handler-private controls. */
2448 		if (ctrl->is_private)
2449 			continue;
2450 		/* And control classes */
2451 		if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2452 			continue;
2453 		/* Filter any unwanted controls */
2454 		if (filter && !filter(ctrl))
2455 			continue;
2456 		ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
2457 		if (ret)
2458 			break;
2459 	}
2460 	mutex_unlock(add->lock);
2461 	return ret;
2462 }
2463 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
2464 
2465 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
2466 {
2467 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
2468 		return true;
2469 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
2470 		return true;
2471 	switch (ctrl->id) {
2472 	case V4L2_CID_AUDIO_MUTE:
2473 	case V4L2_CID_AUDIO_VOLUME:
2474 	case V4L2_CID_AUDIO_BALANCE:
2475 	case V4L2_CID_AUDIO_BASS:
2476 	case V4L2_CID_AUDIO_TREBLE:
2477 	case V4L2_CID_AUDIO_LOUDNESS:
2478 		return true;
2479 	default:
2480 		break;
2481 	}
2482 	return false;
2483 }
2484 EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
2485 
2486 /* Cluster controls */
2487 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
2488 {
2489 	bool has_volatiles = false;
2490 	int i;
2491 
2492 	/* The first control is the master control and it must not be NULL */
2493 	if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
2494 		return;
2495 
2496 	for (i = 0; i < ncontrols; i++) {
2497 		if (controls[i]) {
2498 			controls[i]->cluster = controls;
2499 			controls[i]->ncontrols = ncontrols;
2500 			if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
2501 				has_volatiles = true;
2502 		}
2503 	}
2504 	controls[0]->has_volatiles = has_volatiles;
2505 }
2506 EXPORT_SYMBOL(v4l2_ctrl_cluster);
2507 
2508 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
2509 			    u8 manual_val, bool set_volatile)
2510 {
2511 	struct v4l2_ctrl *master = controls[0];
2512 	u32 flag = 0;
2513 	int i;
2514 
2515 	v4l2_ctrl_cluster(ncontrols, controls);
2516 	WARN_ON(ncontrols <= 1);
2517 	WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
2518 	WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
2519 	master->is_auto = true;
2520 	master->has_volatiles = set_volatile;
2521 	master->manual_mode_value = manual_val;
2522 	master->flags |= V4L2_CTRL_FLAG_UPDATE;
2523 
2524 	if (!is_cur_manual(master))
2525 		flag = V4L2_CTRL_FLAG_INACTIVE |
2526 			(set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
2527 
2528 	for (i = 1; i < ncontrols; i++)
2529 		if (controls[i])
2530 			controls[i]->flags |= flag;
2531 }
2532 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
2533 
2534 /*
2535  * Obtain the current volatile values of an autocluster and mark them
2536  * as new.
2537  */
2538 void update_from_auto_cluster(struct v4l2_ctrl *master)
2539 {
2540 	int i;
2541 
2542 	for (i = 1; i < master->ncontrols; i++)
2543 		cur_to_new(master->cluster[i]);
2544 	if (!call_op(master, g_volatile_ctrl))
2545 		for (i = 1; i < master->ncontrols; i++)
2546 			if (master->cluster[i])
2547 				master->cluster[i]->is_new = 1;
2548 }
2549 
2550 /*
2551  * Return non-zero if one or more of the controls in the cluster has a new
2552  * value that differs from the current value.
2553  */
2554 static int cluster_changed(struct v4l2_ctrl *master)
2555 {
2556 	bool changed = false;
2557 	int i;
2558 
2559 	for (i = 0; i < master->ncontrols; i++) {
2560 		struct v4l2_ctrl *ctrl = master->cluster[i];
2561 		bool ctrl_changed = false;
2562 
2563 		if (!ctrl)
2564 			continue;
2565 
2566 		if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE) {
2567 			changed = true;
2568 			ctrl_changed = true;
2569 		}
2570 
2571 		/*
2572 		 * Set has_changed to false to avoid generating
2573 		 * the event V4L2_EVENT_CTRL_CH_VALUE
2574 		 */
2575 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
2576 			ctrl->has_changed = false;
2577 			continue;
2578 		}
2579 
2580 		if (ctrl->elems != ctrl->new_elems)
2581 			ctrl_changed = true;
2582 		if (!ctrl_changed)
2583 			ctrl_changed = !ctrl->type_ops->equal(ctrl,
2584 				ctrl->p_cur, ctrl->p_new);
2585 		ctrl->has_changed = ctrl_changed;
2586 		changed |= ctrl->has_changed;
2587 	}
2588 	return changed;
2589 }
2590 
2591 /*
2592  * Core function that calls try/s_ctrl and ensures that the new value is
2593  * copied to the current value on a set.
2594  * Must be called with ctrl->handler->lock held.
2595  */
2596 int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
2597 		       bool set, u32 ch_flags)
2598 {
2599 	bool update_flag;
2600 	int ret;
2601 	int i;
2602 
2603 	/*
2604 	 * Go through the cluster and either validate the new value or
2605 	 * (if no new value was set), copy the current value to the new
2606 	 * value, ensuring a consistent view for the control ops when
2607 	 * called.
2608 	 */
2609 	for (i = 0; i < master->ncontrols; i++) {
2610 		struct v4l2_ctrl *ctrl = master->cluster[i];
2611 
2612 		if (!ctrl)
2613 			continue;
2614 
2615 		if (!ctrl->is_new) {
2616 			cur_to_new(ctrl);
2617 			continue;
2618 		}
2619 		/*
2620 		 * Check again: it may have changed since the
2621 		 * previous check in try_or_set_ext_ctrls().
2622 		 */
2623 		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2624 			return -EBUSY;
2625 	}
2626 
2627 	ret = call_op(master, try_ctrl);
2628 
2629 	/* Don't set if there is no change */
2630 	if (ret || !set || !cluster_changed(master))
2631 		return ret;
2632 	ret = call_op(master, s_ctrl);
2633 	if (ret)
2634 		return ret;
2635 
2636 	/* If OK, then make the new values permanent. */
2637 	update_flag = is_cur_manual(master) != is_new_manual(master);
2638 
2639 	for (i = 0; i < master->ncontrols; i++) {
2640 		/*
2641 		 * If we switch from auto to manual mode, and this cluster
2642 		 * contains volatile controls, then all non-master controls
2643 		 * have to be marked as changed. The 'new' value contains
2644 		 * the volatile value (obtained by update_from_auto_cluster),
2645 		 * which now has to become the current value.
2646 		 */
2647 		if (i && update_flag && is_new_manual(master) &&
2648 		    master->has_volatiles && master->cluster[i])
2649 			master->cluster[i]->has_changed = true;
2650 
2651 		new_to_cur(fh, master->cluster[i], ch_flags |
2652 			((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
2653 	}
2654 	return 0;
2655 }
2656 
2657 /* Activate/deactivate a control. */
2658 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
2659 {
2660 	/* invert since the actual flag is called 'inactive' */
2661 	bool inactive = !active;
2662 	bool old;
2663 
2664 	if (ctrl == NULL)
2665 		return;
2666 
2667 	if (inactive)
2668 		/* set V4L2_CTRL_FLAG_INACTIVE */
2669 		old = test_and_set_bit(4, &ctrl->flags);
2670 	else
2671 		/* clear V4L2_CTRL_FLAG_INACTIVE */
2672 		old = test_and_clear_bit(4, &ctrl->flags);
2673 	if (old != inactive)
2674 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2675 }
2676 EXPORT_SYMBOL(v4l2_ctrl_activate);
2677 
2678 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
2679 {
2680 	bool old;
2681 
2682 	if (ctrl == NULL)
2683 		return;
2684 
2685 	lockdep_assert_held(ctrl->handler->lock);
2686 
2687 	if (grabbed)
2688 		/* set V4L2_CTRL_FLAG_GRABBED */
2689 		old = test_and_set_bit(1, &ctrl->flags);
2690 	else
2691 		/* clear V4L2_CTRL_FLAG_GRABBED */
2692 		old = test_and_clear_bit(1, &ctrl->flags);
2693 	if (old != grabbed)
2694 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2695 }
2696 EXPORT_SYMBOL(__v4l2_ctrl_grab);
2697 
2698 /* Call s_ctrl for all controls owned by the handler */
2699 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2700 {
2701 	struct v4l2_ctrl *ctrl;
2702 	int ret = 0;
2703 
2704 	if (hdl == NULL)
2705 		return 0;
2706 
2707 	lockdep_assert_held(hdl->lock);
2708 
2709 	list_for_each_entry(ctrl, &hdl->ctrls, node)
2710 		ctrl->done = false;
2711 
2712 	list_for_each_entry(ctrl, &hdl->ctrls, node) {
2713 		struct v4l2_ctrl *master = ctrl->cluster[0];
2714 		int i;
2715 
2716 		/* Skip if this control was already handled by a cluster. */
2717 		/* Skip button controls and read-only controls. */
2718 		if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
2719 		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
2720 			continue;
2721 
2722 		for (i = 0; i < master->ncontrols; i++) {
2723 			if (master->cluster[i]) {
2724 				cur_to_new(master->cluster[i]);
2725 				master->cluster[i]->is_new = 1;
2726 				master->cluster[i]->done = true;
2727 			}
2728 		}
2729 		ret = call_op(master, s_ctrl);
2730 		if (ret)
2731 			break;
2732 	}
2733 
2734 	return ret;
2735 }
2736 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
2737 
2738 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2739 {
2740 	int ret;
2741 
2742 	if (hdl == NULL)
2743 		return 0;
2744 
2745 	mutex_lock(hdl->lock);
2746 	ret = __v4l2_ctrl_handler_setup(hdl);
2747 	mutex_unlock(hdl->lock);
2748 
2749 	return ret;
2750 }
2751 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
2752 
2753 /* Log the control name and value */
2754 static void log_ctrl(const struct v4l2_ctrl *ctrl,
2755 		     const char *prefix, const char *colon)
2756 {
2757 	if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
2758 		return;
2759 	if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2760 		return;
2761 
2762 	pr_info("%s%s%s: ", prefix, colon, ctrl->name);
2763 
2764 	ctrl->type_ops->log(ctrl);
2765 
2766 	if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
2767 			   V4L2_CTRL_FLAG_GRABBED |
2768 			   V4L2_CTRL_FLAG_VOLATILE)) {
2769 		if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
2770 			pr_cont(" inactive");
2771 		if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
2772 			pr_cont(" grabbed");
2773 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
2774 			pr_cont(" volatile");
2775 	}
2776 	pr_cont("\n");
2777 }
2778 
2779 /* Log all controls owned by the handler */
2780 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
2781 				  const char *prefix)
2782 {
2783 	struct v4l2_ctrl *ctrl;
2784 	const char *colon = "";
2785 	int len;
2786 
2787 	if (!hdl)
2788 		return;
2789 	if (!prefix)
2790 		prefix = "";
2791 	len = strlen(prefix);
2792 	if (len && prefix[len - 1] != ' ')
2793 		colon = ": ";
2794 	mutex_lock(hdl->lock);
2795 	list_for_each_entry(ctrl, &hdl->ctrls, node)
2796 		if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
2797 			log_ctrl(ctrl, prefix, colon);
2798 	mutex_unlock(hdl->lock);
2799 }
2800 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
2801 
2802 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,
2803 				    const struct v4l2_ctrl_ops *ctrl_ops,
2804 				    const struct v4l2_fwnode_device_properties *p)
2805 {
2806 	if (hdl->error)
2807 		return hdl->error;
2808 
2809 	if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) {
2810 		u32 orientation_ctrl;
2811 
2812 		switch (p->orientation) {
2813 		case V4L2_FWNODE_ORIENTATION_FRONT:
2814 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT;
2815 			break;
2816 		case V4L2_FWNODE_ORIENTATION_BACK:
2817 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK;
2818 			break;
2819 		case V4L2_FWNODE_ORIENTATION_EXTERNAL:
2820 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL;
2821 			break;
2822 		default:
2823 			hdl->error = -EINVAL;
2824 			return hdl->error;
2825 		}
2826 		if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops,
2827 					    V4L2_CID_CAMERA_ORIENTATION,
2828 					    V4L2_CAMERA_ORIENTATION_EXTERNAL, 0,
2829 					    orientation_ctrl))
2830 			return hdl->error;
2831 	}
2832 
2833 	if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) {
2834 		if (!v4l2_ctrl_new_std(hdl, ctrl_ops,
2835 				       V4L2_CID_CAMERA_SENSOR_ROTATION,
2836 				       p->rotation, p->rotation, 1,
2837 				       p->rotation))
2838 			return hdl->error;
2839 	}
2840 
2841 	return hdl->error;
2842 }
2843 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties);
2844