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