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