blob: 4ec3cfbdc3ae12d1d0c7c867461b3ffd55a7d77a [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>
Hans Verkuil6e239392011-06-07 11:13:44 -030026#include <media/v4l2-event.h>
Hans Verkuil09965172010-08-01 14:32:42 -030027#include <media/v4l2-dev.h>
28
Hans Verkuilddac5c12011-06-10 05:43:34 -030029#define has_op(master, op) \
30 (master->ops && master->ops->op)
Hans Verkuil54c911e2011-05-25 06:04:58 -030031#define call_op(master, op) \
Hans Verkuilddac5c12011-06-10 05:43:34 -030032 (has_op(master, op) ? master->ops->op(master) : 0)
Hans Verkuil54c911e2011-05-25 06:04:58 -030033
Hans Verkuil09965172010-08-01 14:32:42 -030034/* Internal temporary helper struct, one for each v4l2_ext_control */
Hans Verkuileb5b16e2011-06-14 10:04:06 -030035struct v4l2_ctrl_helper {
36 /* Pointer to the control reference of the master control */
37 struct v4l2_ctrl_ref *mref;
Hans Verkuil09965172010-08-01 14:32:42 -030038 /* The control corresponding to the v4l2_ext_control ID field. */
39 struct v4l2_ctrl *ctrl;
Hans Verkuileb5b16e2011-06-14 10:04:06 -030040 /* v4l2_ext_control index of the next control belonging to the
41 same cluster, or 0 if there isn't any. */
42 u32 next;
Hans Verkuil09965172010-08-01 14:32:42 -030043};
44
Hans Verkuil72d877c2011-06-10 05:44:36 -030045/* Small helper function to determine if the autocluster is set to manual
46 mode. In that case the is_volatile flag should be ignored. */
47static bool is_cur_manual(const struct v4l2_ctrl *master)
48{
49 return master->is_auto && master->cur.val == master->manual_mode_value;
50}
51
52/* Same as above, but this checks the against the new value instead of the
53 current value. */
54static bool is_new_manual(const struct v4l2_ctrl *master)
55{
56 return master->is_auto && master->val == master->manual_mode_value;
57}
58
Hans Verkuil09965172010-08-01 14:32:42 -030059/* Returns NULL or a character pointer array containing the menu for
60 the given control ID. The pointer array ends with a NULL pointer.
61 An empty string signifies a menu entry that is invalid. This allows
62 drivers to disable certain options if it is not supported. */
Hans Verkuil513521e2010-12-29 14:25:52 -030063const char * const *v4l2_ctrl_get_menu(u32 id)
Hans Verkuil09965172010-08-01 14:32:42 -030064{
Hans Verkuil513521e2010-12-29 14:25:52 -030065 static const char * const mpeg_audio_sampling_freq[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030066 "44.1 kHz",
67 "48 kHz",
68 "32 kHz",
69 NULL
70 };
Hans Verkuil513521e2010-12-29 14:25:52 -030071 static const char * const mpeg_audio_encoding[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030072 "MPEG-1/2 Layer I",
73 "MPEG-1/2 Layer II",
74 "MPEG-1/2 Layer III",
75 "MPEG-2/4 AAC",
76 "AC-3",
77 NULL
78 };
Hans Verkuil513521e2010-12-29 14:25:52 -030079 static const char * const mpeg_audio_l1_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030080 "32 kbps",
81 "64 kbps",
82 "96 kbps",
83 "128 kbps",
84 "160 kbps",
85 "192 kbps",
86 "224 kbps",
87 "256 kbps",
88 "288 kbps",
89 "320 kbps",
90 "352 kbps",
91 "384 kbps",
92 "416 kbps",
93 "448 kbps",
94 NULL
95 };
Hans Verkuil513521e2010-12-29 14:25:52 -030096 static const char * const mpeg_audio_l2_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030097 "32 kbps",
98 "48 kbps",
99 "56 kbps",
100 "64 kbps",
101 "80 kbps",
102 "96 kbps",
103 "112 kbps",
104 "128 kbps",
105 "160 kbps",
106 "192 kbps",
107 "224 kbps",
108 "256 kbps",
109 "320 kbps",
110 "384 kbps",
111 NULL
112 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300113 static const char * const mpeg_audio_l3_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 NULL
129 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300130 static const char * const mpeg_audio_ac3_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300131 "32 kbps",
132 "40 kbps",
133 "48 kbps",
134 "56 kbps",
135 "64 kbps",
136 "80 kbps",
137 "96 kbps",
138 "112 kbps",
139 "128 kbps",
140 "160 kbps",
141 "192 kbps",
142 "224 kbps",
143 "256 kbps",
144 "320 kbps",
145 "384 kbps",
146 "448 kbps",
147 "512 kbps",
148 "576 kbps",
149 "640 kbps",
150 NULL
151 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300152 static const char * const mpeg_audio_mode[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300153 "Stereo",
154 "Joint Stereo",
155 "Dual",
156 "Mono",
157 NULL
158 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300159 static const char * const mpeg_audio_mode_extension[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300160 "Bound 4",
161 "Bound 8",
162 "Bound 12",
163 "Bound 16",
164 NULL
165 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300166 static const char * const mpeg_audio_emphasis[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300167 "No Emphasis",
168 "50/15 us",
169 "CCITT J17",
170 NULL
171 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300172 static const char * const mpeg_audio_crc[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300173 "No CRC",
174 "16-bit CRC",
175 NULL
176 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300177 static const char * const mpeg_video_encoding[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300178 "MPEG-1",
179 "MPEG-2",
180 "MPEG-4 AVC",
181 NULL
182 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300183 static const char * const mpeg_video_aspect[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300184 "1x1",
185 "4x3",
186 "16x9",
187 "2.21x1",
188 NULL
189 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300190 static const char * const mpeg_video_bitrate_mode[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300191 "Variable Bitrate",
192 "Constant Bitrate",
193 NULL
194 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300195 static const char * const mpeg_stream_type[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300196 "MPEG-2 Program Stream",
197 "MPEG-2 Transport Stream",
198 "MPEG-1 System Stream",
199 "MPEG-2 DVD-compatible Stream",
200 "MPEG-1 VCD-compatible Stream",
201 "MPEG-2 SVCD-compatible Stream",
202 NULL
203 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300204 static const char * const mpeg_stream_vbi_fmt[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300205 "No VBI",
206 "Private packet, IVTV format",
207 NULL
208 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300209 static const char * const camera_power_line_frequency[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300210 "Disabled",
211 "50 Hz",
212 "60 Hz",
213 NULL
214 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300215 static const char * const camera_exposure_auto[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300216 "Auto Mode",
217 "Manual Mode",
218 "Shutter Priority Mode",
219 "Aperture Priority Mode",
220 NULL
221 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300222 static const char * const colorfx[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300223 "None",
224 "Black & White",
225 "Sepia",
226 "Negative",
227 "Emboss",
228 "Sketch",
229 "Sky blue",
230 "Grass green",
231 "Skin whiten",
232 "Vivid",
233 NULL
234 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300235 static const char * const tune_preemphasis[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300236 "No preemphasis",
237 "50 useconds",
238 "75 useconds",
239 NULL,
240 };
241
242 switch (id) {
243 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
244 return mpeg_audio_sampling_freq;
245 case V4L2_CID_MPEG_AUDIO_ENCODING:
246 return mpeg_audio_encoding;
247 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
248 return mpeg_audio_l1_bitrate;
249 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
250 return mpeg_audio_l2_bitrate;
251 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
252 return mpeg_audio_l3_bitrate;
253 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
254 return mpeg_audio_ac3_bitrate;
255 case V4L2_CID_MPEG_AUDIO_MODE:
256 return mpeg_audio_mode;
257 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
258 return mpeg_audio_mode_extension;
259 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
260 return mpeg_audio_emphasis;
261 case V4L2_CID_MPEG_AUDIO_CRC:
262 return mpeg_audio_crc;
263 case V4L2_CID_MPEG_VIDEO_ENCODING:
264 return mpeg_video_encoding;
265 case V4L2_CID_MPEG_VIDEO_ASPECT:
266 return mpeg_video_aspect;
267 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
268 return mpeg_video_bitrate_mode;
269 case V4L2_CID_MPEG_STREAM_TYPE:
270 return mpeg_stream_type;
271 case V4L2_CID_MPEG_STREAM_VBI_FMT:
272 return mpeg_stream_vbi_fmt;
273 case V4L2_CID_POWER_LINE_FREQUENCY:
274 return camera_power_line_frequency;
275 case V4L2_CID_EXPOSURE_AUTO:
276 return camera_exposure_auto;
277 case V4L2_CID_COLORFX:
278 return colorfx;
279 case V4L2_CID_TUNE_PREEMPHASIS:
280 return tune_preemphasis;
281 default:
282 return NULL;
283 }
284}
285EXPORT_SYMBOL(v4l2_ctrl_get_menu);
286
287/* Return the control name. */
288const char *v4l2_ctrl_get_name(u32 id)
289{
290 switch (id) {
291 /* USER controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300292 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300293 case V4L2_CID_USER_CLASS: return "User Controls";
294 case V4L2_CID_BRIGHTNESS: return "Brightness";
295 case V4L2_CID_CONTRAST: return "Contrast";
296 case V4L2_CID_SATURATION: return "Saturation";
297 case V4L2_CID_HUE: return "Hue";
298 case V4L2_CID_AUDIO_VOLUME: return "Volume";
299 case V4L2_CID_AUDIO_BALANCE: return "Balance";
300 case V4L2_CID_AUDIO_BASS: return "Bass";
301 case V4L2_CID_AUDIO_TREBLE: return "Treble";
302 case V4L2_CID_AUDIO_MUTE: return "Mute";
303 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
Hans Verkuil09965172010-08-01 14:32:42 -0300304 case V4L2_CID_BLACK_LEVEL: return "Black Level";
305 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
306 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
307 case V4L2_CID_RED_BALANCE: return "Red Balance";
308 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
309 case V4L2_CID_GAMMA: return "Gamma";
310 case V4L2_CID_EXPOSURE: return "Exposure";
311 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
312 case V4L2_CID_GAIN: return "Gain";
313 case V4L2_CID_HFLIP: return "Horizontal Flip";
314 case V4L2_CID_VFLIP: return "Vertical Flip";
315 case V4L2_CID_HCENTER: return "Horizontal Center";
316 case V4L2_CID_VCENTER: return "Vertical Center";
317 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
318 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
319 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
320 case V4L2_CID_SHARPNESS: return "Sharpness";
321 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
322 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
Hans Verkuil09965172010-08-01 14:32:42 -0300323 case V4L2_CID_COLOR_KILLER: return "Color Killer";
324 case V4L2_CID_COLORFX: return "Color Effects";
325 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
326 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
327 case V4L2_CID_ROTATE: return "Rotate";
328 case V4L2_CID_BG_COLOR: return "Background Color";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300329 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
Jean-François Moine008d35f2010-09-13 07:04:49 -0300330 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
331 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
Hans Verkuil09965172010-08-01 14:32:42 -0300332
333 /* MPEG controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300334 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300335 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
336 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
337 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
338 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
339 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
340 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300341 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
342 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
343 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
Hans Verkuil09965172010-08-01 14:32:42 -0300344 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300345 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
346 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
347 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
348 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
349 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
Hans Verkuil09965172010-08-01 14:32:42 -0300350 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300351 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
352 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
353 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
354 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
355 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
356 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
357 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
358 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
359 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
360 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
361 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
362 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
363 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
364 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
Hans Verkuil09965172010-08-01 14:32:42 -0300365 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300366 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
Hans Verkuil09965172010-08-01 14:32:42 -0300367 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
Hans Verkuil09965172010-08-01 14:32:42 -0300368
369 /* CAMERA controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300370 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300371 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
372 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
373 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
374 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
375 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
376 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
377 case V4L2_CID_PAN_RESET: return "Pan, Reset";
378 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
379 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
380 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
381 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
382 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
383 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
Hans Verkuil09965172010-08-01 14:32:42 -0300384 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
385 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
386 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
387 case V4L2_CID_PRIVACY: return "Privacy";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300388 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
389 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
Hans Verkuil09965172010-08-01 14:32:42 -0300390
391 /* FM Radio Modulator control */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300392 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300393 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
394 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
395 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
396 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
397 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
398 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
399 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
400 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
401 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
402 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
403 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
404 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
405 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
406 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
407 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
408 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
409 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
410 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
411 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
412 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
413
414 default:
415 return NULL;
416 }
417}
418EXPORT_SYMBOL(v4l2_ctrl_get_name);
419
420void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
421 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
422{
423 *name = v4l2_ctrl_get_name(id);
424 *flags = 0;
425
426 switch (id) {
427 case V4L2_CID_AUDIO_MUTE:
428 case V4L2_CID_AUDIO_LOUDNESS:
429 case V4L2_CID_AUTO_WHITE_BALANCE:
430 case V4L2_CID_AUTOGAIN:
431 case V4L2_CID_HFLIP:
432 case V4L2_CID_VFLIP:
433 case V4L2_CID_HUE_AUTO:
434 case V4L2_CID_CHROMA_AGC:
435 case V4L2_CID_COLOR_KILLER:
436 case V4L2_CID_MPEG_AUDIO_MUTE:
437 case V4L2_CID_MPEG_VIDEO_MUTE:
438 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
439 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
440 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
441 case V4L2_CID_FOCUS_AUTO:
442 case V4L2_CID_PRIVACY:
443 case V4L2_CID_AUDIO_LIMITER_ENABLED:
444 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
445 case V4L2_CID_PILOT_TONE_ENABLED:
Jean-François Moine008d35f2010-09-13 07:04:49 -0300446 case V4L2_CID_ILLUMINATORS_1:
447 case V4L2_CID_ILLUMINATORS_2:
Hans Verkuil09965172010-08-01 14:32:42 -0300448 *type = V4L2_CTRL_TYPE_BOOLEAN;
449 *min = 0;
450 *max = *step = 1;
451 break;
452 case V4L2_CID_PAN_RESET:
453 case V4L2_CID_TILT_RESET:
454 *type = V4L2_CTRL_TYPE_BUTTON;
455 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
456 *min = *max = *step = *def = 0;
457 break;
458 case V4L2_CID_POWER_LINE_FREQUENCY:
459 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
460 case V4L2_CID_MPEG_AUDIO_ENCODING:
461 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
462 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
463 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
464 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
465 case V4L2_CID_MPEG_AUDIO_MODE:
466 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
467 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
468 case V4L2_CID_MPEG_AUDIO_CRC:
469 case V4L2_CID_MPEG_VIDEO_ENCODING:
470 case V4L2_CID_MPEG_VIDEO_ASPECT:
471 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
472 case V4L2_CID_MPEG_STREAM_TYPE:
473 case V4L2_CID_MPEG_STREAM_VBI_FMT:
474 case V4L2_CID_EXPOSURE_AUTO:
475 case V4L2_CID_COLORFX:
476 case V4L2_CID_TUNE_PREEMPHASIS:
477 *type = V4L2_CTRL_TYPE_MENU;
478 break;
479 case V4L2_CID_RDS_TX_PS_NAME:
480 case V4L2_CID_RDS_TX_RADIO_TEXT:
481 *type = V4L2_CTRL_TYPE_STRING;
482 break;
483 case V4L2_CID_USER_CLASS:
484 case V4L2_CID_CAMERA_CLASS:
485 case V4L2_CID_MPEG_CLASS:
486 case V4L2_CID_FM_TX_CLASS:
487 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
488 /* You can neither read not write these */
489 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
490 *min = *max = *step = *def = 0;
491 break;
492 case V4L2_CID_BG_COLOR:
493 *type = V4L2_CTRL_TYPE_INTEGER;
494 *step = 1;
495 *min = 0;
496 /* Max is calculated as RGB888 that is 2^24 */
497 *max = 0xFFFFFF;
498 break;
499 default:
500 *type = V4L2_CTRL_TYPE_INTEGER;
501 break;
502 }
503 switch (id) {
504 case V4L2_CID_MPEG_AUDIO_ENCODING:
505 case V4L2_CID_MPEG_AUDIO_MODE:
506 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
507 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
508 case V4L2_CID_MPEG_STREAM_TYPE:
509 *flags |= V4L2_CTRL_FLAG_UPDATE;
510 break;
511 case V4L2_CID_AUDIO_VOLUME:
512 case V4L2_CID_AUDIO_BALANCE:
513 case V4L2_CID_AUDIO_BASS:
514 case V4L2_CID_AUDIO_TREBLE:
515 case V4L2_CID_BRIGHTNESS:
516 case V4L2_CID_CONTRAST:
517 case V4L2_CID_SATURATION:
518 case V4L2_CID_HUE:
519 case V4L2_CID_RED_BALANCE:
520 case V4L2_CID_BLUE_BALANCE:
521 case V4L2_CID_GAMMA:
522 case V4L2_CID_SHARPNESS:
523 case V4L2_CID_CHROMA_GAIN:
524 case V4L2_CID_RDS_TX_DEVIATION:
525 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
526 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
527 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
528 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
529 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
530 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
531 case V4L2_CID_PILOT_TONE_DEVIATION:
532 case V4L2_CID_PILOT_TONE_FREQUENCY:
533 case V4L2_CID_TUNE_POWER_LEVEL:
534 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
535 *flags |= V4L2_CTRL_FLAG_SLIDER;
536 break;
537 case V4L2_CID_PAN_RELATIVE:
538 case V4L2_CID_TILT_RELATIVE:
539 case V4L2_CID_FOCUS_RELATIVE:
540 case V4L2_CID_IRIS_RELATIVE:
541 case V4L2_CID_ZOOM_RELATIVE:
542 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
543 break;
544 }
545}
546EXPORT_SYMBOL(v4l2_ctrl_fill);
547
548/* Helper function to determine whether the control type is compatible with
549 VIDIOC_G/S_CTRL. */
550static bool type_is_int(const struct v4l2_ctrl *ctrl)
551{
552 switch (ctrl->type) {
553 case V4L2_CTRL_TYPE_INTEGER64:
554 case V4L2_CTRL_TYPE_STRING:
555 /* Nope, these need v4l2_ext_control */
556 return false;
557 default:
558 return true;
559 }
560}
561
Hans Verkuil6e239392011-06-07 11:13:44 -0300562static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
563{
564 memset(ev->reserved, 0, sizeof(ev->reserved));
565 ev->type = V4L2_EVENT_CTRL;
566 ev->id = ctrl->id;
567 ev->u.ctrl.changes = changes;
568 ev->u.ctrl.type = ctrl->type;
569 ev->u.ctrl.flags = ctrl->flags;
570 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
571 ev->u.ctrl.value64 = 0;
572 else
573 ev->u.ctrl.value64 = ctrl->cur.val64;
574 ev->u.ctrl.minimum = ctrl->minimum;
575 ev->u.ctrl.maximum = ctrl->maximum;
576 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
577 ev->u.ctrl.step = 1;
578 else
579 ev->u.ctrl.step = ctrl->step;
580 ev->u.ctrl.default_value = ctrl->default_value;
581}
582
583static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
584{
585 struct v4l2_event ev;
Hans Verkuil77068d32011-06-13 18:55:58 -0300586 struct v4l2_subscribed_event *sev;
Hans Verkuil6e239392011-06-07 11:13:44 -0300587
Hans Verkuil77068d32011-06-13 18:55:58 -0300588 if (list_empty(&ctrl->ev_subs))
Hans Verkuil6e239392011-06-07 11:13:44 -0300589 return;
590 fill_event(&ev, ctrl, changes);
591
Hans Verkuil77068d32011-06-13 18:55:58 -0300592 list_for_each_entry(sev, &ctrl->ev_subs, node)
593 if (sev->fh && sev->fh != fh)
594 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -0300595}
596
Hans Verkuil09965172010-08-01 14:32:42 -0300597/* Helper function: copy the current control value back to the caller */
598static int cur_to_user(struct v4l2_ext_control *c,
599 struct v4l2_ctrl *ctrl)
600{
601 u32 len;
602
603 switch (ctrl->type) {
604 case V4L2_CTRL_TYPE_STRING:
605 len = strlen(ctrl->cur.string);
606 if (c->size < len + 1) {
607 c->size = len + 1;
608 return -ENOSPC;
609 }
610 return copy_to_user(c->string, ctrl->cur.string,
611 len + 1) ? -EFAULT : 0;
612 case V4L2_CTRL_TYPE_INTEGER64:
613 c->value64 = ctrl->cur.val64;
614 break;
615 default:
616 c->value = ctrl->cur.val;
617 break;
618 }
619 return 0;
620}
621
622/* Helper function: copy the caller-provider value as the new control value */
623static int user_to_new(struct v4l2_ext_control *c,
624 struct v4l2_ctrl *ctrl)
625{
626 int ret;
627 u32 size;
628
Hans Verkuil2a863792011-01-11 14:45:03 -0300629 ctrl->is_new = 1;
Hans Verkuil09965172010-08-01 14:32:42 -0300630 switch (ctrl->type) {
631 case V4L2_CTRL_TYPE_INTEGER64:
632 ctrl->val64 = c->value64;
633 break;
634 case V4L2_CTRL_TYPE_STRING:
635 size = c->size;
636 if (size == 0)
637 return -ERANGE;
638 if (size > ctrl->maximum + 1)
639 size = ctrl->maximum + 1;
640 ret = copy_from_user(ctrl->string, c->string, size);
641 if (!ret) {
642 char last = ctrl->string[size - 1];
643
644 ctrl->string[size - 1] = 0;
645 /* If the string was longer than ctrl->maximum,
646 then return an error. */
647 if (strlen(ctrl->string) == ctrl->maximum && last)
648 return -ERANGE;
649 }
650 return ret ? -EFAULT : 0;
651 default:
652 ctrl->val = c->value;
653 break;
654 }
655 return 0;
656}
657
658/* Helper function: copy the new control value back to the caller */
659static int new_to_user(struct v4l2_ext_control *c,
660 struct v4l2_ctrl *ctrl)
661{
662 u32 len;
663
664 switch (ctrl->type) {
665 case V4L2_CTRL_TYPE_STRING:
666 len = strlen(ctrl->string);
667 if (c->size < len + 1) {
668 c->size = ctrl->maximum + 1;
669 return -ENOSPC;
670 }
671 return copy_to_user(c->string, ctrl->string,
672 len + 1) ? -EFAULT : 0;
673 case V4L2_CTRL_TYPE_INTEGER64:
674 c->value64 = ctrl->val64;
675 break;
676 default:
677 c->value = ctrl->val;
678 break;
679 }
680 return 0;
681}
682
683/* Copy the new value to the current value. */
Hans Verkuilab892ba2011-06-07 06:47:18 -0300684static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
685 bool update_inactive)
Hans Verkuil09965172010-08-01 14:32:42 -0300686{
Hans Verkuil6e239392011-06-07 11:13:44 -0300687 bool changed = false;
688
Hans Verkuil09965172010-08-01 14:32:42 -0300689 if (ctrl == NULL)
690 return;
691 switch (ctrl->type) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300692 case V4L2_CTRL_TYPE_BUTTON:
693 changed = true;
694 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300695 case V4L2_CTRL_TYPE_STRING:
696 /* strings are always 0-terminated */
Hans Verkuil6e239392011-06-07 11:13:44 -0300697 changed = strcmp(ctrl->string, ctrl->cur.string);
Hans Verkuil09965172010-08-01 14:32:42 -0300698 strcpy(ctrl->cur.string, ctrl->string);
699 break;
700 case V4L2_CTRL_TYPE_INTEGER64:
Hans Verkuil6e239392011-06-07 11:13:44 -0300701 changed = ctrl->val64 != ctrl->cur.val64;
Hans Verkuil09965172010-08-01 14:32:42 -0300702 ctrl->cur.val64 = ctrl->val64;
703 break;
704 default:
Hans Verkuil6e239392011-06-07 11:13:44 -0300705 changed = ctrl->val != ctrl->cur.val;
Hans Verkuil09965172010-08-01 14:32:42 -0300706 ctrl->cur.val = ctrl->val;
707 break;
708 }
Hans Verkuil72d877c2011-06-10 05:44:36 -0300709 if (update_inactive) {
710 ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
711 if (!is_cur_manual(ctrl->cluster[0]))
712 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
713 }
Hans Verkuil6e239392011-06-07 11:13:44 -0300714 if (changed || update_inactive)
715 send_event(fh, ctrl,
716 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
717 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
Hans Verkuil09965172010-08-01 14:32:42 -0300718}
719
720/* Copy the current value to the new value */
721static void cur_to_new(struct v4l2_ctrl *ctrl)
722{
723 if (ctrl == NULL)
724 return;
725 switch (ctrl->type) {
726 case V4L2_CTRL_TYPE_STRING:
727 /* strings are always 0-terminated */
728 strcpy(ctrl->string, ctrl->cur.string);
729 break;
730 case V4L2_CTRL_TYPE_INTEGER64:
731 ctrl->val64 = ctrl->cur.val64;
732 break;
733 default:
734 ctrl->val = ctrl->cur.val;
735 break;
736 }
737}
738
739/* Return non-zero if one or more of the controls in the cluster has a new
740 value that differs from the current value. */
741static int cluster_changed(struct v4l2_ctrl *master)
742{
743 int diff = 0;
744 int i;
745
746 for (i = 0; !diff && i < master->ncontrols; i++) {
747 struct v4l2_ctrl *ctrl = master->cluster[i];
748
749 if (ctrl == NULL)
750 continue;
751 switch (ctrl->type) {
752 case V4L2_CTRL_TYPE_BUTTON:
753 /* Button controls are always 'different' */
754 return 1;
755 case V4L2_CTRL_TYPE_STRING:
756 /* strings are always 0-terminated */
757 diff = strcmp(ctrl->string, ctrl->cur.string);
758 break;
759 case V4L2_CTRL_TYPE_INTEGER64:
760 diff = ctrl->val64 != ctrl->cur.val64;
761 break;
762 default:
763 diff = ctrl->val != ctrl->cur.val;
764 break;
765 }
766 }
767 return diff;
768}
769
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300770/* Validate integer-type control */
771static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
Hans Verkuil09965172010-08-01 14:32:42 -0300772{
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300773 s32 val = *pval;
Hans Verkuil09965172010-08-01 14:32:42 -0300774 u32 offset;
Hans Verkuil09965172010-08-01 14:32:42 -0300775
776 switch (ctrl->type) {
777 case V4L2_CTRL_TYPE_INTEGER:
778 /* Round towards the closest legal value */
779 val += ctrl->step / 2;
780 if (val < ctrl->minimum)
781 val = ctrl->minimum;
782 if (val > ctrl->maximum)
783 val = ctrl->maximum;
784 offset = val - ctrl->minimum;
785 offset = ctrl->step * (offset / ctrl->step);
786 val = ctrl->minimum + offset;
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300787 *pval = val;
Hans Verkuil09965172010-08-01 14:32:42 -0300788 return 0;
789
790 case V4L2_CTRL_TYPE_BOOLEAN:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300791 *pval = !!val;
Hans Verkuil09965172010-08-01 14:32:42 -0300792 return 0;
793
794 case V4L2_CTRL_TYPE_MENU:
795 if (val < ctrl->minimum || val > ctrl->maximum)
796 return -ERANGE;
797 if (ctrl->qmenu[val][0] == '\0' ||
798 (ctrl->menu_skip_mask & (1 << val)))
799 return -EINVAL;
800 return 0;
801
802 case V4L2_CTRL_TYPE_BUTTON:
803 case V4L2_CTRL_TYPE_CTRL_CLASS:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300804 *pval = 0;
Hans Verkuil09965172010-08-01 14:32:42 -0300805 return 0;
806
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300807 default:
808 return -EINVAL;
809 }
810}
811
812/* Validate a new control */
813static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
814{
815 char *s = c->string;
816 size_t len;
817
818 switch (ctrl->type) {
819 case V4L2_CTRL_TYPE_INTEGER:
820 case V4L2_CTRL_TYPE_BOOLEAN:
821 case V4L2_CTRL_TYPE_MENU:
822 case V4L2_CTRL_TYPE_BUTTON:
823 case V4L2_CTRL_TYPE_CTRL_CLASS:
824 return validate_new_int(ctrl, &c->value);
825
Hans Verkuil09965172010-08-01 14:32:42 -0300826 case V4L2_CTRL_TYPE_INTEGER64:
827 return 0;
828
829 case V4L2_CTRL_TYPE_STRING:
830 len = strlen(s);
831 if (len < ctrl->minimum)
832 return -ERANGE;
833 if ((len - ctrl->minimum) % ctrl->step)
834 return -ERANGE;
835 return 0;
836
837 default:
838 return -EINVAL;
839 }
840}
841
842static inline u32 node2id(struct list_head *node)
843{
844 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
845}
846
847/* Set the handler's error code if it wasn't set earlier already */
848static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
849{
850 if (hdl->error == 0)
851 hdl->error = err;
852 return err;
853}
854
855/* Initialize the handler */
856int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
857 unsigned nr_of_controls_hint)
858{
859 mutex_init(&hdl->lock);
860 INIT_LIST_HEAD(&hdl->ctrls);
861 INIT_LIST_HEAD(&hdl->ctrl_refs);
862 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
863 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
864 GFP_KERNEL);
865 hdl->error = hdl->buckets ? 0 : -ENOMEM;
866 return hdl->error;
867}
868EXPORT_SYMBOL(v4l2_ctrl_handler_init);
869
870/* Free all controls and control refs */
871void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
872{
873 struct v4l2_ctrl_ref *ref, *next_ref;
874 struct v4l2_ctrl *ctrl, *next_ctrl;
Hans Verkuil77068d32011-06-13 18:55:58 -0300875 struct v4l2_subscribed_event *sev, *next_sev;
Hans Verkuil09965172010-08-01 14:32:42 -0300876
877 if (hdl == NULL || hdl->buckets == NULL)
878 return;
879
880 mutex_lock(&hdl->lock);
881 /* Free all nodes */
882 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
883 list_del(&ref->node);
884 kfree(ref);
885 }
886 /* Free all controls owned by the handler */
887 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
888 list_del(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -0300889 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
890 list_del(&sev->node);
Hans Verkuil09965172010-08-01 14:32:42 -0300891 kfree(ctrl);
892 }
893 kfree(hdl->buckets);
894 hdl->buckets = NULL;
895 hdl->cached = NULL;
896 hdl->error = 0;
897 mutex_unlock(&hdl->lock);
898}
899EXPORT_SYMBOL(v4l2_ctrl_handler_free);
900
901/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
902 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
903 with applications that do not use the NEXT_CTRL flag.
904
905 We just find the n-th private user control. It's O(N), but that should not
906 be an issue in this particular case. */
907static struct v4l2_ctrl_ref *find_private_ref(
908 struct v4l2_ctrl_handler *hdl, u32 id)
909{
910 struct v4l2_ctrl_ref *ref;
911
912 id -= V4L2_CID_PRIVATE_BASE;
913 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
914 /* Search for private user controls that are compatible with
915 VIDIOC_G/S_CTRL. */
916 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
917 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
918 if (!type_is_int(ref->ctrl))
919 continue;
920 if (id == 0)
921 return ref;
922 id--;
923 }
924 }
925 return NULL;
926}
927
928/* Find a control with the given ID. */
929static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
930{
931 struct v4l2_ctrl_ref *ref;
932 int bucket;
933
934 id &= V4L2_CTRL_ID_MASK;
935
936 /* Old-style private controls need special handling */
937 if (id >= V4L2_CID_PRIVATE_BASE)
938 return find_private_ref(hdl, id);
939 bucket = id % hdl->nr_of_buckets;
940
941 /* Simple optimization: cache the last control found */
942 if (hdl->cached && hdl->cached->ctrl->id == id)
943 return hdl->cached;
944
945 /* Not in cache, search the hash */
946 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
947 while (ref && ref->ctrl->id != id)
948 ref = ref->next;
949
950 if (ref)
951 hdl->cached = ref; /* cache it! */
952 return ref;
953}
954
955/* Find a control with the given ID. Take the handler's lock first. */
956static struct v4l2_ctrl_ref *find_ref_lock(
957 struct v4l2_ctrl_handler *hdl, u32 id)
958{
959 struct v4l2_ctrl_ref *ref = NULL;
960
961 if (hdl) {
962 mutex_lock(&hdl->lock);
963 ref = find_ref(hdl, id);
964 mutex_unlock(&hdl->lock);
965 }
966 return ref;
967}
968
969/* Find a control with the given ID. */
970struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
971{
972 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
973
974 return ref ? ref->ctrl : NULL;
975}
976EXPORT_SYMBOL(v4l2_ctrl_find);
977
978/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
979static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
980 struct v4l2_ctrl *ctrl)
981{
982 struct v4l2_ctrl_ref *ref;
983 struct v4l2_ctrl_ref *new_ref;
984 u32 id = ctrl->id;
985 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
986 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
987
988 /* Automatically add the control class if it is not yet present. */
989 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
990 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
991 return hdl->error;
992
993 if (hdl->error)
994 return hdl->error;
995
996 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
997 if (!new_ref)
998 return handler_set_err(hdl, -ENOMEM);
999 new_ref->ctrl = ctrl;
1000 if (ctrl->handler == hdl) {
1001 /* By default each control starts in a cluster of its own.
1002 new_ref->ctrl is basically a cluster array with one
1003 element, so that's perfect to use as the cluster pointer.
1004 But only do this for the handler that owns the control. */
1005 ctrl->cluster = &new_ref->ctrl;
1006 ctrl->ncontrols = 1;
1007 }
1008
1009 INIT_LIST_HEAD(&new_ref->node);
1010
1011 mutex_lock(&hdl->lock);
1012
1013 /* Add immediately at the end of the list if the list is empty, or if
1014 the last element in the list has a lower ID.
1015 This ensures that when elements are added in ascending order the
1016 insertion is an O(1) operation. */
1017 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1018 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1019 goto insert_in_hash;
1020 }
1021
1022 /* Find insert position in sorted list */
1023 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1024 if (ref->ctrl->id < id)
1025 continue;
1026 /* Don't add duplicates */
1027 if (ref->ctrl->id == id) {
1028 kfree(new_ref);
1029 goto unlock;
1030 }
1031 list_add(&new_ref->node, ref->node.prev);
1032 break;
1033 }
1034
1035insert_in_hash:
1036 /* Insert the control node in the hash */
1037 new_ref->next = hdl->buckets[bucket];
1038 hdl->buckets[bucket] = new_ref;
1039
1040unlock:
1041 mutex_unlock(&hdl->lock);
1042 return 0;
1043}
1044
1045/* Add a new control */
1046static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1047 const struct v4l2_ctrl_ops *ops,
1048 u32 id, const char *name, enum v4l2_ctrl_type type,
1049 s32 min, s32 max, u32 step, s32 def,
Hans Verkuil513521e2010-12-29 14:25:52 -03001050 u32 flags, const char * const *qmenu, void *priv)
Hans Verkuil09965172010-08-01 14:32:42 -03001051{
1052 struct v4l2_ctrl *ctrl;
1053 unsigned sz_extra = 0;
1054
1055 if (hdl->error)
1056 return NULL;
1057
1058 /* Sanity checks */
1059 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
Hans Verkuil02ac0482010-12-29 14:27:05 -03001060 max < min ||
Hans Verkuil09965172010-08-01 14:32:42 -03001061 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
1062 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1063 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1064 handler_set_err(hdl, -ERANGE);
1065 return NULL;
1066 }
Hans Verkuil02ac0482010-12-29 14:27:05 -03001067 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1068 type == V4L2_CTRL_TYPE_MENU ||
1069 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1070 (def < min || def > max)) {
1071 handler_set_err(hdl, -ERANGE);
1072 return NULL;
1073 }
Hans Verkuil09965172010-08-01 14:32:42 -03001074
1075 if (type == V4L2_CTRL_TYPE_BUTTON)
1076 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1077 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1078 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1079 else if (type == V4L2_CTRL_TYPE_STRING)
1080 sz_extra += 2 * (max + 1);
1081
1082 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1083 if (ctrl == NULL) {
1084 handler_set_err(hdl, -ENOMEM);
1085 return NULL;
1086 }
1087
1088 INIT_LIST_HEAD(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -03001089 INIT_LIST_HEAD(&ctrl->ev_subs);
Hans Verkuil09965172010-08-01 14:32:42 -03001090 ctrl->handler = hdl;
1091 ctrl->ops = ops;
1092 ctrl->id = id;
1093 ctrl->name = name;
1094 ctrl->type = type;
1095 ctrl->flags = flags;
1096 ctrl->minimum = min;
1097 ctrl->maximum = max;
1098 ctrl->step = step;
1099 ctrl->qmenu = qmenu;
1100 ctrl->priv = priv;
1101 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1102
1103 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1104 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1105 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1106 if (ctrl->minimum)
1107 memset(ctrl->cur.string, ' ', ctrl->minimum);
1108 }
1109 if (handler_new_ref(hdl, ctrl)) {
1110 kfree(ctrl);
1111 return NULL;
1112 }
1113 mutex_lock(&hdl->lock);
1114 list_add_tail(&ctrl->node, &hdl->ctrls);
1115 mutex_unlock(&hdl->lock);
1116 return ctrl;
1117}
1118
1119struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1120 const struct v4l2_ctrl_config *cfg, void *priv)
1121{
1122 bool is_menu;
1123 struct v4l2_ctrl *ctrl;
1124 const char *name = cfg->name;
Hans Verkuil513521e2010-12-29 14:25:52 -03001125 const char * const *qmenu = cfg->qmenu;
Hans Verkuil09965172010-08-01 14:32:42 -03001126 enum v4l2_ctrl_type type = cfg->type;
1127 u32 flags = cfg->flags;
1128 s32 min = cfg->min;
1129 s32 max = cfg->max;
1130 u32 step = cfg->step;
1131 s32 def = cfg->def;
1132
1133 if (name == NULL)
1134 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1135 &def, &flags);
1136
1137 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1138 if (is_menu)
1139 WARN_ON(step);
1140 else
1141 WARN_ON(cfg->menu_skip_mask);
1142 if (is_menu && qmenu == NULL)
1143 qmenu = v4l2_ctrl_get_menu(cfg->id);
1144
1145 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1146 type, min, max,
1147 is_menu ? cfg->menu_skip_mask : step,
1148 def, flags, qmenu, priv);
1149 if (ctrl) {
1150 ctrl->is_private = cfg->is_private;
1151 ctrl->is_volatile = cfg->is_volatile;
1152 }
1153 return ctrl;
1154}
1155EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1156
1157/* Helper function for standard non-menu controls */
1158struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1159 const struct v4l2_ctrl_ops *ops,
1160 u32 id, s32 min, s32 max, u32 step, s32 def)
1161{
1162 const char *name;
1163 enum v4l2_ctrl_type type;
1164 u32 flags;
1165
1166 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1167 if (type == V4L2_CTRL_TYPE_MENU) {
1168 handler_set_err(hdl, -EINVAL);
1169 return NULL;
1170 }
1171 return v4l2_ctrl_new(hdl, ops, id, name, type,
1172 min, max, step, def, flags, NULL, NULL);
1173}
1174EXPORT_SYMBOL(v4l2_ctrl_new_std);
1175
1176/* Helper function for standard menu controls */
1177struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1178 const struct v4l2_ctrl_ops *ops,
1179 u32 id, s32 max, s32 mask, s32 def)
1180{
Hans Verkuil513521e2010-12-29 14:25:52 -03001181 const char * const *qmenu = v4l2_ctrl_get_menu(id);
Hans Verkuil09965172010-08-01 14:32:42 -03001182 const char *name;
1183 enum v4l2_ctrl_type type;
1184 s32 min;
1185 s32 step;
1186 u32 flags;
1187
1188 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1189 if (type != V4L2_CTRL_TYPE_MENU) {
1190 handler_set_err(hdl, -EINVAL);
1191 return NULL;
1192 }
1193 return v4l2_ctrl_new(hdl, ops, id, name, type,
1194 0, max, mask, def, flags, qmenu, NULL);
1195}
1196EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1197
1198/* Add a control from another handler to this handler */
1199struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1200 struct v4l2_ctrl *ctrl)
1201{
1202 if (hdl == NULL || hdl->error)
1203 return NULL;
1204 if (ctrl == NULL) {
1205 handler_set_err(hdl, -EINVAL);
1206 return NULL;
1207 }
1208 if (ctrl->handler == hdl)
1209 return ctrl;
1210 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1211}
1212EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1213
1214/* Add the controls from another handler to our own. */
1215int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1216 struct v4l2_ctrl_handler *add)
1217{
1218 struct v4l2_ctrl *ctrl;
1219 int ret = 0;
1220
1221 /* Do nothing if either handler is NULL or if they are the same */
1222 if (!hdl || !add || hdl == add)
1223 return 0;
1224 if (hdl->error)
1225 return hdl->error;
1226 mutex_lock(&add->lock);
1227 list_for_each_entry(ctrl, &add->ctrls, node) {
1228 /* Skip handler-private controls. */
1229 if (ctrl->is_private)
1230 continue;
Hans Verkuil6e239392011-06-07 11:13:44 -03001231 /* And control classes */
1232 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1233 continue;
Hans Verkuil09965172010-08-01 14:32:42 -03001234 ret = handler_new_ref(hdl, ctrl);
1235 if (ret)
1236 break;
1237 }
1238 mutex_unlock(&add->lock);
1239 return ret;
1240}
1241EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1242
1243/* Cluster controls */
1244void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1245{
1246 int i;
1247
1248 /* The first control is the master control and it must not be NULL */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001249 BUG_ON(ncontrols == 0 || controls[0] == NULL);
Hans Verkuil09965172010-08-01 14:32:42 -03001250
1251 for (i = 0; i < ncontrols; i++) {
1252 if (controls[i]) {
1253 controls[i]->cluster = controls;
1254 controls[i]->ncontrols = ncontrols;
1255 }
1256 }
1257}
1258EXPORT_SYMBOL(v4l2_ctrl_cluster);
1259
Hans Verkuil72d877c2011-06-10 05:44:36 -03001260void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1261 u8 manual_val, bool set_volatile)
1262{
1263 struct v4l2_ctrl *master = controls[0];
1264 u32 flag;
1265 int i;
1266
1267 v4l2_ctrl_cluster(ncontrols, controls);
1268 WARN_ON(ncontrols <= 1);
Hans Verkuil82a7c042011-06-28 10:43:13 -03001269 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001270 master->is_auto = true;
1271 master->manual_mode_value = manual_val;
1272 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1273 flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
1274
1275 for (i = 1; i < ncontrols; i++)
1276 if (controls[i]) {
1277 controls[i]->is_volatile = set_volatile;
1278 controls[i]->flags |= flag;
1279 }
1280}
1281EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1282
Hans Verkuil09965172010-08-01 14:32:42 -03001283/* Activate/deactivate a control. */
1284void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1285{
Hans Verkuil6e239392011-06-07 11:13:44 -03001286 /* invert since the actual flag is called 'inactive' */
1287 bool inactive = !active;
1288 bool old;
1289
Hans Verkuil09965172010-08-01 14:32:42 -03001290 if (ctrl == NULL)
1291 return;
1292
Hans Verkuil6e239392011-06-07 11:13:44 -03001293 if (inactive)
Hans Verkuil09965172010-08-01 14:32:42 -03001294 /* set V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001295 old = test_and_set_bit(4, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001296 else
1297 /* clear V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001298 old = test_and_clear_bit(4, &ctrl->flags);
1299 if (old != inactive)
1300 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
Hans Verkuil09965172010-08-01 14:32:42 -03001301}
1302EXPORT_SYMBOL(v4l2_ctrl_activate);
1303
1304/* Grab/ungrab a control.
1305 Typically used when streaming starts and you want to grab controls,
1306 preventing the user from changing them.
1307
1308 Just call this and the framework will block any attempts to change
1309 these controls. */
1310void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1311{
Hans Verkuil6e239392011-06-07 11:13:44 -03001312 bool old;
1313
Hans Verkuil09965172010-08-01 14:32:42 -03001314 if (ctrl == NULL)
1315 return;
1316
Hans Verkuil6e239392011-06-07 11:13:44 -03001317 v4l2_ctrl_lock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001318 if (grabbed)
1319 /* set V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001320 old = test_and_set_bit(1, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001321 else
1322 /* clear V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001323 old = test_and_clear_bit(1, &ctrl->flags);
1324 if (old != grabbed)
1325 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1326 v4l2_ctrl_unlock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001327}
1328EXPORT_SYMBOL(v4l2_ctrl_grab);
1329
1330/* Log the control name and value */
1331static void log_ctrl(const struct v4l2_ctrl *ctrl,
1332 const char *prefix, const char *colon)
1333{
1334 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1335 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1336
1337 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1338 return;
1339 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1340 return;
1341
1342 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1343
1344 switch (ctrl->type) {
1345 case V4L2_CTRL_TYPE_INTEGER:
1346 printk(KERN_CONT "%d", ctrl->cur.val);
1347 break;
1348 case V4L2_CTRL_TYPE_BOOLEAN:
1349 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1350 break;
1351 case V4L2_CTRL_TYPE_MENU:
1352 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1353 break;
1354 case V4L2_CTRL_TYPE_INTEGER64:
1355 printk(KERN_CONT "%lld", ctrl->cur.val64);
1356 break;
1357 case V4L2_CTRL_TYPE_STRING:
1358 printk(KERN_CONT "%s", ctrl->cur.string);
1359 break;
1360 default:
1361 printk(KERN_CONT "unknown type %d", ctrl->type);
1362 break;
1363 }
1364 if (fl_inact && fl_grabbed)
1365 printk(KERN_CONT " (inactive, grabbed)\n");
1366 else if (fl_inact)
1367 printk(KERN_CONT " (inactive)\n");
1368 else if (fl_grabbed)
1369 printk(KERN_CONT " (grabbed)\n");
1370 else
1371 printk(KERN_CONT "\n");
1372}
1373
1374/* Log all controls owned by the handler */
1375void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1376 const char *prefix)
1377{
1378 struct v4l2_ctrl *ctrl;
1379 const char *colon = "";
1380 int len;
1381
1382 if (hdl == NULL)
1383 return;
1384 if (prefix == NULL)
1385 prefix = "";
1386 len = strlen(prefix);
1387 if (len && prefix[len - 1] != ' ')
1388 colon = ": ";
1389 mutex_lock(&hdl->lock);
1390 list_for_each_entry(ctrl, &hdl->ctrls, node)
1391 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1392 log_ctrl(ctrl, prefix, colon);
1393 mutex_unlock(&hdl->lock);
1394}
1395EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1396
1397/* Call s_ctrl for all controls owned by the handler */
1398int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1399{
1400 struct v4l2_ctrl *ctrl;
1401 int ret = 0;
1402
1403 if (hdl == NULL)
1404 return 0;
1405 mutex_lock(&hdl->lock);
1406 list_for_each_entry(ctrl, &hdl->ctrls, node)
1407 ctrl->done = false;
1408
1409 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1410 struct v4l2_ctrl *master = ctrl->cluster[0];
1411 int i;
1412
1413 /* Skip if this control was already handled by a cluster. */
1414 if (ctrl->done)
1415 continue;
1416
Hans Verkuil2a863792011-01-11 14:45:03 -03001417 for (i = 0; i < master->ncontrols; i++) {
1418 if (master->cluster[i]) {
1419 cur_to_new(master->cluster[i]);
1420 master->cluster[i]->is_new = 1;
1421 }
1422 }
Hans Verkuil09965172010-08-01 14:32:42 -03001423
1424 /* Skip button controls and read-only controls. */
1425 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1426 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1427 continue;
Hans Verkuil54c911e2011-05-25 06:04:58 -03001428 ret = call_op(master, s_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001429 if (ret)
1430 break;
1431 for (i = 0; i < master->ncontrols; i++)
1432 if (master->cluster[i])
1433 master->cluster[i]->done = true;
1434 }
1435 mutex_unlock(&hdl->lock);
1436 return ret;
1437}
1438EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1439
1440/* Implement VIDIOC_QUERYCTRL */
1441int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1442{
1443 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1444 struct v4l2_ctrl_ref *ref;
1445 struct v4l2_ctrl *ctrl;
1446
1447 if (hdl == NULL)
1448 return -EINVAL;
1449
1450 mutex_lock(&hdl->lock);
1451
1452 /* Try to find it */
1453 ref = find_ref(hdl, id);
1454
1455 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1456 /* Find the next control with ID > qc->id */
1457
1458 /* Did we reach the end of the control list? */
1459 if (id >= node2id(hdl->ctrl_refs.prev)) {
1460 ref = NULL; /* Yes, so there is no next control */
1461 } else if (ref) {
1462 /* We found a control with the given ID, so just get
1463 the next one in the list. */
1464 ref = list_entry(ref->node.next, typeof(*ref), node);
1465 } else {
1466 /* No control with the given ID exists, so start
1467 searching for the next largest ID. We know there
1468 is one, otherwise the first 'if' above would have
1469 been true. */
1470 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1471 if (id < ref->ctrl->id)
1472 break;
1473 }
1474 }
1475 mutex_unlock(&hdl->lock);
1476 if (!ref)
1477 return -EINVAL;
1478
1479 ctrl = ref->ctrl;
1480 memset(qc, 0, sizeof(*qc));
Hans Verkuil829fb2d2011-01-16 11:21:40 -03001481 if (id >= V4L2_CID_PRIVATE_BASE)
1482 qc->id = id;
1483 else
1484 qc->id = ctrl->id;
Hans Verkuil09965172010-08-01 14:32:42 -03001485 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1486 qc->minimum = ctrl->minimum;
1487 qc->maximum = ctrl->maximum;
1488 qc->default_value = ctrl->default_value;
Laurent Pincharteac9aa02010-12-07 08:57:25 -03001489 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
Hans Verkuil09965172010-08-01 14:32:42 -03001490 qc->step = 1;
1491 else
1492 qc->step = ctrl->step;
1493 qc->flags = ctrl->flags;
1494 qc->type = ctrl->type;
1495 return 0;
1496}
1497EXPORT_SYMBOL(v4l2_queryctrl);
1498
1499int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1500{
Hans Verkuil87a0c942011-02-22 12:31:07 -03001501 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1502 return -EINVAL;
Hans Verkuil09965172010-08-01 14:32:42 -03001503 return v4l2_queryctrl(sd->ctrl_handler, qc);
1504}
1505EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1506
1507/* Implement VIDIOC_QUERYMENU */
1508int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1509{
1510 struct v4l2_ctrl *ctrl;
1511 u32 i = qm->index;
1512
1513 ctrl = v4l2_ctrl_find(hdl, qm->id);
1514 if (!ctrl)
1515 return -EINVAL;
1516
1517 qm->reserved = 0;
1518 /* Sanity checks */
1519 if (ctrl->qmenu == NULL ||
1520 i < ctrl->minimum || i > ctrl->maximum)
1521 return -EINVAL;
1522 /* Use mask to see if this menu item should be skipped */
1523 if (ctrl->menu_skip_mask & (1 << i))
1524 return -EINVAL;
1525 /* Empty menu items should also be skipped */
1526 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1527 return -EINVAL;
1528 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1529 return 0;
1530}
1531EXPORT_SYMBOL(v4l2_querymenu);
1532
1533int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1534{
1535 return v4l2_querymenu(sd->ctrl_handler, qm);
1536}
1537EXPORT_SYMBOL(v4l2_subdev_querymenu);
1538
1539
1540
1541/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1542
1543 It is not a fully atomic operation, just best-effort only. After all, if
1544 multiple controls have to be set through multiple i2c writes (for example)
1545 then some initial writes may succeed while others fail. Thus leaving the
1546 system in an inconsistent state. The question is how much effort you are
1547 willing to spend on trying to make something atomic that really isn't.
1548
1549 From the point of view of an application the main requirement is that
1550 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1551 error should be returned without actually affecting any controls.
1552
1553 If all the values are correct, then it is acceptable to just give up
1554 in case of low-level errors.
1555
1556 It is important though that the application can tell when only a partial
1557 configuration was done. The way we do that is through the error_idx field
1558 of struct v4l2_ext_controls: if that is equal to the count field then no
1559 controls were affected. Otherwise all controls before that index were
1560 successful in performing their 'get' or 'set' operation, the control at
1561 the given index failed, and you don't know what happened with the controls
1562 after the failed one. Since if they were part of a control cluster they
1563 could have been successfully processed (if a cluster member was encountered
1564 at index < error_idx), they could have failed (if a cluster member was at
1565 error_idx), or they may not have been processed yet (if the first cluster
1566 member appeared after error_idx).
1567
1568 It is all fairly theoretical, though. In practice all you can do is to
1569 bail out. If error_idx == count, then it is an application bug. If
1570 error_idx < count then it is only an application bug if the error code was
1571 EBUSY. That usually means that something started streaming just when you
1572 tried to set the controls. In all other cases it is a driver/hardware
1573 problem and all you can do is to retry or bail out.
1574
1575 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1576 never modifies controls the error_idx is just set to whatever control
1577 has an invalid value.
1578 */
1579
1580/* Prepare for the extended g/s/try functions.
1581 Find the controls in the control array and do some basic checks. */
1582static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1583 struct v4l2_ext_controls *cs,
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001584 struct v4l2_ctrl_helper *helpers)
Hans Verkuil09965172010-08-01 14:32:42 -03001585{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001586 struct v4l2_ctrl_helper *h;
1587 bool have_clusters = false;
Hans Verkuil09965172010-08-01 14:32:42 -03001588 u32 i;
1589
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001590 for (i = 0, h = helpers; i < cs->count; i++, h++) {
Hans Verkuil09965172010-08-01 14:32:42 -03001591 struct v4l2_ext_control *c = &cs->controls[i];
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001592 struct v4l2_ctrl_ref *ref;
Hans Verkuil09965172010-08-01 14:32:42 -03001593 struct v4l2_ctrl *ctrl;
1594 u32 id = c->id & V4L2_CTRL_ID_MASK;
1595
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001596 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001597
1598 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1599 return -EINVAL;
1600
1601 /* Old-style private controls are not allowed for
1602 extended controls */
1603 if (id >= V4L2_CID_PRIVATE_BASE)
1604 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001605 ref = find_ref_lock(hdl, id);
1606 if (ref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001607 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001608 ctrl = ref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001609 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1610 return -EINVAL;
1611
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001612 if (ctrl->cluster[0]->ncontrols > 1)
1613 have_clusters = true;
1614 if (ctrl->cluster[0] != ctrl)
1615 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
1616 /* Store the ref to the master control of the cluster */
1617 h->mref = ref;
1618 h->ctrl = ctrl;
1619 /* Initially set next to 0, meaning that there is no other
1620 control in this helper array belonging to the same
1621 cluster */
1622 h->next = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001623 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001624
1625 /* We are done if there were no controls that belong to a multi-
1626 control cluster. */
1627 if (!have_clusters)
1628 return 0;
1629
1630 /* The code below figures out in O(n) time which controls in the list
1631 belong to the same cluster. */
1632
1633 /* This has to be done with the handler lock taken. */
1634 mutex_lock(&hdl->lock);
1635
1636 /* First zero the helper field in the master control references */
1637 for (i = 0; i < cs->count; i++)
1638 helpers[i].mref->helper = 0;
1639 for (i = 0, h = helpers; i < cs->count; i++, h++) {
1640 struct v4l2_ctrl_ref *mref = h->mref;
1641
1642 /* If the mref->helper is set, then it points to an earlier
1643 helper that belongs to the same cluster. */
1644 if (mref->helper) {
1645 /* Set the next field of mref->helper to the current
1646 index: this means that that earlier helper now
1647 points to the next helper in the same cluster. */
1648 mref->helper->next = i;
1649 /* mref should be set only for the first helper in the
1650 cluster, clear the others. */
1651 h->mref = NULL;
1652 }
1653 /* Point the mref helper to the current helper struct. */
1654 mref->helper = h;
1655 }
1656 mutex_unlock(&hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001657 return 0;
1658}
1659
Hans Verkuil09965172010-08-01 14:32:42 -03001660/* Handles the corner case where cs->count == 0. It checks whether the
1661 specified control class exists. If that class ID is 0, then it checks
1662 whether there are any controls at all. */
1663static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1664{
1665 if (ctrl_class == 0)
1666 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1667 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1668}
1669
1670
1671
1672/* Get extended controls. Allocates the helpers array if needed. */
1673int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1674{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001675 struct v4l2_ctrl_helper helper[4];
1676 struct v4l2_ctrl_helper *helpers = helper;
Hans Verkuil09965172010-08-01 14:32:42 -03001677 int ret;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001678 int i, j;
Hans Verkuil09965172010-08-01 14:32:42 -03001679
1680 cs->error_idx = cs->count;
1681 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1682
1683 if (hdl == NULL)
1684 return -EINVAL;
1685
1686 if (cs->count == 0)
1687 return class_check(hdl, cs->ctrl_class);
1688
1689 if (cs->count > ARRAY_SIZE(helper)) {
1690 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1691 if (helpers == NULL)
1692 return -ENOMEM;
1693 }
1694
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001695 ret = prepare_ext_ctrls(hdl, cs, helpers);
1696 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001697
1698 for (i = 0; !ret && i < cs->count; i++)
1699 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1700 ret = -EACCES;
1701
1702 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001703 int (*ctrl_to_user)(struct v4l2_ext_control *c,
1704 struct v4l2_ctrl *ctrl) = cur_to_user;
1705 struct v4l2_ctrl *master;
Hans Verkuil09965172010-08-01 14:32:42 -03001706
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001707 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001708 continue;
1709
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001710 master = helpers[i].mref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001711 cs->error_idx = i;
1712
1713 v4l2_ctrl_lock(master);
Hans Verkuilddac5c12011-06-10 05:43:34 -03001714
1715 /* g_volatile_ctrl will update the new control values */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001716 if (has_op(master, g_volatile_ctrl) && !is_cur_manual(master)) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03001717 for (j = 0; j < master->ncontrols; j++)
1718 cur_to_new(master->cluster[j]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03001719 ret = call_op(master, g_volatile_ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001720 ctrl_to_user = new_to_user;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001721 }
1722 /* If OK, then copy the current (for non-volatile controls)
1723 or the new (for volatile controls) control values to the
1724 caller */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001725 if (!ret) {
1726 u32 idx = i;
1727
1728 do {
1729 ret = ctrl_to_user(cs->controls + idx,
1730 helpers[idx].ctrl);
1731 idx = helpers[idx].next;
1732 } while (!ret && idx);
1733 }
Hans Verkuil09965172010-08-01 14:32:42 -03001734 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001735 }
1736
1737 if (cs->count > ARRAY_SIZE(helper))
1738 kfree(helpers);
1739 return ret;
1740}
1741EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1742
1743int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1744{
1745 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1746}
1747EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1748
1749/* Helper function to get a single control */
1750static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1751{
1752 struct v4l2_ctrl *master = ctrl->cluster[0];
1753 int ret = 0;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001754 int i;
Hans Verkuil09965172010-08-01 14:32:42 -03001755
1756 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1757 return -EACCES;
1758
1759 v4l2_ctrl_lock(master);
1760 /* g_volatile_ctrl will update the current control values */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001761 if (ctrl->is_volatile && !is_cur_manual(master)) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03001762 for (i = 0; i < master->ncontrols; i++)
1763 cur_to_new(master->cluster[i]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03001764 ret = call_op(master, g_volatile_ctrl);
Hans Verkuilddac5c12011-06-10 05:43:34 -03001765 *val = ctrl->val;
1766 } else {
1767 *val = ctrl->cur.val;
1768 }
Hans Verkuil09965172010-08-01 14:32:42 -03001769 v4l2_ctrl_unlock(master);
1770 return ret;
1771}
1772
1773int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1774{
1775 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1776
1777 if (ctrl == NULL || !type_is_int(ctrl))
1778 return -EINVAL;
1779 return get_ctrl(ctrl, &control->value);
1780}
1781EXPORT_SYMBOL(v4l2_g_ctrl);
1782
1783int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1784{
1785 return v4l2_g_ctrl(sd->ctrl_handler, control);
1786}
1787EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1788
1789s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1790{
1791 s32 val = 0;
1792
1793 /* It's a driver bug if this happens. */
1794 WARN_ON(!type_is_int(ctrl));
1795 get_ctrl(ctrl, &val);
1796 return val;
1797}
1798EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1799
1800
1801/* Core function that calls try/s_ctrl and ensures that the new value is
1802 copied to the current value on a set.
1803 Must be called with ctrl->handler->lock held. */
Hans Verkuilab892ba2011-06-07 06:47:18 -03001804static int try_or_set_control_cluster(struct v4l2_fh *fh,
1805 struct v4l2_ctrl *master, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03001806{
Hans Verkuil72d877c2011-06-10 05:44:36 -03001807 bool update_flag;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001808 int ret;
Hans Verkuil09965172010-08-01 14:32:42 -03001809 int i;
1810
1811 /* Go through the cluster and either validate the new value or
1812 (if no new value was set), copy the current value to the new
1813 value, ensuring a consistent view for the control ops when
1814 called. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001815 for (i = 0; i < master->ncontrols; i++) {
Hans Verkuil09965172010-08-01 14:32:42 -03001816 struct v4l2_ctrl *ctrl = master->cluster[i];
1817
1818 if (ctrl == NULL)
1819 continue;
1820
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001821 if (!ctrl->is_new) {
1822 cur_to_new(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001823 continue;
1824 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001825 /* Check again: it may have changed since the
1826 previous check in try_or_set_ext_ctrls(). */
1827 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1828 return -EBUSY;
Hans Verkuil09965172010-08-01 14:32:42 -03001829 }
1830
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001831 ret = call_op(master, try_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001832
1833 /* Don't set if there is no change */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001834 if (ret || !set || !cluster_changed(master))
1835 return ret;
1836 ret = call_op(master, s_ctrl);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001837 if (ret)
1838 return ret;
1839
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001840 /* If OK, then make the new values permanent. */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001841 update_flag = is_cur_manual(master) != is_new_manual(master);
1842 for (i = 0; i < master->ncontrols; i++)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001843 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001844 return 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001845}
1846
1847/* Try or set controls. */
Hans Verkuilab892ba2011-06-07 06:47:18 -03001848static int try_or_set_ext_ctrls(struct v4l2_fh *fh,
1849 struct v4l2_ctrl_handler *hdl,
Hans Verkuil09965172010-08-01 14:32:42 -03001850 struct v4l2_ext_controls *cs,
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001851 struct v4l2_ctrl_helper *helpers,
Hans Verkuil09965172010-08-01 14:32:42 -03001852 bool set)
1853{
1854 unsigned i, j;
1855 int ret = 0;
1856
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001857 /* Phase 1: validation */
1858 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001859 for (i = 0; i < cs->count; i++) {
1860 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1861
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001862 if (!set)
1863 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001864
1865 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1866 return -EACCES;
1867 /* This test is also done in try_set_control_cluster() which
1868 is called in atomic context, so that has the final say,
1869 but it makes sense to do an up-front check as well. Once
1870 an error occurs in try_set_control_cluster() some other
1871 controls may have been set already and we want to do a
1872 best-effort to avoid that. */
1873 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1874 return -EBUSY;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001875 ret = validate_new(ctrl, &cs->controls[i]);
1876 if (ret)
1877 return ret;
Hans Verkuil09965172010-08-01 14:32:42 -03001878 }
1879
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001880 /* Phase 2: set/try controls */
Hans Verkuil09965172010-08-01 14:32:42 -03001881 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001882 struct v4l2_ctrl *master;
1883 u32 idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001884
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001885 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001886 continue;
1887
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001888 cs->error_idx = i;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001889 master = helpers[i].mref->ctrl;
1890 v4l2_ctrl_lock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001891
Hans Verkuil2a863792011-01-11 14:45:03 -03001892 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03001893 for (j = 0; j < master->ncontrols; j++)
1894 if (master->cluster[j])
Hans Verkuil2a863792011-01-11 14:45:03 -03001895 master->cluster[j]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001896
1897 /* Copy the new caller-supplied control values.
Hans Verkuil2a863792011-01-11 14:45:03 -03001898 user_to_new() sets 'is_new' to 1. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001899 do {
1900 ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
1901 idx = helpers[idx].next;
1902 } while (!ret && idx);
Hans Verkuil09965172010-08-01 14:32:42 -03001903
1904 if (!ret)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001905 ret = try_or_set_control_cluster(fh, master, set);
Hans Verkuil09965172010-08-01 14:32:42 -03001906
1907 /* Copy the new values back to userspace. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001908 if (!ret) {
1909 idx = i;
1910 do {
1911 ret = user_to_new(cs->controls + idx,
1912 helpers[idx].ctrl);
1913 idx = helpers[idx].next;
1914 } while (!ret && idx);
1915 }
1916 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001917 }
1918 return ret;
1919}
1920
1921/* Try or try-and-set controls */
Hans Verkuilab892ba2011-06-07 06:47:18 -03001922static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
Hans Verkuil09965172010-08-01 14:32:42 -03001923 struct v4l2_ext_controls *cs,
1924 bool set)
1925{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001926 struct v4l2_ctrl_helper helper[4];
1927 struct v4l2_ctrl_helper *helpers = helper;
Hans Verkuil09965172010-08-01 14:32:42 -03001928 int ret;
Hans Verkuil09965172010-08-01 14:32:42 -03001929
1930 cs->error_idx = cs->count;
1931 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1932
1933 if (hdl == NULL)
1934 return -EINVAL;
1935
1936 if (cs->count == 0)
1937 return class_check(hdl, cs->ctrl_class);
1938
1939 if (cs->count > ARRAY_SIZE(helper)) {
1940 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1941 if (!helpers)
1942 return -ENOMEM;
1943 }
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001944 ret = prepare_ext_ctrls(hdl, cs, helpers);
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001945 if (!ret)
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001946 ret = try_or_set_ext_ctrls(fh, hdl, cs, helpers, set);
1947 else if (set)
Hans Verkuil09965172010-08-01 14:32:42 -03001948 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001949
Hans Verkuil09965172010-08-01 14:32:42 -03001950 if (cs->count > ARRAY_SIZE(helper))
1951 kfree(helpers);
1952 return ret;
1953}
1954
1955int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1956{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001957 return try_set_ext_ctrls(NULL, hdl, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03001958}
1959EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1960
Hans Verkuilab892ba2011-06-07 06:47:18 -03001961int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1962 struct v4l2_ext_controls *cs)
Hans Verkuil09965172010-08-01 14:32:42 -03001963{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001964 return try_set_ext_ctrls(fh, hdl, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03001965}
1966EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1967
1968int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1969{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001970 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03001971}
1972EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1973
1974int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1975{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001976 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03001977}
1978EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1979
1980/* Helper function for VIDIOC_S_CTRL compatibility */
Hans Verkuilab892ba2011-06-07 06:47:18 -03001981static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
Hans Verkuil09965172010-08-01 14:32:42 -03001982{
1983 struct v4l2_ctrl *master = ctrl->cluster[0];
1984 int ret;
1985 int i;
1986
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001987 ret = validate_new_int(ctrl, val);
1988 if (ret)
1989 return ret;
1990
Hans Verkuil09965172010-08-01 14:32:42 -03001991 v4l2_ctrl_lock(ctrl);
1992
Hans Verkuil2a863792011-01-11 14:45:03 -03001993 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03001994 for (i = 0; i < master->ncontrols; i++)
1995 if (master->cluster[i])
Hans Verkuil2a863792011-01-11 14:45:03 -03001996 master->cluster[i]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001997
1998 ctrl->val = *val;
Hans Verkuil2a863792011-01-11 14:45:03 -03001999 ctrl->is_new = 1;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002000 ret = try_or_set_control_cluster(fh, master, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002001 *val = ctrl->cur.val;
2002 v4l2_ctrl_unlock(ctrl);
2003 return ret;
2004}
2005
Hans Verkuilab892ba2011-06-07 06:47:18 -03002006int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2007 struct v4l2_control *control)
Hans Verkuil09965172010-08-01 14:32:42 -03002008{
2009 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2010
2011 if (ctrl == NULL || !type_is_int(ctrl))
2012 return -EINVAL;
2013
Hans Verkuil7ebbc392011-06-07 04:50:31 -03002014 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2015 return -EACCES;
2016
Hans Verkuilab892ba2011-06-07 06:47:18 -03002017 return set_ctrl(fh, ctrl, &control->value);
Hans Verkuil09965172010-08-01 14:32:42 -03002018}
2019EXPORT_SYMBOL(v4l2_s_ctrl);
2020
2021int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2022{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002023 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
Hans Verkuil09965172010-08-01 14:32:42 -03002024}
2025EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2026
2027int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2028{
2029 /* It's a driver bug if this happens. */
2030 WARN_ON(!type_is_int(ctrl));
Hans Verkuilab892ba2011-06-07 06:47:18 -03002031 return set_ctrl(NULL, ctrl, &val);
Hans Verkuil09965172010-08-01 14:32:42 -03002032}
2033EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
Hans Verkuil6e239392011-06-07 11:13:44 -03002034
Hans Verkuil77068d32011-06-13 18:55:58 -03002035void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
2036 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002037{
Hans Verkuil6e239392011-06-07 11:13:44 -03002038 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002039 list_add_tail(&sev->node, &ctrl->ev_subs);
Hans Verkuil6e239392011-06-07 11:13:44 -03002040 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
Hans Verkuil77068d32011-06-13 18:55:58 -03002041 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
Hans Verkuil6e239392011-06-07 11:13:44 -03002042 struct v4l2_event ev;
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002043 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
Hans Verkuil6e239392011-06-07 11:13:44 -03002044
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002045 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2046 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2047 fill_event(&ev, ctrl, changes);
Hans Verkuil77068d32011-06-13 18:55:58 -03002048 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -03002049 }
2050 v4l2_ctrl_unlock(ctrl);
2051}
Hans Verkuil77068d32011-06-13 18:55:58 -03002052EXPORT_SYMBOL(v4l2_ctrl_add_event);
Hans Verkuil6e239392011-06-07 11:13:44 -03002053
Hans Verkuil77068d32011-06-13 18:55:58 -03002054void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
2055 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002056{
Hans Verkuil6e239392011-06-07 11:13:44 -03002057 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002058 list_del(&sev->node);
Hans Verkuil6e239392011-06-07 11:13:44 -03002059 v4l2_ctrl_unlock(ctrl);
2060}
Hans Verkuil77068d32011-06-13 18:55:58 -03002061EXPORT_SYMBOL(v4l2_ctrl_del_event);