• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     V4L2 controls framework implementation.
4 
5     Copyright (C) 2010  Hans Verkuil <hverkuil@xs4all.nl>
6 
7  */
8 
9 #define pr_fmt(fmt) "v4l2-ctrls: " fmt
10 
11 #include <linux/ctype.h>
12 #include <linux/export.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-dev.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-ioctl.h>
21 
22 #define dprintk(vdev, fmt, arg...) do {					\
23 	if (!WARN_ON(!(vdev)) && ((vdev)->dev_debug & V4L2_DEV_DEBUG_CTRL)) \
24 		printk(KERN_DEBUG pr_fmt("%s: %s: " fmt),		\
25 		       __func__, video_device_node_name(vdev), ##arg);	\
26 } while (0)
27 
28 #define has_op(master, op) \
29 	(master->ops && master->ops->op)
30 #define call_op(master, op) \
31 	(has_op(master, op) ? master->ops->op(master) : 0)
32 
33 static const union v4l2_ctrl_ptr ptr_null;
34 
35 /* Internal temporary helper struct, one for each v4l2_ext_control */
36 struct v4l2_ctrl_helper {
37 	/* Pointer to the control reference of the master control */
38 	struct v4l2_ctrl_ref *mref;
39 	/* The control ref corresponding to the v4l2_ext_control ID field. */
40 	struct v4l2_ctrl_ref *ref;
41 	/* v4l2_ext_control index of the next control belonging to the
42 	   same cluster, or 0 if there isn't any. */
43 	u32 next;
44 };
45 
46 /* Small helper function to determine if the autocluster is set to manual
47    mode. */
is_cur_manual(const struct v4l2_ctrl * master)48 static bool is_cur_manual(const struct v4l2_ctrl *master)
49 {
50 	return master->is_auto && master->cur.val == master->manual_mode_value;
51 }
52 
53 /* Same as above, but this checks the against the new value instead of the
54    current value. */
is_new_manual(const struct v4l2_ctrl * master)55 static bool is_new_manual(const struct v4l2_ctrl *master)
56 {
57 	return master->is_auto && master->val == master->manual_mode_value;
58 }
59 
60 /* Returns NULL or a character pointer array containing the menu for
61    the given control ID. The pointer array ends with a NULL pointer.
62    An empty string signifies a menu entry that is invalid. This allows
63    drivers to disable certain options if it is not supported. */
v4l2_ctrl_get_menu(u32 id)64 const char * const *v4l2_ctrl_get_menu(u32 id)
65 {
66 	static const char * const mpeg_audio_sampling_freq[] = {
67 		"44.1 kHz",
68 		"48 kHz",
69 		"32 kHz",
70 		NULL
71 	};
72 	static const char * const mpeg_audio_encoding[] = {
73 		"MPEG-1/2 Layer I",
74 		"MPEG-1/2 Layer II",
75 		"MPEG-1/2 Layer III",
76 		"MPEG-2/4 AAC",
77 		"AC-3",
78 		NULL
79 	};
80 	static const char * const mpeg_audio_l1_bitrate[] = {
81 		"32 kbps",
82 		"64 kbps",
83 		"96 kbps",
84 		"128 kbps",
85 		"160 kbps",
86 		"192 kbps",
87 		"224 kbps",
88 		"256 kbps",
89 		"288 kbps",
90 		"320 kbps",
91 		"352 kbps",
92 		"384 kbps",
93 		"416 kbps",
94 		"448 kbps",
95 		NULL
96 	};
97 	static const char * const mpeg_audio_l2_bitrate[] = {
98 		"32 kbps",
99 		"48 kbps",
100 		"56 kbps",
101 		"64 kbps",
102 		"80 kbps",
103 		"96 kbps",
104 		"112 kbps",
105 		"128 kbps",
106 		"160 kbps",
107 		"192 kbps",
108 		"224 kbps",
109 		"256 kbps",
110 		"320 kbps",
111 		"384 kbps",
112 		NULL
113 	};
114 	static const char * const mpeg_audio_l3_bitrate[] = {
115 		"32 kbps",
116 		"40 kbps",
117 		"48 kbps",
118 		"56 kbps",
119 		"64 kbps",
120 		"80 kbps",
121 		"96 kbps",
122 		"112 kbps",
123 		"128 kbps",
124 		"160 kbps",
125 		"192 kbps",
126 		"224 kbps",
127 		"256 kbps",
128 		"320 kbps",
129 		NULL
130 	};
131 	static const char * const mpeg_audio_ac3_bitrate[] = {
132 		"32 kbps",
133 		"40 kbps",
134 		"48 kbps",
135 		"56 kbps",
136 		"64 kbps",
137 		"80 kbps",
138 		"96 kbps",
139 		"112 kbps",
140 		"128 kbps",
141 		"160 kbps",
142 		"192 kbps",
143 		"224 kbps",
144 		"256 kbps",
145 		"320 kbps",
146 		"384 kbps",
147 		"448 kbps",
148 		"512 kbps",
149 		"576 kbps",
150 		"640 kbps",
151 		NULL
152 	};
153 	static const char * const mpeg_audio_mode[] = {
154 		"Stereo",
155 		"Joint Stereo",
156 		"Dual",
157 		"Mono",
158 		NULL
159 	};
160 	static const char * const mpeg_audio_mode_extension[] = {
161 		"Bound 4",
162 		"Bound 8",
163 		"Bound 12",
164 		"Bound 16",
165 		NULL
166 	};
167 	static const char * const mpeg_audio_emphasis[] = {
168 		"No Emphasis",
169 		"50/15 us",
170 		"CCITT J17",
171 		NULL
172 	};
173 	static const char * const mpeg_audio_crc[] = {
174 		"No CRC",
175 		"16-bit CRC",
176 		NULL
177 	};
178 	static const char * const mpeg_audio_dec_playback[] = {
179 		"Auto",
180 		"Stereo",
181 		"Left",
182 		"Right",
183 		"Mono",
184 		"Swapped Stereo",
185 		NULL
186 	};
187 	static const char * const mpeg_video_encoding[] = {
188 		"MPEG-1",
189 		"MPEG-2",
190 		"MPEG-4 AVC",
191 		NULL
192 	};
193 	static const char * const mpeg_video_aspect[] = {
194 		"1x1",
195 		"4x3",
196 		"16x9",
197 		"2.21x1",
198 		NULL
199 	};
200 	static const char * const mpeg_video_bitrate_mode[] = {
201 		"Variable Bitrate",
202 		"Constant Bitrate",
203 		"Constant Quality",
204 		NULL
205 	};
206 	static const char * const mpeg_stream_type[] = {
207 		"MPEG-2 Program Stream",
208 		"MPEG-2 Transport Stream",
209 		"MPEG-1 System Stream",
210 		"MPEG-2 DVD-compatible Stream",
211 		"MPEG-1 VCD-compatible Stream",
212 		"MPEG-2 SVCD-compatible Stream",
213 		NULL
214 	};
215 	static const char * const mpeg_stream_vbi_fmt[] = {
216 		"No VBI",
217 		"Private Packet, IVTV Format",
218 		NULL
219 	};
220 	static const char * const camera_power_line_frequency[] = {
221 		"Disabled",
222 		"50 Hz",
223 		"60 Hz",
224 		"Auto",
225 		NULL
226 	};
227 	static const char * const camera_exposure_auto[] = {
228 		"Auto Mode",
229 		"Manual Mode",
230 		"Shutter Priority Mode",
231 		"Aperture Priority Mode",
232 		NULL
233 	};
234 	static const char * const camera_exposure_metering[] = {
235 		"Average",
236 		"Center Weighted",
237 		"Spot",
238 		"Matrix",
239 		NULL
240 	};
241 	static const char * const camera_auto_focus_range[] = {
242 		"Auto",
243 		"Normal",
244 		"Macro",
245 		"Infinity",
246 		NULL
247 	};
248 	static const char * const colorfx[] = {
249 		"None",
250 		"Black & White",
251 		"Sepia",
252 		"Negative",
253 		"Emboss",
254 		"Sketch",
255 		"Sky Blue",
256 		"Grass Green",
257 		"Skin Whiten",
258 		"Vivid",
259 		"Aqua",
260 		"Art Freeze",
261 		"Silhouette",
262 		"Solarization",
263 		"Antique",
264 		"Set Cb/Cr",
265 		NULL
266 	};
267 	static const char * const auto_n_preset_white_balance[] = {
268 		"Manual",
269 		"Auto",
270 		"Incandescent",
271 		"Fluorescent",
272 		"Fluorescent H",
273 		"Horizon",
274 		"Daylight",
275 		"Flash",
276 		"Cloudy",
277 		"Shade",
278 		NULL,
279 	};
280 	static const char * const camera_iso_sensitivity_auto[] = {
281 		"Manual",
282 		"Auto",
283 		NULL
284 	};
285 	static const char * const scene_mode[] = {
286 		"None",
287 		"Backlight",
288 		"Beach/Snow",
289 		"Candle Light",
290 		"Dusk/Dawn",
291 		"Fall Colors",
292 		"Fireworks",
293 		"Landscape",
294 		"Night",
295 		"Party/Indoor",
296 		"Portrait",
297 		"Sports",
298 		"Sunset",
299 		"Text",
300 		NULL
301 	};
302 	static const char * const tune_emphasis[] = {
303 		"None",
304 		"50 Microseconds",
305 		"75 Microseconds",
306 		NULL,
307 	};
308 	static const char * const header_mode[] = {
309 		"Separate Buffer",
310 		"Joined With 1st Frame",
311 		NULL,
312 	};
313 	static const char * const multi_slice[] = {
314 		"Single",
315 		"Max Macroblocks",
316 		"Max Bytes",
317 		NULL,
318 	};
319 	static const char * const entropy_mode[] = {
320 		"CAVLC",
321 		"CABAC",
322 		NULL,
323 	};
324 	static const char * const mpeg_h264_level[] = {
325 		"1",
326 		"1b",
327 		"1.1",
328 		"1.2",
329 		"1.3",
330 		"2",
331 		"2.1",
332 		"2.2",
333 		"3",
334 		"3.1",
335 		"3.2",
336 		"4",
337 		"4.1",
338 		"4.2",
339 		"5",
340 		"5.1",
341 		"5.2",
342 		"6.0",
343 		"6.1",
344 		"6.2",
345 		NULL,
346 	};
347 	static const char * const h264_loop_filter[] = {
348 		"Enabled",
349 		"Disabled",
350 		"Disabled at Slice Boundary",
351 		NULL,
352 	};
353 	static const char * const h264_profile[] = {
354 		"Baseline",
355 		"Constrained Baseline",
356 		"Main",
357 		"Extended",
358 		"High",
359 		"High 10",
360 		"High 422",
361 		"High 444 Predictive",
362 		"High 10 Intra",
363 		"High 422 Intra",
364 		"High 444 Intra",
365 		"CAVLC 444 Intra",
366 		"Scalable Baseline",
367 		"Scalable High",
368 		"Scalable High Intra",
369 		"Stereo High",
370 		"Multiview High",
371 		"Constrained High",
372 		NULL,
373 	};
374 	static const char * const vui_sar_idc[] = {
375 		"Unspecified",
376 		"1:1",
377 		"12:11",
378 		"10:11",
379 		"16:11",
380 		"40:33",
381 		"24:11",
382 		"20:11",
383 		"32:11",
384 		"80:33",
385 		"18:11",
386 		"15:11",
387 		"64:33",
388 		"160:99",
389 		"4:3",
390 		"3:2",
391 		"2:1",
392 		"Extended SAR",
393 		NULL,
394 	};
395 	static const char * const h264_fp_arrangement_type[] = {
396 		"Checkerboard",
397 		"Column",
398 		"Row",
399 		"Side by Side",
400 		"Top Bottom",
401 		"Temporal",
402 		NULL,
403 	};
404 	static const char * const h264_fmo_map_type[] = {
405 		"Interleaved Slices",
406 		"Scattered Slices",
407 		"Foreground with Leftover",
408 		"Box Out",
409 		"Raster Scan",
410 		"Wipe Scan",
411 		"Explicit",
412 		NULL,
413 	};
414 	static const char * const h264_decode_mode[] = {
415 		"Slice-Based",
416 		"Frame-Based",
417 		NULL,
418 	};
419 	static const char * const h264_start_code[] = {
420 		"No Start Code",
421 		"Annex B Start Code",
422 		NULL,
423 	};
424 	static const char * const 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 (ptr.p_s32[idx] < BITS_PER_LONG_LONG &&
1991 		    (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx])))
1992 			return -EINVAL;
1993 		if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1994 		    ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
1995 			return -EINVAL;
1996 		return 0;
1997 
1998 	case V4L2_CTRL_TYPE_BITMASK:
1999 		ptr.p_s32[idx] &= ctrl->maximum;
2000 		return 0;
2001 
2002 	case V4L2_CTRL_TYPE_BUTTON:
2003 	case V4L2_CTRL_TYPE_CTRL_CLASS:
2004 		ptr.p_s32[idx] = 0;
2005 		return 0;
2006 
2007 	case V4L2_CTRL_TYPE_STRING:
2008 		idx *= ctrl->elem_size;
2009 		len = strlen(ptr.p_char + idx);
2010 		if (len < ctrl->minimum)
2011 			return -ERANGE;
2012 		if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
2013 			return -ERANGE;
2014 		return 0;
2015 
2016 	default:
2017 		return std_validate_compound(ctrl, idx, ptr);
2018 	}
2019 }
2020 
2021 static const struct v4l2_ctrl_type_ops std_type_ops = {
2022 	.equal = std_equal,
2023 	.init = std_init,
2024 	.log = std_log,
2025 	.validate = std_validate,
2026 };
2027 
2028 /* 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)2029 static int ptr_to_user(struct v4l2_ext_control *c,
2030 		       struct v4l2_ctrl *ctrl,
2031 		       union v4l2_ctrl_ptr ptr)
2032 {
2033 	u32 len;
2034 
2035 	if (ctrl->is_ptr && !ctrl->is_string)
2036 		return copy_to_user(c->ptr, ptr.p_const, c->size) ?
2037 		       -EFAULT : 0;
2038 
2039 	switch (ctrl->type) {
2040 	case V4L2_CTRL_TYPE_STRING:
2041 		len = strlen(ptr.p_char);
2042 		if (c->size < len + 1) {
2043 			c->size = ctrl->elem_size;
2044 			return -ENOSPC;
2045 		}
2046 		return copy_to_user(c->string, ptr.p_char, len + 1) ?
2047 		       -EFAULT : 0;
2048 	case V4L2_CTRL_TYPE_INTEGER64:
2049 		c->value64 = *ptr.p_s64;
2050 		break;
2051 	default:
2052 		c->value = *ptr.p_s32;
2053 		break;
2054 	}
2055 	return 0;
2056 }
2057 
2058 /* Helper function: copy the current control value back to the caller */
cur_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2059 static int cur_to_user(struct v4l2_ext_control *c,
2060 		       struct v4l2_ctrl *ctrl)
2061 {
2062 	return ptr_to_user(c, ctrl, ctrl->p_cur);
2063 }
2064 
2065 /* Helper function: copy the new control value back to the caller */
new_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2066 static int new_to_user(struct v4l2_ext_control *c,
2067 		       struct v4l2_ctrl *ctrl)
2068 {
2069 	return ptr_to_user(c, ctrl, ctrl->p_new);
2070 }
2071 
2072 /* Helper function: copy the request value back to the caller */
req_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl_ref * ref)2073 static int req_to_user(struct v4l2_ext_control *c,
2074 		       struct v4l2_ctrl_ref *ref)
2075 {
2076 	return ptr_to_user(c, ref->ctrl, ref->p_req);
2077 }
2078 
2079 /* Helper function: copy the initial control value back to the caller */
def_to_user(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2080 static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
2081 {
2082 	int idx;
2083 
2084 	for (idx = 0; idx < ctrl->elems; idx++)
2085 		ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
2086 
2087 	return ptr_to_user(c, ctrl, ctrl->p_new);
2088 }
2089 
2090 /* 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)2091 static int user_to_ptr(struct v4l2_ext_control *c,
2092 		       struct v4l2_ctrl *ctrl,
2093 		       union v4l2_ctrl_ptr ptr)
2094 {
2095 	int ret;
2096 	u32 size;
2097 
2098 	ctrl->is_new = 1;
2099 	if (ctrl->is_ptr && !ctrl->is_string) {
2100 		unsigned idx;
2101 
2102 		ret = copy_from_user(ptr.p, c->ptr, c->size) ? -EFAULT : 0;
2103 		if (ret || !ctrl->is_array)
2104 			return ret;
2105 		for (idx = c->size / ctrl->elem_size; idx < ctrl->elems; idx++)
2106 			ctrl->type_ops->init(ctrl, idx, ptr);
2107 		return 0;
2108 	}
2109 
2110 	switch (ctrl->type) {
2111 	case V4L2_CTRL_TYPE_INTEGER64:
2112 		*ptr.p_s64 = c->value64;
2113 		break;
2114 	case V4L2_CTRL_TYPE_STRING:
2115 		size = c->size;
2116 		if (size == 0)
2117 			return -ERANGE;
2118 		if (size > ctrl->maximum + 1)
2119 			size = ctrl->maximum + 1;
2120 		ret = copy_from_user(ptr.p_char, c->string, size) ? -EFAULT : 0;
2121 		if (!ret) {
2122 			char last = ptr.p_char[size - 1];
2123 
2124 			ptr.p_char[size - 1] = 0;
2125 			/* If the string was longer than ctrl->maximum,
2126 			   then return an error. */
2127 			if (strlen(ptr.p_char) == ctrl->maximum && last)
2128 				return -ERANGE;
2129 		}
2130 		return ret;
2131 	default:
2132 		*ptr.p_s32 = c->value;
2133 		break;
2134 	}
2135 	return 0;
2136 }
2137 
2138 /* Helper function: copy the caller-provider value as the new control value */
user_to_new(struct v4l2_ext_control * c,struct v4l2_ctrl * ctrl)2139 static int user_to_new(struct v4l2_ext_control *c,
2140 		       struct v4l2_ctrl *ctrl)
2141 {
2142 	return user_to_ptr(c, ctrl, ctrl->p_new);
2143 }
2144 
2145 /* Copy the one value to another. */
ptr_to_ptr(struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr from,union v4l2_ctrl_ptr to)2146 static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
2147 		       union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to)
2148 {
2149 	if (ctrl == NULL)
2150 		return;
2151 	memcpy(to.p, from.p_const, ctrl->elems * ctrl->elem_size);
2152 }
2153 
2154 /* Copy the new value to the current value. */
new_to_cur(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,u32 ch_flags)2155 static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
2156 {
2157 	bool changed;
2158 
2159 	if (ctrl == NULL)
2160 		return;
2161 
2162 	/* has_changed is set by cluster_changed */
2163 	changed = ctrl->has_changed;
2164 	if (changed)
2165 		ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur);
2166 
2167 	if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
2168 		/* Note: CH_FLAGS is only set for auto clusters. */
2169 		ctrl->flags &=
2170 			~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
2171 		if (!is_cur_manual(ctrl->cluster[0])) {
2172 			ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
2173 			if (ctrl->cluster[0]->has_volatiles)
2174 				ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2175 		}
2176 		fh = NULL;
2177 	}
2178 	if (changed || ch_flags) {
2179 		/* If a control was changed that was not one of the controls
2180 		   modified by the application, then send the event to all. */
2181 		if (!ctrl->is_new)
2182 			fh = NULL;
2183 		send_event(fh, ctrl,
2184 			(changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
2185 		if (ctrl->call_notify && changed && ctrl->handler->notify)
2186 			ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
2187 	}
2188 }
2189 
2190 /* Copy the current value to the new value */
cur_to_new(struct v4l2_ctrl * ctrl)2191 static void cur_to_new(struct v4l2_ctrl *ctrl)
2192 {
2193 	if (ctrl == NULL)
2194 		return;
2195 	ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new);
2196 }
2197 
2198 /* Copy the new value to the request value */
new_to_req(struct v4l2_ctrl_ref * ref)2199 static void new_to_req(struct v4l2_ctrl_ref *ref)
2200 {
2201 	if (!ref)
2202 		return;
2203 	ptr_to_ptr(ref->ctrl, ref->ctrl->p_new, ref->p_req);
2204 	ref->valid_p_req = true;
2205 }
2206 
2207 /* Copy the current value to the request value */
cur_to_req(struct v4l2_ctrl_ref * ref)2208 static void cur_to_req(struct v4l2_ctrl_ref *ref)
2209 {
2210 	if (!ref)
2211 		return;
2212 	ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->p_req);
2213 	ref->valid_p_req = true;
2214 }
2215 
2216 /* Copy the request value to the new value */
req_to_new(struct v4l2_ctrl_ref * ref)2217 static void req_to_new(struct v4l2_ctrl_ref *ref)
2218 {
2219 	if (!ref)
2220 		return;
2221 	if (ref->valid_p_req)
2222 		ptr_to_ptr(ref->ctrl, ref->p_req, ref->ctrl->p_new);
2223 	else
2224 		ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->ctrl->p_new);
2225 }
2226 
2227 /* Return non-zero if one or more of the controls in the cluster has a new
2228    value that differs from the current value. */
cluster_changed(struct v4l2_ctrl * master)2229 static int cluster_changed(struct v4l2_ctrl *master)
2230 {
2231 	bool changed = false;
2232 	unsigned idx;
2233 	int i;
2234 
2235 	for (i = 0; i < master->ncontrols; i++) {
2236 		struct v4l2_ctrl *ctrl = master->cluster[i];
2237 		bool ctrl_changed = false;
2238 
2239 		if (ctrl == NULL)
2240 			continue;
2241 
2242 		if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE)
2243 			changed = ctrl_changed = true;
2244 
2245 		/*
2246 		 * Set has_changed to false to avoid generating
2247 		 * the event V4L2_EVENT_CTRL_CH_VALUE
2248 		 */
2249 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
2250 			ctrl->has_changed = false;
2251 			continue;
2252 		}
2253 
2254 		for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++)
2255 			ctrl_changed = !ctrl->type_ops->equal(ctrl, idx,
2256 				ctrl->p_cur, ctrl->p_new);
2257 		ctrl->has_changed = ctrl_changed;
2258 		changed |= ctrl->has_changed;
2259 	}
2260 	return changed;
2261 }
2262 
2263 /* Control range checking */
check_range(enum v4l2_ctrl_type type,s64 min,s64 max,u64 step,s64 def)2264 static int check_range(enum v4l2_ctrl_type type,
2265 		s64 min, s64 max, u64 step, s64 def)
2266 {
2267 	switch (type) {
2268 	case V4L2_CTRL_TYPE_BOOLEAN:
2269 		if (step != 1 || max > 1 || min < 0)
2270 			return -ERANGE;
2271 		fallthrough;
2272 	case V4L2_CTRL_TYPE_U8:
2273 	case V4L2_CTRL_TYPE_U16:
2274 	case V4L2_CTRL_TYPE_U32:
2275 	case V4L2_CTRL_TYPE_INTEGER:
2276 	case V4L2_CTRL_TYPE_INTEGER64:
2277 		if (step == 0 || min > max || def < min || def > max)
2278 			return -ERANGE;
2279 		return 0;
2280 	case V4L2_CTRL_TYPE_BITMASK:
2281 		if (step || min || !max || (def & ~max))
2282 			return -ERANGE;
2283 		return 0;
2284 	case V4L2_CTRL_TYPE_MENU:
2285 	case V4L2_CTRL_TYPE_INTEGER_MENU:
2286 		if (min > max || def < min || def > max)
2287 			return -ERANGE;
2288 		/* Note: step == menu_skip_mask for menu controls.
2289 		   So here we check if the default value is masked out. */
2290 		if (step && ((1 << def) & step))
2291 			return -EINVAL;
2292 		return 0;
2293 	case V4L2_CTRL_TYPE_STRING:
2294 		if (min > max || min < 0 || step < 1 || def)
2295 			return -ERANGE;
2296 		return 0;
2297 	default:
2298 		return 0;
2299 	}
2300 }
2301 
2302 /* Validate a new control */
validate_new(const struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr p_new)2303 static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
2304 {
2305 	unsigned idx;
2306 	int err = 0;
2307 
2308 	for (idx = 0; !err && idx < ctrl->elems; idx++)
2309 		err = ctrl->type_ops->validate(ctrl, idx, p_new);
2310 	return err;
2311 }
2312 
node2id(struct list_head * node)2313 static inline u32 node2id(struct list_head *node)
2314 {
2315 	return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
2316 }
2317 
2318 /* Set the handler's error code if it wasn't set earlier already */
handler_set_err(struct v4l2_ctrl_handler * hdl,int err)2319 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
2320 {
2321 	if (hdl->error == 0)
2322 		hdl->error = err;
2323 	return err;
2324 }
2325 
2326 /* 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)2327 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
2328 				 unsigned nr_of_controls_hint,
2329 				 struct lock_class_key *key, const char *name)
2330 {
2331 	mutex_init(&hdl->_lock);
2332 	hdl->lock = &hdl->_lock;
2333 	lockdep_set_class_and_name(hdl->lock, key, name);
2334 	INIT_LIST_HEAD(&hdl->ctrls);
2335 	INIT_LIST_HEAD(&hdl->ctrl_refs);
2336 	INIT_LIST_HEAD(&hdl->requests);
2337 	INIT_LIST_HEAD(&hdl->requests_queued);
2338 	hdl->request_is_queued = false;
2339 	hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
2340 	hdl->buckets = kvmalloc_array(hdl->nr_of_buckets,
2341 				      sizeof(hdl->buckets[0]),
2342 				      GFP_KERNEL | __GFP_ZERO);
2343 	hdl->error = hdl->buckets ? 0 : -ENOMEM;
2344 	media_request_object_init(&hdl->req_obj);
2345 	return hdl->error;
2346 }
2347 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
2348 
2349 /* Free all controls and control refs */
v4l2_ctrl_handler_free(struct v4l2_ctrl_handler * hdl)2350 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
2351 {
2352 	struct v4l2_ctrl_ref *ref, *next_ref;
2353 	struct v4l2_ctrl *ctrl, *next_ctrl;
2354 	struct v4l2_subscribed_event *sev, *next_sev;
2355 
2356 	if (hdl == NULL || hdl->buckets == NULL)
2357 		return;
2358 
2359 	/*
2360 	 * If the main handler is freed and it is used by handler objects in
2361 	 * outstanding requests, then unbind and put those objects before
2362 	 * freeing the main handler.
2363 	 *
2364 	 * The main handler can be identified by having a NULL ops pointer in
2365 	 * the request object.
2366 	 */
2367 	if (!hdl->req_obj.ops && !list_empty(&hdl->requests)) {
2368 		struct v4l2_ctrl_handler *req, *next_req;
2369 
2370 		list_for_each_entry_safe(req, next_req, &hdl->requests, requests) {
2371 			media_request_object_unbind(&req->req_obj);
2372 			media_request_object_put(&req->req_obj);
2373 		}
2374 	}
2375 	mutex_lock(hdl->lock);
2376 	/* Free all nodes */
2377 	list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
2378 		list_del(&ref->node);
2379 		kfree(ref);
2380 	}
2381 	/* Free all controls owned by the handler */
2382 	list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
2383 		list_del(&ctrl->node);
2384 		list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
2385 			list_del(&sev->node);
2386 		kvfree(ctrl);
2387 	}
2388 	kvfree(hdl->buckets);
2389 	hdl->buckets = NULL;
2390 	hdl->cached = NULL;
2391 	hdl->error = 0;
2392 	mutex_unlock(hdl->lock);
2393 	mutex_destroy(&hdl->_lock);
2394 }
2395 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
2396 
2397 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
2398    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
2399    with applications that do not use the NEXT_CTRL flag.
2400 
2401    We just find the n-th private user control. It's O(N), but that should not
2402    be an issue in this particular case. */
find_private_ref(struct v4l2_ctrl_handler * hdl,u32 id)2403 static struct v4l2_ctrl_ref *find_private_ref(
2404 		struct v4l2_ctrl_handler *hdl, u32 id)
2405 {
2406 	struct v4l2_ctrl_ref *ref;
2407 
2408 	id -= V4L2_CID_PRIVATE_BASE;
2409 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2410 		/* Search for private user controls that are compatible with
2411 		   VIDIOC_G/S_CTRL. */
2412 		if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
2413 		    V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
2414 			if (!ref->ctrl->is_int)
2415 				continue;
2416 			if (id == 0)
2417 				return ref;
2418 			id--;
2419 		}
2420 	}
2421 	return NULL;
2422 }
2423 
2424 /* Find a control with the given ID. */
find_ref(struct v4l2_ctrl_handler * hdl,u32 id)2425 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
2426 {
2427 	struct v4l2_ctrl_ref *ref;
2428 	int bucket;
2429 
2430 	id &= V4L2_CTRL_ID_MASK;
2431 
2432 	/* Old-style private controls need special handling */
2433 	if (id >= V4L2_CID_PRIVATE_BASE)
2434 		return find_private_ref(hdl, id);
2435 	bucket = id % hdl->nr_of_buckets;
2436 
2437 	/* Simple optimization: cache the last control found */
2438 	if (hdl->cached && hdl->cached->ctrl->id == id)
2439 		return hdl->cached;
2440 
2441 	/* Not in cache, search the hash */
2442 	ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
2443 	while (ref && ref->ctrl->id != id)
2444 		ref = ref->next;
2445 
2446 	if (ref)
2447 		hdl->cached = ref; /* cache it! */
2448 	return ref;
2449 }
2450 
2451 /* Find a control with the given ID. Take the handler's lock first. */
find_ref_lock(struct v4l2_ctrl_handler * hdl,u32 id)2452 static struct v4l2_ctrl_ref *find_ref_lock(
2453 		struct v4l2_ctrl_handler *hdl, u32 id)
2454 {
2455 	struct v4l2_ctrl_ref *ref = NULL;
2456 
2457 	if (hdl) {
2458 		mutex_lock(hdl->lock);
2459 		ref = find_ref(hdl, id);
2460 		mutex_unlock(hdl->lock);
2461 	}
2462 	return ref;
2463 }
2464 
2465 /* Find a control with the given ID. */
v4l2_ctrl_find(struct v4l2_ctrl_handler * hdl,u32 id)2466 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
2467 {
2468 	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
2469 
2470 	return ref ? ref->ctrl : NULL;
2471 }
2472 EXPORT_SYMBOL(v4l2_ctrl_find);
2473 
2474 /* 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)2475 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
2476 			   struct v4l2_ctrl *ctrl,
2477 			   struct v4l2_ctrl_ref **ctrl_ref,
2478 			   bool from_other_dev, bool allocate_req)
2479 {
2480 	struct v4l2_ctrl_ref *ref;
2481 	struct v4l2_ctrl_ref *new_ref;
2482 	u32 id = ctrl->id;
2483 	u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
2484 	int bucket = id % hdl->nr_of_buckets;	/* which bucket to use */
2485 	unsigned int size_extra_req = 0;
2486 
2487 	if (ctrl_ref)
2488 		*ctrl_ref = NULL;
2489 
2490 	/*
2491 	 * Automatically add the control class if it is not yet present and
2492 	 * the new control is not a compound control.
2493 	 */
2494 	if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
2495 	    id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
2496 		if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
2497 			return hdl->error;
2498 
2499 	if (hdl->error)
2500 		return hdl->error;
2501 
2502 	if (allocate_req)
2503 		size_extra_req = ctrl->elems * ctrl->elem_size;
2504 	new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
2505 	if (!new_ref)
2506 		return handler_set_err(hdl, -ENOMEM);
2507 	new_ref->ctrl = ctrl;
2508 	new_ref->from_other_dev = from_other_dev;
2509 	if (size_extra_req)
2510 		new_ref->p_req.p = &new_ref[1];
2511 
2512 	INIT_LIST_HEAD(&new_ref->node);
2513 
2514 	mutex_lock(hdl->lock);
2515 
2516 	/* Add immediately at the end of the list if the list is empty, or if
2517 	   the last element in the list has a lower ID.
2518 	   This ensures that when elements are added in ascending order the
2519 	   insertion is an O(1) operation. */
2520 	if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
2521 		list_add_tail(&new_ref->node, &hdl->ctrl_refs);
2522 		goto insert_in_hash;
2523 	}
2524 
2525 	/* Find insert position in sorted list */
2526 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2527 		if (ref->ctrl->id < id)
2528 			continue;
2529 		/* Don't add duplicates */
2530 		if (ref->ctrl->id == id) {
2531 			kfree(new_ref);
2532 			goto unlock;
2533 		}
2534 		list_add(&new_ref->node, ref->node.prev);
2535 		break;
2536 	}
2537 
2538 insert_in_hash:
2539 	/* Insert the control node in the hash */
2540 	new_ref->next = hdl->buckets[bucket];
2541 	hdl->buckets[bucket] = new_ref;
2542 	if (ctrl_ref)
2543 		*ctrl_ref = new_ref;
2544 	if (ctrl->handler == hdl) {
2545 		/* By default each control starts in a cluster of its own.
2546 		 * new_ref->ctrl is basically a cluster array with one
2547 		 * element, so that's perfect to use as the cluster pointer.
2548 		 * But only do this for the handler that owns the control.
2549 		 */
2550 		ctrl->cluster = &new_ref->ctrl;
2551 		ctrl->ncontrols = 1;
2552 	}
2553 
2554 unlock:
2555 	mutex_unlock(hdl->lock);
2556 	return 0;
2557 }
2558 
2559 /* 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)2560 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
2561 			const struct v4l2_ctrl_ops *ops,
2562 			const struct v4l2_ctrl_type_ops *type_ops,
2563 			u32 id, const char *name, enum v4l2_ctrl_type type,
2564 			s64 min, s64 max, u64 step, s64 def,
2565 			const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
2566 			u32 flags, const char * const *qmenu,
2567 			const s64 *qmenu_int, const union v4l2_ctrl_ptr p_def,
2568 			void *priv)
2569 {
2570 	struct v4l2_ctrl *ctrl;
2571 	unsigned sz_extra;
2572 	unsigned nr_of_dims = 0;
2573 	unsigned elems = 1;
2574 	bool is_array;
2575 	unsigned tot_ctrl_size;
2576 	unsigned idx;
2577 	void *data;
2578 	int err;
2579 
2580 	if (hdl->error)
2581 		return NULL;
2582 
2583 	while (dims && dims[nr_of_dims]) {
2584 		elems *= dims[nr_of_dims];
2585 		nr_of_dims++;
2586 		if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
2587 			break;
2588 	}
2589 	is_array = nr_of_dims > 0;
2590 
2591 	/* Prefill elem_size for all types handled by std_type_ops */
2592 	switch ((u32)type) {
2593 	case V4L2_CTRL_TYPE_INTEGER64:
2594 		elem_size = sizeof(s64);
2595 		break;
2596 	case V4L2_CTRL_TYPE_STRING:
2597 		elem_size = max + 1;
2598 		break;
2599 	case V4L2_CTRL_TYPE_U8:
2600 		elem_size = sizeof(u8);
2601 		break;
2602 	case V4L2_CTRL_TYPE_U16:
2603 		elem_size = sizeof(u16);
2604 		break;
2605 	case V4L2_CTRL_TYPE_U32:
2606 		elem_size = sizeof(u32);
2607 		break;
2608 	case V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS:
2609 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_slice_params);
2610 		break;
2611 	case V4L2_CTRL_TYPE_MPEG2_QUANTIZATION:
2612 		elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantization);
2613 		break;
2614 	case V4L2_CTRL_TYPE_FWHT_PARAMS:
2615 		elem_size = sizeof(struct v4l2_ctrl_fwht_params);
2616 		break;
2617 	case V4L2_CTRL_TYPE_H264_SPS:
2618 		elem_size = sizeof(struct v4l2_ctrl_h264_sps);
2619 		break;
2620 	case V4L2_CTRL_TYPE_H264_PPS:
2621 		elem_size = sizeof(struct v4l2_ctrl_h264_pps);
2622 		break;
2623 	case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
2624 		elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix);
2625 		break;
2626 	case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
2627 		elem_size = sizeof(struct v4l2_ctrl_h264_slice_params);
2628 		break;
2629 	case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
2630 		elem_size = sizeof(struct v4l2_ctrl_h264_decode_params);
2631 		break;
2632 	case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
2633 		elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights);
2634 		break;
2635 	case V4L2_CTRL_TYPE_VP8_FRAME_HEADER:
2636 		elem_size = sizeof(struct v4l2_ctrl_vp8_frame_header);
2637 		break;
2638 	case V4L2_CTRL_TYPE_HEVC_SPS:
2639 		elem_size = sizeof(struct v4l2_ctrl_hevc_sps);
2640 		break;
2641 	case V4L2_CTRL_TYPE_HEVC_PPS:
2642 		elem_size = sizeof(struct v4l2_ctrl_hevc_pps);
2643 		break;
2644 	case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
2645 		elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params);
2646 		break;
2647 	case V4L2_CTRL_TYPE_AREA:
2648 		elem_size = sizeof(struct v4l2_area);
2649 		break;
2650 	default:
2651 		if (type < V4L2_CTRL_COMPOUND_TYPES)
2652 			elem_size = sizeof(s32);
2653 		break;
2654 	}
2655 	tot_ctrl_size = elem_size * elems;
2656 
2657 	/* Sanity checks */
2658 	if (id == 0 || name == NULL || !elem_size ||
2659 	    id >= V4L2_CID_PRIVATE_BASE ||
2660 	    (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
2661 	    (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
2662 		handler_set_err(hdl, -ERANGE);
2663 		return NULL;
2664 	}
2665 	err = check_range(type, min, max, step, def);
2666 	if (err) {
2667 		handler_set_err(hdl, err);
2668 		return NULL;
2669 	}
2670 	if (is_array &&
2671 	    (type == V4L2_CTRL_TYPE_BUTTON ||
2672 	     type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
2673 		handler_set_err(hdl, -EINVAL);
2674 		return NULL;
2675 	}
2676 
2677 	sz_extra = 0;
2678 	if (type == V4L2_CTRL_TYPE_BUTTON)
2679 		flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
2680 			V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
2681 	else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
2682 		flags |= V4L2_CTRL_FLAG_READ_ONLY;
2683 	else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
2684 		 type == V4L2_CTRL_TYPE_STRING ||
2685 		 type >= V4L2_CTRL_COMPOUND_TYPES ||
2686 		 is_array)
2687 		sz_extra += 2 * tot_ctrl_size;
2688 
2689 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const)
2690 		sz_extra += elem_size;
2691 
2692 	ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
2693 	if (ctrl == NULL) {
2694 		handler_set_err(hdl, -ENOMEM);
2695 		return NULL;
2696 	}
2697 
2698 	INIT_LIST_HEAD(&ctrl->node);
2699 	INIT_LIST_HEAD(&ctrl->ev_subs);
2700 	ctrl->handler = hdl;
2701 	ctrl->ops = ops;
2702 	ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
2703 	ctrl->id = id;
2704 	ctrl->name = name;
2705 	ctrl->type = type;
2706 	ctrl->flags = flags;
2707 	ctrl->minimum = min;
2708 	ctrl->maximum = max;
2709 	ctrl->step = step;
2710 	ctrl->default_value = def;
2711 	ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
2712 	ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
2713 	ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
2714 	ctrl->is_array = is_array;
2715 	ctrl->elems = elems;
2716 	ctrl->nr_of_dims = nr_of_dims;
2717 	if (nr_of_dims)
2718 		memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
2719 	ctrl->elem_size = elem_size;
2720 	if (type == V4L2_CTRL_TYPE_MENU)
2721 		ctrl->qmenu = qmenu;
2722 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2723 		ctrl->qmenu_int = qmenu_int;
2724 	ctrl->priv = priv;
2725 	ctrl->cur.val = ctrl->val = def;
2726 	data = &ctrl[1];
2727 
2728 	if (!ctrl->is_int) {
2729 		ctrl->p_new.p = data;
2730 		ctrl->p_cur.p = data + tot_ctrl_size;
2731 	} else {
2732 		ctrl->p_new.p = &ctrl->val;
2733 		ctrl->p_cur.p = &ctrl->cur.val;
2734 	}
2735 
2736 	if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) {
2737 		ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size;
2738 		memcpy(ctrl->p_def.p, p_def.p_const, elem_size);
2739 	}
2740 
2741 	for (idx = 0; idx < elems; idx++) {
2742 		ctrl->type_ops->init(ctrl, idx, ctrl->p_cur);
2743 		ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
2744 	}
2745 
2746 	if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
2747 		kvfree(ctrl);
2748 		return NULL;
2749 	}
2750 	mutex_lock(hdl->lock);
2751 	list_add_tail(&ctrl->node, &hdl->ctrls);
2752 	mutex_unlock(hdl->lock);
2753 	return ctrl;
2754 }
2755 
v4l2_ctrl_new_custom(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_config * cfg,void * priv)2756 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
2757 			const struct v4l2_ctrl_config *cfg, void *priv)
2758 {
2759 	bool is_menu;
2760 	struct v4l2_ctrl *ctrl;
2761 	const char *name = cfg->name;
2762 	const char * const *qmenu = cfg->qmenu;
2763 	const s64 *qmenu_int = cfg->qmenu_int;
2764 	enum v4l2_ctrl_type type = cfg->type;
2765 	u32 flags = cfg->flags;
2766 	s64 min = cfg->min;
2767 	s64 max = cfg->max;
2768 	u64 step = cfg->step;
2769 	s64 def = cfg->def;
2770 
2771 	if (name == NULL)
2772 		v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
2773 								&def, &flags);
2774 
2775 	is_menu = (type == V4L2_CTRL_TYPE_MENU ||
2776 		   type == V4L2_CTRL_TYPE_INTEGER_MENU);
2777 	if (is_menu)
2778 		WARN_ON(step);
2779 	else
2780 		WARN_ON(cfg->menu_skip_mask);
2781 	if (type == V4L2_CTRL_TYPE_MENU && !qmenu) {
2782 		qmenu = v4l2_ctrl_get_menu(cfg->id);
2783 	} else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) {
2784 		handler_set_err(hdl, -EINVAL);
2785 		return NULL;
2786 	}
2787 
2788 	ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
2789 			type, min, max,
2790 			is_menu ? cfg->menu_skip_mask : step, def,
2791 			cfg->dims, cfg->elem_size,
2792 			flags, qmenu, qmenu_int, cfg->p_def, priv);
2793 	if (ctrl)
2794 		ctrl->is_private = cfg->is_private;
2795 	return ctrl;
2796 }
2797 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
2798 
2799 /* 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)2800 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
2801 			const struct v4l2_ctrl_ops *ops,
2802 			u32 id, s64 min, s64 max, u64 step, s64 def)
2803 {
2804 	const char *name;
2805 	enum v4l2_ctrl_type type;
2806 	u32 flags;
2807 
2808 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2809 	if (type == V4L2_CTRL_TYPE_MENU ||
2810 	    type == V4L2_CTRL_TYPE_INTEGER_MENU ||
2811 	    type >= V4L2_CTRL_COMPOUND_TYPES) {
2812 		handler_set_err(hdl, -EINVAL);
2813 		return NULL;
2814 	}
2815 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2816 			     min, max, step, def, NULL, 0,
2817 			     flags, NULL, NULL, ptr_null, NULL);
2818 }
2819 EXPORT_SYMBOL(v4l2_ctrl_new_std);
2820 
2821 /* 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)2822 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
2823 			const struct v4l2_ctrl_ops *ops,
2824 			u32 id, u8 _max, u64 mask, u8 _def)
2825 {
2826 	const char * const *qmenu = NULL;
2827 	const s64 *qmenu_int = NULL;
2828 	unsigned int qmenu_int_len = 0;
2829 	const char *name;
2830 	enum v4l2_ctrl_type type;
2831 	s64 min;
2832 	s64 max = _max;
2833 	s64 def = _def;
2834 	u64 step;
2835 	u32 flags;
2836 
2837 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2838 
2839 	if (type == V4L2_CTRL_TYPE_MENU)
2840 		qmenu = v4l2_ctrl_get_menu(id);
2841 	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2842 		qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
2843 
2844 	if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) {
2845 		handler_set_err(hdl, -EINVAL);
2846 		return NULL;
2847 	}
2848 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2849 			     0, max, mask, def, NULL, 0,
2850 			     flags, qmenu, qmenu_int, ptr_null, NULL);
2851 }
2852 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
2853 
2854 /* 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)2855 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
2856 			const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
2857 			u64 mask, u8 _def, const char * const *qmenu)
2858 {
2859 	enum v4l2_ctrl_type type;
2860 	const char *name;
2861 	u32 flags;
2862 	u64 step;
2863 	s64 min;
2864 	s64 max = _max;
2865 	s64 def = _def;
2866 
2867 	/* v4l2_ctrl_new_std_menu_items() should only be called for
2868 	 * standard controls without a standard menu.
2869 	 */
2870 	if (v4l2_ctrl_get_menu(id)) {
2871 		handler_set_err(hdl, -EINVAL);
2872 		return NULL;
2873 	}
2874 
2875 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2876 	if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
2877 		handler_set_err(hdl, -EINVAL);
2878 		return NULL;
2879 	}
2880 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2881 			     0, max, mask, def, NULL, 0,
2882 			     flags, qmenu, NULL, ptr_null, NULL);
2883 
2884 }
2885 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
2886 
2887 /* 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)2888 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
2889 				const struct v4l2_ctrl_ops *ops, u32 id,
2890 				const union v4l2_ctrl_ptr p_def)
2891 {
2892 	const char *name;
2893 	enum v4l2_ctrl_type type;
2894 	u32 flags;
2895 	s64 min, max, step, def;
2896 
2897 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2898 	if (type < V4L2_CTRL_COMPOUND_TYPES) {
2899 		handler_set_err(hdl, -EINVAL);
2900 		return NULL;
2901 	}
2902 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2903 			     min, max, step, def, NULL, 0,
2904 			     flags, NULL, NULL, p_def, NULL);
2905 }
2906 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound);
2907 
2908 /* 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)2909 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
2910 			const struct v4l2_ctrl_ops *ops,
2911 			u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
2912 {
2913 	const char *name;
2914 	enum v4l2_ctrl_type type;
2915 	s64 min;
2916 	u64 step;
2917 	s64 max = _max;
2918 	s64 def = _def;
2919 	u32 flags;
2920 
2921 	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2922 	if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
2923 		handler_set_err(hdl, -EINVAL);
2924 		return NULL;
2925 	}
2926 	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2927 			     0, max, 0, def, NULL, 0,
2928 			     flags, NULL, qmenu_int, ptr_null, NULL);
2929 }
2930 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
2931 
2932 /* 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)2933 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
2934 			  struct v4l2_ctrl_handler *add,
2935 			  bool (*filter)(const struct v4l2_ctrl *ctrl),
2936 			  bool from_other_dev)
2937 {
2938 	struct v4l2_ctrl_ref *ref;
2939 	int ret = 0;
2940 
2941 	/* Do nothing if either handler is NULL or if they are the same */
2942 	if (!hdl || !add || hdl == add)
2943 		return 0;
2944 	if (hdl->error)
2945 		return hdl->error;
2946 	mutex_lock(add->lock);
2947 	list_for_each_entry(ref, &add->ctrl_refs, node) {
2948 		struct v4l2_ctrl *ctrl = ref->ctrl;
2949 
2950 		/* Skip handler-private controls. */
2951 		if (ctrl->is_private)
2952 			continue;
2953 		/* And control classes */
2954 		if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2955 			continue;
2956 		/* Filter any unwanted controls */
2957 		if (filter && !filter(ctrl))
2958 			continue;
2959 		ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
2960 		if (ret)
2961 			break;
2962 	}
2963 	mutex_unlock(add->lock);
2964 	return ret;
2965 }
2966 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
2967 
v4l2_ctrl_radio_filter(const struct v4l2_ctrl * ctrl)2968 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
2969 {
2970 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
2971 		return true;
2972 	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
2973 		return true;
2974 	switch (ctrl->id) {
2975 	case V4L2_CID_AUDIO_MUTE:
2976 	case V4L2_CID_AUDIO_VOLUME:
2977 	case V4L2_CID_AUDIO_BALANCE:
2978 	case V4L2_CID_AUDIO_BASS:
2979 	case V4L2_CID_AUDIO_TREBLE:
2980 	case V4L2_CID_AUDIO_LOUDNESS:
2981 		return true;
2982 	default:
2983 		break;
2984 	}
2985 	return false;
2986 }
2987 EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
2988 
2989 /* Cluster controls */
v4l2_ctrl_cluster(unsigned ncontrols,struct v4l2_ctrl ** controls)2990 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
2991 {
2992 	bool has_volatiles = false;
2993 	int i;
2994 
2995 	/* The first control is the master control and it must not be NULL */
2996 	if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
2997 		return;
2998 
2999 	for (i = 0; i < ncontrols; i++) {
3000 		if (controls[i]) {
3001 			controls[i]->cluster = controls;
3002 			controls[i]->ncontrols = ncontrols;
3003 			if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
3004 				has_volatiles = true;
3005 		}
3006 	}
3007 	controls[0]->has_volatiles = has_volatiles;
3008 }
3009 EXPORT_SYMBOL(v4l2_ctrl_cluster);
3010 
v4l2_ctrl_auto_cluster(unsigned ncontrols,struct v4l2_ctrl ** controls,u8 manual_val,bool set_volatile)3011 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
3012 			    u8 manual_val, bool set_volatile)
3013 {
3014 	struct v4l2_ctrl *master = controls[0];
3015 	u32 flag = 0;
3016 	int i;
3017 
3018 	v4l2_ctrl_cluster(ncontrols, controls);
3019 	WARN_ON(ncontrols <= 1);
3020 	WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
3021 	WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
3022 	master->is_auto = true;
3023 	master->has_volatiles = set_volatile;
3024 	master->manual_mode_value = manual_val;
3025 	master->flags |= V4L2_CTRL_FLAG_UPDATE;
3026 
3027 	if (!is_cur_manual(master))
3028 		flag = V4L2_CTRL_FLAG_INACTIVE |
3029 			(set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
3030 
3031 	for (i = 1; i < ncontrols; i++)
3032 		if (controls[i])
3033 			controls[i]->flags |= flag;
3034 }
3035 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
3036 
3037 /* Activate/deactivate a control. */
v4l2_ctrl_activate(struct v4l2_ctrl * ctrl,bool active)3038 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
3039 {
3040 	/* invert since the actual flag is called 'inactive' */
3041 	bool inactive = !active;
3042 	bool old;
3043 
3044 	if (ctrl == NULL)
3045 		return;
3046 
3047 	if (inactive)
3048 		/* set V4L2_CTRL_FLAG_INACTIVE */
3049 		old = test_and_set_bit(4, &ctrl->flags);
3050 	else
3051 		/* clear V4L2_CTRL_FLAG_INACTIVE */
3052 		old = test_and_clear_bit(4, &ctrl->flags);
3053 	if (old != inactive)
3054 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
3055 }
3056 EXPORT_SYMBOL(v4l2_ctrl_activate);
3057 
__v4l2_ctrl_grab(struct v4l2_ctrl * ctrl,bool grabbed)3058 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
3059 {
3060 	bool old;
3061 
3062 	if (ctrl == NULL)
3063 		return;
3064 
3065 	lockdep_assert_held(ctrl->handler->lock);
3066 
3067 	if (grabbed)
3068 		/* set V4L2_CTRL_FLAG_GRABBED */
3069 		old = test_and_set_bit(1, &ctrl->flags);
3070 	else
3071 		/* clear V4L2_CTRL_FLAG_GRABBED */
3072 		old = test_and_clear_bit(1, &ctrl->flags);
3073 	if (old != grabbed)
3074 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
3075 }
3076 EXPORT_SYMBOL(__v4l2_ctrl_grab);
3077 
3078 /* Log the control name and value */
log_ctrl(const struct v4l2_ctrl * ctrl,const char * prefix,const char * colon)3079 static void log_ctrl(const struct v4l2_ctrl *ctrl,
3080 		     const char *prefix, const char *colon)
3081 {
3082 	if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
3083 		return;
3084 	if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
3085 		return;
3086 
3087 	pr_info("%s%s%s: ", prefix, colon, ctrl->name);
3088 
3089 	ctrl->type_ops->log(ctrl);
3090 
3091 	if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
3092 			   V4L2_CTRL_FLAG_GRABBED |
3093 			   V4L2_CTRL_FLAG_VOLATILE)) {
3094 		if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
3095 			pr_cont(" inactive");
3096 		if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
3097 			pr_cont(" grabbed");
3098 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
3099 			pr_cont(" volatile");
3100 	}
3101 	pr_cont("\n");
3102 }
3103 
3104 /* Log all controls owned by the handler */
v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler * hdl,const char * prefix)3105 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
3106 				  const char *prefix)
3107 {
3108 	struct v4l2_ctrl *ctrl;
3109 	const char *colon = "";
3110 	int len;
3111 
3112 	if (hdl == NULL)
3113 		return;
3114 	if (prefix == NULL)
3115 		prefix = "";
3116 	len = strlen(prefix);
3117 	if (len && prefix[len - 1] != ' ')
3118 		colon = ": ";
3119 	mutex_lock(hdl->lock);
3120 	list_for_each_entry(ctrl, &hdl->ctrls, node)
3121 		if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
3122 			log_ctrl(ctrl, prefix, colon);
3123 	mutex_unlock(hdl->lock);
3124 }
3125 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
3126 
v4l2_ctrl_subdev_log_status(struct v4l2_subdev * sd)3127 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
3128 {
3129 	v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
3130 	return 0;
3131 }
3132 EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
3133 
3134 /* Call s_ctrl for all controls owned by the handler */
__v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler * hdl)3135 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
3136 {
3137 	struct v4l2_ctrl *ctrl;
3138 	int ret = 0;
3139 
3140 	if (hdl == NULL)
3141 		return 0;
3142 
3143 	lockdep_assert_held(hdl->lock);
3144 
3145 	list_for_each_entry(ctrl, &hdl->ctrls, node)
3146 		ctrl->done = false;
3147 
3148 	list_for_each_entry(ctrl, &hdl->ctrls, node) {
3149 		struct v4l2_ctrl *master = ctrl->cluster[0];
3150 		int i;
3151 
3152 		/* Skip if this control was already handled by a cluster. */
3153 		/* Skip button controls and read-only controls. */
3154 		if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
3155 		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
3156 			continue;
3157 
3158 		for (i = 0; i < master->ncontrols; i++) {
3159 			if (master->cluster[i]) {
3160 				cur_to_new(master->cluster[i]);
3161 				master->cluster[i]->is_new = 1;
3162 				master->cluster[i]->done = true;
3163 			}
3164 		}
3165 		ret = call_op(master, s_ctrl);
3166 		if (ret)
3167 			break;
3168 	}
3169 
3170 	return ret;
3171 }
3172 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
3173 
v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler * hdl)3174 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
3175 {
3176 	int ret;
3177 
3178 	if (hdl == NULL)
3179 		return 0;
3180 
3181 	mutex_lock(hdl->lock);
3182 	ret = __v4l2_ctrl_handler_setup(hdl);
3183 	mutex_unlock(hdl->lock);
3184 
3185 	return ret;
3186 }
3187 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
3188 
3189 /* Implement VIDIOC_QUERY_EXT_CTRL */
v4l2_query_ext_ctrl(struct v4l2_ctrl_handler * hdl,struct v4l2_query_ext_ctrl * qc)3190 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
3191 {
3192 	const unsigned next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
3193 	u32 id = qc->id & V4L2_CTRL_ID_MASK;
3194 	struct v4l2_ctrl_ref *ref;
3195 	struct v4l2_ctrl *ctrl;
3196 
3197 	if (hdl == NULL)
3198 		return -EINVAL;
3199 
3200 	mutex_lock(hdl->lock);
3201 
3202 	/* Try to find it */
3203 	ref = find_ref(hdl, id);
3204 
3205 	if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
3206 		bool is_compound;
3207 		/* Match any control that is not hidden */
3208 		unsigned mask = 1;
3209 		bool match = false;
3210 
3211 		if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
3212 			/* Match any hidden control */
3213 			match = true;
3214 		} else if ((qc->id & next_flags) == next_flags) {
3215 			/* Match any control, compound or not */
3216 			mask = 0;
3217 		}
3218 
3219 		/* Find the next control with ID > qc->id */
3220 
3221 		/* Did we reach the end of the control list? */
3222 		if (id >= node2id(hdl->ctrl_refs.prev)) {
3223 			ref = NULL; /* Yes, so there is no next control */
3224 		} else if (ref) {
3225 			/* We found a control with the given ID, so just get
3226 			   the next valid one in the list. */
3227 			list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
3228 				is_compound = ref->ctrl->is_array ||
3229 					ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
3230 				if (id < ref->ctrl->id &&
3231 				    (is_compound & mask) == match)
3232 					break;
3233 			}
3234 			if (&ref->node == &hdl->ctrl_refs)
3235 				ref = NULL;
3236 		} else {
3237 			/* No control with the given ID exists, so start
3238 			   searching for the next largest ID. We know there
3239 			   is one, otherwise the first 'if' above would have
3240 			   been true. */
3241 			list_for_each_entry(ref, &hdl->ctrl_refs, node) {
3242 				is_compound = ref->ctrl->is_array ||
3243 					ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
3244 				if (id < ref->ctrl->id &&
3245 				    (is_compound & mask) == match)
3246 					break;
3247 			}
3248 			if (&ref->node == &hdl->ctrl_refs)
3249 				ref = NULL;
3250 		}
3251 	}
3252 	mutex_unlock(hdl->lock);
3253 
3254 	if (!ref)
3255 		return -EINVAL;
3256 
3257 	ctrl = ref->ctrl;
3258 	memset(qc, 0, sizeof(*qc));
3259 	if (id >= V4L2_CID_PRIVATE_BASE)
3260 		qc->id = id;
3261 	else
3262 		qc->id = ctrl->id;
3263 	strscpy(qc->name, ctrl->name, sizeof(qc->name));
3264 	qc->flags = user_flags(ctrl);
3265 	qc->type = ctrl->type;
3266 	qc->elem_size = ctrl->elem_size;
3267 	qc->elems = ctrl->elems;
3268 	qc->nr_of_dims = ctrl->nr_of_dims;
3269 	memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
3270 	qc->minimum = ctrl->minimum;
3271 	qc->maximum = ctrl->maximum;
3272 	qc->default_value = ctrl->default_value;
3273 	if (ctrl->type == V4L2_CTRL_TYPE_MENU
3274 	    || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
3275 		qc->step = 1;
3276 	else
3277 		qc->step = ctrl->step;
3278 	return 0;
3279 }
3280 EXPORT_SYMBOL(v4l2_query_ext_ctrl);
3281 
3282 /* Implement VIDIOC_QUERYCTRL */
v4l2_queryctrl(struct v4l2_ctrl_handler * hdl,struct v4l2_queryctrl * qc)3283 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
3284 {
3285 	struct v4l2_query_ext_ctrl qec = { qc->id };
3286 	int rc;
3287 
3288 	rc = v4l2_query_ext_ctrl(hdl, &qec);
3289 	if (rc)
3290 		return rc;
3291 
3292 	qc->id = qec.id;
3293 	qc->type = qec.type;
3294 	qc->flags = qec.flags;
3295 	strscpy(qc->name, qec.name, sizeof(qc->name));
3296 	switch (qc->type) {
3297 	case V4L2_CTRL_TYPE_INTEGER:
3298 	case V4L2_CTRL_TYPE_BOOLEAN:
3299 	case V4L2_CTRL_TYPE_MENU:
3300 	case V4L2_CTRL_TYPE_INTEGER_MENU:
3301 	case V4L2_CTRL_TYPE_STRING:
3302 	case V4L2_CTRL_TYPE_BITMASK:
3303 		qc->minimum = qec.minimum;
3304 		qc->maximum = qec.maximum;
3305 		qc->step = qec.step;
3306 		qc->default_value = qec.default_value;
3307 		break;
3308 	default:
3309 		qc->minimum = 0;
3310 		qc->maximum = 0;
3311 		qc->step = 0;
3312 		qc->default_value = 0;
3313 		break;
3314 	}
3315 	return 0;
3316 }
3317 EXPORT_SYMBOL(v4l2_queryctrl);
3318 
3319 /* Implement VIDIOC_QUERYMENU */
v4l2_querymenu(struct v4l2_ctrl_handler * hdl,struct v4l2_querymenu * qm)3320 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
3321 {
3322 	struct v4l2_ctrl *ctrl;
3323 	u32 i = qm->index;
3324 
3325 	ctrl = v4l2_ctrl_find(hdl, qm->id);
3326 	if (!ctrl)
3327 		return -EINVAL;
3328 
3329 	qm->reserved = 0;
3330 	/* Sanity checks */
3331 	switch (ctrl->type) {
3332 	case V4L2_CTRL_TYPE_MENU:
3333 		if (ctrl->qmenu == NULL)
3334 			return -EINVAL;
3335 		break;
3336 	case V4L2_CTRL_TYPE_INTEGER_MENU:
3337 		if (ctrl->qmenu_int == NULL)
3338 			return -EINVAL;
3339 		break;
3340 	default:
3341 		return -EINVAL;
3342 	}
3343 
3344 	if (i < ctrl->minimum || i > ctrl->maximum)
3345 		return -EINVAL;
3346 
3347 	/* Use mask to see if this menu item should be skipped */
3348 	if (ctrl->menu_skip_mask & (1ULL << i))
3349 		return -EINVAL;
3350 	/* Empty menu items should also be skipped */
3351 	if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
3352 		if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
3353 			return -EINVAL;
3354 		strscpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
3355 	} else {
3356 		qm->value = ctrl->qmenu_int[i];
3357 	}
3358 	return 0;
3359 }
3360 EXPORT_SYMBOL(v4l2_querymenu);
3361 
v4l2_ctrl_request_clone(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_handler * from)3362 static int v4l2_ctrl_request_clone(struct v4l2_ctrl_handler *hdl,
3363 				   const struct v4l2_ctrl_handler *from)
3364 {
3365 	struct v4l2_ctrl_ref *ref;
3366 	int err = 0;
3367 
3368 	if (WARN_ON(!hdl || hdl == from))
3369 		return -EINVAL;
3370 
3371 	if (hdl->error)
3372 		return hdl->error;
3373 
3374 	WARN_ON(hdl->lock != &hdl->_lock);
3375 
3376 	mutex_lock(from->lock);
3377 	list_for_each_entry(ref, &from->ctrl_refs, node) {
3378 		struct v4l2_ctrl *ctrl = ref->ctrl;
3379 		struct v4l2_ctrl_ref *new_ref;
3380 
3381 		/* Skip refs inherited from other devices */
3382 		if (ref->from_other_dev)
3383 			continue;
3384 		/* And buttons */
3385 		if (ctrl->type == V4L2_CTRL_TYPE_BUTTON)
3386 			continue;
3387 		err = handler_new_ref(hdl, ctrl, &new_ref, false, true);
3388 		if (err)
3389 			break;
3390 	}
3391 	mutex_unlock(from->lock);
3392 	return err;
3393 }
3394 
v4l2_ctrl_request_queue(struct media_request_object * obj)3395 static void v4l2_ctrl_request_queue(struct media_request_object *obj)
3396 {
3397 	struct v4l2_ctrl_handler *hdl =
3398 		container_of(obj, struct v4l2_ctrl_handler, req_obj);
3399 	struct v4l2_ctrl_handler *main_hdl = obj->priv;
3400 
3401 	mutex_lock(main_hdl->lock);
3402 	list_add_tail(&hdl->requests_queued, &main_hdl->requests_queued);
3403 	hdl->request_is_queued = true;
3404 	mutex_unlock(main_hdl->lock);
3405 }
3406 
v4l2_ctrl_request_unbind(struct media_request_object * obj)3407 static void v4l2_ctrl_request_unbind(struct media_request_object *obj)
3408 {
3409 	struct v4l2_ctrl_handler *hdl =
3410 		container_of(obj, struct v4l2_ctrl_handler, req_obj);
3411 	struct v4l2_ctrl_handler *main_hdl = obj->priv;
3412 
3413 	mutex_lock(main_hdl->lock);
3414 	list_del_init(&hdl->requests);
3415 	if (hdl->request_is_queued) {
3416 		list_del_init(&hdl->requests_queued);
3417 		hdl->request_is_queued = false;
3418 	}
3419 	mutex_unlock(main_hdl->lock);
3420 }
3421 
v4l2_ctrl_request_release(struct media_request_object * obj)3422 static void v4l2_ctrl_request_release(struct media_request_object *obj)
3423 {
3424 	struct v4l2_ctrl_handler *hdl =
3425 		container_of(obj, struct v4l2_ctrl_handler, req_obj);
3426 
3427 	v4l2_ctrl_handler_free(hdl);
3428 	kfree(hdl);
3429 }
3430 
3431 static const struct media_request_object_ops req_ops = {
3432 	.queue = v4l2_ctrl_request_queue,
3433 	.unbind = v4l2_ctrl_request_unbind,
3434 	.release = v4l2_ctrl_request_release,
3435 };
3436 
v4l2_ctrl_request_hdl_find(struct media_request * req,struct v4l2_ctrl_handler * parent)3437 struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(struct media_request *req,
3438 					struct v4l2_ctrl_handler *parent)
3439 {
3440 	struct media_request_object *obj;
3441 
3442 	if (WARN_ON(req->state != MEDIA_REQUEST_STATE_VALIDATING &&
3443 		    req->state != MEDIA_REQUEST_STATE_QUEUED))
3444 		return NULL;
3445 
3446 	obj = media_request_object_find(req, &req_ops, parent);
3447 	if (obj)
3448 		return container_of(obj, struct v4l2_ctrl_handler, req_obj);
3449 	return NULL;
3450 }
3451 EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_find);
3452 
3453 struct v4l2_ctrl *
v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler * hdl,u32 id)3454 v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
3455 {
3456 	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
3457 
3458 	return (ref && ref->valid_p_req) ? ref->ctrl : NULL;
3459 }
3460 EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_ctrl_find);
3461 
v4l2_ctrl_request_bind(struct media_request * req,struct v4l2_ctrl_handler * hdl,struct v4l2_ctrl_handler * from)3462 static int v4l2_ctrl_request_bind(struct media_request *req,
3463 			   struct v4l2_ctrl_handler *hdl,
3464 			   struct v4l2_ctrl_handler *from)
3465 {
3466 	int ret;
3467 
3468 	ret = v4l2_ctrl_request_clone(hdl, from);
3469 
3470 	if (!ret) {
3471 		ret = media_request_object_bind(req, &req_ops,
3472 						from, false, &hdl->req_obj);
3473 		if (!ret) {
3474 			mutex_lock(from->lock);
3475 			list_add_tail(&hdl->requests, &from->requests);
3476 			mutex_unlock(from->lock);
3477 		}
3478 	}
3479 	return ret;
3480 }
3481 
3482 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
3483 
3484    It is not a fully atomic operation, just best-effort only. After all, if
3485    multiple controls have to be set through multiple i2c writes (for example)
3486    then some initial writes may succeed while others fail. Thus leaving the
3487    system in an inconsistent state. The question is how much effort you are
3488    willing to spend on trying to make something atomic that really isn't.
3489 
3490    From the point of view of an application the main requirement is that
3491    when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
3492    error should be returned without actually affecting any controls.
3493 
3494    If all the values are correct, then it is acceptable to just give up
3495    in case of low-level errors.
3496 
3497    It is important though that the application can tell when only a partial
3498    configuration was done. The way we do that is through the error_idx field
3499    of struct v4l2_ext_controls: if that is equal to the count field then no
3500    controls were affected. Otherwise all controls before that index were
3501    successful in performing their 'get' or 'set' operation, the control at
3502    the given index failed, and you don't know what happened with the controls
3503    after the failed one. Since if they were part of a control cluster they
3504    could have been successfully processed (if a cluster member was encountered
3505    at index < error_idx), they could have failed (if a cluster member was at
3506    error_idx), or they may not have been processed yet (if the first cluster
3507    member appeared after error_idx).
3508 
3509    It is all fairly theoretical, though. In practice all you can do is to
3510    bail out. If error_idx == count, then it is an application bug. If
3511    error_idx < count then it is only an application bug if the error code was
3512    EBUSY. That usually means that something started streaming just when you
3513    tried to set the controls. In all other cases it is a driver/hardware
3514    problem and all you can do is to retry or bail out.
3515 
3516    Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
3517    never modifies controls the error_idx is just set to whatever control
3518    has an invalid value.
3519  */
3520 
3521 /* Prepare for the extended g/s/try functions.
3522    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)3523 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
3524 			     struct v4l2_ext_controls *cs,
3525 			     struct v4l2_ctrl_helper *helpers,
3526 			     struct video_device *vdev,
3527 			     bool get)
3528 {
3529 	struct v4l2_ctrl_helper *h;
3530 	bool have_clusters = false;
3531 	u32 i;
3532 
3533 	for (i = 0, h = helpers; i < cs->count; i++, h++) {
3534 		struct v4l2_ext_control *c = &cs->controls[i];
3535 		struct v4l2_ctrl_ref *ref;
3536 		struct v4l2_ctrl *ctrl;
3537 		u32 id = c->id & V4L2_CTRL_ID_MASK;
3538 
3539 		cs->error_idx = i;
3540 
3541 		if (cs->which &&
3542 		    cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
3543 		    cs->which != V4L2_CTRL_WHICH_REQUEST_VAL &&
3544 		    V4L2_CTRL_ID2WHICH(id) != cs->which) {
3545 			dprintk(vdev,
3546 				"invalid which 0x%x or control id 0x%x\n",
3547 				cs->which, id);
3548 			return -EINVAL;
3549 		}
3550 
3551 		/* Old-style private controls are not allowed for
3552 		   extended controls */
3553 		if (id >= V4L2_CID_PRIVATE_BASE) {
3554 			dprintk(vdev,
3555 				"old-style private controls not allowed\n");
3556 			return -EINVAL;
3557 		}
3558 		ref = find_ref_lock(hdl, id);
3559 		if (ref == NULL) {
3560 			dprintk(vdev, "cannot find control id 0x%x\n", id);
3561 			return -EINVAL;
3562 		}
3563 		h->ref = ref;
3564 		ctrl = ref->ctrl;
3565 		if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED) {
3566 			dprintk(vdev, "control id 0x%x is disabled\n", id);
3567 			return -EINVAL;
3568 		}
3569 
3570 		if (ctrl->cluster[0]->ncontrols > 1)
3571 			have_clusters = true;
3572 		if (ctrl->cluster[0] != ctrl)
3573 			ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
3574 		if (ctrl->is_ptr && !ctrl->is_string) {
3575 			unsigned tot_size = ctrl->elems * ctrl->elem_size;
3576 
3577 			if (c->size < tot_size) {
3578 				/*
3579 				 * In the get case the application first
3580 				 * queries to obtain the size of the control.
3581 				 */
3582 				if (get) {
3583 					c->size = tot_size;
3584 					return -ENOSPC;
3585 				}
3586 				dprintk(vdev,
3587 					"pointer control id 0x%x size too small, %d bytes but %d bytes needed\n",
3588 					id, c->size, tot_size);
3589 				return -EFAULT;
3590 			}
3591 			c->size = tot_size;
3592 		}
3593 		/* Store the ref to the master control of the cluster */
3594 		h->mref = ref;
3595 		/* Initially set next to 0, meaning that there is no other
3596 		   control in this helper array belonging to the same
3597 		   cluster */
3598 		h->next = 0;
3599 	}
3600 
3601 	/* We are done if there were no controls that belong to a multi-
3602 	   control cluster. */
3603 	if (!have_clusters)
3604 		return 0;
3605 
3606 	/* The code below figures out in O(n) time which controls in the list
3607 	   belong to the same cluster. */
3608 
3609 	/* This has to be done with the handler lock taken. */
3610 	mutex_lock(hdl->lock);
3611 
3612 	/* First zero the helper field in the master control references */
3613 	for (i = 0; i < cs->count; i++)
3614 		helpers[i].mref->helper = NULL;
3615 	for (i = 0, h = helpers; i < cs->count; i++, h++) {
3616 		struct v4l2_ctrl_ref *mref = h->mref;
3617 
3618 		/* If the mref->helper is set, then it points to an earlier
3619 		   helper that belongs to the same cluster. */
3620 		if (mref->helper) {
3621 			/* Set the next field of mref->helper to the current
3622 			   index: this means that that earlier helper now
3623 			   points to the next helper in the same cluster. */
3624 			mref->helper->next = i;
3625 			/* mref should be set only for the first helper in the
3626 			   cluster, clear the others. */
3627 			h->mref = NULL;
3628 		}
3629 		/* Point the mref helper to the current helper struct. */
3630 		mref->helper = h;
3631 	}
3632 	mutex_unlock(hdl->lock);
3633 	return 0;
3634 }
3635 
3636 /* Handles the corner case where cs->count == 0. It checks whether the
3637    specified control class exists. If that class ID is 0, then it checks
3638    whether there are any controls at all. */
class_check(struct v4l2_ctrl_handler * hdl,u32 which)3639 static int class_check(struct v4l2_ctrl_handler *hdl, u32 which)
3640 {
3641 	if (which == 0 || which == V4L2_CTRL_WHICH_DEF_VAL ||
3642 	    which == V4L2_CTRL_WHICH_REQUEST_VAL)
3643 		return 0;
3644 	return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL;
3645 }
3646 
3647 /*
3648  * Get extended controls. Allocates the helpers array if needed.
3649  *
3650  * Note that v4l2_g_ext_ctrls_common() with 'which' set to
3651  * V4L2_CTRL_WHICH_REQUEST_VAL is only called if the request was
3652  * completed, and in that case valid_p_req is true for all controls.
3653  */
v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler * hdl,struct v4l2_ext_controls * cs,struct video_device * vdev)3654 static int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl,
3655 				   struct v4l2_ext_controls *cs,
3656 				   struct video_device *vdev)
3657 {
3658 	struct v4l2_ctrl_helper helper[4];
3659 	struct v4l2_ctrl_helper *helpers = helper;
3660 	int ret;
3661 	int i, j;
3662 	bool is_default, is_request;
3663 
3664 	is_default = (cs->which == V4L2_CTRL_WHICH_DEF_VAL);
3665 	is_request = (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL);
3666 
3667 	cs->error_idx = cs->count;
3668 	cs->which = V4L2_CTRL_ID2WHICH(cs->which);
3669 
3670 	if (hdl == NULL)
3671 		return -EINVAL;
3672 
3673 	if (cs->count == 0)
3674 		return class_check(hdl, cs->which);
3675 
3676 	if (cs->count > ARRAY_SIZE(helper)) {
3677 		helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
3678 					 GFP_KERNEL);
3679 		if (helpers == NULL)
3680 			return -ENOMEM;
3681 	}
3682 
3683 	ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, true);
3684 	cs->error_idx = cs->count;
3685 
3686 	for (i = 0; !ret && i < cs->count; i++)
3687 		if (helpers[i].ref->ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
3688 			ret = -EACCES;
3689 
3690 	for (i = 0; !ret && i < cs->count; i++) {
3691 		struct v4l2_ctrl *master;
3692 		bool is_volatile = false;
3693 		u32 idx = i;
3694 
3695 		if (helpers[i].mref == NULL)
3696 			continue;
3697 
3698 		master = helpers[i].mref->ctrl;
3699 		cs->error_idx = i;
3700 
3701 		v4l2_ctrl_lock(master);
3702 
3703 		/*
3704 		 * g_volatile_ctrl will update the new control values.
3705 		 * This makes no sense for V4L2_CTRL_WHICH_DEF_VAL and
3706 		 * V4L2_CTRL_WHICH_REQUEST_VAL. In the case of requests
3707 		 * it is v4l2_ctrl_request_complete() that copies the
3708 		 * volatile controls at the time of request completion
3709 		 * to the request, so you don't want to do that again.
3710 		 */
3711 		if (!is_default && !is_request &&
3712 		    ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
3713 		    (master->has_volatiles && !is_cur_manual(master)))) {
3714 			for (j = 0; j < master->ncontrols; j++)
3715 				cur_to_new(master->cluster[j]);
3716 			ret = call_op(master, g_volatile_ctrl);
3717 			is_volatile = true;
3718 		}
3719 
3720 		if (ret) {
3721 			v4l2_ctrl_unlock(master);
3722 			break;
3723 		}
3724 
3725 		/*
3726 		 * Copy the default value (if is_default is true), the
3727 		 * request value (if is_request is true and p_req is valid),
3728 		 * the new volatile value (if is_volatile is true) or the
3729 		 * current value.
3730 		 */
3731 		do {
3732 			struct v4l2_ctrl_ref *ref = helpers[idx].ref;
3733 
3734 			if (is_default)
3735 				ret = def_to_user(cs->controls + idx, ref->ctrl);
3736 			else if (is_request && ref->valid_p_req)
3737 				ret = req_to_user(cs->controls + idx, ref);
3738 			else if (is_volatile)
3739 				ret = new_to_user(cs->controls + idx, ref->ctrl);
3740 			else
3741 				ret = cur_to_user(cs->controls + idx, ref->ctrl);
3742 			idx = helpers[idx].next;
3743 		} while (!ret && idx);
3744 
3745 		v4l2_ctrl_unlock(master);
3746 	}
3747 
3748 	if (cs->count > ARRAY_SIZE(helper))
3749 		kvfree(helpers);
3750 	return ret;
3751 }
3752 
3753 static struct media_request_object *
v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler * hdl,struct media_request * req,bool set)3754 v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler *hdl,
3755 			struct media_request *req, bool set)
3756 {
3757 	struct media_request_object *obj;
3758 	struct v4l2_ctrl_handler *new_hdl;
3759 	int ret;
3760 
3761 	if (IS_ERR(req))
3762 		return ERR_CAST(req);
3763 
3764 	if (set && WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
3765 		return ERR_PTR(-EBUSY);
3766 
3767 	obj = media_request_object_find(req, &req_ops, hdl);
3768 	if (obj)
3769 		return obj;
3770 	if (!set)
3771 		return ERR_PTR(-ENOENT);
3772 
3773 	new_hdl = kzalloc(sizeof(*new_hdl), GFP_KERNEL);
3774 	if (!new_hdl)
3775 		return ERR_PTR(-ENOMEM);
3776 
3777 	obj = &new_hdl->req_obj;
3778 	ret = v4l2_ctrl_handler_init(new_hdl, (hdl->nr_of_buckets - 1) * 8);
3779 	if (!ret)
3780 		ret = v4l2_ctrl_request_bind(req, new_hdl, hdl);
3781 	if (ret) {
3782 		kfree(new_hdl);
3783 
3784 		return ERR_PTR(ret);
3785 	}
3786 
3787 	media_request_object_get(obj);
3788 	return obj;
3789 }
3790 
v4l2_g_ext_ctrls(struct v4l2_ctrl_handler * hdl,struct video_device * vdev,struct media_device * mdev,struct v4l2_ext_controls * cs)3791 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
3792 		     struct media_device *mdev, struct v4l2_ext_controls *cs)
3793 {
3794 	struct media_request_object *obj = NULL;
3795 	struct media_request *req = NULL;
3796 	int ret;
3797 
3798 	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
3799 		if (!mdev || cs->request_fd < 0)
3800 			return -EINVAL;
3801 
3802 		req = media_request_get_by_fd(mdev, cs->request_fd);
3803 		if (IS_ERR(req))
3804 			return PTR_ERR(req);
3805 
3806 		if (req->state != MEDIA_REQUEST_STATE_COMPLETE) {
3807 			media_request_put(req);
3808 			return -EACCES;
3809 		}
3810 
3811 		ret = media_request_lock_for_access(req);
3812 		if (ret) {
3813 			media_request_put(req);
3814 			return ret;
3815 		}
3816 
3817 		obj = v4l2_ctrls_find_req_obj(hdl, req, false);
3818 		if (IS_ERR(obj)) {
3819 			media_request_unlock_for_access(req);
3820 			media_request_put(req);
3821 			return PTR_ERR(obj);
3822 		}
3823 
3824 		hdl = container_of(obj, struct v4l2_ctrl_handler,
3825 				   req_obj);
3826 	}
3827 
3828 	ret = v4l2_g_ext_ctrls_common(hdl, cs, vdev);
3829 
3830 	if (obj) {
3831 		media_request_unlock_for_access(req);
3832 		media_request_object_put(obj);
3833 		media_request_put(req);
3834 	}
3835 	return ret;
3836 }
3837 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
3838 
3839 /* Helper function to get a single control */
get_ctrl(struct v4l2_ctrl * ctrl,struct v4l2_ext_control * c)3840 static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
3841 {
3842 	struct v4l2_ctrl *master = ctrl->cluster[0];
3843 	int ret = 0;
3844 	int i;
3845 
3846 	/* Compound controls are not supported. The new_to_user() and
3847 	 * cur_to_user() calls below would need to be modified not to access
3848 	 * userspace memory when called from get_ctrl().
3849 	 */
3850 	if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64)
3851 		return -EINVAL;
3852 
3853 	if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
3854 		return -EACCES;
3855 
3856 	v4l2_ctrl_lock(master);
3857 	/* g_volatile_ctrl will update the current control values */
3858 	if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
3859 		for (i = 0; i < master->ncontrols; i++)
3860 			cur_to_new(master->cluster[i]);
3861 		ret = call_op(master, g_volatile_ctrl);
3862 		new_to_user(c, ctrl);
3863 	} else {
3864 		cur_to_user(c, ctrl);
3865 	}
3866 	v4l2_ctrl_unlock(master);
3867 	return ret;
3868 }
3869 
v4l2_g_ctrl(struct v4l2_ctrl_handler * hdl,struct v4l2_control * control)3870 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
3871 {
3872 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
3873 	struct v4l2_ext_control c;
3874 	int ret;
3875 
3876 	if (ctrl == NULL || !ctrl->is_int)
3877 		return -EINVAL;
3878 	ret = get_ctrl(ctrl, &c);
3879 	control->value = c.value;
3880 	return ret;
3881 }
3882 EXPORT_SYMBOL(v4l2_g_ctrl);
3883 
v4l2_ctrl_g_ctrl(struct v4l2_ctrl * ctrl)3884 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
3885 {
3886 	struct v4l2_ext_control c;
3887 
3888 	/* It's a driver bug if this happens. */
3889 	if (WARN_ON(!ctrl->is_int))
3890 		return 0;
3891 	c.value = 0;
3892 	get_ctrl(ctrl, &c);
3893 	return c.value;
3894 }
3895 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
3896 
v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl * ctrl)3897 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
3898 {
3899 	struct v4l2_ext_control c;
3900 
3901 	/* It's a driver bug if this happens. */
3902 	if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
3903 		return 0;
3904 	c.value64 = 0;
3905 	get_ctrl(ctrl, &c);
3906 	return c.value64;
3907 }
3908 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);
3909 
3910 
3911 /* Core function that calls try/s_ctrl and ensures that the new value is
3912    copied to the current value on a set.
3913    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)3914 static int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
3915 			      bool set, u32 ch_flags)
3916 {
3917 	bool update_flag;
3918 	int ret;
3919 	int i;
3920 
3921 	/* Go through the cluster and either validate the new value or
3922 	   (if no new value was set), copy the current value to the new
3923 	   value, ensuring a consistent view for the control ops when
3924 	   called. */
3925 	for (i = 0; i < master->ncontrols; i++) {
3926 		struct v4l2_ctrl *ctrl = master->cluster[i];
3927 
3928 		if (ctrl == NULL)
3929 			continue;
3930 
3931 		if (!ctrl->is_new) {
3932 			cur_to_new(ctrl);
3933 			continue;
3934 		}
3935 		/* Check again: it may have changed since the
3936 		   previous check in try_or_set_ext_ctrls(). */
3937 		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
3938 			return -EBUSY;
3939 	}
3940 
3941 	ret = call_op(master, try_ctrl);
3942 
3943 	/* Don't set if there is no change */
3944 	if (ret || !set || !cluster_changed(master))
3945 		return ret;
3946 	ret = call_op(master, s_ctrl);
3947 	if (ret)
3948 		return ret;
3949 
3950 	/* If OK, then make the new values permanent. */
3951 	update_flag = is_cur_manual(master) != is_new_manual(master);
3952 
3953 	for (i = 0; i < master->ncontrols; i++) {
3954 		/*
3955 		 * If we switch from auto to manual mode, and this cluster
3956 		 * contains volatile controls, then all non-master controls
3957 		 * have to be marked as changed. The 'new' value contains
3958 		 * the volatile value (obtained by update_from_auto_cluster),
3959 		 * which now has to become the current value.
3960 		 */
3961 		if (i && update_flag && is_new_manual(master) &&
3962 		    master->has_volatiles && master->cluster[i])
3963 			master->cluster[i]->has_changed = true;
3964 
3965 		new_to_cur(fh, master->cluster[i], ch_flags |
3966 			((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
3967 	}
3968 	return 0;
3969 }
3970 
3971 /* Validate controls. */
validate_ctrls(struct v4l2_ext_controls * cs,struct v4l2_ctrl_helper * helpers,struct video_device * vdev,bool set)3972 static int validate_ctrls(struct v4l2_ext_controls *cs,
3973 			  struct v4l2_ctrl_helper *helpers,
3974 			  struct video_device *vdev,
3975 			  bool set)
3976 {
3977 	unsigned i;
3978 	int ret = 0;
3979 
3980 	cs->error_idx = cs->count;
3981 	for (i = 0; i < cs->count; i++) {
3982 		struct v4l2_ctrl *ctrl = helpers[i].ref->ctrl;
3983 		union v4l2_ctrl_ptr p_new;
3984 
3985 		cs->error_idx = i;
3986 
3987 		if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY) {
3988 			dprintk(vdev,
3989 				"control id 0x%x is read-only\n",
3990 				ctrl->id);
3991 			return -EACCES;
3992 		}
3993 		/* This test is also done in try_set_control_cluster() which
3994 		   is called in atomic context, so that has the final say,
3995 		   but it makes sense to do an up-front check as well. Once
3996 		   an error occurs in try_set_control_cluster() some other
3997 		   controls may have been set already and we want to do a
3998 		   best-effort to avoid that. */
3999 		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)) {
4000 			dprintk(vdev,
4001 				"control id 0x%x is grabbed, cannot set\n",
4002 				ctrl->id);
4003 			return -EBUSY;
4004 		}
4005 		/*
4006 		 * Skip validation for now if the payload needs to be copied
4007 		 * from userspace into kernelspace. We'll validate those later.
4008 		 */
4009 		if (ctrl->is_ptr)
4010 			continue;
4011 		if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
4012 			p_new.p_s64 = &cs->controls[i].value64;
4013 		else
4014 			p_new.p_s32 = &cs->controls[i].value;
4015 		ret = validate_new(ctrl, p_new);
4016 		if (ret)
4017 			return ret;
4018 	}
4019 	return 0;
4020 }
4021 
4022 /* Obtain the current volatile values of an autocluster and mark them
4023    as new. */
update_from_auto_cluster(struct v4l2_ctrl * master)4024 static void update_from_auto_cluster(struct v4l2_ctrl *master)
4025 {
4026 	int i;
4027 
4028 	for (i = 1; i < master->ncontrols; i++)
4029 		cur_to_new(master->cluster[i]);
4030 	if (!call_op(master, g_volatile_ctrl))
4031 		for (i = 1; i < master->ncontrols; i++)
4032 			if (master->cluster[i])
4033 				master->cluster[i]->is_new = 1;
4034 }
4035 
4036 /* 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)4037 static int try_set_ext_ctrls_common(struct v4l2_fh *fh,
4038 				    struct v4l2_ctrl_handler *hdl,
4039 				    struct v4l2_ext_controls *cs,
4040 				    struct video_device *vdev, bool set)
4041 {
4042 	struct v4l2_ctrl_helper helper[4];
4043 	struct v4l2_ctrl_helper *helpers = helper;
4044 	unsigned i, j;
4045 	int ret;
4046 
4047 	cs->error_idx = cs->count;
4048 
4049 	/* Default value cannot be changed */
4050 	if (cs->which == V4L2_CTRL_WHICH_DEF_VAL) {
4051 		dprintk(vdev, "%s: cannot change default value\n",
4052 			video_device_node_name(vdev));
4053 		return -EINVAL;
4054 	}
4055 
4056 	cs->which = V4L2_CTRL_ID2WHICH(cs->which);
4057 
4058 	if (hdl == NULL) {
4059 		dprintk(vdev, "%s: invalid null control handler\n",
4060 			video_device_node_name(vdev));
4061 		return -EINVAL;
4062 	}
4063 
4064 	if (cs->count == 0)
4065 		return class_check(hdl, cs->which);
4066 
4067 	if (cs->count > ARRAY_SIZE(helper)) {
4068 		helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
4069 					 GFP_KERNEL);
4070 		if (!helpers)
4071 			return -ENOMEM;
4072 	}
4073 	ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, false);
4074 	if (!ret)
4075 		ret = validate_ctrls(cs, helpers, vdev, set);
4076 	if (ret && set)
4077 		cs->error_idx = cs->count;
4078 	for (i = 0; !ret && i < cs->count; i++) {
4079 		struct v4l2_ctrl *master;
4080 		u32 idx = i;
4081 
4082 		if (helpers[i].mref == NULL)
4083 			continue;
4084 
4085 		cs->error_idx = i;
4086 		master = helpers[i].mref->ctrl;
4087 		v4l2_ctrl_lock(master);
4088 
4089 		/* Reset the 'is_new' flags of the cluster */
4090 		for (j = 0; j < master->ncontrols; j++)
4091 			if (master->cluster[j])
4092 				master->cluster[j]->is_new = 0;
4093 
4094 		/* For volatile autoclusters that are currently in auto mode
4095 		   we need to discover if it will be set to manual mode.
4096 		   If so, then we have to copy the current volatile values
4097 		   first since those will become the new manual values (which
4098 		   may be overwritten by explicit new values from this set
4099 		   of controls). */
4100 		if (master->is_auto && master->has_volatiles &&
4101 						!is_cur_manual(master)) {
4102 			/* Pick an initial non-manual value */
4103 			s32 new_auto_val = master->manual_mode_value + 1;
4104 			u32 tmp_idx = idx;
4105 
4106 			do {
4107 				/* Check if the auto control is part of the
4108 				   list, and remember the new value. */
4109 				if (helpers[tmp_idx].ref->ctrl == master)
4110 					new_auto_val = cs->controls[tmp_idx].value;
4111 				tmp_idx = helpers[tmp_idx].next;
4112 			} while (tmp_idx);
4113 			/* If the new value == the manual value, then copy
4114 			   the current volatile values. */
4115 			if (new_auto_val == master->manual_mode_value)
4116 				update_from_auto_cluster(master);
4117 		}
4118 
4119 		/* Copy the new caller-supplied control values.
4120 		   user_to_new() sets 'is_new' to 1. */
4121 		do {
4122 			struct v4l2_ctrl *ctrl = helpers[idx].ref->ctrl;
4123 
4124 			ret = user_to_new(cs->controls + idx, ctrl);
4125 			if (!ret && ctrl->is_ptr)
4126 				ret = validate_new(ctrl, ctrl->p_new);
4127 			idx = helpers[idx].next;
4128 		} while (!ret && idx);
4129 
4130 		if (!ret)
4131 			ret = try_or_set_cluster(fh, master,
4132 						 !hdl->req_obj.req && set, 0);
4133 		if (!ret && hdl->req_obj.req && set) {
4134 			for (j = 0; j < master->ncontrols; j++) {
4135 				struct v4l2_ctrl_ref *ref =
4136 					find_ref(hdl, master->cluster[j]->id);
4137 
4138 				new_to_req(ref);
4139 			}
4140 		}
4141 
4142 		/* Copy the new values back to userspace. */
4143 		if (!ret) {
4144 			idx = i;
4145 			do {
4146 				ret = new_to_user(cs->controls + idx,
4147 						helpers[idx].ref->ctrl);
4148 				idx = helpers[idx].next;
4149 			} while (!ret && idx);
4150 		}
4151 		v4l2_ctrl_unlock(master);
4152 	}
4153 
4154 	if (cs->count > ARRAY_SIZE(helper))
4155 		kvfree(helpers);
4156 	return ret;
4157 }
4158 
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)4159 static int try_set_ext_ctrls(struct v4l2_fh *fh,
4160 			     struct v4l2_ctrl_handler *hdl,
4161 			     struct video_device *vdev,
4162 			     struct media_device *mdev,
4163 			     struct v4l2_ext_controls *cs, bool set)
4164 {
4165 	struct media_request_object *obj = NULL;
4166 	struct media_request *req = NULL;
4167 	int ret;
4168 
4169 	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
4170 		if (!mdev) {
4171 			dprintk(vdev, "%s: missing media device\n",
4172 				video_device_node_name(vdev));
4173 			return -EINVAL;
4174 		}
4175 
4176 		if (cs->request_fd < 0) {
4177 			dprintk(vdev, "%s: invalid request fd %d\n",
4178 				video_device_node_name(vdev), cs->request_fd);
4179 			return -EINVAL;
4180 		}
4181 
4182 		req = media_request_get_by_fd(mdev, cs->request_fd);
4183 		if (IS_ERR(req)) {
4184 			dprintk(vdev, "%s: cannot find request fd %d\n",
4185 				video_device_node_name(vdev), cs->request_fd);
4186 			return PTR_ERR(req);
4187 		}
4188 
4189 		ret = media_request_lock_for_update(req);
4190 		if (ret) {
4191 			dprintk(vdev, "%s: cannot lock request fd %d\n",
4192 				video_device_node_name(vdev), cs->request_fd);
4193 			media_request_put(req);
4194 			return ret;
4195 		}
4196 
4197 		obj = v4l2_ctrls_find_req_obj(hdl, req, set);
4198 		if (IS_ERR(obj)) {
4199 			dprintk(vdev,
4200 				"%s: cannot find request object for request fd %d\n",
4201 				video_device_node_name(vdev),
4202 				cs->request_fd);
4203 			media_request_unlock_for_update(req);
4204 			media_request_put(req);
4205 			return PTR_ERR(obj);
4206 		}
4207 		hdl = container_of(obj, struct v4l2_ctrl_handler,
4208 				   req_obj);
4209 	}
4210 
4211 	ret = try_set_ext_ctrls_common(fh, hdl, cs, vdev, set);
4212 	if (ret)
4213 		dprintk(vdev,
4214 			"%s: try_set_ext_ctrls_common failed (%d)\n",
4215 			video_device_node_name(vdev), ret);
4216 
4217 	if (obj) {
4218 		media_request_unlock_for_update(req);
4219 		media_request_object_put(obj);
4220 		media_request_put(req);
4221 	}
4222 
4223 	return ret;
4224 }
4225 
v4l2_try_ext_ctrls(struct v4l2_ctrl_handler * hdl,struct video_device * vdev,struct media_device * mdev,struct v4l2_ext_controls * cs)4226 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl,
4227 		       struct video_device *vdev,
4228 		       struct media_device *mdev,
4229 		       struct v4l2_ext_controls *cs)
4230 {
4231 	return try_set_ext_ctrls(NULL, hdl, vdev, mdev, cs, false);
4232 }
4233 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
4234 
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)4235 int v4l2_s_ext_ctrls(struct v4l2_fh *fh,
4236 		     struct v4l2_ctrl_handler *hdl,
4237 		     struct video_device *vdev,
4238 		     struct media_device *mdev,
4239 		     struct v4l2_ext_controls *cs)
4240 {
4241 	return try_set_ext_ctrls(fh, hdl, vdev, mdev, cs, true);
4242 }
4243 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
4244 
4245 /* Helper function for VIDIOC_S_CTRL compatibility */
set_ctrl(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,u32 ch_flags)4246 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
4247 {
4248 	struct v4l2_ctrl *master = ctrl->cluster[0];
4249 	int ret;
4250 	int i;
4251 
4252 	/* Reset the 'is_new' flags of the cluster */
4253 	for (i = 0; i < master->ncontrols; i++)
4254 		if (master->cluster[i])
4255 			master->cluster[i]->is_new = 0;
4256 
4257 	ret = validate_new(ctrl, ctrl->p_new);
4258 	if (ret)
4259 		return ret;
4260 
4261 	/* For autoclusters with volatiles that are switched from auto to
4262 	   manual mode we have to update the current volatile values since
4263 	   those will become the initial manual values after such a switch. */
4264 	if (master->is_auto && master->has_volatiles && ctrl == master &&
4265 	    !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
4266 		update_from_auto_cluster(master);
4267 
4268 	ctrl->is_new = 1;
4269 	return try_or_set_cluster(fh, master, true, ch_flags);
4270 }
4271 
4272 /* Helper function for VIDIOC_S_CTRL compatibility */
set_ctrl_lock(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,struct v4l2_ext_control * c)4273 static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
4274 			 struct v4l2_ext_control *c)
4275 {
4276 	int ret;
4277 
4278 	v4l2_ctrl_lock(ctrl);
4279 	user_to_new(c, ctrl);
4280 	ret = set_ctrl(fh, ctrl, 0);
4281 	if (!ret)
4282 		cur_to_user(c, ctrl);
4283 	v4l2_ctrl_unlock(ctrl);
4284 	return ret;
4285 }
4286 
v4l2_s_ctrl(struct v4l2_fh * fh,struct v4l2_ctrl_handler * hdl,struct v4l2_control * control)4287 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
4288 					struct v4l2_control *control)
4289 {
4290 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
4291 	struct v4l2_ext_control c = { control->id };
4292 	int ret;
4293 
4294 	if (ctrl == NULL || !ctrl->is_int)
4295 		return -EINVAL;
4296 
4297 	if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
4298 		return -EACCES;
4299 
4300 	c.value = control->value;
4301 	ret = set_ctrl_lock(fh, ctrl, &c);
4302 	control->value = c.value;
4303 	return ret;
4304 }
4305 EXPORT_SYMBOL(v4l2_s_ctrl);
4306 
__v4l2_ctrl_s_ctrl(struct v4l2_ctrl * ctrl,s32 val)4307 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 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_int))
4313 		return -EINVAL;
4314 	ctrl->val = val;
4315 	return set_ctrl(NULL, ctrl, 0);
4316 }
4317 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
4318 
__v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl * ctrl,s64 val)4319 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
4320 {
4321 	lockdep_assert_held(ctrl->handler->lock);
4322 
4323 	/* It's a driver bug if this happens. */
4324 	if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64))
4325 		return -EINVAL;
4326 	*ctrl->p_new.p_s64 = val;
4327 	return set_ctrl(NULL, ctrl, 0);
4328 }
4329 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
4330 
__v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl * ctrl,const char * s)4331 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
4332 {
4333 	lockdep_assert_held(ctrl->handler->lock);
4334 
4335 	/* It's a driver bug if this happens. */
4336 	if (WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING))
4337 		return -EINVAL;
4338 	strscpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
4339 	return set_ctrl(NULL, ctrl, 0);
4340 }
4341 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);
4342 
__v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl * ctrl,enum v4l2_ctrl_type type,const void * p)4343 int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
4344 				enum v4l2_ctrl_type type, const void *p)
4345 {
4346 	lockdep_assert_held(ctrl->handler->lock);
4347 
4348 	/* It's a driver bug if this happens. */
4349 	if (WARN_ON(ctrl->type != type))
4350 		return -EINVAL;
4351 	memcpy(ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size);
4352 	return set_ctrl(NULL, ctrl, 0);
4353 }
4354 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_compound);
4355 
v4l2_ctrl_request_complete(struct media_request * req,struct v4l2_ctrl_handler * main_hdl)4356 void v4l2_ctrl_request_complete(struct media_request *req,
4357 				struct v4l2_ctrl_handler *main_hdl)
4358 {
4359 	struct media_request_object *obj;
4360 	struct v4l2_ctrl_handler *hdl;
4361 	struct v4l2_ctrl_ref *ref;
4362 
4363 	if (!req || !main_hdl)
4364 		return;
4365 
4366 	/*
4367 	 * Note that it is valid if nothing was found. It means
4368 	 * that this request doesn't have any controls and so just
4369 	 * wants to leave the controls unchanged.
4370 	 */
4371 	obj = media_request_object_find(req, &req_ops, main_hdl);
4372 	if (!obj)
4373 		return;
4374 	hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
4375 
4376 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
4377 		struct v4l2_ctrl *ctrl = ref->ctrl;
4378 		struct v4l2_ctrl *master = ctrl->cluster[0];
4379 		unsigned int i;
4380 
4381 		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
4382 			v4l2_ctrl_lock(master);
4383 			/* g_volatile_ctrl will update the current control values */
4384 			for (i = 0; i < master->ncontrols; i++)
4385 				cur_to_new(master->cluster[i]);
4386 			call_op(master, g_volatile_ctrl);
4387 			new_to_req(ref);
4388 			v4l2_ctrl_unlock(master);
4389 			continue;
4390 		}
4391 		if (ref->valid_p_req)
4392 			continue;
4393 
4394 		/* Copy the current control value into the request */
4395 		v4l2_ctrl_lock(ctrl);
4396 		cur_to_req(ref);
4397 		v4l2_ctrl_unlock(ctrl);
4398 	}
4399 
4400 	mutex_lock(main_hdl->lock);
4401 	WARN_ON(!hdl->request_is_queued);
4402 	list_del_init(&hdl->requests_queued);
4403 	hdl->request_is_queued = false;
4404 	mutex_unlock(main_hdl->lock);
4405 	media_request_object_complete(obj);
4406 	media_request_object_put(obj);
4407 }
4408 EXPORT_SYMBOL(v4l2_ctrl_request_complete);
4409 
v4l2_ctrl_request_setup(struct media_request * req,struct v4l2_ctrl_handler * main_hdl)4410 int v4l2_ctrl_request_setup(struct media_request *req,
4411 			     struct v4l2_ctrl_handler *main_hdl)
4412 {
4413 	struct media_request_object *obj;
4414 	struct v4l2_ctrl_handler *hdl;
4415 	struct v4l2_ctrl_ref *ref;
4416 	int ret = 0;
4417 
4418 	if (!req || !main_hdl)
4419 		return 0;
4420 
4421 	if (WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
4422 		return -EBUSY;
4423 
4424 	/*
4425 	 * Note that it is valid if nothing was found. It means
4426 	 * that this request doesn't have any controls and so just
4427 	 * wants to leave the controls unchanged.
4428 	 */
4429 	obj = media_request_object_find(req, &req_ops, main_hdl);
4430 	if (!obj)
4431 		return 0;
4432 	if (obj->completed) {
4433 		media_request_object_put(obj);
4434 		return -EBUSY;
4435 	}
4436 	hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
4437 
4438 	list_for_each_entry(ref, &hdl->ctrl_refs, node)
4439 		ref->req_done = false;
4440 
4441 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
4442 		struct v4l2_ctrl *ctrl = ref->ctrl;
4443 		struct v4l2_ctrl *master = ctrl->cluster[0];
4444 		bool have_new_data = false;
4445 		int i;
4446 
4447 		/*
4448 		 * Skip if this control was already handled by a cluster.
4449 		 * Skip button controls and read-only controls.
4450 		 */
4451 		if (ref->req_done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
4452 		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
4453 			continue;
4454 
4455 		v4l2_ctrl_lock(master);
4456 		for (i = 0; i < master->ncontrols; i++) {
4457 			if (master->cluster[i]) {
4458 				struct v4l2_ctrl_ref *r =
4459 					find_ref(hdl, master->cluster[i]->id);
4460 
4461 				if (r->valid_p_req) {
4462 					have_new_data = true;
4463 					break;
4464 				}
4465 			}
4466 		}
4467 		if (!have_new_data) {
4468 			v4l2_ctrl_unlock(master);
4469 			continue;
4470 		}
4471 
4472 		for (i = 0; i < master->ncontrols; i++) {
4473 			if (master->cluster[i]) {
4474 				struct v4l2_ctrl_ref *r =
4475 					find_ref(hdl, master->cluster[i]->id);
4476 
4477 				req_to_new(r);
4478 				master->cluster[i]->is_new = 1;
4479 				r->req_done = true;
4480 			}
4481 		}
4482 		/*
4483 		 * For volatile autoclusters that are currently in auto mode
4484 		 * we need to discover if it will be set to manual mode.
4485 		 * If so, then we have to copy the current volatile values
4486 		 * first since those will become the new manual values (which
4487 		 * may be overwritten by explicit new values from this set
4488 		 * of controls).
4489 		 */
4490 		if (master->is_auto && master->has_volatiles &&
4491 		    !is_cur_manual(master)) {
4492 			s32 new_auto_val = *master->p_new.p_s32;
4493 
4494 			/*
4495 			 * If the new value == the manual value, then copy
4496 			 * the current volatile values.
4497 			 */
4498 			if (new_auto_val == master->manual_mode_value)
4499 				update_from_auto_cluster(master);
4500 		}
4501 
4502 		ret = try_or_set_cluster(NULL, master, true, 0);
4503 		v4l2_ctrl_unlock(master);
4504 
4505 		if (ret)
4506 			break;
4507 	}
4508 
4509 	media_request_object_put(obj);
4510 	return ret;
4511 }
4512 EXPORT_SYMBOL(v4l2_ctrl_request_setup);
4513 
v4l2_ctrl_notify(struct v4l2_ctrl * ctrl,v4l2_ctrl_notify_fnc notify,void * priv)4514 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
4515 {
4516 	if (ctrl == NULL)
4517 		return;
4518 	if (notify == NULL) {
4519 		ctrl->call_notify = 0;
4520 		return;
4521 	}
4522 	if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
4523 		return;
4524 	ctrl->handler->notify = notify;
4525 	ctrl->handler->notify_priv = priv;
4526 	ctrl->call_notify = 1;
4527 }
4528 EXPORT_SYMBOL(v4l2_ctrl_notify);
4529 
__v4l2_ctrl_modify_range(struct v4l2_ctrl * ctrl,s64 min,s64 max,u64 step,s64 def)4530 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
4531 			s64 min, s64 max, u64 step, s64 def)
4532 {
4533 	bool value_changed;
4534 	bool range_changed = false;
4535 	int ret;
4536 
4537 	lockdep_assert_held(ctrl->handler->lock);
4538 
4539 	switch (ctrl->type) {
4540 	case V4L2_CTRL_TYPE_INTEGER:
4541 	case V4L2_CTRL_TYPE_INTEGER64:
4542 	case V4L2_CTRL_TYPE_BOOLEAN:
4543 	case V4L2_CTRL_TYPE_MENU:
4544 	case V4L2_CTRL_TYPE_INTEGER_MENU:
4545 	case V4L2_CTRL_TYPE_BITMASK:
4546 	case V4L2_CTRL_TYPE_U8:
4547 	case V4L2_CTRL_TYPE_U16:
4548 	case V4L2_CTRL_TYPE_U32:
4549 		if (ctrl->is_array)
4550 			return -EINVAL;
4551 		ret = check_range(ctrl->type, min, max, step, def);
4552 		if (ret)
4553 			return ret;
4554 		break;
4555 	default:
4556 		return -EINVAL;
4557 	}
4558 	if ((ctrl->minimum != min) || (ctrl->maximum != max) ||
4559 		(ctrl->step != step) || ctrl->default_value != def) {
4560 		range_changed = true;
4561 		ctrl->minimum = min;
4562 		ctrl->maximum = max;
4563 		ctrl->step = step;
4564 		ctrl->default_value = def;
4565 	}
4566 	cur_to_new(ctrl);
4567 	if (validate_new(ctrl, ctrl->p_new)) {
4568 		if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
4569 			*ctrl->p_new.p_s64 = def;
4570 		else
4571 			*ctrl->p_new.p_s32 = def;
4572 	}
4573 
4574 	if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
4575 		value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
4576 	else
4577 		value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
4578 	if (value_changed)
4579 		ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
4580 	else if (range_changed)
4581 		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
4582 	return ret;
4583 }
4584 EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
4585 
v4l2_ctrl_add_event(struct v4l2_subscribed_event * sev,unsigned elems)4586 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
4587 {
4588 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
4589 
4590 	if (ctrl == NULL)
4591 		return -EINVAL;
4592 
4593 	v4l2_ctrl_lock(ctrl);
4594 	list_add_tail(&sev->node, &ctrl->ev_subs);
4595 	if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
4596 	    (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
4597 		struct v4l2_event ev;
4598 		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
4599 
4600 		if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
4601 			changes |= V4L2_EVENT_CTRL_CH_VALUE;
4602 		fill_event(&ev, ctrl, changes);
4603 		/* Mark the queue as active, allowing this initial
4604 		   event to be accepted. */
4605 		sev->elems = elems;
4606 		v4l2_event_queue_fh(sev->fh, &ev);
4607 	}
4608 	v4l2_ctrl_unlock(ctrl);
4609 	return 0;
4610 }
4611 
v4l2_ctrl_del_event(struct v4l2_subscribed_event * sev)4612 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
4613 {
4614 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
4615 
4616 	if (ctrl == NULL)
4617 		return;
4618 
4619 	v4l2_ctrl_lock(ctrl);
4620 	list_del(&sev->node);
4621 	v4l2_ctrl_unlock(ctrl);
4622 }
4623 
v4l2_ctrl_replace(struct v4l2_event * old,const struct v4l2_event * new)4624 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
4625 {
4626 	u32 old_changes = old->u.ctrl.changes;
4627 
4628 	old->u.ctrl = new->u.ctrl;
4629 	old->u.ctrl.changes |= old_changes;
4630 }
4631 EXPORT_SYMBOL(v4l2_ctrl_replace);
4632 
v4l2_ctrl_merge(const struct v4l2_event * old,struct v4l2_event * new)4633 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
4634 {
4635 	new->u.ctrl.changes |= old->u.ctrl.changes;
4636 }
4637 EXPORT_SYMBOL(v4l2_ctrl_merge);
4638 
4639 const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
4640 	.add = v4l2_ctrl_add_event,
4641 	.del = v4l2_ctrl_del_event,
4642 	.replace = v4l2_ctrl_replace,
4643 	.merge = v4l2_ctrl_merge,
4644 };
4645 EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
4646 
v4l2_ctrl_log_status(struct file * file,void * fh)4647 int v4l2_ctrl_log_status(struct file *file, void *fh)
4648 {
4649 	struct video_device *vfd = video_devdata(file);
4650 	struct v4l2_fh *vfh = file->private_data;
4651 
4652 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
4653 		v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
4654 			vfd->v4l2_dev->name);
4655 	return 0;
4656 }
4657 EXPORT_SYMBOL(v4l2_ctrl_log_status);
4658 
v4l2_ctrl_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)4659 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
4660 				const struct v4l2_event_subscription *sub)
4661 {
4662 	if (sub->type == V4L2_EVENT_CTRL)
4663 		return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
4664 	return -EINVAL;
4665 }
4666 EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
4667 
v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)4668 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
4669 				     struct v4l2_event_subscription *sub)
4670 {
4671 	if (!sd->ctrl_handler)
4672 		return -EINVAL;
4673 	return v4l2_ctrl_subscribe_event(fh, sub);
4674 }
4675 EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);
4676 
v4l2_ctrl_poll(struct file * file,struct poll_table_struct * wait)4677 __poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
4678 {
4679 	struct v4l2_fh *fh = file->private_data;
4680 
4681 	poll_wait(file, &fh->wait, wait);
4682 	if (v4l2_event_pending(fh))
4683 		return EPOLLPRI;
4684 	return 0;
4685 }
4686 EXPORT_SYMBOL(v4l2_ctrl_poll);
4687 
v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ctrl_ops,const struct v4l2_fwnode_device_properties * p)4688 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,
4689 				    const struct v4l2_ctrl_ops *ctrl_ops,
4690 				    const struct v4l2_fwnode_device_properties *p)
4691 {
4692 	if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) {
4693 		u32 orientation_ctrl;
4694 
4695 		switch (p->orientation) {
4696 		case V4L2_FWNODE_ORIENTATION_FRONT:
4697 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT;
4698 			break;
4699 		case V4L2_FWNODE_ORIENTATION_BACK:
4700 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK;
4701 			break;
4702 		case V4L2_FWNODE_ORIENTATION_EXTERNAL:
4703 			orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL;
4704 			break;
4705 		default:
4706 			return -EINVAL;
4707 		}
4708 		if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops,
4709 					    V4L2_CID_CAMERA_ORIENTATION,
4710 					    V4L2_CAMERA_ORIENTATION_EXTERNAL, 0,
4711 					    orientation_ctrl))
4712 			return hdl->error;
4713 	}
4714 
4715 	if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) {
4716 		if (!v4l2_ctrl_new_std(hdl, ctrl_ops,
4717 				       V4L2_CID_CAMERA_SENSOR_ROTATION,
4718 				       p->rotation, p->rotation, 1,
4719 				       p->rotation))
4720 			return hdl->error;
4721 	}
4722 
4723 	return hdl->error;
4724 }
4725 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties);
4726