• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     V4L2 controls framework implementation.
4 
5     Copyright (C) 2010  Hans Verkuil <hverkuil@xs4all.nl>
6 
7  */
8 
9 #define pr_fmt(fmt) "v4l2-ctrls: " fmt
10 
11 #include <linux/ctype.h>
12 #include <linux/export.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-dev.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-ioctl.h>
21 
22 #define dprintk(vdev, fmt, arg...) do {					\
23 	if (!WARN_ON(!(vdev)) && ((vdev)->dev_debug & V4L2_DEV_DEBUG_CTRL)) \
24 		printk(KERN_DEBUG pr_fmt("%s: %s: " fmt),		\
25 		       __func__, video_device_node_name(vdev), ##arg);	\
26 } while (0)
27 
28 #define has_op(master, op) \
29 	(master->ops && master->ops->op)
30 #define call_op(master, op) \
31 	(has_op(master, op) ? master->ops->op(master) : 0)
32 
33 static const union v4l2_ctrl_ptr ptr_null;
34 
35 /* Internal temporary helper struct, one for each v4l2_ext_control */
36 struct v4l2_ctrl_helper {
37 	/* Pointer to the control reference of the master control */
38 	struct v4l2_ctrl_ref *mref;
39 	/* The control ref corresponding to the v4l2_ext_control ID field. */
40 	struct v4l2_ctrl_ref *ref;
41 	/* v4l2_ext_control index of the next control belonging to the
42 	   same cluster, or 0 if there isn't any. */
43 	u32 next;
44 };
45 
46 /* Small helper function to determine if the autocluster is set to manual
47    mode. */
is_cur_manual(const struct v4l2_ctrl * master)48 static bool is_cur_manual(const struct v4l2_ctrl *master)
49 {
50 	return master->is_auto && master->cur.val == master->manual_mode_value;
51 }
52 
53 /* Same as above, but this checks the against the new value instead of the
54    current value. */
is_new_manual(const struct v4l2_ctrl * master)55 static bool is_new_manual(const struct v4l2_ctrl *master)
56 {
57 	return master->is_auto && master->val == master->manual_mode_value;
58 }
59 
60 /* Returns NULL or a character pointer array containing the menu for
61    the given control ID. The pointer array ends with a NULL pointer.
62    An empty string signifies a menu entry that is invalid. This allows
63    drivers to disable certain options if it is not supported. */
v4l2_ctrl_get_menu(u32 id)64 const char * const *v4l2_ctrl_get_menu(u32 id)
65 {
66 	static const char * const mpeg_audio_sampling_freq[] = {
67 		"44.1 kHz",
68 		"48 kHz",
69 		"32 kHz",
70 		NULL
71 	};
72 	static const char * const mpeg_audio_encoding[] = {
73 		"MPEG-1/2 Layer I",
74 		"MPEG-1/2 Layer II",
75 		"MPEG-1/2 Layer III",
76 		"MPEG-2/4 AAC",
77 		"AC-3",
78 		NULL
79 	};
80 	static const char * const mpeg_audio_l1_bitrate[] = {
81 		"32 kbps",
82 		"64 kbps",
83 		"96 kbps",
84 		"128 kbps",
85 		"160 kbps",
86 		"192 kbps",
87 		"224 kbps",
88 		"256 kbps",
89 		"288 kbps",
90 		"320 kbps",
91 		"352 kbps",
92 		"384 kbps",
93 		"416 kbps",
94 		"448 kbps",
95 		NULL
96 	};
97 	static const char * const mpeg_audio_l2_bitrate[] = {
98 		"32 kbps",
99 		"48 kbps",
100 		"56 kbps",
101 		"64 kbps",
102 		"80 kbps",
103 		"96 kbps",
104 		"112 kbps",
105 		"128 kbps",
106 		"160 kbps",
107 		"192 kbps",
108 		"224 kbps",
109 		"256 kbps",
110 		"320 kbps",
111 		"384 kbps",
112 		NULL
113 	};
114 	static const char * const mpeg_audio_l3_bitrate[] = {
115 		"32 kbps",
116 		"40 kbps",
117 		"48 kbps",
118 		"56 kbps",
119 		"64 kbps",
120 		"80 kbps",
121 		"96 kbps",
122 		"112 kbps",
123 		"128 kbps",
124 		"160 kbps",
125 		"192 kbps",
126 		"224 kbps",
127 		"256 kbps",
128 		"320 kbps",
129 		NULL
130 	};
131 	static const char * const mpeg_audio_ac3_bitrate[] = {
132 		"32 kbps",
133 		"40 kbps",
134 		"48 kbps",
135 		"56 kbps",
136 		"64 kbps",
137 		"80 kbps",
138 		"96 kbps",
139 		"112 kbps",
140 		"128 kbps",
141 		"160 kbps",
142 		"192 kbps",
143 		"224 kbps",
144 		"256 kbps",
145 		"320 kbps",
146 		"384 kbps",
147 		"448 kbps",
148 		"512 kbps",
149 		"576 kbps",
150 		"640 kbps",
151 		NULL
152 	};
153 	static const char * const mpeg_audio_mode[] = {
154 		"Stereo",
155 		"Joint Stereo",
156 		"Dual",
157 		"Mono",
158 		NULL
159 	};
160 	static const char * const mpeg_audio_mode_extension[] = {
161 		"Bound 4",
162 		"Bound 8",
163 		"Bound 12",
164 		"Bound 16",
165 		NULL
166 	};
167 	static const char * const mpeg_audio_emphasis[] = {
168 		"No Emphasis",
169 		"50/15 us",
170 		"CCITT J17",
171 		NULL
172 	};
173 	static const char * const mpeg_audio_crc[] = {
174 		"No CRC",
175 		"16-bit CRC",
176 		NULL
177 	};
178 	static const char * const mpeg_audio_dec_playback[] = {
179 		"Auto",
180 		"Stereo",
181 		"Left",
182 		"Right",
183 		"Mono",
184 		"Swapped Stereo",
185 		NULL
186 	};
187 	static const char * const mpeg_video_encoding[] = {
188 		"MPEG-1",
189 		"MPEG-2",
190 		"MPEG-4 AVC",
191 		NULL
192 	};
193 	static const char * const mpeg_video_aspect[] = {
194 		"1x1",
195 		"4x3",
196 		"16x9",
197 		"2.21x1",
198 		NULL
199 	};
200 	static const char * const mpeg_video_bitrate_mode[] = {
201 		"Variable Bitrate",
202 		"Constant Bitrate",
203 		"Constant Quality",
204 		NULL
205 	};
206 	static const char * const mpeg_stream_type[] = {
207 		"MPEG-2 Program Stream",
208 		"MPEG-2 Transport Stream",
209 		"MPEG-1 System Stream",
210 		"MPEG-2 DVD-compatible Stream",
211 		"MPEG-1 VCD-compatible Stream",
212 		"MPEG-2 SVCD-compatible Stream",
213 		NULL
214 	};
215 	static const char * const mpeg_stream_vbi_fmt[] = {
216 		"No VBI",
217 		"Private Packet, IVTV Format",
218 		NULL
219 	};
220 	static const char * const camera_power_line_frequency[] = {
221 		"Disabled",
222 		"50 Hz",
223 		"60 Hz",
224 		"Auto",
225 		NULL
226 	};
227 	static const char * const camera_exposure_auto[] = {
228 		"Auto Mode",
229 		"Manual Mode",
230 		"Shutter Priority Mode",
231 		"Aperture Priority Mode",
232 		NULL
233 	};
234 	static const char * const camera_exposure_metering[] = {
235 		"Average",
236 		"Center Weighted",
237 		"Spot",
238 		"Matrix",
239 		NULL
240 	};
241 	static const char * const camera_auto_focus_range[] = {
242 		"Auto",
243 		"Normal",
244 		"Macro",
245 		"Infinity",
246 		NULL
247 	};
248 	static const char * const colorfx[] = {
249 		"None",
250 		"Black & White",
251 		"Sepia",
252 		"Negative",
253 		"Emboss",
254 		"Sketch",
255 		"Sky Blue",
256 		"Grass Green",
257 		"Skin Whiten",
258 		"Vivid",
259 		"Aqua",
260 		"Art Freeze",
261 		"Silhouette",
262 		"Solarization",
263 		"Antique",
264 		"Set Cb/Cr",
265 		NULL
266 	};
267 	static const char * const auto_n_preset_white_balance[] = {
268 		"Manual",
269 		"Auto",
270 		"Incandescent",
271 		"Fluorescent",
272 		"Fluorescent H",
273 		"Horizon",
274 		"Daylight",
275 		"Flash",
276 		"Cloudy",
277 		"Shade",
278 		NULL,
279 	};
280 	static const char * const camera_iso_sensitivity_auto[] = {
281 		"Manual",
282 		"Auto",
283 		NULL
284 	};
285 	static const char * const scene_mode[] = {
286 		"None",
287 		"Backlight",
288 		"Beach/Snow",
289 		"Candle Light",
290 		"Dusk/Dawn",
291 		"Fall Colors",
292 		"Fireworks",
293 		"Landscape",
294 		"Night",
295 		"Party/Indoor",
296 		"Portrait",
297 		"Sports",
298 		"Sunset",
299 		"Text",
300 		NULL
301 	};
302 	static const char * const tune_emphasis[] = {
303 		"None",
304 		"50 Microseconds",
305 		"75 Microseconds",
306 		NULL,
307 	};
308 	static const char * const header_mode[] = {
309 		"Separate Buffer",
310 		"Joined With 1st Frame",
311 		NULL,
312 	};
313 	static const char * const multi_slice[] = {
314 		"Single",
315 		"Max Macroblocks",
316 		"Max Bytes",
317 		NULL,
318 	};
319 	static const char * const entropy_mode[] = {
320 		"CAVLC",
321 		"CABAC",
322 		NULL,
323 	};
324 	static const char * const mpeg_h264_level[] = {
325 		"1",
326 		"1b",
327 		"1.1",
328 		"1.2",
329 		"1.3",
330 		"2",
331 		"2.1",
332 		"2.2",
333 		"3",
334 		"3.1",
335 		"3.2",
336 		"4",
337 		"4.1",
338 		"4.2",
339 		"5",
340 		"5.1",
341 		"5.2",
342 		"6.0",
343 		"6.1",
344 		"6.2",
345 		NULL,
346 	};
347 	static const char * const h264_loop_filter[] = {
348 		"Enabled",
349 		"Disabled",
350 		"Disabled at Slice Boundary",
351 		NULL,
352 	};
353 	static const char * const h264_profile[] = {
354 		"Baseline",
355 		"Constrained Baseline",
356 		"Main",
357 		"Extended",
358 		"High",
359 		"High 10",
360 		"High 422",
361 		"High 444 Predictive",
362 		"High 10 Intra",
363 		"High 422 Intra",
364 		"High 444 Intra",
365 		"CAVLC 444 Intra",
366 		"Scalable Baseline",
367 		"Scalable High",
368 		"Scalable High Intra",
369 		"Stereo High",
370 		"Multiview High",
371 		"Constrained High",
372 		NULL,
373 	};
374 	static const char * const vui_sar_idc[] = {
375 		"Unspecified",
376 		"1:1",
377 		"12:11",
378 		"10:11",
379 		"16:11",
380 		"40:33",
381 		"24:11",
382 		"20:11",
383 		"32:11",
384 		"80:33",
385 		"18:11",
386 		"15:11",
387 		"64:33",
388 		"160:99",
389 		"4:3",
390 		"3:2",
391 		"2:1",
392 		"Extended SAR",
393 		NULL,
394 	};
395 	static const char * const h264_fp_arrangement_type[] = {
396 		"Checkerboard",
397 		"Column",
398 		"Row",
399 		"Side by Side",
400 		"Top Bottom",
401 		"Temporal",
402 		NULL,
403 	};
404 	static const char * const h264_fmo_map_type[] = {
405 		"Interleaved Slices",
406 		"Scattered Slices",
407 		"Foreground with Leftover",
408 		"Box Out",
409 		"Raster Scan",
410 		"Wipe Scan",
411 		"Explicit",
412 		NULL,
413 	};
414 	static const char * const h264_decode_mode[] = {
415 		"Slice-Based",
416 		"Frame-Based",
417 		NULL,
418 	};
419 	static const char * const h264_start_code[] = {
420 		"No Start Code",
421 		"Annex B Start Code",
422 		NULL,
423 	};
424 	static const char * const h264_hierarchical_coding_type[] = {
425 		"Hier Coding B",
426 		"Hier Coding P",
427 		NULL,
428 	};
429 	static const char * const mpeg_mpeg2_level[] = {
430 		"Low",
431 		"Main",
432 		"High 1440",
433 		"High",
434 		NULL,
435 	};
436 	static const char * const mpeg2_profile[] = {
437 		"Simple",
438 		"Main",
439 		"SNR Scalable",
440 		"Spatially Scalable",
441 		"High",
442 		NULL,
443 	};
444 	static const char * const mpeg_mpeg4_level[] = {
445 		"0",
446 		"0b",
447 		"1",
448 		"2",
449 		"3",
450 		"3b",
451 		"4",
452 		"5",
453 		NULL,
454 	};
455 	static const char * const mpeg4_profile[] = {
456 		"Simple",
457 		"Advanced Simple",
458 		"Core",
459 		"Simple Scalable",
460 		"Advanced Coding Efficiency",
461 		NULL,
462 	};
463 
464 	static const char * const vpx_golden_frame_sel[] = {
465 		"Use Previous Frame",
466 		"Use Previous Specific Frame",
467 		NULL,
468 	};
469 	static const char * const vp8_profile[] = {
470 		"0",
471 		"1",
472 		"2",
473 		"3",
474 		NULL,
475 	};
476 	static const char * const vp9_profile[] = {
477 		"0",
478 		"1",
479 		"2",
480 		"3",
481 		NULL,
482 	};
483 	static const char * const vp9_level[] = {
484 		"1",
485 		"1.1",
486 		"2",
487 		"2.1",
488 		"3",
489 		"3.1",
490 		"4",
491 		"4.1",
492 		"5",
493 		"5.1",
494 		"5.2",
495 		"6",
496 		"6.1",
497 		"6.2",
498 		NULL,
499 	};
500 
501 	static const char * const flash_led_mode[] = {
502 		"Off",
503 		"Flash",
504 		"Torch",
505 		NULL,
506 	};
507 	static const char * const flash_strobe_source[] = {
508 		"Software",
509 		"External",
510 		NULL,
511 	};
512 
513 	static const char * const jpeg_chroma_subsampling[] = {
514 		"4:4:4",
515 		"4:2:2",
516 		"4:2:0",
517 		"4:1:1",
518 		"4:1:0",
519 		"Gray",
520 		NULL,
521 	};
522 	static const char * const dv_tx_mode[] = {
523 		"DVI-D",
524 		"HDMI",
525 		NULL,
526 	};
527 	static const char * const dv_rgb_range[] = {
528 		"Automatic",
529 		"RGB Limited Range (16-235)",
530 		"RGB Full Range (0-255)",
531 		NULL,
532 	};
533 	static const char * const dv_it_content_type[] = {
534 		"Graphics",
535 		"Photo",
536 		"Cinema",
537 		"Game",
538 		"No IT Content",
539 		NULL,
540 	};
541 	static const char * const detect_md_mode[] = {
542 		"Disabled",
543 		"Global",
544 		"Threshold Grid",
545 		"Region Grid",
546 		NULL,
547 	};
548 
549 	static const char * const hevc_profile[] = {
550 		"Main",
551 		"Main Still Picture",
552 		"Main 10",
553 		NULL,
554 	};
555 	static const char * const hevc_level[] = {
556 		"1",
557 		"2",
558 		"2.1",
559 		"3",
560 		"3.1",
561 		"4",
562 		"4.1",
563 		"5",
564 		"5.1",
565 		"5.2",
566 		"6",
567 		"6.1",
568 		"6.2",
569 		NULL,
570 	};
571 	static const char * const hevc_hierarchial_coding_type[] = {
572 		"B",
573 		"P",
574 		NULL,
575 	};
576 	static const char * const hevc_refresh_type[] = {
577 		"None",
578 		"CRA",
579 		"IDR",
580 		NULL,
581 	};
582 	static const char * const hevc_size_of_length_field[] = {
583 		"0",
584 		"1",
585 		"2",
586 		"4",
587 		NULL,
588 	};
589 	static const char * const hevc_tier[] = {
590 		"Main",
591 		"High",
592 		NULL,
593 	};
594 	static const char * const hevc_loop_filter_mode[] = {
595 		"Disabled",
596 		"Enabled",
597 		"Disabled at slice boundary",
598 		"NULL",
599 	};
600 	static const char * const hevc_decode_mode[] = {
601 		"Slice-Based",
602 		"Frame-Based",
603 		NULL,
604 	};
605 	static const char * const hevc_start_code[] = {
606 		"No Start Code",
607 		"Annex B Start Code",
608 		NULL,
609 	};
610 	static const char * const camera_orientation[] = {
611 		"Front",
612 		"Back",
613 		"External",
614 		NULL,
615 	};
616 	static const char * const mpeg_video_frame_skip[] = {
617 		"Disabled",
618 		"Level Limit",
619 		"VBV/CPB Limit",
620 		NULL,
621 	};
622 
623 	switch (id) {
624 	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
625 		return mpeg_audio_sampling_freq;
626 	case V4L2_CID_MPEG_AUDIO_ENCODING:
627 		return mpeg_audio_encoding;
628 	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
629 		return mpeg_audio_l1_bitrate;
630 	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
631 		return mpeg_audio_l2_bitrate;
632 	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
633 		return mpeg_audio_l3_bitrate;
634 	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
635 		return mpeg_audio_ac3_bitrate;
636 	case V4L2_CID_MPEG_AUDIO_MODE:
637 		return mpeg_audio_mode;
638 	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
639 		return mpeg_audio_mode_extension;
640 	case V4L2_CID_MPEG_AUDIO_EMPHASIS:
641 		return mpeg_audio_emphasis;
642 	case V4L2_CID_MPEG_AUDIO_CRC:
643 		return mpeg_audio_crc;
644 	case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
645 	case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
646 		return mpeg_audio_dec_playback;
647 	case V4L2_CID_MPEG_VIDEO_ENCODING:
648 		return mpeg_video_encoding;
649 	case V4L2_CID_MPEG_VIDEO_ASPECT:
650 		return mpeg_video_aspect;
651 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
652 		return mpeg_video_bitrate_mode;
653 	case V4L2_CID_MPEG_STREAM_TYPE:
654 		return mpeg_stream_type;
655 	case V4L2_CID_MPEG_STREAM_VBI_FMT:
656 		return mpeg_stream_vbi_fmt;
657 	case V4L2_CID_POWER_LINE_FREQUENCY:
658 		return camera_power_line_frequency;
659 	case V4L2_CID_EXPOSURE_AUTO:
660 		return camera_exposure_auto;
661 	case V4L2_CID_EXPOSURE_METERING:
662 		return camera_exposure_metering;
663 	case V4L2_CID_AUTO_FOCUS_RANGE:
664 		return camera_auto_focus_range;
665 	case V4L2_CID_COLORFX:
666 		return colorfx;
667 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
668 		return auto_n_preset_white_balance;
669 	case V4L2_CID_ISO_SENSITIVITY_AUTO:
670 		return camera_iso_sensitivity_auto;
671 	case V4L2_CID_SCENE_MODE:
672 		return scene_mode;
673 	case V4L2_CID_TUNE_PREEMPHASIS:
674 		return tune_emphasis;
675 	case V4L2_CID_TUNE_DEEMPHASIS:
676 		return tune_emphasis;
677 	case V4L2_CID_FLASH_LED_MODE:
678 		return flash_led_mode;
679 	case V4L2_CID_FLASH_STROBE_SOURCE:
680 		return flash_strobe_source;
681 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
682 		return header_mode;
683 	case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
684 		return mpeg_video_frame_skip;
685 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
686 		return multi_slice;
687 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
688 		return entropy_mode;
689 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
690 		return mpeg_h264_level;
691 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
692 		return h264_loop_filter;
693 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
694 		return h264_profile;
695 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
696 		return vui_sar_idc;
697 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
698 		return h264_fp_arrangement_type;
699 	case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
700 		return h264_fmo_map_type;
701 	case V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE:
702 		return h264_decode_mode;
703 	case V4L2_CID_MPEG_VIDEO_H264_START_CODE:
704 		return h264_start_code;
705 	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE:
706 		return h264_hierarchical_coding_type;
707 	case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
708 		return mpeg_mpeg2_level;
709 	case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
710 		return mpeg2_profile;
711 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
712 		return mpeg_mpeg4_level;
713 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
714 		return mpeg4_profile;
715 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
716 		return vpx_golden_frame_sel;
717 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
718 		return vp8_profile;
719 	case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:
720 		return vp9_profile;
721 	case V4L2_CID_MPEG_VIDEO_VP9_LEVEL:
722 		return vp9_level;
723 	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
724 		return jpeg_chroma_subsampling;
725 	case V4L2_CID_DV_TX_MODE:
726 		return dv_tx_mode;
727 	case V4L2_CID_DV_TX_RGB_RANGE:
728 	case V4L2_CID_DV_RX_RGB_RANGE:
729 		return dv_rgb_range;
730 	case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
731 	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
732 		return dv_it_content_type;
733 	case V4L2_CID_DETECT_MD_MODE:
734 		return detect_md_mode;
735 	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
736 		return hevc_profile;
737 	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
738 		return hevc_level;
739 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
740 		return hevc_hierarchial_coding_type;
741 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
742 		return hevc_refresh_type;
743 	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
744 		return hevc_size_of_length_field;
745 	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
746 		return hevc_tier;
747 	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
748 		return hevc_loop_filter_mode;
749 	case V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE:
750 		return hevc_decode_mode;
751 	case V4L2_CID_MPEG_VIDEO_HEVC_START_CODE:
752 		return hevc_start_code;
753 	case V4L2_CID_CAMERA_ORIENTATION:
754 		return camera_orientation;
755 	default:
756 		return NULL;
757 	}
758 }
759 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
760 
761 #define __v4l2_qmenu_int_len(arr, len) ({ *(len) = ARRAY_SIZE(arr); arr; })
762 /*
763  * Returns NULL or an s64 type array containing the menu for given
764  * control ID. The total number of the menu items is returned in @len.
765  */
v4l2_ctrl_get_int_menu(u32 id,u32 * len)766 const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len)
767 {
768 	static const s64 qmenu_int_vpx_num_partitions[] = {
769 		1, 2, 4, 8,
770 	};
771 
772 	static const s64 qmenu_int_vpx_num_ref_frames[] = {
773 		1, 2, 3,
774 	};
775 
776 	switch (id) {
777 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
778 		return __v4l2_qmenu_int_len(qmenu_int_vpx_num_partitions, len);
779 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
780 		return __v4l2_qmenu_int_len(qmenu_int_vpx_num_ref_frames, len);
781 	default:
782 		*len = 0;
783 		return NULL;
784 	}
785 }
786 EXPORT_SYMBOL(v4l2_ctrl_get_int_menu);
787 
788 /* Return the control name. */
v4l2_ctrl_get_name(u32 id)789 const char *v4l2_ctrl_get_name(u32 id)
790 {
791 	switch (id) {
792 	/* USER controls */
793 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
794 	case V4L2_CID_USER_CLASS:		return "User Controls";
795 	case V4L2_CID_BRIGHTNESS:		return "Brightness";
796 	case V4L2_CID_CONTRAST:			return "Contrast";
797 	case V4L2_CID_SATURATION:		return "Saturation";
798 	case V4L2_CID_HUE:			return "Hue";
799 	case V4L2_CID_AUDIO_VOLUME:		return "Volume";
800 	case V4L2_CID_AUDIO_BALANCE:		return "Balance";
801 	case V4L2_CID_AUDIO_BASS:		return "Bass";
802 	case V4L2_CID_AUDIO_TREBLE:		return "Treble";
803 	case V4L2_CID_AUDIO_MUTE:		return "Mute";
804 	case V4L2_CID_AUDIO_LOUDNESS:		return "Loudness";
805 	case V4L2_CID_BLACK_LEVEL:		return "Black Level";
806 	case V4L2_CID_AUTO_WHITE_BALANCE:	return "White Balance, Automatic";
807 	case V4L2_CID_DO_WHITE_BALANCE:		return "Do White Balance";
808 	case V4L2_CID_RED_BALANCE:		return "Red Balance";
809 	case V4L2_CID_BLUE_BALANCE:		return "Blue Balance";
810 	case V4L2_CID_GAMMA:			return "Gamma";
811 	case V4L2_CID_EXPOSURE:			return "Exposure";
812 	case V4L2_CID_AUTOGAIN:			return "Gain, Automatic";
813 	case V4L2_CID_GAIN:			return "Gain";
814 	case V4L2_CID_HFLIP:			return "Horizontal Flip";
815 	case V4L2_CID_VFLIP:			return "Vertical Flip";
816 	case V4L2_CID_POWER_LINE_FREQUENCY:	return "Power Line Frequency";
817 	case V4L2_CID_HUE_AUTO:			return "Hue, Automatic";
818 	case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
819 	case V4L2_CID_SHARPNESS:		return "Sharpness";
820 	case V4L2_CID_BACKLIGHT_COMPENSATION:	return "Backlight Compensation";
821 	case V4L2_CID_CHROMA_AGC:		return "Chroma AGC";
822 	case V4L2_CID_COLOR_KILLER:		return "Color Killer";
823 	case V4L2_CID_COLORFX:			return "Color Effects";
824 	case V4L2_CID_AUTOBRIGHTNESS:		return "Brightness, Automatic";
825 	case V4L2_CID_BAND_STOP_FILTER:		return "Band-Stop Filter";
826 	case V4L2_CID_ROTATE:			return "Rotate";
827 	case V4L2_CID_BG_COLOR:			return "Background Color";
828 	case V4L2_CID_CHROMA_GAIN:		return "Chroma Gain";
829 	case V4L2_CID_ILLUMINATORS_1:		return "Illuminator 1";
830 	case V4L2_CID_ILLUMINATORS_2:		return "Illuminator 2";
831 	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:	return "Min Number of Capture Buffers";
832 	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:	return "Min Number of Output Buffers";
833 	case V4L2_CID_ALPHA_COMPONENT:		return "Alpha Component";
834 	case V4L2_CID_COLORFX_CBCR:		return "Color Effects, CbCr";
835 
836 	/* Codec controls */
837 	/* The MPEG controls are applicable to all codec controls
838 	 * and the 'MPEG' part of the define is historical */
839 	/* Keep the order of the 'case's the same as in videodev2.h! */
840 	case V4L2_CID_MPEG_CLASS:		return "Codec Controls";
841 	case V4L2_CID_MPEG_STREAM_TYPE:		return "Stream Type";
842 	case V4L2_CID_MPEG_STREAM_PID_PMT:	return "Stream PMT Program ID";
843 	case V4L2_CID_MPEG_STREAM_PID_AUDIO:	return "Stream Audio Program ID";
844 	case V4L2_CID_MPEG_STREAM_PID_VIDEO:	return "Stream Video Program ID";
845 	case V4L2_CID_MPEG_STREAM_PID_PCR:	return "Stream PCR Program ID";
846 	case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
847 	case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
848 	case V4L2_CID_MPEG_STREAM_VBI_FMT:	return "Stream VBI Format";
849 	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
850 	case V4L2_CID_MPEG_AUDIO_ENCODING:	return "Audio Encoding";
851 	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:	return "Audio Layer I Bitrate";
852 	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:	return "Audio Layer II Bitrate";
853 	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:	return "Audio Layer III Bitrate";
854 	case V4L2_CID_MPEG_AUDIO_MODE:		return "Audio Stereo Mode";
855 	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
856 	case V4L2_CID_MPEG_AUDIO_EMPHASIS:	return "Audio Emphasis";
857 	case V4L2_CID_MPEG_AUDIO_CRC:		return "Audio CRC";
858 	case V4L2_CID_MPEG_AUDIO_MUTE:		return "Audio Mute";
859 	case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:	return "Audio AAC Bitrate";
860 	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:	return "Audio AC-3 Bitrate";
861 	case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:	return "Audio Playback";
862 	case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
863 	case V4L2_CID_MPEG_VIDEO_ENCODING:	return "Video Encoding";
864 	case V4L2_CID_MPEG_VIDEO_ASPECT:	return "Video Aspect";
865 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:	return "Video B Frames";
866 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:	return "Video GOP Size";
867 	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:	return "Video GOP Closure";
868 	case V4L2_CID_MPEG_VIDEO_PULLDOWN:	return "Video Pulldown";
869 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:	return "Video Bitrate Mode";
870 	case V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY:	return "Constant Quality";
871 	case V4L2_CID_MPEG_VIDEO_BITRATE:	return "Video Bitrate";
872 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:	return "Video Peak Bitrate";
873 	case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
874 	case V4L2_CID_MPEG_VIDEO_MUTE:		return "Video Mute";
875 	case V4L2_CID_MPEG_VIDEO_MUTE_YUV:	return "Video Mute YUV";
876 	case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:	return "Decoder Slice Interface";
877 	case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:	return "MPEG4 Loop Filter Enable";
878 	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:	return "Number of Intra Refresh MBs";
879 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:		return "Frame Level Rate Control Enable";
880 	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:			return "H264 MB Level Rate Control";
881 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:			return "Sequence Header Mode";
882 	case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC:			return "Max Number of Reference Pics";
883 	case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:		return "Frame Skip Mode";
884 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:		return "Display Delay";
885 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:	return "Display Delay Enable";
886 	case V4L2_CID_MPEG_VIDEO_AU_DELIMITER:			return "Generate Access Unit Delimiters";
887 	case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:		return "H263 I-Frame QP Value";
888 	case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:		return "H263 P-Frame QP Value";
889 	case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:		return "H263 B-Frame QP Value";
890 	case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:			return "H263 Minimum QP Value";
891 	case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:			return "H263 Maximum QP Value";
892 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:		return "H264 I-Frame QP Value";
893 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:		return "H264 P-Frame QP Value";
894 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:		return "H264 B-Frame QP Value";
895 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:			return "H264 Maximum QP Value";
896 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:			return "H264 Minimum QP Value";
897 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:		return "H264 8x8 Transform Enable";
898 	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:			return "H264 CPB Buffer Size";
899 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:		return "H264 Entropy Mode";
900 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:			return "H264 I-Frame Period";
901 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:			return "H264 Level";
902 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:	return "H264 Loop Filter Alpha Offset";
903 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:		return "H264 Loop Filter Beta Offset";
904 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:		return "H264 Loop Filter Mode";
905 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:			return "H264 Profile";
906 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:	return "Vertical Size of SAR";
907 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:	return "Horizontal Size of SAR";
908 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:		return "Aspect Ratio VUI Enable";
909 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:		return "VUI Aspect Ratio IDC";
910 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:	return "H264 Enable Frame Packing SEI";
911 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_CURRENT_FRAME_0:	return "H264 Set Curr. Frame as Frame0";
912 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:	return "H264 FP Arrangement Type";
913 	case V4L2_CID_MPEG_VIDEO_H264_FMO:			return "H264 Flexible MB Ordering";
914 	case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:		return "H264 Map Type for FMO";
915 	case V4L2_CID_MPEG_VIDEO_H264_FMO_SLICE_GROUP:		return "H264 FMO Number of Slice Groups";
916 	case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_DIRECTION:	return "H264 FMO Direction of Change";
917 	case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_RATE:		return "H264 FMO Size of 1st Slice Grp";
918 	case V4L2_CID_MPEG_VIDEO_H264_FMO_RUN_LENGTH:		return "H264 FMO No. of Consecutive MBs";
919 	case V4L2_CID_MPEG_VIDEO_H264_ASO:			return "H264 Arbitrary Slice Ordering";
920 	case V4L2_CID_MPEG_VIDEO_H264_ASO_SLICE_ORDER:		return "H264 ASO Slice Order";
921 	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING:	return "Enable H264 Hierarchical Coding";
922 	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE:	return "H264 Hierarchical Coding Type";
923 	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER:return "H264 Number of HC Layers";
924 	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER_QP:
925 								return "H264 Set QP Value for HC Layers";
926 	case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
927 								return "H264 Constrained Intra Pred";
928 	case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:	return "H264 Chroma QP Index Offset";
929 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MIN_QP:		return "H264 I-Frame Minimum QP Value";
930 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MAX_QP:		return "H264 I-Frame Maximum QP Value";
931 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MIN_QP:		return "H264 P-Frame Minimum QP Value";
932 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MAX_QP:		return "H264 P-Frame Maximum QP Value";
933 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MIN_QP:		return "H264 B-Frame Minimum QP Value";
934 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MAX_QP:		return "H264 B-Frame Maximum QP Value";
935 	case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L0_BR:	return "H264 Hierarchical Lay 0 Bitrate";
936 	case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L1_BR:	return "H264 Hierarchical Lay 1 Bitrate";
937 	case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L2_BR:	return "H264 Hierarchical Lay 2 Bitrate";
938 	case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L3_BR:	return "H264 Hierarchical Lay 3 Bitrate";
939 	case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L4_BR:	return "H264 Hierarchical Lay 4 Bitrate";
940 	case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L5_BR:	return "H264 Hierarchical Lay 5 Bitrate";
941 	case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L6_BR:	return "H264 Hierarchical Lay 6 Bitrate";
942 	case V4L2_CID_MPEG_VIDEO_H264_SPS:			return "H264 Sequence Parameter Set";
943 	case V4L2_CID_MPEG_VIDEO_H264_PPS:			return "H264 Picture Parameter Set";
944 	case V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX:		return "H264 Scaling Matrix";
945 	case V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAMS:		return "H264 Slice Parameters";
946 	case V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAMS:		return "H264 Decode Parameters";
947 	case V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE:		return "H264 Decode Mode";
948 	case V4L2_CID_MPEG_VIDEO_H264_START_CODE:		return "H264 Start Code";
949 	case V4L2_CID_MPEG_VIDEO_H264_PRED_WEIGHTS:		return "H264 Prediction Weight Table";
950 	case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:			return "MPEG2 Level";
951 	case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:			return "MPEG2 Profile";
952 	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:		return "MPEG4 I-Frame QP Value";
953 	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:		return "MPEG4 P-Frame QP Value";
954 	case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:		return "MPEG4 B-Frame QP Value";
955 	case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:			return "MPEG4 Minimum QP Value";
956 	case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:			return "MPEG4 Maximum QP Value";
957 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:			return "MPEG4 Level";
958 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:			return "MPEG4 Profile";
959 	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:			return "Quarter Pixel Search Enable";
960 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:		return "Maximum Bytes in a Slice";
961 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:		return "Number of MBs in a Slice";
962 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:		return "Slice Partitioning Method";
963 	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:			return "VBV Buffer Size";
964 	case V4L2_CID_MPEG_VIDEO_DEC_PTS:			return "Video Decoder PTS";
965 	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:			return "Video Decoder Frame Count";
966 	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:		return "Video Decoder Conceal Color";
967 	case V4L2_CID_MPEG_VIDEO_VBV_DELAY:			return "Initial Delay for VBV Control";
968 	case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:		return "Horizontal MV Search Range";
969 	case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:		return "Vertical MV Search Range";
970 	case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:		return "Repeat Sequence Header";
971 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:		return "Force Key Frame";
972 	case V4L2_CID_MPEG_VIDEO_BASELAYER_PRIORITY_ID:		return "Base Layer Priority ID";
973 	case V4L2_CID_MPEG_VIDEO_LTR_COUNT:			return "LTR Count";
974 	case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:		return "Frame LTR Index";
975 	case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:		return "Use LTR Frames";
976 	case V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS:		return "MPEG-2 Slice Parameters";
977 	case V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION:		return "MPEG-2 Quantization Matrices";
978 	case V4L2_CID_MPEG_VIDEO_FWHT_PARAMS:			return "FWHT Stateless Parameters";
979 	case V4L2_CID_FWHT_I_FRAME_QP:				return "FWHT I-Frame QP Value";
980 	case V4L2_CID_FWHT_P_FRAME_QP:				return "FWHT P-Frame QP Value";
981 
982 	/* VPX controls */
983 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:		return "VPX Number of Partitions";
984 	case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:		return "VPX Intra Mode Decision Disable";
985 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:		return "VPX No. of Refs for P Frame";
986 	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:		return "VPX Loop Filter Level Range";
987 	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:		return "VPX Deblocking Effect Control";
988 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:	return "VPX Golden Frame Refresh Period";
989 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:		return "VPX Golden Frame Indicator";
990 	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:			return "VPX Minimum QP Value";
991 	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:			return "VPX Maximum QP Value";
992 	case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:		return "VPX I-Frame QP Value";
993 	case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:		return "VPX P-Frame QP Value";
994 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:			return "VP8 Profile";
995 	case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:			return "VP9 Profile";
996 	case V4L2_CID_MPEG_VIDEO_VP9_LEVEL:			return "VP9 Level";
997 	case V4L2_CID_MPEG_VIDEO_VP8_FRAME_HEADER:		return "VP8 Frame Header";
998 
999 	/* HEVC controls */
1000 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:		return "HEVC I-Frame QP Value";
1001 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP:		return "HEVC P-Frame QP Value";
1002 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP:		return "HEVC B-Frame QP Value";
1003 	case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP:			return "HEVC Minimum QP Value";
1004 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP:			return "HEVC Maximum QP Value";
1005 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QP:		return "HEVC I-Frame Minimum QP Value";
1006 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QP:		return "HEVC I-Frame Maximum QP Value";
1007 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MIN_QP:		return "HEVC P-Frame Minimum QP Value";
1008 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MAX_QP:		return "HEVC P-Frame Maximum QP Value";
1009 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MIN_QP:		return "HEVC B-Frame Minimum QP Value";
1010 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MAX_QP:		return "HEVC B-Frame Maximum QP Value";
1011 	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:			return "HEVC Profile";
1012 	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:			return "HEVC Level";
1013 	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:			return "HEVC Tier";
1014 	case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION:	return "HEVC Frame Rate Resolution";
1015 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH:	return "HEVC Maximum Coding Unit Depth";
1016 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:		return "HEVC Refresh Type";
1017 	case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED:		return "HEVC Constant Intra Prediction";
1018 	case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU:		return "HEVC Lossless Encoding";
1019 	case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT:		return "HEVC Wavefront";
1020 	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:		return "HEVC Loop Filter";
1021 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP:			return "HEVC QP Values";
1022 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:		return "HEVC Hierarchical Coding Type";
1023 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:	return "HEVC Hierarchical Coding Layer";
1024 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP:	return "HEVC Hierarchical Layer 0 QP";
1025 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP:	return "HEVC Hierarchical Layer 1 QP";
1026 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP:	return "HEVC Hierarchical Layer 2 QP";
1027 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP:	return "HEVC Hierarchical Layer 3 QP";
1028 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP:	return "HEVC Hierarchical Layer 4 QP";
1029 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP:	return "HEVC Hierarchical Layer 5 QP";
1030 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP:	return "HEVC Hierarchical Layer 6 QP";
1031 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:	return "HEVC Hierarchical Lay 0 BitRate";
1032 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:	return "HEVC Hierarchical Lay 1 BitRate";
1033 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:	return "HEVC Hierarchical Lay 2 BitRate";
1034 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:	return "HEVC Hierarchical Lay 3 BitRate";
1035 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:	return "HEVC Hierarchical Lay 4 BitRate";
1036 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:	return "HEVC Hierarchical Lay 5 BitRate";
1037 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR:	return "HEVC Hierarchical Lay 6 BitRate";
1038 	case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB:		return "HEVC General PB";
1039 	case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID:		return "HEVC Temporal ID";
1040 	case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING:		return "HEVC Strong Intra Smoothing";
1041 	case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT:		return "HEVC Intra PU Split";
1042 	case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION:		return "HEVC TMV Prediction";
1043 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1:	return "HEVC Max Num of Candidate MVs";
1044 	case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE:	return "HEVC ENC Without Startcode";
1045 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD:		return "HEVC Num of I-Frame b/w 2 IDR";
1046 	case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2:	return "HEVC Loop Filter Beta Offset";
1047 	case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2:	return "HEVC Loop Filter TC Offset";
1048 	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:	return "HEVC Size of Length Field";
1049 	case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:	return "Reference Frames for a P-Frame";
1050 	case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:		return "Prepend SPS and PPS to IDR";
1051 	case V4L2_CID_MPEG_VIDEO_HEVC_SPS:			return "HEVC Sequence Parameter Set";
1052 	case V4L2_CID_MPEG_VIDEO_HEVC_PPS:			return "HEVC Picture Parameter Set";
1053 	case V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS:		return "HEVC Slice Parameters";
1054 	case V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE:		return "HEVC Decode Mode";
1055 	case V4L2_CID_MPEG_VIDEO_HEVC_START_CODE:		return "HEVC Start Code";
1056 
1057 	/* CAMERA controls */
1058 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1059 	case V4L2_CID_CAMERA_CLASS:		return "Camera Controls";
1060 	case V4L2_CID_EXPOSURE_AUTO:		return "Auto Exposure";
1061 	case V4L2_CID_EXPOSURE_ABSOLUTE:	return "Exposure Time, Absolute";
1062 	case V4L2_CID_EXPOSURE_AUTO_PRIORITY:	return "Exposure, Dynamic Framerate";
1063 	case V4L2_CID_PAN_RELATIVE:		return "Pan, Relative";
1064 	case V4L2_CID_TILT_RELATIVE:		return "Tilt, Relative";
1065 	case V4L2_CID_PAN_RESET:		return "Pan, Reset";
1066 	case V4L2_CID_TILT_RESET:		return "Tilt, Reset";
1067 	case V4L2_CID_PAN_ABSOLUTE:		return "Pan, Absolute";
1068 	case V4L2_CID_TILT_ABSOLUTE:		return "Tilt, Absolute";
1069 	case V4L2_CID_FOCUS_ABSOLUTE:		return "Focus, Absolute";
1070 	case V4L2_CID_FOCUS_RELATIVE:		return "Focus, Relative";
1071 	case V4L2_CID_FOCUS_AUTO:		return "Focus, Automatic Continuous";
1072 	case V4L2_CID_ZOOM_ABSOLUTE:		return "Zoom, Absolute";
1073 	case V4L2_CID_ZOOM_RELATIVE:		return "Zoom, Relative";
1074 	case V4L2_CID_ZOOM_CONTINUOUS:		return "Zoom, Continuous";
1075 	case V4L2_CID_PRIVACY:			return "Privacy";
1076 	case V4L2_CID_IRIS_ABSOLUTE:		return "Iris, Absolute";
1077 	case V4L2_CID_IRIS_RELATIVE:		return "Iris, Relative";
1078 	case V4L2_CID_AUTO_EXPOSURE_BIAS:	return "Auto Exposure, Bias";
1079 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
1080 	case V4L2_CID_WIDE_DYNAMIC_RANGE:	return "Wide Dynamic Range";
1081 	case V4L2_CID_IMAGE_STABILIZATION:	return "Image Stabilization";
1082 	case V4L2_CID_ISO_SENSITIVITY:		return "ISO Sensitivity";
1083 	case V4L2_CID_ISO_SENSITIVITY_AUTO:	return "ISO Sensitivity, Auto";
1084 	case V4L2_CID_EXPOSURE_METERING:	return "Exposure, Metering Mode";
1085 	case V4L2_CID_SCENE_MODE:		return "Scene Mode";
1086 	case V4L2_CID_3A_LOCK:			return "3A Lock";
1087 	case V4L2_CID_AUTO_FOCUS_START:		return "Auto Focus, Start";
1088 	case V4L2_CID_AUTO_FOCUS_STOP:		return "Auto Focus, Stop";
1089 	case V4L2_CID_AUTO_FOCUS_STATUS:	return "Auto Focus, Status";
1090 	case V4L2_CID_AUTO_FOCUS_RANGE:		return "Auto Focus, Range";
1091 	case V4L2_CID_PAN_SPEED:		return "Pan, Speed";
1092 	case V4L2_CID_TILT_SPEED:		return "Tilt, Speed";
1093 	case V4L2_CID_UNIT_CELL_SIZE:		return "Unit Cell Size";
1094 	case V4L2_CID_CAMERA_ORIENTATION:	return "Camera Orientation";
1095 	case V4L2_CID_CAMERA_SENSOR_ROTATION:	return "Camera Sensor Rotation";
1096 
1097 	/* FM Radio Modulator controls */
1098 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1099 	case V4L2_CID_FM_TX_CLASS:		return "FM Radio Modulator Controls";
1100 	case V4L2_CID_RDS_TX_DEVIATION:		return "RDS Signal Deviation";
1101 	case V4L2_CID_RDS_TX_PI:		return "RDS Program ID";
1102 	case V4L2_CID_RDS_TX_PTY:		return "RDS Program Type";
1103 	case V4L2_CID_RDS_TX_PS_NAME:		return "RDS PS Name";
1104 	case V4L2_CID_RDS_TX_RADIO_TEXT:	return "RDS Radio Text";
1105 	case V4L2_CID_RDS_TX_MONO_STEREO:	return "RDS Stereo";
1106 	case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:	return "RDS Artificial Head";
1107 	case V4L2_CID_RDS_TX_COMPRESSED:	return "RDS Compressed";
1108 	case V4L2_CID_RDS_TX_DYNAMIC_PTY:	return "RDS Dynamic PTY";
1109 	case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
1110 	case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:	return "RDS Traffic Program";
1111 	case V4L2_CID_RDS_TX_MUSIC_SPEECH:	return "RDS Music";
1112 	case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:	return "RDS Enable Alt Frequencies";
1113 	case V4L2_CID_RDS_TX_ALT_FREQS:		return "RDS Alternate Frequencies";
1114 	case V4L2_CID_AUDIO_LIMITER_ENABLED:	return "Audio Limiter Feature Enabled";
1115 	case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
1116 	case V4L2_CID_AUDIO_LIMITER_DEVIATION:	return "Audio Limiter Deviation";
1117 	case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
1118 	case V4L2_CID_AUDIO_COMPRESSION_GAIN:	return "Audio Compression Gain";
1119 	case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
1120 	case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
1121 	case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
1122 	case V4L2_CID_PILOT_TONE_ENABLED:	return "Pilot Tone Feature Enabled";
1123 	case V4L2_CID_PILOT_TONE_DEVIATION:	return "Pilot Tone Deviation";
1124 	case V4L2_CID_PILOT_TONE_FREQUENCY:	return "Pilot Tone Frequency";
1125 	case V4L2_CID_TUNE_PREEMPHASIS:		return "Pre-Emphasis";
1126 	case V4L2_CID_TUNE_POWER_LEVEL:		return "Tune Power Level";
1127 	case V4L2_CID_TUNE_ANTENNA_CAPACITOR:	return "Tune Antenna Capacitor";
1128 
1129 	/* Flash controls */
1130 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1131 	case V4L2_CID_FLASH_CLASS:		return "Flash Controls";
1132 	case V4L2_CID_FLASH_LED_MODE:		return "LED Mode";
1133 	case V4L2_CID_FLASH_STROBE_SOURCE:	return "Strobe Source";
1134 	case V4L2_CID_FLASH_STROBE:		return "Strobe";
1135 	case V4L2_CID_FLASH_STROBE_STOP:	return "Stop Strobe";
1136 	case V4L2_CID_FLASH_STROBE_STATUS:	return "Strobe Status";
1137 	case V4L2_CID_FLASH_TIMEOUT:		return "Strobe Timeout";
1138 	case V4L2_CID_FLASH_INTENSITY:		return "Intensity, Flash Mode";
1139 	case V4L2_CID_FLASH_TORCH_INTENSITY:	return "Intensity, Torch Mode";
1140 	case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
1141 	case V4L2_CID_FLASH_FAULT:		return "Faults";
1142 	case V4L2_CID_FLASH_CHARGE:		return "Charge";
1143 	case V4L2_CID_FLASH_READY:		return "Ready to Strobe";
1144 
1145 	/* JPEG encoder controls */
1146 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1147 	case V4L2_CID_JPEG_CLASS:		return "JPEG Compression Controls";
1148 	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:	return "Chroma Subsampling";
1149 	case V4L2_CID_JPEG_RESTART_INTERVAL:	return "Restart Interval";
1150 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:	return "Compression Quality";
1151 	case V4L2_CID_JPEG_ACTIVE_MARKER:	return "Active Markers";
1152 
1153 	/* Image source controls */
1154 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1155 	case V4L2_CID_IMAGE_SOURCE_CLASS:	return "Image Source Controls";
1156 	case V4L2_CID_VBLANK:			return "Vertical Blanking";
1157 	case V4L2_CID_HBLANK:			return "Horizontal Blanking";
1158 	case V4L2_CID_ANALOGUE_GAIN:		return "Analogue Gain";
1159 	case V4L2_CID_TEST_PATTERN_RED:		return "Red Pixel Value";
1160 	case V4L2_CID_TEST_PATTERN_GREENR:	return "Green (Red) Pixel Value";
1161 	case V4L2_CID_TEST_PATTERN_BLUE:	return "Blue Pixel Value";
1162 	case V4L2_CID_TEST_PATTERN_GREENB:	return "Green (Blue) Pixel Value";
1163 
1164 	/* Image processing controls */
1165 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1166 	case V4L2_CID_IMAGE_PROC_CLASS:		return "Image Processing Controls";
1167 	case V4L2_CID_LINK_FREQ:		return "Link Frequency";
1168 	case V4L2_CID_PIXEL_RATE:		return "Pixel Rate";
1169 	case V4L2_CID_TEST_PATTERN:		return "Test Pattern";
1170 	case V4L2_CID_DEINTERLACING_MODE:	return "Deinterlacing Mode";
1171 	case V4L2_CID_DIGITAL_GAIN:		return "Digital Gain";
1172 
1173 	/* DV controls */
1174 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1175 	case V4L2_CID_DV_CLASS:			return "Digital Video Controls";
1176 	case V4L2_CID_DV_TX_HOTPLUG:		return "Hotplug Present";
1177 	case V4L2_CID_DV_TX_RXSENSE:		return "RxSense Present";
1178 	case V4L2_CID_DV_TX_EDID_PRESENT:	return "EDID Present";
1179 	case V4L2_CID_DV_TX_MODE:		return "Transmit Mode";
1180 	case V4L2_CID_DV_TX_RGB_RANGE:		return "Tx RGB Quantization Range";
1181 	case V4L2_CID_DV_TX_IT_CONTENT_TYPE:	return "Tx IT Content Type";
1182 	case V4L2_CID_DV_RX_POWER_PRESENT:	return "Power Present";
1183 	case V4L2_CID_DV_RX_RGB_RANGE:		return "Rx RGB Quantization Range";
1184 	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:	return "Rx IT Content Type";
1185 
1186 	case V4L2_CID_FM_RX_CLASS:		return "FM Radio Receiver Controls";
1187 	case V4L2_CID_TUNE_DEEMPHASIS:		return "De-Emphasis";
1188 	case V4L2_CID_RDS_RECEPTION:		return "RDS Reception";
1189 	case V4L2_CID_RF_TUNER_CLASS:		return "RF Tuner Controls";
1190 	case V4L2_CID_RF_TUNER_RF_GAIN:		return "RF Gain";
1191 	case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:	return "LNA Gain, Auto";
1192 	case V4L2_CID_RF_TUNER_LNA_GAIN:	return "LNA Gain";
1193 	case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:	return "Mixer Gain, Auto";
1194 	case V4L2_CID_RF_TUNER_MIXER_GAIN:	return "Mixer Gain";
1195 	case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:	return "IF Gain, Auto";
1196 	case V4L2_CID_RF_TUNER_IF_GAIN:		return "IF Gain";
1197 	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:	return "Bandwidth, Auto";
1198 	case V4L2_CID_RF_TUNER_BANDWIDTH:	return "Bandwidth";
1199 	case V4L2_CID_RF_TUNER_PLL_LOCK:	return "PLL Lock";
1200 	case V4L2_CID_RDS_RX_PTY:		return "RDS Program Type";
1201 	case V4L2_CID_RDS_RX_PS_NAME:		return "RDS PS Name";
1202 	case V4L2_CID_RDS_RX_RADIO_TEXT:	return "RDS Radio Text";
1203 	case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
1204 	case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:	return "RDS Traffic Program";
1205 	case V4L2_CID_RDS_RX_MUSIC_SPEECH:	return "RDS Music";
1206 
1207 	/* Detection controls */
1208 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1209 	case V4L2_CID_DETECT_CLASS:		return "Detection Controls";
1210 	case V4L2_CID_DETECT_MD_MODE:		return "Motion Detection Mode";
1211 	case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD: return "MD Global Threshold";
1212 	case V4L2_CID_DETECT_MD_THRESHOLD_GRID:	return "MD Threshold Grid";
1213 	case V4L2_CID_DETECT_MD_REGION_GRID:	return "MD Region Grid";
1214 
1215 	/* Colorimetry controls */
1216 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1217 	case V4L2_CID_COLORIMETRY_CLASS:	return "Colorimetry Controls";
1218 	case V4L2_CID_COLORIMETRY_HDR10_CLL_INFO:		return "HDR10 Content Light Info";
1219 	case V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY:	return "HDR10 Mastering Display";
1220 	default:
1221 		return NULL;
1222 	}
1223 }
1224 EXPORT_SYMBOL(v4l2_ctrl_get_name);
1225 
v4l2_ctrl_fill(u32 id,const char ** name,enum v4l2_ctrl_type * type,s64 * min,s64 * max,u64 * step,s64 * def,u32 * flags)1226 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
1227 		    s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags)
1228 {
1229 	*name = v4l2_ctrl_get_name(id);
1230 	*flags = 0;
1231 
1232 	switch (id) {
1233 	case V4L2_CID_AUDIO_MUTE:
1234 	case V4L2_CID_AUDIO_LOUDNESS:
1235 	case V4L2_CID_AUTO_WHITE_BALANCE:
1236 	case V4L2_CID_AUTOGAIN:
1237 	case V4L2_CID_HFLIP:
1238 	case V4L2_CID_VFLIP:
1239 	case V4L2_CID_HUE_AUTO:
1240 	case V4L2_CID_CHROMA_AGC:
1241 	case V4L2_CID_COLOR_KILLER:
1242 	case V4L2_CID_AUTOBRIGHTNESS:
1243 	case V4L2_CID_MPEG_AUDIO_MUTE:
1244 	case V4L2_CID_MPEG_VIDEO_MUTE:
1245 	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1246 	case V4L2_CID_MPEG_VIDEO_PULLDOWN:
1247 	case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1248 	case V4L2_CID_FOCUS_AUTO:
1249 	case V4L2_CID_PRIVACY:
1250 	case V4L2_CID_AUDIO_LIMITER_ENABLED:
1251 	case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1252 	case V4L2_CID_PILOT_TONE_ENABLED:
1253 	case V4L2_CID_ILLUMINATORS_1:
1254 	case V4L2_CID_ILLUMINATORS_2:
1255 	case V4L2_CID_FLASH_STROBE_STATUS:
1256 	case V4L2_CID_FLASH_CHARGE:
1257 	case V4L2_CID_FLASH_READY:
1258 	case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
1259 	case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
1260 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
1261 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1262 	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1263 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1264 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1265 	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1266 	case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:
1267 	case V4L2_CID_MPEG_VIDEO_AU_DELIMITER:
1268 	case V4L2_CID_WIDE_DYNAMIC_RANGE:
1269 	case V4L2_CID_IMAGE_STABILIZATION:
1270 	case V4L2_CID_RDS_RECEPTION:
1271 	case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
1272 	case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
1273 	case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
1274 	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1275 	case V4L2_CID_RF_TUNER_PLL_LOCK:
1276 	case V4L2_CID_RDS_TX_MONO_STEREO:
1277 	case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
1278 	case V4L2_CID_RDS_TX_COMPRESSED:
1279 	case V4L2_CID_RDS_TX_DYNAMIC_PTY:
1280 	case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
1281 	case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
1282 	case V4L2_CID_RDS_TX_MUSIC_SPEECH:
1283 	case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
1284 	case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
1285 	case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
1286 	case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1287 		*type = V4L2_CTRL_TYPE_BOOLEAN;
1288 		*min = 0;
1289 		*max = *step = 1;
1290 		break;
1291 	case V4L2_CID_ROTATE:
1292 		*type = V4L2_CTRL_TYPE_INTEGER;
1293 		*flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1294 		break;
1295 	case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1296 	case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1297 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
1298 		*type = V4L2_CTRL_TYPE_INTEGER;
1299 		break;
1300 	case V4L2_CID_MPEG_VIDEO_LTR_COUNT:
1301 		*type = V4L2_CTRL_TYPE_INTEGER;
1302 		break;
1303 	case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:
1304 		*type = V4L2_CTRL_TYPE_INTEGER;
1305 		*flags |= V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1306 		break;
1307 	case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:
1308 		*type = V4L2_CTRL_TYPE_BITMASK;
1309 		*flags |= V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1310 		break;
1311 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1312 	case V4L2_CID_PAN_RESET:
1313 	case V4L2_CID_TILT_RESET:
1314 	case V4L2_CID_FLASH_STROBE:
1315 	case V4L2_CID_FLASH_STROBE_STOP:
1316 	case V4L2_CID_AUTO_FOCUS_START:
1317 	case V4L2_CID_AUTO_FOCUS_STOP:
1318 	case V4L2_CID_DO_WHITE_BALANCE:
1319 		*type = V4L2_CTRL_TYPE_BUTTON;
1320 		*flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1321 			  V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1322 		*min = *max = *step = *def = 0;
1323 		break;
1324 	case V4L2_CID_POWER_LINE_FREQUENCY:
1325 	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
1326 	case V4L2_CID_MPEG_AUDIO_ENCODING:
1327 	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
1328 	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
1329 	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
1330 	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
1331 	case V4L2_CID_MPEG_AUDIO_MODE:
1332 	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
1333 	case V4L2_CID_MPEG_AUDIO_EMPHASIS:
1334 	case V4L2_CID_MPEG_AUDIO_CRC:
1335 	case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
1336 	case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
1337 	case V4L2_CID_MPEG_VIDEO_ENCODING:
1338 	case V4L2_CID_MPEG_VIDEO_ASPECT:
1339 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1340 	case V4L2_CID_MPEG_STREAM_TYPE:
1341 	case V4L2_CID_MPEG_STREAM_VBI_FMT:
1342 	case V4L2_CID_EXPOSURE_AUTO:
1343 	case V4L2_CID_AUTO_FOCUS_RANGE:
1344 	case V4L2_CID_COLORFX:
1345 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1346 	case V4L2_CID_TUNE_PREEMPHASIS:
1347 	case V4L2_CID_FLASH_LED_MODE:
1348 	case V4L2_CID_FLASH_STROBE_SOURCE:
1349 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1350 	case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
1351 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1352 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1353 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1354 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1355 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1356 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1357 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
1358 	case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
1359 	case V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE:
1360 	case V4L2_CID_MPEG_VIDEO_H264_START_CODE:
1361 	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE:
1362 	case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
1363 	case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
1364 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1365 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1366 	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1367 	case V4L2_CID_ISO_SENSITIVITY_AUTO:
1368 	case V4L2_CID_EXPOSURE_METERING:
1369 	case V4L2_CID_SCENE_MODE:
1370 	case V4L2_CID_DV_TX_MODE:
1371 	case V4L2_CID_DV_TX_RGB_RANGE:
1372 	case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
1373 	case V4L2_CID_DV_RX_RGB_RANGE:
1374 	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1375 	case V4L2_CID_TEST_PATTERN:
1376 	case V4L2_CID_DEINTERLACING_MODE:
1377 	case V4L2_CID_TUNE_DEEMPHASIS:
1378 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
1379 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
1380 	case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:
1381 	case V4L2_CID_MPEG_VIDEO_VP9_LEVEL:
1382 	case V4L2_CID_DETECT_MD_MODE:
1383 	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
1384 	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
1385 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
1386 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
1387 	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
1388 	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
1389 	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
1390 	case V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE:
1391 	case V4L2_CID_MPEG_VIDEO_HEVC_START_CODE:
1392 	case V4L2_CID_CAMERA_ORIENTATION:
1393 		*type = V4L2_CTRL_TYPE_MENU;
1394 		break;
1395 	case V4L2_CID_LINK_FREQ:
1396 		*type = V4L2_CTRL_TYPE_INTEGER_MENU;
1397 		break;
1398 	case V4L2_CID_RDS_TX_PS_NAME:
1399 	case V4L2_CID_RDS_TX_RADIO_TEXT:
1400 	case V4L2_CID_RDS_RX_PS_NAME:
1401 	case V4L2_CID_RDS_RX_RADIO_TEXT:
1402 		*type = V4L2_CTRL_TYPE_STRING;
1403 		break;
1404 	case V4L2_CID_ISO_SENSITIVITY:
1405 	case V4L2_CID_AUTO_EXPOSURE_BIAS:
1406 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
1407 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
1408 		*type = V4L2_CTRL_TYPE_INTEGER_MENU;
1409 		break;
1410 	case V4L2_CID_USER_CLASS:
1411 	case V4L2_CID_CAMERA_CLASS:
1412 	case V4L2_CID_MPEG_CLASS:
1413 	case V4L2_CID_FM_TX_CLASS:
1414 	case V4L2_CID_FLASH_CLASS:
1415 	case V4L2_CID_JPEG_CLASS:
1416 	case V4L2_CID_IMAGE_SOURCE_CLASS:
1417 	case V4L2_CID_IMAGE_PROC_CLASS:
1418 	case V4L2_CID_DV_CLASS:
1419 	case V4L2_CID_FM_RX_CLASS:
1420 	case V4L2_CID_RF_TUNER_CLASS:
1421 	case V4L2_CID_DETECT_CLASS:
1422 	case V4L2_CID_COLORIMETRY_CLASS:
1423 		*type = V4L2_CTRL_TYPE_CTRL_CLASS;
1424 		/* You can neither read nor write these */
1425 		*flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
1426 		*min = *max = *step = *def = 0;
1427 		break;
1428 	case V4L2_CID_BG_COLOR:
1429 		*type = V4L2_CTRL_TYPE_INTEGER;
1430 		*step = 1;
1431 		*min = 0;
1432 		/* Max is calculated as RGB888 that is 2^24 */
1433 		*max = 0xFFFFFF;
1434 		break;
1435 	case V4L2_CID_FLASH_FAULT:
1436 	case V4L2_CID_JPEG_ACTIVE_MARKER:
1437 	case V4L2_CID_3A_LOCK:
1438 	case V4L2_CID_AUTO_FOCUS_STATUS:
1439 	case V4L2_CID_DV_TX_HOTPLUG:
1440 	case V4L2_CID_DV_TX_RXSENSE:
1441 	case V4L2_CID_DV_TX_EDID_PRESENT:
1442 	case V4L2_CID_DV_RX_POWER_PRESENT:
1443 		*type = V4L2_CTRL_TYPE_BITMASK;
1444 		break;
1445 	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1446 	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
1447 		*type = V4L2_CTRL_TYPE_INTEGER;
1448 		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
1449 		break;
1450 	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
1451 		*type = V4L2_CTRL_TYPE_INTEGER64;
1452 		*flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
1453 		*min = *def = 0;
1454 		*max = 0x1ffffffffLL;
1455 		*step = 1;
1456 		break;
1457 	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
1458 		*type = V4L2_CTRL_TYPE_INTEGER64;
1459 		*flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
1460 		*min = *def = 0;
1461 		*max = 0x7fffffffffffffffLL;
1462 		*step = 1;
1463 		break;
1464 	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
1465 		*type = V4L2_CTRL_TYPE_INTEGER64;
1466 		*min = 0;
1467 		/* default for 8 bit black, luma is 16, chroma is 128 */
1468 		*def = 0x8000800010LL;
1469 		*max = 0xffffffffffffLL;
1470 		*step = 1;
1471 		break;
1472 	case V4L2_CID_PIXEL_RATE:
1473 		*type = V4L2_CTRL_TYPE_INTEGER64;
1474 		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
1475 		break;
1476 	case V4L2_CID_DETECT_MD_REGION_GRID:
1477 		*type = V4L2_CTRL_TYPE_U8;
1478 		break;
1479 	case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
1480 		*type = V4L2_CTRL_TYPE_U16;
1481 		break;
1482 	case V4L2_CID_RDS_TX_ALT_FREQS:
1483 		*type = V4L2_CTRL_TYPE_U32;
1484 		break;
1485 	case V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS:
1486 		*type = V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS;
1487 		break;
1488 	case V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION:
1489 		*type = V4L2_CTRL_TYPE_MPEG2_QUANTIZATION;
1490 		break;
1491 	case V4L2_CID_MPEG_VIDEO_FWHT_PARAMS:
1492 		*type = V4L2_CTRL_TYPE_FWHT_PARAMS;
1493 		break;
1494 	case V4L2_CID_MPEG_VIDEO_H264_SPS:
1495 		*type = V4L2_CTRL_TYPE_H264_SPS;
1496 		break;
1497 	case V4L2_CID_MPEG_VIDEO_H264_PPS:
1498 		*type = V4L2_CTRL_TYPE_H264_PPS;
1499 		break;
1500 	case V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX:
1501 		*type = V4L2_CTRL_TYPE_H264_SCALING_MATRIX;
1502 		break;
1503 	case V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAMS:
1504 		*type = V4L2_CTRL_TYPE_H264_SLICE_PARAMS;
1505 		break;
1506 	case V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAMS:
1507 		*type = V4L2_CTRL_TYPE_H264_DECODE_PARAMS;
1508 		break;
1509 	case V4L2_CID_MPEG_VIDEO_H264_PRED_WEIGHTS:
1510 		*type = V4L2_CTRL_TYPE_H264_PRED_WEIGHTS;
1511 		break;
1512 	case V4L2_CID_MPEG_VIDEO_VP8_FRAME_HEADER:
1513 		*type = V4L2_CTRL_TYPE_VP8_FRAME_HEADER;
1514 		break;
1515 	case V4L2_CID_MPEG_VIDEO_HEVC_SPS:
1516 		*type = V4L2_CTRL_TYPE_HEVC_SPS;
1517 		break;
1518 	case V4L2_CID_MPEG_VIDEO_HEVC_PPS:
1519 		*type = V4L2_CTRL_TYPE_HEVC_PPS;
1520 		break;
1521 	case V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS:
1522 		*type = V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS;
1523 		break;
1524 	case V4L2_CID_UNIT_CELL_SIZE:
1525 		*type = V4L2_CTRL_TYPE_AREA;
1526 		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
1527 		break;
1528 	case V4L2_CID_COLORIMETRY_HDR10_CLL_INFO:
1529 		*type = V4L2_CTRL_TYPE_HDR10_CLL_INFO;
1530 		break;
1531 	case V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY:
1532 		*type = V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY;
1533 		break;
1534 	default:
1535 		*type = V4L2_CTRL_TYPE_INTEGER;
1536 		break;
1537 	}
1538 	switch (id) {
1539 	case V4L2_CID_MPEG_AUDIO_ENCODING:
1540 	case V4L2_CID_MPEG_AUDIO_MODE:
1541 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1542 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1543 	case V4L2_CID_MPEG_STREAM_TYPE:
1544 		*flags |= V4L2_CTRL_FLAG_UPDATE;
1545 		break;
1546 	case V4L2_CID_AUDIO_VOLUME:
1547 	case V4L2_CID_AUDIO_BALANCE:
1548 	case V4L2_CID_AUDIO_BASS:
1549 	case V4L2_CID_AUDIO_TREBLE:
1550 	case V4L2_CID_BRIGHTNESS:
1551 	case V4L2_CID_CONTRAST:
1552 	case V4L2_CID_SATURATION:
1553 	case V4L2_CID_HUE:
1554 	case V4L2_CID_RED_BALANCE:
1555 	case V4L2_CID_BLUE_BALANCE:
1556 	case V4L2_CID_GAMMA:
1557 	case V4L2_CID_SHARPNESS:
1558 	case V4L2_CID_CHROMA_GAIN:
1559 	case V4L2_CID_RDS_TX_DEVIATION:
1560 	case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1561 	case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1562 	case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1563 	case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1564 	case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1565 	case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1566 	case V4L2_CID_PILOT_TONE_DEVIATION:
1567 	case V4L2_CID_PILOT_TONE_FREQUENCY:
1568 	case V4L2_CID_TUNE_POWER_LEVEL:
1569 	case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1570 	case V4L2_CID_RF_TUNER_RF_GAIN:
1571 	case V4L2_CID_RF_TUNER_LNA_GAIN:
1572 	case V4L2_CID_RF_TUNER_MIXER_GAIN:
1573 	case V4L2_CID_RF_TUNER_IF_GAIN:
1574 	case V4L2_CID_RF_TUNER_BANDWIDTH:
1575 	case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
1576 		*flags |= V4L2_CTRL_FLAG_SLIDER;
1577 		break;
1578 	case V4L2_CID_PAN_RELATIVE:
1579 	case V4L2_CID_TILT_RELATIVE:
1580 	case V4L2_CID_FOCUS_RELATIVE:
1581 	case V4L2_CID_IRIS_RELATIVE:
1582 	case V4L2_CID_ZOOM_RELATIVE:
1583 		*flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1584 			  V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1585 		break;
1586 	case V4L2_CID_FLASH_STROBE_STATUS:
1587 	case V4L2_CID_AUTO_FOCUS_STATUS:
1588 	case V4L2_CID_FLASH_READY:
1589 	case V4L2_CID_DV_TX_HOTPLUG:
1590 	case V4L2_CID_DV_TX_RXSENSE:
1591 	case V4L2_CID_DV_TX_EDID_PRESENT:
1592 	case V4L2_CID_DV_RX_POWER_PRESENT:
1593 	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1594 	case V4L2_CID_RDS_RX_PTY:
1595 	case V4L2_CID_RDS_RX_PS_NAME:
1596 	case V4L2_CID_RDS_RX_RADIO_TEXT:
1597 	case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
1598 	case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
1599 	case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1600 	case V4L2_CID_CAMERA_ORIENTATION:
1601 	case V4L2_CID_CAMERA_SENSOR_ROTATION:
1602 		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
1603 		break;
1604 	case V4L2_CID_RF_TUNER_PLL_LOCK:
1605 		*flags |= V4L2_CTRL_FLAG_VOLATILE;
1606 		break;
1607 	}
1608 }
1609 EXPORT_SYMBOL(v4l2_ctrl_fill);
1610 
user_flags(const struct v4l2_ctrl * ctrl)1611 static u32 user_flags(const struct v4l2_ctrl *ctrl)
1612 {
1613 	u32 flags = ctrl->flags;
1614 
1615 	if (ctrl->is_ptr)
1616 		flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
1617 
1618 	return flags;
1619 }
1620 
fill_event(struct v4l2_event * ev,struct v4l2_ctrl * ctrl,u32 changes)1621 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
1622 {
1623 	memset(ev, 0, sizeof(*ev));
1624 	ev->type = V4L2_EVENT_CTRL;
1625 	ev->id = ctrl->id;
1626 	ev->u.ctrl.changes = changes;
1627 	ev->u.ctrl.type = ctrl->type;
1628 	ev->u.ctrl.flags = user_flags(ctrl);
1629 	if (ctrl->is_ptr)
1630 		ev->u.ctrl.value64 = 0;
1631 	else
1632 		ev->u.ctrl.value64 = *ctrl->p_cur.p_s64;
1633 	ev->u.ctrl.minimum = ctrl->minimum;
1634 	ev->u.ctrl.maximum = ctrl->maximum;
1635 	if (ctrl->type == V4L2_CTRL_TYPE_MENU
1636 	    || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1637 		ev->u.ctrl.step = 1;
1638 	else
1639 		ev->u.ctrl.step = ctrl->step;
1640 	ev->u.ctrl.default_value = ctrl->default_value;
1641 }
1642 
send_event(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,u32 changes)1643 static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
1644 {
1645 	struct v4l2_event ev;
1646 	struct v4l2_subscribed_event *sev;
1647 
1648 	if (list_empty(&ctrl->ev_subs))
1649 		return;
1650 	fill_event(&ev, ctrl, changes);
1651 
1652 	list_for_each_entry(sev, &ctrl->ev_subs, node)
1653 		if (sev->fh != fh ||
1654 		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
1655 			v4l2_event_queue_fh(sev->fh, &ev);
1656 }
1657 
std_equal(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr1,union v4l2_ctrl_ptr ptr2)1658 static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx,
1659 		      union v4l2_ctrl_ptr ptr1,
1660 		      union v4l2_ctrl_ptr ptr2)
1661 {
1662 	switch (ctrl->type) {
1663 	case V4L2_CTRL_TYPE_BUTTON:
1664 		return false;
1665 	case V4L2_CTRL_TYPE_STRING:
1666 		idx *= ctrl->elem_size;
1667 		/* strings are always 0-terminated */
1668 		return !strcmp(ptr1.p_char + idx, ptr2.p_char + idx);
1669 	case V4L2_CTRL_TYPE_INTEGER64:
1670 		return ptr1.p_s64[idx] == ptr2.p_s64[idx];
1671 	case V4L2_CTRL_TYPE_U8:
1672 		return ptr1.p_u8[idx] == ptr2.p_u8[idx];
1673 	case V4L2_CTRL_TYPE_U16:
1674 		return ptr1.p_u16[idx] == ptr2.p_u16[idx];
1675 	case V4L2_CTRL_TYPE_U32:
1676 		return ptr1.p_u32[idx] == ptr2.p_u32[idx];
1677 	default:
1678 		if (ctrl->is_int)
1679 			return ptr1.p_s32[idx] == ptr2.p_s32[idx];
1680 		idx *= ctrl->elem_size;
1681 		return !memcmp(ptr1.p_const + idx, ptr2.p_const + idx,
1682 			       ctrl->elem_size);
1683 	}
1684 }
1685 
std_init_compound(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)1686 static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx,
1687 			      union v4l2_ctrl_ptr ptr)
1688 {
1689 	struct v4l2_ctrl_mpeg2_slice_params *p_mpeg2_slice_params;
1690 	void *p = ptr.p + idx * ctrl->elem_size;
1691 
1692 	if (ctrl->p_def.p_const)
1693 		memcpy(p, ctrl->p_def.p_const, ctrl->elem_size);
1694 	else
1695 		memset(p, 0, ctrl->elem_size);
1696 
1697 	/*
1698 	 * The cast is needed to get rid of a gcc warning complaining that
1699 	 * V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS is not part of the
1700 	 * v4l2_ctrl_type enum.
1701 	 */
1702 	switch ((u32)ctrl->type) {
1703 	case V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS:
1704 		p_mpeg2_slice_params = p;
1705 		/* 4:2:0 */
1706 		p_mpeg2_slice_params->sequence.chroma_format = 1;
1707 		/* interlaced top field */
1708 		p_mpeg2_slice_params->picture.picture_structure = 1;
1709 		p_mpeg2_slice_params->picture.picture_coding_type =
1710 					V4L2_MPEG2_PICTURE_CODING_TYPE_I;
1711 		break;
1712 	}
1713 }
1714 
std_init(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)1715 static void std_init(const struct v4l2_ctrl *ctrl, u32 idx,
1716 		     union v4l2_ctrl_ptr ptr)
1717 {
1718 	switch (ctrl->type) {
1719 	case V4L2_CTRL_TYPE_STRING:
1720 		idx *= ctrl->elem_size;
1721 		memset(ptr.p_char + idx, ' ', ctrl->minimum);
1722 		ptr.p_char[idx + ctrl->minimum] = '\0';
1723 		break;
1724 	case V4L2_CTRL_TYPE_INTEGER64:
1725 		ptr.p_s64[idx] = ctrl->default_value;
1726 		break;
1727 	case V4L2_CTRL_TYPE_INTEGER:
1728 	case V4L2_CTRL_TYPE_INTEGER_MENU:
1729 	case V4L2_CTRL_TYPE_MENU:
1730 	case V4L2_CTRL_TYPE_BITMASK:
1731 	case V4L2_CTRL_TYPE_BOOLEAN:
1732 		ptr.p_s32[idx] = ctrl->default_value;
1733 		break;
1734 	case V4L2_CTRL_TYPE_BUTTON:
1735 	case V4L2_CTRL_TYPE_CTRL_CLASS:
1736 		ptr.p_s32[idx] = 0;
1737 		break;
1738 	case V4L2_CTRL_TYPE_U8:
1739 		ptr.p_u8[idx] = ctrl->default_value;
1740 		break;
1741 	case V4L2_CTRL_TYPE_U16:
1742 		ptr.p_u16[idx] = ctrl->default_value;
1743 		break;
1744 	case V4L2_CTRL_TYPE_U32:
1745 		ptr.p_u32[idx] = ctrl->default_value;
1746 		break;
1747 	default:
1748 		std_init_compound(ctrl, idx, ptr);
1749 		break;
1750 	}
1751 }
1752 
std_log(const struct v4l2_ctrl * ctrl)1753 static void std_log(const struct v4l2_ctrl *ctrl)
1754 {
1755 	union v4l2_ctrl_ptr ptr = ctrl->p_cur;
1756 
1757 	if (ctrl->is_array) {
1758 		unsigned i;
1759 
1760 		for (i = 0; i < ctrl->nr_of_dims; i++)
1761 			pr_cont("[%u]", ctrl->dims[i]);
1762 		pr_cont(" ");
1763 	}
1764 
1765 	switch (ctrl->type) {
1766 	case V4L2_CTRL_TYPE_INTEGER:
1767 		pr_cont("%d", *ptr.p_s32);
1768 		break;
1769 	case V4L2_CTRL_TYPE_BOOLEAN:
1770 		pr_cont("%s", *ptr.p_s32 ? "true" : "false");
1771 		break;
1772 	case V4L2_CTRL_TYPE_MENU:
1773 		pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
1774 		break;
1775 	case V4L2_CTRL_TYPE_INTEGER_MENU:
1776 		pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
1777 		break;
1778 	case V4L2_CTRL_TYPE_BITMASK:
1779 		pr_cont("0x%08x", *ptr.p_s32);
1780 		break;
1781 	case V4L2_CTRL_TYPE_INTEGER64:
1782 		pr_cont("%lld", *ptr.p_s64);
1783 		break;
1784 	case V4L2_CTRL_TYPE_STRING:
1785 		pr_cont("%s", ptr.p_char);
1786 		break;
1787 	case V4L2_CTRL_TYPE_U8:
1788 		pr_cont("%u", (unsigned)*ptr.p_u8);
1789 		break;
1790 	case V4L2_CTRL_TYPE_U16:
1791 		pr_cont("%u", (unsigned)*ptr.p_u16);
1792 		break;
1793 	case V4L2_CTRL_TYPE_U32:
1794 		pr_cont("%u", (unsigned)*ptr.p_u32);
1795 		break;
1796 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
1797 		pr_cont("HDR10_CLL_INFO");
1798 		break;
1799 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
1800 		pr_cont("HDR10_MASTERING_DISPLAY");
1801 		break;
1802 	default:
1803 		pr_cont("unknown type %d", ctrl->type);
1804 		break;
1805 	}
1806 }
1807 
1808 /*
1809  * Round towards the closest legal value. Be careful when we are
1810  * close to the maximum range of the control type to prevent
1811  * wrap-arounds.
1812  */
1813 #define ROUND_TO_RANGE(val, offset_type, ctrl)			\
1814 ({								\
1815 	offset_type offset;					\
1816 	if ((ctrl)->maximum >= 0 &&				\
1817 	    val >= (ctrl)->maximum - (s32)((ctrl)->step / 2))	\
1818 		val = (ctrl)->maximum;				\
1819 	else							\
1820 		val += (s32)((ctrl)->step / 2);			\
1821 	val = clamp_t(typeof(val), val,				\
1822 		      (ctrl)->minimum, (ctrl)->maximum);	\
1823 	offset = (val) - (ctrl)->minimum;			\
1824 	offset = (ctrl)->step * (offset / (u32)(ctrl)->step);	\
1825 	val = (ctrl)->minimum + offset;				\
1826 	0;							\
1827 })
1828 
1829 /* Validate a new control */
1830 
1831 #define zero_padding(s) \
1832 	memset(&(s).padding, 0, sizeof((s).padding))
1833 #define zero_reserved(s) \
1834 	memset(&(s).reserved, 0, sizeof((s).reserved))
1835 
1836 /*
1837  * Compound controls validation requires setting unused fields/flags to zero
1838  * in order to properly detect unchanged controls with std_equal's memcmp.
1839  */
std_validate_compound(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)1840 static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
1841 				 union v4l2_ctrl_ptr ptr)
1842 {
1843 	struct v4l2_ctrl_mpeg2_slice_params *p_mpeg2_slice_params;
1844 	struct v4l2_ctrl_vp8_frame_header *p_vp8_frame_header;
1845 	struct v4l2_ctrl_h264_slice_params *p_h264_slice_params;
1846 	struct v4l2_ctrl_h264_decode_params *p_h264_dec_params;
1847 	struct v4l2_ctrl_hevc_sps *p_hevc_sps;
1848 	struct v4l2_ctrl_hevc_pps *p_hevc_pps;
1849 	struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params;
1850 	struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering;
1851 	struct v4l2_area *area;
1852 	void *p = ptr.p + idx * ctrl->elem_size;
1853 	unsigned int i;
1854 
1855 	switch ((u32)ctrl->type) {
1856 	case V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS:
1857 		p_mpeg2_slice_params = p;
1858 
1859 		switch (p_mpeg2_slice_params->sequence.chroma_format) {
1860 		case 1: /* 4:2:0 */
1861 		case 2: /* 4:2:2 */
1862 		case 3: /* 4:4:4 */
1863 			break;
1864 		default:
1865 			return -EINVAL;
1866 		}
1867 
1868 		switch (p_mpeg2_slice_params->picture.intra_dc_precision) {
1869 		case 0: /* 8 bits */
1870 		case 1: /* 9 bits */
1871 		case 2: /* 10 bits */
1872 		case 3: /* 11 bits */
1873 			break;
1874 		default:
1875 			return -EINVAL;
1876 		}
1877 
1878 		switch (p_mpeg2_slice_params->picture.picture_structure) {
1879 		case 1: /* interlaced top field */
1880 		case 2: /* interlaced bottom field */
1881 		case 3: /* progressive */
1882 			break;
1883 		default:
1884 			return -EINVAL;
1885 		}
1886 
1887 		switch (p_mpeg2_slice_params->picture.picture_coding_type) {
1888 		case V4L2_MPEG2_PICTURE_CODING_TYPE_I:
1889 		case V4L2_MPEG2_PICTURE_CODING_TYPE_P:
1890 		case V4L2_MPEG2_PICTURE_CODING_TYPE_B:
1891 			break;
1892 		default:
1893 			return -EINVAL;
1894 		}
1895 
1896 		break;
1897 
1898 	case V4L2_CTRL_TYPE_MPEG2_QUANTIZATION:
1899 		break;
1900 
1901 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
1902 		break;
1903 
1904 	case V4L2_CTRL_TYPE_H264_SPS:
1905 	case V4L2_CTRL_TYPE_H264_PPS:
1906 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
1907 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
1908 		break;
1909 
1910 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
1911 		p_h264_slice_params = p;
1912 
1913 		zero_reserved(*p_h264_slice_params);
1914 		break;
1915 
1916 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
1917 		p_h264_dec_params = p;
1918 
1919 		for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
1920 			struct v4l2_h264_dpb_entry *dpb_entry =
1921 				&p_h264_dec_params->dpb[i];
1922 
1923 			zero_reserved(*dpb_entry);
1924 		}
1925 		zero_reserved(*p_h264_dec_params);
1926 		break;
1927 
1928 	case V4L2_CTRL_TYPE_VP8_FRAME_HEADER:
1929 		p_vp8_frame_header = p;
1930 
1931 		switch (p_vp8_frame_header->num_dct_parts) {
1932 		case 1:
1933 		case 2:
1934 		case 4:
1935 		case 8:
1936 			break;
1937 		default:
1938 			return -EINVAL;
1939 		}
1940 		zero_padding(p_vp8_frame_header->segment_header);
1941 		zero_padding(p_vp8_frame_header->lf_header);
1942 		zero_padding(p_vp8_frame_header->quant_header);
1943 		zero_padding(p_vp8_frame_header->entropy_header);
1944 		zero_padding(p_vp8_frame_header->coder_state);
1945 		break;
1946 
1947 	case V4L2_CTRL_TYPE_HEVC_SPS:
1948 		p_hevc_sps = p;
1949 
1950 		if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) {
1951 			p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0;
1952 			p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;
1953 			p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0;
1954 			p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0;
1955 		}
1956 
1957 		if (!(p_hevc_sps->flags &
1958 		      V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT))
1959 			p_hevc_sps->num_long_term_ref_pics_sps = 0;
1960 		break;
1961 
1962 	case V4L2_CTRL_TYPE_HEVC_PPS:
1963 		p_hevc_pps = p;
1964 
1965 		if (!(p_hevc_pps->flags &
1966 		      V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED))
1967 			p_hevc_pps->diff_cu_qp_delta_depth = 0;
1968 
1969 		if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
1970 			p_hevc_pps->num_tile_columns_minus1 = 0;
1971 			p_hevc_pps->num_tile_rows_minus1 = 0;
1972 			memset(&p_hevc_pps->column_width_minus1, 0,
1973 			       sizeof(p_hevc_pps->column_width_minus1));
1974 			memset(&p_hevc_pps->row_height_minus1, 0,
1975 			       sizeof(p_hevc_pps->row_height_minus1));
1976 
1977 			p_hevc_pps->flags &=
1978 				~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED;
1979 		}
1980 
1981 		if (p_hevc_pps->flags &
1982 		    V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) {
1983 			p_hevc_pps->pps_beta_offset_div2 = 0;
1984 			p_hevc_pps->pps_tc_offset_div2 = 0;
1985 		}
1986 
1987 		zero_padding(*p_hevc_pps);
1988 		break;
1989 
1990 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
1991 		p_hevc_slice_params = p;
1992 
1993 		if (p_hevc_slice_params->num_active_dpb_entries >
1994 		    V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
1995 			return -EINVAL;
1996 
1997 		zero_padding(p_hevc_slice_params->pred_weight_table);
1998 
1999 		for (i = 0; i < p_hevc_slice_params->num_active_dpb_entries;
2000 		     i++) {
2001 			struct v4l2_hevc_dpb_entry *dpb_entry =
2002 				&p_hevc_slice_params->dpb[i];
2003 
2004 			zero_padding(*dpb_entry);
2005 		}
2006 
2007 		zero_padding(*p_hevc_slice_params);
2008 		break;
2009 
2010 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
2011 		break;
2012 
2013 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
2014 		p_hdr10_mastering = p;
2015 
2016 		for (i = 0; i < 3; ++i) {
2017 			if (p_hdr10_mastering->display_primaries_x[i] <
2018 				V4L2_HDR10_MASTERING_PRIMARIES_X_LOW ||
2019 			    p_hdr10_mastering->display_primaries_x[i] >
2020 				V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH ||
2021 			    p_hdr10_mastering->display_primaries_y[i] <
2022 				V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW ||
2023 			    p_hdr10_mastering->display_primaries_y[i] >
2024 				V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH)
2025 				return -EINVAL;
2026 		}
2027 
2028 		if (p_hdr10_mastering->white_point_x <
2029 			V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW ||
2030 		    p_hdr10_mastering->white_point_x >
2031 			V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH ||
2032 		    p_hdr10_mastering->white_point_y <
2033 			V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW ||
2034 		    p_hdr10_mastering->white_point_y >
2035 			V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH)
2036 			return -EINVAL;
2037 
2038 		if (p_hdr10_mastering->max_display_mastering_luminance <
2039 			V4L2_HDR10_MASTERING_MAX_LUMA_LOW ||
2040 		    p_hdr10_mastering->max_display_mastering_luminance >
2041 			V4L2_HDR10_MASTERING_MAX_LUMA_HIGH ||
2042 		    p_hdr10_mastering->min_display_mastering_luminance <
2043 			V4L2_HDR10_MASTERING_MIN_LUMA_LOW ||
2044 		    p_hdr10_mastering->min_display_mastering_luminance >
2045 			V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
2046 			return -EINVAL;
2047 
2048 		/* The following restriction comes from ITU-T Rec. H.265 spec */
2049 		if (p_hdr10_mastering->max_display_mastering_luminance ==
2050 			V4L2_HDR10_MASTERING_MAX_LUMA_LOW &&
2051 		    p_hdr10_mastering->min_display_mastering_luminance ==
2052 			V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
2053 			return -EINVAL;
2054 
2055 		break;
2056 
2057 	case V4L2_CTRL_TYPE_AREA:
2058 		area = p;
2059 		if (!area->width || !area->height)
2060 			return -EINVAL;
2061 		break;
2062 
2063 	default:
2064 		return -EINVAL;
2065 	}
2066 
2067 	return 0;
2068 }
2069 
std_validate(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)2070 static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx,
2071 			union v4l2_ctrl_ptr ptr)
2072 {
2073 	size_t len;
2074 	u64 offset;
2075 	s64 val;
2076 
2077 	switch ((u32)ctrl->type) {
2078 	case V4L2_CTRL_TYPE_INTEGER:
2079 		return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
2080 	case V4L2_CTRL_TYPE_INTEGER64:
2081 		/*
2082 		 * We can't use the ROUND_TO_RANGE define here due to
2083 		 * the u64 divide that needs special care.
2084 		 */
2085 		val = ptr.p_s64[idx];
2086 		if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
2087 			val = ctrl->maximum;
2088 		else
2089 			val += (s64)(ctrl->step / 2);
2090 		val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
2091 		offset = val - ctrl->minimum;
2092 		do_div(offset, ctrl->step);
2093 		ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
2094 		return 0;
2095 	case V4L2_CTRL_TYPE_U8:
2096 		return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
2097 	case V4L2_CTRL_TYPE_U16:
2098 		return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
2099 	case V4L2_CTRL_TYPE_U32:
2100 		return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
2101 
2102 	case V4L2_CTRL_TYPE_BOOLEAN:
2103 		ptr.p_s32[idx] = !!ptr.p_s32[idx];
2104 		return 0;
2105 
2106 	case V4L2_CTRL_TYPE_MENU:
2107 	case V4L2_CTRL_TYPE_INTEGER_MENU:
2108 		if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
2109 			return -ERANGE;
2110 		if (ptr.p_s32[idx] < BITS_PER_LONG_LONG &&
2111 		    (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx])))
2112 			return -EINVAL;
2113 		if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
2114 		    ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
2115 			return -EINVAL;
2116 		return 0;
2117 
2118 	case V4L2_CTRL_TYPE_BITMASK:
2119 		ptr.p_s32[idx] &= ctrl->maximum;
2120 		return 0;
2121 
2122 	case V4L2_CTRL_TYPE_BUTTON:
2123 	case V4L2_CTRL_TYPE_CTRL_CLASS:
2124 		ptr.p_s32[idx] = 0;
2125 		return 0;
2126 
2127 	case V4L2_CTRL_TYPE_STRING:
2128 		idx *= ctrl->elem_size;
2129 		len = strlen(ptr.p_char + idx);
2130 		if (len < ctrl->minimum)
2131 			return -ERANGE;
2132 		if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
2133 			return -ERANGE;
2134 		return 0;
2135 
2136 	default:
2137 		return std_validate_compound(ctrl, idx, ptr);
2138 	}
2139 }
2140 
2141 static const struct v4l2_ctrl_type_ops std_type_ops = {
2142 	.equal = std_equal,
2143 	.init = std_init,
2144 	.log = std_log,
2145 	.validate = std_validate,
2146 };
2147 
2148 /* Helper function: copy the given control value back to the caller */
ptr_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr ptr)2149 static int ptr_to_user(struct v4l2_ext_control *c,
2150 		       struct v4l2_ctrl *ctrl,
2151 		       union v4l2_ctrl_ptr ptr)
2152 {
2153 	u32 len;
2154 
2155 	if (ctrl->is_ptr && !ctrl->is_string)
2156 		return copy_to_user(c->ptr, ptr.p_const, c->size) ?
2157 		       -EFAULT : 0;
2158 
2159 	switch (ctrl->type) {
2160 	case V4L2_CTRL_TYPE_STRING:
2161 		len = strlen(ptr.p_char);
2162 		if (c->size < len + 1) {
2163 			c->size = ctrl->elem_size;
2164 			return -ENOSPC;
2165 		}
2166 		return copy_to_user(c->string, ptr.p_char, len + 1) ?
2167 		       -EFAULT : 0;
2168 	case V4L2_CTRL_TYPE_INTEGER64:
2169 		c->value64 = *ptr.p_s64;
2170 		break;
2171 	default:
2172 		c->value = *ptr.p_s32;
2173 		break;
2174 	}
2175 	return 0;
2176 }
2177 
2178 /* Helper function: copy the current control value back to the caller */
cur_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2179 static int cur_to_user(struct v4l2_ext_control *c,
2180 		       struct v4l2_ctrl *ctrl)
2181 {
2182 	return ptr_to_user(c, ctrl, ctrl->p_cur);
2183 }
2184 
2185 /* Helper function: copy the new control value back to the caller */
new_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2186 static int new_to_user(struct v4l2_ext_control *c,
2187 		       struct v4l2_ctrl *ctrl)
2188 {
2189 	return ptr_to_user(c, ctrl, ctrl->p_new);
2190 }
2191 
2192 /* Helper function: copy the request value back to the caller */
req_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl_ref * ref)2193 static int req_to_user(struct v4l2_ext_control *c,
2194 		       struct v4l2_ctrl_ref *ref)
2195 {
2196 	return ptr_to_user(c, ref->ctrl, ref->p_req);
2197 }
2198 
2199 /* Helper function: copy the initial control value back to the caller */
def_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2200 static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
2201 {
2202 	int idx;
2203 
2204 	for (idx = 0; idx < ctrl->elems; idx++)
2205 		ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
2206 
2207 	return ptr_to_user(c, ctrl, ctrl->p_new);
2208 }
2209 
2210 /* Helper function: copy the caller-provider value to the given control value */
user_to_ptr(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr ptr)2211 static int user_to_ptr(struct v4l2_ext_control *c,
2212 		       struct v4l2_ctrl *ctrl,
2213 		       union v4l2_ctrl_ptr ptr)
2214 {
2215 	int ret;
2216 	u32 size;
2217 
2218 	ctrl->is_new = 1;
2219 	if (ctrl->is_ptr && !ctrl->is_string) {
2220 		unsigned idx;
2221 
2222 		ret = copy_from_user(ptr.p, c->ptr, c->size) ? -EFAULT : 0;
2223 		if (ret || !ctrl->is_array)
2224 			return ret;
2225 		for (idx = c->size / ctrl->elem_size; idx < ctrl->elems; idx++)
2226 			ctrl->type_ops->init(ctrl, idx, ptr);
2227 		return 0;
2228 	}
2229 
2230 	switch (ctrl->type) {
2231 	case V4L2_CTRL_TYPE_INTEGER64:
2232 		*ptr.p_s64 = c->value64;
2233 		break;
2234 	case V4L2_CTRL_TYPE_STRING:
2235 		size = c->size;
2236 		if (size == 0)
2237 			return -ERANGE;
2238 		if (size > ctrl->maximum + 1)
2239 			size = ctrl->maximum + 1;
2240 		ret = copy_from_user(ptr.p_char, c->string, size) ? -EFAULT : 0;
2241 		if (!ret) {
2242 			char last = ptr.p_char[size - 1];
2243 
2244 			ptr.p_char[size - 1] = 0;
2245 			/* If the string was longer than ctrl->maximum,
2246 			   then return an error. */
2247 			if (strlen(ptr.p_char) == ctrl->maximum && last)
2248 				return -ERANGE;
2249 		}
2250 		return ret;
2251 	default:
2252 		*ptr.p_s32 = c->value;
2253 		break;
2254 	}
2255 	return 0;
2256 }
2257 
2258 /* Helper function: copy the caller-provider value as the new control value */
user_to_new(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2259 static int user_to_new(struct v4l2_ext_control *c,
2260 		       struct v4l2_ctrl *ctrl)
2261 {
2262 	return user_to_ptr(c, ctrl, ctrl->p_new);
2263 }
2264 
2265 /* Copy the one value to another. */
ptr_to_ptr(struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr from,union v4l2_ctrl_ptr to)2266 static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
2267 		       union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to)
2268 {
2269 	if (ctrl == NULL)
2270 		return;
2271 	memcpy(to.p, from.p_const, ctrl->elems * ctrl->elem_size);
2272 }
2273 
2274 /* Copy the new value to the current value. */
new_to_cur(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,u32 ch_flags)2275 static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
2276 {
2277 	bool changed;
2278 
2279 	if (ctrl == NULL)
2280 		return;
2281 
2282 	/* has_changed is set by cluster_changed */
2283 	changed = ctrl->has_changed;
2284 	if (changed)
2285 		ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur);
2286 
2287 	if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
2288 		/* Note: CH_FLAGS is only set for auto clusters. */
2289 		ctrl->flags &=
2290 			~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
2291 		if (!is_cur_manual(ctrl->cluster[0])) {
2292 			ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
2293 			if (ctrl->cluster[0]->has_volatiles)
2294 				ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2295 		}
2296 		fh = NULL;
2297 	}
2298 	if (changed || ch_flags) {
2299 		/* If a control was changed that was not one of the controls
2300 		   modified by the application, then send the event to all. */
2301 		if (!ctrl->is_new)
2302 			fh = NULL;
2303 		send_event(fh, ctrl,
2304 			(changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
2305 		if (ctrl->call_notify && changed && ctrl->handler->notify)
2306 			ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
2307 	}
2308 }
2309 
2310 /* Copy the current value to the new value */
cur_to_new(struct v4l2_ctrl * ctrl)2311 static void cur_to_new(struct v4l2_ctrl *ctrl)
2312 {
2313 	if (ctrl == NULL)
2314 		return;
2315 	ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new);
2316 }
2317 
2318 /* Copy the new value to the request value */
new_to_req(struct v4l2_ctrl_ref * ref)2319 static void new_to_req(struct v4l2_ctrl_ref *ref)
2320 {
2321 	if (!ref)
2322 		return;
2323 	ptr_to_ptr(ref->ctrl, ref->ctrl->p_new, ref->p_req);
2324 	ref->valid_p_req = true;
2325 }
2326 
2327 /* Copy the current value to the request value */
cur_to_req(struct v4l2_ctrl_ref * ref)2328 static void cur_to_req(struct v4l2_ctrl_ref *ref)
2329 {
2330 	if (!ref)
2331 		return;
2332 	ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->p_req);
2333 	ref->valid_p_req = true;
2334 }
2335 
2336 /* Copy the request value to the new value */
req_to_new(struct v4l2_ctrl_ref * ref)2337 static void req_to_new(struct v4l2_ctrl_ref *ref)
2338 {
2339 	if (!ref)
2340 		return;
2341 	if (ref->valid_p_req)
2342 		ptr_to_ptr(ref->ctrl, ref->p_req, ref->ctrl->p_new);
2343 	else
2344 		ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->ctrl->p_new);
2345 }
2346 
2347 /* Return non-zero if one or more of the controls in the cluster has a new
2348    value that differs from the current value. */
cluster_changed(struct v4l2_ctrl * master)2349 static int cluster_changed(struct v4l2_ctrl *master)
2350 {
2351 	bool changed = false;
2352 	unsigned idx;
2353 	int i;
2354 
2355 	for (i = 0; i < master->ncontrols; i++) {
2356 		struct v4l2_ctrl *ctrl = master->cluster[i];
2357 		bool ctrl_changed = false;
2358 
2359 		if (ctrl == NULL)
2360 			continue;
2361 
2362 		if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE)
2363 			changed = ctrl_changed = true;
2364 
2365 		/*
2366 		 * Set has_changed to false to avoid generating
2367 		 * the event V4L2_EVENT_CTRL_CH_VALUE
2368 		 */
2369 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
2370 			ctrl->has_changed = false;
2371 			continue;
2372 		}
2373 
2374 		for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++)
2375 			ctrl_changed = !ctrl->type_ops->equal(ctrl, idx,
2376 				ctrl->p_cur, ctrl->p_new);
2377 		ctrl->has_changed = ctrl_changed;
2378 		changed |= ctrl->has_changed;
2379 	}
2380 	return changed;
2381 }
2382 
2383 /* Control range checking */
check_range(enum v4l2_ctrl_type type,s64 min,s64 max,u64 step,s64 def)2384 static int check_range(enum v4l2_ctrl_type type,
2385 		s64 min, s64 max, u64 step, s64 def)
2386 {
2387 	switch (type) {
2388 	case V4L2_CTRL_TYPE_BOOLEAN:
2389 		if (step != 1 || max > 1 || min < 0)
2390 			return -ERANGE;
2391 		fallthrough;
2392 	case V4L2_CTRL_TYPE_U8:
2393 	case V4L2_CTRL_TYPE_U16:
2394 	case V4L2_CTRL_TYPE_U32:
2395 	case V4L2_CTRL_TYPE_INTEGER:
2396 	case V4L2_CTRL_TYPE_INTEGER64:
2397 		if (step == 0 || min > max || def < min || def > max)
2398 			return -ERANGE;
2399 		return 0;
2400 	case V4L2_CTRL_TYPE_BITMASK:
2401 		if (step || min || !max || (def & ~max))
2402 			return -ERANGE;
2403 		return 0;
2404 	case V4L2_CTRL_TYPE_MENU:
2405 	case V4L2_CTRL_TYPE_INTEGER_MENU:
2406 		if (min > max || def < min || def > max)
2407 			return -ERANGE;
2408 		/* Note: step == menu_skip_mask for menu controls.
2409 		   So here we check if the default value is masked out. */
2410 		if (step && ((1 << def) & step))
2411 			return -EINVAL;
2412 		return 0;
2413 	case V4L2_CTRL_TYPE_STRING:
2414 		if (min > max || min < 0 || step < 1 || def)
2415 			return -ERANGE;
2416 		return 0;
2417 	default:
2418 		return 0;
2419 	}
2420 }
2421 
2422 /* Validate a new control */
validate_new(const struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr p_new)2423 static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
2424 {
2425 	unsigned idx;
2426 	int err = 0;
2427 
2428 	for (idx = 0; !err && idx < ctrl->elems; idx++)
2429 		err = ctrl->type_ops->validate(ctrl, idx, p_new);
2430 	return err;
2431 }
2432 
node2id(struct list_head * node)2433 static inline u32 node2id(struct list_head *node)
2434 {
2435 	return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
2436 }
2437 
2438 /* Set the handler's error code if it wasn't set earlier already */
handler_set_err(struct v4l2_ctrl_handler * hdl,int err)2439 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
2440 {
2441 	if (hdl->error == 0)
2442 		hdl->error = err;
2443 	return err;
2444 }
2445 
2446 /* Initialize the handler */
v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler * hdl,unsigned nr_of_controls_hint,struct lock_class_key * key,const char * name)2447 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
2448 				 unsigned nr_of_controls_hint,
2449 				 struct lock_class_key *key, const char *name)
2450 {
2451 	mutex_init(&hdl->_lock);
2452 	hdl->lock = &hdl->_lock;
2453 	lockdep_set_class_and_name(hdl->lock, key, name);
2454 	INIT_LIST_HEAD(&hdl->ctrls);
2455 	INIT_LIST_HEAD(&hdl->ctrl_refs);
2456 	INIT_LIST_HEAD(&hdl->requests);
2457 	INIT_LIST_HEAD(&hdl->requests_queued);
2458 	hdl->request_is_queued = false;
2459 	hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
2460 	hdl->buckets = kvmalloc_array(hdl->nr_of_buckets,
2461 				      sizeof(hdl->buckets[0]),
2462 				      GFP_KERNEL | __GFP_ZERO);
2463 	hdl->error = hdl->buckets ? 0 : -ENOMEM;
2464 	media_request_object_init(&hdl->req_obj);
2465 	return hdl->error;
2466 }
2467 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
2468 
2469 /* Free all controls and control refs */
v4l2_ctrl_handler_free(struct v4l2_ctrl_handler * hdl)2470 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
2471 {
2472 	struct v4l2_ctrl_ref *ref, *next_ref;
2473 	struct v4l2_ctrl *ctrl, *next_ctrl;
2474 	struct v4l2_subscribed_event *sev, *next_sev;
2475 
2476 	if (hdl == NULL || hdl->buckets == NULL)
2477 		return;
2478 
2479 	/*
2480 	 * If the main handler is freed and it is used by handler objects in
2481 	 * outstanding requests, then unbind and put those objects before
2482 	 * freeing the main handler.
2483 	 *
2484 	 * The main handler can be identified by having a NULL ops pointer in
2485 	 * the request object.
2486 	 */
2487 	if (!hdl->req_obj.ops && !list_empty(&hdl->requests)) {
2488 		struct v4l2_ctrl_handler *req, *next_req;
2489 
2490 		list_for_each_entry_safe(req, next_req, &hdl->requests, requests) {
2491 			media_request_object_unbind(&req->req_obj);
2492 			media_request_object_put(&req->req_obj);
2493 		}
2494 	}
2495 	mutex_lock(hdl->lock);
2496 	/* Free all nodes */
2497 	list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
2498 		list_del(&ref->node);
2499 		kfree(ref);
2500 	}
2501 	/* Free all controls owned by the handler */
2502 	list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
2503 		list_del(&ctrl->node);
2504 		list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
2505 			list_del(&sev->node);
2506 		kvfree(ctrl);
2507 	}
2508 	kvfree(hdl->buckets);
2509 	hdl->buckets = NULL;
2510 	hdl->cached = NULL;
2511 	hdl->error = 0;
2512 	mutex_unlock(hdl->lock);
2513 	mutex_destroy(&hdl->_lock);
2514 }
2515 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
2516 
2517 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
2518    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
2519    with applications that do not use the NEXT_CTRL flag.
2520 
2521    We just find the n-th private user control. It's O(N), but that should not
2522    be an issue in this particular case. */
find_private_ref(struct v4l2_ctrl_handler * hdl,u32 id)2523 static struct v4l2_ctrl_ref *find_private_ref(
2524 		struct v4l2_ctrl_handler *hdl, u32 id)
2525 {
2526 	struct v4l2_ctrl_ref *ref;
2527 
2528 	id -= V4L2_CID_PRIVATE_BASE;
2529 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2530 		/* Search for private user controls that are compatible with
2531 		   VIDIOC_G/S_CTRL. */
2532 		if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
2533 		    V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
2534 			if (!ref->ctrl->is_int)
2535 				continue;
2536 			if (id == 0)
2537 				return ref;
2538 			id--;
2539 		}
2540 	}
2541 	return NULL;
2542 }
2543 
2544 /* Find a control with the given ID. */
find_ref(struct v4l2_ctrl_handler * hdl,u32 id)2545 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
2546 {
2547 	struct v4l2_ctrl_ref *ref;
2548 	int bucket;
2549 
2550 	id &= V4L2_CTRL_ID_MASK;
2551 
2552 	/* Old-style private controls need special handling */
2553 	if (id >= V4L2_CID_PRIVATE_BASE)
2554 		return find_private_ref(hdl, id);
2555 	bucket = id % hdl->nr_of_buckets;
2556 
2557 	/* Simple optimization: cache the last control found */
2558 	if (hdl->cached && hdl->cached->ctrl->id == id)
2559 		return hdl->cached;
2560 
2561 	/* Not in cache, search the hash */
2562 	ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
2563 	while (ref && ref->ctrl->id != id)
2564 		ref = ref->next;
2565 
2566 	if (ref)
2567 		hdl->cached = ref; /* cache it! */
2568 	return ref;
2569 }
2570 
2571 /* Find a control with the given ID. Take the handler's lock first. */
find_ref_lock(struct v4l2_ctrl_handler * hdl,u32 id)2572 static struct v4l2_ctrl_ref *find_ref_lock(
2573 		struct v4l2_ctrl_handler *hdl, u32 id)
2574 {
2575 	struct v4l2_ctrl_ref *ref = NULL;
2576 
2577 	if (hdl) {
2578 		mutex_lock(hdl->lock);
2579 		ref = find_ref(hdl, id);
2580 		mutex_unlock(hdl->lock);
2581 	}
2582 	return ref;
2583 }
2584 
2585 /* Find a control with the given ID. */
v4l2_ctrl_find(struct v4l2_ctrl_handler * hdl,u32 id)2586 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
2587 {
2588 	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
2589 
2590 	return ref ? ref->ctrl : NULL;
2591 }
2592 EXPORT_SYMBOL(v4l2_ctrl_find);
2593 
2594 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
handler_new_ref(struct v4l2_ctrl_handler * hdl,struct v4l2_ctrl * ctrl,struct v4l2_ctrl_ref ** ctrl_ref,bool from_other_dev,bool allocate_req)2595 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
2596 			   struct v4l2_ctrl *ctrl,
2597 			   struct v4l2_ctrl_ref **ctrl_ref,
2598 			   bool from_other_dev, bool allocate_req)
2599 {
2600 	struct v4l2_ctrl_ref *ref;
2601 	struct v4l2_ctrl_ref *new_ref;
2602 	u32 id = ctrl->id;
2603 	u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
2604 	int bucket = id % hdl->nr_of_buckets;	/* which bucket to use */
2605 	unsigned int size_extra_req = 0;
2606 
2607 	if (ctrl_ref)
2608 		*ctrl_ref = NULL;
2609 
2610 	/*
2611 	 * Automatically add the control class if it is not yet present and
2612 	 * the new control is not a compound control.
2613 	 */
2614 	if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
2615 	    id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
2616 		if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
2617 			return hdl->error;
2618 
2619 	if (hdl->error)
2620 		return hdl->error;
2621 
2622 	if (allocate_req)
2623 		size_extra_req = ctrl->elems * ctrl->elem_size;
2624 	new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
2625 	if (!new_ref)
2626 		return handler_set_err(hdl, -ENOMEM);
2627 	new_ref->ctrl = ctrl;
2628 	new_ref->from_other_dev = from_other_dev;
2629 	if (size_extra_req)
2630 		new_ref->p_req.p = &new_ref[1];
2631 
2632 	INIT_LIST_HEAD(&new_ref->node);
2633 
2634 	mutex_lock(hdl->lock);
2635 
2636 	/* Add immediately at the end of the list if the list is empty, or if
2637 	   the last element in the list has a lower ID.
2638 	   This ensures that when elements are added in ascending order the
2639 	   insertion is an O(1) operation. */
2640 	if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
2641 		list_add_tail(&new_ref->node, &hdl->ctrl_refs);
2642 		goto insert_in_hash;
2643 	}
2644 
2645 	/* Find insert position in sorted list */
2646 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2647 		if (ref->ctrl->id < id)
2648 			continue;
2649 		/* Don't add duplicates */
2650 		if (ref->ctrl->id == id) {
2651 			kfree(new_ref);
2652 			goto unlock;
2653 		}
2654 		list_add(&new_ref->node, ref->node.prev);
2655 		break;
2656 	}
2657 
2658 insert_in_hash:
2659 	/* Insert the control node in the hash */
2660 	new_ref->next = hdl->buckets[bucket];
2661 	hdl->buckets[bucket] = new_ref;
2662 	if (ctrl_ref)
2663 		*ctrl_ref = new_ref;
2664 	if (ctrl->handler == hdl) {
2665 		/* By default each control starts in a cluster of its own.
2666 		 * new_ref->ctrl is basically a cluster array with one
2667 		 * element, so that's perfect to use as the cluster pointer.
2668 		 * But only do this for the handler that owns the control.
2669 		 */
2670 		ctrl->cluster = &new_ref->ctrl;
2671 		ctrl->ncontrols = 1;
2672 	}
2673 
2674 unlock:
2675 	mutex_unlock(hdl->lock);
2676 	return 0;
2677 }
2678 
2679 /* Add a new control */
v4l2_ctrl_new(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,const struct v4l2_ctrl_type_ops * type_ops,u32 id,const char * name,enum v4l2_ctrl_type type,s64 min,s64 max,u64 step,s64 def,const u32 dims[V4L2_CTRL_MAX_DIMS],u32 elem_size,u32 flags,const char * const * qmenu,const s64 * qmenu_int,const union v4l2_ctrl_ptr p_def,void * priv)2680 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
2681 			const struct v4l2_ctrl_ops *ops,
2682 			const struct v4l2_ctrl_type_ops *type_ops,
2683 			u32 id, const char *name, enum v4l2_ctrl_type type,
2684 			s64 min, s64 max, u64 step, s64 def,
2685 			const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
2686 			u32 flags, const char * const *qmenu,
2687 			const s64 *qmenu_int, const union v4l2_ctrl_ptr p_def,
2688 			void *priv)
2689 {
2690 	struct v4l2_ctrl *ctrl;
2691 	unsigned sz_extra;
2692 	unsigned nr_of_dims = 0;
2693 	unsigned elems = 1;
2694 	bool is_array;
2695 	unsigned tot_ctrl_size;
2696 	unsigned idx;
2697 	void *data;
2698 	int err;
2699 
2700 	if (hdl->error)
2701 		return NULL;
2702 
2703 	while (dims && dims[nr_of_dims]) {
2704 		elems *= dims[nr_of_dims];
2705 		nr_of_dims++;
2706 		if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
2707 			break;
2708 	}
2709 	is_array = nr_of_dims > 0;
2710 
2711 	/* Prefill elem_size for all types handled by std_type_ops */
2712 	switch ((u32)type) {
2713 	case V4L2_CTRL_TYPE_INTEGER64:
2714 		elem_size = sizeof(s64);
2715 		break;
2716 	case V4L2_CTRL_TYPE_STRING:
2717 		elem_size = max + 1;
2718 		break;
2719 	case V4L2_CTRL_TYPE_U8:
2720 		elem_size = sizeof(u8);
2721 		break;
2722 	case V4L2_CTRL_TYPE_U16:
2723 		elem_size = sizeof(u16);
2724 		break;
2725 	case V4L2_CTRL_TYPE_U32:
2726 		elem_size = sizeof(u32);
2727 		break;
2728 	case V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS:
2729 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_slice_params);
2730 		break;
2731 	case V4L2_CTRL_TYPE_MPEG2_QUANTIZATION:
2732 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantization);
2733 		break;
2734 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
2735 		elem_size = sizeof(struct v4l2_ctrl_fwht_params);
2736 		break;
2737 	case V4L2_CTRL_TYPE_H264_SPS:
2738 		elem_size = sizeof(struct v4l2_ctrl_h264_sps);
2739 		break;
2740 	case V4L2_CTRL_TYPE_H264_PPS:
2741 		elem_size = sizeof(struct v4l2_ctrl_h264_pps);
2742 		break;
2743 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
2744 		elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix);
2745 		break;
2746 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
2747 		elem_size = sizeof(struct v4l2_ctrl_h264_slice_params);
2748 		break;
2749 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
2750 		elem_size = sizeof(struct v4l2_ctrl_h264_decode_params);
2751 		break;
2752 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
2753 		elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights);
2754 		break;
2755 	case V4L2_CTRL_TYPE_VP8_FRAME_HEADER:
2756 		elem_size = sizeof(struct v4l2_ctrl_vp8_frame_header);
2757 		break;
2758 	case V4L2_CTRL_TYPE_HEVC_SPS:
2759 		elem_size = sizeof(struct v4l2_ctrl_hevc_sps);
2760 		break;
2761 	case V4L2_CTRL_TYPE_HEVC_PPS:
2762 		elem_size = sizeof(struct v4l2_ctrl_hevc_pps);
2763 		break;
2764 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
2765 		elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params);
2766 		break;
2767 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
2768 		elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info);
2769 		break;
2770 	case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
2771 		elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display);
2772 		break;
2773 	case V4L2_CTRL_TYPE_AREA:
2774 		elem_size = sizeof(struct v4l2_area);
2775 		break;
2776 	default:
2777 		if (type < V4L2_CTRL_COMPOUND_TYPES)
2778 			elem_size = sizeof(s32);
2779 		break;
2780 	}
2781 	tot_ctrl_size = elem_size * elems;
2782 
2783 	/* Sanity checks */
2784 	if (id == 0 || name == NULL || !elem_size ||
2785 	    id >= V4L2_CID_PRIVATE_BASE ||
2786 	    (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
2787 	    (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
2788 		handler_set_err(hdl, -ERANGE);
2789 		return NULL;
2790 	}
2791 	err = check_range(type, min, max, step, def);
2792 	if (err) {
2793 		handler_set_err(hdl, err);
2794 		return NULL;
2795 	}
2796 	if (is_array &&
2797 	    (type == V4L2_CTRL_TYPE_BUTTON ||
2798 	     type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
2799 		handler_set_err(hdl, -EINVAL);
2800 		return NULL;
2801 	}
2802 
2803 	sz_extra = 0;
2804 	if (type == V4L2_CTRL_TYPE_BUTTON)
2805 		flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
2806 			V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
2807 	else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
2808 		flags |= V4L2_CTRL_FLAG_READ_ONLY;
2809 	else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
2810 		 type == V4L2_CTRL_TYPE_STRING ||
2811 		 type >= V4L2_CTRL_COMPOUND_TYPES ||
2812 		 is_array)
2813 		sz_extra += 2 * tot_ctrl_size;
2814 
2815 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const)
2816 		sz_extra += elem_size;
2817 
2818 	ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
2819 	if (ctrl == NULL) {
2820 		handler_set_err(hdl, -ENOMEM);
2821 		return NULL;
2822 	}
2823 
2824 	INIT_LIST_HEAD(&ctrl->node);
2825 	INIT_LIST_HEAD(&ctrl->ev_subs);
2826 	ctrl->handler = hdl;
2827 	ctrl->ops = ops;
2828 	ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
2829 	ctrl->id = id;
2830 	ctrl->name = name;
2831 	ctrl->type = type;
2832 	ctrl->flags = flags;
2833 	ctrl->minimum = min;
2834 	ctrl->maximum = max;
2835 	ctrl->step = step;
2836 	ctrl->default_value = def;
2837 	ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
2838 	ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
2839 	ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
2840 	ctrl->is_array = is_array;
2841 	ctrl->elems = elems;
2842 	ctrl->nr_of_dims = nr_of_dims;
2843 	if (nr_of_dims)
2844 		memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
2845 	ctrl->elem_size = elem_size;
2846 	if (type == V4L2_CTRL_TYPE_MENU)
2847 		ctrl->qmenu = qmenu;
2848 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2849 		ctrl->qmenu_int = qmenu_int;
2850 	ctrl->priv = priv;
2851 	ctrl->cur.val = ctrl->val = def;
2852 	data = &ctrl[1];
2853 
2854 	if (!ctrl->is_int) {
2855 		ctrl->p_new.p = data;
2856 		ctrl->p_cur.p = data + tot_ctrl_size;
2857 	} else {
2858 		ctrl->p_new.p = &ctrl->val;
2859 		ctrl->p_cur.p = &ctrl->cur.val;
2860 	}
2861 
2862 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) {
2863 		ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size;
2864 		memcpy(ctrl->p_def.p, p_def.p_const, elem_size);
2865 	}
2866 
2867 	for (idx = 0; idx < elems; idx++) {
2868 		ctrl->type_ops->init(ctrl, idx, ctrl->p_cur);
2869 		ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
2870 	}
2871 
2872 	if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
2873 		kvfree(ctrl);
2874 		return NULL;
2875 	}
2876 	mutex_lock(hdl->lock);
2877 	list_add_tail(&ctrl->node, &hdl->ctrls);
2878 	mutex_unlock(hdl->lock);
2879 	return ctrl;
2880 }
2881 
v4l2_ctrl_new_custom(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_config * cfg,void * priv)2882 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
2883 			const struct v4l2_ctrl_config *cfg, void *priv)
2884 {
2885 	bool is_menu;
2886 	struct v4l2_ctrl *ctrl;
2887 	const char *name = cfg->name;
2888 	const char * const *qmenu = cfg->qmenu;
2889 	const s64 *qmenu_int = cfg->qmenu_int;
2890 	enum v4l2_ctrl_type type = cfg->type;
2891 	u32 flags = cfg->flags;
2892 	s64 min = cfg->min;
2893 	s64 max = cfg->max;
2894 	u64 step = cfg->step;
2895 	s64 def = cfg->def;
2896 
2897 	if (name == NULL)
2898 		v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
2899 								&def, &flags);
2900 
2901 	is_menu = (type == V4L2_CTRL_TYPE_MENU ||
2902 		   type == V4L2_CTRL_TYPE_INTEGER_MENU);
2903 	if (is_menu)
2904 		WARN_ON(step);
2905 	else
2906 		WARN_ON(cfg->menu_skip_mask);
2907 	if (type == V4L2_CTRL_TYPE_MENU && !qmenu) {
2908 		qmenu = v4l2_ctrl_get_menu(cfg->id);
2909 	} else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) {
2910 		handler_set_err(hdl, -EINVAL);
2911 		return NULL;
2912 	}
2913 
2914 	ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
2915 			type, min, max,
2916 			is_menu ? cfg->menu_skip_mask : step, def,
2917 			cfg->dims, cfg->elem_size,
2918 			flags, qmenu, qmenu_int, cfg->p_def, priv);
2919 	if (ctrl)
2920 		ctrl->is_private = cfg->is_private;
2921 	return ctrl;
2922 }
2923 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
2924 
2925 /* Helper function for standard non-menu controls */
v4l2_ctrl_new_std(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,s64 min,s64 max,u64 step,s64 def)2926 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
2927 			const struct v4l2_ctrl_ops *ops,
2928 			u32 id, s64 min, s64 max, u64 step, s64 def)
2929 {
2930 	const char *name;
2931 	enum v4l2_ctrl_type type;
2932 	u32 flags;
2933 
2934 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2935 	if (type == V4L2_CTRL_TYPE_MENU ||
2936 	    type == V4L2_CTRL_TYPE_INTEGER_MENU ||
2937 	    type >= V4L2_CTRL_COMPOUND_TYPES) {
2938 		handler_set_err(hdl, -EINVAL);
2939 		return NULL;
2940 	}
2941 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2942 			     min, max, step, def, NULL, 0,
2943 			     flags, NULL, NULL, ptr_null, NULL);
2944 }
2945 EXPORT_SYMBOL(v4l2_ctrl_new_std);
2946 
2947 /* Helper function for standard menu controls */
v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,u8 _max,u64 mask,u8 _def)2948 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
2949 			const struct v4l2_ctrl_ops *ops,
2950 			u32 id, u8 _max, u64 mask, u8 _def)
2951 {
2952 	const char * const *qmenu = NULL;
2953 	const s64 *qmenu_int = NULL;
2954 	unsigned int qmenu_int_len = 0;
2955 	const char *name;
2956 	enum v4l2_ctrl_type type;
2957 	s64 min;
2958 	s64 max = _max;
2959 	s64 def = _def;
2960 	u64 step;
2961 	u32 flags;
2962 
2963 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2964 
2965 	if (type == V4L2_CTRL_TYPE_MENU)
2966 		qmenu = v4l2_ctrl_get_menu(id);
2967 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2968 		qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
2969 
2970 	if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) {
2971 		handler_set_err(hdl, -EINVAL);
2972 		return NULL;
2973 	}
2974 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2975 			     0, max, mask, def, NULL, 0,
2976 			     flags, qmenu, qmenu_int, ptr_null, NULL);
2977 }
2978 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
2979 
2980 /* Helper function for standard menu controls with driver defined menu */
v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,u8 _max,u64 mask,u8 _def,const char * const * qmenu)2981 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
2982 			const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
2983 			u64 mask, u8 _def, const char * const *qmenu)
2984 {
2985 	enum v4l2_ctrl_type type;
2986 	const char *name;
2987 	u32 flags;
2988 	u64 step;
2989 	s64 min;
2990 	s64 max = _max;
2991 	s64 def = _def;
2992 
2993 	/* v4l2_ctrl_new_std_menu_items() should only be called for
2994 	 * standard controls without a standard menu.
2995 	 */
2996 	if (v4l2_ctrl_get_menu(id)) {
2997 		handler_set_err(hdl, -EINVAL);
2998 		return NULL;
2999 	}
3000 
3001 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3002 	if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
3003 		handler_set_err(hdl, -EINVAL);
3004 		return NULL;
3005 	}
3006 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3007 			     0, max, mask, def, NULL, 0,
3008 			     flags, qmenu, NULL, ptr_null, NULL);
3009 
3010 }
3011 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
3012 
3013 /* Helper function for standard compound controls */
v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,const union v4l2_ctrl_ptr p_def)3014 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
3015 				const struct v4l2_ctrl_ops *ops, u32 id,
3016 				const union v4l2_ctrl_ptr p_def)
3017 {
3018 	const char *name;
3019 	enum v4l2_ctrl_type type;
3020 	u32 flags;
3021 	s64 min, max, step, def;
3022 
3023 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3024 	if (type < V4L2_CTRL_COMPOUND_TYPES) {
3025 		handler_set_err(hdl, -EINVAL);
3026 		return NULL;
3027 	}
3028 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3029 			     min, max, step, def, NULL, 0,
3030 			     flags, NULL, NULL, p_def, NULL);
3031 }
3032 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound);
3033 
3034 /* Helper function for standard integer menu controls */
v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,u8 _max,u8 _def,const s64 * qmenu_int)3035 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
3036 			const struct v4l2_ctrl_ops *ops,
3037 			u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
3038 {
3039 	const char *name;
3040 	enum v4l2_ctrl_type type;
3041 	s64 min;
3042 	u64 step;
3043 	s64 max = _max;
3044 	s64 def = _def;
3045 	u32 flags;
3046 
3047 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3048 	if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
3049 		handler_set_err(hdl, -EINVAL);
3050 		return NULL;
3051 	}
3052 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3053 			     0, max, 0, def, NULL, 0,
3054 			     flags, NULL, qmenu_int, ptr_null, NULL);
3055 }
3056 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
3057 
3058 /* Add the controls from another handler to our own. */
v4l2_ctrl_add_handler(struct v4l2_ctrl_handler * hdl,struct v4l2_ctrl_handler * add,bool (* filter)(const struct v4l2_ctrl * ctrl),bool from_other_dev)3059 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
3060 			  struct v4l2_ctrl_handler *add,
3061 			  bool (*filter)(const struct v4l2_ctrl *ctrl),
3062 			  bool from_other_dev)
3063 {
3064 	struct v4l2_ctrl_ref *ref;
3065 	int ret = 0;
3066 
3067 	/* Do nothing if either handler is NULL or if they are the same */
3068 	if (!hdl || !add || hdl == add)
3069 		return 0;
3070 	if (hdl->error)
3071 		return hdl->error;
3072 	mutex_lock(add->lock);
3073 	list_for_each_entry(ref, &add->ctrl_refs, node) {
3074 		struct v4l2_ctrl *ctrl = ref->ctrl;
3075 
3076 		/* Skip handler-private controls. */
3077 		if (ctrl->is_private)
3078 			continue;
3079 		/* And control classes */
3080 		if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
3081 			continue;
3082 		/* Filter any unwanted controls */
3083 		if (filter && !filter(ctrl))
3084 			continue;
3085 		ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
3086 		if (ret)
3087 			break;
3088 	}
3089 	mutex_unlock(add->lock);
3090 	return ret;
3091 }
3092 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
3093 
v4l2_ctrl_radio_filter(const struct v4l2_ctrl * ctrl)3094 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
3095 {
3096 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
3097 		return true;
3098 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
3099 		return true;
3100 	switch (ctrl->id) {
3101 	case V4L2_CID_AUDIO_MUTE:
3102 	case V4L2_CID_AUDIO_VOLUME:
3103 	case V4L2_CID_AUDIO_BALANCE:
3104 	case V4L2_CID_AUDIO_BASS:
3105 	case V4L2_CID_AUDIO_TREBLE:
3106 	case V4L2_CID_AUDIO_LOUDNESS:
3107 		return true;
3108 	default:
3109 		break;
3110 	}
3111 	return false;
3112 }
3113 EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
3114 
3115 /* Cluster controls */
v4l2_ctrl_cluster(unsigned ncontrols,struct v4l2_ctrl ** controls)3116 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
3117 {
3118 	bool has_volatiles = false;
3119 	int i;
3120 
3121 	/* The first control is the master control and it must not be NULL */
3122 	if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
3123 		return;
3124 
3125 	for (i = 0; i < ncontrols; i++) {
3126 		if (controls[i]) {
3127 			controls[i]->cluster = controls;
3128 			controls[i]->ncontrols = ncontrols;
3129 			if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
3130 				has_volatiles = true;
3131 		}
3132 	}
3133 	controls[0]->has_volatiles = has_volatiles;
3134 }
3135 EXPORT_SYMBOL(v4l2_ctrl_cluster);
3136 
v4l2_ctrl_auto_cluster(unsigned ncontrols,struct v4l2_ctrl ** controls,u8 manual_val,bool set_volatile)3137 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
3138 			    u8 manual_val, bool set_volatile)
3139 {
3140 	struct v4l2_ctrl *master = controls[0];
3141 	u32 flag = 0;
3142 	int i;
3143 
3144 	v4l2_ctrl_cluster(ncontrols, controls);
3145 	WARN_ON(ncontrols <= 1);
3146 	WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
3147 	WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
3148 	master->is_auto = true;
3149 	master->has_volatiles = set_volatile;
3150 	master->manual_mode_value = manual_val;
3151 	master->flags |= V4L2_CTRL_FLAG_UPDATE;
3152 
3153 	if (!is_cur_manual(master))
3154 		flag = V4L2_CTRL_FLAG_INACTIVE |
3155 			(set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
3156 
3157 	for (i = 1; i < ncontrols; i++)
3158 		if (controls[i])
3159 			controls[i]->flags |= flag;
3160 }
3161 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
3162 
3163 /* Activate/deactivate a control. */
v4l2_ctrl_activate(struct v4l2_ctrl * ctrl,bool active)3164 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
3165 {
3166 	/* invert since the actual flag is called 'inactive' */
3167 	bool inactive = !active;
3168 	bool old;
3169 
3170 	if (ctrl == NULL)
3171 		return;
3172 
3173 	if (inactive)
3174 		/* set V4L2_CTRL_FLAG_INACTIVE */
3175 		old = test_and_set_bit(4, &ctrl->flags);
3176 	else
3177 		/* clear V4L2_CTRL_FLAG_INACTIVE */
3178 		old = test_and_clear_bit(4, &ctrl->flags);
3179 	if (old != inactive)
3180 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
3181 }
3182 EXPORT_SYMBOL(v4l2_ctrl_activate);
3183 
__v4l2_ctrl_grab(struct v4l2_ctrl * ctrl,bool grabbed)3184 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
3185 {
3186 	bool old;
3187 
3188 	if (ctrl == NULL)
3189 		return;
3190 
3191 	lockdep_assert_held(ctrl->handler->lock);
3192 
3193 	if (grabbed)
3194 		/* set V4L2_CTRL_FLAG_GRABBED */
3195 		old = test_and_set_bit(1, &ctrl->flags);
3196 	else
3197 		/* clear V4L2_CTRL_FLAG_GRABBED */
3198 		old = test_and_clear_bit(1, &ctrl->flags);
3199 	if (old != grabbed)
3200 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
3201 }
3202 EXPORT_SYMBOL(__v4l2_ctrl_grab);
3203 
3204 /* Log the control name and value */
log_ctrl(const struct v4l2_ctrl * ctrl,const char * prefix,const char * colon)3205 static void log_ctrl(const struct v4l2_ctrl *ctrl,
3206 		     const char *prefix, const char *colon)
3207 {
3208 	if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
3209 		return;
3210 	if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
3211 		return;
3212 
3213 	pr_info("%s%s%s: ", prefix, colon, ctrl->name);
3214 
3215 	ctrl->type_ops->log(ctrl);
3216 
3217 	if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
3218 			   V4L2_CTRL_FLAG_GRABBED |
3219 			   V4L2_CTRL_FLAG_VOLATILE)) {
3220 		if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
3221 			pr_cont(" inactive");
3222 		if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
3223 			pr_cont(" grabbed");
3224 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
3225 			pr_cont(" volatile");
3226 	}
3227 	pr_cont("\n");
3228 }
3229 
3230 /* Log all controls owned by the handler */
v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler * hdl,const char * prefix)3231 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
3232 				  const char *prefix)
3233 {
3234 	struct v4l2_ctrl *ctrl;
3235 	const char *colon = "";
3236 	int len;
3237 
3238 	if (hdl == NULL)
3239 		return;
3240 	if (prefix == NULL)
3241 		prefix = "";
3242 	len = strlen(prefix);
3243 	if (len && prefix[len - 1] != ' ')
3244 		colon = ": ";
3245 	mutex_lock(hdl->lock);
3246 	list_for_each_entry(ctrl, &hdl->ctrls, node)
3247 		if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
3248 			log_ctrl(ctrl, prefix, colon);
3249 	mutex_unlock(hdl->lock);
3250 }
3251 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
3252 
v4l2_ctrl_subdev_log_status(struct v4l2_subdev * sd)3253 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
3254 {
3255 	v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
3256 	return 0;
3257 }
3258 EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
3259 
3260 /* Call s_ctrl for all controls owned by the handler */
__v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler * hdl)3261 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
3262 {
3263 	struct v4l2_ctrl *ctrl;
3264 	int ret = 0;
3265 
3266 	if (hdl == NULL)
3267 		return 0;
3268 
3269 	lockdep_assert_held(hdl->lock);
3270 
3271 	list_for_each_entry(ctrl, &hdl->ctrls, node)
3272 		ctrl->done = false;
3273 
3274 	list_for_each_entry(ctrl, &hdl->ctrls, node) {
3275 		struct v4l2_ctrl *master = ctrl->cluster[0];
3276 		int i;
3277 
3278 		/* Skip if this control was already handled by a cluster. */
3279 		/* Skip button controls and read-only controls. */
3280 		if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
3281 		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
3282 			continue;
3283 
3284 		for (i = 0; i < master->ncontrols; i++) {
3285 			if (master->cluster[i]) {
3286 				cur_to_new(master->cluster[i]);
3287 				master->cluster[i]->is_new = 1;
3288 				master->cluster[i]->done = true;
3289 			}
3290 		}
3291 		ret = call_op(master, s_ctrl);
3292 		if (ret)
3293 			break;
3294 	}
3295 
3296 	return ret;
3297 }
3298 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
3299 
v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler * hdl)3300 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
3301 {
3302 	int ret;
3303 
3304 	if (hdl == NULL)
3305 		return 0;
3306 
3307 	mutex_lock(hdl->lock);
3308 	ret = __v4l2_ctrl_handler_setup(hdl);
3309 	mutex_unlock(hdl->lock);
3310 
3311 	return ret;
3312 }
3313 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
3314 
3315 /* Implement VIDIOC_QUERY_EXT_CTRL */
v4l2_query_ext_ctrl(struct v4l2_ctrl_handler * hdl,struct v4l2_query_ext_ctrl * qc)3316 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
3317 {
3318 	const unsigned next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
3319 	u32 id = qc->id & V4L2_CTRL_ID_MASK;
3320 	struct v4l2_ctrl_ref *ref;
3321 	struct v4l2_ctrl *ctrl;
3322 
3323 	if (hdl == NULL)
3324 		return -EINVAL;
3325 
3326 	mutex_lock(hdl->lock);
3327 
3328 	/* Try to find it */
3329 	ref = find_ref(hdl, id);
3330 
3331 	if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
3332 		bool is_compound;
3333 		/* Match any control that is not hidden */
3334 		unsigned mask = 1;
3335 		bool match = false;
3336 
3337 		if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
3338 			/* Match any hidden control */
3339 			match = true;
3340 		} else if ((qc->id & next_flags) == next_flags) {
3341 			/* Match any control, compound or not */
3342 			mask = 0;
3343 		}
3344 
3345 		/* Find the next control with ID > qc->id */
3346 
3347 		/* Did we reach the end of the control list? */
3348 		if (id >= node2id(hdl->ctrl_refs.prev)) {
3349 			ref = NULL; /* Yes, so there is no next control */
3350 		} else if (ref) {
3351 			/* We found a control with the given ID, so just get
3352 			   the next valid one in the list. */
3353 			list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
3354 				is_compound = ref->ctrl->is_array ||
3355 					ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
3356 				if (id < ref->ctrl->id &&
3357 				    (is_compound & mask) == match)
3358 					break;
3359 			}
3360 			if (&ref->node == &hdl->ctrl_refs)
3361 				ref = NULL;
3362 		} else {
3363 			/* No control with the given ID exists, so start
3364 			   searching for the next largest ID. We know there
3365 			   is one, otherwise the first 'if' above would have
3366 			   been true. */
3367 			list_for_each_entry(ref, &hdl->ctrl_refs, node) {
3368 				is_compound = ref->ctrl->is_array ||
3369 					ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
3370 				if (id < ref->ctrl->id &&
3371 				    (is_compound & mask) == match)
3372 					break;
3373 			}
3374 			if (&ref->node == &hdl->ctrl_refs)
3375 				ref = NULL;
3376 		}
3377 	}
3378 	mutex_unlock(hdl->lock);
3379 
3380 	if (!ref)
3381 		return -EINVAL;
3382 
3383 	ctrl = ref->ctrl;
3384 	memset(qc, 0, sizeof(*qc));
3385 	if (id >= V4L2_CID_PRIVATE_BASE)
3386 		qc->id = id;
3387 	else
3388 		qc->id = ctrl->id;
3389 	strscpy(qc->name, ctrl->name, sizeof(qc->name));
3390 	qc->flags = user_flags(ctrl);
3391 	qc->type = ctrl->type;
3392 	qc->elem_size = ctrl->elem_size;
3393 	qc->elems = ctrl->elems;
3394 	qc->nr_of_dims = ctrl->nr_of_dims;
3395 	memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
3396 	qc->minimum = ctrl->minimum;
3397 	qc->maximum = ctrl->maximum;
3398 	qc->default_value = ctrl->default_value;
3399 	if (ctrl->type == V4L2_CTRL_TYPE_MENU
3400 	    || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
3401 		qc->step = 1;
3402 	else
3403 		qc->step = ctrl->step;
3404 	return 0;
3405 }
3406 EXPORT_SYMBOL(v4l2_query_ext_ctrl);
3407 
3408 /* Implement VIDIOC_QUERYCTRL */
v4l2_queryctrl(struct v4l2_ctrl_handler * hdl,struct v4l2_queryctrl * qc)3409 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
3410 {
3411 	struct v4l2_query_ext_ctrl qec = { qc->id };
3412 	int rc;
3413 
3414 	rc = v4l2_query_ext_ctrl(hdl, &qec);
3415 	if (rc)
3416 		return rc;
3417 
3418 	qc->id = qec.id;
3419 	qc->type = qec.type;
3420 	qc->flags = qec.flags;
3421 	strscpy(qc->name, qec.name, sizeof(qc->name));
3422 	switch (qc->type) {
3423 	case V4L2_CTRL_TYPE_INTEGER:
3424 	case V4L2_CTRL_TYPE_BOOLEAN:
3425 	case V4L2_CTRL_TYPE_MENU:
3426 	case V4L2_CTRL_TYPE_INTEGER_MENU:
3427 	case V4L2_CTRL_TYPE_STRING:
3428 	case V4L2_CTRL_TYPE_BITMASK:
3429 		qc->minimum = qec.minimum;
3430 		qc->maximum = qec.maximum;
3431 		qc->step = qec.step;
3432 		qc->default_value = qec.default_value;
3433 		break;
3434 	default:
3435 		qc->minimum = 0;
3436 		qc->maximum = 0;
3437 		qc->step = 0;
3438 		qc->default_value = 0;
3439 		break;
3440 	}
3441 	return 0;
3442 }
3443 EXPORT_SYMBOL(v4l2_queryctrl);
3444 
3445 /* Implement VIDIOC_QUERYMENU */
v4l2_querymenu(struct v4l2_ctrl_handler * hdl,struct v4l2_querymenu * qm)3446 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
3447 {
3448 	struct v4l2_ctrl *ctrl;
3449 	u32 i = qm->index;
3450 
3451 	ctrl = v4l2_ctrl_find(hdl, qm->id);
3452 	if (!ctrl)
3453 		return -EINVAL;
3454 
3455 	qm->reserved = 0;
3456 	/* Sanity checks */
3457 	switch (ctrl->type) {
3458 	case V4L2_CTRL_TYPE_MENU:
3459 		if (ctrl->qmenu == NULL)
3460 			return -EINVAL;
3461 		break;
3462 	case V4L2_CTRL_TYPE_INTEGER_MENU:
3463 		if (ctrl->qmenu_int == NULL)
3464 			return -EINVAL;
3465 		break;
3466 	default:
3467 		return -EINVAL;
3468 	}
3469 
3470 	if (i < ctrl->minimum || i > ctrl->maximum)
3471 		return -EINVAL;
3472 
3473 	/* Use mask to see if this menu item should be skipped */
3474 	if (ctrl->menu_skip_mask & (1ULL << i))
3475 		return -EINVAL;
3476 	/* Empty menu items should also be skipped */
3477 	if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
3478 		if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
3479 			return -EINVAL;
3480 		strscpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
3481 	} else {
3482 		qm->value = ctrl->qmenu_int[i];
3483 	}
3484 	return 0;
3485 }
3486 EXPORT_SYMBOL(v4l2_querymenu);
3487 
v4l2_ctrl_request_clone(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_handler * from)3488 static int v4l2_ctrl_request_clone(struct v4l2_ctrl_handler *hdl,
3489 				   const struct v4l2_ctrl_handler *from)
3490 {
3491 	struct v4l2_ctrl_ref *ref;
3492 	int err = 0;
3493 
3494 	if (WARN_ON(!hdl || hdl == from))
3495 		return -EINVAL;
3496 
3497 	if (hdl->error)
3498 		return hdl->error;
3499 
3500 	WARN_ON(hdl->lock != &hdl->_lock);
3501 
3502 	mutex_lock(from->lock);
3503 	list_for_each_entry(ref, &from->ctrl_refs, node) {
3504 		struct v4l2_ctrl *ctrl = ref->ctrl;
3505 		struct v4l2_ctrl_ref *new_ref;
3506 
3507 		/* Skip refs inherited from other devices */
3508 		if (ref->from_other_dev)
3509 			continue;
3510 		/* And buttons */
3511 		if (ctrl->type == V4L2_CTRL_TYPE_BUTTON)
3512 			continue;
3513 		err = handler_new_ref(hdl, ctrl, &new_ref, false, true);
3514 		if (err)
3515 			break;
3516 	}
3517 	mutex_unlock(from->lock);
3518 	return err;
3519 }
3520 
v4l2_ctrl_request_queue(struct media_request_object * obj)3521 static void v4l2_ctrl_request_queue(struct media_request_object *obj)
3522 {
3523 	struct v4l2_ctrl_handler *hdl =
3524 		container_of(obj, struct v4l2_ctrl_handler, req_obj);
3525 	struct v4l2_ctrl_handler *main_hdl = obj->priv;
3526 
3527 	mutex_lock(main_hdl->lock);
3528 	list_add_tail(&hdl->requests_queued, &main_hdl->requests_queued);
3529 	hdl->request_is_queued = true;
3530 	mutex_unlock(main_hdl->lock);
3531 }
3532 
v4l2_ctrl_request_unbind(struct media_request_object * obj)3533 static void v4l2_ctrl_request_unbind(struct media_request_object *obj)
3534 {
3535 	struct v4l2_ctrl_handler *hdl =
3536 		container_of(obj, struct v4l2_ctrl_handler, req_obj);
3537 	struct v4l2_ctrl_handler *main_hdl = obj->priv;
3538 
3539 	mutex_lock(main_hdl->lock);
3540 	list_del_init(&hdl->requests);
3541 	if (hdl->request_is_queued) {
3542 		list_del_init(&hdl->requests_queued);
3543 		hdl->request_is_queued = false;
3544 	}
3545 	mutex_unlock(main_hdl->lock);
3546 }
3547 
v4l2_ctrl_request_release(struct media_request_object * obj)3548 static void v4l2_ctrl_request_release(struct media_request_object *obj)
3549 {
3550 	struct v4l2_ctrl_handler *hdl =
3551 		container_of(obj, struct v4l2_ctrl_handler, req_obj);
3552 
3553 	v4l2_ctrl_handler_free(hdl);
3554 	kfree(hdl);
3555 }
3556 
3557 static const struct media_request_object_ops req_ops = {
3558 	.queue = v4l2_ctrl_request_queue,
3559 	.unbind = v4l2_ctrl_request_unbind,
3560 	.release = v4l2_ctrl_request_release,
3561 };
3562 
v4l2_ctrl_request_hdl_find(struct media_request * req,struct v4l2_ctrl_handler * parent)3563 struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(struct media_request *req,
3564 					struct v4l2_ctrl_handler *parent)
3565 {
3566 	struct media_request_object *obj;
3567 
3568 	if (WARN_ON(req->state != MEDIA_REQUEST_STATE_VALIDATING &&
3569 		    req->state != MEDIA_REQUEST_STATE_QUEUED))
3570 		return NULL;
3571 
3572 	obj = media_request_object_find(req, &req_ops, parent);
3573 	if (obj)
3574 		return container_of(obj, struct v4l2_ctrl_handler, req_obj);
3575 	return NULL;
3576 }
3577 EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_find);
3578 
3579 struct v4l2_ctrl *
v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler * hdl,u32 id)3580 v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
3581 {
3582 	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
3583 
3584 	return (ref && ref->valid_p_req) ? ref->ctrl : NULL;
3585 }
3586 EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_ctrl_find);
3587 
v4l2_ctrl_request_bind(struct media_request * req,struct v4l2_ctrl_handler * hdl,struct v4l2_ctrl_handler * from)3588 static int v4l2_ctrl_request_bind(struct media_request *req,
3589 			   struct v4l2_ctrl_handler *hdl,
3590 			   struct v4l2_ctrl_handler *from)
3591 {
3592 	int ret;
3593 
3594 	ret = v4l2_ctrl_request_clone(hdl, from);
3595 
3596 	if (!ret) {
3597 		ret = media_request_object_bind(req, &req_ops,
3598 						from, false, &hdl->req_obj);
3599 		if (!ret) {
3600 			mutex_lock(from->lock);
3601 			list_add_tail(&hdl->requests, &from->requests);
3602 			mutex_unlock(from->lock);
3603 		}
3604 	}
3605 	return ret;
3606 }
3607 
3608 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
3609 
3610    It is not a fully atomic operation, just best-effort only. After all, if
3611    multiple controls have to be set through multiple i2c writes (for example)
3612    then some initial writes may succeed while others fail. Thus leaving the
3613    system in an inconsistent state. The question is how much effort you are
3614    willing to spend on trying to make something atomic that really isn't.
3615 
3616    From the point of view of an application the main requirement is that
3617    when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
3618    error should be returned without actually affecting any controls.
3619 
3620    If all the values are correct, then it is acceptable to just give up
3621    in case of low-level errors.
3622 
3623    It is important though that the application can tell when only a partial
3624    configuration was done. The way we do that is through the error_idx field
3625    of struct v4l2_ext_controls: if that is equal to the count field then no
3626    controls were affected. Otherwise all controls before that index were
3627    successful in performing their 'get' or 'set' operation, the control at
3628    the given index failed, and you don't know what happened with the controls
3629    after the failed one. Since if they were part of a control cluster they
3630    could have been successfully processed (if a cluster member was encountered
3631    at index < error_idx), they could have failed (if a cluster member was at
3632    error_idx), or they may not have been processed yet (if the first cluster
3633    member appeared after error_idx).
3634 
3635    It is all fairly theoretical, though. In practice all you can do is to
3636    bail out. If error_idx == count, then it is an application bug. If
3637    error_idx < count then it is only an application bug if the error code was
3638    EBUSY. That usually means that something started streaming just when you
3639    tried to set the controls. In all other cases it is a driver/hardware
3640    problem and all you can do is to retry or bail out.
3641 
3642    Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
3643    never modifies controls the error_idx is just set to whatever control
3644    has an invalid value.
3645  */
3646 
3647 /* Prepare for the extended g/s/try functions.
3648    Find the controls in the control array and do some basic checks. */
prepare_ext_ctrls(struct v4l2_ctrl_handler * hdl,struct v4l2_ext_controls * cs,struct v4l2_ctrl_helper * helpers,struct video_device * vdev,bool get)3649 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
3650 			     struct v4l2_ext_controls *cs,
3651 			     struct v4l2_ctrl_helper *helpers,
3652 			     struct video_device *vdev,
3653 			     bool get)
3654 {
3655 	struct v4l2_ctrl_helper *h;
3656 	bool have_clusters = false;
3657 	u32 i;
3658 
3659 	for (i = 0, h = helpers; i < cs->count; i++, h++) {
3660 		struct v4l2_ext_control *c = &cs->controls[i];
3661 		struct v4l2_ctrl_ref *ref;
3662 		struct v4l2_ctrl *ctrl;
3663 		u32 id = c->id & V4L2_CTRL_ID_MASK;
3664 
3665 		cs->error_idx = i;
3666 
3667 		if (cs->which &&
3668 		    cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
3669 		    cs->which != V4L2_CTRL_WHICH_REQUEST_VAL &&
3670 		    V4L2_CTRL_ID2WHICH(id) != cs->which) {
3671 			dprintk(vdev,
3672 				"invalid which 0x%x or control id 0x%x\n",
3673 				cs->which, id);
3674 			return -EINVAL;
3675 		}
3676 
3677 		/* Old-style private controls are not allowed for
3678 		   extended controls */
3679 		if (id >= V4L2_CID_PRIVATE_BASE) {
3680 			dprintk(vdev,
3681 				"old-style private controls not allowed\n");
3682 			return -EINVAL;
3683 		}
3684 		ref = find_ref_lock(hdl, id);
3685 		if (ref == NULL) {
3686 			dprintk(vdev, "cannot find control id 0x%x\n", id);
3687 			return -EINVAL;
3688 		}
3689 		h->ref = ref;
3690 		ctrl = ref->ctrl;
3691 		if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED) {
3692 			dprintk(vdev, "control id 0x%x is disabled\n", id);
3693 			return -EINVAL;
3694 		}
3695 
3696 		if (ctrl->cluster[0]->ncontrols > 1)
3697 			have_clusters = true;
3698 		if (ctrl->cluster[0] != ctrl)
3699 			ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
3700 		if (ctrl->is_ptr && !ctrl->is_string) {
3701 			unsigned tot_size = ctrl->elems * ctrl->elem_size;
3702 
3703 			if (c->size < tot_size) {
3704 				/*
3705 				 * In the get case the application first
3706 				 * queries to obtain the size of the control.
3707 				 */
3708 				if (get) {
3709 					c->size = tot_size;
3710 					return -ENOSPC;
3711 				}
3712 				dprintk(vdev,
3713 					"pointer control id 0x%x size too small, %d bytes but %d bytes needed\n",
3714 					id, c->size, tot_size);
3715 				return -EFAULT;
3716 			}
3717 			c->size = tot_size;
3718 		}
3719 		/* Store the ref to the master control of the cluster */
3720 		h->mref = ref;
3721 		/* Initially set next to 0, meaning that there is no other
3722 		   control in this helper array belonging to the same
3723 		   cluster */
3724 		h->next = 0;
3725 	}
3726 
3727 	/* We are done if there were no controls that belong to a multi-
3728 	   control cluster. */
3729 	if (!have_clusters)
3730 		return 0;
3731 
3732 	/* The code below figures out in O(n) time which controls in the list
3733 	   belong to the same cluster. */
3734 
3735 	/* This has to be done with the handler lock taken. */
3736 	mutex_lock(hdl->lock);
3737 
3738 	/* First zero the helper field in the master control references */
3739 	for (i = 0; i < cs->count; i++)
3740 		helpers[i].mref->helper = NULL;
3741 	for (i = 0, h = helpers; i < cs->count; i++, h++) {
3742 		struct v4l2_ctrl_ref *mref = h->mref;
3743 
3744 		/* If the mref->helper is set, then it points to an earlier
3745 		   helper that belongs to the same cluster. */
3746 		if (mref->helper) {
3747 			/* Set the next field of mref->helper to the current
3748 			   index: this means that that earlier helper now
3749 			   points to the next helper in the same cluster. */
3750 			mref->helper->next = i;
3751 			/* mref should be set only for the first helper in the
3752 			   cluster, clear the others. */
3753 			h->mref = NULL;
3754 		}
3755 		/* Point the mref helper to the current helper struct. */
3756 		mref->helper = h;
3757 	}
3758 	mutex_unlock(hdl->lock);
3759 	return 0;
3760 }
3761 
3762 /* Handles the corner case where cs->count == 0. It checks whether the
3763    specified control class exists. If that class ID is 0, then it checks
3764    whether there are any controls at all. */
class_check(struct v4l2_ctrl_handler * hdl,u32 which)3765 static int class_check(struct v4l2_ctrl_handler *hdl, u32 which)
3766 {
3767 	if (which == 0 || which == V4L2_CTRL_WHICH_DEF_VAL ||
3768 	    which == V4L2_CTRL_WHICH_REQUEST_VAL)
3769 		return 0;
3770 	return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL;
3771 }
3772 
3773 /*
3774  * Get extended controls. Allocates the helpers array if needed.
3775  *
3776  * Note that v4l2_g_ext_ctrls_common() with 'which' set to
3777  * V4L2_CTRL_WHICH_REQUEST_VAL is only called if the request was
3778  * completed, and in that case valid_p_req is true for all controls.
3779  */
v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler * hdl,struct v4l2_ext_controls * cs,struct video_device * vdev)3780 static int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl,
3781 				   struct v4l2_ext_controls *cs,
3782 				   struct video_device *vdev)
3783 {
3784 	struct v4l2_ctrl_helper helper[4];
3785 	struct v4l2_ctrl_helper *helpers = helper;
3786 	int ret;
3787 	int i, j;
3788 	bool is_default, is_request;
3789 
3790 	is_default = (cs->which == V4L2_CTRL_WHICH_DEF_VAL);
3791 	is_request = (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL);
3792 
3793 	cs->error_idx = cs->count;
3794 	cs->which = V4L2_CTRL_ID2WHICH(cs->which);
3795 
3796 	if (hdl == NULL)
3797 		return -EINVAL;
3798 
3799 	if (cs->count == 0)
3800 		return class_check(hdl, cs->which);
3801 
3802 	if (cs->count > ARRAY_SIZE(helper)) {
3803 		helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
3804 					 GFP_KERNEL);
3805 		if (helpers == NULL)
3806 			return -ENOMEM;
3807 	}
3808 
3809 	ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, true);
3810 	cs->error_idx = cs->count;
3811 
3812 	for (i = 0; !ret && i < cs->count; i++)
3813 		if (helpers[i].ref->ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
3814 			ret = -EACCES;
3815 
3816 	for (i = 0; !ret && i < cs->count; i++) {
3817 		struct v4l2_ctrl *master;
3818 		bool is_volatile = false;
3819 		u32 idx = i;
3820 
3821 		if (helpers[i].mref == NULL)
3822 			continue;
3823 
3824 		master = helpers[i].mref->ctrl;
3825 		cs->error_idx = i;
3826 
3827 		v4l2_ctrl_lock(master);
3828 
3829 		/*
3830 		 * g_volatile_ctrl will update the new control values.
3831 		 * This makes no sense for V4L2_CTRL_WHICH_DEF_VAL and
3832 		 * V4L2_CTRL_WHICH_REQUEST_VAL. In the case of requests
3833 		 * it is v4l2_ctrl_request_complete() that copies the
3834 		 * volatile controls at the time of request completion
3835 		 * to the request, so you don't want to do that again.
3836 		 */
3837 		if (!is_default && !is_request &&
3838 		    ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
3839 		    (master->has_volatiles && !is_cur_manual(master)))) {
3840 			for (j = 0; j < master->ncontrols; j++)
3841 				cur_to_new(master->cluster[j]);
3842 			ret = call_op(master, g_volatile_ctrl);
3843 			is_volatile = true;
3844 		}
3845 
3846 		if (ret) {
3847 			v4l2_ctrl_unlock(master);
3848 			break;
3849 		}
3850 
3851 		/*
3852 		 * Copy the default value (if is_default is true), the
3853 		 * request value (if is_request is true and p_req is valid),
3854 		 * the new volatile value (if is_volatile is true) or the
3855 		 * current value.
3856 		 */
3857 		do {
3858 			struct v4l2_ctrl_ref *ref = helpers[idx].ref;
3859 
3860 			if (is_default)
3861 				ret = def_to_user(cs->controls + idx, ref->ctrl);
3862 			else if (is_request && ref->valid_p_req)
3863 				ret = req_to_user(cs->controls + idx, ref);
3864 			else if (is_volatile)
3865 				ret = new_to_user(cs->controls + idx, ref->ctrl);
3866 			else
3867 				ret = cur_to_user(cs->controls + idx, ref->ctrl);
3868 			idx = helpers[idx].next;
3869 		} while (!ret && idx);
3870 
3871 		v4l2_ctrl_unlock(master);
3872 	}
3873 
3874 	if (cs->count > ARRAY_SIZE(helper))
3875 		kvfree(helpers);
3876 	return ret;
3877 }
3878 
3879 static struct media_request_object *
v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler * hdl,struct media_request * req,bool set)3880 v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler *hdl,
3881 			struct media_request *req, bool set)
3882 {
3883 	struct media_request_object *obj;
3884 	struct v4l2_ctrl_handler *new_hdl;
3885 	int ret;
3886 
3887 	if (IS_ERR(req))
3888 		return ERR_CAST(req);
3889 
3890 	if (set && WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
3891 		return ERR_PTR(-EBUSY);
3892 
3893 	obj = media_request_object_find(req, &req_ops, hdl);
3894 	if (obj)
3895 		return obj;
3896 	if (!set)
3897 		return ERR_PTR(-ENOENT);
3898 
3899 	new_hdl = kzalloc(sizeof(*new_hdl), GFP_KERNEL);
3900 	if (!new_hdl)
3901 		return ERR_PTR(-ENOMEM);
3902 
3903 	obj = &new_hdl->req_obj;
3904 	ret = v4l2_ctrl_handler_init(new_hdl, (hdl->nr_of_buckets - 1) * 8);
3905 	if (!ret)
3906 		ret = v4l2_ctrl_request_bind(req, new_hdl, hdl);
3907 	if (ret) {
3908 		kfree(new_hdl);
3909 
3910 		return ERR_PTR(ret);
3911 	}
3912 
3913 	media_request_object_get(obj);
3914 	return obj;
3915 }
3916 
v4l2_g_ext_ctrls(struct v4l2_ctrl_handler * hdl,struct video_device * vdev,struct media_device * mdev,struct v4l2_ext_controls * cs)3917 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
3918 		     struct media_device *mdev, struct v4l2_ext_controls *cs)
3919 {
3920 	struct media_request_object *obj = NULL;
3921 	struct media_request *req = NULL;
3922 	int ret;
3923 
3924 	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
3925 		if (!mdev || cs->request_fd < 0)
3926 			return -EINVAL;
3927 
3928 		req = media_request_get_by_fd(mdev, cs->request_fd);
3929 		if (IS_ERR(req))
3930 			return PTR_ERR(req);
3931 
3932 		if (req->state != MEDIA_REQUEST_STATE_COMPLETE) {
3933 			media_request_put(req);
3934 			return -EACCES;
3935 		}
3936 
3937 		ret = media_request_lock_for_access(req);
3938 		if (ret) {
3939 			media_request_put(req);
3940 			return ret;
3941 		}
3942 
3943 		obj = v4l2_ctrls_find_req_obj(hdl, req, false);
3944 		if (IS_ERR(obj)) {
3945 			media_request_unlock_for_access(req);
3946 			media_request_put(req);
3947 			return PTR_ERR(obj);
3948 		}
3949 
3950 		hdl = container_of(obj, struct v4l2_ctrl_handler,
3951 				   req_obj);
3952 	}
3953 
3954 	ret = v4l2_g_ext_ctrls_common(hdl, cs, vdev);
3955 
3956 	if (obj) {
3957 		media_request_unlock_for_access(req);
3958 		media_request_object_put(obj);
3959 		media_request_put(req);
3960 	}
3961 	return ret;
3962 }
3963 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
3964 
3965 /* Helper function to get a single control */
get_ctrl(struct v4l2_ctrl * ctrl,struct v4l2_ext_control * c)3966 static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
3967 {
3968 	struct v4l2_ctrl *master = ctrl->cluster[0];
3969 	int ret = 0;
3970 	int i;
3971 
3972 	/* Compound controls are not supported. The new_to_user() and
3973 	 * cur_to_user() calls below would need to be modified not to access
3974 	 * userspace memory when called from get_ctrl().
3975 	 */
3976 	if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64)
3977 		return -EINVAL;
3978 
3979 	if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
3980 		return -EACCES;
3981 
3982 	v4l2_ctrl_lock(master);
3983 	/* g_volatile_ctrl will update the current control values */
3984 	if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
3985 		for (i = 0; i < master->ncontrols; i++)
3986 			cur_to_new(master->cluster[i]);
3987 		ret = call_op(master, g_volatile_ctrl);
3988 		new_to_user(c, ctrl);
3989 	} else {
3990 		cur_to_user(c, ctrl);
3991 	}
3992 	v4l2_ctrl_unlock(master);
3993 	return ret;
3994 }
3995 
v4l2_g_ctrl(struct v4l2_ctrl_handler * hdl,struct v4l2_control * control)3996 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
3997 {
3998 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
3999 	struct v4l2_ext_control c;
4000 	int ret;
4001 
4002 	if (ctrl == NULL || !ctrl->is_int)
4003 		return -EINVAL;
4004 	ret = get_ctrl(ctrl, &c);
4005 	control->value = c.value;
4006 	return ret;
4007 }
4008 EXPORT_SYMBOL(v4l2_g_ctrl);
4009 
v4l2_ctrl_g_ctrl(struct v4l2_ctrl * ctrl)4010 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
4011 {
4012 	struct v4l2_ext_control c;
4013 
4014 	/* It's a driver bug if this happens. */
4015 	if (WARN_ON(!ctrl->is_int))
4016 		return 0;
4017 	c.value = 0;
4018 	get_ctrl(ctrl, &c);
4019 	return c.value;
4020 }
4021 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
4022 
v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl * ctrl)4023 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
4024 {
4025 	struct v4l2_ext_control c;
4026 
4027 	/* It's a driver bug if this happens. */
4028 	if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
4029 		return 0;
4030 	c.value64 = 0;
4031 	get_ctrl(ctrl, &c);
4032 	return c.value64;
4033 }
4034 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);
4035 
4036 
4037 /* Core function that calls try/s_ctrl and ensures that the new value is
4038    copied to the current value on a set.
4039    Must be called with ctrl->handler->lock held. */
try_or_set_cluster(struct v4l2_fh * fh,struct v4l2_ctrl * master,bool set,u32 ch_flags)4040 static int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
4041 			      bool set, u32 ch_flags)
4042 {
4043 	bool update_flag;
4044 	int ret;
4045 	int i;
4046 
4047 	/* Go through the cluster and either validate the new value or
4048 	   (if no new value was set), copy the current value to the new
4049 	   value, ensuring a consistent view for the control ops when
4050 	   called. */
4051 	for (i = 0; i < master->ncontrols; i++) {
4052 		struct v4l2_ctrl *ctrl = master->cluster[i];
4053 
4054 		if (ctrl == NULL)
4055 			continue;
4056 
4057 		if (!ctrl->is_new) {
4058 			cur_to_new(ctrl);
4059 			continue;
4060 		}
4061 		/* Check again: it may have changed since the
4062 		   previous check in try_or_set_ext_ctrls(). */
4063 		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
4064 			return -EBUSY;
4065 	}
4066 
4067 	ret = call_op(master, try_ctrl);
4068 
4069 	/* Don't set if there is no change */
4070 	if (ret || !set || !cluster_changed(master))
4071 		return ret;
4072 	ret = call_op(master, s_ctrl);
4073 	if (ret)
4074 		return ret;
4075 
4076 	/* If OK, then make the new values permanent. */
4077 	update_flag = is_cur_manual(master) != is_new_manual(master);
4078 
4079 	for (i = 0; i < master->ncontrols; i++) {
4080 		/*
4081 		 * If we switch from auto to manual mode, and this cluster
4082 		 * contains volatile controls, then all non-master controls
4083 		 * have to be marked as changed. The 'new' value contains
4084 		 * the volatile value (obtained by update_from_auto_cluster),
4085 		 * which now has to become the current value.
4086 		 */
4087 		if (i && update_flag && is_new_manual(master) &&
4088 		    master->has_volatiles && master->cluster[i])
4089 			master->cluster[i]->has_changed = true;
4090 
4091 		new_to_cur(fh, master->cluster[i], ch_flags |
4092 			((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
4093 	}
4094 	return 0;
4095 }
4096 
4097 /* Validate controls. */
validate_ctrls(struct v4l2_ext_controls * cs,struct v4l2_ctrl_helper * helpers,struct video_device * vdev,bool set)4098 static int validate_ctrls(struct v4l2_ext_controls *cs,
4099 			  struct v4l2_ctrl_helper *helpers,
4100 			  struct video_device *vdev,
4101 			  bool set)
4102 {
4103 	unsigned i;
4104 	int ret = 0;
4105 
4106 	cs->error_idx = cs->count;
4107 	for (i = 0; i < cs->count; i++) {
4108 		struct v4l2_ctrl *ctrl = helpers[i].ref->ctrl;
4109 		union v4l2_ctrl_ptr p_new;
4110 
4111 		cs->error_idx = i;
4112 
4113 		if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY) {
4114 			dprintk(vdev,
4115 				"control id 0x%x is read-only\n",
4116 				ctrl->id);
4117 			return -EACCES;
4118 		}
4119 		/* This test is also done in try_set_control_cluster() which
4120 		   is called in atomic context, so that has the final say,
4121 		   but it makes sense to do an up-front check as well. Once
4122 		   an error occurs in try_set_control_cluster() some other
4123 		   controls may have been set already and we want to do a
4124 		   best-effort to avoid that. */
4125 		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)) {
4126 			dprintk(vdev,
4127 				"control id 0x%x is grabbed, cannot set\n",
4128 				ctrl->id);
4129 			return -EBUSY;
4130 		}
4131 		/*
4132 		 * Skip validation for now if the payload needs to be copied
4133 		 * from userspace into kernelspace. We'll validate those later.
4134 		 */
4135 		if (ctrl->is_ptr)
4136 			continue;
4137 		if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
4138 			p_new.p_s64 = &cs->controls[i].value64;
4139 		else
4140 			p_new.p_s32 = &cs->controls[i].value;
4141 		ret = validate_new(ctrl, p_new);
4142 		if (ret)
4143 			return ret;
4144 	}
4145 	return 0;
4146 }
4147 
4148 /* Obtain the current volatile values of an autocluster and mark them
4149    as new. */
update_from_auto_cluster(struct v4l2_ctrl * master)4150 static void update_from_auto_cluster(struct v4l2_ctrl *master)
4151 {
4152 	int i;
4153 
4154 	for (i = 1; i < master->ncontrols; i++)
4155 		cur_to_new(master->cluster[i]);
4156 	if (!call_op(master, g_volatile_ctrl))
4157 		for (i = 1; i < master->ncontrols; i++)
4158 			if (master->cluster[i])
4159 				master->cluster[i]->is_new = 1;
4160 }
4161 
4162 /* Try or try-and-set controls */
try_set_ext_ctrls_common(struct v4l2_fh * fh,struct v4l2_ctrl_handler * hdl,struct v4l2_ext_controls * cs,struct video_device * vdev,bool set)4163 static int try_set_ext_ctrls_common(struct v4l2_fh *fh,
4164 				    struct v4l2_ctrl_handler *hdl,
4165 				    struct v4l2_ext_controls *cs,
4166 				    struct video_device *vdev, bool set)
4167 {
4168 	struct v4l2_ctrl_helper helper[4];
4169 	struct v4l2_ctrl_helper *helpers = helper;
4170 	unsigned i, j;
4171 	int ret;
4172 
4173 	cs->error_idx = cs->count;
4174 
4175 	/* Default value cannot be changed */
4176 	if (cs->which == V4L2_CTRL_WHICH_DEF_VAL) {
4177 		dprintk(vdev, "%s: cannot change default value\n",
4178 			video_device_node_name(vdev));
4179 		return -EINVAL;
4180 	}
4181 
4182 	cs->which = V4L2_CTRL_ID2WHICH(cs->which);
4183 
4184 	if (hdl == NULL) {
4185 		dprintk(vdev, "%s: invalid null control handler\n",
4186 			video_device_node_name(vdev));
4187 		return -EINVAL;
4188 	}
4189 
4190 	if (cs->count == 0)
4191 		return class_check(hdl, cs->which);
4192 
4193 	if (cs->count > ARRAY_SIZE(helper)) {
4194 		helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
4195 					 GFP_KERNEL);
4196 		if (!helpers)
4197 			return -ENOMEM;
4198 	}
4199 	ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, false);
4200 	if (!ret)
4201 		ret = validate_ctrls(cs, helpers, vdev, set);
4202 	if (ret && set)
4203 		cs->error_idx = cs->count;
4204 	for (i = 0; !ret && i < cs->count; i++) {
4205 		struct v4l2_ctrl *master;
4206 		u32 idx = i;
4207 
4208 		if (helpers[i].mref == NULL)
4209 			continue;
4210 
4211 		cs->error_idx = i;
4212 		master = helpers[i].mref->ctrl;
4213 		v4l2_ctrl_lock(master);
4214 
4215 		/* Reset the 'is_new' flags of the cluster */
4216 		for (j = 0; j < master->ncontrols; j++)
4217 			if (master->cluster[j])
4218 				master->cluster[j]->is_new = 0;
4219 
4220 		/* For volatile autoclusters that are currently in auto mode
4221 		   we need to discover if it will be set to manual mode.
4222 		   If so, then we have to copy the current volatile values
4223 		   first since those will become the new manual values (which
4224 		   may be overwritten by explicit new values from this set
4225 		   of controls). */
4226 		if (master->is_auto && master->has_volatiles &&
4227 						!is_cur_manual(master)) {
4228 			/* Pick an initial non-manual value */
4229 			s32 new_auto_val = master->manual_mode_value + 1;
4230 			u32 tmp_idx = idx;
4231 
4232 			do {
4233 				/* Check if the auto control is part of the
4234 				   list, and remember the new value. */
4235 				if (helpers[tmp_idx].ref->ctrl == master)
4236 					new_auto_val = cs->controls[tmp_idx].value;
4237 				tmp_idx = helpers[tmp_idx].next;
4238 			} while (tmp_idx);
4239 			/* If the new value == the manual value, then copy
4240 			   the current volatile values. */
4241 			if (new_auto_val == master->manual_mode_value)
4242 				update_from_auto_cluster(master);
4243 		}
4244 
4245 		/* Copy the new caller-supplied control values.
4246 		   user_to_new() sets 'is_new' to 1. */
4247 		do {
4248 			struct v4l2_ctrl *ctrl = helpers[idx].ref->ctrl;
4249 
4250 			ret = user_to_new(cs->controls + idx, ctrl);
4251 			if (!ret && ctrl->is_ptr)
4252 				ret = validate_new(ctrl, ctrl->p_new);
4253 			idx = helpers[idx].next;
4254 		} while (!ret && idx);
4255 
4256 		if (!ret)
4257 			ret = try_or_set_cluster(fh, master,
4258 						 !hdl->req_obj.req && set, 0);
4259 		if (!ret && hdl->req_obj.req && set) {
4260 			for (j = 0; j < master->ncontrols; j++) {
4261 				struct v4l2_ctrl_ref *ref =
4262 					find_ref(hdl, master->cluster[j]->id);
4263 
4264 				new_to_req(ref);
4265 			}
4266 		}
4267 
4268 		/* Copy the new values back to userspace. */
4269 		if (!ret) {
4270 			idx = i;
4271 			do {
4272 				ret = new_to_user(cs->controls + idx,
4273 						helpers[idx].ref->ctrl);
4274 				idx = helpers[idx].next;
4275 			} while (!ret && idx);
4276 		}
4277 		v4l2_ctrl_unlock(master);
4278 	}
4279 
4280 	if (cs->count > ARRAY_SIZE(helper))
4281 		kvfree(helpers);
4282 	return ret;
4283 }
4284 
try_set_ext_ctrls(struct v4l2_fh * fh,struct v4l2_ctrl_handler * hdl,struct video_device * vdev,struct media_device * mdev,struct v4l2_ext_controls * cs,bool set)4285 static int try_set_ext_ctrls(struct v4l2_fh *fh,
4286 			     struct v4l2_ctrl_handler *hdl,
4287 			     struct video_device *vdev,
4288 			     struct media_device *mdev,
4289 			     struct v4l2_ext_controls *cs, bool set)
4290 {
4291 	struct media_request_object *obj = NULL;
4292 	struct media_request *req = NULL;
4293 	int ret;
4294 
4295 	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
4296 		if (!mdev) {
4297 			dprintk(vdev, "%s: missing media device\n",
4298 				video_device_node_name(vdev));
4299 			return -EINVAL;
4300 		}
4301 
4302 		if (cs->request_fd < 0) {
4303 			dprintk(vdev, "%s: invalid request fd %d\n",
4304 				video_device_node_name(vdev), cs->request_fd);
4305 			return -EINVAL;
4306 		}
4307 
4308 		req = media_request_get_by_fd(mdev, cs->request_fd);
4309 		if (IS_ERR(req)) {
4310 			dprintk(vdev, "%s: cannot find request fd %d\n",
4311 				video_device_node_name(vdev), cs->request_fd);
4312 			return PTR_ERR(req);
4313 		}
4314 
4315 		ret = media_request_lock_for_update(req);
4316 		if (ret) {
4317 			dprintk(vdev, "%s: cannot lock request fd %d\n",
4318 				video_device_node_name(vdev), cs->request_fd);
4319 			media_request_put(req);
4320 			return ret;
4321 		}
4322 
4323 		obj = v4l2_ctrls_find_req_obj(hdl, req, set);
4324 		if (IS_ERR(obj)) {
4325 			dprintk(vdev,
4326 				"%s: cannot find request object for request fd %d\n",
4327 				video_device_node_name(vdev),
4328 				cs->request_fd);
4329 			media_request_unlock_for_update(req);
4330 			media_request_put(req);
4331 			return PTR_ERR(obj);
4332 		}
4333 		hdl = container_of(obj, struct v4l2_ctrl_handler,
4334 				   req_obj);
4335 	}
4336 
4337 	ret = try_set_ext_ctrls_common(fh, hdl, cs, vdev, set);
4338 	if (ret)
4339 		dprintk(vdev,
4340 			"%s: try_set_ext_ctrls_common failed (%d)\n",
4341 			video_device_node_name(vdev), ret);
4342 
4343 	if (obj) {
4344 		media_request_unlock_for_update(req);
4345 		media_request_object_put(obj);
4346 		media_request_put(req);
4347 	}
4348 
4349 	return ret;
4350 }
4351 
v4l2_try_ext_ctrls(struct v4l2_ctrl_handler * hdl,struct video_device * vdev,struct media_device * mdev,struct v4l2_ext_controls * cs)4352 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl,
4353 		       struct video_device *vdev,
4354 		       struct media_device *mdev,
4355 		       struct v4l2_ext_controls *cs)
4356 {
4357 	return try_set_ext_ctrls(NULL, hdl, vdev, mdev, cs, false);
4358 }
4359 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
4360 
v4l2_s_ext_ctrls(struct v4l2_fh * fh,struct v4l2_ctrl_handler * hdl,struct video_device * vdev,struct media_device * mdev,struct v4l2_ext_controls * cs)4361 int v4l2_s_ext_ctrls(struct v4l2_fh *fh,
4362 		     struct v4l2_ctrl_handler *hdl,
4363 		     struct video_device *vdev,
4364 		     struct media_device *mdev,
4365 		     struct v4l2_ext_controls *cs)
4366 {
4367 	return try_set_ext_ctrls(fh, hdl, vdev, mdev, cs, true);
4368 }
4369 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
4370 
4371 /* Helper function for VIDIOC_S_CTRL compatibility */
set_ctrl(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,u32 ch_flags)4372 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
4373 {
4374 	struct v4l2_ctrl *master = ctrl->cluster[0];
4375 	int ret;
4376 	int i;
4377 
4378 	/* Reset the 'is_new' flags of the cluster */
4379 	for (i = 0; i < master->ncontrols; i++)
4380 		if (master->cluster[i])
4381 			master->cluster[i]->is_new = 0;
4382 
4383 	ret = validate_new(ctrl, ctrl->p_new);
4384 	if (ret)
4385 		return ret;
4386 
4387 	/* For autoclusters with volatiles that are switched from auto to
4388 	   manual mode we have to update the current volatile values since
4389 	   those will become the initial manual values after such a switch. */
4390 	if (master->is_auto && master->has_volatiles && ctrl == master &&
4391 	    !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
4392 		update_from_auto_cluster(master);
4393 
4394 	ctrl->is_new = 1;
4395 	return try_or_set_cluster(fh, master, true, ch_flags);
4396 }
4397 
4398 /* Helper function for VIDIOC_S_CTRL compatibility */
set_ctrl_lock(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,struct v4l2_ext_control * c)4399 static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
4400 			 struct v4l2_ext_control *c)
4401 {
4402 	int ret;
4403 
4404 	v4l2_ctrl_lock(ctrl);
4405 	user_to_new(c, ctrl);
4406 	ret = set_ctrl(fh, ctrl, 0);
4407 	if (!ret)
4408 		cur_to_user(c, ctrl);
4409 	v4l2_ctrl_unlock(ctrl);
4410 	return ret;
4411 }
4412 
v4l2_s_ctrl(struct v4l2_fh * fh,struct v4l2_ctrl_handler * hdl,struct v4l2_control * control)4413 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
4414 					struct v4l2_control *control)
4415 {
4416 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
4417 	struct v4l2_ext_control c = { control->id };
4418 	int ret;
4419 
4420 	if (ctrl == NULL || !ctrl->is_int)
4421 		return -EINVAL;
4422 
4423 	if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
4424 		return -EACCES;
4425 
4426 	c.value = control->value;
4427 	ret = set_ctrl_lock(fh, ctrl, &c);
4428 	control->value = c.value;
4429 	return ret;
4430 }
4431 EXPORT_SYMBOL(v4l2_s_ctrl);
4432 
__v4l2_ctrl_s_ctrl(struct v4l2_ctrl * ctrl,s32 val)4433 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
4434 {
4435 	lockdep_assert_held(ctrl->handler->lock);
4436 
4437 	/* It's a driver bug if this happens. */
4438 	if (WARN_ON(!ctrl->is_int))
4439 		return -EINVAL;
4440 	ctrl->val = val;
4441 	return set_ctrl(NULL, ctrl, 0);
4442 }
4443 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
4444 
__v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl * ctrl,s64 val)4445 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
4446 {
4447 	lockdep_assert_held(ctrl->handler->lock);
4448 
4449 	/* It's a driver bug if this happens. */
4450 	if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
4451 		return -EINVAL;
4452 	*ctrl->p_new.p_s64 = val;
4453 	return set_ctrl(NULL, ctrl, 0);
4454 }
4455 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
4456 
__v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl * ctrl,const char * s)4457 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
4458 {
4459 	lockdep_assert_held(ctrl->handler->lock);
4460 
4461 	/* It's a driver bug if this happens. */
4462 	if (WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING))
4463 		return -EINVAL;
4464 	strscpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
4465 	return set_ctrl(NULL, ctrl, 0);
4466 }
4467 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);
4468 
__v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl * ctrl,enum v4l2_ctrl_type type,const void * p)4469 int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
4470 				enum v4l2_ctrl_type type, const void *p)
4471 {
4472 	lockdep_assert_held(ctrl->handler->lock);
4473 
4474 	/* It's a driver bug if this happens. */
4475 	if (WARN_ON(ctrl->type != type))
4476 		return -EINVAL;
4477 	memcpy(ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size);
4478 	return set_ctrl(NULL, ctrl, 0);
4479 }
4480 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_compound);
4481 
v4l2_ctrl_request_complete(struct media_request * req,struct v4l2_ctrl_handler * main_hdl)4482 void v4l2_ctrl_request_complete(struct media_request *req,
4483 				struct v4l2_ctrl_handler *main_hdl)
4484 {
4485 	struct media_request_object *obj;
4486 	struct v4l2_ctrl_handler *hdl;
4487 	struct v4l2_ctrl_ref *ref;
4488 
4489 	if (!req || !main_hdl)
4490 		return;
4491 
4492 	/*
4493 	 * Note that it is valid if nothing was found. It means
4494 	 * that this request doesn't have any controls and so just
4495 	 * wants to leave the controls unchanged.
4496 	 */
4497 	obj = media_request_object_find(req, &req_ops, main_hdl);
4498 	if (!obj)
4499 		return;
4500 	hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
4501 
4502 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
4503 		struct v4l2_ctrl *ctrl = ref->ctrl;
4504 		struct v4l2_ctrl *master = ctrl->cluster[0];
4505 		unsigned int i;
4506 
4507 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
4508 			v4l2_ctrl_lock(master);
4509 			/* g_volatile_ctrl will update the current control values */
4510 			for (i = 0; i < master->ncontrols; i++)
4511 				cur_to_new(master->cluster[i]);
4512 			call_op(master, g_volatile_ctrl);
4513 			new_to_req(ref);
4514 			v4l2_ctrl_unlock(master);
4515 			continue;
4516 		}
4517 		if (ref->valid_p_req)
4518 			continue;
4519 
4520 		/* Copy the current control value into the request */
4521 		v4l2_ctrl_lock(ctrl);
4522 		cur_to_req(ref);
4523 		v4l2_ctrl_unlock(ctrl);
4524 	}
4525 
4526 	mutex_lock(main_hdl->lock);
4527 	WARN_ON(!hdl->request_is_queued);
4528 	list_del_init(&hdl->requests_queued);
4529 	hdl->request_is_queued = false;
4530 	mutex_unlock(main_hdl->lock);
4531 	media_request_object_complete(obj);
4532 	media_request_object_put(obj);
4533 }
4534 EXPORT_SYMBOL(v4l2_ctrl_request_complete);
4535 
v4l2_ctrl_request_setup(struct media_request * req,struct v4l2_ctrl_handler * main_hdl)4536 int v4l2_ctrl_request_setup(struct media_request *req,
4537 			     struct v4l2_ctrl_handler *main_hdl)
4538 {
4539 	struct media_request_object *obj;
4540 	struct v4l2_ctrl_handler *hdl;
4541 	struct v4l2_ctrl_ref *ref;
4542 	int ret = 0;
4543 
4544 	if (!req || !main_hdl)
4545 		return 0;
4546 
4547 	if (WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
4548 		return -EBUSY;
4549 
4550 	/*
4551 	 * Note that it is valid if nothing was found. It means
4552 	 * that this request doesn't have any controls and so just
4553 	 * wants to leave the controls unchanged.
4554 	 */
4555 	obj = media_request_object_find(req, &req_ops, main_hdl);
4556 	if (!obj)
4557 		return 0;
4558 	if (obj->completed) {
4559 		media_request_object_put(obj);
4560 		return -EBUSY;
4561 	}
4562 	hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
4563 
4564 	list_for_each_entry(ref, &hdl->ctrl_refs, node)
4565 		ref->req_done = false;
4566 
4567 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
4568 		struct v4l2_ctrl *ctrl = ref->ctrl;
4569 		struct v4l2_ctrl *master = ctrl->cluster[0];
4570 		bool have_new_data = false;
4571 		int i;
4572 
4573 		/*
4574 		 * Skip if this control was already handled by a cluster.
4575 		 * Skip button controls and read-only controls.
4576 		 */
4577 		if (ref->req_done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
4578 		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
4579 			continue;
4580 
4581 		v4l2_ctrl_lock(master);
4582 		for (i = 0; i < master->ncontrols; i++) {
4583 			if (master->cluster[i]) {
4584 				struct v4l2_ctrl_ref *r =
4585 					find_ref(hdl, master->cluster[i]->id);
4586 
4587 				if (r->valid_p_req) {
4588 					have_new_data = true;
4589 					break;
4590 				}
4591 			}
4592 		}
4593 		if (!have_new_data) {
4594 			v4l2_ctrl_unlock(master);
4595 			continue;
4596 		}
4597 
4598 		for (i = 0; i < master->ncontrols; i++) {
4599 			if (master->cluster[i]) {
4600 				struct v4l2_ctrl_ref *r =
4601 					find_ref(hdl, master->cluster[i]->id);
4602 
4603 				req_to_new(r);
4604 				master->cluster[i]->is_new = 1;
4605 				r->req_done = true;
4606 			}
4607 		}
4608 		/*
4609 		 * For volatile autoclusters that are currently in auto mode
4610 		 * we need to discover if it will be set to manual mode.
4611 		 * If so, then we have to copy the current volatile values
4612 		 * first since those will become the new manual values (which
4613 		 * may be overwritten by explicit new values from this set
4614 		 * of controls).
4615 		 */
4616 		if (master->is_auto && master->has_volatiles &&
4617 		    !is_cur_manual(master)) {
4618 			s32 new_auto_val = *master->p_new.p_s32;
4619 
4620 			/*
4621 			 * If the new value == the manual value, then copy
4622 			 * the current volatile values.
4623 			 */
4624 			if (new_auto_val == master->manual_mode_value)
4625 				update_from_auto_cluster(master);
4626 		}
4627 
4628 		ret = try_or_set_cluster(NULL, master, true, 0);
4629 		v4l2_ctrl_unlock(master);
4630 
4631 		if (ret)
4632 			break;
4633 	}
4634 
4635 	media_request_object_put(obj);
4636 	return ret;
4637 }
4638 EXPORT_SYMBOL(v4l2_ctrl_request_setup);
4639 
v4l2_ctrl_notify(struct v4l2_ctrl * ctrl,v4l2_ctrl_notify_fnc notify,void * priv)4640 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
4641 {
4642 	if (ctrl == NULL)
4643 		return;
4644 	if (notify == NULL) {
4645 		ctrl->call_notify = 0;
4646 		return;
4647 	}
4648 	if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
4649 		return;
4650 	ctrl->handler->notify = notify;
4651 	ctrl->handler->notify_priv = priv;
4652 	ctrl->call_notify = 1;
4653 }
4654 EXPORT_SYMBOL(v4l2_ctrl_notify);
4655 
__v4l2_ctrl_modify_range(struct v4l2_ctrl * ctrl,s64 min,s64 max,u64 step,s64 def)4656 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
4657 			s64 min, s64 max, u64 step, s64 def)
4658 {
4659 	bool value_changed;
4660 	bool range_changed = false;
4661 	int ret;
4662 
4663 	lockdep_assert_held(ctrl->handler->lock);
4664 
4665 	switch (ctrl->type) {
4666 	case V4L2_CTRL_TYPE_INTEGER:
4667 	case V4L2_CTRL_TYPE_INTEGER64:
4668 	case V4L2_CTRL_TYPE_BOOLEAN:
4669 	case V4L2_CTRL_TYPE_MENU:
4670 	case V4L2_CTRL_TYPE_INTEGER_MENU:
4671 	case V4L2_CTRL_TYPE_BITMASK:
4672 	case V4L2_CTRL_TYPE_U8:
4673 	case V4L2_CTRL_TYPE_U16:
4674 	case V4L2_CTRL_TYPE_U32:
4675 		if (ctrl->is_array)
4676 			return -EINVAL;
4677 		ret = check_range(ctrl->type, min, max, step, def);
4678 		if (ret)
4679 			return ret;
4680 		break;
4681 	default:
4682 		return -EINVAL;
4683 	}
4684 	if ((ctrl->minimum != min) || (ctrl->maximum != max) ||
4685 		(ctrl->step != step) || ctrl->default_value != def) {
4686 		range_changed = true;
4687 		ctrl->minimum = min;
4688 		ctrl->maximum = max;
4689 		ctrl->step = step;
4690 		ctrl->default_value = def;
4691 	}
4692 	cur_to_new(ctrl);
4693 	if (validate_new(ctrl, ctrl->p_new)) {
4694 		if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
4695 			*ctrl->p_new.p_s64 = def;
4696 		else
4697 			*ctrl->p_new.p_s32 = def;
4698 	}
4699 
4700 	if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
4701 		value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
4702 	else
4703 		value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
4704 	if (value_changed)
4705 		ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
4706 	else if (range_changed)
4707 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
4708 	return ret;
4709 }
4710 EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
4711 
v4l2_ctrl_add_event(struct v4l2_subscribed_event * sev,unsigned elems)4712 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
4713 {
4714 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
4715 
4716 	if (ctrl == NULL)
4717 		return -EINVAL;
4718 
4719 	v4l2_ctrl_lock(ctrl);
4720 	list_add_tail(&sev->node, &ctrl->ev_subs);
4721 	if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
4722 	    (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
4723 		struct v4l2_event ev;
4724 		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
4725 
4726 		if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
4727 			changes |= V4L2_EVENT_CTRL_CH_VALUE;
4728 		fill_event(&ev, ctrl, changes);
4729 		/* Mark the queue as active, allowing this initial
4730 		   event to be accepted. */
4731 		sev->elems = elems;
4732 		v4l2_event_queue_fh(sev->fh, &ev);
4733 	}
4734 	v4l2_ctrl_unlock(ctrl);
4735 	return 0;
4736 }
4737 
v4l2_ctrl_del_event(struct v4l2_subscribed_event * sev)4738 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
4739 {
4740 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
4741 
4742 	if (ctrl == NULL)
4743 		return;
4744 
4745 	v4l2_ctrl_lock(ctrl);
4746 	list_del(&sev->node);
4747 	v4l2_ctrl_unlock(ctrl);
4748 }
4749 
v4l2_ctrl_replace(struct v4l2_event * old,const struct v4l2_event * new)4750 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
4751 {
4752 	u32 old_changes = old->u.ctrl.changes;
4753 
4754 	old->u.ctrl = new->u.ctrl;
4755 	old->u.ctrl.changes |= old_changes;
4756 }
4757 EXPORT_SYMBOL(v4l2_ctrl_replace);
4758 
v4l2_ctrl_merge(const struct v4l2_event * old,struct v4l2_event * new)4759 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
4760 {
4761 	new->u.ctrl.changes |= old->u.ctrl.changes;
4762 }
4763 EXPORT_SYMBOL(v4l2_ctrl_merge);
4764 
4765 const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
4766 	.add = v4l2_ctrl_add_event,
4767 	.del = v4l2_ctrl_del_event,
4768 	.replace = v4l2_ctrl_replace,
4769 	.merge = v4l2_ctrl_merge,
4770 };
4771 EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
4772 
v4l2_ctrl_log_status(struct file * file,void * fh)4773 int v4l2_ctrl_log_status(struct file *file, void *fh)
4774 {
4775 	struct video_device *vfd = video_devdata(file);
4776 	struct v4l2_fh *vfh = file->private_data;
4777 
4778 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
4779 		v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
4780 			vfd->v4l2_dev->name);
4781 	return 0;
4782 }
4783 EXPORT_SYMBOL(v4l2_ctrl_log_status);
4784 
v4l2_ctrl_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)4785 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
4786 				const struct v4l2_event_subscription *sub)
4787 {
4788 	if (sub->type == V4L2_EVENT_CTRL)
4789 		return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
4790 	return -EINVAL;
4791 }
4792 EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
4793 
v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)4794 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
4795 				     struct v4l2_event_subscription *sub)
4796 {
4797 	if (!sd->ctrl_handler)
4798 		return -EINVAL;
4799 	return v4l2_ctrl_subscribe_event(fh, sub);
4800 }
4801 EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);
4802 
v4l2_ctrl_poll(struct file * file,struct poll_table_struct * wait)4803 __poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
4804 {
4805 	struct v4l2_fh *fh = file->private_data;
4806 
4807 	poll_wait(file, &fh->wait, wait);
4808 	if (v4l2_event_pending(fh))
4809 		return EPOLLPRI;
4810 	return 0;
4811 }
4812 EXPORT_SYMBOL(v4l2_ctrl_poll);
4813 
v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ctrl_ops,const struct v4l2_fwnode_device_properties * p)4814 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,
4815 				    const struct v4l2_ctrl_ops *ctrl_ops,
4816 				    const struct v4l2_fwnode_device_properties *p)
4817 {
4818 	if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) {
4819 		u32 orientation_ctrl;
4820 
4821 		switch (p->orientation) {
4822 		case V4L2_FWNODE_ORIENTATION_FRONT:
4823 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT;
4824 			break;
4825 		case V4L2_FWNODE_ORIENTATION_BACK:
4826 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK;
4827 			break;
4828 		case V4L2_FWNODE_ORIENTATION_EXTERNAL:
4829 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL;
4830 			break;
4831 		default:
4832 			return -EINVAL;
4833 		}
4834 		if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops,
4835 					    V4L2_CID_CAMERA_ORIENTATION,
4836 					    V4L2_CAMERA_ORIENTATION_EXTERNAL, 0,
4837 					    orientation_ctrl))
4838 			return hdl->error;
4839 	}
4840 
4841 	if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) {
4842 		if (!v4l2_ctrl_new_std(hdl, ctrl_ops,
4843 				       V4L2_CID_CAMERA_SENSOR_ROTATION,
4844 				       p->rotation, p->rotation, 1,
4845 				       p->rotation))
4846 			return hdl->error;
4847 	}
4848 
4849 	return hdl->error;
4850 }
4851 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties);
4852