xref: /linux/drivers/media/v4l2-core/v4l2-ctrls-core.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
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_hdr10_mastering_display *p_hdr10_mastering;
975 	struct v4l2_ctrl_hevc_decode_params *p_hevc_decode_params;
976 	struct v4l2_area *area;
977 	struct v4l2_rect *rect;
978 	void *p = ptr.p + idx * ctrl->elem_size;
979 	unsigned int i;
980 
981 	switch ((u32)ctrl->type) {
982 	case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
983 		p_mpeg2_sequence = p;
984 
985 		switch (p_mpeg2_sequence->chroma_format) {
986 		case 1: /* 4:2:0 */
987 		case 2: /* 4:2:2 */
988 		case 3: /* 4:4:4 */
989 			break;
990 		default:
991 			return -EINVAL;
992 		}
993 		break;
994 
995 	case V4L2_CTRL_TYPE_MPEG2_PICTURE:
996 		p_mpeg2_picture = p;
997 
998 		switch (p_mpeg2_picture->intra_dc_precision) {
999 		case 0: /* 8 bits */
1000 		case 1: /* 9 bits */
1001 		case 2: /* 10 bits */
1002 		case 3: /* 11 bits */
1003 			break;
1004 		default:
1005 			return -EINVAL;
1006 		}
1007 
1008 		switch (p_mpeg2_picture->picture_structure) {
1009 		case V4L2_MPEG2_PIC_TOP_FIELD:
1010 		case V4L2_MPEG2_PIC_BOTTOM_FIELD:
1011 		case V4L2_MPEG2_PIC_FRAME:
1012 			break;
1013 		default:
1014 			return -EINVAL;
1015 		}
1016 
1017 		switch (p_mpeg2_picture->picture_coding_type) {
1018 		case V4L2_MPEG2_PIC_CODING_TYPE_I:
1019 		case V4L2_MPEG2_PIC_CODING_TYPE_P:
1020 		case V4L2_MPEG2_PIC_CODING_TYPE_B:
1021 			break;
1022 		default:
1023 			return -EINVAL;
1024 		}
1025 		zero_reserved(*p_mpeg2_picture);
1026 		break;
1027 
1028 	case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
1029 		break;
1030 
1031 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
1032 		p_fwht_params = p;
1033 		if (p_fwht_params->version < V4L2_FWHT_VERSION)
1034 			return -EINVAL;
1035 		if (!p_fwht_params->width || !p_fwht_params->height)
1036 			return -EINVAL;
1037 		break;
1038 
1039 	case V4L2_CTRL_TYPE_H264_SPS:
1040 		p_h264_sps = p;
1041 
1042 		/* Some syntax elements are only conditionally valid */
1043 		if (p_h264_sps->pic_order_cnt_type != 0) {
1044 			p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 = 0;
1045 		} else if (p_h264_sps->pic_order_cnt_type != 1) {
1046 			p_h264_sps->num_ref_frames_in_pic_order_cnt_cycle = 0;
1047 			p_h264_sps->offset_for_non_ref_pic = 0;
1048 			p_h264_sps->offset_for_top_to_bottom_field = 0;
1049 			memset(&p_h264_sps->offset_for_ref_frame, 0,
1050 			       sizeof(p_h264_sps->offset_for_ref_frame));
1051 		}
1052 
1053 		if (!V4L2_H264_SPS_HAS_CHROMA_FORMAT(p_h264_sps)) {
1054 			p_h264_sps->chroma_format_idc = 1;
1055 			p_h264_sps->bit_depth_luma_minus8 = 0;
1056 			p_h264_sps->bit_depth_chroma_minus8 = 0;
1057 
1058 			p_h264_sps->flags &=
1059 				~V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS;
1060 		}
1061 
1062 		if (p_h264_sps->chroma_format_idc < 3)
1063 			p_h264_sps->flags &=
1064 				~V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE;
1065 
1066 		if (p_h264_sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)
1067 			p_h264_sps->flags &=
1068 				~V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD;
1069 
1070 		/*
1071 		 * Chroma 4:2:2 format require at least High 4:2:2 profile.
1072 		 *
1073 		 * The H264 specification and well-known parser implementations
1074 		 * use profile-idc values directly, as that is clearer and
1075 		 * less ambiguous. We do the same here.
1076 		 */
1077 		if (p_h264_sps->profile_idc < 122 &&
1078 		    p_h264_sps->chroma_format_idc > 1)
1079 			return -EINVAL;
1080 		/* Chroma 4:4:4 format require at least High 4:2:2 profile */
1081 		if (p_h264_sps->profile_idc < 244 &&
1082 		    p_h264_sps->chroma_format_idc > 2)
1083 			return -EINVAL;
1084 		if (p_h264_sps->chroma_format_idc > 3)
1085 			return -EINVAL;
1086 
1087 		if (p_h264_sps->bit_depth_luma_minus8 > 6)
1088 			return -EINVAL;
1089 		if (p_h264_sps->bit_depth_chroma_minus8 > 6)
1090 			return -EINVAL;
1091 		if (p_h264_sps->log2_max_frame_num_minus4 > 12)
1092 			return -EINVAL;
1093 		if (p_h264_sps->pic_order_cnt_type > 2)
1094 			return -EINVAL;
1095 		if (p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 > 12)
1096 			return -EINVAL;
1097 		if (p_h264_sps->max_num_ref_frames > V4L2_H264_REF_LIST_LEN)
1098 			return -EINVAL;
1099 		break;
1100 
1101 	case V4L2_CTRL_TYPE_H264_PPS:
1102 		p_h264_pps = p;
1103 
1104 		if (p_h264_pps->num_slice_groups_minus1 > 7)
1105 			return -EINVAL;
1106 		if (p_h264_pps->num_ref_idx_l0_default_active_minus1 >
1107 		    (V4L2_H264_REF_LIST_LEN - 1))
1108 			return -EINVAL;
1109 		if (p_h264_pps->num_ref_idx_l1_default_active_minus1 >
1110 		    (V4L2_H264_REF_LIST_LEN - 1))
1111 			return -EINVAL;
1112 		if (p_h264_pps->weighted_bipred_idc > 2)
1113 			return -EINVAL;
1114 		/*
1115 		 * pic_init_qp_minus26 shall be in the range of
1116 		 * -(26 + QpBdOffset_y) to +25, inclusive,
1117 		 *  where QpBdOffset_y is 6 * bit_depth_luma_minus8
1118 		 */
1119 		if (p_h264_pps->pic_init_qp_minus26 < -62 ||
1120 		    p_h264_pps->pic_init_qp_minus26 > 25)
1121 			return -EINVAL;
1122 		if (p_h264_pps->pic_init_qs_minus26 < -26 ||
1123 		    p_h264_pps->pic_init_qs_minus26 > 25)
1124 			return -EINVAL;
1125 		if (p_h264_pps->chroma_qp_index_offset < -12 ||
1126 		    p_h264_pps->chroma_qp_index_offset > 12)
1127 			return -EINVAL;
1128 		if (p_h264_pps->second_chroma_qp_index_offset < -12 ||
1129 		    p_h264_pps->second_chroma_qp_index_offset > 12)
1130 			return -EINVAL;
1131 		break;
1132 
1133 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
1134 		break;
1135 
1136 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
1137 		p_h264_pred_weights = p;
1138 
1139 		if (p_h264_pred_weights->luma_log2_weight_denom > 7)
1140 			return -EINVAL;
1141 		if (p_h264_pred_weights->chroma_log2_weight_denom > 7)
1142 			return -EINVAL;
1143 		break;
1144 
1145 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
1146 		p_h264_slice_params = p;
1147 
1148 		if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
1149 			p_h264_slice_params->flags &=
1150 				~V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED;
1151 
1152 		if (p_h264_slice_params->colour_plane_id > 2)
1153 			return -EINVAL;
1154 		if (p_h264_slice_params->cabac_init_idc > 2)
1155 			return -EINVAL;
1156 		if (p_h264_slice_params->disable_deblocking_filter_idc > 2)
1157 			return -EINVAL;
1158 		if (p_h264_slice_params->slice_alpha_c0_offset_div2 < -6 ||
1159 		    p_h264_slice_params->slice_alpha_c0_offset_div2 > 6)
1160 			return -EINVAL;
1161 		if (p_h264_slice_params->slice_beta_offset_div2 < -6 ||
1162 		    p_h264_slice_params->slice_beta_offset_div2 > 6)
1163 			return -EINVAL;
1164 
1165 		if (p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_I ||
1166 		    p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_SI)
1167 			p_h264_slice_params->num_ref_idx_l0_active_minus1 = 0;
1168 		if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
1169 			p_h264_slice_params->num_ref_idx_l1_active_minus1 = 0;
1170 
1171 		if (p_h264_slice_params->num_ref_idx_l0_active_minus1 >
1172 		    (V4L2_H264_REF_LIST_LEN - 1))
1173 			return -EINVAL;
1174 		if (p_h264_slice_params->num_ref_idx_l1_active_minus1 >
1175 		    (V4L2_H264_REF_LIST_LEN - 1))
1176 			return -EINVAL;
1177 		zero_reserved(*p_h264_slice_params);
1178 		break;
1179 
1180 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
1181 		p_h264_dec_params = p;
1182 
1183 		if (p_h264_dec_params->nal_ref_idc > 3)
1184 			return -EINVAL;
1185 		for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
1186 			struct v4l2_h264_dpb_entry *dpb_entry =
1187 				&p_h264_dec_params->dpb[i];
1188 
1189 			zero_reserved(*dpb_entry);
1190 		}
1191 		zero_reserved(*p_h264_dec_params);
1192 		break;
1193 
1194 	case V4L2_CTRL_TYPE_VP8_FRAME:
1195 		p_vp8_frame = p;
1196 
1197 		switch (p_vp8_frame->num_dct_parts) {
1198 		case 1:
1199 		case 2:
1200 		case 4:
1201 		case 8:
1202 			break;
1203 		default:
1204 			return -EINVAL;
1205 		}
1206 		zero_padding(p_vp8_frame->segment);
1207 		zero_padding(p_vp8_frame->lf);
1208 		zero_padding(p_vp8_frame->quant);
1209 		zero_padding(p_vp8_frame->entropy);
1210 		zero_padding(p_vp8_frame->coder_state);
1211 		break;
1212 
1213 	case V4L2_CTRL_TYPE_HEVC_SPS:
1214 		p_hevc_sps = p;
1215 
1216 		if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) {
1217 			p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0;
1218 			p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;
1219 			p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0;
1220 			p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0;
1221 		}
1222 
1223 		if (!(p_hevc_sps->flags &
1224 		      V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT))
1225 			p_hevc_sps->num_long_term_ref_pics_sps = 0;
1226 		break;
1227 
1228 	case V4L2_CTRL_TYPE_HEVC_PPS:
1229 		p_hevc_pps = p;
1230 
1231 		if (!(p_hevc_pps->flags &
1232 		      V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED))
1233 			p_hevc_pps->diff_cu_qp_delta_depth = 0;
1234 
1235 		if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
1236 			p_hevc_pps->num_tile_columns_minus1 = 0;
1237 			p_hevc_pps->num_tile_rows_minus1 = 0;
1238 			memset(&p_hevc_pps->column_width_minus1, 0,
1239 			       sizeof(p_hevc_pps->column_width_minus1));
1240 			memset(&p_hevc_pps->row_height_minus1, 0,
1241 			       sizeof(p_hevc_pps->row_height_minus1));
1242 
1243 			p_hevc_pps->flags &=
1244 				~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED;
1245 		}
1246 
1247 		if (p_hevc_pps->flags &
1248 		    V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) {
1249 			p_hevc_pps->pps_beta_offset_div2 = 0;
1250 			p_hevc_pps->pps_tc_offset_div2 = 0;
1251 		}
1252 		break;
1253 
1254 	case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
1255 		p_hevc_decode_params = p;
1256 
1257 		if (p_hevc_decode_params->num_active_dpb_entries >
1258 		    V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
1259 			return -EINVAL;
1260 		break;
1261 
1262 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
1263 		break;
1264 
1265 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_ST_RPS:
1266 		p_hevc_st_rps = p;
1267 
1268 		if (p_hevc_st_rps->flags & ~V4L2_HEVC_EXT_SPS_ST_RPS_FLAG_INTER_REF_PIC_SET_PRED)
1269 			return -EINVAL;
1270 		break;
1271 
1272 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS:
1273 		p_hevc_lt_rps = p;
1274 
1275 		if (p_hevc_lt_rps->flags & ~V4L2_HEVC_EXT_SPS_LT_RPS_FLAG_USED_LT)
1276 			return -EINVAL;
1277 		break;
1278 
1279 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
1280 		break;
1281 
1282 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
1283 		p_hdr10_mastering = p;
1284 
1285 		for (i = 0; i < 3; ++i) {
1286 			if (p_hdr10_mastering->display_primaries_x[i] <
1287 				V4L2_HDR10_MASTERING_PRIMARIES_X_LOW ||
1288 			    p_hdr10_mastering->display_primaries_x[i] >
1289 				V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH ||
1290 			    p_hdr10_mastering->display_primaries_y[i] <
1291 				V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW ||
1292 			    p_hdr10_mastering->display_primaries_y[i] >
1293 				V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH)
1294 				return -EINVAL;
1295 		}
1296 
1297 		if (p_hdr10_mastering->white_point_x <
1298 			V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW ||
1299 		    p_hdr10_mastering->white_point_x >
1300 			V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH ||
1301 		    p_hdr10_mastering->white_point_y <
1302 			V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW ||
1303 		    p_hdr10_mastering->white_point_y >
1304 			V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH)
1305 			return -EINVAL;
1306 
1307 		if (p_hdr10_mastering->max_display_mastering_luminance <
1308 			V4L2_HDR10_MASTERING_MAX_LUMA_LOW ||
1309 		    p_hdr10_mastering->max_display_mastering_luminance >
1310 			V4L2_HDR10_MASTERING_MAX_LUMA_HIGH ||
1311 		    p_hdr10_mastering->min_display_mastering_luminance <
1312 			V4L2_HDR10_MASTERING_MIN_LUMA_LOW ||
1313 		    p_hdr10_mastering->min_display_mastering_luminance >
1314 			V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
1315 			return -EINVAL;
1316 
1317 		/* The following restriction comes from ITU-T Rec. H.265 spec */
1318 		if (p_hdr10_mastering->max_display_mastering_luminance ==
1319 			V4L2_HDR10_MASTERING_MAX_LUMA_LOW &&
1320 		    p_hdr10_mastering->min_display_mastering_luminance ==
1321 			V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
1322 			return -EINVAL;
1323 
1324 		break;
1325 
1326 	case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
1327 		break;
1328 
1329 	case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
1330 		return validate_vp9_compressed_hdr(p);
1331 
1332 	case V4L2_CTRL_TYPE_VP9_FRAME:
1333 		return validate_vp9_frame(p);
1334 	case V4L2_CTRL_TYPE_AV1_FRAME:
1335 		return validate_av1_frame(p);
1336 	case V4L2_CTRL_TYPE_AV1_SEQUENCE:
1337 		return validate_av1_sequence(p);
1338 	case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
1339 		break;
1340 	case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
1341 		return validate_av1_film_grain(p);
1342 
1343 	case V4L2_CTRL_TYPE_AREA:
1344 		area = p;
1345 		if (!area->width || !area->height)
1346 			return -EINVAL;
1347 		break;
1348 
1349 	case V4L2_CTRL_TYPE_RECT:
1350 		rect = p;
1351 		if (!rect->width || !rect->height)
1352 			return -EINVAL;
1353 		break;
1354 
1355 	default:
1356 		return -EINVAL;
1357 	}
1358 
1359 	return 0;
1360 }
1361 
1362 static int std_validate_elem(const struct v4l2_ctrl *ctrl, u32 idx,
1363 			     union v4l2_ctrl_ptr ptr)
1364 {
1365 	size_t len;
1366 	u64 offset;
1367 	s64 val;
1368 
1369 	switch ((u32)ctrl->type) {
1370 	case V4L2_CTRL_TYPE_INTEGER:
1371 		return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
1372 	case V4L2_CTRL_TYPE_INTEGER64:
1373 		/*
1374 		 * We can't use the ROUND_TO_RANGE define here due to
1375 		 * the u64 divide that needs special care.
1376 		 */
1377 		val = ptr.p_s64[idx];
1378 		if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
1379 			val = ctrl->maximum;
1380 		else
1381 			val += (s64)(ctrl->step / 2);
1382 		val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
1383 		offset = val - ctrl->minimum;
1384 		do_div(offset, ctrl->step);
1385 		ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
1386 		return 0;
1387 	case V4L2_CTRL_TYPE_U8:
1388 		return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
1389 	case V4L2_CTRL_TYPE_U16:
1390 		return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
1391 	case V4L2_CTRL_TYPE_U32:
1392 		return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
1393 
1394 	case V4L2_CTRL_TYPE_BOOLEAN:
1395 		ptr.p_s32[idx] = !!ptr.p_s32[idx];
1396 		return 0;
1397 
1398 	case V4L2_CTRL_TYPE_MENU:
1399 	case V4L2_CTRL_TYPE_INTEGER_MENU:
1400 		if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
1401 			return -ERANGE;
1402 		if (ptr.p_s32[idx] < BITS_PER_LONG_LONG &&
1403 		    (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx])))
1404 			return -EINVAL;
1405 		if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1406 		    ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
1407 			return -EINVAL;
1408 		return 0;
1409 
1410 	case V4L2_CTRL_TYPE_BITMASK:
1411 		ptr.p_s32[idx] &= ctrl->maximum;
1412 		return 0;
1413 
1414 	case V4L2_CTRL_TYPE_BUTTON:
1415 	case V4L2_CTRL_TYPE_CTRL_CLASS:
1416 		ptr.p_s32[idx] = 0;
1417 		return 0;
1418 
1419 	case V4L2_CTRL_TYPE_STRING:
1420 		idx *= ctrl->elem_size;
1421 		len = strlen(ptr.p_char + idx);
1422 		if (len < ctrl->minimum)
1423 			return -ERANGE;
1424 		if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
1425 			return -ERANGE;
1426 		return 0;
1427 
1428 	default:
1429 		return std_validate_compound(ctrl, idx, ptr);
1430 	}
1431 }
1432 
1433 int v4l2_ctrl_type_op_validate(const struct v4l2_ctrl *ctrl,
1434 			       union v4l2_ctrl_ptr ptr)
1435 {
1436 	unsigned int i;
1437 	int ret = 0;
1438 
1439 	switch ((u32)ctrl->type) {
1440 	case V4L2_CTRL_TYPE_U8:
1441 		if (ctrl->maximum == 0xff && ctrl->minimum == 0 && ctrl->step == 1)
1442 			return 0;
1443 		break;
1444 	case V4L2_CTRL_TYPE_U16:
1445 		if (ctrl->maximum == 0xffff && ctrl->minimum == 0 && ctrl->step == 1)
1446 			return 0;
1447 		break;
1448 	case V4L2_CTRL_TYPE_U32:
1449 		if (ctrl->maximum == 0xffffffff && ctrl->minimum == 0 && ctrl->step == 1)
1450 			return 0;
1451 		break;
1452 
1453 	case V4L2_CTRL_TYPE_BUTTON:
1454 	case V4L2_CTRL_TYPE_CTRL_CLASS:
1455 		memset(ptr.p_s32, 0, ctrl->new_elems * sizeof(s32));
1456 		return 0;
1457 	}
1458 
1459 	for (i = 0; !ret && i < ctrl->new_elems; i++)
1460 		ret = std_validate_elem(ctrl, i, ptr);
1461 	return ret;
1462 }
1463 EXPORT_SYMBOL(v4l2_ctrl_type_op_validate);
1464 
1465 static const struct v4l2_ctrl_type_ops std_type_ops = {
1466 	.equal = v4l2_ctrl_type_op_equal,
1467 	.init = v4l2_ctrl_type_op_init,
1468 	.minimum = v4l2_ctrl_type_op_minimum,
1469 	.maximum = v4l2_ctrl_type_op_maximum,
1470 	.log = v4l2_ctrl_type_op_log,
1471 	.validate = v4l2_ctrl_type_op_validate,
1472 };
1473 
1474 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
1475 {
1476 	if (!ctrl)
1477 		return;
1478 	if (!notify) {
1479 		ctrl->call_notify = 0;
1480 		return;
1481 	}
1482 	if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
1483 		return;
1484 	ctrl->handler->notify = notify;
1485 	ctrl->handler->notify_priv = priv;
1486 	ctrl->call_notify = 1;
1487 }
1488 EXPORT_SYMBOL(v4l2_ctrl_notify);
1489 
1490 /* Copy the one value to another. */
1491 static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
1492 		       union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to,
1493 		       unsigned int elems)
1494 {
1495 	if (ctrl == NULL)
1496 		return;
1497 	memcpy(to.p, from.p_const, elems * ctrl->elem_size);
1498 }
1499 
1500 /* Copy the new value to the current value. */
1501 void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
1502 {
1503 	bool changed;
1504 
1505 	if (ctrl == NULL)
1506 		return;
1507 
1508 	/* has_changed is set by cluster_changed */
1509 	changed = ctrl->has_changed;
1510 	if (changed) {
1511 		if (ctrl->is_dyn_array)
1512 			ctrl->elems = ctrl->new_elems;
1513 		ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur, ctrl->elems);
1514 	}
1515 
1516 	if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
1517 		/* Note: CH_FLAGS is only set for auto clusters. */
1518 		ctrl->flags &=
1519 			~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
1520 		if (!is_cur_manual(ctrl->cluster[0])) {
1521 			ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1522 			if (ctrl->cluster[0]->has_volatiles)
1523 				ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1524 		}
1525 		fh = NULL;
1526 	}
1527 	if (changed || ch_flags) {
1528 		/* If a control was changed that was not one of the controls
1529 		   modified by the application, then send the event to all. */
1530 		if (!ctrl->is_new)
1531 			fh = NULL;
1532 		send_event(fh, ctrl,
1533 			(changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
1534 		if (ctrl->call_notify && changed && ctrl->handler->notify)
1535 			ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
1536 	}
1537 }
1538 
1539 /* Copy the current value to the new value */
1540 void cur_to_new(struct v4l2_ctrl *ctrl)
1541 {
1542 	if (ctrl == NULL)
1543 		return;
1544 	if (ctrl->is_dyn_array)
1545 		ctrl->new_elems = ctrl->elems;
1546 	ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems);
1547 }
1548 
1549 static bool req_alloc_array(struct v4l2_ctrl_ref *ref, u32 elems)
1550 {
1551 	void *tmp;
1552 
1553 	if (elems == ref->p_req_array_alloc_elems)
1554 		return true;
1555 	if (ref->ctrl->is_dyn_array &&
1556 	    elems < ref->p_req_array_alloc_elems)
1557 		return true;
1558 
1559 	tmp = kvmalloc(elems * ref->ctrl->elem_size, GFP_KERNEL);
1560 
1561 	if (!tmp) {
1562 		ref->p_req_array_enomem = true;
1563 		return false;
1564 	}
1565 	ref->p_req_array_enomem = false;
1566 	kvfree(ref->p_req.p);
1567 	ref->p_req.p = tmp;
1568 	ref->p_req_array_alloc_elems = elems;
1569 	return true;
1570 }
1571 
1572 /* Copy the new value to the request value */
1573 void new_to_req(struct v4l2_ctrl_ref *ref)
1574 {
1575 	struct v4l2_ctrl *ctrl;
1576 
1577 	if (!ref)
1578 		return;
1579 
1580 	ctrl = ref->ctrl;
1581 	if (ctrl->is_array && !req_alloc_array(ref, ctrl->new_elems))
1582 		return;
1583 
1584 	ref->p_req_elems = ctrl->new_elems;
1585 	ptr_to_ptr(ctrl, ctrl->p_new, ref->p_req, ref->p_req_elems);
1586 	ref->p_req_valid = true;
1587 }
1588 
1589 /* Copy the current value to the request value */
1590 void cur_to_req(struct v4l2_ctrl_ref *ref)
1591 {
1592 	struct v4l2_ctrl *ctrl;
1593 
1594 	if (!ref)
1595 		return;
1596 
1597 	ctrl = ref->ctrl;
1598 	if (ctrl->is_array && !req_alloc_array(ref, ctrl->elems))
1599 		return;
1600 
1601 	ref->p_req_elems = ctrl->elems;
1602 	ptr_to_ptr(ctrl, ctrl->p_cur, ref->p_req, ctrl->elems);
1603 	ref->p_req_valid = true;
1604 }
1605 
1606 /* Copy the request value to the new value */
1607 int req_to_new(struct v4l2_ctrl_ref *ref)
1608 {
1609 	struct v4l2_ctrl *ctrl;
1610 
1611 	if (!ref)
1612 		return 0;
1613 
1614 	ctrl = ref->ctrl;
1615 
1616 	/*
1617 	 * This control was never set in the request, so just use the current
1618 	 * value.
1619 	 */
1620 	if (!ref->p_req_valid) {
1621 		if (ctrl->is_dyn_array)
1622 			ctrl->new_elems = ctrl->elems;
1623 		ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems);
1624 		return 0;
1625 	}
1626 
1627 	/* Not an array, so just copy the request value */
1628 	if (!ctrl->is_array) {
1629 		ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems);
1630 		return 0;
1631 	}
1632 
1633 	/* Sanity check, should never happen */
1634 	if (WARN_ON(!ref->p_req_array_alloc_elems))
1635 		return -ENOMEM;
1636 
1637 	if (!ctrl->is_dyn_array &&
1638 	    ref->p_req_elems != ctrl->p_array_alloc_elems)
1639 		return -ENOMEM;
1640 
1641 	/*
1642 	 * Check if the number of elements in the request is more than the
1643 	 * elements in ctrl->p_array. If so, attempt to realloc ctrl->p_array.
1644 	 * Note that p_array is allocated with twice the number of elements
1645 	 * in the dynamic array since it has to store both the current and
1646 	 * new value of such a control.
1647 	 */
1648 	if (ref->p_req_elems > ctrl->p_array_alloc_elems) {
1649 		unsigned int sz = ref->p_req_elems * ctrl->elem_size;
1650 		void *old = ctrl->p_array;
1651 		void *tmp = kvzalloc(2 * sz, GFP_KERNEL);
1652 
1653 		if (!tmp)
1654 			return -ENOMEM;
1655 		memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size);
1656 		memcpy(tmp + sz, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size);
1657 		ctrl->p_new.p = tmp;
1658 		ctrl->p_cur.p = tmp + sz;
1659 		ctrl->p_array = tmp;
1660 		ctrl->p_array_alloc_elems = ref->p_req_elems;
1661 		kvfree(old);
1662 	}
1663 
1664 	ctrl->new_elems = ref->p_req_elems;
1665 	ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems);
1666 	return 0;
1667 }
1668 
1669 /* Control range checking */
1670 int check_range(enum v4l2_ctrl_type type,
1671 		s64 min, s64 max, u64 step, s64 def)
1672 {
1673 	switch (type) {
1674 	case V4L2_CTRL_TYPE_BOOLEAN:
1675 		if (step != 1 || max > 1 || min < 0)
1676 			return -ERANGE;
1677 		fallthrough;
1678 	case V4L2_CTRL_TYPE_U8:
1679 	case V4L2_CTRL_TYPE_U16:
1680 	case V4L2_CTRL_TYPE_U32:
1681 	case V4L2_CTRL_TYPE_INTEGER:
1682 	case V4L2_CTRL_TYPE_INTEGER64:
1683 		if (step == 0 || min > max || def < min || def > max)
1684 			return -ERANGE;
1685 		return 0;
1686 	case V4L2_CTRL_TYPE_BITMASK:
1687 		if (step || min || !max || (def & ~max))
1688 			return -ERANGE;
1689 		return 0;
1690 	case V4L2_CTRL_TYPE_MENU:
1691 	case V4L2_CTRL_TYPE_INTEGER_MENU:
1692 		if (min > max || def < min || def > max ||
1693 		    min < 0 || (step && max >= BITS_PER_LONG_LONG))
1694 			return -ERANGE;
1695 		/* Note: step == menu_skip_mask for menu controls.
1696 		   So here we check if the default value is masked out. */
1697 		if (def < BITS_PER_LONG_LONG && (step & BIT_ULL(def)))
1698 			return -EINVAL;
1699 		return 0;
1700 	case V4L2_CTRL_TYPE_STRING:
1701 		if (min > max || min < 0 || step < 1 || def)
1702 			return -ERANGE;
1703 		return 0;
1704 	default:
1705 		return 0;
1706 	}
1707 }
1708 
1709 /* Set the handler's error code if it wasn't set earlier already */
1710 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1711 {
1712 	if (hdl->error == 0)
1713 		hdl->error = err;
1714 	return err;
1715 }
1716 
1717 /* Initialize the handler */
1718 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
1719 				 unsigned nr_of_controls_hint,
1720 				 struct lock_class_key *key, const char *name)
1721 {
1722 	mutex_init(&hdl->_lock);
1723 	hdl->lock = &hdl->_lock;
1724 	lockdep_set_class_and_name(hdl->lock, key, name);
1725 	INIT_LIST_HEAD(&hdl->ctrls);
1726 	INIT_LIST_HEAD(&hdl->ctrl_refs);
1727 	hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
1728 	hdl->buckets = kvzalloc_objs(hdl->buckets[0], hdl->nr_of_buckets);
1729 	hdl->error = hdl->buckets ? 0 : -ENOMEM;
1730 	v4l2_ctrl_handler_init_request(hdl);
1731 	return hdl->error;
1732 }
1733 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
1734 
1735 /* Free all controls and control refs */
1736 int v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1737 {
1738 	struct v4l2_ctrl_ref *ref, *next_ref;
1739 	struct v4l2_ctrl *ctrl, *next_ctrl;
1740 	struct v4l2_subscribed_event *sev, *next_sev;
1741 
1742 	if (!hdl)
1743 		return 0;
1744 
1745 	if (!hdl->buckets)
1746 		return hdl->error;
1747 
1748 	v4l2_ctrl_handler_free_request(hdl);
1749 
1750 	mutex_lock(hdl->lock);
1751 	/* Free all nodes */
1752 	list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1753 		list_del(&ref->node);
1754 		if (ref->p_req_array_alloc_elems)
1755 			kvfree(ref->p_req.p);
1756 		kfree(ref);
1757 	}
1758 	/* Free all controls owned by the handler */
1759 	list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1760 		list_del(&ctrl->node);
1761 		list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1762 			list_del(&sev->node);
1763 		kvfree(ctrl->p_array);
1764 		kvfree(ctrl);
1765 	}
1766 	kvfree(hdl->buckets);
1767 	hdl->buckets = NULL;
1768 	hdl->cached = NULL;
1769 	mutex_unlock(hdl->lock);
1770 	mutex_destroy(&hdl->_lock);
1771 
1772 	return hdl->error;
1773 }
1774 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1775 
1776 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1777    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1778    with applications that do not use the NEXT_CTRL flag.
1779 
1780    We just find the n-th private user control. It's O(N), but that should not
1781    be an issue in this particular case. */
1782 static struct v4l2_ctrl_ref *find_private_ref(
1783 		struct v4l2_ctrl_handler *hdl, u32 id)
1784 {
1785 	struct v4l2_ctrl_ref *ref;
1786 
1787 	id -= V4L2_CID_PRIVATE_BASE;
1788 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1789 		/* Search for private user controls that are compatible with
1790 		   VIDIOC_G/S_CTRL. */
1791 		if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1792 		    V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1793 			if (!ref->ctrl->is_int)
1794 				continue;
1795 			if (id == 0)
1796 				return ref;
1797 			id--;
1798 		}
1799 	}
1800 	return NULL;
1801 }
1802 
1803 /* Find a control with the given ID. */
1804 struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1805 {
1806 	struct v4l2_ctrl_ref *ref;
1807 	int bucket;
1808 
1809 	id &= V4L2_CTRL_ID_MASK;
1810 
1811 	/* Old-style private controls need special handling */
1812 	if (id >= V4L2_CID_PRIVATE_BASE)
1813 		return find_private_ref(hdl, id);
1814 	bucket = id % hdl->nr_of_buckets;
1815 
1816 	/* Simple optimization: cache the last control found */
1817 	if (hdl->cached && hdl->cached->ctrl->id == id)
1818 		return hdl->cached;
1819 
1820 	/* Not in cache, search the hash */
1821 	ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1822 	while (ref && ref->ctrl->id != id)
1823 		ref = ref->next;
1824 
1825 	if (ref)
1826 		hdl->cached = ref; /* cache it! */
1827 	return ref;
1828 }
1829 
1830 /* Find a control with the given ID. Take the handler's lock first. */
1831 struct v4l2_ctrl_ref *find_ref_lock(struct v4l2_ctrl_handler *hdl, u32 id)
1832 {
1833 	struct v4l2_ctrl_ref *ref = NULL;
1834 
1835 	if (hdl) {
1836 		mutex_lock(hdl->lock);
1837 		ref = find_ref(hdl, id);
1838 		mutex_unlock(hdl->lock);
1839 	}
1840 	return ref;
1841 }
1842 
1843 /* Find a control with the given ID. */
1844 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1845 {
1846 	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1847 
1848 	return ref ? ref->ctrl : NULL;
1849 }
1850 EXPORT_SYMBOL(v4l2_ctrl_find);
1851 
1852 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1853 int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1854 		    struct v4l2_ctrl *ctrl,
1855 		    struct v4l2_ctrl_ref **ctrl_ref,
1856 		    bool from_other_dev, bool allocate_req)
1857 {
1858 	struct v4l2_ctrl_ref *ref;
1859 	struct v4l2_ctrl_ref *new_ref;
1860 	u32 id = ctrl->id;
1861 	u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
1862 	int bucket = id % hdl->nr_of_buckets;	/* which bucket to use */
1863 	unsigned int size_extra_req = 0;
1864 
1865 	if (ctrl_ref)
1866 		*ctrl_ref = NULL;
1867 
1868 	/*
1869 	 * Automatically add the control class if it is not yet present and
1870 	 * the new control is not a compound control.
1871 	 */
1872 	if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
1873 	    id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1874 		if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1875 			return hdl->error;
1876 
1877 	if (hdl->error)
1878 		return hdl->error;
1879 
1880 	if (allocate_req && !ctrl->is_array)
1881 		size_extra_req = ctrl->elems * ctrl->elem_size;
1882 	new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
1883 	if (!new_ref)
1884 		return handler_set_err(hdl, -ENOMEM);
1885 	new_ref->ctrl = ctrl;
1886 	new_ref->from_other_dev = from_other_dev;
1887 	if (size_extra_req)
1888 		new_ref->p_req.p = &new_ref[1];
1889 
1890 	INIT_LIST_HEAD(&new_ref->node);
1891 
1892 	mutex_lock(hdl->lock);
1893 
1894 	/* Add immediately at the end of the list if the list is empty, or if
1895 	   the last element in the list has a lower ID.
1896 	   This ensures that when elements are added in ascending order the
1897 	   insertion is an O(1) operation. */
1898 	if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1899 		list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1900 		goto insert_in_hash;
1901 	}
1902 
1903 	/* Find insert position in sorted list */
1904 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1905 		if (ref->ctrl->id < id)
1906 			continue;
1907 		/* Don't add duplicates */
1908 		if (ref->ctrl->id == id) {
1909 			kfree(new_ref);
1910 			goto unlock;
1911 		}
1912 		list_add(&new_ref->node, ref->node.prev);
1913 		break;
1914 	}
1915 
1916 insert_in_hash:
1917 	/* Insert the control node in the hash */
1918 	new_ref->next = hdl->buckets[bucket];
1919 	hdl->buckets[bucket] = new_ref;
1920 	if (ctrl_ref)
1921 		*ctrl_ref = new_ref;
1922 	if (ctrl->handler == hdl) {
1923 		/* By default each control starts in a cluster of its own.
1924 		 * new_ref->ctrl is basically a cluster array with one
1925 		 * element, so that's perfect to use as the cluster pointer.
1926 		 * But only do this for the handler that owns the control.
1927 		 */
1928 		ctrl->cluster = &new_ref->ctrl;
1929 		ctrl->ncontrols = 1;
1930 	}
1931 
1932 unlock:
1933 	mutex_unlock(hdl->lock);
1934 	return 0;
1935 }
1936 
1937 /* Add a new control */
1938 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1939 			const struct v4l2_ctrl_ops *ops,
1940 			const struct v4l2_ctrl_type_ops *type_ops,
1941 			u32 id, const char *name, enum v4l2_ctrl_type type,
1942 			s64 min, s64 max, u64 step, s64 def,
1943 			const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
1944 			u32 flags, const char * const *qmenu,
1945 			const s64 *qmenu_int,
1946 			const union v4l2_ctrl_ptr p_def,
1947 			const union v4l2_ctrl_ptr p_min,
1948 			const union v4l2_ctrl_ptr p_max,
1949 			void *priv)
1950 {
1951 	struct v4l2_ctrl *ctrl;
1952 	unsigned sz_extra;
1953 	unsigned nr_of_dims = 0;
1954 	unsigned elems = 1;
1955 	bool is_array;
1956 	unsigned tot_ctrl_size;
1957 	void *data;
1958 	int err;
1959 
1960 	if (hdl->error)
1961 		return NULL;
1962 
1963 	while (dims && dims[nr_of_dims]) {
1964 		elems *= dims[nr_of_dims];
1965 		nr_of_dims++;
1966 		if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
1967 			break;
1968 	}
1969 	is_array = nr_of_dims > 0;
1970 
1971 	/* Prefill elem_size for all types handled by std_type_ops */
1972 	switch ((u32)type) {
1973 	case V4L2_CTRL_TYPE_INTEGER64:
1974 		elem_size = sizeof(s64);
1975 		break;
1976 	case V4L2_CTRL_TYPE_STRING:
1977 		elem_size = max + 1;
1978 		break;
1979 	case V4L2_CTRL_TYPE_U8:
1980 		elem_size = sizeof(u8);
1981 		break;
1982 	case V4L2_CTRL_TYPE_U16:
1983 		elem_size = sizeof(u16);
1984 		break;
1985 	case V4L2_CTRL_TYPE_U32:
1986 		elem_size = sizeof(u32);
1987 		break;
1988 	case V4L2_CTRL_TYPE_MPEG2_SEQUENCE:
1989 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_sequence);
1990 		break;
1991 	case V4L2_CTRL_TYPE_MPEG2_PICTURE:
1992 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_picture);
1993 		break;
1994 	case V4L2_CTRL_TYPE_MPEG2_QUANTISATION:
1995 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantisation);
1996 		break;
1997 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
1998 		elem_size = sizeof(struct v4l2_ctrl_fwht_params);
1999 		break;
2000 	case V4L2_CTRL_TYPE_H264_SPS:
2001 		elem_size = sizeof(struct v4l2_ctrl_h264_sps);
2002 		break;
2003 	case V4L2_CTRL_TYPE_H264_PPS:
2004 		elem_size = sizeof(struct v4l2_ctrl_h264_pps);
2005 		break;
2006 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
2007 		elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix);
2008 		break;
2009 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
2010 		elem_size = sizeof(struct v4l2_ctrl_h264_slice_params);
2011 		break;
2012 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
2013 		elem_size = sizeof(struct v4l2_ctrl_h264_decode_params);
2014 		break;
2015 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
2016 		elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights);
2017 		break;
2018 	case V4L2_CTRL_TYPE_VP8_FRAME:
2019 		elem_size = sizeof(struct v4l2_ctrl_vp8_frame);
2020 		break;
2021 	case V4L2_CTRL_TYPE_HEVC_SPS:
2022 		elem_size = sizeof(struct v4l2_ctrl_hevc_sps);
2023 		break;
2024 	case V4L2_CTRL_TYPE_HEVC_PPS:
2025 		elem_size = sizeof(struct v4l2_ctrl_hevc_pps);
2026 		break;
2027 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
2028 		elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params);
2029 		break;
2030 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_ST_RPS:
2031 		elem_size = sizeof(struct v4l2_ctrl_hevc_ext_sps_st_rps);
2032 		break;
2033 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS:
2034 		elem_size = sizeof(struct v4l2_ctrl_hevc_ext_sps_lt_rps);
2035 		break;
2036 	case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX:
2037 		elem_size = sizeof(struct v4l2_ctrl_hevc_scaling_matrix);
2038 		break;
2039 	case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
2040 		elem_size = sizeof(struct v4l2_ctrl_hevc_decode_params);
2041 		break;
2042 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
2043 		elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info);
2044 		break;
2045 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
2046 		elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display);
2047 		break;
2048 	case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR:
2049 		elem_size = sizeof(struct v4l2_ctrl_vp9_compressed_hdr);
2050 		break;
2051 	case V4L2_CTRL_TYPE_VP9_FRAME:
2052 		elem_size = sizeof(struct v4l2_ctrl_vp9_frame);
2053 		break;
2054 	case V4L2_CTRL_TYPE_AV1_SEQUENCE:
2055 		elem_size = sizeof(struct v4l2_ctrl_av1_sequence);
2056 		break;
2057 	case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY:
2058 		elem_size = sizeof(struct v4l2_ctrl_av1_tile_group_entry);
2059 		break;
2060 	case V4L2_CTRL_TYPE_AV1_FRAME:
2061 		elem_size = sizeof(struct v4l2_ctrl_av1_frame);
2062 		break;
2063 	case V4L2_CTRL_TYPE_AV1_FILM_GRAIN:
2064 		elem_size = sizeof(struct v4l2_ctrl_av1_film_grain);
2065 		break;
2066 	case V4L2_CTRL_TYPE_AREA:
2067 		elem_size = sizeof(struct v4l2_area);
2068 		break;
2069 	case V4L2_CTRL_TYPE_RECT:
2070 		elem_size = sizeof(struct v4l2_rect);
2071 		break;
2072 	default:
2073 		if (type < V4L2_CTRL_COMPOUND_TYPES)
2074 			elem_size = sizeof(s32);
2075 		break;
2076 	}
2077 
2078 	if (type < V4L2_CTRL_COMPOUND_TYPES &&
2079 	    type != V4L2_CTRL_TYPE_BUTTON &&
2080 	    type != V4L2_CTRL_TYPE_CTRL_CLASS &&
2081 	    type != V4L2_CTRL_TYPE_STRING)
2082 		flags |= V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX;
2083 
2084 	/* Sanity checks */
2085 	if (id == 0 || name == NULL || !elem_size ||
2086 	    id >= V4L2_CID_PRIVATE_BASE ||
2087 	    (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
2088 	    (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
2089 		handler_set_err(hdl, -ERANGE);
2090 		return NULL;
2091 	}
2092 
2093 	err = check_range(type, min, max, step, def);
2094 	if (err) {
2095 		handler_set_err(hdl, err);
2096 		return NULL;
2097 	}
2098 	if (is_array &&
2099 	    (type == V4L2_CTRL_TYPE_BUTTON ||
2100 	     type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
2101 		handler_set_err(hdl, -EINVAL);
2102 		return NULL;
2103 	}
2104 	if (flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY) {
2105 		/*
2106 		 * For now only support this for one-dimensional arrays only.
2107 		 *
2108 		 * This can be relaxed in the future, but this will
2109 		 * require more effort.
2110 		 */
2111 		if (nr_of_dims != 1) {
2112 			handler_set_err(hdl, -EINVAL);
2113 			return NULL;
2114 		}
2115 		/* Start with just 1 element */
2116 		elems = 1;
2117 	}
2118 
2119 	tot_ctrl_size = elem_size * elems;
2120 	sz_extra = 0;
2121 	if (type == V4L2_CTRL_TYPE_BUTTON)
2122 		flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
2123 			V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
2124 	else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
2125 		flags |= V4L2_CTRL_FLAG_READ_ONLY;
2126 	else if (!is_array &&
2127 		 (type == V4L2_CTRL_TYPE_INTEGER64 ||
2128 		  type == V4L2_CTRL_TYPE_STRING ||
2129 		  type >= V4L2_CTRL_COMPOUND_TYPES))
2130 		sz_extra += 2 * tot_ctrl_size;
2131 
2132 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const)
2133 		sz_extra += elem_size;
2134 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_min.p_const)
2135 		sz_extra += elem_size;
2136 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_max.p_const)
2137 		sz_extra += elem_size;
2138 
2139 	ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
2140 	if (ctrl == NULL) {
2141 		handler_set_err(hdl, -ENOMEM);
2142 		return NULL;
2143 	}
2144 
2145 	INIT_LIST_HEAD(&ctrl->node);
2146 	INIT_LIST_HEAD(&ctrl->ev_subs);
2147 	ctrl->handler = hdl;
2148 	ctrl->ops = ops;
2149 	ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
2150 	ctrl->id = id;
2151 	ctrl->name = name;
2152 	ctrl->type = type;
2153 	ctrl->flags = flags;
2154 	ctrl->minimum = min;
2155 	ctrl->maximum = max;
2156 	ctrl->step = step;
2157 	ctrl->default_value = def;
2158 	ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
2159 	ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
2160 	ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
2161 	ctrl->is_array = is_array;
2162 	ctrl->is_dyn_array = !!(flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY);
2163 	ctrl->elems = elems;
2164 	ctrl->new_elems = elems;
2165 	ctrl->nr_of_dims = nr_of_dims;
2166 	if (nr_of_dims)
2167 		memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
2168 	ctrl->elem_size = elem_size;
2169 	if (type == V4L2_CTRL_TYPE_MENU)
2170 		ctrl->qmenu = qmenu;
2171 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2172 		ctrl->qmenu_int = qmenu_int;
2173 	ctrl->priv = priv;
2174 	ctrl->cur.val = ctrl->val = def;
2175 	data = &ctrl[1];
2176 
2177 	if (ctrl->is_array) {
2178 		ctrl->p_array_alloc_elems = elems;
2179 		ctrl->p_array = kvzalloc(2 * elems * elem_size, GFP_KERNEL);
2180 		if (!ctrl->p_array) {
2181 			kvfree(ctrl);
2182 			return NULL;
2183 		}
2184 		data = ctrl->p_array;
2185 	}
2186 
2187 	if (!ctrl->is_int) {
2188 		ctrl->p_new.p = data;
2189 		ctrl->p_cur.p = data + tot_ctrl_size;
2190 	} else {
2191 		ctrl->p_new.p = &ctrl->val;
2192 		ctrl->p_cur.p = &ctrl->cur.val;
2193 	}
2194 
2195 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) {
2196 		if (ctrl->is_array)
2197 			ctrl->p_def.p = &ctrl[1];
2198 		else
2199 			ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size;
2200 		memcpy(ctrl->p_def.p, p_def.p_const, elem_size);
2201 	}
2202 
2203 	if (flags & V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX) {
2204 		void *ptr = ctrl->p_def.p;
2205 
2206 		if (p_min.p_const) {
2207 			ptr += elem_size;
2208 			ctrl->p_min.p = ptr;
2209 			memcpy(ctrl->p_min.p, p_min.p_const, elem_size);
2210 		}
2211 
2212 		if (p_max.p_const) {
2213 			ptr += elem_size;
2214 			ctrl->p_max.p = ptr;
2215 			memcpy(ctrl->p_max.p, p_max.p_const, elem_size);
2216 		}
2217 	}
2218 
2219 	ctrl->type_ops->init(ctrl, 0, ctrl->p_cur);
2220 	cur_to_new(ctrl);
2221 
2222 	if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
2223 		kvfree(ctrl->p_array);
2224 		kvfree(ctrl);
2225 		return NULL;
2226 	}
2227 	mutex_lock(hdl->lock);
2228 	list_add_tail(&ctrl->node, &hdl->ctrls);
2229 	mutex_unlock(hdl->lock);
2230 	return ctrl;
2231 }
2232 
2233 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
2234 			const struct v4l2_ctrl_config *cfg, void *priv)
2235 {
2236 	bool is_menu;
2237 	struct v4l2_ctrl *ctrl;
2238 	const char *name = cfg->name;
2239 	const char * const *qmenu = cfg->qmenu;
2240 	const s64 *qmenu_int = cfg->qmenu_int;
2241 	enum v4l2_ctrl_type type = cfg->type;
2242 	u32 flags = cfg->flags;
2243 	s64 min = cfg->min;
2244 	s64 max = cfg->max;
2245 	u64 step = cfg->step;
2246 	s64 def = cfg->def;
2247 
2248 	if (name == NULL)
2249 		v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
2250 								&def, &flags);
2251 
2252 	is_menu = (type == V4L2_CTRL_TYPE_MENU ||
2253 		   type == V4L2_CTRL_TYPE_INTEGER_MENU);
2254 	if (is_menu)
2255 		WARN_ON(step);
2256 	else
2257 		WARN_ON(cfg->menu_skip_mask);
2258 	if (type == V4L2_CTRL_TYPE_MENU && !qmenu) {
2259 		qmenu = v4l2_ctrl_get_menu(cfg->id);
2260 	} else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) {
2261 		handler_set_err(hdl, -EINVAL);
2262 		return NULL;
2263 	}
2264 
2265 	ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
2266 			type, min, max,
2267 			is_menu ? cfg->menu_skip_mask : step, def,
2268 			cfg->dims, cfg->elem_size,
2269 			flags, qmenu, qmenu_int, cfg->p_def, cfg->p_min,
2270 			cfg->p_max, priv);
2271 	if (ctrl)
2272 		ctrl->is_private = cfg->is_private;
2273 	return ctrl;
2274 }
2275 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
2276 
2277 /* Helper function for standard non-menu controls */
2278 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
2279 			const struct v4l2_ctrl_ops *ops,
2280 			u32 id, s64 min, s64 max, u64 step, s64 def)
2281 {
2282 	const char *name;
2283 	enum v4l2_ctrl_type type;
2284 	u32 flags;
2285 
2286 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2287 	if (type == V4L2_CTRL_TYPE_MENU ||
2288 	    type == V4L2_CTRL_TYPE_INTEGER_MENU ||
2289 	    type >= V4L2_CTRL_COMPOUND_TYPES) {
2290 		handler_set_err(hdl, -EINVAL);
2291 		return NULL;
2292 	}
2293 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2294 			     min, max, step, def, NULL, 0,
2295 			     flags, NULL, NULL, ptr_null, ptr_null,
2296 			     ptr_null, NULL);
2297 }
2298 EXPORT_SYMBOL(v4l2_ctrl_new_std);
2299 
2300 /* Helper function for standard menu controls */
2301 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
2302 			const struct v4l2_ctrl_ops *ops,
2303 			u32 id, u8 _max, u64 mask, u8 _def)
2304 {
2305 	const char * const *qmenu = NULL;
2306 	const s64 *qmenu_int = NULL;
2307 	unsigned int qmenu_int_len = 0;
2308 	const char *name;
2309 	enum v4l2_ctrl_type type;
2310 	s64 min;
2311 	s64 max = _max;
2312 	s64 def = _def;
2313 	u64 step;
2314 	u32 flags;
2315 
2316 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2317 
2318 	if (type == V4L2_CTRL_TYPE_MENU)
2319 		qmenu = v4l2_ctrl_get_menu(id);
2320 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2321 		qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
2322 
2323 	if ((!qmenu && !qmenu_int) || (qmenu_int && max >= qmenu_int_len)) {
2324 		handler_set_err(hdl, -EINVAL);
2325 		return NULL;
2326 	}
2327 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2328 			     0, max, mask, def, NULL, 0,
2329 			     flags, qmenu, qmenu_int, ptr_null, ptr_null,
2330 			     ptr_null, NULL);
2331 }
2332 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
2333 
2334 /* Helper function for standard menu controls with driver defined menu */
2335 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
2336 			const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
2337 			u64 mask, u8 _def, const char * const *qmenu)
2338 {
2339 	enum v4l2_ctrl_type type;
2340 	const char *name;
2341 	u32 flags;
2342 	u64 step;
2343 	s64 min;
2344 	s64 max = _max;
2345 	s64 def = _def;
2346 
2347 	/* v4l2_ctrl_new_std_menu_items() should only be called for
2348 	 * standard controls without a standard menu.
2349 	 */
2350 	if (v4l2_ctrl_get_menu(id)) {
2351 		handler_set_err(hdl, -EINVAL);
2352 		return NULL;
2353 	}
2354 
2355 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2356 	if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
2357 		handler_set_err(hdl, -EINVAL);
2358 		return NULL;
2359 	}
2360 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2361 			     0, max, mask, def, NULL, 0,
2362 			     flags, qmenu, NULL, ptr_null, ptr_null,
2363 			     ptr_null, NULL);
2364 
2365 }
2366 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
2367 
2368 /* Helper function for standard compound controls */
2369 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
2370 				const struct v4l2_ctrl_ops *ops, u32 id,
2371 				const union v4l2_ctrl_ptr p_def,
2372 				const union v4l2_ctrl_ptr p_min,
2373 				const union v4l2_ctrl_ptr p_max)
2374 {
2375 	const char *name;
2376 	enum v4l2_ctrl_type type;
2377 	u32 flags;
2378 	s64 min, max, step, def;
2379 
2380 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2381 	if (type < V4L2_CTRL_COMPOUND_TYPES) {
2382 		handler_set_err(hdl, -EINVAL);
2383 		return NULL;
2384 	}
2385 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2386 			     min, max, step, def, NULL, 0,
2387 			     flags, NULL, NULL, p_def, p_min, p_max, NULL);
2388 }
2389 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound);
2390 
2391 /* Helper function for standard integer menu controls */
2392 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
2393 			const struct v4l2_ctrl_ops *ops,
2394 			u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
2395 {
2396 	const char *name;
2397 	enum v4l2_ctrl_type type;
2398 	s64 min;
2399 	u64 step;
2400 	s64 max = _max;
2401 	s64 def = _def;
2402 	u32 flags;
2403 
2404 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2405 	if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
2406 		handler_set_err(hdl, -EINVAL);
2407 		return NULL;
2408 	}
2409 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2410 			     0, max, 0, def, NULL, 0,
2411 			     flags, NULL, qmenu_int, ptr_null, ptr_null,
2412 			     ptr_null, NULL);
2413 }
2414 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
2415 
2416 /* Add the controls from another handler to our own. */
2417 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
2418 			  struct v4l2_ctrl_handler *add,
2419 			  bool (*filter)(const struct v4l2_ctrl *ctrl),
2420 			  bool from_other_dev)
2421 {
2422 	struct v4l2_ctrl_ref *ref;
2423 	int ret = 0;
2424 
2425 	/* Do nothing if either handler is NULL or if they are the same */
2426 	if (!hdl || !add || hdl == add)
2427 		return 0;
2428 	if (hdl->error)
2429 		return hdl->error;
2430 	mutex_lock(add->lock);
2431 	list_for_each_entry(ref, &add->ctrl_refs, node) {
2432 		struct v4l2_ctrl *ctrl = ref->ctrl;
2433 
2434 		/* Skip handler-private controls. */
2435 		if (ctrl->is_private)
2436 			continue;
2437 		/* And control classes */
2438 		if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2439 			continue;
2440 		/* Filter any unwanted controls */
2441 		if (filter && !filter(ctrl))
2442 			continue;
2443 		ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
2444 		if (ret)
2445 			break;
2446 	}
2447 	mutex_unlock(add->lock);
2448 	return ret;
2449 }
2450 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
2451 
2452 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
2453 {
2454 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
2455 		return true;
2456 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
2457 		return true;
2458 	switch (ctrl->id) {
2459 	case V4L2_CID_AUDIO_MUTE:
2460 	case V4L2_CID_AUDIO_VOLUME:
2461 	case V4L2_CID_AUDIO_BALANCE:
2462 	case V4L2_CID_AUDIO_BASS:
2463 	case V4L2_CID_AUDIO_TREBLE:
2464 	case V4L2_CID_AUDIO_LOUDNESS:
2465 		return true;
2466 	default:
2467 		break;
2468 	}
2469 	return false;
2470 }
2471 EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
2472 
2473 /* Cluster controls */
2474 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
2475 {
2476 	bool has_volatiles = false;
2477 	int i;
2478 
2479 	/* The first control is the master control and it must not be NULL */
2480 	if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
2481 		return;
2482 
2483 	for (i = 0; i < ncontrols; i++) {
2484 		if (controls[i]) {
2485 			controls[i]->cluster = controls;
2486 			controls[i]->ncontrols = ncontrols;
2487 			if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
2488 				has_volatiles = true;
2489 		}
2490 	}
2491 	controls[0]->has_volatiles = has_volatiles;
2492 }
2493 EXPORT_SYMBOL(v4l2_ctrl_cluster);
2494 
2495 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
2496 			    u8 manual_val, bool set_volatile)
2497 {
2498 	struct v4l2_ctrl *master = controls[0];
2499 	u32 flag = 0;
2500 	int i;
2501 
2502 	v4l2_ctrl_cluster(ncontrols, controls);
2503 	WARN_ON(ncontrols <= 1);
2504 	WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
2505 	WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
2506 	master->is_auto = true;
2507 	master->has_volatiles = set_volatile;
2508 	master->manual_mode_value = manual_val;
2509 	master->flags |= V4L2_CTRL_FLAG_UPDATE;
2510 
2511 	if (!is_cur_manual(master))
2512 		flag = V4L2_CTRL_FLAG_INACTIVE |
2513 			(set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
2514 
2515 	for (i = 1; i < ncontrols; i++)
2516 		if (controls[i])
2517 			controls[i]->flags |= flag;
2518 }
2519 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
2520 
2521 /*
2522  * Obtain the current volatile values of an autocluster and mark them
2523  * as new.
2524  */
2525 void update_from_auto_cluster(struct v4l2_ctrl *master)
2526 {
2527 	int i;
2528 
2529 	for (i = 1; i < master->ncontrols; i++)
2530 		cur_to_new(master->cluster[i]);
2531 	if (!call_op(master, g_volatile_ctrl))
2532 		for (i = 1; i < master->ncontrols; i++)
2533 			if (master->cluster[i])
2534 				master->cluster[i]->is_new = 1;
2535 }
2536 
2537 /*
2538  * Return non-zero if one or more of the controls in the cluster has a new
2539  * value that differs from the current value.
2540  */
2541 static int cluster_changed(struct v4l2_ctrl *master)
2542 {
2543 	bool changed = false;
2544 	int i;
2545 
2546 	for (i = 0; i < master->ncontrols; i++) {
2547 		struct v4l2_ctrl *ctrl = master->cluster[i];
2548 		bool ctrl_changed = false;
2549 
2550 		if (!ctrl)
2551 			continue;
2552 
2553 		if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE) {
2554 			changed = true;
2555 			ctrl_changed = true;
2556 		}
2557 
2558 		/*
2559 		 * Set has_changed to false to avoid generating
2560 		 * the event V4L2_EVENT_CTRL_CH_VALUE
2561 		 */
2562 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
2563 			ctrl->has_changed = false;
2564 			continue;
2565 		}
2566 
2567 		if (ctrl->elems != ctrl->new_elems)
2568 			ctrl_changed = true;
2569 		if (!ctrl_changed)
2570 			ctrl_changed = !ctrl->type_ops->equal(ctrl,
2571 				ctrl->p_cur, ctrl->p_new);
2572 		ctrl->has_changed = ctrl_changed;
2573 		changed |= ctrl->has_changed;
2574 	}
2575 	return changed;
2576 }
2577 
2578 /*
2579  * Core function that calls try/s_ctrl and ensures that the new value is
2580  * copied to the current value on a set.
2581  * Must be called with ctrl->handler->lock held.
2582  */
2583 int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
2584 		       bool set, u32 ch_flags)
2585 {
2586 	bool update_flag;
2587 	int ret;
2588 	int i;
2589 
2590 	/*
2591 	 * Go through the cluster and either validate the new value or
2592 	 * (if no new value was set), copy the current value to the new
2593 	 * value, ensuring a consistent view for the control ops when
2594 	 * called.
2595 	 */
2596 	for (i = 0; i < master->ncontrols; i++) {
2597 		struct v4l2_ctrl *ctrl = master->cluster[i];
2598 
2599 		if (!ctrl)
2600 			continue;
2601 
2602 		if (!ctrl->is_new) {
2603 			cur_to_new(ctrl);
2604 			continue;
2605 		}
2606 		/*
2607 		 * Check again: it may have changed since the
2608 		 * previous check in try_or_set_ext_ctrls().
2609 		 */
2610 		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2611 			return -EBUSY;
2612 	}
2613 
2614 	ret = call_op(master, try_ctrl);
2615 
2616 	/* Don't set if there is no change */
2617 	if (ret || !set || !cluster_changed(master))
2618 		return ret;
2619 	ret = call_op(master, s_ctrl);
2620 	if (ret)
2621 		return ret;
2622 
2623 	/* If OK, then make the new values permanent. */
2624 	update_flag = is_cur_manual(master) != is_new_manual(master);
2625 
2626 	for (i = 0; i < master->ncontrols; i++) {
2627 		/*
2628 		 * If we switch from auto to manual mode, and this cluster
2629 		 * contains volatile controls, then all non-master controls
2630 		 * have to be marked as changed. The 'new' value contains
2631 		 * the volatile value (obtained by update_from_auto_cluster),
2632 		 * which now has to become the current value.
2633 		 */
2634 		if (i && update_flag && is_new_manual(master) &&
2635 		    master->has_volatiles && master->cluster[i])
2636 			master->cluster[i]->has_changed = true;
2637 
2638 		new_to_cur(fh, master->cluster[i], ch_flags |
2639 			((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
2640 	}
2641 	return 0;
2642 }
2643 
2644 /* Activate/deactivate a control. */
2645 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
2646 {
2647 	/* invert since the actual flag is called 'inactive' */
2648 	bool inactive = !active;
2649 	bool old;
2650 
2651 	if (ctrl == NULL)
2652 		return;
2653 
2654 	if (inactive)
2655 		/* set V4L2_CTRL_FLAG_INACTIVE */
2656 		old = test_and_set_bit(4, &ctrl->flags);
2657 	else
2658 		/* clear V4L2_CTRL_FLAG_INACTIVE */
2659 		old = test_and_clear_bit(4, &ctrl->flags);
2660 	if (old != inactive)
2661 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2662 }
2663 EXPORT_SYMBOL(v4l2_ctrl_activate);
2664 
2665 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
2666 {
2667 	bool old;
2668 
2669 	if (ctrl == NULL)
2670 		return;
2671 
2672 	lockdep_assert_held(ctrl->handler->lock);
2673 
2674 	if (grabbed)
2675 		/* set V4L2_CTRL_FLAG_GRABBED */
2676 		old = test_and_set_bit(1, &ctrl->flags);
2677 	else
2678 		/* clear V4L2_CTRL_FLAG_GRABBED */
2679 		old = test_and_clear_bit(1, &ctrl->flags);
2680 	if (old != grabbed)
2681 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2682 }
2683 EXPORT_SYMBOL(__v4l2_ctrl_grab);
2684 
2685 /* Call s_ctrl for all controls owned by the handler */
2686 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2687 {
2688 	struct v4l2_ctrl *ctrl;
2689 	int ret = 0;
2690 
2691 	if (hdl == NULL)
2692 		return 0;
2693 
2694 	lockdep_assert_held(hdl->lock);
2695 
2696 	list_for_each_entry(ctrl, &hdl->ctrls, node)
2697 		ctrl->done = false;
2698 
2699 	list_for_each_entry(ctrl, &hdl->ctrls, node) {
2700 		struct v4l2_ctrl *master = ctrl->cluster[0];
2701 		int i;
2702 
2703 		/* Skip if this control was already handled by a cluster. */
2704 		/* Skip button controls and read-only controls. */
2705 		if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
2706 		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
2707 			continue;
2708 
2709 		for (i = 0; i < master->ncontrols; i++) {
2710 			if (master->cluster[i]) {
2711 				cur_to_new(master->cluster[i]);
2712 				master->cluster[i]->is_new = 1;
2713 				master->cluster[i]->done = true;
2714 			}
2715 		}
2716 		ret = call_op(master, s_ctrl);
2717 		if (ret)
2718 			break;
2719 	}
2720 
2721 	return ret;
2722 }
2723 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
2724 
2725 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2726 {
2727 	int ret;
2728 
2729 	if (hdl == NULL)
2730 		return 0;
2731 
2732 	mutex_lock(hdl->lock);
2733 	ret = __v4l2_ctrl_handler_setup(hdl);
2734 	mutex_unlock(hdl->lock);
2735 
2736 	return ret;
2737 }
2738 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
2739 
2740 /* Log the control name and value */
2741 static void log_ctrl(const struct v4l2_ctrl *ctrl,
2742 		     const char *prefix, const char *colon)
2743 {
2744 	if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
2745 		return;
2746 	if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2747 		return;
2748 
2749 	pr_info("%s%s%s: ", prefix, colon, ctrl->name);
2750 
2751 	ctrl->type_ops->log(ctrl);
2752 
2753 	if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
2754 			   V4L2_CTRL_FLAG_GRABBED |
2755 			   V4L2_CTRL_FLAG_VOLATILE)) {
2756 		if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
2757 			pr_cont(" inactive");
2758 		if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
2759 			pr_cont(" grabbed");
2760 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
2761 			pr_cont(" volatile");
2762 	}
2763 	pr_cont("\n");
2764 }
2765 
2766 /* Log all controls owned by the handler */
2767 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
2768 				  const char *prefix)
2769 {
2770 	struct v4l2_ctrl *ctrl;
2771 	const char *colon = "";
2772 	int len;
2773 
2774 	if (!hdl)
2775 		return;
2776 	if (!prefix)
2777 		prefix = "";
2778 	len = strlen(prefix);
2779 	if (len && prefix[len - 1] != ' ')
2780 		colon = ": ";
2781 	mutex_lock(hdl->lock);
2782 	list_for_each_entry(ctrl, &hdl->ctrls, node)
2783 		if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
2784 			log_ctrl(ctrl, prefix, colon);
2785 	mutex_unlock(hdl->lock);
2786 }
2787 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
2788 
2789 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,
2790 				    const struct v4l2_ctrl_ops *ctrl_ops,
2791 				    const struct v4l2_fwnode_device_properties *p)
2792 {
2793 	if (hdl->error)
2794 		return hdl->error;
2795 
2796 	if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) {
2797 		u32 orientation_ctrl;
2798 
2799 		switch (p->orientation) {
2800 		case V4L2_FWNODE_ORIENTATION_FRONT:
2801 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT;
2802 			break;
2803 		case V4L2_FWNODE_ORIENTATION_BACK:
2804 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK;
2805 			break;
2806 		case V4L2_FWNODE_ORIENTATION_EXTERNAL:
2807 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL;
2808 			break;
2809 		default:
2810 			hdl->error = -EINVAL;
2811 			return hdl->error;
2812 		}
2813 		if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops,
2814 					    V4L2_CID_CAMERA_ORIENTATION,
2815 					    V4L2_CAMERA_ORIENTATION_EXTERNAL, 0,
2816 					    orientation_ctrl))
2817 			return hdl->error;
2818 	}
2819 
2820 	if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) {
2821 		if (!v4l2_ctrl_new_std(hdl, ctrl_ops,
2822 				       V4L2_CID_CAMERA_SENSOR_ROTATION,
2823 				       p->rotation, p->rotation, 1,
2824 				       p->rotation))
2825 			return hdl->error;
2826 	}
2827 
2828 	return hdl->error;
2829 }
2830 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties);
2831