[media] v4l2-event: add optional merge and replace callbacks
[linux-2.6-block.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>
0996517c
HV
23#include <media/v4l2-ioctl.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ctrls.h>
6e239399 26#include <media/v4l2-event.h>
0996517c
HV
27#include <media/v4l2-dev.h>
28
ddac5c10
HV
29#define has_op(master, op) \
30 (master->ops && master->ops->op)
54c911eb 31#define call_op(master, op) \
ddac5c10 32 (has_op(master, op) ? master->ops->op(master) : 0)
54c911eb 33
0996517c
HV
34/* Internal temporary helper struct, one for each v4l2_ext_control */
35struct ctrl_helper {
36 /* The control corresponding to the v4l2_ext_control ID field. */
37 struct v4l2_ctrl *ctrl;
38 /* Used internally to mark whether this control was already
39 processed. */
40 bool handled;
41};
42
72d877ca
HV
43/* Small helper function to determine if the autocluster is set to manual
44 mode. In that case the is_volatile flag should be ignored. */
45static bool is_cur_manual(const struct v4l2_ctrl *master)
46{
47 return master->is_auto && master->cur.val == master->manual_mode_value;
48}
49
50/* Same as above, but this checks the against the new value instead of the
51 current value. */
52static bool is_new_manual(const struct v4l2_ctrl *master)
53{
54 return master->is_auto && master->val == master->manual_mode_value;
55}
56
0996517c
HV
57/* Returns NULL or a character pointer array containing the menu for
58 the given control ID. The pointer array ends with a NULL pointer.
59 An empty string signifies a menu entry that is invalid. This allows
60 drivers to disable certain options if it is not supported. */
513521ea 61const char * const *v4l2_ctrl_get_menu(u32 id)
0996517c 62{
513521ea 63 static const char * const mpeg_audio_sampling_freq[] = {
0996517c
HV
64 "44.1 kHz",
65 "48 kHz",
66 "32 kHz",
67 NULL
68 };
513521ea 69 static const char * const mpeg_audio_encoding[] = {
0996517c
HV
70 "MPEG-1/2 Layer I",
71 "MPEG-1/2 Layer II",
72 "MPEG-1/2 Layer III",
73 "MPEG-2/4 AAC",
74 "AC-3",
75 NULL
76 };
513521ea 77 static const char * const mpeg_audio_l1_bitrate[] = {
0996517c
HV
78 "32 kbps",
79 "64 kbps",
80 "96 kbps",
81 "128 kbps",
82 "160 kbps",
83 "192 kbps",
84 "224 kbps",
85 "256 kbps",
86 "288 kbps",
87 "320 kbps",
88 "352 kbps",
89 "384 kbps",
90 "416 kbps",
91 "448 kbps",
92 NULL
93 };
513521ea 94 static const char * const mpeg_audio_l2_bitrate[] = {
0996517c
HV
95 "32 kbps",
96 "48 kbps",
97 "56 kbps",
98 "64 kbps",
99 "80 kbps",
100 "96 kbps",
101 "112 kbps",
102 "128 kbps",
103 "160 kbps",
104 "192 kbps",
105 "224 kbps",
106 "256 kbps",
107 "320 kbps",
108 "384 kbps",
109 NULL
110 };
513521ea 111 static const char * const mpeg_audio_l3_bitrate[] = {
0996517c
HV
112 "32 kbps",
113 "40 kbps",
114 "48 kbps",
115 "56 kbps",
116 "64 kbps",
117 "80 kbps",
118 "96 kbps",
119 "112 kbps",
120 "128 kbps",
121 "160 kbps",
122 "192 kbps",
123 "224 kbps",
124 "256 kbps",
125 "320 kbps",
126 NULL
127 };
513521ea 128 static const char * const mpeg_audio_ac3_bitrate[] = {
0996517c
HV
129 "32 kbps",
130 "40 kbps",
131 "48 kbps",
132 "56 kbps",
133 "64 kbps",
134 "80 kbps",
135 "96 kbps",
136 "112 kbps",
137 "128 kbps",
138 "160 kbps",
139 "192 kbps",
140 "224 kbps",
141 "256 kbps",
142 "320 kbps",
143 "384 kbps",
144 "448 kbps",
145 "512 kbps",
146 "576 kbps",
147 "640 kbps",
148 NULL
149 };
513521ea 150 static const char * const mpeg_audio_mode[] = {
0996517c
HV
151 "Stereo",
152 "Joint Stereo",
153 "Dual",
154 "Mono",
155 NULL
156 };
513521ea 157 static const char * const mpeg_audio_mode_extension[] = {
0996517c
HV
158 "Bound 4",
159 "Bound 8",
160 "Bound 12",
161 "Bound 16",
162 NULL
163 };
513521ea 164 static const char * const mpeg_audio_emphasis[] = {
0996517c
HV
165 "No Emphasis",
166 "50/15 us",
167 "CCITT J17",
168 NULL
169 };
513521ea 170 static const char * const mpeg_audio_crc[] = {
0996517c
HV
171 "No CRC",
172 "16-bit CRC",
173 NULL
174 };
513521ea 175 static const char * const mpeg_video_encoding[] = {
0996517c
HV
176 "MPEG-1",
177 "MPEG-2",
178 "MPEG-4 AVC",
179 NULL
180 };
513521ea 181 static const char * const mpeg_video_aspect[] = {
0996517c
HV
182 "1x1",
183 "4x3",
184 "16x9",
185 "2.21x1",
186 NULL
187 };
513521ea 188 static const char * const mpeg_video_bitrate_mode[] = {
0996517c
HV
189 "Variable Bitrate",
190 "Constant Bitrate",
191 NULL
192 };
513521ea 193 static const char * const mpeg_stream_type[] = {
0996517c
HV
194 "MPEG-2 Program Stream",
195 "MPEG-2 Transport Stream",
196 "MPEG-1 System Stream",
197 "MPEG-2 DVD-compatible Stream",
198 "MPEG-1 VCD-compatible Stream",
199 "MPEG-2 SVCD-compatible Stream",
200 NULL
201 };
513521ea 202 static const char * const mpeg_stream_vbi_fmt[] = {
0996517c
HV
203 "No VBI",
204 "Private packet, IVTV format",
205 NULL
206 };
513521ea 207 static const char * const camera_power_line_frequency[] = {
0996517c
HV
208 "Disabled",
209 "50 Hz",
210 "60 Hz",
211 NULL
212 };
513521ea 213 static const char * const camera_exposure_auto[] = {
0996517c
HV
214 "Auto Mode",
215 "Manual Mode",
216 "Shutter Priority Mode",
217 "Aperture Priority Mode",
218 NULL
219 };
513521ea 220 static const char * const colorfx[] = {
0996517c
HV
221 "None",
222 "Black & White",
223 "Sepia",
224 "Negative",
225 "Emboss",
226 "Sketch",
227 "Sky blue",
228 "Grass green",
229 "Skin whiten",
230 "Vivid",
231 NULL
232 };
513521ea 233 static const char * const tune_preemphasis[] = {
0996517c
HV
234 "No preemphasis",
235 "50 useconds",
236 "75 useconds",
237 NULL,
238 };
239
240 switch (id) {
241 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
242 return mpeg_audio_sampling_freq;
243 case V4L2_CID_MPEG_AUDIO_ENCODING:
244 return mpeg_audio_encoding;
245 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
246 return mpeg_audio_l1_bitrate;
247 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
248 return mpeg_audio_l2_bitrate;
249 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
250 return mpeg_audio_l3_bitrate;
251 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
252 return mpeg_audio_ac3_bitrate;
253 case V4L2_CID_MPEG_AUDIO_MODE:
254 return mpeg_audio_mode;
255 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
256 return mpeg_audio_mode_extension;
257 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
258 return mpeg_audio_emphasis;
259 case V4L2_CID_MPEG_AUDIO_CRC:
260 return mpeg_audio_crc;
261 case V4L2_CID_MPEG_VIDEO_ENCODING:
262 return mpeg_video_encoding;
263 case V4L2_CID_MPEG_VIDEO_ASPECT:
264 return mpeg_video_aspect;
265 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
266 return mpeg_video_bitrate_mode;
267 case V4L2_CID_MPEG_STREAM_TYPE:
268 return mpeg_stream_type;
269 case V4L2_CID_MPEG_STREAM_VBI_FMT:
270 return mpeg_stream_vbi_fmt;
271 case V4L2_CID_POWER_LINE_FREQUENCY:
272 return camera_power_line_frequency;
273 case V4L2_CID_EXPOSURE_AUTO:
274 return camera_exposure_auto;
275 case V4L2_CID_COLORFX:
276 return colorfx;
277 case V4L2_CID_TUNE_PREEMPHASIS:
278 return tune_preemphasis;
279 default:
280 return NULL;
281 }
282}
283EXPORT_SYMBOL(v4l2_ctrl_get_menu);
284
285/* Return the control name. */
286const char *v4l2_ctrl_get_name(u32 id)
287{
288 switch (id) {
289 /* USER controls */
6c8d6111 290 /* Keep the order of the 'case's the same as in videodev2.h! */
6dd5aff3
MCC
291 case V4L2_CID_USER_CLASS: return "User Controls";
292 case V4L2_CID_BRIGHTNESS: return "Brightness";
293 case V4L2_CID_CONTRAST: return "Contrast";
294 case V4L2_CID_SATURATION: return "Saturation";
295 case V4L2_CID_HUE: return "Hue";
296 case V4L2_CID_AUDIO_VOLUME: return "Volume";
297 case V4L2_CID_AUDIO_BALANCE: return "Balance";
298 case V4L2_CID_AUDIO_BASS: return "Bass";
299 case V4L2_CID_AUDIO_TREBLE: return "Treble";
300 case V4L2_CID_AUDIO_MUTE: return "Mute";
301 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
0996517c
HV
302 case V4L2_CID_BLACK_LEVEL: return "Black Level";
303 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
304 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
305 case V4L2_CID_RED_BALANCE: return "Red Balance";
306 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
307 case V4L2_CID_GAMMA: return "Gamma";
308 case V4L2_CID_EXPOSURE: return "Exposure";
309 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
310 case V4L2_CID_GAIN: return "Gain";
311 case V4L2_CID_HFLIP: return "Horizontal Flip";
312 case V4L2_CID_VFLIP: return "Vertical Flip";
313 case V4L2_CID_HCENTER: return "Horizontal Center";
314 case V4L2_CID_VCENTER: return "Vertical Center";
315 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
316 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
317 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
318 case V4L2_CID_SHARPNESS: return "Sharpness";
319 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
320 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
0996517c
HV
321 case V4L2_CID_COLOR_KILLER: return "Color Killer";
322 case V4L2_CID_COLORFX: return "Color Effects";
323 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
324 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
325 case V4L2_CID_ROTATE: return "Rotate";
326 case V4L2_CID_BG_COLOR: return "Background Color";
6c8d6111 327 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
008d35f2
JFM
328 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
329 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
0996517c
HV
330
331 /* MPEG controls */
6c8d6111 332 /* Keep the order of the 'case's the same as in videodev2.h! */
6dd5aff3
MCC
333 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
334 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
335 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
336 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
337 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
338 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
6c8d6111
HV
339 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
340 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
341 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
0996517c 342 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
6dd5aff3
MCC
343 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
344 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
345 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
346 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
347 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
0996517c 348 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
6dd5aff3
MCC
349 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
350 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
351 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
352 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
353 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
354 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
355 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
356 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
357 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
358 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
359 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
360 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
361 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
362 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
0996517c 363 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
6dd5aff3 364 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
0996517c 365 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
0996517c
HV
366
367 /* CAMERA controls */
6c8d6111 368 /* Keep the order of the 'case's the same as in videodev2.h! */
0996517c
HV
369 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
370 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
371 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
372 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
373 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
374 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
375 case V4L2_CID_PAN_RESET: return "Pan, Reset";
376 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
377 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
378 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
379 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
380 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
381 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
0996517c
HV
382 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
383 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
384 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
385 case V4L2_CID_PRIVACY: return "Privacy";
6c8d6111
HV
386 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
387 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
0996517c
HV
388
389 /* FM Radio Modulator control */
6c8d6111 390 /* Keep the order of the 'case's the same as in videodev2.h! */
0996517c
HV
391 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
392 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
393 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
394 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
395 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
396 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
397 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
398 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
399 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
400 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
401 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
402 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
403 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
404 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
405 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
406 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
407 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
408 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
409 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
410 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
411
412 default:
413 return NULL;
414 }
415}
416EXPORT_SYMBOL(v4l2_ctrl_get_name);
417
418void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
419 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
420{
421 *name = v4l2_ctrl_get_name(id);
422 *flags = 0;
423
424 switch (id) {
425 case V4L2_CID_AUDIO_MUTE:
426 case V4L2_CID_AUDIO_LOUDNESS:
427 case V4L2_CID_AUTO_WHITE_BALANCE:
428 case V4L2_CID_AUTOGAIN:
429 case V4L2_CID_HFLIP:
430 case V4L2_CID_VFLIP:
431 case V4L2_CID_HUE_AUTO:
432 case V4L2_CID_CHROMA_AGC:
433 case V4L2_CID_COLOR_KILLER:
434 case V4L2_CID_MPEG_AUDIO_MUTE:
435 case V4L2_CID_MPEG_VIDEO_MUTE:
436 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
437 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
438 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
439 case V4L2_CID_FOCUS_AUTO:
440 case V4L2_CID_PRIVACY:
441 case V4L2_CID_AUDIO_LIMITER_ENABLED:
442 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
443 case V4L2_CID_PILOT_TONE_ENABLED:
008d35f2
JFM
444 case V4L2_CID_ILLUMINATORS_1:
445 case V4L2_CID_ILLUMINATORS_2:
0996517c
HV
446 *type = V4L2_CTRL_TYPE_BOOLEAN;
447 *min = 0;
448 *max = *step = 1;
449 break;
450 case V4L2_CID_PAN_RESET:
451 case V4L2_CID_TILT_RESET:
452 *type = V4L2_CTRL_TYPE_BUTTON;
453 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
454 *min = *max = *step = *def = 0;
455 break;
456 case V4L2_CID_POWER_LINE_FREQUENCY:
457 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
458 case V4L2_CID_MPEG_AUDIO_ENCODING:
459 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
460 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
461 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
462 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
463 case V4L2_CID_MPEG_AUDIO_MODE:
464 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
465 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
466 case V4L2_CID_MPEG_AUDIO_CRC:
467 case V4L2_CID_MPEG_VIDEO_ENCODING:
468 case V4L2_CID_MPEG_VIDEO_ASPECT:
469 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
470 case V4L2_CID_MPEG_STREAM_TYPE:
471 case V4L2_CID_MPEG_STREAM_VBI_FMT:
472 case V4L2_CID_EXPOSURE_AUTO:
473 case V4L2_CID_COLORFX:
474 case V4L2_CID_TUNE_PREEMPHASIS:
475 *type = V4L2_CTRL_TYPE_MENU;
476 break;
477 case V4L2_CID_RDS_TX_PS_NAME:
478 case V4L2_CID_RDS_TX_RADIO_TEXT:
479 *type = V4L2_CTRL_TYPE_STRING;
480 break;
481 case V4L2_CID_USER_CLASS:
482 case V4L2_CID_CAMERA_CLASS:
483 case V4L2_CID_MPEG_CLASS:
484 case V4L2_CID_FM_TX_CLASS:
485 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
486 /* You can neither read not write these */
487 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
488 *min = *max = *step = *def = 0;
489 break;
490 case V4L2_CID_BG_COLOR:
491 *type = V4L2_CTRL_TYPE_INTEGER;
492 *step = 1;
493 *min = 0;
494 /* Max is calculated as RGB888 that is 2^24 */
495 *max = 0xFFFFFF;
496 break;
497 default:
498 *type = V4L2_CTRL_TYPE_INTEGER;
499 break;
500 }
501 switch (id) {
502 case V4L2_CID_MPEG_AUDIO_ENCODING:
503 case V4L2_CID_MPEG_AUDIO_MODE:
504 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
505 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
506 case V4L2_CID_MPEG_STREAM_TYPE:
507 *flags |= V4L2_CTRL_FLAG_UPDATE;
508 break;
509 case V4L2_CID_AUDIO_VOLUME:
510 case V4L2_CID_AUDIO_BALANCE:
511 case V4L2_CID_AUDIO_BASS:
512 case V4L2_CID_AUDIO_TREBLE:
513 case V4L2_CID_BRIGHTNESS:
514 case V4L2_CID_CONTRAST:
515 case V4L2_CID_SATURATION:
516 case V4L2_CID_HUE:
517 case V4L2_CID_RED_BALANCE:
518 case V4L2_CID_BLUE_BALANCE:
519 case V4L2_CID_GAMMA:
520 case V4L2_CID_SHARPNESS:
521 case V4L2_CID_CHROMA_GAIN:
522 case V4L2_CID_RDS_TX_DEVIATION:
523 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
524 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
525 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
526 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
527 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
528 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
529 case V4L2_CID_PILOT_TONE_DEVIATION:
530 case V4L2_CID_PILOT_TONE_FREQUENCY:
531 case V4L2_CID_TUNE_POWER_LEVEL:
532 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
533 *flags |= V4L2_CTRL_FLAG_SLIDER;
534 break;
535 case V4L2_CID_PAN_RELATIVE:
536 case V4L2_CID_TILT_RELATIVE:
537 case V4L2_CID_FOCUS_RELATIVE:
538 case V4L2_CID_IRIS_RELATIVE:
539 case V4L2_CID_ZOOM_RELATIVE:
540 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
541 break;
542 }
543}
544EXPORT_SYMBOL(v4l2_ctrl_fill);
545
546/* Helper function to determine whether the control type is compatible with
547 VIDIOC_G/S_CTRL. */
548static bool type_is_int(const struct v4l2_ctrl *ctrl)
549{
550 switch (ctrl->type) {
551 case V4L2_CTRL_TYPE_INTEGER64:
552 case V4L2_CTRL_TYPE_STRING:
553 /* Nope, these need v4l2_ext_control */
554 return false;
555 default:
556 return true;
557 }
558}
559
6e239399
HV
560static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
561{
562 memset(ev->reserved, 0, sizeof(ev->reserved));
563 ev->type = V4L2_EVENT_CTRL;
564 ev->id = ctrl->id;
565 ev->u.ctrl.changes = changes;
566 ev->u.ctrl.type = ctrl->type;
567 ev->u.ctrl.flags = ctrl->flags;
568 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
569 ev->u.ctrl.value64 = 0;
570 else
571 ev->u.ctrl.value64 = ctrl->cur.val64;
572 ev->u.ctrl.minimum = ctrl->minimum;
573 ev->u.ctrl.maximum = ctrl->maximum;
574 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
575 ev->u.ctrl.step = 1;
576 else
577 ev->u.ctrl.step = ctrl->step;
578 ev->u.ctrl.default_value = ctrl->default_value;
579}
580
581static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
582{
583 struct v4l2_event ev;
77068d36 584 struct v4l2_subscribed_event *sev;
6e239399 585
77068d36 586 if (list_empty(&ctrl->ev_subs))
6e239399
HV
587 return;
588 fill_event(&ev, ctrl, changes);
589
77068d36
HV
590 list_for_each_entry(sev, &ctrl->ev_subs, node)
591 if (sev->fh && sev->fh != fh)
592 v4l2_event_queue_fh(sev->fh, &ev);
6e239399
HV
593}
594
0996517c
HV
595/* Helper function: copy the current control value back to the caller */
596static int cur_to_user(struct v4l2_ext_control *c,
597 struct v4l2_ctrl *ctrl)
598{
599 u32 len;
600
601 switch (ctrl->type) {
602 case V4L2_CTRL_TYPE_STRING:
603 len = strlen(ctrl->cur.string);
604 if (c->size < len + 1) {
605 c->size = len + 1;
606 return -ENOSPC;
607 }
608 return copy_to_user(c->string, ctrl->cur.string,
609 len + 1) ? -EFAULT : 0;
610 case V4L2_CTRL_TYPE_INTEGER64:
611 c->value64 = ctrl->cur.val64;
612 break;
613 default:
614 c->value = ctrl->cur.val;
615 break;
616 }
617 return 0;
618}
619
620/* Helper function: copy the caller-provider value as the new control value */
621static int user_to_new(struct v4l2_ext_control *c,
622 struct v4l2_ctrl *ctrl)
623{
624 int ret;
625 u32 size;
626
2a863793 627 ctrl->is_new = 1;
0996517c
HV
628 switch (ctrl->type) {
629 case V4L2_CTRL_TYPE_INTEGER64:
630 ctrl->val64 = c->value64;
631 break;
632 case V4L2_CTRL_TYPE_STRING:
633 size = c->size;
634 if (size == 0)
635 return -ERANGE;
636 if (size > ctrl->maximum + 1)
637 size = ctrl->maximum + 1;
638 ret = copy_from_user(ctrl->string, c->string, size);
639 if (!ret) {
640 char last = ctrl->string[size - 1];
641
642 ctrl->string[size - 1] = 0;
643 /* If the string was longer than ctrl->maximum,
644 then return an error. */
645 if (strlen(ctrl->string) == ctrl->maximum && last)
646 return -ERANGE;
647 }
648 return ret ? -EFAULT : 0;
649 default:
650 ctrl->val = c->value;
651 break;
652 }
653 return 0;
654}
655
656/* Helper function: copy the new control value back to the caller */
657static int new_to_user(struct v4l2_ext_control *c,
658 struct v4l2_ctrl *ctrl)
659{
660 u32 len;
661
662 switch (ctrl->type) {
663 case V4L2_CTRL_TYPE_STRING:
664 len = strlen(ctrl->string);
665 if (c->size < len + 1) {
666 c->size = ctrl->maximum + 1;
667 return -ENOSPC;
668 }
669 return copy_to_user(c->string, ctrl->string,
670 len + 1) ? -EFAULT : 0;
671 case V4L2_CTRL_TYPE_INTEGER64:
672 c->value64 = ctrl->val64;
673 break;
674 default:
675 c->value = ctrl->val;
676 break;
677 }
678 return 0;
679}
680
ddac5c10
HV
681static int ctrl_to_user(struct v4l2_ext_control *c,
682 struct v4l2_ctrl *ctrl)
683{
684 if (ctrl->is_volatile)
685 return new_to_user(c, ctrl);
686 return cur_to_user(c, ctrl);
687}
688
689static int ctrl_is_volatile(struct v4l2_ext_control *c,
690 struct v4l2_ctrl *ctrl)
691{
692 return ctrl->is_volatile;
693}
694
0996517c 695/* Copy the new value to the current value. */
ab892bac
HV
696static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
697 bool update_inactive)
0996517c 698{
6e239399
HV
699 bool changed = false;
700
0996517c
HV
701 if (ctrl == NULL)
702 return;
703 switch (ctrl->type) {
6e239399
HV
704 case V4L2_CTRL_TYPE_BUTTON:
705 changed = true;
706 break;
0996517c
HV
707 case V4L2_CTRL_TYPE_STRING:
708 /* strings are always 0-terminated */
6e239399 709 changed = strcmp(ctrl->string, ctrl->cur.string);
0996517c
HV
710 strcpy(ctrl->cur.string, ctrl->string);
711 break;
712 case V4L2_CTRL_TYPE_INTEGER64:
6e239399 713 changed = ctrl->val64 != ctrl->cur.val64;
0996517c
HV
714 ctrl->cur.val64 = ctrl->val64;
715 break;
716 default:
6e239399 717 changed = ctrl->val != ctrl->cur.val;
0996517c
HV
718 ctrl->cur.val = ctrl->val;
719 break;
720 }
72d877ca
HV
721 if (update_inactive) {
722 ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
723 if (!is_cur_manual(ctrl->cluster[0]))
724 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
725 }
6e239399
HV
726 if (changed || update_inactive)
727 send_event(fh, ctrl,
728 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
729 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
0996517c
HV
730}
731
732/* Copy the current value to the new value */
733static void cur_to_new(struct v4l2_ctrl *ctrl)
734{
735 if (ctrl == NULL)
736 return;
737 switch (ctrl->type) {
738 case V4L2_CTRL_TYPE_STRING:
739 /* strings are always 0-terminated */
740 strcpy(ctrl->string, ctrl->cur.string);
741 break;
742 case V4L2_CTRL_TYPE_INTEGER64:
743 ctrl->val64 = ctrl->cur.val64;
744 break;
745 default:
746 ctrl->val = ctrl->cur.val;
747 break;
748 }
749}
750
751/* Return non-zero if one or more of the controls in the cluster has a new
752 value that differs from the current value. */
753static int cluster_changed(struct v4l2_ctrl *master)
754{
755 int diff = 0;
756 int i;
757
758 for (i = 0; !diff && i < master->ncontrols; i++) {
759 struct v4l2_ctrl *ctrl = master->cluster[i];
760
761 if (ctrl == NULL)
762 continue;
763 switch (ctrl->type) {
764 case V4L2_CTRL_TYPE_BUTTON:
765 /* Button controls are always 'different' */
766 return 1;
767 case V4L2_CTRL_TYPE_STRING:
768 /* strings are always 0-terminated */
769 diff = strcmp(ctrl->string, ctrl->cur.string);
770 break;
771 case V4L2_CTRL_TYPE_INTEGER64:
772 diff = ctrl->val64 != ctrl->cur.val64;
773 break;
774 default:
775 diff = ctrl->val != ctrl->cur.val;
776 break;
777 }
778 }
779 return diff;
780}
781
782/* Validate a new control */
783static int validate_new(struct v4l2_ctrl *ctrl)
784{
785 s32 val = ctrl->val;
786 char *s = ctrl->string;
787 u32 offset;
788 size_t len;
789
790 switch (ctrl->type) {
791 case V4L2_CTRL_TYPE_INTEGER:
792 /* Round towards the closest legal value */
793 val += ctrl->step / 2;
794 if (val < ctrl->minimum)
795 val = ctrl->minimum;
796 if (val > ctrl->maximum)
797 val = ctrl->maximum;
798 offset = val - ctrl->minimum;
799 offset = ctrl->step * (offset / ctrl->step);
800 val = ctrl->minimum + offset;
801 ctrl->val = val;
802 return 0;
803
804 case V4L2_CTRL_TYPE_BOOLEAN:
805 ctrl->val = !!ctrl->val;
806 return 0;
807
808 case V4L2_CTRL_TYPE_MENU:
809 if (val < ctrl->minimum || val > ctrl->maximum)
810 return -ERANGE;
811 if (ctrl->qmenu[val][0] == '\0' ||
812 (ctrl->menu_skip_mask & (1 << val)))
813 return -EINVAL;
814 return 0;
815
816 case V4L2_CTRL_TYPE_BUTTON:
817 case V4L2_CTRL_TYPE_CTRL_CLASS:
818 ctrl->val64 = 0;
819 return 0;
820
821 case V4L2_CTRL_TYPE_INTEGER64:
822 return 0;
823
824 case V4L2_CTRL_TYPE_STRING:
825 len = strlen(s);
826 if (len < ctrl->minimum)
827 return -ERANGE;
828 if ((len - ctrl->minimum) % ctrl->step)
829 return -ERANGE;
830 return 0;
831
832 default:
833 return -EINVAL;
834 }
835}
836
837static inline u32 node2id(struct list_head *node)
838{
839 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
840}
841
842/* Set the handler's error code if it wasn't set earlier already */
843static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
844{
845 if (hdl->error == 0)
846 hdl->error = err;
847 return err;
848}
849
850/* Initialize the handler */
851int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
852 unsigned nr_of_controls_hint)
853{
854 mutex_init(&hdl->lock);
855 INIT_LIST_HEAD(&hdl->ctrls);
856 INIT_LIST_HEAD(&hdl->ctrl_refs);
857 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
858 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
859 GFP_KERNEL);
860 hdl->error = hdl->buckets ? 0 : -ENOMEM;
861 return hdl->error;
862}
863EXPORT_SYMBOL(v4l2_ctrl_handler_init);
864
865/* Free all controls and control refs */
866void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
867{
868 struct v4l2_ctrl_ref *ref, *next_ref;
869 struct v4l2_ctrl *ctrl, *next_ctrl;
77068d36 870 struct v4l2_subscribed_event *sev, *next_sev;
0996517c
HV
871
872 if (hdl == NULL || hdl->buckets == NULL)
873 return;
874
875 mutex_lock(&hdl->lock);
876 /* Free all nodes */
877 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
878 list_del(&ref->node);
879 kfree(ref);
880 }
881 /* Free all controls owned by the handler */
882 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
883 list_del(&ctrl->node);
77068d36
HV
884 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
885 list_del(&sev->node);
0996517c
HV
886 kfree(ctrl);
887 }
888 kfree(hdl->buckets);
889 hdl->buckets = NULL;
890 hdl->cached = NULL;
891 hdl->error = 0;
892 mutex_unlock(&hdl->lock);
893}
894EXPORT_SYMBOL(v4l2_ctrl_handler_free);
895
896/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
897 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
898 with applications that do not use the NEXT_CTRL flag.
899
900 We just find the n-th private user control. It's O(N), but that should not
901 be an issue in this particular case. */
902static struct v4l2_ctrl_ref *find_private_ref(
903 struct v4l2_ctrl_handler *hdl, u32 id)
904{
905 struct v4l2_ctrl_ref *ref;
906
907 id -= V4L2_CID_PRIVATE_BASE;
908 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
909 /* Search for private user controls that are compatible with
910 VIDIOC_G/S_CTRL. */
911 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
912 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
913 if (!type_is_int(ref->ctrl))
914 continue;
915 if (id == 0)
916 return ref;
917 id--;
918 }
919 }
920 return NULL;
921}
922
923/* Find a control with the given ID. */
924static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
925{
926 struct v4l2_ctrl_ref *ref;
927 int bucket;
928
929 id &= V4L2_CTRL_ID_MASK;
930
931 /* Old-style private controls need special handling */
932 if (id >= V4L2_CID_PRIVATE_BASE)
933 return find_private_ref(hdl, id);
934 bucket = id % hdl->nr_of_buckets;
935
936 /* Simple optimization: cache the last control found */
937 if (hdl->cached && hdl->cached->ctrl->id == id)
938 return hdl->cached;
939
940 /* Not in cache, search the hash */
941 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
942 while (ref && ref->ctrl->id != id)
943 ref = ref->next;
944
945 if (ref)
946 hdl->cached = ref; /* cache it! */
947 return ref;
948}
949
950/* Find a control with the given ID. Take the handler's lock first. */
951static struct v4l2_ctrl_ref *find_ref_lock(
952 struct v4l2_ctrl_handler *hdl, u32 id)
953{
954 struct v4l2_ctrl_ref *ref = NULL;
955
956 if (hdl) {
957 mutex_lock(&hdl->lock);
958 ref = find_ref(hdl, id);
959 mutex_unlock(&hdl->lock);
960 }
961 return ref;
962}
963
964/* Find a control with the given ID. */
965struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
966{
967 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
968
969 return ref ? ref->ctrl : NULL;
970}
971EXPORT_SYMBOL(v4l2_ctrl_find);
972
973/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
974static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
975 struct v4l2_ctrl *ctrl)
976{
977 struct v4l2_ctrl_ref *ref;
978 struct v4l2_ctrl_ref *new_ref;
979 u32 id = ctrl->id;
980 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
981 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
982
983 /* Automatically add the control class if it is not yet present. */
984 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
985 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
986 return hdl->error;
987
988 if (hdl->error)
989 return hdl->error;
990
991 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
992 if (!new_ref)
993 return handler_set_err(hdl, -ENOMEM);
994 new_ref->ctrl = ctrl;
995 if (ctrl->handler == hdl) {
996 /* By default each control starts in a cluster of its own.
997 new_ref->ctrl is basically a cluster array with one
998 element, so that's perfect to use as the cluster pointer.
999 But only do this for the handler that owns the control. */
1000 ctrl->cluster = &new_ref->ctrl;
1001 ctrl->ncontrols = 1;
1002 }
1003
1004 INIT_LIST_HEAD(&new_ref->node);
1005
1006 mutex_lock(&hdl->lock);
1007
1008 /* Add immediately at the end of the list if the list is empty, or if
1009 the last element in the list has a lower ID.
1010 This ensures that when elements are added in ascending order the
1011 insertion is an O(1) operation. */
1012 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1013 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1014 goto insert_in_hash;
1015 }
1016
1017 /* Find insert position in sorted list */
1018 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1019 if (ref->ctrl->id < id)
1020 continue;
1021 /* Don't add duplicates */
1022 if (ref->ctrl->id == id) {
1023 kfree(new_ref);
1024 goto unlock;
1025 }
1026 list_add(&new_ref->node, ref->node.prev);
1027 break;
1028 }
1029
1030insert_in_hash:
1031 /* Insert the control node in the hash */
1032 new_ref->next = hdl->buckets[bucket];
1033 hdl->buckets[bucket] = new_ref;
1034
1035unlock:
1036 mutex_unlock(&hdl->lock);
1037 return 0;
1038}
1039
1040/* Add a new control */
1041static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1042 const struct v4l2_ctrl_ops *ops,
1043 u32 id, const char *name, enum v4l2_ctrl_type type,
1044 s32 min, s32 max, u32 step, s32 def,
513521ea 1045 u32 flags, const char * const *qmenu, void *priv)
0996517c
HV
1046{
1047 struct v4l2_ctrl *ctrl;
1048 unsigned sz_extra = 0;
1049
1050 if (hdl->error)
1051 return NULL;
1052
1053 /* Sanity checks */
1054 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
02ac0480 1055 max < min ||
0996517c
HV
1056 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
1057 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1058 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1059 handler_set_err(hdl, -ERANGE);
1060 return NULL;
1061 }
02ac0480
HV
1062 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1063 type == V4L2_CTRL_TYPE_MENU ||
1064 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1065 (def < min || def > max)) {
1066 handler_set_err(hdl, -ERANGE);
1067 return NULL;
1068 }
0996517c
HV
1069
1070 if (type == V4L2_CTRL_TYPE_BUTTON)
1071 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1072 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1073 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1074 else if (type == V4L2_CTRL_TYPE_STRING)
1075 sz_extra += 2 * (max + 1);
1076
1077 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1078 if (ctrl == NULL) {
1079 handler_set_err(hdl, -ENOMEM);
1080 return NULL;
1081 }
1082
1083 INIT_LIST_HEAD(&ctrl->node);
77068d36 1084 INIT_LIST_HEAD(&ctrl->ev_subs);
0996517c
HV
1085 ctrl->handler = hdl;
1086 ctrl->ops = ops;
1087 ctrl->id = id;
1088 ctrl->name = name;
1089 ctrl->type = type;
1090 ctrl->flags = flags;
1091 ctrl->minimum = min;
1092 ctrl->maximum = max;
1093 ctrl->step = step;
1094 ctrl->qmenu = qmenu;
1095 ctrl->priv = priv;
1096 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1097
1098 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1099 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1100 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1101 if (ctrl->minimum)
1102 memset(ctrl->cur.string, ' ', ctrl->minimum);
1103 }
1104 if (handler_new_ref(hdl, ctrl)) {
1105 kfree(ctrl);
1106 return NULL;
1107 }
1108 mutex_lock(&hdl->lock);
1109 list_add_tail(&ctrl->node, &hdl->ctrls);
1110 mutex_unlock(&hdl->lock);
1111 return ctrl;
1112}
1113
1114struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1115 const struct v4l2_ctrl_config *cfg, void *priv)
1116{
1117 bool is_menu;
1118 struct v4l2_ctrl *ctrl;
1119 const char *name = cfg->name;
513521ea 1120 const char * const *qmenu = cfg->qmenu;
0996517c
HV
1121 enum v4l2_ctrl_type type = cfg->type;
1122 u32 flags = cfg->flags;
1123 s32 min = cfg->min;
1124 s32 max = cfg->max;
1125 u32 step = cfg->step;
1126 s32 def = cfg->def;
1127
1128 if (name == NULL)
1129 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1130 &def, &flags);
1131
1132 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1133 if (is_menu)
1134 WARN_ON(step);
1135 else
1136 WARN_ON(cfg->menu_skip_mask);
1137 if (is_menu && qmenu == NULL)
1138 qmenu = v4l2_ctrl_get_menu(cfg->id);
1139
1140 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1141 type, min, max,
1142 is_menu ? cfg->menu_skip_mask : step,
1143 def, flags, qmenu, priv);
1144 if (ctrl) {
1145 ctrl->is_private = cfg->is_private;
1146 ctrl->is_volatile = cfg->is_volatile;
1147 }
1148 return ctrl;
1149}
1150EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1151
1152/* Helper function for standard non-menu controls */
1153struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1154 const struct v4l2_ctrl_ops *ops,
1155 u32 id, s32 min, s32 max, u32 step, s32 def)
1156{
1157 const char *name;
1158 enum v4l2_ctrl_type type;
1159 u32 flags;
1160
1161 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1162 if (type == V4L2_CTRL_TYPE_MENU) {
1163 handler_set_err(hdl, -EINVAL);
1164 return NULL;
1165 }
1166 return v4l2_ctrl_new(hdl, ops, id, name, type,
1167 min, max, step, def, flags, NULL, NULL);
1168}
1169EXPORT_SYMBOL(v4l2_ctrl_new_std);
1170
1171/* Helper function for standard menu controls */
1172struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1173 const struct v4l2_ctrl_ops *ops,
1174 u32 id, s32 max, s32 mask, s32 def)
1175{
513521ea 1176 const char * const *qmenu = v4l2_ctrl_get_menu(id);
0996517c
HV
1177 const char *name;
1178 enum v4l2_ctrl_type type;
1179 s32 min;
1180 s32 step;
1181 u32 flags;
1182
1183 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1184 if (type != V4L2_CTRL_TYPE_MENU) {
1185 handler_set_err(hdl, -EINVAL);
1186 return NULL;
1187 }
1188 return v4l2_ctrl_new(hdl, ops, id, name, type,
1189 0, max, mask, def, flags, qmenu, NULL);
1190}
1191EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1192
1193/* Add a control from another handler to this handler */
1194struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1195 struct v4l2_ctrl *ctrl)
1196{
1197 if (hdl == NULL || hdl->error)
1198 return NULL;
1199 if (ctrl == NULL) {
1200 handler_set_err(hdl, -EINVAL);
1201 return NULL;
1202 }
1203 if (ctrl->handler == hdl)
1204 return ctrl;
1205 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1206}
1207EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1208
1209/* Add the controls from another handler to our own. */
1210int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1211 struct v4l2_ctrl_handler *add)
1212{
1213 struct v4l2_ctrl *ctrl;
1214 int ret = 0;
1215
1216 /* Do nothing if either handler is NULL or if they are the same */
1217 if (!hdl || !add || hdl == add)
1218 return 0;
1219 if (hdl->error)
1220 return hdl->error;
1221 mutex_lock(&add->lock);
1222 list_for_each_entry(ctrl, &add->ctrls, node) {
1223 /* Skip handler-private controls. */
1224 if (ctrl->is_private)
1225 continue;
6e239399
HV
1226 /* And control classes */
1227 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1228 continue;
0996517c
HV
1229 ret = handler_new_ref(hdl, ctrl);
1230 if (ret)
1231 break;
1232 }
1233 mutex_unlock(&add->lock);
1234 return ret;
1235}
1236EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1237
1238/* Cluster controls */
1239void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1240{
1241 int i;
1242
1243 /* The first control is the master control and it must not be NULL */
72d877ca 1244 BUG_ON(ncontrols == 0 || controls[0] == NULL);
0996517c
HV
1245
1246 for (i = 0; i < ncontrols; i++) {
1247 if (controls[i]) {
1248 controls[i]->cluster = controls;
1249 controls[i]->ncontrols = ncontrols;
1250 }
1251 }
1252}
1253EXPORT_SYMBOL(v4l2_ctrl_cluster);
1254
72d877ca
HV
1255void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1256 u8 manual_val, bool set_volatile)
1257{
1258 struct v4l2_ctrl *master = controls[0];
1259 u32 flag;
1260 int i;
1261
1262 v4l2_ctrl_cluster(ncontrols, controls);
1263 WARN_ON(ncontrols <= 1);
82a7c049 1264 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
72d877ca
HV
1265 master->is_auto = true;
1266 master->manual_mode_value = manual_val;
1267 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1268 flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
1269
1270 for (i = 1; i < ncontrols; i++)
1271 if (controls[i]) {
1272 controls[i]->is_volatile = set_volatile;
1273 controls[i]->flags |= flag;
1274 }
1275}
1276EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1277
0996517c
HV
1278/* Activate/deactivate a control. */
1279void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1280{
6e239399
HV
1281 /* invert since the actual flag is called 'inactive' */
1282 bool inactive = !active;
1283 bool old;
1284
0996517c
HV
1285 if (ctrl == NULL)
1286 return;
1287
6e239399 1288 if (inactive)
0996517c 1289 /* set V4L2_CTRL_FLAG_INACTIVE */
6e239399 1290 old = test_and_set_bit(4, &ctrl->flags);
0996517c
HV
1291 else
1292 /* clear V4L2_CTRL_FLAG_INACTIVE */
6e239399
HV
1293 old = test_and_clear_bit(4, &ctrl->flags);
1294 if (old != inactive)
1295 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
0996517c
HV
1296}
1297EXPORT_SYMBOL(v4l2_ctrl_activate);
1298
1299/* Grab/ungrab a control.
1300 Typically used when streaming starts and you want to grab controls,
1301 preventing the user from changing them.
1302
1303 Just call this and the framework will block any attempts to change
1304 these controls. */
1305void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1306{
6e239399
HV
1307 bool old;
1308
0996517c
HV
1309 if (ctrl == NULL)
1310 return;
1311
6e239399 1312 v4l2_ctrl_lock(ctrl);
0996517c
HV
1313 if (grabbed)
1314 /* set V4L2_CTRL_FLAG_GRABBED */
6e239399 1315 old = test_and_set_bit(1, &ctrl->flags);
0996517c
HV
1316 else
1317 /* clear V4L2_CTRL_FLAG_GRABBED */
6e239399
HV
1318 old = test_and_clear_bit(1, &ctrl->flags);
1319 if (old != grabbed)
1320 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1321 v4l2_ctrl_unlock(ctrl);
0996517c
HV
1322}
1323EXPORT_SYMBOL(v4l2_ctrl_grab);
1324
1325/* Log the control name and value */
1326static void log_ctrl(const struct v4l2_ctrl *ctrl,
1327 const char *prefix, const char *colon)
1328{
1329 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1330 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1331
1332 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1333 return;
1334 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1335 return;
1336
1337 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1338
1339 switch (ctrl->type) {
1340 case V4L2_CTRL_TYPE_INTEGER:
1341 printk(KERN_CONT "%d", ctrl->cur.val);
1342 break;
1343 case V4L2_CTRL_TYPE_BOOLEAN:
1344 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1345 break;
1346 case V4L2_CTRL_TYPE_MENU:
1347 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1348 break;
1349 case V4L2_CTRL_TYPE_INTEGER64:
1350 printk(KERN_CONT "%lld", ctrl->cur.val64);
1351 break;
1352 case V4L2_CTRL_TYPE_STRING:
1353 printk(KERN_CONT "%s", ctrl->cur.string);
1354 break;
1355 default:
1356 printk(KERN_CONT "unknown type %d", ctrl->type);
1357 break;
1358 }
1359 if (fl_inact && fl_grabbed)
1360 printk(KERN_CONT " (inactive, grabbed)\n");
1361 else if (fl_inact)
1362 printk(KERN_CONT " (inactive)\n");
1363 else if (fl_grabbed)
1364 printk(KERN_CONT " (grabbed)\n");
1365 else
1366 printk(KERN_CONT "\n");
1367}
1368
1369/* Log all controls owned by the handler */
1370void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1371 const char *prefix)
1372{
1373 struct v4l2_ctrl *ctrl;
1374 const char *colon = "";
1375 int len;
1376
1377 if (hdl == NULL)
1378 return;
1379 if (prefix == NULL)
1380 prefix = "";
1381 len = strlen(prefix);
1382 if (len && prefix[len - 1] != ' ')
1383 colon = ": ";
1384 mutex_lock(&hdl->lock);
1385 list_for_each_entry(ctrl, &hdl->ctrls, node)
1386 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1387 log_ctrl(ctrl, prefix, colon);
1388 mutex_unlock(&hdl->lock);
1389}
1390EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1391
1392/* Call s_ctrl for all controls owned by the handler */
1393int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1394{
1395 struct v4l2_ctrl *ctrl;
1396 int ret = 0;
1397
1398 if (hdl == NULL)
1399 return 0;
1400 mutex_lock(&hdl->lock);
1401 list_for_each_entry(ctrl, &hdl->ctrls, node)
1402 ctrl->done = false;
1403
1404 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1405 struct v4l2_ctrl *master = ctrl->cluster[0];
1406 int i;
1407
1408 /* Skip if this control was already handled by a cluster. */
1409 if (ctrl->done)
1410 continue;
1411
2a863793
HV
1412 for (i = 0; i < master->ncontrols; i++) {
1413 if (master->cluster[i]) {
1414 cur_to_new(master->cluster[i]);
1415 master->cluster[i]->is_new = 1;
1416 }
1417 }
0996517c
HV
1418
1419 /* Skip button controls and read-only controls. */
1420 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1421 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1422 continue;
54c911eb 1423 ret = call_op(master, s_ctrl);
0996517c
HV
1424 if (ret)
1425 break;
1426 for (i = 0; i < master->ncontrols; i++)
1427 if (master->cluster[i])
1428 master->cluster[i]->done = true;
1429 }
1430 mutex_unlock(&hdl->lock);
1431 return ret;
1432}
1433EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1434
1435/* Implement VIDIOC_QUERYCTRL */
1436int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1437{
1438 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1439 struct v4l2_ctrl_ref *ref;
1440 struct v4l2_ctrl *ctrl;
1441
1442 if (hdl == NULL)
1443 return -EINVAL;
1444
1445 mutex_lock(&hdl->lock);
1446
1447 /* Try to find it */
1448 ref = find_ref(hdl, id);
1449
1450 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1451 /* Find the next control with ID > qc->id */
1452
1453 /* Did we reach the end of the control list? */
1454 if (id >= node2id(hdl->ctrl_refs.prev)) {
1455 ref = NULL; /* Yes, so there is no next control */
1456 } else if (ref) {
1457 /* We found a control with the given ID, so just get
1458 the next one in the list. */
1459 ref = list_entry(ref->node.next, typeof(*ref), node);
1460 } else {
1461 /* No control with the given ID exists, so start
1462 searching for the next largest ID. We know there
1463 is one, otherwise the first 'if' above would have
1464 been true. */
1465 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1466 if (id < ref->ctrl->id)
1467 break;
1468 }
1469 }
1470 mutex_unlock(&hdl->lock);
1471 if (!ref)
1472 return -EINVAL;
1473
1474 ctrl = ref->ctrl;
1475 memset(qc, 0, sizeof(*qc));
829fb2dc
HV
1476 if (id >= V4L2_CID_PRIVATE_BASE)
1477 qc->id = id;
1478 else
1479 qc->id = ctrl->id;
0996517c
HV
1480 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1481 qc->minimum = ctrl->minimum;
1482 qc->maximum = ctrl->maximum;
1483 qc->default_value = ctrl->default_value;
eac9aa00 1484 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
0996517c
HV
1485 qc->step = 1;
1486 else
1487 qc->step = ctrl->step;
1488 qc->flags = ctrl->flags;
1489 qc->type = ctrl->type;
1490 return 0;
1491}
1492EXPORT_SYMBOL(v4l2_queryctrl);
1493
1494int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1495{
87a0c94c
HV
1496 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1497 return -EINVAL;
0996517c
HV
1498 return v4l2_queryctrl(sd->ctrl_handler, qc);
1499}
1500EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1501
1502/* Implement VIDIOC_QUERYMENU */
1503int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1504{
1505 struct v4l2_ctrl *ctrl;
1506 u32 i = qm->index;
1507
1508 ctrl = v4l2_ctrl_find(hdl, qm->id);
1509 if (!ctrl)
1510 return -EINVAL;
1511
1512 qm->reserved = 0;
1513 /* Sanity checks */
1514 if (ctrl->qmenu == NULL ||
1515 i < ctrl->minimum || i > ctrl->maximum)
1516 return -EINVAL;
1517 /* Use mask to see if this menu item should be skipped */
1518 if (ctrl->menu_skip_mask & (1 << i))
1519 return -EINVAL;
1520 /* Empty menu items should also be skipped */
1521 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1522 return -EINVAL;
1523 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1524 return 0;
1525}
1526EXPORT_SYMBOL(v4l2_querymenu);
1527
1528int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1529{
1530 return v4l2_querymenu(sd->ctrl_handler, qm);
1531}
1532EXPORT_SYMBOL(v4l2_subdev_querymenu);
1533
1534
1535
1536/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1537
1538 It is not a fully atomic operation, just best-effort only. After all, if
1539 multiple controls have to be set through multiple i2c writes (for example)
1540 then some initial writes may succeed while others fail. Thus leaving the
1541 system in an inconsistent state. The question is how much effort you are
1542 willing to spend on trying to make something atomic that really isn't.
1543
1544 From the point of view of an application the main requirement is that
1545 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1546 error should be returned without actually affecting any controls.
1547
1548 If all the values are correct, then it is acceptable to just give up
1549 in case of low-level errors.
1550
1551 It is important though that the application can tell when only a partial
1552 configuration was done. The way we do that is through the error_idx field
1553 of struct v4l2_ext_controls: if that is equal to the count field then no
1554 controls were affected. Otherwise all controls before that index were
1555 successful in performing their 'get' or 'set' operation, the control at
1556 the given index failed, and you don't know what happened with the controls
1557 after the failed one. Since if they were part of a control cluster they
1558 could have been successfully processed (if a cluster member was encountered
1559 at index < error_idx), they could have failed (if a cluster member was at
1560 error_idx), or they may not have been processed yet (if the first cluster
1561 member appeared after error_idx).
1562
1563 It is all fairly theoretical, though. In practice all you can do is to
1564 bail out. If error_idx == count, then it is an application bug. If
1565 error_idx < count then it is only an application bug if the error code was
1566 EBUSY. That usually means that something started streaming just when you
1567 tried to set the controls. In all other cases it is a driver/hardware
1568 problem and all you can do is to retry or bail out.
1569
1570 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1571 never modifies controls the error_idx is just set to whatever control
1572 has an invalid value.
1573 */
1574
1575/* Prepare for the extended g/s/try functions.
1576 Find the controls in the control array and do some basic checks. */
1577static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1578 struct v4l2_ext_controls *cs,
37cd3b73 1579 struct ctrl_helper *helpers)
0996517c
HV
1580{
1581 u32 i;
1582
1583 for (i = 0; i < cs->count; i++) {
1584 struct v4l2_ext_control *c = &cs->controls[i];
1585 struct v4l2_ctrl *ctrl;
1586 u32 id = c->id & V4L2_CTRL_ID_MASK;
1587
37cd3b73 1588 cs->error_idx = i;
0996517c
HV
1589
1590 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1591 return -EINVAL;
1592
1593 /* Old-style private controls are not allowed for
1594 extended controls */
1595 if (id >= V4L2_CID_PRIVATE_BASE)
1596 return -EINVAL;
1597 ctrl = v4l2_ctrl_find(hdl, id);
1598 if (ctrl == NULL)
1599 return -EINVAL;
1600 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1601 return -EINVAL;
1602
1603 helpers[i].ctrl = ctrl;
1604 helpers[i].handled = false;
1605 }
1606 return 0;
1607}
1608
1609typedef int (*cluster_func)(struct v4l2_ext_control *c,
1610 struct v4l2_ctrl *ctrl);
1611
1612/* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1613 and call the provided function. */
1614static int cluster_walk(unsigned from,
1615 struct v4l2_ext_controls *cs,
1616 struct ctrl_helper *helpers,
1617 cluster_func f)
1618{
1619 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1620 int ret = 0;
1621 int i;
1622
1623 /* Find any controls from the same cluster and call the function */
1624 for (i = from; !ret && i < cs->count; i++) {
1625 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1626
1627 if (!helpers[i].handled && ctrl->cluster == cluster)
1628 ret = f(&cs->controls[i], ctrl);
1629 }
1630 return ret;
1631}
1632
1633static void cluster_done(unsigned from,
1634 struct v4l2_ext_controls *cs,
1635 struct ctrl_helper *helpers)
1636{
1637 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1638 int i;
1639
1640 /* Find any controls from the same cluster and mark them as handled */
1641 for (i = from; i < cs->count; i++)
1642 if (helpers[i].ctrl->cluster == cluster)
1643 helpers[i].handled = true;
1644}
1645
1646/* Handles the corner case where cs->count == 0. It checks whether the
1647 specified control class exists. If that class ID is 0, then it checks
1648 whether there are any controls at all. */
1649static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1650{
1651 if (ctrl_class == 0)
1652 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1653 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1654}
1655
1656
1657
1658/* Get extended controls. Allocates the helpers array if needed. */
1659int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1660{
1661 struct ctrl_helper helper[4];
1662 struct ctrl_helper *helpers = helper;
1663 int ret;
ddac5c10 1664 int i, j;
0996517c
HV
1665
1666 cs->error_idx = cs->count;
1667 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1668
1669 if (hdl == NULL)
1670 return -EINVAL;
1671
1672 if (cs->count == 0)
1673 return class_check(hdl, cs->ctrl_class);
1674
1675 if (cs->count > ARRAY_SIZE(helper)) {
1676 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1677 if (helpers == NULL)
1678 return -ENOMEM;
1679 }
1680
37cd3b73
HV
1681 ret = prepare_ext_ctrls(hdl, cs, helpers);
1682 cs->error_idx = cs->count;
0996517c
HV
1683
1684 for (i = 0; !ret && i < cs->count; i++)
1685 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1686 ret = -EACCES;
1687
1688 for (i = 0; !ret && i < cs->count; i++) {
1689 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1690 struct v4l2_ctrl *master = ctrl->cluster[0];
ddac5c10 1691 bool has_volatiles;
0996517c
HV
1692
1693 if (helpers[i].handled)
1694 continue;
1695
1696 cs->error_idx = i;
1697
ddac5c10
HV
1698 /* Any volatile controls requested from this cluster? */
1699 has_volatiles = ctrl->is_volatile;
1700 if (!has_volatiles && has_op(master, g_volatile_ctrl) &&
1701 master->ncontrols > 1)
1702 has_volatiles = cluster_walk(i, cs, helpers,
1703 ctrl_is_volatile);
1704
0996517c 1705 v4l2_ctrl_lock(master);
ddac5c10
HV
1706
1707 /* g_volatile_ctrl will update the new control values */
72d877ca 1708 if (has_volatiles && !is_cur_manual(master)) {
ddac5c10
HV
1709 for (j = 0; j < master->ncontrols; j++)
1710 cur_to_new(master->cluster[j]);
54c911eb 1711 ret = call_op(master, g_volatile_ctrl);
ddac5c10
HV
1712 }
1713 /* If OK, then copy the current (for non-volatile controls)
1714 or the new (for volatile controls) control values to the
1715 caller */
0996517c 1716 if (!ret)
ddac5c10 1717 ret = cluster_walk(i, cs, helpers, ctrl_to_user);
0996517c
HV
1718 v4l2_ctrl_unlock(master);
1719 cluster_done(i, cs, helpers);
1720 }
1721
1722 if (cs->count > ARRAY_SIZE(helper))
1723 kfree(helpers);
1724 return ret;
1725}
1726EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1727
1728int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1729{
1730 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1731}
1732EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1733
1734/* Helper function to get a single control */
1735static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1736{
1737 struct v4l2_ctrl *master = ctrl->cluster[0];
1738 int ret = 0;
ddac5c10 1739 int i;
0996517c
HV
1740
1741 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1742 return -EACCES;
1743
1744 v4l2_ctrl_lock(master);
1745 /* g_volatile_ctrl will update the current control values */
72d877ca 1746 if (ctrl->is_volatile && !is_cur_manual(master)) {
ddac5c10
HV
1747 for (i = 0; i < master->ncontrols; i++)
1748 cur_to_new(master->cluster[i]);
54c911eb 1749 ret = call_op(master, g_volatile_ctrl);
ddac5c10
HV
1750 *val = ctrl->val;
1751 } else {
1752 *val = ctrl->cur.val;
1753 }
0996517c
HV
1754 v4l2_ctrl_unlock(master);
1755 return ret;
1756}
1757
1758int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1759{
1760 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1761
1762 if (ctrl == NULL || !type_is_int(ctrl))
1763 return -EINVAL;
1764 return get_ctrl(ctrl, &control->value);
1765}
1766EXPORT_SYMBOL(v4l2_g_ctrl);
1767
1768int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1769{
1770 return v4l2_g_ctrl(sd->ctrl_handler, control);
1771}
1772EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1773
1774s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1775{
1776 s32 val = 0;
1777
1778 /* It's a driver bug if this happens. */
1779 WARN_ON(!type_is_int(ctrl));
1780 get_ctrl(ctrl, &val);
1781 return val;
1782}
1783EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1784
1785
1786/* Core function that calls try/s_ctrl and ensures that the new value is
1787 copied to the current value on a set.
1788 Must be called with ctrl->handler->lock held. */
ab892bac
HV
1789static int try_or_set_control_cluster(struct v4l2_fh *fh,
1790 struct v4l2_ctrl *master, bool set)
0996517c 1791{
72d877ca 1792 bool update_flag;
0996517c
HV
1793 bool try = !set;
1794 int ret = 0;
1795 int i;
1796
1797 /* Go through the cluster and either validate the new value or
1798 (if no new value was set), copy the current value to the new
1799 value, ensuring a consistent view for the control ops when
1800 called. */
1801 for (i = 0; !ret && i < master->ncontrols; i++) {
1802 struct v4l2_ctrl *ctrl = master->cluster[i];
1803
1804 if (ctrl == NULL)
1805 continue;
1806
2a863793 1807 if (ctrl->is_new) {
0996517c
HV
1808 /* Double check this: it may have changed since the
1809 last check in try_or_set_ext_ctrls(). */
1810 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1811 return -EBUSY;
1812
1813 /* Validate if required */
1814 if (!set)
1815 ret = validate_new(ctrl);
1816 continue;
1817 }
1818 /* No new value was set, so copy the current and force
1819 a call to try_ctrl later, since the values for the cluster
1820 may now have changed and the end result might be invalid. */
1821 try = true;
1822 cur_to_new(ctrl);
1823 }
1824
1825 /* For larger clusters you have to call try_ctrl again to
1826 verify that the controls are still valid after the
1827 'cur_to_new' above. */
54c911eb
HV
1828 if (!ret && try)
1829 ret = call_op(master, try_ctrl);
0996517c
HV
1830
1831 /* Don't set if there is no change */
72d877ca
HV
1832 if (ret || !set || !cluster_changed(master))
1833 return ret;
1834 ret = call_op(master, s_ctrl);
1835 /* If OK, then make the new values permanent. */
1836 if (ret)
1837 return ret;
1838
1839 update_flag = is_cur_manual(master) != is_new_manual(master);
1840 for (i = 0; i < master->ncontrols; i++)
ab892bac 1841 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
72d877ca 1842 return 0;
0996517c
HV
1843}
1844
1845/* Try or set controls. */
ab892bac
HV
1846static int try_or_set_ext_ctrls(struct v4l2_fh *fh,
1847 struct v4l2_ctrl_handler *hdl,
0996517c
HV
1848 struct v4l2_ext_controls *cs,
1849 struct ctrl_helper *helpers,
1850 bool set)
1851{
1852 unsigned i, j;
1853 int ret = 0;
1854
0996517c
HV
1855 for (i = 0; i < cs->count; i++) {
1856 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1857
37cd3b73 1858 cs->error_idx = i;
0996517c
HV
1859
1860 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1861 return -EACCES;
1862 /* This test is also done in try_set_control_cluster() which
1863 is called in atomic context, so that has the final say,
1864 but it makes sense to do an up-front check as well. Once
1865 an error occurs in try_set_control_cluster() some other
1866 controls may have been set already and we want to do a
1867 best-effort to avoid that. */
1868 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1869 return -EBUSY;
1870 }
1871
1872 for (i = 0; !ret && i < cs->count; i++) {
1873 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1874 struct v4l2_ctrl *master = ctrl->cluster[0];
1875
0996517c
HV
1876 if (helpers[i].handled)
1877 continue;
1878
37cd3b73 1879 cs->error_idx = i;
0996517c
HV
1880 v4l2_ctrl_lock(ctrl);
1881
2a863793 1882 /* Reset the 'is_new' flags of the cluster */
0996517c
HV
1883 for (j = 0; j < master->ncontrols; j++)
1884 if (master->cluster[j])
2a863793 1885 master->cluster[j]->is_new = 0;
0996517c
HV
1886
1887 /* Copy the new caller-supplied control values.
2a863793 1888 user_to_new() sets 'is_new' to 1. */
0996517c
HV
1889 ret = cluster_walk(i, cs, helpers, user_to_new);
1890
1891 if (!ret)
ab892bac 1892 ret = try_or_set_control_cluster(fh, master, set);
0996517c
HV
1893
1894 /* Copy the new values back to userspace. */
1895 if (!ret)
1896 ret = cluster_walk(i, cs, helpers, new_to_user);
1897
1898 v4l2_ctrl_unlock(ctrl);
1899 cluster_done(i, cs, helpers);
1900 }
1901 return ret;
1902}
1903
1904/* Try or try-and-set controls */
ab892bac 1905static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
0996517c
HV
1906 struct v4l2_ext_controls *cs,
1907 bool set)
1908{
1909 struct ctrl_helper helper[4];
1910 struct ctrl_helper *helpers = helper;
1911 int ret;
1912 int i;
1913
1914 cs->error_idx = cs->count;
1915 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1916
1917 if (hdl == NULL)
1918 return -EINVAL;
1919
1920 if (cs->count == 0)
1921 return class_check(hdl, cs->ctrl_class);
1922
1923 if (cs->count > ARRAY_SIZE(helper)) {
1924 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1925 if (!helpers)
1926 return -ENOMEM;
1927 }
37cd3b73 1928 ret = prepare_ext_ctrls(hdl, cs, helpers);
0996517c
HV
1929
1930 /* First 'try' all controls and abort on error */
37cd3b73 1931 if (!ret)
ab892bac 1932 ret = try_or_set_ext_ctrls(NULL, hdl, cs, helpers, false);
0996517c
HV
1933 /* If this is a 'set' operation and the initial 'try' failed,
1934 then set error_idx to count to tell the application that no
1935 controls changed value yet. */
1936 if (set)
1937 cs->error_idx = cs->count;
1938 if (!ret && set) {
1939 /* Reset 'handled' state */
1940 for (i = 0; i < cs->count; i++)
1941 helpers[i].handled = false;
ab892bac 1942 ret = try_or_set_ext_ctrls(fh, hdl, cs, helpers, true);
0996517c
HV
1943 }
1944
0996517c
HV
1945 if (cs->count > ARRAY_SIZE(helper))
1946 kfree(helpers);
1947 return ret;
1948}
1949
1950int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1951{
ab892bac 1952 return try_set_ext_ctrls(NULL, hdl, cs, false);
0996517c
HV
1953}
1954EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1955
ab892bac
HV
1956int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1957 struct v4l2_ext_controls *cs)
0996517c 1958{
ab892bac 1959 return try_set_ext_ctrls(fh, hdl, cs, true);
0996517c
HV
1960}
1961EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1962
1963int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1964{
ab892bac 1965 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
0996517c
HV
1966}
1967EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1968
1969int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1970{
ab892bac 1971 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
0996517c
HV
1972}
1973EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1974
1975/* Helper function for VIDIOC_S_CTRL compatibility */
ab892bac 1976static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
0996517c
HV
1977{
1978 struct v4l2_ctrl *master = ctrl->cluster[0];
1979 int ret;
1980 int i;
1981
1982 v4l2_ctrl_lock(ctrl);
1983
2a863793 1984 /* Reset the 'is_new' flags of the cluster */
0996517c
HV
1985 for (i = 0; i < master->ncontrols; i++)
1986 if (master->cluster[i])
2a863793 1987 master->cluster[i]->is_new = 0;
0996517c
HV
1988
1989 ctrl->val = *val;
2a863793 1990 ctrl->is_new = 1;
ab892bac 1991 ret = try_or_set_control_cluster(NULL, master, false);
0996517c 1992 if (!ret)
ab892bac 1993 ret = try_or_set_control_cluster(fh, master, true);
0996517c
HV
1994 *val = ctrl->cur.val;
1995 v4l2_ctrl_unlock(ctrl);
1996 return ret;
1997}
1998
ab892bac
HV
1999int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2000 struct v4l2_control *control)
0996517c
HV
2001{
2002 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2003
2004 if (ctrl == NULL || !type_is_int(ctrl))
2005 return -EINVAL;
2006
7ebbc39f
HV
2007 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2008 return -EACCES;
2009
ab892bac 2010 return set_ctrl(fh, ctrl, &control->value);
0996517c
HV
2011}
2012EXPORT_SYMBOL(v4l2_s_ctrl);
2013
2014int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2015{
ab892bac 2016 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
0996517c
HV
2017}
2018EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2019
2020int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2021{
2022 /* It's a driver bug if this happens. */
2023 WARN_ON(!type_is_int(ctrl));
ab892bac 2024 return set_ctrl(NULL, ctrl, &val);
0996517c
HV
2025}
2026EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
6e239399 2027
77068d36
HV
2028void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
2029 struct v4l2_subscribed_event *sev)
6e239399 2030{
6e239399 2031 v4l2_ctrl_lock(ctrl);
77068d36 2032 list_add_tail(&sev->node, &ctrl->ev_subs);
6e239399 2033 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
77068d36 2034 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
6e239399
HV
2035 struct v4l2_event ev;
2036
2037 fill_event(&ev, ctrl, V4L2_EVENT_CTRL_CH_VALUE |
2038 V4L2_EVENT_CTRL_CH_FLAGS);
77068d36 2039 v4l2_event_queue_fh(sev->fh, &ev);
6e239399
HV
2040 }
2041 v4l2_ctrl_unlock(ctrl);
2042}
77068d36 2043EXPORT_SYMBOL(v4l2_ctrl_add_event);
6e239399 2044
77068d36
HV
2045void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
2046 struct v4l2_subscribed_event *sev)
6e239399 2047{
6e239399 2048 v4l2_ctrl_lock(ctrl);
77068d36 2049 list_del(&sev->node);
6e239399
HV
2050 v4l2_ctrl_unlock(ctrl);
2051}
77068d36 2052EXPORT_SYMBOL(v4l2_ctrl_del_event);