]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/v4l2-ctrls.c
[media] V4L: Add camera exposure metering control
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / v4l2-ctrls.c
CommitLineData
0996517c
HV
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>
2b80163c 22#include <linux/slab.h>
35a24636 23#include <linux/export.h>
0996517c
HV
24#include <media/v4l2-ioctl.h>
25#include <media/v4l2-device.h>
26#include <media/v4l2-ctrls.h>
6e239399 27#include <media/v4l2-event.h>
0996517c
HV
28#include <media/v4l2-dev.h>
29
ddac5c10
HV
30#define has_op(master, op) \
31 (master->ops && master->ops->op)
54c911eb 32#define call_op(master, op) \
ddac5c10 33 (has_op(master, op) ? master->ops->op(master) : 0)
54c911eb 34
0996517c 35/* Internal temporary helper struct, one for each v4l2_ext_control */
eb5b16ef
HV
36struct v4l2_ctrl_helper {
37 /* Pointer to the control reference of the master control */
38 struct v4l2_ctrl_ref *mref;
0996517c
HV
39 /* The control corresponding to the v4l2_ext_control ID field. */
40 struct v4l2_ctrl *ctrl;
eb5b16ef
HV
41 /* v4l2_ext_control index of the next control belonging to the
42 same cluster, or 0 if there isn't any. */
43 u32 next;
0996517c
HV
44};
45
72d877ca 46/* Small helper function to determine if the autocluster is set to manual
88365105 47 mode. */
72d877ca
HV
48static bool is_cur_manual(const struct v4l2_ctrl *master)
49{
50 return master->is_auto && master->cur.val == master->manual_mode_value;
51}
52
53/* Same as above, but this checks the against the new value instead of the
54 current value. */
55static bool is_new_manual(const struct v4l2_ctrl *master)
56{
57 return master->is_auto && master->val == master->manual_mode_value;
58}
59
0996517c
HV
60/* Returns NULL or a character pointer array containing the menu for
61 the given control ID. The pointer array ends with a NULL pointer.
62 An empty string signifies a menu entry that is invalid. This allows
63 drivers to disable certain options if it is not supported. */
513521ea 64const char * const *v4l2_ctrl_get_menu(u32 id)
0996517c 65{
513521ea 66 static const char * const mpeg_audio_sampling_freq[] = {
0996517c
HV
67 "44.1 kHz",
68 "48 kHz",
69 "32 kHz",
70 NULL
71 };
513521ea 72 static const char * const mpeg_audio_encoding[] = {
0996517c
HV
73 "MPEG-1/2 Layer I",
74 "MPEG-1/2 Layer II",
75 "MPEG-1/2 Layer III",
76 "MPEG-2/4 AAC",
77 "AC-3",
78 NULL
79 };
513521ea 80 static const char * const mpeg_audio_l1_bitrate[] = {
0996517c
HV
81 "32 kbps",
82 "64 kbps",
83 "96 kbps",
84 "128 kbps",
85 "160 kbps",
86 "192 kbps",
87 "224 kbps",
88 "256 kbps",
89 "288 kbps",
90 "320 kbps",
91 "352 kbps",
92 "384 kbps",
93 "416 kbps",
94 "448 kbps",
95 NULL
96 };
513521ea 97 static const char * const mpeg_audio_l2_bitrate[] = {
0996517c
HV
98 "32 kbps",
99 "48 kbps",
100 "56 kbps",
101 "64 kbps",
102 "80 kbps",
103 "96 kbps",
104 "112 kbps",
105 "128 kbps",
106 "160 kbps",
107 "192 kbps",
108 "224 kbps",
109 "256 kbps",
110 "320 kbps",
111 "384 kbps",
112 NULL
113 };
513521ea 114 static const char * const mpeg_audio_l3_bitrate[] = {
0996517c
HV
115 "32 kbps",
116 "40 kbps",
117 "48 kbps",
118 "56 kbps",
119 "64 kbps",
120 "80 kbps",
121 "96 kbps",
122 "112 kbps",
123 "128 kbps",
124 "160 kbps",
125 "192 kbps",
126 "224 kbps",
127 "256 kbps",
128 "320 kbps",
129 NULL
130 };
513521ea 131 static const char * const mpeg_audio_ac3_bitrate[] = {
0996517c
HV
132 "32 kbps",
133 "40 kbps",
134 "48 kbps",
135 "56 kbps",
136 "64 kbps",
137 "80 kbps",
138 "96 kbps",
139 "112 kbps",
140 "128 kbps",
141 "160 kbps",
142 "192 kbps",
143 "224 kbps",
144 "256 kbps",
145 "320 kbps",
146 "384 kbps",
147 "448 kbps",
148 "512 kbps",
149 "576 kbps",
150 "640 kbps",
151 NULL
152 };
513521ea 153 static const char * const mpeg_audio_mode[] = {
0996517c
HV
154 "Stereo",
155 "Joint Stereo",
156 "Dual",
157 "Mono",
158 NULL
159 };
513521ea 160 static const char * const mpeg_audio_mode_extension[] = {
0996517c
HV
161 "Bound 4",
162 "Bound 8",
163 "Bound 12",
164 "Bound 16",
165 NULL
166 };
513521ea 167 static const char * const mpeg_audio_emphasis[] = {
0996517c
HV
168 "No Emphasis",
169 "50/15 us",
170 "CCITT J17",
171 NULL
172 };
513521ea 173 static const char * const mpeg_audio_crc[] = {
0996517c
HV
174 "No CRC",
175 "16-bit CRC",
176 NULL
177 };
24c19a21
HV
178 static const char * const mpeg_audio_dec_playback[] = {
179 "Auto",
180 "Stereo",
181 "Left",
182 "Right",
183 "Mono",
184 "Swapped Stereo",
185 NULL
186 };
513521ea 187 static const char * const mpeg_video_encoding[] = {
0996517c
HV
188 "MPEG-1",
189 "MPEG-2",
190 "MPEG-4 AVC",
191 NULL
192 };
513521ea 193 static const char * const mpeg_video_aspect[] = {
0996517c
HV
194 "1x1",
195 "4x3",
196 "16x9",
197 "2.21x1",
198 NULL
199 };
513521ea 200 static const char * const mpeg_video_bitrate_mode[] = {
0996517c
HV
201 "Variable Bitrate",
202 "Constant Bitrate",
203 NULL
204 };
513521ea 205 static const char * const mpeg_stream_type[] = {
0996517c
HV
206 "MPEG-2 Program Stream",
207 "MPEG-2 Transport Stream",
208 "MPEG-1 System Stream",
209 "MPEG-2 DVD-compatible Stream",
210 "MPEG-1 VCD-compatible Stream",
211 "MPEG-2 SVCD-compatible Stream",
212 NULL
213 };
513521ea 214 static const char * const mpeg_stream_vbi_fmt[] = {
0996517c 215 "No VBI",
064f5096 216 "Private Packet, IVTV Format",
0996517c
HV
217 NULL
218 };
513521ea 219 static const char * const camera_power_line_frequency[] = {
0996517c
HV
220 "Disabled",
221 "50 Hz",
222 "60 Hz",
d26a6635 223 "Auto",
0996517c
HV
224 NULL
225 };
513521ea 226 static const char * const camera_exposure_auto[] = {
0996517c
HV
227 "Auto Mode",
228 "Manual Mode",
229 "Shutter Priority Mode",
230 "Aperture Priority Mode",
231 NULL
232 };
cf072139
SN
233 static const char * const camera_exposure_metering[] = {
234 "Average",
235 "Center Weighted",
236 "Spot",
237 NULL
238 };
513521ea 239 static const char * const colorfx[] = {
0996517c
HV
240 "None",
241 "Black & White",
242 "Sepia",
243 "Negative",
244 "Emboss",
245 "Sketch",
064f5096
KD
246 "Sky Blue",
247 "Grass Green",
248 "Skin Whiten",
0996517c 249 "Vivid",
6491d1ad
SN
250 "Aqua",
251 "Art Freeze",
252 "Silhouette",
253 "Solarization",
254 "Antique",
255 "Set Cb/Cr",
0996517c
HV
256 NULL
257 };
e40a0573
SN
258 static const char * const auto_n_preset_white_balance[] = {
259 "Manual",
260 "Auto",
261 "Incandescent",
262 "Fluorescent",
263 "Fluorescent H",
264 "Horizon",
265 "Daylight",
266 "Flash",
267 "Cloudy",
268 "Shade",
269 NULL,
270 };
7f84ad8b
SN
271 static const char * const camera_iso_sensitivity_auto[] = {
272 "Manual",
273 "Auto",
274 NULL
275 };
513521ea 276 static const char * const tune_preemphasis[] = {
064f5096 277 "No Preemphasis",
f769c260
HV
278 "50 Microseconds",
279 "75 Microseconds",
0996517c
HV
280 NULL,
281 };
064f5096
KD
282 static const char * const header_mode[] = {
283 "Separate Buffer",
284 "Joined With 1st Frame",
285 NULL,
286 };
287 static const char * const multi_slice[] = {
288 "Single",
289 "Max Macroblocks",
290 "Max Bytes",
291 NULL,
292 };
293 static const char * const entropy_mode[] = {
294 "CAVLC",
295 "CABAC",
296 NULL,
297 };
298 static const char * const mpeg_h264_level[] = {
299 "1",
300 "1b",
301 "1.1",
302 "1.2",
303 "1.3",
304 "2",
305 "2.1",
306 "2.2",
307 "3",
308 "3.1",
309 "3.2",
310 "4",
311 "4.1",
312 "4.2",
313 "5",
314 "5.1",
315 NULL,
316 };
317 static const char * const h264_loop_filter[] = {
318 "Enabled",
319 "Disabled",
320 "Disabled at Slice Boundary",
321 NULL,
322 };
323 static const char * const h264_profile[] = {
324 "Baseline",
325 "Constrained Baseline",
326 "Main",
327 "Extended",
328 "High",
329 "High 10",
330 "High 422",
331 "High 444 Predictive",
332 "High 10 Intra",
333 "High 422 Intra",
334 "High 444 Intra",
335 "CAVLC 444 Intra",
336 "Scalable Baseline",
337 "Scalable High",
338 "Scalable High Intra",
339 "Multiview High",
340 NULL,
341 };
342 static const char * const vui_sar_idc[] = {
343 "Unspecified",
344 "1:1",
345 "12:11",
346 "10:11",
347 "16:11",
348 "40:33",
349 "24:11",
350 "20:11",
351 "32:11",
352 "80:33",
353 "18:11",
354 "15:11",
355 "64:33",
356 "160:99",
357 "4:3",
358 "3:2",
359 "2:1",
360 "Extended SAR",
361 NULL,
362 };
363 static const char * const mpeg_mpeg4_level[] = {
364 "0",
365 "0b",
366 "1",
367 "2",
368 "3",
369 "3b",
370 "4",
371 "5",
372 NULL,
373 };
374 static const char * const mpeg4_profile[] = {
375 "Simple",
f769c260 376 "Advanced Simple",
064f5096
KD
377 "Core",
378 "Simple Scalable",
379 "Advanced Coding Efficency",
380 NULL,
381 };
382
0b159acd
SA
383 static const char * const flash_led_mode[] = {
384 "Off",
385 "Flash",
386 "Torch",
387 NULL,
388 };
389 static const char * const flash_strobe_source[] = {
390 "Software",
391 "External",
392 NULL,
393 };
0996517c 394
c7361ae1
SN
395 static const char * const jpeg_chroma_subsampling[] = {
396 "4:4:4",
397 "4:2:2",
398 "4:2:0",
399 "4:1:1",
400 "4:1:0",
401 "Gray",
402 NULL,
403 };
404
0996517c
HV
405 switch (id) {
406 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
407 return mpeg_audio_sampling_freq;
408 case V4L2_CID_MPEG_AUDIO_ENCODING:
409 return mpeg_audio_encoding;
410 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
411 return mpeg_audio_l1_bitrate;
412 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
413 return mpeg_audio_l2_bitrate;
414 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
415 return mpeg_audio_l3_bitrate;
416 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
417 return mpeg_audio_ac3_bitrate;
418 case V4L2_CID_MPEG_AUDIO_MODE:
419 return mpeg_audio_mode;
420 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
421 return mpeg_audio_mode_extension;
422 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
423 return mpeg_audio_emphasis;
424 case V4L2_CID_MPEG_AUDIO_CRC:
425 return mpeg_audio_crc;
24c19a21
HV
426 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
427 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
428 return mpeg_audio_dec_playback;
0996517c
HV
429 case V4L2_CID_MPEG_VIDEO_ENCODING:
430 return mpeg_video_encoding;
431 case V4L2_CID_MPEG_VIDEO_ASPECT:
432 return mpeg_video_aspect;
433 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
434 return mpeg_video_bitrate_mode;
435 case V4L2_CID_MPEG_STREAM_TYPE:
436 return mpeg_stream_type;
437 case V4L2_CID_MPEG_STREAM_VBI_FMT:
438 return mpeg_stream_vbi_fmt;
439 case V4L2_CID_POWER_LINE_FREQUENCY:
440 return camera_power_line_frequency;
441 case V4L2_CID_EXPOSURE_AUTO:
442 return camera_exposure_auto;
cf072139
SN
443 case V4L2_CID_EXPOSURE_METERING:
444 return camera_exposure_metering;
0996517c
HV
445 case V4L2_CID_COLORFX:
446 return colorfx;
e40a0573
SN
447 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
448 return auto_n_preset_white_balance;
7f84ad8b
SN
449 case V4L2_CID_ISO_SENSITIVITY_AUTO:
450 return camera_iso_sensitivity_auto;
0996517c
HV
451 case V4L2_CID_TUNE_PREEMPHASIS:
452 return tune_preemphasis;
0b159acd
SA
453 case V4L2_CID_FLASH_LED_MODE:
454 return flash_led_mode;
455 case V4L2_CID_FLASH_STROBE_SOURCE:
456 return flash_strobe_source;
064f5096
KD
457 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
458 return header_mode;
459 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
460 return multi_slice;
461 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
462 return entropy_mode;
463 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
464 return mpeg_h264_level;
465 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
466 return h264_loop_filter;
467 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
468 return h264_profile;
469 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
470 return vui_sar_idc;
471 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
472 return mpeg_mpeg4_level;
473 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
474 return mpeg4_profile;
c7361ae1
SN
475 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
476 return jpeg_chroma_subsampling;
477
0996517c
HV
478 default:
479 return NULL;
480 }
481}
482EXPORT_SYMBOL(v4l2_ctrl_get_menu);
483
484/* Return the control name. */
485const char *v4l2_ctrl_get_name(u32 id)
486{
487 switch (id) {
488 /* USER controls */
6c8d6111 489 /* Keep the order of the 'case's the same as in videodev2.h! */
6dd5aff3
MCC
490 case V4L2_CID_USER_CLASS: return "User Controls";
491 case V4L2_CID_BRIGHTNESS: return "Brightness";
492 case V4L2_CID_CONTRAST: return "Contrast";
493 case V4L2_CID_SATURATION: return "Saturation";
494 case V4L2_CID_HUE: return "Hue";
495 case V4L2_CID_AUDIO_VOLUME: return "Volume";
496 case V4L2_CID_AUDIO_BALANCE: return "Balance";
497 case V4L2_CID_AUDIO_BASS: return "Bass";
498 case V4L2_CID_AUDIO_TREBLE: return "Treble";
499 case V4L2_CID_AUDIO_MUTE: return "Mute";
500 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
0996517c
HV
501 case V4L2_CID_BLACK_LEVEL: return "Black Level";
502 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
503 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
504 case V4L2_CID_RED_BALANCE: return "Red Balance";
505 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
506 case V4L2_CID_GAMMA: return "Gamma";
507 case V4L2_CID_EXPOSURE: return "Exposure";
508 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
509 case V4L2_CID_GAIN: return "Gain";
510 case V4L2_CID_HFLIP: return "Horizontal Flip";
511 case V4L2_CID_VFLIP: return "Vertical Flip";
512 case V4L2_CID_HCENTER: return "Horizontal Center";
513 case V4L2_CID_VCENTER: return "Vertical Center";
514 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
515 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
516 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
517 case V4L2_CID_SHARPNESS: return "Sharpness";
518 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
519 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
0996517c
HV
520 case V4L2_CID_COLOR_KILLER: return "Color Killer";
521 case V4L2_CID_COLORFX: return "Color Effects";
522 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
523 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
524 case V4L2_CID_ROTATE: return "Rotate";
525 case V4L2_CID_BG_COLOR: return "Background Color";
6c8d6111 526 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
008d35f2
JFM
527 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
528 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
f08aacf8
HV
529 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: return "Min Number of Capture Buffers";
530 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: return "Min Number of Output Buffers";
cc1d3272 531 case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component";
6491d1ad 532 case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr";
0996517c
HV
533
534 /* MPEG controls */
6c8d6111 535 /* Keep the order of the 'case's the same as in videodev2.h! */
6dd5aff3
MCC
536 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
537 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
538 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
539 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
540 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
541 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
6c8d6111
HV
542 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
543 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
544 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
0996517c 545 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
6dd5aff3
MCC
546 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
547 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
548 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
549 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
550 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
0996517c 551 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
6dd5aff3
MCC
552 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
553 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
554 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
555 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
556 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
24c19a21
HV
557 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK: return "Audio Playback";
558 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
6dd5aff3
MCC
559 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
560 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
561 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
562 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
563 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
564 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
565 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
566 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
567 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
0996517c 568 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
6dd5aff3 569 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
0996517c 570 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
064f5096
KD
571 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE: return "Decoder Slice Interface";
572 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER: return "MPEG4 Loop Filter Enable";
f08aacf8 573 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: return "Number of Intra Refresh MBs";
064f5096
KD
574 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: return "Frame Level Rate Control Enable";
575 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE: return "H264 MB Level Rate Control";
576 case V4L2_CID_MPEG_VIDEO_HEADER_MODE: return "Sequence Header Mode";
f08aacf8 577 case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC: return "Max Number of Reference Pics";
064f5096 578 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP: return "H263 I-Frame QP Value";
f08aacf8
HV
579 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP: return "H263 P-Frame QP Value";
580 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP: return "H263 B-Frame QP Value";
064f5096
KD
581 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP: return "H263 Minimum QP Value";
582 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP: return "H263 Maximum QP Value";
583 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: return "H264 I-Frame QP Value";
f08aacf8
HV
584 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: return "H264 P-Frame QP Value";
585 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP: return "H264 B-Frame QP Value";
064f5096
KD
586 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: return "H264 Maximum QP Value";
587 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: return "H264 Minimum QP Value";
588 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM: return "H264 8x8 Transform Enable";
589 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE: return "H264 CPB Buffer Size";
f08aacf8
HV
590 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: return "H264 Entropy Mode";
591 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD: return "H264 I-Frame Period";
064f5096
KD
592 case V4L2_CID_MPEG_VIDEO_H264_LEVEL: return "H264 Level";
593 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA: return "H264 Loop Filter Alpha Offset";
594 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA: return "H264 Loop Filter Beta Offset";
595 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: return "H264 Loop Filter Mode";
596 case V4L2_CID_MPEG_VIDEO_H264_PROFILE: return "H264 Profile";
597 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT: return "Vertical Size of SAR";
598 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH: return "Horizontal Size of SAR";
599 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE: return "Aspect Ratio VUI Enable";
600 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC: return "VUI Aspect Ratio IDC";
601 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: return "MPEG4 I-Frame QP Value";
f08aacf8
HV
602 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: return "MPEG4 P-Frame QP Value";
603 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP: return "MPEG4 B-Frame QP Value";
064f5096
KD
604 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP: return "MPEG4 Minimum QP Value";
605 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP: return "MPEG4 Maximum QP Value";
606 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL: return "MPEG4 Level";
607 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE: return "MPEG4 Profile";
608 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL: return "Quarter Pixel Search Enable";
f08aacf8
HV
609 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: return "Maximum Bytes in a Slice";
610 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: return "Number of MBs in a Slice";
611 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: return "Slice Partitioning Method";
064f5096 612 case V4L2_CID_MPEG_VIDEO_VBV_SIZE: return "VBV Buffer Size";
24c19a21
HV
613 case V4L2_CID_MPEG_VIDEO_DEC_PTS: return "Video Decoder PTS";
614 case V4L2_CID_MPEG_VIDEO_DEC_FRAME: return "Video Decoder Frame Count";
0996517c
HV
615
616 /* CAMERA controls */
6c8d6111 617 /* Keep the order of the 'case's the same as in videodev2.h! */
0996517c
HV
618 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
619 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
620 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
621 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
622 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
623 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
624 case V4L2_CID_PAN_RESET: return "Pan, Reset";
625 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
626 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
627 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
628 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
629 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
630 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
0996517c
HV
631 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
632 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
633 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
634 case V4L2_CID_PRIVACY: return "Privacy";
6c8d6111
HV
635 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
636 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
d58083c9 637 case V4L2_CID_AUTO_EXPOSURE_BIAS: return "Auto Exposure, Bias";
e40a0573 638 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
44d44a1a 639 case V4L2_CID_WIDE_DYNAMIC_RANGE: return "Wide Dynamic Range";
82b3056c 640 case V4L2_CID_IMAGE_STABILIZATION: return "Image Stabilization";
7f84ad8b
SN
641 case V4L2_CID_ISO_SENSITIVITY: return "ISO Sensitivity";
642 case V4L2_CID_ISO_SENSITIVITY_AUTO: return "ISO Sensitivity, Auto";
cf072139 643 case V4L2_CID_EXPOSURE_METERING: return "Exposure, Metering Mode";
0996517c
HV
644
645 /* FM Radio Modulator control */
6c8d6111 646 /* Keep the order of the 'case's the same as in videodev2.h! */
0996517c
HV
647 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
648 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
649 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
650 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
651 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
652 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
653 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
654 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
655 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
f08aacf8 656 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
0996517c
HV
657 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
658 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
659 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
660 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
661 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
662 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
663 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
f08aacf8 664 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-Emphasis";
0996517c
HV
665 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
666 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
667
0b159acd 668 /* Flash controls */
f08aacf8
HV
669 case V4L2_CID_FLASH_CLASS: return "Flash Controls";
670 case V4L2_CID_FLASH_LED_MODE: return "LED Mode";
671 case V4L2_CID_FLASH_STROBE_SOURCE: return "Strobe Source";
0b159acd 672 case V4L2_CID_FLASH_STROBE: return "Strobe";
f08aacf8
HV
673 case V4L2_CID_FLASH_STROBE_STOP: return "Stop Strobe";
674 case V4L2_CID_FLASH_STROBE_STATUS: return "Strobe Status";
675 case V4L2_CID_FLASH_TIMEOUT: return "Strobe Timeout";
676 case V4L2_CID_FLASH_INTENSITY: return "Intensity, Flash Mode";
677 case V4L2_CID_FLASH_TORCH_INTENSITY: return "Intensity, Torch Mode";
678 case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
0b159acd
SA
679 case V4L2_CID_FLASH_FAULT: return "Faults";
680 case V4L2_CID_FLASH_CHARGE: return "Charge";
f08aacf8 681 case V4L2_CID_FLASH_READY: return "Ready to Strobe";
0b159acd 682
c7361ae1
SN
683 /* JPEG encoder controls */
684 /* Keep the order of the 'case's the same as in videodev2.h! */
685 case V4L2_CID_JPEG_CLASS: return "JPEG Compression Controls";
686 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING: return "Chroma Subsampling";
687 case V4L2_CID_JPEG_RESTART_INTERVAL: return "Restart Interval";
688 case V4L2_CID_JPEG_COMPRESSION_QUALITY: return "Compression Quality";
689 case V4L2_CID_JPEG_ACTIVE_MARKER: return "Active Markers";
690
8c9d236e
SA
691 /* Image source controls */
692 case V4L2_CID_IMAGE_SOURCE_CLASS: return "Image Source Controls";
693 case V4L2_CID_VBLANK: return "Vertical Blanking";
694 case V4L2_CID_HBLANK: return "Horizontal Blanking";
695 case V4L2_CID_ANALOGUE_GAIN: return "Analogue Gain";
696
c643ee13
SA
697 /* Image processing controls */
698 case V4L2_CID_IMAGE_PROC_CLASS: return "Image Processing Controls";
699 case V4L2_CID_LINK_FREQ: return "Link Frequency";
700 case V4L2_CID_PIXEL_RATE: return "Pixel Rate";
701
0996517c
HV
702 default:
703 return NULL;
704 }
705}
706EXPORT_SYMBOL(v4l2_ctrl_get_name);
707
708void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
709 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
710{
711 *name = v4l2_ctrl_get_name(id);
712 *flags = 0;
713
714 switch (id) {
715 case V4L2_CID_AUDIO_MUTE:
716 case V4L2_CID_AUDIO_LOUDNESS:
717 case V4L2_CID_AUTO_WHITE_BALANCE:
718 case V4L2_CID_AUTOGAIN:
719 case V4L2_CID_HFLIP:
720 case V4L2_CID_VFLIP:
721 case V4L2_CID_HUE_AUTO:
722 case V4L2_CID_CHROMA_AGC:
723 case V4L2_CID_COLOR_KILLER:
724 case V4L2_CID_MPEG_AUDIO_MUTE:
725 case V4L2_CID_MPEG_VIDEO_MUTE:
726 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
727 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
728 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
729 case V4L2_CID_FOCUS_AUTO:
730 case V4L2_CID_PRIVACY:
731 case V4L2_CID_AUDIO_LIMITER_ENABLED:
732 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
733 case V4L2_CID_PILOT_TONE_ENABLED:
008d35f2
JFM
734 case V4L2_CID_ILLUMINATORS_1:
735 case V4L2_CID_ILLUMINATORS_2:
0b159acd
SA
736 case V4L2_CID_FLASH_STROBE_STATUS:
737 case V4L2_CID_FLASH_CHARGE:
738 case V4L2_CID_FLASH_READY:
064f5096
KD
739 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
740 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
741 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
742 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
743 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
744 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
745 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
44d44a1a 746 case V4L2_CID_WIDE_DYNAMIC_RANGE:
82b3056c 747 case V4L2_CID_IMAGE_STABILIZATION:
0996517c
HV
748 *type = V4L2_CTRL_TYPE_BOOLEAN;
749 *min = 0;
750 *max = *step = 1;
751 break;
752 case V4L2_CID_PAN_RESET:
753 case V4L2_CID_TILT_RESET:
0b159acd
SA
754 case V4L2_CID_FLASH_STROBE:
755 case V4L2_CID_FLASH_STROBE_STOP:
0996517c
HV
756 *type = V4L2_CTRL_TYPE_BUTTON;
757 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
758 *min = *max = *step = *def = 0;
759 break;
760 case V4L2_CID_POWER_LINE_FREQUENCY:
761 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
762 case V4L2_CID_MPEG_AUDIO_ENCODING:
763 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
764 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
765 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
766 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
767 case V4L2_CID_MPEG_AUDIO_MODE:
768 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
769 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
770 case V4L2_CID_MPEG_AUDIO_CRC:
24c19a21
HV
771 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
772 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
0996517c
HV
773 case V4L2_CID_MPEG_VIDEO_ENCODING:
774 case V4L2_CID_MPEG_VIDEO_ASPECT:
775 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
776 case V4L2_CID_MPEG_STREAM_TYPE:
777 case V4L2_CID_MPEG_STREAM_VBI_FMT:
778 case V4L2_CID_EXPOSURE_AUTO:
779 case V4L2_CID_COLORFX:
e40a0573 780 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
0996517c 781 case V4L2_CID_TUNE_PREEMPHASIS:
0b159acd
SA
782 case V4L2_CID_FLASH_LED_MODE:
783 case V4L2_CID_FLASH_STROBE_SOURCE:
064f5096
KD
784 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
785 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
786 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
787 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
788 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
789 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
790 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
791 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
792 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
c7361ae1 793 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
7f84ad8b 794 case V4L2_CID_ISO_SENSITIVITY_AUTO:
cf072139 795 case V4L2_CID_EXPOSURE_METERING:
0996517c
HV
796 *type = V4L2_CTRL_TYPE_MENU;
797 break;
c643ee13
SA
798 case V4L2_CID_LINK_FREQ:
799 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
800 break;
0996517c
HV
801 case V4L2_CID_RDS_TX_PS_NAME:
802 case V4L2_CID_RDS_TX_RADIO_TEXT:
803 *type = V4L2_CTRL_TYPE_STRING;
804 break;
7f84ad8b 805 case V4L2_CID_ISO_SENSITIVITY:
d58083c9
SN
806 case V4L2_CID_AUTO_EXPOSURE_BIAS:
807 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
808 break;
0996517c
HV
809 case V4L2_CID_USER_CLASS:
810 case V4L2_CID_CAMERA_CLASS:
811 case V4L2_CID_MPEG_CLASS:
812 case V4L2_CID_FM_TX_CLASS:
0b159acd 813 case V4L2_CID_FLASH_CLASS:
c7361ae1 814 case V4L2_CID_JPEG_CLASS:
8c9d236e 815 case V4L2_CID_IMAGE_SOURCE_CLASS:
c643ee13 816 case V4L2_CID_IMAGE_PROC_CLASS:
0996517c
HV
817 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
818 /* You can neither read not write these */
819 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
820 *min = *max = *step = *def = 0;
821 break;
822 case V4L2_CID_BG_COLOR:
823 *type = V4L2_CTRL_TYPE_INTEGER;
824 *step = 1;
825 *min = 0;
826 /* Max is calculated as RGB888 that is 2^24 */
827 *max = 0xFFFFFF;
828 break;
0b159acd 829 case V4L2_CID_FLASH_FAULT:
c7361ae1 830 case V4L2_CID_JPEG_ACTIVE_MARKER:
0b159acd
SA
831 *type = V4L2_CTRL_TYPE_BITMASK;
832 break;
064f5096
KD
833 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
834 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
835 *type = V4L2_CTRL_TYPE_INTEGER;
836 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
837 break;
24c19a21
HV
838 case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
839 case V4L2_CID_MPEG_VIDEO_DEC_PTS:
c643ee13
SA
840 *flags |= V4L2_CTRL_FLAG_VOLATILE;
841 /* Fall through */
842 case V4L2_CID_PIXEL_RATE:
24c19a21 843 *type = V4L2_CTRL_TYPE_INTEGER64;
c643ee13 844 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
ca576811 845 *min = *max = *step = *def = 0;
24c19a21 846 break;
0996517c
HV
847 default:
848 *type = V4L2_CTRL_TYPE_INTEGER;
849 break;
850 }
851 switch (id) {
852 case V4L2_CID_MPEG_AUDIO_ENCODING:
853 case V4L2_CID_MPEG_AUDIO_MODE:
854 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
855 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
856 case V4L2_CID_MPEG_STREAM_TYPE:
857 *flags |= V4L2_CTRL_FLAG_UPDATE;
858 break;
859 case V4L2_CID_AUDIO_VOLUME:
860 case V4L2_CID_AUDIO_BALANCE:
861 case V4L2_CID_AUDIO_BASS:
862 case V4L2_CID_AUDIO_TREBLE:
863 case V4L2_CID_BRIGHTNESS:
864 case V4L2_CID_CONTRAST:
865 case V4L2_CID_SATURATION:
866 case V4L2_CID_HUE:
867 case V4L2_CID_RED_BALANCE:
868 case V4L2_CID_BLUE_BALANCE:
869 case V4L2_CID_GAMMA:
870 case V4L2_CID_SHARPNESS:
871 case V4L2_CID_CHROMA_GAIN:
872 case V4L2_CID_RDS_TX_DEVIATION:
873 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
874 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
875 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
876 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
877 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
878 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
879 case V4L2_CID_PILOT_TONE_DEVIATION:
880 case V4L2_CID_PILOT_TONE_FREQUENCY:
881 case V4L2_CID_TUNE_POWER_LEVEL:
882 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
883 *flags |= V4L2_CTRL_FLAG_SLIDER;
884 break;
885 case V4L2_CID_PAN_RELATIVE:
886 case V4L2_CID_TILT_RELATIVE:
887 case V4L2_CID_FOCUS_RELATIVE:
888 case V4L2_CID_IRIS_RELATIVE:
889 case V4L2_CID_ZOOM_RELATIVE:
890 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
891 break;
0b159acd
SA
892 case V4L2_CID_FLASH_STROBE_STATUS:
893 case V4L2_CID_FLASH_READY:
894 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
895 break;
0996517c
HV
896 }
897}
898EXPORT_SYMBOL(v4l2_ctrl_fill);
899
900/* Helper function to determine whether the control type is compatible with
901 VIDIOC_G/S_CTRL. */
902static bool type_is_int(const struct v4l2_ctrl *ctrl)
903{
904 switch (ctrl->type) {
905 case V4L2_CTRL_TYPE_INTEGER64:
906 case V4L2_CTRL_TYPE_STRING:
907 /* Nope, these need v4l2_ext_control */
908 return false;
909 default:
910 return true;
911 }
912}
913
6e239399
HV
914static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
915{
916 memset(ev->reserved, 0, sizeof(ev->reserved));
917 ev->type = V4L2_EVENT_CTRL;
918 ev->id = ctrl->id;
919 ev->u.ctrl.changes = changes;
920 ev->u.ctrl.type = ctrl->type;
921 ev->u.ctrl.flags = ctrl->flags;
922 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
923 ev->u.ctrl.value64 = 0;
924 else
925 ev->u.ctrl.value64 = ctrl->cur.val64;
926 ev->u.ctrl.minimum = ctrl->minimum;
927 ev->u.ctrl.maximum = ctrl->maximum;
ce580fe5
SA
928 if (ctrl->type == V4L2_CTRL_TYPE_MENU
929 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
6e239399
HV
930 ev->u.ctrl.step = 1;
931 else
932 ev->u.ctrl.step = ctrl->step;
933 ev->u.ctrl.default_value = ctrl->default_value;
934}
935
936static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
937{
938 struct v4l2_event ev;
77068d36 939 struct v4l2_subscribed_event *sev;
6e239399 940
77068d36 941 if (list_empty(&ctrl->ev_subs))
3f66f0ed 942 return;
6e239399
HV
943 fill_event(&ev, ctrl, changes);
944
77068d36 945 list_for_each_entry(sev, &ctrl->ev_subs, node)
e3e72f39
HG
946 if (sev->fh != fh ||
947 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
77068d36 948 v4l2_event_queue_fh(sev->fh, &ev);
6e239399
HV
949}
950
0996517c
HV
951/* Helper function: copy the current control value back to the caller */
952static int cur_to_user(struct v4l2_ext_control *c,
953 struct v4l2_ctrl *ctrl)
954{
955 u32 len;
956
957 switch (ctrl->type) {
958 case V4L2_CTRL_TYPE_STRING:
959 len = strlen(ctrl->cur.string);
960 if (c->size < len + 1) {
961 c->size = len + 1;
962 return -ENOSPC;
963 }
964 return copy_to_user(c->string, ctrl->cur.string,
965 len + 1) ? -EFAULT : 0;
966 case V4L2_CTRL_TYPE_INTEGER64:
967 c->value64 = ctrl->cur.val64;
968 break;
969 default:
970 c->value = ctrl->cur.val;
971 break;
972 }
973 return 0;
974}
975
976/* Helper function: copy the caller-provider value as the new control value */
977static int user_to_new(struct v4l2_ext_control *c,
978 struct v4l2_ctrl *ctrl)
979{
980 int ret;
981 u32 size;
982
2a863793 983 ctrl->is_new = 1;
0996517c
HV
984 switch (ctrl->type) {
985 case V4L2_CTRL_TYPE_INTEGER64:
986 ctrl->val64 = c->value64;
987 break;
988 case V4L2_CTRL_TYPE_STRING:
989 size = c->size;
990 if (size == 0)
991 return -ERANGE;
992 if (size > ctrl->maximum + 1)
993 size = ctrl->maximum + 1;
994 ret = copy_from_user(ctrl->string, c->string, size);
995 if (!ret) {
996 char last = ctrl->string[size - 1];
997
998 ctrl->string[size - 1] = 0;
999 /* If the string was longer than ctrl->maximum,
1000 then return an error. */
1001 if (strlen(ctrl->string) == ctrl->maximum && last)
1002 return -ERANGE;
1003 }
1004 return ret ? -EFAULT : 0;
1005 default:
1006 ctrl->val = c->value;
1007 break;
1008 }
1009 return 0;
1010}
1011
1012/* Helper function: copy the new control value back to the caller */
1013static int new_to_user(struct v4l2_ext_control *c,
1014 struct v4l2_ctrl *ctrl)
1015{
1016 u32 len;
1017
1018 switch (ctrl->type) {
1019 case V4L2_CTRL_TYPE_STRING:
1020 len = strlen(ctrl->string);
1021 if (c->size < len + 1) {
1022 c->size = ctrl->maximum + 1;
1023 return -ENOSPC;
1024 }
1025 return copy_to_user(c->string, ctrl->string,
1026 len + 1) ? -EFAULT : 0;
1027 case V4L2_CTRL_TYPE_INTEGER64:
1028 c->value64 = ctrl->val64;
1029 break;
1030 default:
1031 c->value = ctrl->val;
1032 break;
1033 }
1034 return 0;
1035}
1036
1037/* Copy the new value to the current value. */
ab892bac
HV
1038static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
1039 bool update_inactive)
0996517c 1040{
6e239399
HV
1041 bool changed = false;
1042
0996517c
HV
1043 if (ctrl == NULL)
1044 return;
1045 switch (ctrl->type) {
6e239399
HV
1046 case V4L2_CTRL_TYPE_BUTTON:
1047 changed = true;
1048 break;
0996517c
HV
1049 case V4L2_CTRL_TYPE_STRING:
1050 /* strings are always 0-terminated */
6e239399 1051 changed = strcmp(ctrl->string, ctrl->cur.string);
0996517c
HV
1052 strcpy(ctrl->cur.string, ctrl->string);
1053 break;
1054 case V4L2_CTRL_TYPE_INTEGER64:
6e239399 1055 changed = ctrl->val64 != ctrl->cur.val64;
0996517c
HV
1056 ctrl->cur.val64 = ctrl->val64;
1057 break;
1058 default:
6e239399 1059 changed = ctrl->val != ctrl->cur.val;
0996517c
HV
1060 ctrl->cur.val = ctrl->val;
1061 break;
1062 }
72d877ca 1063 if (update_inactive) {
5626b8c7
HV
1064 /* Note: update_inactive can only be true for auto clusters. */
1065 ctrl->flags &=
1066 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
1067 if (!is_cur_manual(ctrl->cluster[0])) {
72d877ca 1068 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
5626b8c7
HV
1069 if (ctrl->cluster[0]->has_volatiles)
1070 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1071 }
1249a3a8 1072 fh = NULL;
72d877ca 1073 }
639884a6
HV
1074 if (changed || update_inactive) {
1075 /* If a control was changed that was not one of the controls
1076 modified by the application, then send the event to all. */
1077 if (!ctrl->is_new)
1078 fh = NULL;
6e239399
HV
1079 send_event(fh, ctrl,
1080 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
1081 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
639884a6 1082 }
0996517c
HV
1083}
1084
1085/* Copy the current value to the new value */
1086static void cur_to_new(struct v4l2_ctrl *ctrl)
1087{
1088 if (ctrl == NULL)
1089 return;
1090 switch (ctrl->type) {
1091 case V4L2_CTRL_TYPE_STRING:
1092 /* strings are always 0-terminated */
1093 strcpy(ctrl->string, ctrl->cur.string);
1094 break;
1095 case V4L2_CTRL_TYPE_INTEGER64:
1096 ctrl->val64 = ctrl->cur.val64;
1097 break;
1098 default:
1099 ctrl->val = ctrl->cur.val;
1100 break;
1101 }
1102}
1103
1104/* Return non-zero if one or more of the controls in the cluster has a new
1105 value that differs from the current value. */
1106static int cluster_changed(struct v4l2_ctrl *master)
1107{
1108 int diff = 0;
1109 int i;
1110
1111 for (i = 0; !diff && i < master->ncontrols; i++) {
1112 struct v4l2_ctrl *ctrl = master->cluster[i];
1113
1114 if (ctrl == NULL)
1115 continue;
1116 switch (ctrl->type) {
1117 case V4L2_CTRL_TYPE_BUTTON:
1118 /* Button controls are always 'different' */
1119 return 1;
1120 case V4L2_CTRL_TYPE_STRING:
1121 /* strings are always 0-terminated */
1122 diff = strcmp(ctrl->string, ctrl->cur.string);
1123 break;
1124 case V4L2_CTRL_TYPE_INTEGER64:
1125 diff = ctrl->val64 != ctrl->cur.val64;
1126 break;
1127 default:
1128 diff = ctrl->val != ctrl->cur.val;
1129 break;
1130 }
1131 }
1132 return diff;
1133}
1134
eb5b16ef
HV
1135/* Validate integer-type control */
1136static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
0996517c 1137{
eb5b16ef 1138 s32 val = *pval;
0996517c 1139 u32 offset;
0996517c
HV
1140
1141 switch (ctrl->type) {
1142 case V4L2_CTRL_TYPE_INTEGER:
1143 /* Round towards the closest legal value */
1144 val += ctrl->step / 2;
1145 if (val < ctrl->minimum)
1146 val = ctrl->minimum;
1147 if (val > ctrl->maximum)
1148 val = ctrl->maximum;
1149 offset = val - ctrl->minimum;
1150 offset = ctrl->step * (offset / ctrl->step);
1151 val = ctrl->minimum + offset;
eb5b16ef 1152 *pval = val;
0996517c
HV
1153 return 0;
1154
1155 case V4L2_CTRL_TYPE_BOOLEAN:
eb5b16ef 1156 *pval = !!val;
0996517c
HV
1157 return 0;
1158
1159 case V4L2_CTRL_TYPE_MENU:
ce580fe5 1160 case V4L2_CTRL_TYPE_INTEGER_MENU:
0996517c
HV
1161 if (val < ctrl->minimum || val > ctrl->maximum)
1162 return -ERANGE;
ce580fe5
SA
1163 if (ctrl->menu_skip_mask & (1 << val))
1164 return -EINVAL;
1165 if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1166 ctrl->qmenu[val][0] == '\0')
0996517c
HV
1167 return -EINVAL;
1168 return 0;
1169
fa4d7096
HV
1170 case V4L2_CTRL_TYPE_BITMASK:
1171 *pval &= ctrl->maximum;
1172 return 0;
1173
0996517c
HV
1174 case V4L2_CTRL_TYPE_BUTTON:
1175 case V4L2_CTRL_TYPE_CTRL_CLASS:
eb5b16ef 1176 *pval = 0;
0996517c
HV
1177 return 0;
1178
eb5b16ef
HV
1179 default:
1180 return -EINVAL;
1181 }
1182}
1183
1184/* Validate a new control */
1185static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
1186{
1187 char *s = c->string;
1188 size_t len;
1189
1190 switch (ctrl->type) {
1191 case V4L2_CTRL_TYPE_INTEGER:
1192 case V4L2_CTRL_TYPE_BOOLEAN:
1193 case V4L2_CTRL_TYPE_MENU:
ce580fe5 1194 case V4L2_CTRL_TYPE_INTEGER_MENU:
fa4d7096 1195 case V4L2_CTRL_TYPE_BITMASK:
eb5b16ef
HV
1196 case V4L2_CTRL_TYPE_BUTTON:
1197 case V4L2_CTRL_TYPE_CTRL_CLASS:
1198 return validate_new_int(ctrl, &c->value);
1199
0996517c
HV
1200 case V4L2_CTRL_TYPE_INTEGER64:
1201 return 0;
1202
1203 case V4L2_CTRL_TYPE_STRING:
1204 len = strlen(s);
1205 if (len < ctrl->minimum)
1206 return -ERANGE;
1207 if ((len - ctrl->minimum) % ctrl->step)
1208 return -ERANGE;
1209 return 0;
1210
1211 default:
1212 return -EINVAL;
1213 }
1214}
1215
1216static inline u32 node2id(struct list_head *node)
1217{
1218 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
1219}
1220
1221/* Set the handler's error code if it wasn't set earlier already */
1222static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1223{
1224 if (hdl->error == 0)
1225 hdl->error = err;
1226 return err;
1227}
1228
1229/* Initialize the handler */
1230int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
1231 unsigned nr_of_controls_hint)
1232{
77e7c4e6
SA
1233 hdl->lock = &hdl->_lock;
1234 mutex_init(hdl->lock);
0996517c
HV
1235 INIT_LIST_HEAD(&hdl->ctrls);
1236 INIT_LIST_HEAD(&hdl->ctrl_refs);
1237 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
9884d7be
TM
1238 hdl->buckets = kcalloc(hdl->nr_of_buckets, sizeof(hdl->buckets[0]),
1239 GFP_KERNEL);
0996517c
HV
1240 hdl->error = hdl->buckets ? 0 : -ENOMEM;
1241 return hdl->error;
1242}
1243EXPORT_SYMBOL(v4l2_ctrl_handler_init);
1244
1245/* Free all controls and control refs */
1246void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1247{
1248 struct v4l2_ctrl_ref *ref, *next_ref;
1249 struct v4l2_ctrl *ctrl, *next_ctrl;
77068d36 1250 struct v4l2_subscribed_event *sev, *next_sev;
0996517c
HV
1251
1252 if (hdl == NULL || hdl->buckets == NULL)
1253 return;
1254
77e7c4e6 1255 mutex_lock(hdl->lock);
0996517c
HV
1256 /* Free all nodes */
1257 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1258 list_del(&ref->node);
1259 kfree(ref);
1260 }
1261 /* Free all controls owned by the handler */
1262 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1263 list_del(&ctrl->node);
77068d36
HV
1264 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1265 list_del(&sev->node);
0996517c
HV
1266 kfree(ctrl);
1267 }
1268 kfree(hdl->buckets);
1269 hdl->buckets = NULL;
1270 hdl->cached = NULL;
1271 hdl->error = 0;
77e7c4e6 1272 mutex_unlock(hdl->lock);
0996517c
HV
1273}
1274EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1275
1276/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1277 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1278 with applications that do not use the NEXT_CTRL flag.
1279
1280 We just find the n-th private user control. It's O(N), but that should not
1281 be an issue in this particular case. */
1282static struct v4l2_ctrl_ref *find_private_ref(
1283 struct v4l2_ctrl_handler *hdl, u32 id)
1284{
1285 struct v4l2_ctrl_ref *ref;
1286
1287 id -= V4L2_CID_PRIVATE_BASE;
1288 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1289 /* Search for private user controls that are compatible with
1290 VIDIOC_G/S_CTRL. */
1291 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1292 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1293 if (!type_is_int(ref->ctrl))
1294 continue;
1295 if (id == 0)
1296 return ref;
1297 id--;
1298 }
1299 }
1300 return NULL;
1301}
1302
1303/* Find a control with the given ID. */
1304static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1305{
1306 struct v4l2_ctrl_ref *ref;
1307 int bucket;
1308
1309 id &= V4L2_CTRL_ID_MASK;
1310
1311 /* Old-style private controls need special handling */
1312 if (id >= V4L2_CID_PRIVATE_BASE)
1313 return find_private_ref(hdl, id);
1314 bucket = id % hdl->nr_of_buckets;
1315
1316 /* Simple optimization: cache the last control found */
1317 if (hdl->cached && hdl->cached->ctrl->id == id)
1318 return hdl->cached;
1319
1320 /* Not in cache, search the hash */
1321 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1322 while (ref && ref->ctrl->id != id)
1323 ref = ref->next;
1324
1325 if (ref)
1326 hdl->cached = ref; /* cache it! */
1327 return ref;
1328}
1329
1330/* Find a control with the given ID. Take the handler's lock first. */
1331static struct v4l2_ctrl_ref *find_ref_lock(
1332 struct v4l2_ctrl_handler *hdl, u32 id)
1333{
1334 struct v4l2_ctrl_ref *ref = NULL;
1335
1336 if (hdl) {
77e7c4e6 1337 mutex_lock(hdl->lock);
0996517c 1338 ref = find_ref(hdl, id);
77e7c4e6 1339 mutex_unlock(hdl->lock);
0996517c
HV
1340 }
1341 return ref;
1342}
1343
1344/* Find a control with the given ID. */
1345struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1346{
1347 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1348
1349 return ref ? ref->ctrl : NULL;
1350}
1351EXPORT_SYMBOL(v4l2_ctrl_find);
1352
1353/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1354static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1355 struct v4l2_ctrl *ctrl)
1356{
1357 struct v4l2_ctrl_ref *ref;
1358 struct v4l2_ctrl_ref *new_ref;
1359 u32 id = ctrl->id;
1360 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
1361 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
1362
1363 /* Automatically add the control class if it is not yet present. */
1364 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1365 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1366 return hdl->error;
1367
1368 if (hdl->error)
1369 return hdl->error;
1370
1371 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
1372 if (!new_ref)
1373 return handler_set_err(hdl, -ENOMEM);
1374 new_ref->ctrl = ctrl;
1375 if (ctrl->handler == hdl) {
1376 /* By default each control starts in a cluster of its own.
1377 new_ref->ctrl is basically a cluster array with one
1378 element, so that's perfect to use as the cluster pointer.
1379 But only do this for the handler that owns the control. */
1380 ctrl->cluster = &new_ref->ctrl;
1381 ctrl->ncontrols = 1;
1382 }
1383
1384 INIT_LIST_HEAD(&new_ref->node);
1385
77e7c4e6 1386 mutex_lock(hdl->lock);
0996517c
HV
1387
1388 /* Add immediately at the end of the list if the list is empty, or if
1389 the last element in the list has a lower ID.
1390 This ensures that when elements are added in ascending order the
1391 insertion is an O(1) operation. */
1392 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1393 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1394 goto insert_in_hash;
1395 }
1396
1397 /* Find insert position in sorted list */
1398 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1399 if (ref->ctrl->id < id)
1400 continue;
1401 /* Don't add duplicates */
1402 if (ref->ctrl->id == id) {
1403 kfree(new_ref);
1404 goto unlock;
1405 }
1406 list_add(&new_ref->node, ref->node.prev);
1407 break;
1408 }
1409
1410insert_in_hash:
1411 /* Insert the control node in the hash */
1412 new_ref->next = hdl->buckets[bucket];
1413 hdl->buckets[bucket] = new_ref;
1414
1415unlock:
77e7c4e6 1416 mutex_unlock(hdl->lock);
0996517c
HV
1417 return 0;
1418}
1419
1420/* Add a new control */
1421static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1422 const struct v4l2_ctrl_ops *ops,
1423 u32 id, const char *name, enum v4l2_ctrl_type type,
1424 s32 min, s32 max, u32 step, s32 def,
ce580fe5
SA
1425 u32 flags, const char * const *qmenu,
1426 const s64 *qmenu_int, void *priv)
0996517c
HV
1427{
1428 struct v4l2_ctrl *ctrl;
1429 unsigned sz_extra = 0;
1430
1431 if (hdl->error)
1432 return NULL;
1433
1434 /* Sanity checks */
1435 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
0996517c 1436 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
fa4d7096 1437 (type == V4L2_CTRL_TYPE_BITMASK && max == 0) ||
0996517c 1438 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
ce580fe5 1439 (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL) ||
0996517c
HV
1440 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1441 handler_set_err(hdl, -ERANGE);
1442 return NULL;
1443 }
fa4d7096
HV
1444 if (type != V4L2_CTRL_TYPE_BITMASK && max < min) {
1445 handler_set_err(hdl, -ERANGE);
1446 return NULL;
1447 }
02ac0480
HV
1448 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1449 type == V4L2_CTRL_TYPE_MENU ||
ce580fe5 1450 type == V4L2_CTRL_TYPE_INTEGER_MENU ||
02ac0480
HV
1451 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1452 (def < min || def > max)) {
1453 handler_set_err(hdl, -ERANGE);
1454 return NULL;
1455 }
fa4d7096
HV
1456 if (type == V4L2_CTRL_TYPE_BITMASK && ((def & ~max) || min || step)) {
1457 handler_set_err(hdl, -ERANGE);
1458 return NULL;
1459 }
0996517c
HV
1460
1461 if (type == V4L2_CTRL_TYPE_BUTTON)
1462 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1463 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1464 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1465 else if (type == V4L2_CTRL_TYPE_STRING)
1466 sz_extra += 2 * (max + 1);
1467
1468 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1469 if (ctrl == NULL) {
1470 handler_set_err(hdl, -ENOMEM);
1471 return NULL;
1472 }
1473
1474 INIT_LIST_HEAD(&ctrl->node);
77068d36 1475 INIT_LIST_HEAD(&ctrl->ev_subs);
0996517c
HV
1476 ctrl->handler = hdl;
1477 ctrl->ops = ops;
1478 ctrl->id = id;
1479 ctrl->name = name;
1480 ctrl->type = type;
1481 ctrl->flags = flags;
1482 ctrl->minimum = min;
1483 ctrl->maximum = max;
1484 ctrl->step = step;
ce580fe5
SA
1485 if (type == V4L2_CTRL_TYPE_MENU)
1486 ctrl->qmenu = qmenu;
1487 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
1488 ctrl->qmenu_int = qmenu_int;
0996517c
HV
1489 ctrl->priv = priv;
1490 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1491
1492 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1493 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1494 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1495 if (ctrl->minimum)
1496 memset(ctrl->cur.string, ' ', ctrl->minimum);
1497 }
1498 if (handler_new_ref(hdl, ctrl)) {
1499 kfree(ctrl);
1500 return NULL;
1501 }
77e7c4e6 1502 mutex_lock(hdl->lock);
0996517c 1503 list_add_tail(&ctrl->node, &hdl->ctrls);
77e7c4e6 1504 mutex_unlock(hdl->lock);
0996517c
HV
1505 return ctrl;
1506}
1507
1508struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1509 const struct v4l2_ctrl_config *cfg, void *priv)
1510{
1511 bool is_menu;
1512 struct v4l2_ctrl *ctrl;
1513 const char *name = cfg->name;
513521ea 1514 const char * const *qmenu = cfg->qmenu;
ce580fe5 1515 const s64 *qmenu_int = cfg->qmenu_int;
0996517c
HV
1516 enum v4l2_ctrl_type type = cfg->type;
1517 u32 flags = cfg->flags;
1518 s32 min = cfg->min;
1519 s32 max = cfg->max;
1520 u32 step = cfg->step;
1521 s32 def = cfg->def;
1522
1523 if (name == NULL)
1524 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1525 &def, &flags);
1526
ce580fe5
SA
1527 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU ||
1528 cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU);
0996517c
HV
1529 if (is_menu)
1530 WARN_ON(step);
1531 else
1532 WARN_ON(cfg->menu_skip_mask);
ce580fe5 1533 if (cfg->type == V4L2_CTRL_TYPE_MENU && qmenu == NULL)
0996517c 1534 qmenu = v4l2_ctrl_get_menu(cfg->id);
ce580fe5
SA
1535 else if (cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU &&
1536 qmenu_int == NULL) {
1537 handler_set_err(hdl, -EINVAL);
1538 return NULL;
1539 }
0996517c
HV
1540
1541 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1542 type, min, max,
1543 is_menu ? cfg->menu_skip_mask : step,
ce580fe5 1544 def, flags, qmenu, qmenu_int, priv);
88365105 1545 if (ctrl)
0996517c 1546 ctrl->is_private = cfg->is_private;
0996517c
HV
1547 return ctrl;
1548}
1549EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1550
1551/* Helper function for standard non-menu controls */
1552struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1553 const struct v4l2_ctrl_ops *ops,
1554 u32 id, s32 min, s32 max, u32 step, s32 def)
1555{
1556 const char *name;
1557 enum v4l2_ctrl_type type;
1558 u32 flags;
1559
1560 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
ce580fe5
SA
1561 if (type == V4L2_CTRL_TYPE_MENU
1562 || type == V4L2_CTRL_TYPE_INTEGER_MENU) {
0996517c
HV
1563 handler_set_err(hdl, -EINVAL);
1564 return NULL;
1565 }
1566 return v4l2_ctrl_new(hdl, ops, id, name, type,
ce580fe5 1567 min, max, step, def, flags, NULL, NULL, NULL);
0996517c
HV
1568}
1569EXPORT_SYMBOL(v4l2_ctrl_new_std);
1570
1571/* Helper function for standard menu controls */
1572struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1573 const struct v4l2_ctrl_ops *ops,
1574 u32 id, s32 max, s32 mask, s32 def)
1575{
513521ea 1576 const char * const *qmenu = v4l2_ctrl_get_menu(id);
0996517c
HV
1577 const char *name;
1578 enum v4l2_ctrl_type type;
1579 s32 min;
1580 s32 step;
1581 u32 flags;
1582
1583 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1584 if (type != V4L2_CTRL_TYPE_MENU) {
1585 handler_set_err(hdl, -EINVAL);
1586 return NULL;
1587 }
1588 return v4l2_ctrl_new(hdl, ops, id, name, type,
ce580fe5 1589 0, max, mask, def, flags, qmenu, NULL, NULL);
0996517c
HV
1590}
1591EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1592
515f3287
SN
1593/* Helper function for standard integer menu controls */
1594struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
1595 const struct v4l2_ctrl_ops *ops,
1596 u32 id, s32 max, s32 def, const s64 *qmenu_int)
1597{
1598 const char *name;
1599 enum v4l2_ctrl_type type;
1600 s32 min;
1601 s32 step;
1602 u32 flags;
1603
1604 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1605 if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
1606 handler_set_err(hdl, -EINVAL);
1607 return NULL;
1608 }
1609 return v4l2_ctrl_new(hdl, ops, id, name, type,
1610 0, max, 0, def, flags, NULL, qmenu_int, NULL);
1611}
1612EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
1613
0996517c
HV
1614/* Add a control from another handler to this handler */
1615struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1616 struct v4l2_ctrl *ctrl)
1617{
1618 if (hdl == NULL || hdl->error)
1619 return NULL;
1620 if (ctrl == NULL) {
1621 handler_set_err(hdl, -EINVAL);
1622 return NULL;
1623 }
1624 if (ctrl->handler == hdl)
1625 return ctrl;
1626 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1627}
1628EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1629
1630/* Add the controls from another handler to our own. */
1631int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1632 struct v4l2_ctrl_handler *add)
1633{
072e6602 1634 struct v4l2_ctrl_ref *ref;
0996517c
HV
1635 int ret = 0;
1636
1637 /* Do nothing if either handler is NULL or if they are the same */
1638 if (!hdl || !add || hdl == add)
1639 return 0;
1640 if (hdl->error)
1641 return hdl->error;
77e7c4e6 1642 mutex_lock(add->lock);
072e6602
HV
1643 list_for_each_entry(ref, &add->ctrl_refs, node) {
1644 struct v4l2_ctrl *ctrl = ref->ctrl;
1645
0996517c
HV
1646 /* Skip handler-private controls. */
1647 if (ctrl->is_private)
1648 continue;
6e239399
HV
1649 /* And control classes */
1650 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1651 continue;
0996517c
HV
1652 ret = handler_new_ref(hdl, ctrl);
1653 if (ret)
1654 break;
1655 }
77e7c4e6 1656 mutex_unlock(add->lock);
0996517c
HV
1657 return ret;
1658}
1659EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1660
1661/* Cluster controls */
1662void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1663{
5626b8c7 1664 bool has_volatiles = false;
0996517c
HV
1665 int i;
1666
1667 /* The first control is the master control and it must not be NULL */
72d877ca 1668 BUG_ON(ncontrols == 0 || controls[0] == NULL);
0996517c
HV
1669
1670 for (i = 0; i < ncontrols; i++) {
1671 if (controls[i]) {
1672 controls[i]->cluster = controls;
1673 controls[i]->ncontrols = ncontrols;
5626b8c7
HV
1674 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
1675 has_volatiles = true;
0996517c
HV
1676 }
1677 }
5626b8c7 1678 controls[0]->has_volatiles = has_volatiles;
0996517c
HV
1679}
1680EXPORT_SYMBOL(v4l2_ctrl_cluster);
1681
72d877ca
HV
1682void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1683 u8 manual_val, bool set_volatile)
1684{
1685 struct v4l2_ctrl *master = controls[0];
5626b8c7 1686 u32 flag = 0;
72d877ca
HV
1687 int i;
1688
1689 v4l2_ctrl_cluster(ncontrols, controls);
1690 WARN_ON(ncontrols <= 1);
82a7c049 1691 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
5626b8c7 1692 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
72d877ca 1693 master->is_auto = true;
5626b8c7 1694 master->has_volatiles = set_volatile;
72d877ca
HV
1695 master->manual_mode_value = manual_val;
1696 master->flags |= V4L2_CTRL_FLAG_UPDATE;
5626b8c7
HV
1697
1698 if (!is_cur_manual(master))
1699 flag = V4L2_CTRL_FLAG_INACTIVE |
1700 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
72d877ca
HV
1701
1702 for (i = 1; i < ncontrols; i++)
88365105 1703 if (controls[i])
72d877ca 1704 controls[i]->flags |= flag;
72d877ca
HV
1705}
1706EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1707
0996517c
HV
1708/* Activate/deactivate a control. */
1709void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1710{
6e239399
HV
1711 /* invert since the actual flag is called 'inactive' */
1712 bool inactive = !active;
1713 bool old;
1714
0996517c
HV
1715 if (ctrl == NULL)
1716 return;
1717
6e239399 1718 if (inactive)
0996517c 1719 /* set V4L2_CTRL_FLAG_INACTIVE */
6e239399 1720 old = test_and_set_bit(4, &ctrl->flags);
0996517c
HV
1721 else
1722 /* clear V4L2_CTRL_FLAG_INACTIVE */
6e239399
HV
1723 old = test_and_clear_bit(4, &ctrl->flags);
1724 if (old != inactive)
1725 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
0996517c
HV
1726}
1727EXPORT_SYMBOL(v4l2_ctrl_activate);
1728
1729/* Grab/ungrab a control.
1730 Typically used when streaming starts and you want to grab controls,
1731 preventing the user from changing them.
1732
1733 Just call this and the framework will block any attempts to change
1734 these controls. */
1735void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1736{
6e239399
HV
1737 bool old;
1738
0996517c
HV
1739 if (ctrl == NULL)
1740 return;
1741
6e239399 1742 v4l2_ctrl_lock(ctrl);
0996517c
HV
1743 if (grabbed)
1744 /* set V4L2_CTRL_FLAG_GRABBED */
6e239399 1745 old = test_and_set_bit(1, &ctrl->flags);
0996517c
HV
1746 else
1747 /* clear V4L2_CTRL_FLAG_GRABBED */
6e239399
HV
1748 old = test_and_clear_bit(1, &ctrl->flags);
1749 if (old != grabbed)
1750 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1751 v4l2_ctrl_unlock(ctrl);
0996517c
HV
1752}
1753EXPORT_SYMBOL(v4l2_ctrl_grab);
1754
1755/* Log the control name and value */
1756static void log_ctrl(const struct v4l2_ctrl *ctrl,
1757 const char *prefix, const char *colon)
1758{
0996517c
HV
1759 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1760 return;
1761 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1762 return;
1763
1764 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1765
1766 switch (ctrl->type) {
1767 case V4L2_CTRL_TYPE_INTEGER:
1768 printk(KERN_CONT "%d", ctrl->cur.val);
1769 break;
1770 case V4L2_CTRL_TYPE_BOOLEAN:
1771 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1772 break;
1773 case V4L2_CTRL_TYPE_MENU:
1774 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1775 break;
ce580fe5
SA
1776 case V4L2_CTRL_TYPE_INTEGER_MENU:
1777 printk(KERN_CONT "%lld", ctrl->qmenu_int[ctrl->cur.val]);
1778 break;
fa4d7096
HV
1779 case V4L2_CTRL_TYPE_BITMASK:
1780 printk(KERN_CONT "0x%08x", ctrl->cur.val);
1781 break;
0996517c
HV
1782 case V4L2_CTRL_TYPE_INTEGER64:
1783 printk(KERN_CONT "%lld", ctrl->cur.val64);
1784 break;
1785 case V4L2_CTRL_TYPE_STRING:
1786 printk(KERN_CONT "%s", ctrl->cur.string);
1787 break;
1788 default:
1789 printk(KERN_CONT "unknown type %d", ctrl->type);
1790 break;
1791 }
88365105
HV
1792 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
1793 V4L2_CTRL_FLAG_GRABBED |
1794 V4L2_CTRL_FLAG_VOLATILE)) {
1795 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1796 printk(KERN_CONT " inactive");
1797 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
1798 printk(KERN_CONT " grabbed");
1799 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
1800 printk(KERN_CONT " volatile");
1801 }
1802 printk(KERN_CONT "\n");
0996517c
HV
1803}
1804
1805/* Log all controls owned by the handler */
1806void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1807 const char *prefix)
1808{
1809 struct v4l2_ctrl *ctrl;
1810 const char *colon = "";
1811 int len;
1812
1813 if (hdl == NULL)
1814 return;
1815 if (prefix == NULL)
1816 prefix = "";
1817 len = strlen(prefix);
1818 if (len && prefix[len - 1] != ' ')
1819 colon = ": ";
77e7c4e6 1820 mutex_lock(hdl->lock);
0996517c
HV
1821 list_for_each_entry(ctrl, &hdl->ctrls, node)
1822 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1823 log_ctrl(ctrl, prefix, colon);
77e7c4e6 1824 mutex_unlock(hdl->lock);
0996517c
HV
1825}
1826EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1827
1828/* Call s_ctrl for all controls owned by the handler */
1829int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1830{
1831 struct v4l2_ctrl *ctrl;
1832 int ret = 0;
1833
1834 if (hdl == NULL)
1835 return 0;
77e7c4e6 1836 mutex_lock(hdl->lock);
0996517c
HV
1837 list_for_each_entry(ctrl, &hdl->ctrls, node)
1838 ctrl->done = false;
1839
1840 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1841 struct v4l2_ctrl *master = ctrl->cluster[0];
1842 int i;
1843
1844 /* Skip if this control was already handled by a cluster. */
71c6c4c9
HV
1845 /* Skip button controls and read-only controls. */
1846 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1847 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
0996517c
HV
1848 continue;
1849
2a863793
HV
1850 for (i = 0; i < master->ncontrols; i++) {
1851 if (master->cluster[i]) {
1852 cur_to_new(master->cluster[i]);
1853 master->cluster[i]->is_new = 1;
71c6c4c9 1854 master->cluster[i]->done = true;
2a863793
HV
1855 }
1856 }
54c911eb 1857 ret = call_op(master, s_ctrl);
0996517c
HV
1858 if (ret)
1859 break;
0996517c 1860 }
77e7c4e6 1861 mutex_unlock(hdl->lock);
0996517c
HV
1862 return ret;
1863}
1864EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1865
1866/* Implement VIDIOC_QUERYCTRL */
1867int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1868{
1869 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1870 struct v4l2_ctrl_ref *ref;
1871 struct v4l2_ctrl *ctrl;
1872
1873 if (hdl == NULL)
1874 return -EINVAL;
1875
77e7c4e6 1876 mutex_lock(hdl->lock);
0996517c
HV
1877
1878 /* Try to find it */
1879 ref = find_ref(hdl, id);
1880
1881 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1882 /* Find the next control with ID > qc->id */
1883
1884 /* Did we reach the end of the control list? */
1885 if (id >= node2id(hdl->ctrl_refs.prev)) {
1886 ref = NULL; /* Yes, so there is no next control */
1887 } else if (ref) {
1888 /* We found a control with the given ID, so just get
1889 the next one in the list. */
1890 ref = list_entry(ref->node.next, typeof(*ref), node);
1891 } else {
1892 /* No control with the given ID exists, so start
1893 searching for the next largest ID. We know there
1894 is one, otherwise the first 'if' above would have
1895 been true. */
1896 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1897 if (id < ref->ctrl->id)
1898 break;
1899 }
1900 }
77e7c4e6 1901 mutex_unlock(hdl->lock);
0996517c
HV
1902 if (!ref)
1903 return -EINVAL;
1904
1905 ctrl = ref->ctrl;
1906 memset(qc, 0, sizeof(*qc));
829fb2dc
HV
1907 if (id >= V4L2_CID_PRIVATE_BASE)
1908 qc->id = id;
1909 else
1910 qc->id = ctrl->id;
0996517c
HV
1911 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1912 qc->minimum = ctrl->minimum;
1913 qc->maximum = ctrl->maximum;
1914 qc->default_value = ctrl->default_value;
ce580fe5
SA
1915 if (ctrl->type == V4L2_CTRL_TYPE_MENU
1916 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
0996517c
HV
1917 qc->step = 1;
1918 else
1919 qc->step = ctrl->step;
1920 qc->flags = ctrl->flags;
1921 qc->type = ctrl->type;
1922 return 0;
1923}
1924EXPORT_SYMBOL(v4l2_queryctrl);
1925
1926int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1927{
87a0c94c
HV
1928 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1929 return -EINVAL;
0996517c
HV
1930 return v4l2_queryctrl(sd->ctrl_handler, qc);
1931}
1932EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1933
1934/* Implement VIDIOC_QUERYMENU */
1935int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1936{
1937 struct v4l2_ctrl *ctrl;
1938 u32 i = qm->index;
1939
1940 ctrl = v4l2_ctrl_find(hdl, qm->id);
1941 if (!ctrl)
1942 return -EINVAL;
1943
1944 qm->reserved = 0;
1945 /* Sanity checks */
ce580fe5
SA
1946 switch (ctrl->type) {
1947 case V4L2_CTRL_TYPE_MENU:
1948 if (ctrl->qmenu == NULL)
1949 return -EINVAL;
1950 break;
1951 case V4L2_CTRL_TYPE_INTEGER_MENU:
1952 if (ctrl->qmenu_int == NULL)
1953 return -EINVAL;
1954 break;
1955 default:
1956 return -EINVAL;
1957 }
1958
1959 if (i < ctrl->minimum || i > ctrl->maximum)
0996517c 1960 return -EINVAL;
ce580fe5 1961
0996517c
HV
1962 /* Use mask to see if this menu item should be skipped */
1963 if (ctrl->menu_skip_mask & (1 << i))
1964 return -EINVAL;
1965 /* Empty menu items should also be skipped */
ce580fe5
SA
1966 if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
1967 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1968 return -EINVAL;
1969 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1970 } else {
1971 qm->value = ctrl->qmenu_int[i];
1972 }
0996517c
HV
1973 return 0;
1974}
1975EXPORT_SYMBOL(v4l2_querymenu);
1976
1977int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1978{
1979 return v4l2_querymenu(sd->ctrl_handler, qm);
1980}
1981EXPORT_SYMBOL(v4l2_subdev_querymenu);
1982
1983
1984
1985/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1986
1987 It is not a fully atomic operation, just best-effort only. After all, if
1988 multiple controls have to be set through multiple i2c writes (for example)
1989 then some initial writes may succeed while others fail. Thus leaving the
1990 system in an inconsistent state. The question is how much effort you are
1991 willing to spend on trying to make something atomic that really isn't.
1992
1993 From the point of view of an application the main requirement is that
1994 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1995 error should be returned without actually affecting any controls.
1996
1997 If all the values are correct, then it is acceptable to just give up
1998 in case of low-level errors.
1999
2000 It is important though that the application can tell when only a partial
2001 configuration was done. The way we do that is through the error_idx field
2002 of struct v4l2_ext_controls: if that is equal to the count field then no
2003 controls were affected. Otherwise all controls before that index were
2004 successful in performing their 'get' or 'set' operation, the control at
2005 the given index failed, and you don't know what happened with the controls
2006 after the failed one. Since if they were part of a control cluster they
2007 could have been successfully processed (if a cluster member was encountered
2008 at index < error_idx), they could have failed (if a cluster member was at
2009 error_idx), or they may not have been processed yet (if the first cluster
2010 member appeared after error_idx).
2011
2012 It is all fairly theoretical, though. In practice all you can do is to
2013 bail out. If error_idx == count, then it is an application bug. If
2014 error_idx < count then it is only an application bug if the error code was
2015 EBUSY. That usually means that something started streaming just when you
2016 tried to set the controls. In all other cases it is a driver/hardware
2017 problem and all you can do is to retry or bail out.
2018
2019 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
2020 never modifies controls the error_idx is just set to whatever control
2021 has an invalid value.
2022 */
2023
2024/* Prepare for the extended g/s/try functions.
2025 Find the controls in the control array and do some basic checks. */
2026static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
2027 struct v4l2_ext_controls *cs,
eb5b16ef 2028 struct v4l2_ctrl_helper *helpers)
0996517c 2029{
eb5b16ef
HV
2030 struct v4l2_ctrl_helper *h;
2031 bool have_clusters = false;
0996517c
HV
2032 u32 i;
2033
eb5b16ef 2034 for (i = 0, h = helpers; i < cs->count; i++, h++) {
0996517c 2035 struct v4l2_ext_control *c = &cs->controls[i];
eb5b16ef 2036 struct v4l2_ctrl_ref *ref;
0996517c
HV
2037 struct v4l2_ctrl *ctrl;
2038 u32 id = c->id & V4L2_CTRL_ID_MASK;
2039
37cd3b73 2040 cs->error_idx = i;
0996517c
HV
2041
2042 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
2043 return -EINVAL;
2044
2045 /* Old-style private controls are not allowed for
2046 extended controls */
2047 if (id >= V4L2_CID_PRIVATE_BASE)
2048 return -EINVAL;
eb5b16ef
HV
2049 ref = find_ref_lock(hdl, id);
2050 if (ref == NULL)
0996517c 2051 return -EINVAL;
eb5b16ef 2052 ctrl = ref->ctrl;
0996517c
HV
2053 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
2054 return -EINVAL;
2055
eb5b16ef
HV
2056 if (ctrl->cluster[0]->ncontrols > 1)
2057 have_clusters = true;
2058 if (ctrl->cluster[0] != ctrl)
2059 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
2060 /* Store the ref to the master control of the cluster */
2061 h->mref = ref;
2062 h->ctrl = ctrl;
2063 /* Initially set next to 0, meaning that there is no other
2064 control in this helper array belonging to the same
2065 cluster */
2066 h->next = 0;
0996517c 2067 }
0996517c 2068
eb5b16ef
HV
2069 /* We are done if there were no controls that belong to a multi-
2070 control cluster. */
2071 if (!have_clusters)
2072 return 0;
0996517c 2073
eb5b16ef
HV
2074 /* The code below figures out in O(n) time which controls in the list
2075 belong to the same cluster. */
0996517c 2076
eb5b16ef 2077 /* This has to be done with the handler lock taken. */
77e7c4e6 2078 mutex_lock(hdl->lock);
0996517c 2079
eb5b16ef
HV
2080 /* First zero the helper field in the master control references */
2081 for (i = 0; i < cs->count; i++)
2082 helpers[i].mref->helper = 0;
2083 for (i = 0, h = helpers; i < cs->count; i++, h++) {
2084 struct v4l2_ctrl_ref *mref = h->mref;
2085
2086 /* If the mref->helper is set, then it points to an earlier
2087 helper that belongs to the same cluster. */
2088 if (mref->helper) {
2089 /* Set the next field of mref->helper to the current
2090 index: this means that that earlier helper now
2091 points to the next helper in the same cluster. */
2092 mref->helper->next = i;
2093 /* mref should be set only for the first helper in the
2094 cluster, clear the others. */
2095 h->mref = NULL;
2096 }
2097 /* Point the mref helper to the current helper struct. */
2098 mref->helper = h;
0996517c 2099 }
77e7c4e6 2100 mutex_unlock(hdl->lock);
eb5b16ef 2101 return 0;
0996517c
HV
2102}
2103
2104/* Handles the corner case where cs->count == 0. It checks whether the
2105 specified control class exists. If that class ID is 0, then it checks
2106 whether there are any controls at all. */
2107static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
2108{
2109 if (ctrl_class == 0)
2110 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
2111 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
2112}
2113
2114
2115
2116/* Get extended controls. Allocates the helpers array if needed. */
2117int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2118{
eb5b16ef
HV
2119 struct v4l2_ctrl_helper helper[4];
2120 struct v4l2_ctrl_helper *helpers = helper;
0996517c 2121 int ret;
ddac5c10 2122 int i, j;
0996517c
HV
2123
2124 cs->error_idx = cs->count;
2125 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
2126
2127 if (hdl == NULL)
2128 return -EINVAL;
2129
2130 if (cs->count == 0)
2131 return class_check(hdl, cs->ctrl_class);
2132
2133 if (cs->count > ARRAY_SIZE(helper)) {
5f0049bd
XW
2134 helpers = kmalloc_array(cs->count, sizeof(helper[0]),
2135 GFP_KERNEL);
0996517c
HV
2136 if (helpers == NULL)
2137 return -ENOMEM;
2138 }
2139
37cd3b73
HV
2140 ret = prepare_ext_ctrls(hdl, cs, helpers);
2141 cs->error_idx = cs->count;
0996517c
HV
2142
2143 for (i = 0; !ret && i < cs->count; i++)
2144 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2145 ret = -EACCES;
2146
2147 for (i = 0; !ret && i < cs->count; i++) {
eb5b16ef
HV
2148 int (*ctrl_to_user)(struct v4l2_ext_control *c,
2149 struct v4l2_ctrl *ctrl) = cur_to_user;
2150 struct v4l2_ctrl *master;
0996517c 2151
eb5b16ef 2152 if (helpers[i].mref == NULL)
0996517c
HV
2153 continue;
2154
eb5b16ef 2155 master = helpers[i].mref->ctrl;
0996517c
HV
2156 cs->error_idx = i;
2157
2158 v4l2_ctrl_lock(master);
ddac5c10
HV
2159
2160 /* g_volatile_ctrl will update the new control values */
5626b8c7
HV
2161 if ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
2162 (master->has_volatiles && !is_cur_manual(master))) {
ddac5c10
HV
2163 for (j = 0; j < master->ncontrols; j++)
2164 cur_to_new(master->cluster[j]);
54c911eb 2165 ret = call_op(master, g_volatile_ctrl);
eb5b16ef 2166 ctrl_to_user = new_to_user;
ddac5c10
HV
2167 }
2168 /* If OK, then copy the current (for non-volatile controls)
2169 or the new (for volatile controls) control values to the
2170 caller */
eb5b16ef
HV
2171 if (!ret) {
2172 u32 idx = i;
2173
2174 do {
2175 ret = ctrl_to_user(cs->controls + idx,
2176 helpers[idx].ctrl);
2177 idx = helpers[idx].next;
2178 } while (!ret && idx);
2179 }
0996517c 2180 v4l2_ctrl_unlock(master);
0996517c
HV
2181 }
2182
2183 if (cs->count > ARRAY_SIZE(helper))
2184 kfree(helpers);
2185 return ret;
2186}
2187EXPORT_SYMBOL(v4l2_g_ext_ctrls);
2188
2189int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2190{
2191 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
2192}
2193EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
2194
2195/* Helper function to get a single control */
2196static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
2197{
2198 struct v4l2_ctrl *master = ctrl->cluster[0];
2199 int ret = 0;
ddac5c10 2200 int i;
0996517c
HV
2201
2202 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2203 return -EACCES;
2204
2205 v4l2_ctrl_lock(master);
2206 /* g_volatile_ctrl will update the current control values */
5626b8c7 2207 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
ddac5c10
HV
2208 for (i = 0; i < master->ncontrols; i++)
2209 cur_to_new(master->cluster[i]);
54c911eb 2210 ret = call_op(master, g_volatile_ctrl);
ddac5c10
HV
2211 *val = ctrl->val;
2212 } else {
2213 *val = ctrl->cur.val;
2214 }
0996517c
HV
2215 v4l2_ctrl_unlock(master);
2216 return ret;
2217}
2218
2219int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
2220{
2221 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2222
2223 if (ctrl == NULL || !type_is_int(ctrl))
2224 return -EINVAL;
2225 return get_ctrl(ctrl, &control->value);
2226}
2227EXPORT_SYMBOL(v4l2_g_ctrl);
2228
2229int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2230{
2231 return v4l2_g_ctrl(sd->ctrl_handler, control);
2232}
2233EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
2234
2235s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
2236{
2237 s32 val = 0;
2238
2239 /* It's a driver bug if this happens. */
2240 WARN_ON(!type_is_int(ctrl));
2241 get_ctrl(ctrl, &val);
2242 return val;
2243}
2244EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
2245
2246
2247/* Core function that calls try/s_ctrl and ensures that the new value is
2248 copied to the current value on a set.
2249 Must be called with ctrl->handler->lock held. */
e6402585
HV
2250static int try_or_set_cluster(struct v4l2_fh *fh,
2251 struct v4l2_ctrl *master, bool set)
0996517c 2252{
72d877ca 2253 bool update_flag;
eb5b16ef 2254 int ret;
0996517c
HV
2255 int i;
2256
2257 /* Go through the cluster and either validate the new value or
2258 (if no new value was set), copy the current value to the new
2259 value, ensuring a consistent view for the control ops when
2260 called. */
eb5b16ef 2261 for (i = 0; i < master->ncontrols; i++) {
0996517c
HV
2262 struct v4l2_ctrl *ctrl = master->cluster[i];
2263
2264 if (ctrl == NULL)
2265 continue;
2266
eb5b16ef
HV
2267 if (!ctrl->is_new) {
2268 cur_to_new(ctrl);
0996517c
HV
2269 continue;
2270 }
eb5b16ef
HV
2271 /* Check again: it may have changed since the
2272 previous check in try_or_set_ext_ctrls(). */
2273 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2274 return -EBUSY;
0996517c
HV
2275 }
2276
eb5b16ef 2277 ret = call_op(master, try_ctrl);
0996517c
HV
2278
2279 /* Don't set if there is no change */
72d877ca
HV
2280 if (ret || !set || !cluster_changed(master))
2281 return ret;
2282 ret = call_op(master, s_ctrl);
72d877ca
HV
2283 if (ret)
2284 return ret;
2285
eb5b16ef 2286 /* If OK, then make the new values permanent. */
72d877ca
HV
2287 update_flag = is_cur_manual(master) != is_new_manual(master);
2288 for (i = 0; i < master->ncontrols; i++)
ab892bac 2289 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
72d877ca 2290 return 0;
0996517c
HV
2291}
2292
e6402585
HV
2293/* Validate controls. */
2294static int validate_ctrls(struct v4l2_ext_controls *cs,
2295 struct v4l2_ctrl_helper *helpers, bool set)
0996517c 2296{
e6402585 2297 unsigned i;
0996517c
HV
2298 int ret = 0;
2299
eb5b16ef 2300 cs->error_idx = cs->count;
0996517c
HV
2301 for (i = 0; i < cs->count; i++) {
2302 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
2303
e6402585 2304 cs->error_idx = i;
0996517c
HV
2305
2306 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2307 return -EACCES;
2308 /* This test is also done in try_set_control_cluster() which
2309 is called in atomic context, so that has the final say,
2310 but it makes sense to do an up-front check as well. Once
2311 an error occurs in try_set_control_cluster() some other
2312 controls may have been set already and we want to do a
2313 best-effort to avoid that. */
2314 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2315 return -EBUSY;
eb5b16ef
HV
2316 ret = validate_new(ctrl, &cs->controls[i]);
2317 if (ret)
2318 return ret;
0996517c 2319 }
e6402585
HV
2320 return 0;
2321}
2322
5626b8c7
HV
2323/* Obtain the current volatile values of an autocluster and mark them
2324 as new. */
2325static void update_from_auto_cluster(struct v4l2_ctrl *master)
2326{
2327 int i;
2328
2329 for (i = 0; i < master->ncontrols; i++)
2330 cur_to_new(master->cluster[i]);
2331 if (!call_op(master, g_volatile_ctrl))
2332 for (i = 1; i < master->ncontrols; i++)
2333 if (master->cluster[i])
2334 master->cluster[i]->is_new = 1;
2335}
2336
e6402585
HV
2337/* Try or try-and-set controls */
2338static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2339 struct v4l2_ext_controls *cs,
2340 bool set)
2341{
2342 struct v4l2_ctrl_helper helper[4];
2343 struct v4l2_ctrl_helper *helpers = helper;
2344 unsigned i, j;
2345 int ret;
2346
2347 cs->error_idx = cs->count;
2348 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
2349
2350 if (hdl == NULL)
2351 return -EINVAL;
2352
2353 if (cs->count == 0)
2354 return class_check(hdl, cs->ctrl_class);
0996517c 2355
e6402585 2356 if (cs->count > ARRAY_SIZE(helper)) {
0a3475eb
XW
2357 helpers = kmalloc_array(cs->count, sizeof(helper[0]),
2358 GFP_KERNEL);
e6402585
HV
2359 if (!helpers)
2360 return -ENOMEM;
2361 }
2362 ret = prepare_ext_ctrls(hdl, cs, helpers);
2363 if (!ret)
2364 ret = validate_ctrls(cs, helpers, set);
2365 if (ret && set)
2366 cs->error_idx = cs->count;
0996517c 2367 for (i = 0; !ret && i < cs->count; i++) {
eb5b16ef
HV
2368 struct v4l2_ctrl *master;
2369 u32 idx = i;
0996517c 2370
eb5b16ef 2371 if (helpers[i].mref == NULL)
0996517c
HV
2372 continue;
2373
37cd3b73 2374 cs->error_idx = i;
eb5b16ef
HV
2375 master = helpers[i].mref->ctrl;
2376 v4l2_ctrl_lock(master);
0996517c 2377
2a863793 2378 /* Reset the 'is_new' flags of the cluster */
0996517c
HV
2379 for (j = 0; j < master->ncontrols; j++)
2380 if (master->cluster[j])
2a863793 2381 master->cluster[j]->is_new = 0;
0996517c 2382
5626b8c7
HV
2383 /* For volatile autoclusters that are currently in auto mode
2384 we need to discover if it will be set to manual mode.
2385 If so, then we have to copy the current volatile values
2386 first since those will become the new manual values (which
2387 may be overwritten by explicit new values from this set
2388 of controls). */
2389 if (master->is_auto && master->has_volatiles &&
2390 !is_cur_manual(master)) {
2391 /* Pick an initial non-manual value */
2392 s32 new_auto_val = master->manual_mode_value + 1;
2393 u32 tmp_idx = idx;
2394
2395 do {
2396 /* Check if the auto control is part of the
2397 list, and remember the new value. */
2398 if (helpers[tmp_idx].ctrl == master)
2399 new_auto_val = cs->controls[tmp_idx].value;
2400 tmp_idx = helpers[tmp_idx].next;
2401 } while (tmp_idx);
2402 /* If the new value == the manual value, then copy
2403 the current volatile values. */
2404 if (new_auto_val == master->manual_mode_value)
2405 update_from_auto_cluster(master);
2406 }
2407
0996517c 2408 /* Copy the new caller-supplied control values.
2a863793 2409 user_to_new() sets 'is_new' to 1. */
eb5b16ef
HV
2410 do {
2411 ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
2412 idx = helpers[idx].next;
2413 } while (!ret && idx);
0996517c
HV
2414
2415 if (!ret)
e6402585 2416 ret = try_or_set_cluster(fh, master, set);
0996517c
HV
2417
2418 /* Copy the new values back to userspace. */
eb5b16ef
HV
2419 if (!ret) {
2420 idx = i;
2421 do {
adf41b9b 2422 ret = new_to_user(cs->controls + idx,
e6402585 2423 helpers[idx].ctrl);
eb5b16ef
HV
2424 idx = helpers[idx].next;
2425 } while (!ret && idx);
2426 }
2427 v4l2_ctrl_unlock(master);
0996517c 2428 }
0996517c 2429
0996517c
HV
2430 if (cs->count > ARRAY_SIZE(helper))
2431 kfree(helpers);
2432 return ret;
2433}
2434
2435int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2436{
ab892bac 2437 return try_set_ext_ctrls(NULL, hdl, cs, false);
0996517c
HV
2438}
2439EXPORT_SYMBOL(v4l2_try_ext_ctrls);
2440
ab892bac
HV
2441int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2442 struct v4l2_ext_controls *cs)
0996517c 2443{
ab892bac 2444 return try_set_ext_ctrls(fh, hdl, cs, true);
0996517c
HV
2445}
2446EXPORT_SYMBOL(v4l2_s_ext_ctrls);
2447
2448int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2449{
ab892bac 2450 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
0996517c
HV
2451}
2452EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
2453
2454int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2455{
ab892bac 2456 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
0996517c
HV
2457}
2458EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
2459
2460/* Helper function for VIDIOC_S_CTRL compatibility */
ab892bac 2461static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
0996517c
HV
2462{
2463 struct v4l2_ctrl *master = ctrl->cluster[0];
2464 int ret;
2465 int i;
2466
eb5b16ef
HV
2467 ret = validate_new_int(ctrl, val);
2468 if (ret)
2469 return ret;
2470
0996517c
HV
2471 v4l2_ctrl_lock(ctrl);
2472
2a863793 2473 /* Reset the 'is_new' flags of the cluster */
0996517c
HV
2474 for (i = 0; i < master->ncontrols; i++)
2475 if (master->cluster[i])
2a863793 2476 master->cluster[i]->is_new = 0;
0996517c 2477
5626b8c7
HV
2478 /* For autoclusters with volatiles that are switched from auto to
2479 manual mode we have to update the current volatile values since
2480 those will become the initial manual values after such a switch. */
2481 if (master->is_auto && master->has_volatiles && ctrl == master &&
2482 !is_cur_manual(master) && *val == master->manual_mode_value)
2483 update_from_auto_cluster(master);
0996517c 2484 ctrl->val = *val;
2a863793 2485 ctrl->is_new = 1;
e6402585 2486 ret = try_or_set_cluster(fh, master, true);
0996517c
HV
2487 *val = ctrl->cur.val;
2488 v4l2_ctrl_unlock(ctrl);
2489 return ret;
2490}
2491
ab892bac
HV
2492int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2493 struct v4l2_control *control)
0996517c
HV
2494{
2495 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2496
2497 if (ctrl == NULL || !type_is_int(ctrl))
2498 return -EINVAL;
2499
7ebbc39f
HV
2500 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2501 return -EACCES;
2502
ab892bac 2503 return set_ctrl(fh, ctrl, &control->value);
0996517c
HV
2504}
2505EXPORT_SYMBOL(v4l2_s_ctrl);
2506
2507int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2508{
ab892bac 2509 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
0996517c
HV
2510}
2511EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2512
2513int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2514{
2515 /* It's a driver bug if this happens. */
2516 WARN_ON(!type_is_int(ctrl));
ab892bac 2517 return set_ctrl(NULL, ctrl, &val);
0996517c
HV
2518}
2519EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
6e239399 2520
3e366149 2521static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev)
6e239399 2522{
3e366149
HG
2523 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
2524
2525 if (ctrl == NULL)
2526 return -EINVAL;
2527
6e239399 2528 v4l2_ctrl_lock(ctrl);
77068d36 2529 list_add_tail(&sev->node, &ctrl->ev_subs);
6e239399 2530 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
77068d36 2531 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
6e239399 2532 struct v4l2_event ev;
c12fcfd6 2533 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
6e239399 2534
c12fcfd6
HV
2535 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2536 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2537 fill_event(&ev, ctrl, changes);
77068d36 2538 v4l2_event_queue_fh(sev->fh, &ev);
6e239399
HV
2539 }
2540 v4l2_ctrl_unlock(ctrl);
3e366149 2541 return 0;
6e239399 2542}
6e239399 2543
3e366149 2544static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
6e239399 2545{
3e366149
HG
2546 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
2547
6e239399 2548 v4l2_ctrl_lock(ctrl);
77068d36 2549 list_del(&sev->node);
6e239399
HV
2550 v4l2_ctrl_unlock(ctrl);
2551}
3e366149
HG
2552
2553void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
2554{
2555 u32 old_changes = old->u.ctrl.changes;
2556
2557 old->u.ctrl = new->u.ctrl;
2558 old->u.ctrl.changes |= old_changes;
2559}
2560EXPORT_SYMBOL(v4l2_ctrl_replace);
2561
2562void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
2563{
2564 new->u.ctrl.changes |= old->u.ctrl.changes;
2565}
2566EXPORT_SYMBOL(v4l2_ctrl_merge);
2567
2568const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
2569 .add = v4l2_ctrl_add_event,
2570 .del = v4l2_ctrl_del_event,
2571 .replace = v4l2_ctrl_replace,
2572 .merge = v4l2_ctrl_merge,
2573};
2574EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
e2ecb257
HV
2575
2576int v4l2_ctrl_log_status(struct file *file, void *fh)
2577{
2578 struct video_device *vfd = video_devdata(file);
2579 struct v4l2_fh *vfh = file->private_data;
2580
2581 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
2582 v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
2583 vfd->v4l2_dev->name);
2584 return 0;
2585}
2586EXPORT_SYMBOL(v4l2_ctrl_log_status);
a26243b0
HV
2587
2588int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
2589 struct v4l2_event_subscription *sub)
2590{
2591 if (sub->type == V4L2_EVENT_CTRL)
3e366149 2592 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
a26243b0
HV
2593 return -EINVAL;
2594}
2595EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
2596
2597unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
2598{
2599 struct v4l2_fh *fh = file->private_data;
2600
2601 if (v4l2_event_pending(fh))
2602 return POLLPRI;
2603 poll_wait(file, &fh->wait, wait);
2604 return 0;
2605}
2606EXPORT_SYMBOL(v4l2_ctrl_poll);