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