media: v4l2-ctrl: Change control for VP8 profile to menu control
[linux-block.git] / drivers / media / v4l2-core / v4l2-ctrls.c
... / ...
CommitLineData
1/*
2 V4L2 controls framework implementation.
3
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/ctype.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
24#include <linux/export.h>
25#include <media/v4l2-ioctl.h>
26#include <media/v4l2-device.h>
27#include <media/v4l2-ctrls.h>
28#include <media/v4l2-event.h>
29#include <media/v4l2-dev.h>
30
31#define has_op(master, op) \
32 (master->ops && master->ops->op)
33#define call_op(master, op) \
34 (has_op(master, op) ? master->ops->op(master) : 0)
35
36/* Internal temporary helper struct, one for each v4l2_ext_control */
37struct v4l2_ctrl_helper {
38 /* Pointer to the control reference of the master control */
39 struct v4l2_ctrl_ref *mref;
40 /* The control corresponding to the v4l2_ext_control ID field. */
41 struct v4l2_ctrl *ctrl;
42 /* v4l2_ext_control index of the next control belonging to the
43 same cluster, or 0 if there isn't any. */
44 u32 next;
45};
46
47/* Small helper function to determine if the autocluster is set to manual
48 mode. */
49static bool is_cur_manual(const struct v4l2_ctrl *master)
50{
51 return master->is_auto && master->cur.val == master->manual_mode_value;
52}
53
54/* Same as above, but this checks the against the new value instead of the
55 current value. */
56static bool is_new_manual(const struct v4l2_ctrl *master)
57{
58 return master->is_auto && master->val == master->manual_mode_value;
59}
60
61/* Returns NULL or a character pointer array containing the menu for
62 the given control ID. The pointer array ends with a NULL pointer.
63 An empty string signifies a menu entry that is invalid. This allows
64 drivers to disable certain options if it is not supported. */
65const char * const *v4l2_ctrl_get_menu(u32 id)
66{
67 static const char * const mpeg_audio_sampling_freq[] = {
68 "44.1 kHz",
69 "48 kHz",
70 "32 kHz",
71 NULL
72 };
73 static const char * const mpeg_audio_encoding[] = {
74 "MPEG-1/2 Layer I",
75 "MPEG-1/2 Layer II",
76 "MPEG-1/2 Layer III",
77 "MPEG-2/4 AAC",
78 "AC-3",
79 NULL
80 };
81 static const char * const mpeg_audio_l1_bitrate[] = {
82 "32 kbps",
83 "64 kbps",
84 "96 kbps",
85 "128 kbps",
86 "160 kbps",
87 "192 kbps",
88 "224 kbps",
89 "256 kbps",
90 "288 kbps",
91 "320 kbps",
92 "352 kbps",
93 "384 kbps",
94 "416 kbps",
95 "448 kbps",
96 NULL
97 };
98 static const char * const mpeg_audio_l2_bitrate[] = {
99 "32 kbps",
100 "48 kbps",
101 "56 kbps",
102 "64 kbps",
103 "80 kbps",
104 "96 kbps",
105 "112 kbps",
106 "128 kbps",
107 "160 kbps",
108 "192 kbps",
109 "224 kbps",
110 "256 kbps",
111 "320 kbps",
112 "384 kbps",
113 NULL
114 };
115 static const char * const mpeg_audio_l3_bitrate[] = {
116 "32 kbps",
117 "40 kbps",
118 "48 kbps",
119 "56 kbps",
120 "64 kbps",
121 "80 kbps",
122 "96 kbps",
123 "112 kbps",
124 "128 kbps",
125 "160 kbps",
126 "192 kbps",
127 "224 kbps",
128 "256 kbps",
129 "320 kbps",
130 NULL
131 };
132 static const char * const mpeg_audio_ac3_bitrate[] = {
133 "32 kbps",
134 "40 kbps",
135 "48 kbps",
136 "56 kbps",
137 "64 kbps",
138 "80 kbps",
139 "96 kbps",
140 "112 kbps",
141 "128 kbps",
142 "160 kbps",
143 "192 kbps",
144 "224 kbps",
145 "256 kbps",
146 "320 kbps",
147 "384 kbps",
148 "448 kbps",
149 "512 kbps",
150 "576 kbps",
151 "640 kbps",
152 NULL
153 };
154 static const char * const mpeg_audio_mode[] = {
155 "Stereo",
156 "Joint Stereo",
157 "Dual",
158 "Mono",
159 NULL
160 };
161 static const char * const mpeg_audio_mode_extension[] = {
162 "Bound 4",
163 "Bound 8",
164 "Bound 12",
165 "Bound 16",
166 NULL
167 };
168 static const char * const mpeg_audio_emphasis[] = {
169 "No Emphasis",
170 "50/15 us",
171 "CCITT J17",
172 NULL
173 };
174 static const char * const mpeg_audio_crc[] = {
175 "No CRC",
176 "16-bit CRC",
177 NULL
178 };
179 static const char * const mpeg_audio_dec_playback[] = {
180 "Auto",
181 "Stereo",
182 "Left",
183 "Right",
184 "Mono",
185 "Swapped Stereo",
186 NULL
187 };
188 static const char * const mpeg_video_encoding[] = {
189 "MPEG-1",
190 "MPEG-2",
191 "MPEG-4 AVC",
192 NULL
193 };
194 static const char * const mpeg_video_aspect[] = {
195 "1x1",
196 "4x3",
197 "16x9",
198 "2.21x1",
199 NULL
200 };
201 static const char * const mpeg_video_bitrate_mode[] = {
202 "Variable Bitrate",
203 "Constant Bitrate",
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 NULL,
342 };
343 static const char * const h264_loop_filter[] = {
344 "Enabled",
345 "Disabled",
346 "Disabled at Slice Boundary",
347 NULL,
348 };
349 static const char * const h264_profile[] = {
350 "Baseline",
351 "Constrained Baseline",
352 "Main",
353 "Extended",
354 "High",
355 "High 10",
356 "High 422",
357 "High 444 Predictive",
358 "High 10 Intra",
359 "High 422 Intra",
360 "High 444 Intra",
361 "CAVLC 444 Intra",
362 "Scalable Baseline",
363 "Scalable High",
364 "Scalable High Intra",
365 "Stereo High",
366 "Multiview High",
367 NULL,
368 };
369 static const char * const vui_sar_idc[] = {
370 "Unspecified",
371 "1:1",
372 "12:11",
373 "10:11",
374 "16:11",
375 "40:33",
376 "24:11",
377 "20:11",
378 "32:11",
379 "80:33",
380 "18:11",
381 "15:11",
382 "64:33",
383 "160:99",
384 "4:3",
385 "3:2",
386 "2:1",
387 "Extended SAR",
388 NULL,
389 };
390 static const char * const h264_fp_arrangement_type[] = {
391 "Checkerboard",
392 "Column",
393 "Row",
394 "Side by Side",
395 "Top Bottom",
396 "Temporal",
397 NULL,
398 };
399 static const char * const h264_fmo_map_type[] = {
400 "Interleaved Slices",
401 "Scattered Slices",
402 "Foreground with Leftover",
403 "Box Out",
404 "Raster Scan",
405 "Wipe Scan",
406 "Explicit",
407 NULL,
408 };
409 static const char * const mpeg_mpeg4_level[] = {
410 "0",
411 "0b",
412 "1",
413 "2",
414 "3",
415 "3b",
416 "4",
417 "5",
418 NULL,
419 };
420 static const char * const mpeg4_profile[] = {
421 "Simple",
422 "Advanced Simple",
423 "Core",
424 "Simple Scalable",
425 "Advanced Coding Efficiency",
426 NULL,
427 };
428
429 static const char * const vpx_golden_frame_sel[] = {
430 "Use Previous Frame",
431 "Use Previous Specific Frame",
432 NULL,
433 };
434 static const char * const vp8_profile[] = {
435 "0",
436 "1",
437 "2",
438 "3",
439 NULL,
440 };
441
442 static const char * const flash_led_mode[] = {
443 "Off",
444 "Flash",
445 "Torch",
446 NULL,
447 };
448 static const char * const flash_strobe_source[] = {
449 "Software",
450 "External",
451 NULL,
452 };
453
454 static const char * const jpeg_chroma_subsampling[] = {
455 "4:4:4",
456 "4:2:2",
457 "4:2:0",
458 "4:1:1",
459 "4:1:0",
460 "Gray",
461 NULL,
462 };
463 static const char * const dv_tx_mode[] = {
464 "DVI-D",
465 "HDMI",
466 NULL,
467 };
468 static const char * const dv_rgb_range[] = {
469 "Automatic",
470 "RGB Limited Range (16-235)",
471 "RGB Full Range (0-255)",
472 NULL,
473 };
474 static const char * const dv_it_content_type[] = {
475 "Graphics",
476 "Photo",
477 "Cinema",
478 "Game",
479 "No IT Content",
480 NULL,
481 };
482 static const char * const detect_md_mode[] = {
483 "Disabled",
484 "Global",
485 "Threshold Grid",
486 "Region Grid",
487 NULL,
488 };
489
490 static const char * const hevc_profile[] = {
491 "Main",
492 "Main Still Picture",
493 "Main 10",
494 NULL,
495 };
496 static const char * const hevc_level[] = {
497 "1",
498 "2",
499 "2.1",
500 "3",
501 "3.1",
502 "4",
503 "4.1",
504 "5",
505 "5.1",
506 "5.2",
507 "6",
508 "6.1",
509 "6.2",
510 NULL,
511 };
512 static const char * const hevc_hierarchial_coding_type[] = {
513 "B",
514 "P",
515 NULL,
516 };
517 static const char * const hevc_refresh_type[] = {
518 "None",
519 "CRA",
520 "IDR",
521 NULL,
522 };
523 static const char * const hevc_size_of_length_field[] = {
524 "0",
525 "1",
526 "2",
527 "4",
528 NULL,
529 };
530 static const char * const hevc_tier[] = {
531 "Main",
532 "High",
533 NULL,
534 };
535 static const char * const hevc_loop_filter_mode[] = {
536 "Disabled",
537 "Enabled",
538 "Disabled at slice boundary",
539 "NULL",
540 };
541
542 switch (id) {
543 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
544 return mpeg_audio_sampling_freq;
545 case V4L2_CID_MPEG_AUDIO_ENCODING:
546 return mpeg_audio_encoding;
547 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
548 return mpeg_audio_l1_bitrate;
549 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
550 return mpeg_audio_l2_bitrate;
551 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
552 return mpeg_audio_l3_bitrate;
553 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
554 return mpeg_audio_ac3_bitrate;
555 case V4L2_CID_MPEG_AUDIO_MODE:
556 return mpeg_audio_mode;
557 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
558 return mpeg_audio_mode_extension;
559 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
560 return mpeg_audio_emphasis;
561 case V4L2_CID_MPEG_AUDIO_CRC:
562 return mpeg_audio_crc;
563 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
564 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
565 return mpeg_audio_dec_playback;
566 case V4L2_CID_MPEG_VIDEO_ENCODING:
567 return mpeg_video_encoding;
568 case V4L2_CID_MPEG_VIDEO_ASPECT:
569 return mpeg_video_aspect;
570 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
571 return mpeg_video_bitrate_mode;
572 case V4L2_CID_MPEG_STREAM_TYPE:
573 return mpeg_stream_type;
574 case V4L2_CID_MPEG_STREAM_VBI_FMT:
575 return mpeg_stream_vbi_fmt;
576 case V4L2_CID_POWER_LINE_FREQUENCY:
577 return camera_power_line_frequency;
578 case V4L2_CID_EXPOSURE_AUTO:
579 return camera_exposure_auto;
580 case V4L2_CID_EXPOSURE_METERING:
581 return camera_exposure_metering;
582 case V4L2_CID_AUTO_FOCUS_RANGE:
583 return camera_auto_focus_range;
584 case V4L2_CID_COLORFX:
585 return colorfx;
586 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
587 return auto_n_preset_white_balance;
588 case V4L2_CID_ISO_SENSITIVITY_AUTO:
589 return camera_iso_sensitivity_auto;
590 case V4L2_CID_SCENE_MODE:
591 return scene_mode;
592 case V4L2_CID_TUNE_PREEMPHASIS:
593 return tune_emphasis;
594 case V4L2_CID_TUNE_DEEMPHASIS:
595 return tune_emphasis;
596 case V4L2_CID_FLASH_LED_MODE:
597 return flash_led_mode;
598 case V4L2_CID_FLASH_STROBE_SOURCE:
599 return flash_strobe_source;
600 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
601 return header_mode;
602 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
603 return multi_slice;
604 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
605 return entropy_mode;
606 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
607 return mpeg_h264_level;
608 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
609 return h264_loop_filter;
610 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
611 return h264_profile;
612 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
613 return vui_sar_idc;
614 case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
615 return h264_fp_arrangement_type;
616 case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
617 return h264_fmo_map_type;
618 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
619 return mpeg_mpeg4_level;
620 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
621 return mpeg4_profile;
622 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
623 return vpx_golden_frame_sel;
624 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
625 return vp8_profile;
626 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
627 return jpeg_chroma_subsampling;
628 case V4L2_CID_DV_TX_MODE:
629 return dv_tx_mode;
630 case V4L2_CID_DV_TX_RGB_RANGE:
631 case V4L2_CID_DV_RX_RGB_RANGE:
632 return dv_rgb_range;
633 case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
634 case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
635 return dv_it_content_type;
636 case V4L2_CID_DETECT_MD_MODE:
637 return detect_md_mode;
638 case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
639 return hevc_profile;
640 case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
641 return hevc_level;
642 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
643 return hevc_hierarchial_coding_type;
644 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
645 return hevc_refresh_type;
646 case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
647 return hevc_size_of_length_field;
648 case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
649 return hevc_tier;
650 case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
651 return hevc_loop_filter_mode;
652
653 default:
654 return NULL;
655 }
656}
657EXPORT_SYMBOL(v4l2_ctrl_get_menu);
658
659#define __v4l2_qmenu_int_len(arr, len) ({ *(len) = ARRAY_SIZE(arr); arr; })
660/*
661 * Returns NULL or an s64 type array containing the menu for given
662 * control ID. The total number of the menu items is returned in @len.
663 */
664const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len)
665{
666 static const s64 qmenu_int_vpx_num_partitions[] = {
667 1, 2, 4, 8,
668 };
669
670 static const s64 qmenu_int_vpx_num_ref_frames[] = {
671 1, 2, 3,
672 };
673
674 switch (id) {
675 case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
676 return __v4l2_qmenu_int_len(qmenu_int_vpx_num_partitions, len);
677 case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
678 return __v4l2_qmenu_int_len(qmenu_int_vpx_num_ref_frames, len);
679 default:
680 *len = 0;
681 return NULL;
682 }
683}
684EXPORT_SYMBOL(v4l2_ctrl_get_int_menu);
685
686/* Return the control name. */
687const char *v4l2_ctrl_get_name(u32 id)
688{
689 switch (id) {
690 /* USER controls */
691 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
692 case V4L2_CID_USER_CLASS: return "User Controls";
693 case V4L2_CID_BRIGHTNESS: return "Brightness";
694 case V4L2_CID_CONTRAST: return "Contrast";
695 case V4L2_CID_SATURATION: return "Saturation";
696 case V4L2_CID_HUE: return "Hue";
697 case V4L2_CID_AUDIO_VOLUME: return "Volume";
698 case V4L2_CID_AUDIO_BALANCE: return "Balance";
699 case V4L2_CID_AUDIO_BASS: return "Bass";
700 case V4L2_CID_AUDIO_TREBLE: return "Treble";
701 case V4L2_CID_AUDIO_MUTE: return "Mute";
702 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
703 case V4L2_CID_BLACK_LEVEL: return "Black Level";
704 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
705 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
706 case V4L2_CID_RED_BALANCE: return "Red Balance";
707 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
708 case V4L2_CID_GAMMA: return "Gamma";
709 case V4L2_CID_EXPOSURE: return "Exposure";
710 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
711 case V4L2_CID_GAIN: return "Gain";
712 case V4L2_CID_HFLIP: return "Horizontal Flip";
713 case V4L2_CID_VFLIP: return "Vertical Flip";
714 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
715 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
716 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
717 case V4L2_CID_SHARPNESS: return "Sharpness";
718 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
719 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
720 case V4L2_CID_COLOR_KILLER: return "Color Killer";
721 case V4L2_CID_COLORFX: return "Color Effects";
722 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
723 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
724 case V4L2_CID_ROTATE: return "Rotate";
725 case V4L2_CID_BG_COLOR: return "Background Color";
726 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
727 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
728 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
729 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: return "Min Number of Capture Buffers";
730 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: return "Min Number of Output Buffers";
731 case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component";
732 case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr";
733
734 /* Codec controls */
735 /* The MPEG controls are applicable to all codec controls
736 * and the 'MPEG' part of the define is historical */
737 /* Keep the order of the 'case's the same as in videodev2.h! */
738 case V4L2_CID_MPEG_CLASS: return "Codec Controls";
739 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
740 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
741 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
742 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
743 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
744 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
745 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
746 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
747 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
748 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
749 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
750 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
751 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
752 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
753 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
754 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
755 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
756 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
757 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
758 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
759 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK: return "Audio Playback";
760 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
761 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
762 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
763 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
764 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
765 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
766 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
767 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
768 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
769 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
770 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
771 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
772 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
773 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE: return "Decoder Slice Interface";
774 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER: return "MPEG4 Loop Filter Enable";
775 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: return "Number of Intra Refresh MBs";
776 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: return "Frame Level Rate Control Enable";
777 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE: return "H264 MB Level Rate Control";
778 case V4L2_CID_MPEG_VIDEO_HEADER_MODE: return "Sequence Header Mode";
779 case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC: return "Max Number of Reference Pics";
780 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP: return "H263 I-Frame QP Value";
781 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP: return "H263 P-Frame QP Value";
782 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP: return "H263 B-Frame QP Value";
783 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP: return "H263 Minimum QP Value";
784 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP: return "H263 Maximum QP Value";
785 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: return "H264 I-Frame QP Value";
786 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: return "H264 P-Frame QP Value";
787 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP: return "H264 B-Frame QP Value";
788 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: return "H264 Maximum QP Value";
789 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: return "H264 Minimum QP Value";
790 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM: return "H264 8x8 Transform Enable";
791 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE: return "H264 CPB Buffer Size";
792 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: return "H264 Entropy Mode";
793 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD: return "H264 I-Frame Period";
794 case V4L2_CID_MPEG_VIDEO_H264_LEVEL: return "H264 Level";
795 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA: return "H264 Loop Filter Alpha Offset";
796 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA: return "H264 Loop Filter Beta Offset";
797 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: return "H264 Loop Filter Mode";
798 case V4L2_CID_MPEG_VIDEO_H264_PROFILE: return "H264 Profile";
799 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT: return "Vertical Size of SAR";
800 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH: return "Horizontal Size of SAR";
801 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE: return "Aspect Ratio VUI Enable";
802 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC: return "VUI Aspect Ratio IDC";
803 case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING: return "H264 Enable Frame Packing SEI";
804 case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_CURRENT_FRAME_0: return "H264 Set Curr. Frame as Frame0";
805 case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE: return "H264 FP Arrangement Type";
806 case V4L2_CID_MPEG_VIDEO_H264_FMO: return "H264 Flexible MB Ordering";
807 case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE: return "H264 Map Type for FMO";
808 case V4L2_CID_MPEG_VIDEO_H264_FMO_SLICE_GROUP: return "H264 FMO Number of Slice Groups";
809 case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_DIRECTION: return "H264 FMO Direction of Change";
810 case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_RATE: return "H264 FMO Size of 1st Slice Grp";
811 case V4L2_CID_MPEG_VIDEO_H264_FMO_RUN_LENGTH: return "H264 FMO No. of Consecutive MBs";
812 case V4L2_CID_MPEG_VIDEO_H264_ASO: return "H264 Arbitrary Slice Ordering";
813 case V4L2_CID_MPEG_VIDEO_H264_ASO_SLICE_ORDER: return "H264 ASO Slice Order";
814 case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING: return "Enable H264 Hierarchical Coding";
815 case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE: return "H264 Hierarchical Coding Type";
816 case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER:return "H264 Number of HC Layers";
817 case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER_QP:
818 return "H264 Set QP Value for HC Layers";
819 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: return "MPEG4 I-Frame QP Value";
820 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: return "MPEG4 P-Frame QP Value";
821 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP: return "MPEG4 B-Frame QP Value";
822 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP: return "MPEG4 Minimum QP Value";
823 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP: return "MPEG4 Maximum QP Value";
824 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL: return "MPEG4 Level";
825 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE: return "MPEG4 Profile";
826 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL: return "Quarter Pixel Search Enable";
827 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: return "Maximum Bytes in a Slice";
828 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: return "Number of MBs in a Slice";
829 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: return "Slice Partitioning Method";
830 case V4L2_CID_MPEG_VIDEO_VBV_SIZE: return "VBV Buffer Size";
831 case V4L2_CID_MPEG_VIDEO_DEC_PTS: return "Video Decoder PTS";
832 case V4L2_CID_MPEG_VIDEO_DEC_FRAME: return "Video Decoder Frame Count";
833 case V4L2_CID_MPEG_VIDEO_VBV_DELAY: return "Initial Delay for VBV Control";
834 case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE: return "Horizontal MV Search Range";
835 case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE: return "Vertical MV Search Range";
836 case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER: return "Repeat Sequence Header";
837 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME: return "Force Key Frame";
838
839 /* VPX controls */
840 case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS: return "VPX Number of Partitions";
841 case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4: return "VPX Intra Mode Decision Disable";
842 case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES: return "VPX No. of Refs for P Frame";
843 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL: return "VPX Loop Filter Level Range";
844 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS: return "VPX Deblocking Effect Control";
845 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD: return "VPX Golden Frame Refresh Period";
846 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL: return "VPX Golden Frame Indicator";
847 case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP: return "VPX Minimum QP Value";
848 case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP: return "VPX Maximum QP Value";
849 case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP: return "VPX I-Frame QP Value";
850 case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP: return "VPX P-Frame QP Value";
851 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE: return "VP8 Profile";
852
853 /* HEVC controls */
854 case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP: return "HEVC I-Frame QP Value";
855 case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP: return "HEVC P-Frame QP Value";
856 case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP: return "HEVC B-Frame QP Value";
857 case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP: return "HEVC Minimum QP Value";
858 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP: return "HEVC Maximum QP Value";
859 case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE: return "HEVC Profile";
860 case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL: return "HEVC Level";
861 case V4L2_CID_MPEG_VIDEO_HEVC_TIER: return "HEVC Tier";
862 case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION: return "HEVC Frame Rate Resolution";
863 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH: return "HEVC Maximum Coding Unit Depth";
864 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE: return "HEVC Refresh Type";
865 case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED: return "HEVC Constant Intra Prediction";
866 case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU: return "HEVC Lossless Encoding";
867 case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT: return "HEVC Wavefront";
868 case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE: return "HEVC Loop Filter";
869 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP: return "HEVC QP Values";
870 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE: return "HEVC Hierarchical Coding Type";
871 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER: return "HEVC Hierarchical Coding Layer";
872 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP: return "HEVC Hierarchical Layer 0 QP";
873 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP: return "HEVC Hierarchical Layer 1 QP";
874 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP: return "HEVC Hierarchical Layer 2 QP";
875 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP: return "HEVC Hierarchical Layer 3 QP";
876 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP: return "HEVC Hierarchical Layer 4 QP";
877 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP: return "HEVC Hierarchical Layer 5 QP";
878 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP: return "HEVC Hierarchical Layer 6 QP";
879 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR: return "HEVC Hierarchical Lay 0 BitRate";
880 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR: return "HEVC Hierarchical Lay 1 BitRate";
881 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR: return "HEVC Hierarchical Lay 2 BitRate";
882 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR: return "HEVC Hierarchical Lay 3 BitRate";
883 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR: return "HEVC Hierarchical Lay 4 BitRate";
884 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR: return "HEVC Hierarchical Lay 5 BitRate";
885 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR: return "HEVC Hierarchical Lay 6 BitRate";
886 case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB: return "HEVC General PB";
887 case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID: return "HEVC Temporal ID";
888 case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING: return "HEVC Strong Intra Smoothing";
889 case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT: return "HEVC Intra PU Split";
890 case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION: return "HEVC TMV Prediction";
891 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1: return "HEVC Max Num of Candidate MVs";
892 case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE: return "HEVC ENC Without Startcode";
893 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD: return "HEVC Num of I-Frame b/w 2 IDR";
894 case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2: return "HEVC Loop Filter Beta Offset";
895 case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2: return "HEVC Loop Filter TC Offset";
896 case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD: return "HEVC Size of Length Field";
897 case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES: return "Reference Frames for a P-Frame";
898 case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR: return "Prepend SPS and PPS to IDR";
899
900 /* CAMERA controls */
901 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
902 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
903 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
904 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
905 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
906 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
907 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
908 case V4L2_CID_PAN_RESET: return "Pan, Reset";
909 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
910 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
911 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
912 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
913 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
914 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic Continuous";
915 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
916 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
917 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
918 case V4L2_CID_PRIVACY: return "Privacy";
919 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
920 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
921 case V4L2_CID_AUTO_EXPOSURE_BIAS: return "Auto Exposure, Bias";
922 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
923 case V4L2_CID_WIDE_DYNAMIC_RANGE: return "Wide Dynamic Range";
924 case V4L2_CID_IMAGE_STABILIZATION: return "Image Stabilization";
925 case V4L2_CID_ISO_SENSITIVITY: return "ISO Sensitivity";
926 case V4L2_CID_ISO_SENSITIVITY_AUTO: return "ISO Sensitivity, Auto";
927 case V4L2_CID_EXPOSURE_METERING: return "Exposure, Metering Mode";
928 case V4L2_CID_SCENE_MODE: return "Scene Mode";
929 case V4L2_CID_3A_LOCK: return "3A Lock";
930 case V4L2_CID_AUTO_FOCUS_START: return "Auto Focus, Start";
931 case V4L2_CID_AUTO_FOCUS_STOP: return "Auto Focus, Stop";
932 case V4L2_CID_AUTO_FOCUS_STATUS: return "Auto Focus, Status";
933 case V4L2_CID_AUTO_FOCUS_RANGE: return "Auto Focus, Range";
934 case V4L2_CID_PAN_SPEED: return "Pan, Speed";
935 case V4L2_CID_TILT_SPEED: return "Tilt, Speed";
936
937 /* FM Radio Modulator controls */
938 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
939 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
940 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
941 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
942 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
943 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
944 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
945 case V4L2_CID_RDS_TX_MONO_STEREO: return "RDS Stereo";
946 case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD: return "RDS Artificial Head";
947 case V4L2_CID_RDS_TX_COMPRESSED: return "RDS Compressed";
948 case V4L2_CID_RDS_TX_DYNAMIC_PTY: return "RDS Dynamic PTY";
949 case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
950 case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM: return "RDS Traffic Program";
951 case V4L2_CID_RDS_TX_MUSIC_SPEECH: return "RDS Music";
952 case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE: return "RDS Enable Alt Frequencies";
953 case V4L2_CID_RDS_TX_ALT_FREQS: return "RDS Alternate Frequencies";
954 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
955 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
956 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
957 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
958 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
959 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
960 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
961 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
962 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
963 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
964 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
965 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-Emphasis";
966 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
967 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
968
969 /* Flash controls */
970 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
971 case V4L2_CID_FLASH_CLASS: return "Flash Controls";
972 case V4L2_CID_FLASH_LED_MODE: return "LED Mode";
973 case V4L2_CID_FLASH_STROBE_SOURCE: return "Strobe Source";
974 case V4L2_CID_FLASH_STROBE: return "Strobe";
975 case V4L2_CID_FLASH_STROBE_STOP: return "Stop Strobe";
976 case V4L2_CID_FLASH_STROBE_STATUS: return "Strobe Status";
977 case V4L2_CID_FLASH_TIMEOUT: return "Strobe Timeout";
978 case V4L2_CID_FLASH_INTENSITY: return "Intensity, Flash Mode";
979 case V4L2_CID_FLASH_TORCH_INTENSITY: return "Intensity, Torch Mode";
980 case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
981 case V4L2_CID_FLASH_FAULT: return "Faults";
982 case V4L2_CID_FLASH_CHARGE: return "Charge";
983 case V4L2_CID_FLASH_READY: return "Ready to Strobe";
984
985 /* JPEG encoder controls */
986 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
987 case V4L2_CID_JPEG_CLASS: return "JPEG Compression Controls";
988 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING: return "Chroma Subsampling";
989 case V4L2_CID_JPEG_RESTART_INTERVAL: return "Restart Interval";
990 case V4L2_CID_JPEG_COMPRESSION_QUALITY: return "Compression Quality";
991 case V4L2_CID_JPEG_ACTIVE_MARKER: return "Active Markers";
992
993 /* Image source controls */
994 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
995 case V4L2_CID_IMAGE_SOURCE_CLASS: return "Image Source Controls";
996 case V4L2_CID_VBLANK: return "Vertical Blanking";
997 case V4L2_CID_HBLANK: return "Horizontal Blanking";
998 case V4L2_CID_ANALOGUE_GAIN: return "Analogue Gain";
999 case V4L2_CID_TEST_PATTERN_RED: return "Red Pixel Value";
1000 case V4L2_CID_TEST_PATTERN_GREENR: return "Green (Red) Pixel Value";
1001 case V4L2_CID_TEST_PATTERN_BLUE: return "Blue Pixel Value";
1002 case V4L2_CID_TEST_PATTERN_GREENB: return "Green (Blue) Pixel Value";
1003
1004 /* Image processing controls */
1005 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1006 case V4L2_CID_IMAGE_PROC_CLASS: return "Image Processing Controls";
1007 case V4L2_CID_LINK_FREQ: return "Link Frequency";
1008 case V4L2_CID_PIXEL_RATE: return "Pixel Rate";
1009 case V4L2_CID_TEST_PATTERN: return "Test Pattern";
1010 case V4L2_CID_DEINTERLACING_MODE: return "Deinterlacing Mode";
1011 case V4L2_CID_DIGITAL_GAIN: return "Digital Gain";
1012
1013 /* DV controls */
1014 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1015 case V4L2_CID_DV_CLASS: return "Digital Video Controls";
1016 case V4L2_CID_DV_TX_HOTPLUG: return "Hotplug Present";
1017 case V4L2_CID_DV_TX_RXSENSE: return "RxSense Present";
1018 case V4L2_CID_DV_TX_EDID_PRESENT: return "EDID Present";
1019 case V4L2_CID_DV_TX_MODE: return "Transmit Mode";
1020 case V4L2_CID_DV_TX_RGB_RANGE: return "Tx RGB Quantization Range";
1021 case V4L2_CID_DV_TX_IT_CONTENT_TYPE: return "Tx IT Content Type";
1022 case V4L2_CID_DV_RX_POWER_PRESENT: return "Power Present";
1023 case V4L2_CID_DV_RX_RGB_RANGE: return "Rx RGB Quantization Range";
1024 case V4L2_CID_DV_RX_IT_CONTENT_TYPE: return "Rx IT Content Type";
1025
1026 case V4L2_CID_FM_RX_CLASS: return "FM Radio Receiver Controls";
1027 case V4L2_CID_TUNE_DEEMPHASIS: return "De-Emphasis";
1028 case V4L2_CID_RDS_RECEPTION: return "RDS Reception";
1029 case V4L2_CID_RF_TUNER_CLASS: return "RF Tuner Controls";
1030 case V4L2_CID_RF_TUNER_RF_GAIN: return "RF Gain";
1031 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO: return "LNA Gain, Auto";
1032 case V4L2_CID_RF_TUNER_LNA_GAIN: return "LNA Gain";
1033 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO: return "Mixer Gain, Auto";
1034 case V4L2_CID_RF_TUNER_MIXER_GAIN: return "Mixer Gain";
1035 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO: return "IF Gain, Auto";
1036 case V4L2_CID_RF_TUNER_IF_GAIN: return "IF Gain";
1037 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: return "Bandwidth, Auto";
1038 case V4L2_CID_RF_TUNER_BANDWIDTH: return "Bandwidth";
1039 case V4L2_CID_RF_TUNER_PLL_LOCK: return "PLL Lock";
1040 case V4L2_CID_RDS_RX_PTY: return "RDS Program Type";
1041 case V4L2_CID_RDS_RX_PS_NAME: return "RDS PS Name";
1042 case V4L2_CID_RDS_RX_RADIO_TEXT: return "RDS Radio Text";
1043 case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
1044 case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM: return "RDS Traffic Program";
1045 case V4L2_CID_RDS_RX_MUSIC_SPEECH: return "RDS Music";
1046
1047 /* Detection controls */
1048 /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1049 case V4L2_CID_DETECT_CLASS: return "Detection Controls";
1050 case V4L2_CID_DETECT_MD_MODE: return "Motion Detection Mode";
1051 case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD: return "MD Global Threshold";
1052 case V4L2_CID_DETECT_MD_THRESHOLD_GRID: return "MD Threshold Grid";
1053 case V4L2_CID_DETECT_MD_REGION_GRID: return "MD Region Grid";
1054 default:
1055 return NULL;
1056 }
1057}
1058EXPORT_SYMBOL(v4l2_ctrl_get_name);
1059
1060void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
1061 s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags)
1062{
1063 *name = v4l2_ctrl_get_name(id);
1064 *flags = 0;
1065
1066 switch (id) {
1067 case V4L2_CID_AUDIO_MUTE:
1068 case V4L2_CID_AUDIO_LOUDNESS:
1069 case V4L2_CID_AUTO_WHITE_BALANCE:
1070 case V4L2_CID_AUTOGAIN:
1071 case V4L2_CID_HFLIP:
1072 case V4L2_CID_VFLIP:
1073 case V4L2_CID_HUE_AUTO:
1074 case V4L2_CID_CHROMA_AGC:
1075 case V4L2_CID_COLOR_KILLER:
1076 case V4L2_CID_AUTOBRIGHTNESS:
1077 case V4L2_CID_MPEG_AUDIO_MUTE:
1078 case V4L2_CID_MPEG_VIDEO_MUTE:
1079 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1080 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
1081 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1082 case V4L2_CID_FOCUS_AUTO:
1083 case V4L2_CID_PRIVACY:
1084 case V4L2_CID_AUDIO_LIMITER_ENABLED:
1085 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1086 case V4L2_CID_PILOT_TONE_ENABLED:
1087 case V4L2_CID_ILLUMINATORS_1:
1088 case V4L2_CID_ILLUMINATORS_2:
1089 case V4L2_CID_FLASH_STROBE_STATUS:
1090 case V4L2_CID_FLASH_CHARGE:
1091 case V4L2_CID_FLASH_READY:
1092 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
1093 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
1094 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1095 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1096 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1097 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1098 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1099 case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:
1100 case V4L2_CID_WIDE_DYNAMIC_RANGE:
1101 case V4L2_CID_IMAGE_STABILIZATION:
1102 case V4L2_CID_RDS_RECEPTION:
1103 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
1104 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
1105 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
1106 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1107 case V4L2_CID_RF_TUNER_PLL_LOCK:
1108 case V4L2_CID_RDS_TX_MONO_STEREO:
1109 case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
1110 case V4L2_CID_RDS_TX_COMPRESSED:
1111 case V4L2_CID_RDS_TX_DYNAMIC_PTY:
1112 case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
1113 case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
1114 case V4L2_CID_RDS_TX_MUSIC_SPEECH:
1115 case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
1116 case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
1117 case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
1118 case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1119 *type = V4L2_CTRL_TYPE_BOOLEAN;
1120 *min = 0;
1121 *max = *step = 1;
1122 break;
1123 case V4L2_CID_ROTATE:
1124 *type = V4L2_CTRL_TYPE_INTEGER;
1125 *flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1126 break;
1127 case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1128 case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1129 *type = V4L2_CTRL_TYPE_INTEGER;
1130 break;
1131 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1132 case V4L2_CID_PAN_RESET:
1133 case V4L2_CID_TILT_RESET:
1134 case V4L2_CID_FLASH_STROBE:
1135 case V4L2_CID_FLASH_STROBE_STOP:
1136 case V4L2_CID_AUTO_FOCUS_START:
1137 case V4L2_CID_AUTO_FOCUS_STOP:
1138 *type = V4L2_CTRL_TYPE_BUTTON;
1139 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1140 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1141 *min = *max = *step = *def = 0;
1142 break;
1143 case V4L2_CID_POWER_LINE_FREQUENCY:
1144 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
1145 case V4L2_CID_MPEG_AUDIO_ENCODING:
1146 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
1147 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
1148 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
1149 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
1150 case V4L2_CID_MPEG_AUDIO_MODE:
1151 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
1152 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
1153 case V4L2_CID_MPEG_AUDIO_CRC:
1154 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
1155 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
1156 case V4L2_CID_MPEG_VIDEO_ENCODING:
1157 case V4L2_CID_MPEG_VIDEO_ASPECT:
1158 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1159 case V4L2_CID_MPEG_STREAM_TYPE:
1160 case V4L2_CID_MPEG_STREAM_VBI_FMT:
1161 case V4L2_CID_EXPOSURE_AUTO:
1162 case V4L2_CID_AUTO_FOCUS_RANGE:
1163 case V4L2_CID_COLORFX:
1164 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1165 case V4L2_CID_TUNE_PREEMPHASIS:
1166 case V4L2_CID_FLASH_LED_MODE:
1167 case V4L2_CID_FLASH_STROBE_SOURCE:
1168 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1169 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1170 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1171 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1172 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1173 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1174 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1175 case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
1176 case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
1177 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1178 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1179 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1180 case V4L2_CID_ISO_SENSITIVITY_AUTO:
1181 case V4L2_CID_EXPOSURE_METERING:
1182 case V4L2_CID_SCENE_MODE:
1183 case V4L2_CID_DV_TX_MODE:
1184 case V4L2_CID_DV_TX_RGB_RANGE:
1185 case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
1186 case V4L2_CID_DV_RX_RGB_RANGE:
1187 case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1188 case V4L2_CID_TEST_PATTERN:
1189 case V4L2_CID_DEINTERLACING_MODE:
1190 case V4L2_CID_TUNE_DEEMPHASIS:
1191 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
1192 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
1193 case V4L2_CID_DETECT_MD_MODE:
1194 case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
1195 case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
1196 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
1197 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
1198 case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
1199 case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
1200 case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
1201 *type = V4L2_CTRL_TYPE_MENU;
1202 break;
1203 case V4L2_CID_LINK_FREQ:
1204 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
1205 break;
1206 case V4L2_CID_RDS_TX_PS_NAME:
1207 case V4L2_CID_RDS_TX_RADIO_TEXT:
1208 case V4L2_CID_RDS_RX_PS_NAME:
1209 case V4L2_CID_RDS_RX_RADIO_TEXT:
1210 *type = V4L2_CTRL_TYPE_STRING;
1211 break;
1212 case V4L2_CID_ISO_SENSITIVITY:
1213 case V4L2_CID_AUTO_EXPOSURE_BIAS:
1214 case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
1215 case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
1216 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
1217 break;
1218 case V4L2_CID_USER_CLASS:
1219 case V4L2_CID_CAMERA_CLASS:
1220 case V4L2_CID_MPEG_CLASS:
1221 case V4L2_CID_FM_TX_CLASS:
1222 case V4L2_CID_FLASH_CLASS:
1223 case V4L2_CID_JPEG_CLASS:
1224 case V4L2_CID_IMAGE_SOURCE_CLASS:
1225 case V4L2_CID_IMAGE_PROC_CLASS:
1226 case V4L2_CID_DV_CLASS:
1227 case V4L2_CID_FM_RX_CLASS:
1228 case V4L2_CID_RF_TUNER_CLASS:
1229 case V4L2_CID_DETECT_CLASS:
1230 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
1231 /* You can neither read not write these */
1232 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
1233 *min = *max = *step = *def = 0;
1234 break;
1235 case V4L2_CID_BG_COLOR:
1236 *type = V4L2_CTRL_TYPE_INTEGER;
1237 *step = 1;
1238 *min = 0;
1239 /* Max is calculated as RGB888 that is 2^24 */
1240 *max = 0xFFFFFF;
1241 break;
1242 case V4L2_CID_FLASH_FAULT:
1243 case V4L2_CID_JPEG_ACTIVE_MARKER:
1244 case V4L2_CID_3A_LOCK:
1245 case V4L2_CID_AUTO_FOCUS_STATUS:
1246 case V4L2_CID_DV_TX_HOTPLUG:
1247 case V4L2_CID_DV_TX_RXSENSE:
1248 case V4L2_CID_DV_TX_EDID_PRESENT:
1249 case V4L2_CID_DV_RX_POWER_PRESENT:
1250 *type = V4L2_CTRL_TYPE_BITMASK;
1251 break;
1252 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1253 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
1254 *type = V4L2_CTRL_TYPE_INTEGER;
1255 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
1256 break;
1257 case V4L2_CID_MPEG_VIDEO_DEC_PTS:
1258 *type = V4L2_CTRL_TYPE_INTEGER64;
1259 *flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
1260 *min = *def = 0;
1261 *max = 0x1ffffffffLL;
1262 *step = 1;
1263 break;
1264 case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
1265 *type = V4L2_CTRL_TYPE_INTEGER64;
1266 *flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
1267 *min = *def = 0;
1268 *max = 0x7fffffffffffffffLL;
1269 *step = 1;
1270 break;
1271 case V4L2_CID_PIXEL_RATE:
1272 *type = V4L2_CTRL_TYPE_INTEGER64;
1273 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
1274 break;
1275 case V4L2_CID_DETECT_MD_REGION_GRID:
1276 *type = V4L2_CTRL_TYPE_U8;
1277 break;
1278 case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
1279 *type = V4L2_CTRL_TYPE_U16;
1280 break;
1281 case V4L2_CID_RDS_TX_ALT_FREQS:
1282 *type = V4L2_CTRL_TYPE_U32;
1283 break;
1284 default:
1285 *type = V4L2_CTRL_TYPE_INTEGER;
1286 break;
1287 }
1288 switch (id) {
1289 case V4L2_CID_MPEG_AUDIO_ENCODING:
1290 case V4L2_CID_MPEG_AUDIO_MODE:
1291 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1292 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1293 case V4L2_CID_MPEG_STREAM_TYPE:
1294 *flags |= V4L2_CTRL_FLAG_UPDATE;
1295 break;
1296 case V4L2_CID_AUDIO_VOLUME:
1297 case V4L2_CID_AUDIO_BALANCE:
1298 case V4L2_CID_AUDIO_BASS:
1299 case V4L2_CID_AUDIO_TREBLE:
1300 case V4L2_CID_BRIGHTNESS:
1301 case V4L2_CID_CONTRAST:
1302 case V4L2_CID_SATURATION:
1303 case V4L2_CID_HUE:
1304 case V4L2_CID_RED_BALANCE:
1305 case V4L2_CID_BLUE_BALANCE:
1306 case V4L2_CID_GAMMA:
1307 case V4L2_CID_SHARPNESS:
1308 case V4L2_CID_CHROMA_GAIN:
1309 case V4L2_CID_RDS_TX_DEVIATION:
1310 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1311 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1312 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1313 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1314 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1315 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1316 case V4L2_CID_PILOT_TONE_DEVIATION:
1317 case V4L2_CID_PILOT_TONE_FREQUENCY:
1318 case V4L2_CID_TUNE_POWER_LEVEL:
1319 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1320 case V4L2_CID_RF_TUNER_RF_GAIN:
1321 case V4L2_CID_RF_TUNER_LNA_GAIN:
1322 case V4L2_CID_RF_TUNER_MIXER_GAIN:
1323 case V4L2_CID_RF_TUNER_IF_GAIN:
1324 case V4L2_CID_RF_TUNER_BANDWIDTH:
1325 case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
1326 *flags |= V4L2_CTRL_FLAG_SLIDER;
1327 break;
1328 case V4L2_CID_PAN_RELATIVE:
1329 case V4L2_CID_TILT_RELATIVE:
1330 case V4L2_CID_FOCUS_RELATIVE:
1331 case V4L2_CID_IRIS_RELATIVE:
1332 case V4L2_CID_ZOOM_RELATIVE:
1333 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1334 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1335 break;
1336 case V4L2_CID_FLASH_STROBE_STATUS:
1337 case V4L2_CID_AUTO_FOCUS_STATUS:
1338 case V4L2_CID_FLASH_READY:
1339 case V4L2_CID_DV_TX_HOTPLUG:
1340 case V4L2_CID_DV_TX_RXSENSE:
1341 case V4L2_CID_DV_TX_EDID_PRESENT:
1342 case V4L2_CID_DV_RX_POWER_PRESENT:
1343 case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1344 case V4L2_CID_RDS_RX_PTY:
1345 case V4L2_CID_RDS_RX_PS_NAME:
1346 case V4L2_CID_RDS_RX_RADIO_TEXT:
1347 case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
1348 case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
1349 case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1350 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
1351 break;
1352 case V4L2_CID_RF_TUNER_PLL_LOCK:
1353 *flags |= V4L2_CTRL_FLAG_VOLATILE;
1354 break;
1355 }
1356}
1357EXPORT_SYMBOL(v4l2_ctrl_fill);
1358
1359static u32 user_flags(const struct v4l2_ctrl *ctrl)
1360{
1361 u32 flags = ctrl->flags;
1362
1363 if (ctrl->is_ptr)
1364 flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
1365
1366 return flags;
1367}
1368
1369static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
1370{
1371 memset(ev->reserved, 0, sizeof(ev->reserved));
1372 ev->type = V4L2_EVENT_CTRL;
1373 ev->id = ctrl->id;
1374 ev->u.ctrl.changes = changes;
1375 ev->u.ctrl.type = ctrl->type;
1376 ev->u.ctrl.flags = user_flags(ctrl);
1377 if (ctrl->is_ptr)
1378 ev->u.ctrl.value64 = 0;
1379 else
1380 ev->u.ctrl.value64 = *ctrl->p_cur.p_s64;
1381 ev->u.ctrl.minimum = ctrl->minimum;
1382 ev->u.ctrl.maximum = ctrl->maximum;
1383 if (ctrl->type == V4L2_CTRL_TYPE_MENU
1384 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1385 ev->u.ctrl.step = 1;
1386 else
1387 ev->u.ctrl.step = ctrl->step;
1388 ev->u.ctrl.default_value = ctrl->default_value;
1389}
1390
1391static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
1392{
1393 struct v4l2_event ev;
1394 struct v4l2_subscribed_event *sev;
1395
1396 if (list_empty(&ctrl->ev_subs))
1397 return;
1398 fill_event(&ev, ctrl, changes);
1399
1400 list_for_each_entry(sev, &ctrl->ev_subs, node)
1401 if (sev->fh != fh ||
1402 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
1403 v4l2_event_queue_fh(sev->fh, &ev);
1404}
1405
1406static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx,
1407 union v4l2_ctrl_ptr ptr1,
1408 union v4l2_ctrl_ptr ptr2)
1409{
1410 switch (ctrl->type) {
1411 case V4L2_CTRL_TYPE_BUTTON:
1412 return false;
1413 case V4L2_CTRL_TYPE_STRING:
1414 idx *= ctrl->elem_size;
1415 /* strings are always 0-terminated */
1416 return !strcmp(ptr1.p_char + idx, ptr2.p_char + idx);
1417 case V4L2_CTRL_TYPE_INTEGER64:
1418 return ptr1.p_s64[idx] == ptr2.p_s64[idx];
1419 case V4L2_CTRL_TYPE_U8:
1420 return ptr1.p_u8[idx] == ptr2.p_u8[idx];
1421 case V4L2_CTRL_TYPE_U16:
1422 return ptr1.p_u16[idx] == ptr2.p_u16[idx];
1423 case V4L2_CTRL_TYPE_U32:
1424 return ptr1.p_u32[idx] == ptr2.p_u32[idx];
1425 default:
1426 if (ctrl->is_int)
1427 return ptr1.p_s32[idx] == ptr2.p_s32[idx];
1428 idx *= ctrl->elem_size;
1429 return !memcmp(ptr1.p + idx, ptr2.p + idx, ctrl->elem_size);
1430 }
1431}
1432
1433static void std_init(const struct v4l2_ctrl *ctrl, u32 idx,
1434 union v4l2_ctrl_ptr ptr)
1435{
1436 switch (ctrl->type) {
1437 case V4L2_CTRL_TYPE_STRING:
1438 idx *= ctrl->elem_size;
1439 memset(ptr.p_char + idx, ' ', ctrl->minimum);
1440 ptr.p_char[idx + ctrl->minimum] = '\0';
1441 break;
1442 case V4L2_CTRL_TYPE_INTEGER64:
1443 ptr.p_s64[idx] = ctrl->default_value;
1444 break;
1445 case V4L2_CTRL_TYPE_INTEGER:
1446 case V4L2_CTRL_TYPE_INTEGER_MENU:
1447 case V4L2_CTRL_TYPE_MENU:
1448 case V4L2_CTRL_TYPE_BITMASK:
1449 case V4L2_CTRL_TYPE_BOOLEAN:
1450 ptr.p_s32[idx] = ctrl->default_value;
1451 break;
1452 case V4L2_CTRL_TYPE_U8:
1453 ptr.p_u8[idx] = ctrl->default_value;
1454 break;
1455 case V4L2_CTRL_TYPE_U16:
1456 ptr.p_u16[idx] = ctrl->default_value;
1457 break;
1458 case V4L2_CTRL_TYPE_U32:
1459 ptr.p_u32[idx] = ctrl->default_value;
1460 break;
1461 default:
1462 idx *= ctrl->elem_size;
1463 memset(ptr.p + idx, 0, ctrl->elem_size);
1464 break;
1465 }
1466}
1467
1468static void std_log(const struct v4l2_ctrl *ctrl)
1469{
1470 union v4l2_ctrl_ptr ptr = ctrl->p_cur;
1471
1472 if (ctrl->is_array) {
1473 unsigned i;
1474
1475 for (i = 0; i < ctrl->nr_of_dims; i++)
1476 pr_cont("[%u]", ctrl->dims[i]);
1477 pr_cont(" ");
1478 }
1479
1480 switch (ctrl->type) {
1481 case V4L2_CTRL_TYPE_INTEGER:
1482 pr_cont("%d", *ptr.p_s32);
1483 break;
1484 case V4L2_CTRL_TYPE_BOOLEAN:
1485 pr_cont("%s", *ptr.p_s32 ? "true" : "false");
1486 break;
1487 case V4L2_CTRL_TYPE_MENU:
1488 pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
1489 break;
1490 case V4L2_CTRL_TYPE_INTEGER_MENU:
1491 pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
1492 break;
1493 case V4L2_CTRL_TYPE_BITMASK:
1494 pr_cont("0x%08x", *ptr.p_s32);
1495 break;
1496 case V4L2_CTRL_TYPE_INTEGER64:
1497 pr_cont("%lld", *ptr.p_s64);
1498 break;
1499 case V4L2_CTRL_TYPE_STRING:
1500 pr_cont("%s", ptr.p_char);
1501 break;
1502 case V4L2_CTRL_TYPE_U8:
1503 pr_cont("%u", (unsigned)*ptr.p_u8);
1504 break;
1505 case V4L2_CTRL_TYPE_U16:
1506 pr_cont("%u", (unsigned)*ptr.p_u16);
1507 break;
1508 case V4L2_CTRL_TYPE_U32:
1509 pr_cont("%u", (unsigned)*ptr.p_u32);
1510 break;
1511 default:
1512 pr_cont("unknown type %d", ctrl->type);
1513 break;
1514 }
1515}
1516
1517/*
1518 * Round towards the closest legal value. Be careful when we are
1519 * close to the maximum range of the control type to prevent
1520 * wrap-arounds.
1521 */
1522#define ROUND_TO_RANGE(val, offset_type, ctrl) \
1523({ \
1524 offset_type offset; \
1525 if ((ctrl)->maximum >= 0 && \
1526 val >= (ctrl)->maximum - (s32)((ctrl)->step / 2)) \
1527 val = (ctrl)->maximum; \
1528 else \
1529 val += (s32)((ctrl)->step / 2); \
1530 val = clamp_t(typeof(val), val, \
1531 (ctrl)->minimum, (ctrl)->maximum); \
1532 offset = (val) - (ctrl)->minimum; \
1533 offset = (ctrl)->step * (offset / (u32)(ctrl)->step); \
1534 val = (ctrl)->minimum + offset; \
1535 0; \
1536})
1537
1538/* Validate a new control */
1539static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx,
1540 union v4l2_ctrl_ptr ptr)
1541{
1542 size_t len;
1543 u64 offset;
1544 s64 val;
1545
1546 switch (ctrl->type) {
1547 case V4L2_CTRL_TYPE_INTEGER:
1548 return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
1549 case V4L2_CTRL_TYPE_INTEGER64:
1550 /*
1551 * We can't use the ROUND_TO_RANGE define here due to
1552 * the u64 divide that needs special care.
1553 */
1554 val = ptr.p_s64[idx];
1555 if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
1556 val = ctrl->maximum;
1557 else
1558 val += (s64)(ctrl->step / 2);
1559 val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
1560 offset = val - ctrl->minimum;
1561 do_div(offset, ctrl->step);
1562 ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
1563 return 0;
1564 case V4L2_CTRL_TYPE_U8:
1565 return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
1566 case V4L2_CTRL_TYPE_U16:
1567 return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
1568 case V4L2_CTRL_TYPE_U32:
1569 return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
1570
1571 case V4L2_CTRL_TYPE_BOOLEAN:
1572 ptr.p_s32[idx] = !!ptr.p_s32[idx];
1573 return 0;
1574
1575 case V4L2_CTRL_TYPE_MENU:
1576 case V4L2_CTRL_TYPE_INTEGER_MENU:
1577 if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
1578 return -ERANGE;
1579 if (ctrl->menu_skip_mask & (1 << ptr.p_s32[idx]))
1580 return -EINVAL;
1581 if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1582 ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
1583 return -EINVAL;
1584 return 0;
1585
1586 case V4L2_CTRL_TYPE_BITMASK:
1587 ptr.p_s32[idx] &= ctrl->maximum;
1588 return 0;
1589
1590 case V4L2_CTRL_TYPE_BUTTON:
1591 case V4L2_CTRL_TYPE_CTRL_CLASS:
1592 ptr.p_s32[idx] = 0;
1593 return 0;
1594
1595 case V4L2_CTRL_TYPE_STRING:
1596 idx *= ctrl->elem_size;
1597 len = strlen(ptr.p_char + idx);
1598 if (len < ctrl->minimum)
1599 return -ERANGE;
1600 if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
1601 return -ERANGE;
1602 return 0;
1603
1604 default:
1605 return -EINVAL;
1606 }
1607}
1608
1609static const struct v4l2_ctrl_type_ops std_type_ops = {
1610 .equal = std_equal,
1611 .init = std_init,
1612 .log = std_log,
1613 .validate = std_validate,
1614};
1615
1616/* Helper function: copy the given control value back to the caller */
1617static int ptr_to_user(struct v4l2_ext_control *c,
1618 struct v4l2_ctrl *ctrl,
1619 union v4l2_ctrl_ptr ptr)
1620{
1621 u32 len;
1622
1623 if (ctrl->is_ptr && !ctrl->is_string)
1624 return copy_to_user(c->ptr, ptr.p, c->size) ?
1625 -EFAULT : 0;
1626
1627 switch (ctrl->type) {
1628 case V4L2_CTRL_TYPE_STRING:
1629 len = strlen(ptr.p_char);
1630 if (c->size < len + 1) {
1631 c->size = ctrl->elem_size;
1632 return -ENOSPC;
1633 }
1634 return copy_to_user(c->string, ptr.p_char, len + 1) ?
1635 -EFAULT : 0;
1636 case V4L2_CTRL_TYPE_INTEGER64:
1637 c->value64 = *ptr.p_s64;
1638 break;
1639 default:
1640 c->value = *ptr.p_s32;
1641 break;
1642 }
1643 return 0;
1644}
1645
1646/* Helper function: copy the current control value back to the caller */
1647static int cur_to_user(struct v4l2_ext_control *c,
1648 struct v4l2_ctrl *ctrl)
1649{
1650 return ptr_to_user(c, ctrl, ctrl->p_cur);
1651}
1652
1653/* Helper function: copy the new control value back to the caller */
1654static int new_to_user(struct v4l2_ext_control *c,
1655 struct v4l2_ctrl *ctrl)
1656{
1657 return ptr_to_user(c, ctrl, ctrl->p_new);
1658}
1659
1660/* Helper function: copy the initial control value back to the caller */
1661static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
1662{
1663 int idx;
1664
1665 for (idx = 0; idx < ctrl->elems; idx++)
1666 ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
1667
1668 return ptr_to_user(c, ctrl, ctrl->p_new);
1669}
1670
1671/* Helper function: copy the caller-provider value to the given control value */
1672static int user_to_ptr(struct v4l2_ext_control *c,
1673 struct v4l2_ctrl *ctrl,
1674 union v4l2_ctrl_ptr ptr)
1675{
1676 int ret;
1677 u32 size;
1678
1679 ctrl->is_new = 1;
1680 if (ctrl->is_ptr && !ctrl->is_string) {
1681 unsigned idx;
1682
1683 ret = copy_from_user(ptr.p, c->ptr, c->size) ? -EFAULT : 0;
1684 if (ret || !ctrl->is_array)
1685 return ret;
1686 for (idx = c->size / ctrl->elem_size; idx < ctrl->elems; idx++)
1687 ctrl->type_ops->init(ctrl, idx, ptr);
1688 return 0;
1689 }
1690
1691 switch (ctrl->type) {
1692 case V4L2_CTRL_TYPE_INTEGER64:
1693 *ptr.p_s64 = c->value64;
1694 break;
1695 case V4L2_CTRL_TYPE_STRING:
1696 size = c->size;
1697 if (size == 0)
1698 return -ERANGE;
1699 if (size > ctrl->maximum + 1)
1700 size = ctrl->maximum + 1;
1701 ret = copy_from_user(ptr.p_char, c->string, size) ? -EFAULT : 0;
1702 if (!ret) {
1703 char last = ptr.p_char[size - 1];
1704
1705 ptr.p_char[size - 1] = 0;
1706 /* If the string was longer than ctrl->maximum,
1707 then return an error. */
1708 if (strlen(ptr.p_char) == ctrl->maximum && last)
1709 return -ERANGE;
1710 }
1711 return ret;
1712 default:
1713 *ptr.p_s32 = c->value;
1714 break;
1715 }
1716 return 0;
1717}
1718
1719/* Helper function: copy the caller-provider value as the new control value */
1720static int user_to_new(struct v4l2_ext_control *c,
1721 struct v4l2_ctrl *ctrl)
1722{
1723 return user_to_ptr(c, ctrl, ctrl->p_new);
1724}
1725
1726/* Copy the one value to another. */
1727static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
1728 union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to)
1729{
1730 if (ctrl == NULL)
1731 return;
1732 memcpy(to.p, from.p, ctrl->elems * ctrl->elem_size);
1733}
1734
1735/* Copy the new value to the current value. */
1736static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
1737{
1738 bool changed;
1739
1740 if (ctrl == NULL)
1741 return;
1742
1743 /* has_changed is set by cluster_changed */
1744 changed = ctrl->has_changed;
1745 if (changed)
1746 ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur);
1747
1748 if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
1749 /* Note: CH_FLAGS is only set for auto clusters. */
1750 ctrl->flags &=
1751 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
1752 if (!is_cur_manual(ctrl->cluster[0])) {
1753 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1754 if (ctrl->cluster[0]->has_volatiles)
1755 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1756 }
1757 fh = NULL;
1758 }
1759 if (changed || ch_flags) {
1760 /* If a control was changed that was not one of the controls
1761 modified by the application, then send the event to all. */
1762 if (!ctrl->is_new)
1763 fh = NULL;
1764 send_event(fh, ctrl,
1765 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
1766 if (ctrl->call_notify && changed && ctrl->handler->notify)
1767 ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
1768 }
1769}
1770
1771/* Copy the current value to the new value */
1772static void cur_to_new(struct v4l2_ctrl *ctrl)
1773{
1774 if (ctrl == NULL)
1775 return;
1776 ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new);
1777}
1778
1779/* Return non-zero if one or more of the controls in the cluster has a new
1780 value that differs from the current value. */
1781static int cluster_changed(struct v4l2_ctrl *master)
1782{
1783 bool changed = false;
1784 unsigned idx;
1785 int i;
1786
1787 for (i = 0; i < master->ncontrols; i++) {
1788 struct v4l2_ctrl *ctrl = master->cluster[i];
1789 bool ctrl_changed = false;
1790
1791 if (ctrl == NULL)
1792 continue;
1793
1794 if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE)
1795 changed = ctrl_changed = true;
1796
1797 /*
1798 * Set has_changed to false to avoid generating
1799 * the event V4L2_EVENT_CTRL_CH_VALUE
1800 */
1801 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
1802 ctrl->has_changed = false;
1803 continue;
1804 }
1805
1806 for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++)
1807 ctrl_changed = !ctrl->type_ops->equal(ctrl, idx,
1808 ctrl->p_cur, ctrl->p_new);
1809 ctrl->has_changed = ctrl_changed;
1810 changed |= ctrl->has_changed;
1811 }
1812 return changed;
1813}
1814
1815/* Control range checking */
1816static int check_range(enum v4l2_ctrl_type type,
1817 s64 min, s64 max, u64 step, s64 def)
1818{
1819 switch (type) {
1820 case V4L2_CTRL_TYPE_BOOLEAN:
1821 if (step != 1 || max > 1 || min < 0)
1822 return -ERANGE;
1823 /* fall through */
1824 case V4L2_CTRL_TYPE_U8:
1825 case V4L2_CTRL_TYPE_U16:
1826 case V4L2_CTRL_TYPE_U32:
1827 case V4L2_CTRL_TYPE_INTEGER:
1828 case V4L2_CTRL_TYPE_INTEGER64:
1829 if (step == 0 || min > max || def < min || def > max)
1830 return -ERANGE;
1831 return 0;
1832 case V4L2_CTRL_TYPE_BITMASK:
1833 if (step || min || !max || (def & ~max))
1834 return -ERANGE;
1835 return 0;
1836 case V4L2_CTRL_TYPE_MENU:
1837 case V4L2_CTRL_TYPE_INTEGER_MENU:
1838 if (min > max || def < min || def > max)
1839 return -ERANGE;
1840 /* Note: step == menu_skip_mask for menu controls.
1841 So here we check if the default value is masked out. */
1842 if (step && ((1 << def) & step))
1843 return -EINVAL;
1844 return 0;
1845 case V4L2_CTRL_TYPE_STRING:
1846 if (min > max || min < 0 || step < 1 || def)
1847 return -ERANGE;
1848 return 0;
1849 default:
1850 return 0;
1851 }
1852}
1853
1854/* Validate a new control */
1855static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
1856{
1857 unsigned idx;
1858 int err = 0;
1859
1860 for (idx = 0; !err && idx < ctrl->elems; idx++)
1861 err = ctrl->type_ops->validate(ctrl, idx, p_new);
1862 return err;
1863}
1864
1865static inline u32 node2id(struct list_head *node)
1866{
1867 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
1868}
1869
1870/* Set the handler's error code if it wasn't set earlier already */
1871static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1872{
1873 if (hdl->error == 0)
1874 hdl->error = err;
1875 return err;
1876}
1877
1878/* Initialize the handler */
1879int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
1880 unsigned nr_of_controls_hint,
1881 struct lock_class_key *key, const char *name)
1882{
1883 mutex_init(&hdl->_lock);
1884 hdl->lock = &hdl->_lock;
1885 lockdep_set_class_and_name(hdl->lock, key, name);
1886 INIT_LIST_HEAD(&hdl->ctrls);
1887 INIT_LIST_HEAD(&hdl->ctrl_refs);
1888 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
1889 hdl->buckets = kvmalloc_array(hdl->nr_of_buckets,
1890 sizeof(hdl->buckets[0]),
1891 GFP_KERNEL | __GFP_ZERO);
1892 hdl->error = hdl->buckets ? 0 : -ENOMEM;
1893 return hdl->error;
1894}
1895EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
1896
1897/* Free all controls and control refs */
1898void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1899{
1900 struct v4l2_ctrl_ref *ref, *next_ref;
1901 struct v4l2_ctrl *ctrl, *next_ctrl;
1902 struct v4l2_subscribed_event *sev, *next_sev;
1903
1904 if (hdl == NULL || hdl->buckets == NULL)
1905 return;
1906
1907 mutex_lock(hdl->lock);
1908 /* Free all nodes */
1909 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1910 list_del(&ref->node);
1911 kfree(ref);
1912 }
1913 /* Free all controls owned by the handler */
1914 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1915 list_del(&ctrl->node);
1916 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1917 list_del(&sev->node);
1918 kvfree(ctrl);
1919 }
1920 kvfree(hdl->buckets);
1921 hdl->buckets = NULL;
1922 hdl->cached = NULL;
1923 hdl->error = 0;
1924 mutex_unlock(hdl->lock);
1925 mutex_destroy(&hdl->_lock);
1926}
1927EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1928
1929/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1930 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1931 with applications that do not use the NEXT_CTRL flag.
1932
1933 We just find the n-th private user control. It's O(N), but that should not
1934 be an issue in this particular case. */
1935static struct v4l2_ctrl_ref *find_private_ref(
1936 struct v4l2_ctrl_handler *hdl, u32 id)
1937{
1938 struct v4l2_ctrl_ref *ref;
1939
1940 id -= V4L2_CID_PRIVATE_BASE;
1941 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1942 /* Search for private user controls that are compatible with
1943 VIDIOC_G/S_CTRL. */
1944 if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1945 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1946 if (!ref->ctrl->is_int)
1947 continue;
1948 if (id == 0)
1949 return ref;
1950 id--;
1951 }
1952 }
1953 return NULL;
1954}
1955
1956/* Find a control with the given ID. */
1957static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1958{
1959 struct v4l2_ctrl_ref *ref;
1960 int bucket;
1961
1962 id &= V4L2_CTRL_ID_MASK;
1963
1964 /* Old-style private controls need special handling */
1965 if (id >= V4L2_CID_PRIVATE_BASE)
1966 return find_private_ref(hdl, id);
1967 bucket = id % hdl->nr_of_buckets;
1968
1969 /* Simple optimization: cache the last control found */
1970 if (hdl->cached && hdl->cached->ctrl->id == id)
1971 return hdl->cached;
1972
1973 /* Not in cache, search the hash */
1974 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1975 while (ref && ref->ctrl->id != id)
1976 ref = ref->next;
1977
1978 if (ref)
1979 hdl->cached = ref; /* cache it! */
1980 return ref;
1981}
1982
1983/* Find a control with the given ID. Take the handler's lock first. */
1984static struct v4l2_ctrl_ref *find_ref_lock(
1985 struct v4l2_ctrl_handler *hdl, u32 id)
1986{
1987 struct v4l2_ctrl_ref *ref = NULL;
1988
1989 if (hdl) {
1990 mutex_lock(hdl->lock);
1991 ref = find_ref(hdl, id);
1992 mutex_unlock(hdl->lock);
1993 }
1994 return ref;
1995}
1996
1997/* Find a control with the given ID. */
1998struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1999{
2000 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
2001
2002 return ref ? ref->ctrl : NULL;
2003}
2004EXPORT_SYMBOL(v4l2_ctrl_find);
2005
2006/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
2007static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
2008 struct v4l2_ctrl *ctrl)
2009{
2010 struct v4l2_ctrl_ref *ref;
2011 struct v4l2_ctrl_ref *new_ref;
2012 u32 id = ctrl->id;
2013 u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
2014 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
2015
2016 /*
2017 * Automatically add the control class if it is not yet present and
2018 * the new control is not a compound control.
2019 */
2020 if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
2021 id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
2022 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
2023 return hdl->error;
2024
2025 if (hdl->error)
2026 return hdl->error;
2027
2028 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
2029 if (!new_ref)
2030 return handler_set_err(hdl, -ENOMEM);
2031 new_ref->ctrl = ctrl;
2032 if (ctrl->handler == hdl) {
2033 /* By default each control starts in a cluster of its own.
2034 new_ref->ctrl is basically a cluster array with one
2035 element, so that's perfect to use as the cluster pointer.
2036 But only do this for the handler that owns the control. */
2037 ctrl->cluster = &new_ref->ctrl;
2038 ctrl->ncontrols = 1;
2039 }
2040
2041 INIT_LIST_HEAD(&new_ref->node);
2042
2043 mutex_lock(hdl->lock);
2044
2045 /* Add immediately at the end of the list if the list is empty, or if
2046 the last element in the list has a lower ID.
2047 This ensures that when elements are added in ascending order the
2048 insertion is an O(1) operation. */
2049 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
2050 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
2051 goto insert_in_hash;
2052 }
2053
2054 /* Find insert position in sorted list */
2055 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2056 if (ref->ctrl->id < id)
2057 continue;
2058 /* Don't add duplicates */
2059 if (ref->ctrl->id == id) {
2060 kfree(new_ref);
2061 goto unlock;
2062 }
2063 list_add(&new_ref->node, ref->node.prev);
2064 break;
2065 }
2066
2067insert_in_hash:
2068 /* Insert the control node in the hash */
2069 new_ref->next = hdl->buckets[bucket];
2070 hdl->buckets[bucket] = new_ref;
2071
2072unlock:
2073 mutex_unlock(hdl->lock);
2074 return 0;
2075}
2076
2077/* Add a new control */
2078static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
2079 const struct v4l2_ctrl_ops *ops,
2080 const struct v4l2_ctrl_type_ops *type_ops,
2081 u32 id, const char *name, enum v4l2_ctrl_type type,
2082 s64 min, s64 max, u64 step, s64 def,
2083 const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
2084 u32 flags, const char * const *qmenu,
2085 const s64 *qmenu_int, void *priv)
2086{
2087 struct v4l2_ctrl *ctrl;
2088 unsigned sz_extra;
2089 unsigned nr_of_dims = 0;
2090 unsigned elems = 1;
2091 bool is_array;
2092 unsigned tot_ctrl_size;
2093 unsigned idx;
2094 void *data;
2095 int err;
2096
2097 if (hdl->error)
2098 return NULL;
2099
2100 while (dims && dims[nr_of_dims]) {
2101 elems *= dims[nr_of_dims];
2102 nr_of_dims++;
2103 if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
2104 break;
2105 }
2106 is_array = nr_of_dims > 0;
2107
2108 /* Prefill elem_size for all types handled by std_type_ops */
2109 switch (type) {
2110 case V4L2_CTRL_TYPE_INTEGER64:
2111 elem_size = sizeof(s64);
2112 break;
2113 case V4L2_CTRL_TYPE_STRING:
2114 elem_size = max + 1;
2115 break;
2116 case V4L2_CTRL_TYPE_U8:
2117 elem_size = sizeof(u8);
2118 break;
2119 case V4L2_CTRL_TYPE_U16:
2120 elem_size = sizeof(u16);
2121 break;
2122 case V4L2_CTRL_TYPE_U32:
2123 elem_size = sizeof(u32);
2124 break;
2125 default:
2126 if (type < V4L2_CTRL_COMPOUND_TYPES)
2127 elem_size = sizeof(s32);
2128 break;
2129 }
2130 tot_ctrl_size = elem_size * elems;
2131
2132 /* Sanity checks */
2133 if (id == 0 || name == NULL || !elem_size ||
2134 id >= V4L2_CID_PRIVATE_BASE ||
2135 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
2136 (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
2137 handler_set_err(hdl, -ERANGE);
2138 return NULL;
2139 }
2140 err = check_range(type, min, max, step, def);
2141 if (err) {
2142 handler_set_err(hdl, err);
2143 return NULL;
2144 }
2145 if (is_array &&
2146 (type == V4L2_CTRL_TYPE_BUTTON ||
2147 type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
2148 handler_set_err(hdl, -EINVAL);
2149 return NULL;
2150 }
2151
2152 sz_extra = 0;
2153 if (type == V4L2_CTRL_TYPE_BUTTON)
2154 flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
2155 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
2156 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
2157 flags |= V4L2_CTRL_FLAG_READ_ONLY;
2158 else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
2159 type == V4L2_CTRL_TYPE_STRING ||
2160 type >= V4L2_CTRL_COMPOUND_TYPES ||
2161 is_array)
2162 sz_extra += 2 * tot_ctrl_size;
2163
2164 ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
2165 if (ctrl == NULL) {
2166 handler_set_err(hdl, -ENOMEM);
2167 return NULL;
2168 }
2169
2170 INIT_LIST_HEAD(&ctrl->node);
2171 INIT_LIST_HEAD(&ctrl->ev_subs);
2172 ctrl->handler = hdl;
2173 ctrl->ops = ops;
2174 ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
2175 ctrl->id = id;
2176 ctrl->name = name;
2177 ctrl->type = type;
2178 ctrl->flags = flags;
2179 ctrl->minimum = min;
2180 ctrl->maximum = max;
2181 ctrl->step = step;
2182 ctrl->default_value = def;
2183 ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
2184 ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
2185 ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
2186 ctrl->is_array = is_array;
2187 ctrl->elems = elems;
2188 ctrl->nr_of_dims = nr_of_dims;
2189 if (nr_of_dims)
2190 memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
2191 ctrl->elem_size = elem_size;
2192 if (type == V4L2_CTRL_TYPE_MENU)
2193 ctrl->qmenu = qmenu;
2194 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2195 ctrl->qmenu_int = qmenu_int;
2196 ctrl->priv = priv;
2197 ctrl->cur.val = ctrl->val = def;
2198 data = &ctrl[1];
2199
2200 if (!ctrl->is_int) {
2201 ctrl->p_new.p = data;
2202 ctrl->p_cur.p = data + tot_ctrl_size;
2203 } else {
2204 ctrl->p_new.p = &ctrl->val;
2205 ctrl->p_cur.p = &ctrl->cur.val;
2206 }
2207 for (idx = 0; idx < elems; idx++) {
2208 ctrl->type_ops->init(ctrl, idx, ctrl->p_cur);
2209 ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
2210 }
2211
2212 if (handler_new_ref(hdl, ctrl)) {
2213 kvfree(ctrl);
2214 return NULL;
2215 }
2216 mutex_lock(hdl->lock);
2217 list_add_tail(&ctrl->node, &hdl->ctrls);
2218 mutex_unlock(hdl->lock);
2219 return ctrl;
2220}
2221
2222struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
2223 const struct v4l2_ctrl_config *cfg, void *priv)
2224{
2225 bool is_menu;
2226 struct v4l2_ctrl *ctrl;
2227 const char *name = cfg->name;
2228 const char * const *qmenu = cfg->qmenu;
2229 const s64 *qmenu_int = cfg->qmenu_int;
2230 enum v4l2_ctrl_type type = cfg->type;
2231 u32 flags = cfg->flags;
2232 s64 min = cfg->min;
2233 s64 max = cfg->max;
2234 u64 step = cfg->step;
2235 s64 def = cfg->def;
2236
2237 if (name == NULL)
2238 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
2239 &def, &flags);
2240
2241 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU ||
2242 cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU);
2243 if (is_menu)
2244 WARN_ON(step);
2245 else
2246 WARN_ON(cfg->menu_skip_mask);
2247 if (cfg->type == V4L2_CTRL_TYPE_MENU && qmenu == NULL)
2248 qmenu = v4l2_ctrl_get_menu(cfg->id);
2249 else if (cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU &&
2250 qmenu_int == NULL) {
2251 handler_set_err(hdl, -EINVAL);
2252 return NULL;
2253 }
2254
2255 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
2256 type, min, max,
2257 is_menu ? cfg->menu_skip_mask : step, def,
2258 cfg->dims, cfg->elem_size,
2259 flags, qmenu, qmenu_int, priv);
2260 if (ctrl)
2261 ctrl->is_private = cfg->is_private;
2262 return ctrl;
2263}
2264EXPORT_SYMBOL(v4l2_ctrl_new_custom);
2265
2266/* Helper function for standard non-menu controls */
2267struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
2268 const struct v4l2_ctrl_ops *ops,
2269 u32 id, s64 min, s64 max, u64 step, s64 def)
2270{
2271 const char *name;
2272 enum v4l2_ctrl_type type;
2273 u32 flags;
2274
2275 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2276 if (type == V4L2_CTRL_TYPE_MENU ||
2277 type == V4L2_CTRL_TYPE_INTEGER_MENU ||
2278 type >= V4L2_CTRL_COMPOUND_TYPES) {
2279 handler_set_err(hdl, -EINVAL);
2280 return NULL;
2281 }
2282 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2283 min, max, step, def, NULL, 0,
2284 flags, NULL, NULL, NULL);
2285}
2286EXPORT_SYMBOL(v4l2_ctrl_new_std);
2287
2288/* Helper function for standard menu controls */
2289struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
2290 const struct v4l2_ctrl_ops *ops,
2291 u32 id, u8 _max, u64 mask, u8 _def)
2292{
2293 const char * const *qmenu = NULL;
2294 const s64 *qmenu_int = NULL;
2295 unsigned int qmenu_int_len = 0;
2296 const char *name;
2297 enum v4l2_ctrl_type type;
2298 s64 min;
2299 s64 max = _max;
2300 s64 def = _def;
2301 u64 step;
2302 u32 flags;
2303
2304 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2305
2306 if (type == V4L2_CTRL_TYPE_MENU)
2307 qmenu = v4l2_ctrl_get_menu(id);
2308 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
2309 qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
2310
2311 if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) {
2312 handler_set_err(hdl, -EINVAL);
2313 return NULL;
2314 }
2315 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2316 0, max, mask, def, NULL, 0,
2317 flags, qmenu, qmenu_int, NULL);
2318}
2319EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
2320
2321/* Helper function for standard menu controls with driver defined menu */
2322struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
2323 const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
2324 u64 mask, u8 _def, const char * const *qmenu)
2325{
2326 enum v4l2_ctrl_type type;
2327 const char *name;
2328 u32 flags;
2329 u64 step;
2330 s64 min;
2331 s64 max = _max;
2332 s64 def = _def;
2333
2334 /* v4l2_ctrl_new_std_menu_items() should only be called for
2335 * standard controls without a standard menu.
2336 */
2337 if (v4l2_ctrl_get_menu(id)) {
2338 handler_set_err(hdl, -EINVAL);
2339 return NULL;
2340 }
2341
2342 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2343 if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
2344 handler_set_err(hdl, -EINVAL);
2345 return NULL;
2346 }
2347 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2348 0, max, mask, def, NULL, 0,
2349 flags, qmenu, NULL, NULL);
2350
2351}
2352EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
2353
2354/* Helper function for standard integer menu controls */
2355struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
2356 const struct v4l2_ctrl_ops *ops,
2357 u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
2358{
2359 const char *name;
2360 enum v4l2_ctrl_type type;
2361 s64 min;
2362 u64 step;
2363 s64 max = _max;
2364 s64 def = _def;
2365 u32 flags;
2366
2367 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2368 if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
2369 handler_set_err(hdl, -EINVAL);
2370 return NULL;
2371 }
2372 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2373 0, max, 0, def, NULL, 0,
2374 flags, NULL, qmenu_int, NULL);
2375}
2376EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
2377
2378/* Add the controls from another handler to our own. */
2379int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
2380 struct v4l2_ctrl_handler *add,
2381 bool (*filter)(const struct v4l2_ctrl *ctrl))
2382{
2383 struct v4l2_ctrl_ref *ref;
2384 int ret = 0;
2385
2386 /* Do nothing if either handler is NULL or if they are the same */
2387 if (!hdl || !add || hdl == add)
2388 return 0;
2389 if (hdl->error)
2390 return hdl->error;
2391 mutex_lock(add->lock);
2392 list_for_each_entry(ref, &add->ctrl_refs, node) {
2393 struct v4l2_ctrl *ctrl = ref->ctrl;
2394
2395 /* Skip handler-private controls. */
2396 if (ctrl->is_private)
2397 continue;
2398 /* And control classes */
2399 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2400 continue;
2401 /* Filter any unwanted controls */
2402 if (filter && !filter(ctrl))
2403 continue;
2404 ret = handler_new_ref(hdl, ctrl);
2405 if (ret)
2406 break;
2407 }
2408 mutex_unlock(add->lock);
2409 return ret;
2410}
2411EXPORT_SYMBOL(v4l2_ctrl_add_handler);
2412
2413bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
2414{
2415 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
2416 return true;
2417 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
2418 return true;
2419 switch (ctrl->id) {
2420 case V4L2_CID_AUDIO_MUTE:
2421 case V4L2_CID_AUDIO_VOLUME:
2422 case V4L2_CID_AUDIO_BALANCE:
2423 case V4L2_CID_AUDIO_BASS:
2424 case V4L2_CID_AUDIO_TREBLE:
2425 case V4L2_CID_AUDIO_LOUDNESS:
2426 return true;
2427 default:
2428 break;
2429 }
2430 return false;
2431}
2432EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
2433
2434/* Cluster controls */
2435void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
2436{
2437 bool has_volatiles = false;
2438 int i;
2439
2440 /* The first control is the master control and it must not be NULL */
2441 if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
2442 return;
2443
2444 for (i = 0; i < ncontrols; i++) {
2445 if (controls[i]) {
2446 controls[i]->cluster = controls;
2447 controls[i]->ncontrols = ncontrols;
2448 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
2449 has_volatiles = true;
2450 }
2451 }
2452 controls[0]->has_volatiles = has_volatiles;
2453}
2454EXPORT_SYMBOL(v4l2_ctrl_cluster);
2455
2456void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
2457 u8 manual_val, bool set_volatile)
2458{
2459 struct v4l2_ctrl *master = controls[0];
2460 u32 flag = 0;
2461 int i;
2462
2463 v4l2_ctrl_cluster(ncontrols, controls);
2464 WARN_ON(ncontrols <= 1);
2465 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
2466 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
2467 master->is_auto = true;
2468 master->has_volatiles = set_volatile;
2469 master->manual_mode_value = manual_val;
2470 master->flags |= V4L2_CTRL_FLAG_UPDATE;
2471
2472 if (!is_cur_manual(master))
2473 flag = V4L2_CTRL_FLAG_INACTIVE |
2474 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
2475
2476 for (i = 1; i < ncontrols; i++)
2477 if (controls[i])
2478 controls[i]->flags |= flag;
2479}
2480EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
2481
2482/* Activate/deactivate a control. */
2483void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
2484{
2485 /* invert since the actual flag is called 'inactive' */
2486 bool inactive = !active;
2487 bool old;
2488
2489 if (ctrl == NULL)
2490 return;
2491
2492 if (inactive)
2493 /* set V4L2_CTRL_FLAG_INACTIVE */
2494 old = test_and_set_bit(4, &ctrl->flags);
2495 else
2496 /* clear V4L2_CTRL_FLAG_INACTIVE */
2497 old = test_and_clear_bit(4, &ctrl->flags);
2498 if (old != inactive)
2499 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2500}
2501EXPORT_SYMBOL(v4l2_ctrl_activate);
2502
2503/* Grab/ungrab a control.
2504 Typically used when streaming starts and you want to grab controls,
2505 preventing the user from changing them.
2506
2507 Just call this and the framework will block any attempts to change
2508 these controls. */
2509void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
2510{
2511 bool old;
2512
2513 if (ctrl == NULL)
2514 return;
2515
2516 v4l2_ctrl_lock(ctrl);
2517 if (grabbed)
2518 /* set V4L2_CTRL_FLAG_GRABBED */
2519 old = test_and_set_bit(1, &ctrl->flags);
2520 else
2521 /* clear V4L2_CTRL_FLAG_GRABBED */
2522 old = test_and_clear_bit(1, &ctrl->flags);
2523 if (old != grabbed)
2524 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2525 v4l2_ctrl_unlock(ctrl);
2526}
2527EXPORT_SYMBOL(v4l2_ctrl_grab);
2528
2529/* Log the control name and value */
2530static void log_ctrl(const struct v4l2_ctrl *ctrl,
2531 const char *prefix, const char *colon)
2532{
2533 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
2534 return;
2535 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
2536 return;
2537
2538 pr_info("%s%s%s: ", prefix, colon, ctrl->name);
2539
2540 ctrl->type_ops->log(ctrl);
2541
2542 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
2543 V4L2_CTRL_FLAG_GRABBED |
2544 V4L2_CTRL_FLAG_VOLATILE)) {
2545 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
2546 pr_cont(" inactive");
2547 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
2548 pr_cont(" grabbed");
2549 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
2550 pr_cont(" volatile");
2551 }
2552 pr_cont("\n");
2553}
2554
2555/* Log all controls owned by the handler */
2556void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
2557 const char *prefix)
2558{
2559 struct v4l2_ctrl *ctrl;
2560 const char *colon = "";
2561 int len;
2562
2563 if (hdl == NULL)
2564 return;
2565 if (prefix == NULL)
2566 prefix = "";
2567 len = strlen(prefix);
2568 if (len && prefix[len - 1] != ' ')
2569 colon = ": ";
2570 mutex_lock(hdl->lock);
2571 list_for_each_entry(ctrl, &hdl->ctrls, node)
2572 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
2573 log_ctrl(ctrl, prefix, colon);
2574 mutex_unlock(hdl->lock);
2575}
2576EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
2577
2578int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
2579{
2580 v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
2581 return 0;
2582}
2583EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
2584
2585/* Call s_ctrl for all controls owned by the handler */
2586int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2587{
2588 struct v4l2_ctrl *ctrl;
2589 int ret = 0;
2590
2591 if (hdl == NULL)
2592 return 0;
2593
2594 lockdep_assert_held(hdl->lock);
2595
2596 list_for_each_entry(ctrl, &hdl->ctrls, node)
2597 ctrl->done = false;
2598
2599 list_for_each_entry(ctrl, &hdl->ctrls, node) {
2600 struct v4l2_ctrl *master = ctrl->cluster[0];
2601 int i;
2602
2603 /* Skip if this control was already handled by a cluster. */
2604 /* Skip button controls and read-only controls. */
2605 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
2606 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
2607 continue;
2608
2609 for (i = 0; i < master->ncontrols; i++) {
2610 if (master->cluster[i]) {
2611 cur_to_new(master->cluster[i]);
2612 master->cluster[i]->is_new = 1;
2613 master->cluster[i]->done = true;
2614 }
2615 }
2616 ret = call_op(master, s_ctrl);
2617 if (ret)
2618 break;
2619 }
2620
2621 return ret;
2622}
2623EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
2624
2625int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2626{
2627 int ret;
2628
2629 if (hdl == NULL)
2630 return 0;
2631
2632 mutex_lock(hdl->lock);
2633 ret = __v4l2_ctrl_handler_setup(hdl);
2634 mutex_unlock(hdl->lock);
2635
2636 return ret;
2637}
2638EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
2639
2640/* Implement VIDIOC_QUERY_EXT_CTRL */
2641int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
2642{
2643 const unsigned next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
2644 u32 id = qc->id & V4L2_CTRL_ID_MASK;
2645 struct v4l2_ctrl_ref *ref;
2646 struct v4l2_ctrl *ctrl;
2647
2648 if (hdl == NULL)
2649 return -EINVAL;
2650
2651 mutex_lock(hdl->lock);
2652
2653 /* Try to find it */
2654 ref = find_ref(hdl, id);
2655
2656 if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
2657 bool is_compound;
2658 /* Match any control that is not hidden */
2659 unsigned mask = 1;
2660 bool match = false;
2661
2662 if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
2663 /* Match any hidden control */
2664 match = true;
2665 } else if ((qc->id & next_flags) == next_flags) {
2666 /* Match any control, compound or not */
2667 mask = 0;
2668 }
2669
2670 /* Find the next control with ID > qc->id */
2671
2672 /* Did we reach the end of the control list? */
2673 if (id >= node2id(hdl->ctrl_refs.prev)) {
2674 ref = NULL; /* Yes, so there is no next control */
2675 } else if (ref) {
2676 /* We found a control with the given ID, so just get
2677 the next valid one in the list. */
2678 list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
2679 is_compound = ref->ctrl->is_array ||
2680 ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
2681 if (id < ref->ctrl->id &&
2682 (is_compound & mask) == match)
2683 break;
2684 }
2685 if (&ref->node == &hdl->ctrl_refs)
2686 ref = NULL;
2687 } else {
2688 /* No control with the given ID exists, so start
2689 searching for the next largest ID. We know there
2690 is one, otherwise the first 'if' above would have
2691 been true. */
2692 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2693 is_compound = ref->ctrl->is_array ||
2694 ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
2695 if (id < ref->ctrl->id &&
2696 (is_compound & mask) == match)
2697 break;
2698 }
2699 if (&ref->node == &hdl->ctrl_refs)
2700 ref = NULL;
2701 }
2702 }
2703 mutex_unlock(hdl->lock);
2704
2705 if (!ref)
2706 return -EINVAL;
2707
2708 ctrl = ref->ctrl;
2709 memset(qc, 0, sizeof(*qc));
2710 if (id >= V4L2_CID_PRIVATE_BASE)
2711 qc->id = id;
2712 else
2713 qc->id = ctrl->id;
2714 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
2715 qc->flags = user_flags(ctrl);
2716 qc->type = ctrl->type;
2717 qc->elem_size = ctrl->elem_size;
2718 qc->elems = ctrl->elems;
2719 qc->nr_of_dims = ctrl->nr_of_dims;
2720 memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
2721 qc->minimum = ctrl->minimum;
2722 qc->maximum = ctrl->maximum;
2723 qc->default_value = ctrl->default_value;
2724 if (ctrl->type == V4L2_CTRL_TYPE_MENU
2725 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
2726 qc->step = 1;
2727 else
2728 qc->step = ctrl->step;
2729 return 0;
2730}
2731EXPORT_SYMBOL(v4l2_query_ext_ctrl);
2732
2733/* Implement VIDIOC_QUERYCTRL */
2734int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
2735{
2736 struct v4l2_query_ext_ctrl qec = { qc->id };
2737 int rc;
2738
2739 rc = v4l2_query_ext_ctrl(hdl, &qec);
2740 if (rc)
2741 return rc;
2742
2743 qc->id = qec.id;
2744 qc->type = qec.type;
2745 qc->flags = qec.flags;
2746 strlcpy(qc->name, qec.name, sizeof(qc->name));
2747 switch (qc->type) {
2748 case V4L2_CTRL_TYPE_INTEGER:
2749 case V4L2_CTRL_TYPE_BOOLEAN:
2750 case V4L2_CTRL_TYPE_MENU:
2751 case V4L2_CTRL_TYPE_INTEGER_MENU:
2752 case V4L2_CTRL_TYPE_STRING:
2753 case V4L2_CTRL_TYPE_BITMASK:
2754 qc->minimum = qec.minimum;
2755 qc->maximum = qec.maximum;
2756 qc->step = qec.step;
2757 qc->default_value = qec.default_value;
2758 break;
2759 default:
2760 qc->minimum = 0;
2761 qc->maximum = 0;
2762 qc->step = 0;
2763 qc->default_value = 0;
2764 break;
2765 }
2766 return 0;
2767}
2768EXPORT_SYMBOL(v4l2_queryctrl);
2769
2770/* Implement VIDIOC_QUERYMENU */
2771int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
2772{
2773 struct v4l2_ctrl *ctrl;
2774 u32 i = qm->index;
2775
2776 ctrl = v4l2_ctrl_find(hdl, qm->id);
2777 if (!ctrl)
2778 return -EINVAL;
2779
2780 qm->reserved = 0;
2781 /* Sanity checks */
2782 switch (ctrl->type) {
2783 case V4L2_CTRL_TYPE_MENU:
2784 if (ctrl->qmenu == NULL)
2785 return -EINVAL;
2786 break;
2787 case V4L2_CTRL_TYPE_INTEGER_MENU:
2788 if (ctrl->qmenu_int == NULL)
2789 return -EINVAL;
2790 break;
2791 default:
2792 return -EINVAL;
2793 }
2794
2795 if (i < ctrl->minimum || i > ctrl->maximum)
2796 return -EINVAL;
2797
2798 /* Use mask to see if this menu item should be skipped */
2799 if (ctrl->menu_skip_mask & (1 << i))
2800 return -EINVAL;
2801 /* Empty menu items should also be skipped */
2802 if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
2803 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
2804 return -EINVAL;
2805 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
2806 } else {
2807 qm->value = ctrl->qmenu_int[i];
2808 }
2809 return 0;
2810}
2811EXPORT_SYMBOL(v4l2_querymenu);
2812
2813
2814/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
2815
2816 It is not a fully atomic operation, just best-effort only. After all, if
2817 multiple controls have to be set through multiple i2c writes (for example)
2818 then some initial writes may succeed while others fail. Thus leaving the
2819 system in an inconsistent state. The question is how much effort you are
2820 willing to spend on trying to make something atomic that really isn't.
2821
2822 From the point of view of an application the main requirement is that
2823 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
2824 error should be returned without actually affecting any controls.
2825
2826 If all the values are correct, then it is acceptable to just give up
2827 in case of low-level errors.
2828
2829 It is important though that the application can tell when only a partial
2830 configuration was done. The way we do that is through the error_idx field
2831 of struct v4l2_ext_controls: if that is equal to the count field then no
2832 controls were affected. Otherwise all controls before that index were
2833 successful in performing their 'get' or 'set' operation, the control at
2834 the given index failed, and you don't know what happened with the controls
2835 after the failed one. Since if they were part of a control cluster they
2836 could have been successfully processed (if a cluster member was encountered
2837 at index < error_idx), they could have failed (if a cluster member was at
2838 error_idx), or they may not have been processed yet (if the first cluster
2839 member appeared after error_idx).
2840
2841 It is all fairly theoretical, though. In practice all you can do is to
2842 bail out. If error_idx == count, then it is an application bug. If
2843 error_idx < count then it is only an application bug if the error code was
2844 EBUSY. That usually means that something started streaming just when you
2845 tried to set the controls. In all other cases it is a driver/hardware
2846 problem and all you can do is to retry or bail out.
2847
2848 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
2849 never modifies controls the error_idx is just set to whatever control
2850 has an invalid value.
2851 */
2852
2853/* Prepare for the extended g/s/try functions.
2854 Find the controls in the control array and do some basic checks. */
2855static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
2856 struct v4l2_ext_controls *cs,
2857 struct v4l2_ctrl_helper *helpers,
2858 bool get)
2859{
2860 struct v4l2_ctrl_helper *h;
2861 bool have_clusters = false;
2862 u32 i;
2863
2864 for (i = 0, h = helpers; i < cs->count; i++, h++) {
2865 struct v4l2_ext_control *c = &cs->controls[i];
2866 struct v4l2_ctrl_ref *ref;
2867 struct v4l2_ctrl *ctrl;
2868 u32 id = c->id & V4L2_CTRL_ID_MASK;
2869
2870 cs->error_idx = i;
2871
2872 if (cs->which &&
2873 cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
2874 V4L2_CTRL_ID2WHICH(id) != cs->which)
2875 return -EINVAL;
2876
2877 /* Old-style private controls are not allowed for
2878 extended controls */
2879 if (id >= V4L2_CID_PRIVATE_BASE)
2880 return -EINVAL;
2881 ref = find_ref_lock(hdl, id);
2882 if (ref == NULL)
2883 return -EINVAL;
2884 ctrl = ref->ctrl;
2885 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
2886 return -EINVAL;
2887
2888 if (ctrl->cluster[0]->ncontrols > 1)
2889 have_clusters = true;
2890 if (ctrl->cluster[0] != ctrl)
2891 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
2892 if (ctrl->is_ptr && !ctrl->is_string) {
2893 unsigned tot_size = ctrl->elems * ctrl->elem_size;
2894
2895 if (c->size < tot_size) {
2896 if (get) {
2897 c->size = tot_size;
2898 return -ENOSPC;
2899 }
2900 return -EFAULT;
2901 }
2902 c->size = tot_size;
2903 }
2904 /* Store the ref to the master control of the cluster */
2905 h->mref = ref;
2906 h->ctrl = ctrl;
2907 /* Initially set next to 0, meaning that there is no other
2908 control in this helper array belonging to the same
2909 cluster */
2910 h->next = 0;
2911 }
2912
2913 /* We are done if there were no controls that belong to a multi-
2914 control cluster. */
2915 if (!have_clusters)
2916 return 0;
2917
2918 /* The code below figures out in O(n) time which controls in the list
2919 belong to the same cluster. */
2920
2921 /* This has to be done with the handler lock taken. */
2922 mutex_lock(hdl->lock);
2923
2924 /* First zero the helper field in the master control references */
2925 for (i = 0; i < cs->count; i++)
2926 helpers[i].mref->helper = NULL;
2927 for (i = 0, h = helpers; i < cs->count; i++, h++) {
2928 struct v4l2_ctrl_ref *mref = h->mref;
2929
2930 /* If the mref->helper is set, then it points to an earlier
2931 helper that belongs to the same cluster. */
2932 if (mref->helper) {
2933 /* Set the next field of mref->helper to the current
2934 index: this means that that earlier helper now
2935 points to the next helper in the same cluster. */
2936 mref->helper->next = i;
2937 /* mref should be set only for the first helper in the
2938 cluster, clear the others. */
2939 h->mref = NULL;
2940 }
2941 /* Point the mref helper to the current helper struct. */
2942 mref->helper = h;
2943 }
2944 mutex_unlock(hdl->lock);
2945 return 0;
2946}
2947
2948/* Handles the corner case where cs->count == 0. It checks whether the
2949 specified control class exists. If that class ID is 0, then it checks
2950 whether there are any controls at all. */
2951static int class_check(struct v4l2_ctrl_handler *hdl, u32 which)
2952{
2953 if (which == 0 || which == V4L2_CTRL_WHICH_DEF_VAL)
2954 return 0;
2955 return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL;
2956}
2957
2958
2959
2960/* Get extended controls. Allocates the helpers array if needed. */
2961int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2962{
2963 struct v4l2_ctrl_helper helper[4];
2964 struct v4l2_ctrl_helper *helpers = helper;
2965 int ret;
2966 int i, j;
2967 bool def_value;
2968
2969 def_value = (cs->which == V4L2_CTRL_WHICH_DEF_VAL);
2970
2971 cs->error_idx = cs->count;
2972 cs->which = V4L2_CTRL_ID2WHICH(cs->which);
2973
2974 if (hdl == NULL)
2975 return -EINVAL;
2976
2977 if (cs->count == 0)
2978 return class_check(hdl, cs->which);
2979
2980 if (cs->count > ARRAY_SIZE(helper)) {
2981 helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
2982 GFP_KERNEL);
2983 if (helpers == NULL)
2984 return -ENOMEM;
2985 }
2986
2987 ret = prepare_ext_ctrls(hdl, cs, helpers, true);
2988 cs->error_idx = cs->count;
2989
2990 for (i = 0; !ret && i < cs->count; i++)
2991 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2992 ret = -EACCES;
2993
2994 for (i = 0; !ret && i < cs->count; i++) {
2995 int (*ctrl_to_user)(struct v4l2_ext_control *c,
2996 struct v4l2_ctrl *ctrl);
2997 struct v4l2_ctrl *master;
2998
2999 ctrl_to_user = def_value ? def_to_user : cur_to_user;
3000
3001 if (helpers[i].mref == NULL)
3002 continue;
3003
3004 master = helpers[i].mref->ctrl;
3005 cs->error_idx = i;
3006
3007 v4l2_ctrl_lock(master);
3008
3009 /* g_volatile_ctrl will update the new control values */
3010 if (!def_value &&
3011 ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
3012 (master->has_volatiles && !is_cur_manual(master)))) {
3013 for (j = 0; j < master->ncontrols; j++)
3014 cur_to_new(master->cluster[j]);
3015 ret = call_op(master, g_volatile_ctrl);
3016 ctrl_to_user = new_to_user;
3017 }
3018 /* If OK, then copy the current (for non-volatile controls)
3019 or the new (for volatile controls) control values to the
3020 caller */
3021 if (!ret) {
3022 u32 idx = i;
3023
3024 do {
3025 ret = ctrl_to_user(cs->controls + idx,
3026 helpers[idx].ctrl);
3027 idx = helpers[idx].next;
3028 } while (!ret && idx);
3029 }
3030 v4l2_ctrl_unlock(master);
3031 }
3032
3033 if (cs->count > ARRAY_SIZE(helper))
3034 kvfree(helpers);
3035 return ret;
3036}
3037EXPORT_SYMBOL(v4l2_g_ext_ctrls);
3038
3039/* Helper function to get a single control */
3040static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
3041{
3042 struct v4l2_ctrl *master = ctrl->cluster[0];
3043 int ret = 0;
3044 int i;
3045
3046 /* Compound controls are not supported. The new_to_user() and
3047 * cur_to_user() calls below would need to be modified not to access
3048 * userspace memory when called from get_ctrl().
3049 */
3050 if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64)
3051 return -EINVAL;
3052
3053 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
3054 return -EACCES;
3055
3056 v4l2_ctrl_lock(master);
3057 /* g_volatile_ctrl will update the current control values */
3058 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
3059 for (i = 0; i < master->ncontrols; i++)
3060 cur_to_new(master->cluster[i]);
3061 ret = call_op(master, g_volatile_ctrl);
3062 new_to_user(c, ctrl);
3063 } else {
3064 cur_to_user(c, ctrl);
3065 }
3066 v4l2_ctrl_unlock(master);
3067 return ret;
3068}
3069
3070int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
3071{
3072 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
3073 struct v4l2_ext_control c;
3074 int ret;
3075
3076 if (ctrl == NULL || !ctrl->is_int)
3077 return -EINVAL;
3078 ret = get_ctrl(ctrl, &c);
3079 control->value = c.value;
3080 return ret;
3081}
3082EXPORT_SYMBOL(v4l2_g_ctrl);
3083
3084s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
3085{
3086 struct v4l2_ext_control c;
3087
3088 /* It's a driver bug if this happens. */
3089 WARN_ON(!ctrl->is_int);
3090 c.value = 0;
3091 get_ctrl(ctrl, &c);
3092 return c.value;
3093}
3094EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
3095
3096s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
3097{
3098 struct v4l2_ext_control c;
3099
3100 /* It's a driver bug if this happens. */
3101 WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
3102 c.value64 = 0;
3103 get_ctrl(ctrl, &c);
3104 return c.value64;
3105}
3106EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);
3107
3108
3109/* Core function that calls try/s_ctrl and ensures that the new value is
3110 copied to the current value on a set.
3111 Must be called with ctrl->handler->lock held. */
3112static int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
3113 bool set, u32 ch_flags)
3114{
3115 bool update_flag;
3116 int ret;
3117 int i;
3118
3119 /* Go through the cluster and either validate the new value or
3120 (if no new value was set), copy the current value to the new
3121 value, ensuring a consistent view for the control ops when
3122 called. */
3123 for (i = 0; i < master->ncontrols; i++) {
3124 struct v4l2_ctrl *ctrl = master->cluster[i];
3125
3126 if (ctrl == NULL)
3127 continue;
3128
3129 if (!ctrl->is_new) {
3130 cur_to_new(ctrl);
3131 continue;
3132 }
3133 /* Check again: it may have changed since the
3134 previous check in try_or_set_ext_ctrls(). */
3135 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
3136 return -EBUSY;
3137 }
3138
3139 ret = call_op(master, try_ctrl);
3140
3141 /* Don't set if there is no change */
3142 if (ret || !set || !cluster_changed(master))
3143 return ret;
3144 ret = call_op(master, s_ctrl);
3145 if (ret)
3146 return ret;
3147
3148 /* If OK, then make the new values permanent. */
3149 update_flag = is_cur_manual(master) != is_new_manual(master);
3150
3151 for (i = 0; i < master->ncontrols; i++) {
3152 /*
3153 * If we switch from auto to manual mode, and this cluster
3154 * contains volatile controls, then all non-master controls
3155 * have to be marked as changed. The 'new' value contains
3156 * the volatile value (obtained by update_from_auto_cluster),
3157 * which now has to become the current value.
3158 */
3159 if (i && update_flag && is_new_manual(master) &&
3160 master->has_volatiles && master->cluster[i])
3161 master->cluster[i]->has_changed = true;
3162
3163 new_to_cur(fh, master->cluster[i], ch_flags |
3164 ((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
3165 }
3166 return 0;
3167}
3168
3169/* Validate controls. */
3170static int validate_ctrls(struct v4l2_ext_controls *cs,
3171 struct v4l2_ctrl_helper *helpers, bool set)
3172{
3173 unsigned i;
3174 int ret = 0;
3175
3176 cs->error_idx = cs->count;
3177 for (i = 0; i < cs->count; i++) {
3178 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
3179 union v4l2_ctrl_ptr p_new;
3180
3181 cs->error_idx = i;
3182
3183 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
3184 return -EACCES;
3185 /* This test is also done in try_set_control_cluster() which
3186 is called in atomic context, so that has the final say,
3187 but it makes sense to do an up-front check as well. Once
3188 an error occurs in try_set_control_cluster() some other
3189 controls may have been set already and we want to do a
3190 best-effort to avoid that. */
3191 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
3192 return -EBUSY;
3193 /*
3194 * Skip validation for now if the payload needs to be copied
3195 * from userspace into kernelspace. We'll validate those later.
3196 */
3197 if (ctrl->is_ptr)
3198 continue;
3199 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
3200 p_new.p_s64 = &cs->controls[i].value64;
3201 else
3202 p_new.p_s32 = &cs->controls[i].value;
3203 ret = validate_new(ctrl, p_new);
3204 if (ret)
3205 return ret;
3206 }
3207 return 0;
3208}
3209
3210/* Obtain the current volatile values of an autocluster and mark them
3211 as new. */
3212static void update_from_auto_cluster(struct v4l2_ctrl *master)
3213{
3214 int i;
3215
3216 for (i = 1; i < master->ncontrols; i++)
3217 cur_to_new(master->cluster[i]);
3218 if (!call_op(master, g_volatile_ctrl))
3219 for (i = 1; i < master->ncontrols; i++)
3220 if (master->cluster[i])
3221 master->cluster[i]->is_new = 1;
3222}
3223
3224/* Try or try-and-set controls */
3225static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
3226 struct v4l2_ext_controls *cs,
3227 bool set)
3228{
3229 struct v4l2_ctrl_helper helper[4];
3230 struct v4l2_ctrl_helper *helpers = helper;
3231 unsigned i, j;
3232 int ret;
3233
3234 cs->error_idx = cs->count;
3235
3236 /* Default value cannot be changed */
3237 if (cs->which == V4L2_CTRL_WHICH_DEF_VAL)
3238 return -EINVAL;
3239
3240 cs->which = V4L2_CTRL_ID2WHICH(cs->which);
3241
3242 if (hdl == NULL)
3243 return -EINVAL;
3244
3245 if (cs->count == 0)
3246 return class_check(hdl, cs->which);
3247
3248 if (cs->count > ARRAY_SIZE(helper)) {
3249 helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
3250 GFP_KERNEL);
3251 if (!helpers)
3252 return -ENOMEM;
3253 }
3254 ret = prepare_ext_ctrls(hdl, cs, helpers, false);
3255 if (!ret)
3256 ret = validate_ctrls(cs, helpers, set);
3257 if (ret && set)
3258 cs->error_idx = cs->count;
3259 for (i = 0; !ret && i < cs->count; i++) {
3260 struct v4l2_ctrl *master;
3261 u32 idx = i;
3262
3263 if (helpers[i].mref == NULL)
3264 continue;
3265
3266 cs->error_idx = i;
3267 master = helpers[i].mref->ctrl;
3268 v4l2_ctrl_lock(master);
3269
3270 /* Reset the 'is_new' flags of the cluster */
3271 for (j = 0; j < master->ncontrols; j++)
3272 if (master->cluster[j])
3273 master->cluster[j]->is_new = 0;
3274
3275 /* For volatile autoclusters that are currently in auto mode
3276 we need to discover if it will be set to manual mode.
3277 If so, then we have to copy the current volatile values
3278 first since those will become the new manual values (which
3279 may be overwritten by explicit new values from this set
3280 of controls). */
3281 if (master->is_auto && master->has_volatiles &&
3282 !is_cur_manual(master)) {
3283 /* Pick an initial non-manual value */
3284 s32 new_auto_val = master->manual_mode_value + 1;
3285 u32 tmp_idx = idx;
3286
3287 do {
3288 /* Check if the auto control is part of the
3289 list, and remember the new value. */
3290 if (helpers[tmp_idx].ctrl == master)
3291 new_auto_val = cs->controls[tmp_idx].value;
3292 tmp_idx = helpers[tmp_idx].next;
3293 } while (tmp_idx);
3294 /* If the new value == the manual value, then copy
3295 the current volatile values. */
3296 if (new_auto_val == master->manual_mode_value)
3297 update_from_auto_cluster(master);
3298 }
3299
3300 /* Copy the new caller-supplied control values.
3301 user_to_new() sets 'is_new' to 1. */
3302 do {
3303 struct v4l2_ctrl *ctrl = helpers[idx].ctrl;
3304
3305 ret = user_to_new(cs->controls + idx, ctrl);
3306 if (!ret && ctrl->is_ptr)
3307 ret = validate_new(ctrl, ctrl->p_new);
3308 idx = helpers[idx].next;
3309 } while (!ret && idx);
3310
3311 if (!ret)
3312 ret = try_or_set_cluster(fh, master, set, 0);
3313
3314 /* Copy the new values back to userspace. */
3315 if (!ret) {
3316 idx = i;
3317 do {
3318 ret = new_to_user(cs->controls + idx,
3319 helpers[idx].ctrl);
3320 idx = helpers[idx].next;
3321 } while (!ret && idx);
3322 }
3323 v4l2_ctrl_unlock(master);
3324 }
3325
3326 if (cs->count > ARRAY_SIZE(helper))
3327 kvfree(helpers);
3328 return ret;
3329}
3330
3331int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
3332{
3333 return try_set_ext_ctrls(NULL, hdl, cs, false);
3334}
3335EXPORT_SYMBOL(v4l2_try_ext_ctrls);
3336
3337int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
3338 struct v4l2_ext_controls *cs)
3339{
3340 return try_set_ext_ctrls(fh, hdl, cs, true);
3341}
3342EXPORT_SYMBOL(v4l2_s_ext_ctrls);
3343
3344/* Helper function for VIDIOC_S_CTRL compatibility */
3345static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
3346{
3347 struct v4l2_ctrl *master = ctrl->cluster[0];
3348 int ret;
3349 int i;
3350
3351 /* Reset the 'is_new' flags of the cluster */
3352 for (i = 0; i < master->ncontrols; i++)
3353 if (master->cluster[i])
3354 master->cluster[i]->is_new = 0;
3355
3356 ret = validate_new(ctrl, ctrl->p_new);
3357 if (ret)
3358 return ret;
3359
3360 /* For autoclusters with volatiles that are switched from auto to
3361 manual mode we have to update the current volatile values since
3362 those will become the initial manual values after such a switch. */
3363 if (master->is_auto && master->has_volatiles && ctrl == master &&
3364 !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
3365 update_from_auto_cluster(master);
3366
3367 ctrl->is_new = 1;
3368 return try_or_set_cluster(fh, master, true, ch_flags);
3369}
3370
3371/* Helper function for VIDIOC_S_CTRL compatibility */
3372static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
3373 struct v4l2_ext_control *c)
3374{
3375 int ret;
3376
3377 v4l2_ctrl_lock(ctrl);
3378 user_to_new(c, ctrl);
3379 ret = set_ctrl(fh, ctrl, 0);
3380 if (!ret)
3381 cur_to_user(c, ctrl);
3382 v4l2_ctrl_unlock(ctrl);
3383 return ret;
3384}
3385
3386int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
3387 struct v4l2_control *control)
3388{
3389 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
3390 struct v4l2_ext_control c = { control->id };
3391 int ret;
3392
3393 if (ctrl == NULL || !ctrl->is_int)
3394 return -EINVAL;
3395
3396 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
3397 return -EACCES;
3398
3399 c.value = control->value;
3400 ret = set_ctrl_lock(fh, ctrl, &c);
3401 control->value = c.value;
3402 return ret;
3403}
3404EXPORT_SYMBOL(v4l2_s_ctrl);
3405
3406int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
3407{
3408 lockdep_assert_held(ctrl->handler->lock);
3409
3410 /* It's a driver bug if this happens. */
3411 WARN_ON(!ctrl->is_int);
3412 ctrl->val = val;
3413 return set_ctrl(NULL, ctrl, 0);
3414}
3415EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
3416
3417int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
3418{
3419 lockdep_assert_held(ctrl->handler->lock);
3420
3421 /* It's a driver bug if this happens. */
3422 WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
3423 *ctrl->p_new.p_s64 = val;
3424 return set_ctrl(NULL, ctrl, 0);
3425}
3426EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
3427
3428int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
3429{
3430 lockdep_assert_held(ctrl->handler->lock);
3431
3432 /* It's a driver bug if this happens. */
3433 WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING);
3434 strlcpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
3435 return set_ctrl(NULL, ctrl, 0);
3436}
3437EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);
3438
3439void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
3440{
3441 if (ctrl == NULL)
3442 return;
3443 if (notify == NULL) {
3444 ctrl->call_notify = 0;
3445 return;
3446 }
3447 if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
3448 return;
3449 ctrl->handler->notify = notify;
3450 ctrl->handler->notify_priv = priv;
3451 ctrl->call_notify = 1;
3452}
3453EXPORT_SYMBOL(v4l2_ctrl_notify);
3454
3455int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
3456 s64 min, s64 max, u64 step, s64 def)
3457{
3458 bool value_changed;
3459 bool range_changed = false;
3460 int ret;
3461
3462 lockdep_assert_held(ctrl->handler->lock);
3463
3464 switch (ctrl->type) {
3465 case V4L2_CTRL_TYPE_INTEGER:
3466 case V4L2_CTRL_TYPE_INTEGER64:
3467 case V4L2_CTRL_TYPE_BOOLEAN:
3468 case V4L2_CTRL_TYPE_MENU:
3469 case V4L2_CTRL_TYPE_INTEGER_MENU:
3470 case V4L2_CTRL_TYPE_BITMASK:
3471 case V4L2_CTRL_TYPE_U8:
3472 case V4L2_CTRL_TYPE_U16:
3473 case V4L2_CTRL_TYPE_U32:
3474 if (ctrl->is_array)
3475 return -EINVAL;
3476 ret = check_range(ctrl->type, min, max, step, def);
3477 if (ret)
3478 return ret;
3479 break;
3480 default:
3481 return -EINVAL;
3482 }
3483 if ((ctrl->minimum != min) || (ctrl->maximum != max) ||
3484 (ctrl->step != step) || ctrl->default_value != def) {
3485 range_changed = true;
3486 ctrl->minimum = min;
3487 ctrl->maximum = max;
3488 ctrl->step = step;
3489 ctrl->default_value = def;
3490 }
3491 cur_to_new(ctrl);
3492 if (validate_new(ctrl, ctrl->p_new)) {
3493 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
3494 *ctrl->p_new.p_s64 = def;
3495 else
3496 *ctrl->p_new.p_s32 = def;
3497 }
3498
3499 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
3500 value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
3501 else
3502 value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
3503 if (value_changed)
3504 ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
3505 else if (range_changed)
3506 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
3507 return ret;
3508}
3509EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
3510
3511static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
3512{
3513 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
3514
3515 if (ctrl == NULL)
3516 return -EINVAL;
3517
3518 v4l2_ctrl_lock(ctrl);
3519 list_add_tail(&sev->node, &ctrl->ev_subs);
3520 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
3521 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
3522 struct v4l2_event ev;
3523 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
3524
3525 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
3526 changes |= V4L2_EVENT_CTRL_CH_VALUE;
3527 fill_event(&ev, ctrl, changes);
3528 /* Mark the queue as active, allowing this initial
3529 event to be accepted. */
3530 sev->elems = elems;
3531 v4l2_event_queue_fh(sev->fh, &ev);
3532 }
3533 v4l2_ctrl_unlock(ctrl);
3534 return 0;
3535}
3536
3537static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
3538{
3539 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
3540
3541 if (ctrl == NULL)
3542 return;
3543
3544 v4l2_ctrl_lock(ctrl);
3545 list_del(&sev->node);
3546 v4l2_ctrl_unlock(ctrl);
3547}
3548
3549void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
3550{
3551 u32 old_changes = old->u.ctrl.changes;
3552
3553 old->u.ctrl = new->u.ctrl;
3554 old->u.ctrl.changes |= old_changes;
3555}
3556EXPORT_SYMBOL(v4l2_ctrl_replace);
3557
3558void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
3559{
3560 new->u.ctrl.changes |= old->u.ctrl.changes;
3561}
3562EXPORT_SYMBOL(v4l2_ctrl_merge);
3563
3564const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
3565 .add = v4l2_ctrl_add_event,
3566 .del = v4l2_ctrl_del_event,
3567 .replace = v4l2_ctrl_replace,
3568 .merge = v4l2_ctrl_merge,
3569};
3570EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
3571
3572int v4l2_ctrl_log_status(struct file *file, void *fh)
3573{
3574 struct video_device *vfd = video_devdata(file);
3575 struct v4l2_fh *vfh = file->private_data;
3576
3577 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
3578 v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
3579 vfd->v4l2_dev->name);
3580 return 0;
3581}
3582EXPORT_SYMBOL(v4l2_ctrl_log_status);
3583
3584int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
3585 const struct v4l2_event_subscription *sub)
3586{
3587 if (sub->type == V4L2_EVENT_CTRL)
3588 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
3589 return -EINVAL;
3590}
3591EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
3592
3593int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
3594 struct v4l2_event_subscription *sub)
3595{
3596 if (!sd->ctrl_handler)
3597 return -EINVAL;
3598 return v4l2_ctrl_subscribe_event(fh, sub);
3599}
3600EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);
3601
3602__poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
3603{
3604 struct v4l2_fh *fh = file->private_data;
3605
3606 if (v4l2_event_pending(fh))
3607 return EPOLLPRI;
3608 poll_wait(file, &fh->wait, wait);
3609 return 0;
3610}
3611EXPORT_SYMBOL(v4l2_ctrl_poll);