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