blob: 86554b5cdca08315470bf286c63b3e23200047ef [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 Verkuil3f66f0e2011-06-20 11:56:24 -0300589 return;
Hans Verkuil6e239392011-06-07 11:13:44 -0300590 fill_event(&ev, ctrl, changes);
591
Hans Verkuil77068d32011-06-13 18:55:58 -0300592 list_for_each_entry(sev, &ctrl->ev_subs, node)
Hans Verkuil60c073222011-06-29 08:56:22 -0300593 if (sev->fh && (sev->fh != fh ||
594 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK)))
Hans Verkuil77068d32011-06-13 18:55:58 -0300595 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -0300596}
597
Hans Verkuil09965172010-08-01 14:32:42 -0300598/* Helper function: copy the current control value back to the caller */
599static int cur_to_user(struct v4l2_ext_control *c,
600 struct v4l2_ctrl *ctrl)
601{
602 u32 len;
603
604 switch (ctrl->type) {
605 case V4L2_CTRL_TYPE_STRING:
606 len = strlen(ctrl->cur.string);
607 if (c->size < len + 1) {
608 c->size = len + 1;
609 return -ENOSPC;
610 }
611 return copy_to_user(c->string, ctrl->cur.string,
612 len + 1) ? -EFAULT : 0;
613 case V4L2_CTRL_TYPE_INTEGER64:
614 c->value64 = ctrl->cur.val64;
615 break;
616 default:
617 c->value = ctrl->cur.val;
618 break;
619 }
620 return 0;
621}
622
623/* Helper function: copy the caller-provider value as the new control value */
624static int user_to_new(struct v4l2_ext_control *c,
625 struct v4l2_ctrl *ctrl)
626{
627 int ret;
628 u32 size;
629
Hans Verkuil2a863792011-01-11 14:45:03 -0300630 ctrl->is_new = 1;
Hans Verkuil09965172010-08-01 14:32:42 -0300631 switch (ctrl->type) {
632 case V4L2_CTRL_TYPE_INTEGER64:
633 ctrl->val64 = c->value64;
634 break;
635 case V4L2_CTRL_TYPE_STRING:
636 size = c->size;
637 if (size == 0)
638 return -ERANGE;
639 if (size > ctrl->maximum + 1)
640 size = ctrl->maximum + 1;
641 ret = copy_from_user(ctrl->string, c->string, size);
642 if (!ret) {
643 char last = ctrl->string[size - 1];
644
645 ctrl->string[size - 1] = 0;
646 /* If the string was longer than ctrl->maximum,
647 then return an error. */
648 if (strlen(ctrl->string) == ctrl->maximum && last)
649 return -ERANGE;
650 }
651 return ret ? -EFAULT : 0;
652 default:
653 ctrl->val = c->value;
654 break;
655 }
656 return 0;
657}
658
659/* Helper function: copy the new control value back to the caller */
660static int new_to_user(struct v4l2_ext_control *c,
661 struct v4l2_ctrl *ctrl)
662{
663 u32 len;
664
665 switch (ctrl->type) {
666 case V4L2_CTRL_TYPE_STRING:
667 len = strlen(ctrl->string);
668 if (c->size < len + 1) {
669 c->size = ctrl->maximum + 1;
670 return -ENOSPC;
671 }
672 return copy_to_user(c->string, ctrl->string,
673 len + 1) ? -EFAULT : 0;
674 case V4L2_CTRL_TYPE_INTEGER64:
675 c->value64 = ctrl->val64;
676 break;
677 default:
678 c->value = ctrl->val;
679 break;
680 }
681 return 0;
682}
683
684/* Copy the new value to the current value. */
Hans Verkuilab892ba2011-06-07 06:47:18 -0300685static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
686 bool update_inactive)
Hans Verkuil09965172010-08-01 14:32:42 -0300687{
Hans Verkuil6e239392011-06-07 11:13:44 -0300688 bool changed = false;
689
Hans Verkuil09965172010-08-01 14:32:42 -0300690 if (ctrl == NULL)
691 return;
692 switch (ctrl->type) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300693 case V4L2_CTRL_TYPE_BUTTON:
694 changed = true;
695 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300696 case V4L2_CTRL_TYPE_STRING:
697 /* strings are always 0-terminated */
Hans Verkuil6e239392011-06-07 11:13:44 -0300698 changed = strcmp(ctrl->string, ctrl->cur.string);
Hans Verkuil09965172010-08-01 14:32:42 -0300699 strcpy(ctrl->cur.string, ctrl->string);
700 break;
701 case V4L2_CTRL_TYPE_INTEGER64:
Hans Verkuil6e239392011-06-07 11:13:44 -0300702 changed = ctrl->val64 != ctrl->cur.val64;
Hans Verkuil09965172010-08-01 14:32:42 -0300703 ctrl->cur.val64 = ctrl->val64;
704 break;
705 default:
Hans Verkuil6e239392011-06-07 11:13:44 -0300706 changed = ctrl->val != ctrl->cur.val;
Hans Verkuil09965172010-08-01 14:32:42 -0300707 ctrl->cur.val = ctrl->val;
708 break;
709 }
Hans Verkuil72d877c2011-06-10 05:44:36 -0300710 if (update_inactive) {
711 ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
712 if (!is_cur_manual(ctrl->cluster[0]))
713 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
714 }
Hans Verkuil639884a2011-07-05 07:09:26 -0300715 if (changed || update_inactive) {
716 /* If a control was changed that was not one of the controls
717 modified by the application, then send the event to all. */
718 if (!ctrl->is_new)
719 fh = NULL;
Hans Verkuil6e239392011-06-07 11:13:44 -0300720 send_event(fh, ctrl,
721 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
722 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
Hans Verkuil639884a2011-07-05 07:09:26 -0300723 }
Hans Verkuil09965172010-08-01 14:32:42 -0300724}
725
726/* Copy the current value to the new value */
727static void cur_to_new(struct v4l2_ctrl *ctrl)
728{
729 if (ctrl == NULL)
730 return;
731 switch (ctrl->type) {
732 case V4L2_CTRL_TYPE_STRING:
733 /* strings are always 0-terminated */
734 strcpy(ctrl->string, ctrl->cur.string);
735 break;
736 case V4L2_CTRL_TYPE_INTEGER64:
737 ctrl->val64 = ctrl->cur.val64;
738 break;
739 default:
740 ctrl->val = ctrl->cur.val;
741 break;
742 }
743}
744
745/* Return non-zero if one or more of the controls in the cluster has a new
746 value that differs from the current value. */
747static int cluster_changed(struct v4l2_ctrl *master)
748{
749 int diff = 0;
750 int i;
751
752 for (i = 0; !diff && i < master->ncontrols; i++) {
753 struct v4l2_ctrl *ctrl = master->cluster[i];
754
755 if (ctrl == NULL)
756 continue;
757 switch (ctrl->type) {
758 case V4L2_CTRL_TYPE_BUTTON:
759 /* Button controls are always 'different' */
760 return 1;
761 case V4L2_CTRL_TYPE_STRING:
762 /* strings are always 0-terminated */
763 diff = strcmp(ctrl->string, ctrl->cur.string);
764 break;
765 case V4L2_CTRL_TYPE_INTEGER64:
766 diff = ctrl->val64 != ctrl->cur.val64;
767 break;
768 default:
769 diff = ctrl->val != ctrl->cur.val;
770 break;
771 }
772 }
773 return diff;
774}
775
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300776/* Validate integer-type control */
777static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
Hans Verkuil09965172010-08-01 14:32:42 -0300778{
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300779 s32 val = *pval;
Hans Verkuil09965172010-08-01 14:32:42 -0300780 u32 offset;
Hans Verkuil09965172010-08-01 14:32:42 -0300781
782 switch (ctrl->type) {
783 case V4L2_CTRL_TYPE_INTEGER:
784 /* Round towards the closest legal value */
785 val += ctrl->step / 2;
786 if (val < ctrl->minimum)
787 val = ctrl->minimum;
788 if (val > ctrl->maximum)
789 val = ctrl->maximum;
790 offset = val - ctrl->minimum;
791 offset = ctrl->step * (offset / ctrl->step);
792 val = ctrl->minimum + offset;
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300793 *pval = val;
Hans Verkuil09965172010-08-01 14:32:42 -0300794 return 0;
795
796 case V4L2_CTRL_TYPE_BOOLEAN:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300797 *pval = !!val;
Hans Verkuil09965172010-08-01 14:32:42 -0300798 return 0;
799
800 case V4L2_CTRL_TYPE_MENU:
801 if (val < ctrl->minimum || val > ctrl->maximum)
802 return -ERANGE;
803 if (ctrl->qmenu[val][0] == '\0' ||
804 (ctrl->menu_skip_mask & (1 << val)))
805 return -EINVAL;
806 return 0;
807
Hans Verkuilfa4d7092011-05-23 04:07:05 -0300808 case V4L2_CTRL_TYPE_BITMASK:
809 *pval &= ctrl->maximum;
810 return 0;
811
Hans Verkuil09965172010-08-01 14:32:42 -0300812 case V4L2_CTRL_TYPE_BUTTON:
813 case V4L2_CTRL_TYPE_CTRL_CLASS:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300814 *pval = 0;
Hans Verkuil09965172010-08-01 14:32:42 -0300815 return 0;
816
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300817 default:
818 return -EINVAL;
819 }
820}
821
822/* Validate a new control */
823static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
824{
825 char *s = c->string;
826 size_t len;
827
828 switch (ctrl->type) {
829 case V4L2_CTRL_TYPE_INTEGER:
830 case V4L2_CTRL_TYPE_BOOLEAN:
831 case V4L2_CTRL_TYPE_MENU:
Hans Verkuilfa4d7092011-05-23 04:07:05 -0300832 case V4L2_CTRL_TYPE_BITMASK:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300833 case V4L2_CTRL_TYPE_BUTTON:
834 case V4L2_CTRL_TYPE_CTRL_CLASS:
835 return validate_new_int(ctrl, &c->value);
836
Hans Verkuil09965172010-08-01 14:32:42 -0300837 case V4L2_CTRL_TYPE_INTEGER64:
838 return 0;
839
840 case V4L2_CTRL_TYPE_STRING:
841 len = strlen(s);
842 if (len < ctrl->minimum)
843 return -ERANGE;
844 if ((len - ctrl->minimum) % ctrl->step)
845 return -ERANGE;
846 return 0;
847
848 default:
849 return -EINVAL;
850 }
851}
852
853static inline u32 node2id(struct list_head *node)
854{
855 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
856}
857
858/* Set the handler's error code if it wasn't set earlier already */
859static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
860{
861 if (hdl->error == 0)
862 hdl->error = err;
863 return err;
864}
865
866/* Initialize the handler */
867int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
868 unsigned nr_of_controls_hint)
869{
870 mutex_init(&hdl->lock);
871 INIT_LIST_HEAD(&hdl->ctrls);
872 INIT_LIST_HEAD(&hdl->ctrl_refs);
873 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
874 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
875 GFP_KERNEL);
876 hdl->error = hdl->buckets ? 0 : -ENOMEM;
877 return hdl->error;
878}
879EXPORT_SYMBOL(v4l2_ctrl_handler_init);
880
881/* Free all controls and control refs */
882void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
883{
884 struct v4l2_ctrl_ref *ref, *next_ref;
885 struct v4l2_ctrl *ctrl, *next_ctrl;
Hans Verkuil77068d32011-06-13 18:55:58 -0300886 struct v4l2_subscribed_event *sev, *next_sev;
Hans Verkuil09965172010-08-01 14:32:42 -0300887
888 if (hdl == NULL || hdl->buckets == NULL)
889 return;
890
891 mutex_lock(&hdl->lock);
892 /* Free all nodes */
893 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
894 list_del(&ref->node);
895 kfree(ref);
896 }
897 /* Free all controls owned by the handler */
898 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
899 list_del(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -0300900 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
901 list_del(&sev->node);
Hans Verkuil09965172010-08-01 14:32:42 -0300902 kfree(ctrl);
903 }
904 kfree(hdl->buckets);
905 hdl->buckets = NULL;
906 hdl->cached = NULL;
907 hdl->error = 0;
908 mutex_unlock(&hdl->lock);
909}
910EXPORT_SYMBOL(v4l2_ctrl_handler_free);
911
912/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
913 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
914 with applications that do not use the NEXT_CTRL flag.
915
916 We just find the n-th private user control. It's O(N), but that should not
917 be an issue in this particular case. */
918static struct v4l2_ctrl_ref *find_private_ref(
919 struct v4l2_ctrl_handler *hdl, u32 id)
920{
921 struct v4l2_ctrl_ref *ref;
922
923 id -= V4L2_CID_PRIVATE_BASE;
924 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
925 /* Search for private user controls that are compatible with
926 VIDIOC_G/S_CTRL. */
927 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
928 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
929 if (!type_is_int(ref->ctrl))
930 continue;
931 if (id == 0)
932 return ref;
933 id--;
934 }
935 }
936 return NULL;
937}
938
939/* Find a control with the given ID. */
940static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
941{
942 struct v4l2_ctrl_ref *ref;
943 int bucket;
944
945 id &= V4L2_CTRL_ID_MASK;
946
947 /* Old-style private controls need special handling */
948 if (id >= V4L2_CID_PRIVATE_BASE)
949 return find_private_ref(hdl, id);
950 bucket = id % hdl->nr_of_buckets;
951
952 /* Simple optimization: cache the last control found */
953 if (hdl->cached && hdl->cached->ctrl->id == id)
954 return hdl->cached;
955
956 /* Not in cache, search the hash */
957 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
958 while (ref && ref->ctrl->id != id)
959 ref = ref->next;
960
961 if (ref)
962 hdl->cached = ref; /* cache it! */
963 return ref;
964}
965
966/* Find a control with the given ID. Take the handler's lock first. */
967static struct v4l2_ctrl_ref *find_ref_lock(
968 struct v4l2_ctrl_handler *hdl, u32 id)
969{
970 struct v4l2_ctrl_ref *ref = NULL;
971
972 if (hdl) {
973 mutex_lock(&hdl->lock);
974 ref = find_ref(hdl, id);
975 mutex_unlock(&hdl->lock);
976 }
977 return ref;
978}
979
980/* Find a control with the given ID. */
981struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
982{
983 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
984
985 return ref ? ref->ctrl : NULL;
986}
987EXPORT_SYMBOL(v4l2_ctrl_find);
988
989/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
990static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
991 struct v4l2_ctrl *ctrl)
992{
993 struct v4l2_ctrl_ref *ref;
994 struct v4l2_ctrl_ref *new_ref;
995 u32 id = ctrl->id;
996 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
997 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
998
999 /* Automatically add the control class if it is not yet present. */
1000 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1001 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1002 return hdl->error;
1003
1004 if (hdl->error)
1005 return hdl->error;
1006
1007 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
1008 if (!new_ref)
1009 return handler_set_err(hdl, -ENOMEM);
1010 new_ref->ctrl = ctrl;
1011 if (ctrl->handler == hdl) {
1012 /* By default each control starts in a cluster of its own.
1013 new_ref->ctrl is basically a cluster array with one
1014 element, so that's perfect to use as the cluster pointer.
1015 But only do this for the handler that owns the control. */
1016 ctrl->cluster = &new_ref->ctrl;
1017 ctrl->ncontrols = 1;
1018 }
1019
1020 INIT_LIST_HEAD(&new_ref->node);
1021
1022 mutex_lock(&hdl->lock);
1023
1024 /* Add immediately at the end of the list if the list is empty, or if
1025 the last element in the list has a lower ID.
1026 This ensures that when elements are added in ascending order the
1027 insertion is an O(1) operation. */
1028 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1029 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1030 goto insert_in_hash;
1031 }
1032
1033 /* Find insert position in sorted list */
1034 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1035 if (ref->ctrl->id < id)
1036 continue;
1037 /* Don't add duplicates */
1038 if (ref->ctrl->id == id) {
1039 kfree(new_ref);
1040 goto unlock;
1041 }
1042 list_add(&new_ref->node, ref->node.prev);
1043 break;
1044 }
1045
1046insert_in_hash:
1047 /* Insert the control node in the hash */
1048 new_ref->next = hdl->buckets[bucket];
1049 hdl->buckets[bucket] = new_ref;
1050
1051unlock:
1052 mutex_unlock(&hdl->lock);
1053 return 0;
1054}
1055
1056/* Add a new control */
1057static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1058 const struct v4l2_ctrl_ops *ops,
1059 u32 id, const char *name, enum v4l2_ctrl_type type,
1060 s32 min, s32 max, u32 step, s32 def,
Hans Verkuil513521e2010-12-29 14:25:52 -03001061 u32 flags, const char * const *qmenu, void *priv)
Hans Verkuil09965172010-08-01 14:32:42 -03001062{
1063 struct v4l2_ctrl *ctrl;
1064 unsigned sz_extra = 0;
1065
1066 if (hdl->error)
1067 return NULL;
1068
1069 /* Sanity checks */
1070 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
Hans Verkuil09965172010-08-01 14:32:42 -03001071 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001072 (type == V4L2_CTRL_TYPE_BITMASK && max == 0) ||
Hans Verkuil09965172010-08-01 14:32:42 -03001073 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1074 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1075 handler_set_err(hdl, -ERANGE);
1076 return NULL;
1077 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001078 if (type != V4L2_CTRL_TYPE_BITMASK && max < min) {
1079 handler_set_err(hdl, -ERANGE);
1080 return NULL;
1081 }
Hans Verkuil02ac0482010-12-29 14:27:05 -03001082 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1083 type == V4L2_CTRL_TYPE_MENU ||
1084 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1085 (def < min || def > max)) {
1086 handler_set_err(hdl, -ERANGE);
1087 return NULL;
1088 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001089 if (type == V4L2_CTRL_TYPE_BITMASK && ((def & ~max) || min || step)) {
1090 handler_set_err(hdl, -ERANGE);
1091 return NULL;
1092 }
Hans Verkuil09965172010-08-01 14:32:42 -03001093
1094 if (type == V4L2_CTRL_TYPE_BUTTON)
1095 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1096 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1097 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1098 else if (type == V4L2_CTRL_TYPE_STRING)
1099 sz_extra += 2 * (max + 1);
1100
1101 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1102 if (ctrl == NULL) {
1103 handler_set_err(hdl, -ENOMEM);
1104 return NULL;
1105 }
1106
1107 INIT_LIST_HEAD(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -03001108 INIT_LIST_HEAD(&ctrl->ev_subs);
Hans Verkuil09965172010-08-01 14:32:42 -03001109 ctrl->handler = hdl;
1110 ctrl->ops = ops;
1111 ctrl->id = id;
1112 ctrl->name = name;
1113 ctrl->type = type;
1114 ctrl->flags = flags;
1115 ctrl->minimum = min;
1116 ctrl->maximum = max;
1117 ctrl->step = step;
1118 ctrl->qmenu = qmenu;
1119 ctrl->priv = priv;
1120 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1121
1122 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1123 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1124 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1125 if (ctrl->minimum)
1126 memset(ctrl->cur.string, ' ', ctrl->minimum);
1127 }
1128 if (handler_new_ref(hdl, ctrl)) {
1129 kfree(ctrl);
1130 return NULL;
1131 }
1132 mutex_lock(&hdl->lock);
1133 list_add_tail(&ctrl->node, &hdl->ctrls);
1134 mutex_unlock(&hdl->lock);
1135 return ctrl;
1136}
1137
1138struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1139 const struct v4l2_ctrl_config *cfg, void *priv)
1140{
1141 bool is_menu;
1142 struct v4l2_ctrl *ctrl;
1143 const char *name = cfg->name;
Hans Verkuil513521e2010-12-29 14:25:52 -03001144 const char * const *qmenu = cfg->qmenu;
Hans Verkuil09965172010-08-01 14:32:42 -03001145 enum v4l2_ctrl_type type = cfg->type;
1146 u32 flags = cfg->flags;
1147 s32 min = cfg->min;
1148 s32 max = cfg->max;
1149 u32 step = cfg->step;
1150 s32 def = cfg->def;
1151
1152 if (name == NULL)
1153 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1154 &def, &flags);
1155
1156 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1157 if (is_menu)
1158 WARN_ON(step);
1159 else
1160 WARN_ON(cfg->menu_skip_mask);
1161 if (is_menu && qmenu == NULL)
1162 qmenu = v4l2_ctrl_get_menu(cfg->id);
1163
1164 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1165 type, min, max,
1166 is_menu ? cfg->menu_skip_mask : step,
1167 def, flags, qmenu, priv);
1168 if (ctrl) {
1169 ctrl->is_private = cfg->is_private;
1170 ctrl->is_volatile = cfg->is_volatile;
1171 }
1172 return ctrl;
1173}
1174EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1175
1176/* Helper function for standard non-menu controls */
1177struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1178 const struct v4l2_ctrl_ops *ops,
1179 u32 id, s32 min, s32 max, u32 step, s32 def)
1180{
1181 const char *name;
1182 enum v4l2_ctrl_type type;
1183 u32 flags;
1184
1185 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1186 if (type == V4L2_CTRL_TYPE_MENU) {
1187 handler_set_err(hdl, -EINVAL);
1188 return NULL;
1189 }
1190 return v4l2_ctrl_new(hdl, ops, id, name, type,
1191 min, max, step, def, flags, NULL, NULL);
1192}
1193EXPORT_SYMBOL(v4l2_ctrl_new_std);
1194
1195/* Helper function for standard menu controls */
1196struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1197 const struct v4l2_ctrl_ops *ops,
1198 u32 id, s32 max, s32 mask, s32 def)
1199{
Hans Verkuil513521e2010-12-29 14:25:52 -03001200 const char * const *qmenu = v4l2_ctrl_get_menu(id);
Hans Verkuil09965172010-08-01 14:32:42 -03001201 const char *name;
1202 enum v4l2_ctrl_type type;
1203 s32 min;
1204 s32 step;
1205 u32 flags;
1206
1207 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1208 if (type != V4L2_CTRL_TYPE_MENU) {
1209 handler_set_err(hdl, -EINVAL);
1210 return NULL;
1211 }
1212 return v4l2_ctrl_new(hdl, ops, id, name, type,
1213 0, max, mask, def, flags, qmenu, NULL);
1214}
1215EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1216
1217/* Add a control from another handler to this handler */
1218struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1219 struct v4l2_ctrl *ctrl)
1220{
1221 if (hdl == NULL || hdl->error)
1222 return NULL;
1223 if (ctrl == NULL) {
1224 handler_set_err(hdl, -EINVAL);
1225 return NULL;
1226 }
1227 if (ctrl->handler == hdl)
1228 return ctrl;
1229 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1230}
1231EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1232
1233/* Add the controls from another handler to our own. */
1234int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1235 struct v4l2_ctrl_handler *add)
1236{
1237 struct v4l2_ctrl *ctrl;
1238 int ret = 0;
1239
1240 /* Do nothing if either handler is NULL or if they are the same */
1241 if (!hdl || !add || hdl == add)
1242 return 0;
1243 if (hdl->error)
1244 return hdl->error;
1245 mutex_lock(&add->lock);
1246 list_for_each_entry(ctrl, &add->ctrls, node) {
1247 /* Skip handler-private controls. */
1248 if (ctrl->is_private)
1249 continue;
Hans Verkuil6e239392011-06-07 11:13:44 -03001250 /* And control classes */
1251 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1252 continue;
Hans Verkuil09965172010-08-01 14:32:42 -03001253 ret = handler_new_ref(hdl, ctrl);
1254 if (ret)
1255 break;
1256 }
1257 mutex_unlock(&add->lock);
1258 return ret;
1259}
1260EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1261
1262/* Cluster controls */
1263void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1264{
1265 int i;
1266
1267 /* The first control is the master control and it must not be NULL */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001268 BUG_ON(ncontrols == 0 || controls[0] == NULL);
Hans Verkuil09965172010-08-01 14:32:42 -03001269
1270 for (i = 0; i < ncontrols; i++) {
1271 if (controls[i]) {
1272 controls[i]->cluster = controls;
1273 controls[i]->ncontrols = ncontrols;
1274 }
1275 }
1276}
1277EXPORT_SYMBOL(v4l2_ctrl_cluster);
1278
Hans Verkuil72d877c2011-06-10 05:44:36 -03001279void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1280 u8 manual_val, bool set_volatile)
1281{
1282 struct v4l2_ctrl *master = controls[0];
1283 u32 flag;
1284 int i;
1285
1286 v4l2_ctrl_cluster(ncontrols, controls);
1287 WARN_ON(ncontrols <= 1);
Hans Verkuil82a7c042011-06-28 10:43:13 -03001288 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001289 master->is_auto = true;
1290 master->manual_mode_value = manual_val;
1291 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1292 flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
1293
1294 for (i = 1; i < ncontrols; i++)
1295 if (controls[i]) {
1296 controls[i]->is_volatile = set_volatile;
1297 controls[i]->flags |= flag;
1298 }
1299}
1300EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1301
Hans Verkuil09965172010-08-01 14:32:42 -03001302/* Activate/deactivate a control. */
1303void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1304{
Hans Verkuil6e239392011-06-07 11:13:44 -03001305 /* invert since the actual flag is called 'inactive' */
1306 bool inactive = !active;
1307 bool old;
1308
Hans Verkuil09965172010-08-01 14:32:42 -03001309 if (ctrl == NULL)
1310 return;
1311
Hans Verkuil6e239392011-06-07 11:13:44 -03001312 if (inactive)
Hans Verkuil09965172010-08-01 14:32:42 -03001313 /* set V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001314 old = test_and_set_bit(4, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001315 else
1316 /* clear V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001317 old = test_and_clear_bit(4, &ctrl->flags);
1318 if (old != inactive)
1319 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
Hans Verkuil09965172010-08-01 14:32:42 -03001320}
1321EXPORT_SYMBOL(v4l2_ctrl_activate);
1322
1323/* Grab/ungrab a control.
1324 Typically used when streaming starts and you want to grab controls,
1325 preventing the user from changing them.
1326
1327 Just call this and the framework will block any attempts to change
1328 these controls. */
1329void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1330{
Hans Verkuil6e239392011-06-07 11:13:44 -03001331 bool old;
1332
Hans Verkuil09965172010-08-01 14:32:42 -03001333 if (ctrl == NULL)
1334 return;
1335
Hans Verkuil6e239392011-06-07 11:13:44 -03001336 v4l2_ctrl_lock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001337 if (grabbed)
1338 /* set V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001339 old = test_and_set_bit(1, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001340 else
1341 /* clear V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001342 old = test_and_clear_bit(1, &ctrl->flags);
1343 if (old != grabbed)
1344 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1345 v4l2_ctrl_unlock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001346}
1347EXPORT_SYMBOL(v4l2_ctrl_grab);
1348
1349/* Log the control name and value */
1350static void log_ctrl(const struct v4l2_ctrl *ctrl,
1351 const char *prefix, const char *colon)
1352{
1353 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1354 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1355
1356 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1357 return;
1358 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1359 return;
1360
1361 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1362
1363 switch (ctrl->type) {
1364 case V4L2_CTRL_TYPE_INTEGER:
1365 printk(KERN_CONT "%d", ctrl->cur.val);
1366 break;
1367 case V4L2_CTRL_TYPE_BOOLEAN:
1368 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1369 break;
1370 case V4L2_CTRL_TYPE_MENU:
1371 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1372 break;
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001373 case V4L2_CTRL_TYPE_BITMASK:
1374 printk(KERN_CONT "0x%08x", ctrl->cur.val);
1375 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001376 case V4L2_CTRL_TYPE_INTEGER64:
1377 printk(KERN_CONT "%lld", ctrl->cur.val64);
1378 break;
1379 case V4L2_CTRL_TYPE_STRING:
1380 printk(KERN_CONT "%s", ctrl->cur.string);
1381 break;
1382 default:
1383 printk(KERN_CONT "unknown type %d", ctrl->type);
1384 break;
1385 }
1386 if (fl_inact && fl_grabbed)
1387 printk(KERN_CONT " (inactive, grabbed)\n");
1388 else if (fl_inact)
1389 printk(KERN_CONT " (inactive)\n");
1390 else if (fl_grabbed)
1391 printk(KERN_CONT " (grabbed)\n");
1392 else
1393 printk(KERN_CONT "\n");
1394}
1395
1396/* Log all controls owned by the handler */
1397void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1398 const char *prefix)
1399{
1400 struct v4l2_ctrl *ctrl;
1401 const char *colon = "";
1402 int len;
1403
1404 if (hdl == NULL)
1405 return;
1406 if (prefix == NULL)
1407 prefix = "";
1408 len = strlen(prefix);
1409 if (len && prefix[len - 1] != ' ')
1410 colon = ": ";
1411 mutex_lock(&hdl->lock);
1412 list_for_each_entry(ctrl, &hdl->ctrls, node)
1413 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1414 log_ctrl(ctrl, prefix, colon);
1415 mutex_unlock(&hdl->lock);
1416}
1417EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1418
1419/* Call s_ctrl for all controls owned by the handler */
1420int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1421{
1422 struct v4l2_ctrl *ctrl;
1423 int ret = 0;
1424
1425 if (hdl == NULL)
1426 return 0;
1427 mutex_lock(&hdl->lock);
1428 list_for_each_entry(ctrl, &hdl->ctrls, node)
1429 ctrl->done = false;
1430
1431 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1432 struct v4l2_ctrl *master = ctrl->cluster[0];
1433 int i;
1434
1435 /* Skip if this control was already handled by a cluster. */
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001436 /* Skip button controls and read-only controls. */
1437 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1438 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
Hans Verkuil09965172010-08-01 14:32:42 -03001439 continue;
1440
Hans Verkuil2a863792011-01-11 14:45:03 -03001441 for (i = 0; i < master->ncontrols; i++) {
1442 if (master->cluster[i]) {
1443 cur_to_new(master->cluster[i]);
1444 master->cluster[i]->is_new = 1;
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001445 master->cluster[i]->done = true;
Hans Verkuil2a863792011-01-11 14:45:03 -03001446 }
1447 }
Hans Verkuil54c911e2011-05-25 06:04:58 -03001448 ret = call_op(master, s_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001449 if (ret)
1450 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001451 }
1452 mutex_unlock(&hdl->lock);
1453 return ret;
1454}
1455EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1456
1457/* Implement VIDIOC_QUERYCTRL */
1458int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1459{
1460 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1461 struct v4l2_ctrl_ref *ref;
1462 struct v4l2_ctrl *ctrl;
1463
1464 if (hdl == NULL)
1465 return -EINVAL;
1466
1467 mutex_lock(&hdl->lock);
1468
1469 /* Try to find it */
1470 ref = find_ref(hdl, id);
1471
1472 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1473 /* Find the next control with ID > qc->id */
1474
1475 /* Did we reach the end of the control list? */
1476 if (id >= node2id(hdl->ctrl_refs.prev)) {
1477 ref = NULL; /* Yes, so there is no next control */
1478 } else if (ref) {
1479 /* We found a control with the given ID, so just get
1480 the next one in the list. */
1481 ref = list_entry(ref->node.next, typeof(*ref), node);
1482 } else {
1483 /* No control with the given ID exists, so start
1484 searching for the next largest ID. We know there
1485 is one, otherwise the first 'if' above would have
1486 been true. */
1487 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1488 if (id < ref->ctrl->id)
1489 break;
1490 }
1491 }
1492 mutex_unlock(&hdl->lock);
1493 if (!ref)
1494 return -EINVAL;
1495
1496 ctrl = ref->ctrl;
1497 memset(qc, 0, sizeof(*qc));
Hans Verkuil829fb2d2011-01-16 11:21:40 -03001498 if (id >= V4L2_CID_PRIVATE_BASE)
1499 qc->id = id;
1500 else
1501 qc->id = ctrl->id;
Hans Verkuil09965172010-08-01 14:32:42 -03001502 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1503 qc->minimum = ctrl->minimum;
1504 qc->maximum = ctrl->maximum;
1505 qc->default_value = ctrl->default_value;
Laurent Pincharteac9aa02010-12-07 08:57:25 -03001506 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
Hans Verkuil09965172010-08-01 14:32:42 -03001507 qc->step = 1;
1508 else
1509 qc->step = ctrl->step;
1510 qc->flags = ctrl->flags;
1511 qc->type = ctrl->type;
1512 return 0;
1513}
1514EXPORT_SYMBOL(v4l2_queryctrl);
1515
1516int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1517{
Hans Verkuil87a0c942011-02-22 12:31:07 -03001518 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1519 return -EINVAL;
Hans Verkuil09965172010-08-01 14:32:42 -03001520 return v4l2_queryctrl(sd->ctrl_handler, qc);
1521}
1522EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1523
1524/* Implement VIDIOC_QUERYMENU */
1525int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1526{
1527 struct v4l2_ctrl *ctrl;
1528 u32 i = qm->index;
1529
1530 ctrl = v4l2_ctrl_find(hdl, qm->id);
1531 if (!ctrl)
1532 return -EINVAL;
1533
1534 qm->reserved = 0;
1535 /* Sanity checks */
1536 if (ctrl->qmenu == NULL ||
1537 i < ctrl->minimum || i > ctrl->maximum)
1538 return -EINVAL;
1539 /* Use mask to see if this menu item should be skipped */
1540 if (ctrl->menu_skip_mask & (1 << i))
1541 return -EINVAL;
1542 /* Empty menu items should also be skipped */
1543 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1544 return -EINVAL;
1545 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1546 return 0;
1547}
1548EXPORT_SYMBOL(v4l2_querymenu);
1549
1550int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1551{
1552 return v4l2_querymenu(sd->ctrl_handler, qm);
1553}
1554EXPORT_SYMBOL(v4l2_subdev_querymenu);
1555
1556
1557
1558/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1559
1560 It is not a fully atomic operation, just best-effort only. After all, if
1561 multiple controls have to be set through multiple i2c writes (for example)
1562 then some initial writes may succeed while others fail. Thus leaving the
1563 system in an inconsistent state. The question is how much effort you are
1564 willing to spend on trying to make something atomic that really isn't.
1565
1566 From the point of view of an application the main requirement is that
1567 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1568 error should be returned without actually affecting any controls.
1569
1570 If all the values are correct, then it is acceptable to just give up
1571 in case of low-level errors.
1572
1573 It is important though that the application can tell when only a partial
1574 configuration was done. The way we do that is through the error_idx field
1575 of struct v4l2_ext_controls: if that is equal to the count field then no
1576 controls were affected. Otherwise all controls before that index were
1577 successful in performing their 'get' or 'set' operation, the control at
1578 the given index failed, and you don't know what happened with the controls
1579 after the failed one. Since if they were part of a control cluster they
1580 could have been successfully processed (if a cluster member was encountered
1581 at index < error_idx), they could have failed (if a cluster member was at
1582 error_idx), or they may not have been processed yet (if the first cluster
1583 member appeared after error_idx).
1584
1585 It is all fairly theoretical, though. In practice all you can do is to
1586 bail out. If error_idx == count, then it is an application bug. If
1587 error_idx < count then it is only an application bug if the error code was
1588 EBUSY. That usually means that something started streaming just when you
1589 tried to set the controls. In all other cases it is a driver/hardware
1590 problem and all you can do is to retry or bail out.
1591
1592 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1593 never modifies controls the error_idx is just set to whatever control
1594 has an invalid value.
1595 */
1596
1597/* Prepare for the extended g/s/try functions.
1598 Find the controls in the control array and do some basic checks. */
1599static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1600 struct v4l2_ext_controls *cs,
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001601 struct v4l2_ctrl_helper *helpers)
Hans Verkuil09965172010-08-01 14:32:42 -03001602{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001603 struct v4l2_ctrl_helper *h;
1604 bool have_clusters = false;
Hans Verkuil09965172010-08-01 14:32:42 -03001605 u32 i;
1606
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001607 for (i = 0, h = helpers; i < cs->count; i++, h++) {
Hans Verkuil09965172010-08-01 14:32:42 -03001608 struct v4l2_ext_control *c = &cs->controls[i];
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001609 struct v4l2_ctrl_ref *ref;
Hans Verkuil09965172010-08-01 14:32:42 -03001610 struct v4l2_ctrl *ctrl;
1611 u32 id = c->id & V4L2_CTRL_ID_MASK;
1612
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001613 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001614
1615 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1616 return -EINVAL;
1617
1618 /* Old-style private controls are not allowed for
1619 extended controls */
1620 if (id >= V4L2_CID_PRIVATE_BASE)
1621 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001622 ref = find_ref_lock(hdl, id);
1623 if (ref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001624 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001625 ctrl = ref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001626 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1627 return -EINVAL;
1628
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001629 if (ctrl->cluster[0]->ncontrols > 1)
1630 have_clusters = true;
1631 if (ctrl->cluster[0] != ctrl)
1632 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
1633 /* Store the ref to the master control of the cluster */
1634 h->mref = ref;
1635 h->ctrl = ctrl;
1636 /* Initially set next to 0, meaning that there is no other
1637 control in this helper array belonging to the same
1638 cluster */
1639 h->next = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001640 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001641
1642 /* We are done if there were no controls that belong to a multi-
1643 control cluster. */
1644 if (!have_clusters)
1645 return 0;
1646
1647 /* The code below figures out in O(n) time which controls in the list
1648 belong to the same cluster. */
1649
1650 /* This has to be done with the handler lock taken. */
1651 mutex_lock(&hdl->lock);
1652
1653 /* First zero the helper field in the master control references */
1654 for (i = 0; i < cs->count; i++)
1655 helpers[i].mref->helper = 0;
1656 for (i = 0, h = helpers; i < cs->count; i++, h++) {
1657 struct v4l2_ctrl_ref *mref = h->mref;
1658
1659 /* If the mref->helper is set, then it points to an earlier
1660 helper that belongs to the same cluster. */
1661 if (mref->helper) {
1662 /* Set the next field of mref->helper to the current
1663 index: this means that that earlier helper now
1664 points to the next helper in the same cluster. */
1665 mref->helper->next = i;
1666 /* mref should be set only for the first helper in the
1667 cluster, clear the others. */
1668 h->mref = NULL;
1669 }
1670 /* Point the mref helper to the current helper struct. */
1671 mref->helper = h;
1672 }
1673 mutex_unlock(&hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001674 return 0;
1675}
1676
Hans Verkuil09965172010-08-01 14:32:42 -03001677/* Handles the corner case where cs->count == 0. It checks whether the
1678 specified control class exists. If that class ID is 0, then it checks
1679 whether there are any controls at all. */
1680static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1681{
1682 if (ctrl_class == 0)
1683 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1684 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1685}
1686
1687
1688
1689/* Get extended controls. Allocates the helpers array if needed. */
1690int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1691{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001692 struct v4l2_ctrl_helper helper[4];
1693 struct v4l2_ctrl_helper *helpers = helper;
Hans Verkuil09965172010-08-01 14:32:42 -03001694 int ret;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001695 int i, j;
Hans Verkuil09965172010-08-01 14:32:42 -03001696
1697 cs->error_idx = cs->count;
1698 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1699
1700 if (hdl == NULL)
1701 return -EINVAL;
1702
1703 if (cs->count == 0)
1704 return class_check(hdl, cs->ctrl_class);
1705
1706 if (cs->count > ARRAY_SIZE(helper)) {
1707 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1708 if (helpers == NULL)
1709 return -ENOMEM;
1710 }
1711
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001712 ret = prepare_ext_ctrls(hdl, cs, helpers);
1713 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001714
1715 for (i = 0; !ret && i < cs->count; i++)
1716 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1717 ret = -EACCES;
1718
1719 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001720 int (*ctrl_to_user)(struct v4l2_ext_control *c,
1721 struct v4l2_ctrl *ctrl) = cur_to_user;
1722 struct v4l2_ctrl *master;
Hans Verkuil09965172010-08-01 14:32:42 -03001723
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001724 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001725 continue;
1726
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001727 master = helpers[i].mref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001728 cs->error_idx = i;
1729
1730 v4l2_ctrl_lock(master);
Hans Verkuilddac5c12011-06-10 05:43:34 -03001731
1732 /* g_volatile_ctrl will update the new control values */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001733 if (has_op(master, g_volatile_ctrl) && !is_cur_manual(master)) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03001734 for (j = 0; j < master->ncontrols; j++)
1735 cur_to_new(master->cluster[j]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03001736 ret = call_op(master, g_volatile_ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001737 ctrl_to_user = new_to_user;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001738 }
1739 /* If OK, then copy the current (for non-volatile controls)
1740 or the new (for volatile controls) control values to the
1741 caller */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001742 if (!ret) {
1743 u32 idx = i;
1744
1745 do {
1746 ret = ctrl_to_user(cs->controls + idx,
1747 helpers[idx].ctrl);
1748 idx = helpers[idx].next;
1749 } while (!ret && idx);
1750 }
Hans Verkuil09965172010-08-01 14:32:42 -03001751 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001752 }
1753
1754 if (cs->count > ARRAY_SIZE(helper))
1755 kfree(helpers);
1756 return ret;
1757}
1758EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1759
1760int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1761{
1762 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1763}
1764EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1765
1766/* Helper function to get a single control */
1767static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1768{
1769 struct v4l2_ctrl *master = ctrl->cluster[0];
1770 int ret = 0;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001771 int i;
Hans Verkuil09965172010-08-01 14:32:42 -03001772
1773 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1774 return -EACCES;
1775
1776 v4l2_ctrl_lock(master);
1777 /* g_volatile_ctrl will update the current control values */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001778 if (ctrl->is_volatile && !is_cur_manual(master)) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03001779 for (i = 0; i < master->ncontrols; i++)
1780 cur_to_new(master->cluster[i]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03001781 ret = call_op(master, g_volatile_ctrl);
Hans Verkuilddac5c12011-06-10 05:43:34 -03001782 *val = ctrl->val;
1783 } else {
1784 *val = ctrl->cur.val;
1785 }
Hans Verkuil09965172010-08-01 14:32:42 -03001786 v4l2_ctrl_unlock(master);
1787 return ret;
1788}
1789
1790int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1791{
1792 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1793
1794 if (ctrl == NULL || !type_is_int(ctrl))
1795 return -EINVAL;
1796 return get_ctrl(ctrl, &control->value);
1797}
1798EXPORT_SYMBOL(v4l2_g_ctrl);
1799
1800int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1801{
1802 return v4l2_g_ctrl(sd->ctrl_handler, control);
1803}
1804EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1805
1806s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1807{
1808 s32 val = 0;
1809
1810 /* It's a driver bug if this happens. */
1811 WARN_ON(!type_is_int(ctrl));
1812 get_ctrl(ctrl, &val);
1813 return val;
1814}
1815EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1816
1817
1818/* Core function that calls try/s_ctrl and ensures that the new value is
1819 copied to the current value on a set.
1820 Must be called with ctrl->handler->lock held. */
Hans Verkuile6402582011-06-14 10:56:42 -03001821static int try_or_set_cluster(struct v4l2_fh *fh,
1822 struct v4l2_ctrl *master, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03001823{
Hans Verkuil72d877c2011-06-10 05:44:36 -03001824 bool update_flag;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001825 int ret;
Hans Verkuil09965172010-08-01 14:32:42 -03001826 int i;
1827
1828 /* Go through the cluster and either validate the new value or
1829 (if no new value was set), copy the current value to the new
1830 value, ensuring a consistent view for the control ops when
1831 called. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001832 for (i = 0; i < master->ncontrols; i++) {
Hans Verkuil09965172010-08-01 14:32:42 -03001833 struct v4l2_ctrl *ctrl = master->cluster[i];
1834
1835 if (ctrl == NULL)
1836 continue;
1837
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001838 if (!ctrl->is_new) {
1839 cur_to_new(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001840 continue;
1841 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001842 /* Check again: it may have changed since the
1843 previous check in try_or_set_ext_ctrls(). */
1844 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1845 return -EBUSY;
Hans Verkuil09965172010-08-01 14:32:42 -03001846 }
1847
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001848 ret = call_op(master, try_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001849
1850 /* Don't set if there is no change */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001851 if (ret || !set || !cluster_changed(master))
1852 return ret;
1853 ret = call_op(master, s_ctrl);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001854 if (ret)
1855 return ret;
1856
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001857 /* If OK, then make the new values permanent. */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001858 update_flag = is_cur_manual(master) != is_new_manual(master);
1859 for (i = 0; i < master->ncontrols; i++)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001860 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001861 return 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001862}
1863
Hans Verkuile6402582011-06-14 10:56:42 -03001864/* Validate controls. */
1865static int validate_ctrls(struct v4l2_ext_controls *cs,
1866 struct v4l2_ctrl_helper *helpers, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03001867{
Hans Verkuile6402582011-06-14 10:56:42 -03001868 unsigned i;
Hans Verkuil09965172010-08-01 14:32:42 -03001869 int ret = 0;
1870
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001871 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001872 for (i = 0; i < cs->count; i++) {
1873 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1874
Hans Verkuile6402582011-06-14 10:56:42 -03001875 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001876
1877 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1878 return -EACCES;
1879 /* This test is also done in try_set_control_cluster() which
1880 is called in atomic context, so that has the final say,
1881 but it makes sense to do an up-front check as well. Once
1882 an error occurs in try_set_control_cluster() some other
1883 controls may have been set already and we want to do a
1884 best-effort to avoid that. */
1885 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1886 return -EBUSY;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001887 ret = validate_new(ctrl, &cs->controls[i]);
1888 if (ret)
1889 return ret;
Hans Verkuil09965172010-08-01 14:32:42 -03001890 }
Hans Verkuile6402582011-06-14 10:56:42 -03001891 return 0;
1892}
Hans Verkuil09965172010-08-01 14:32:42 -03001893
Hans Verkuile6402582011-06-14 10:56:42 -03001894/* Try or try-and-set controls */
1895static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1896 struct v4l2_ext_controls *cs,
1897 bool set)
1898{
1899 struct v4l2_ctrl_helper helper[4];
1900 struct v4l2_ctrl_helper *helpers = helper;
1901 unsigned i, j;
1902 int ret;
1903
1904 cs->error_idx = cs->count;
1905 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1906
1907 if (hdl == NULL)
1908 return -EINVAL;
1909
1910 if (cs->count == 0)
1911 return class_check(hdl, cs->ctrl_class);
1912
1913 if (cs->count > ARRAY_SIZE(helper)) {
1914 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1915 if (!helpers)
1916 return -ENOMEM;
1917 }
1918 ret = prepare_ext_ctrls(hdl, cs, helpers);
1919 if (!ret)
1920 ret = validate_ctrls(cs, helpers, set);
1921 if (ret && set)
1922 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001923 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001924 struct v4l2_ctrl *master;
1925 u32 idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001926
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001927 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001928 continue;
1929
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001930 cs->error_idx = i;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001931 master = helpers[i].mref->ctrl;
1932 v4l2_ctrl_lock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001933
Hans Verkuil2a863792011-01-11 14:45:03 -03001934 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03001935 for (j = 0; j < master->ncontrols; j++)
1936 if (master->cluster[j])
Hans Verkuil2a863792011-01-11 14:45:03 -03001937 master->cluster[j]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001938
1939 /* Copy the new caller-supplied control values.
Hans Verkuil2a863792011-01-11 14:45:03 -03001940 user_to_new() sets 'is_new' to 1. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001941 do {
1942 ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
1943 idx = helpers[idx].next;
1944 } while (!ret && idx);
Hans Verkuil09965172010-08-01 14:32:42 -03001945
1946 if (!ret)
Hans Verkuile6402582011-06-14 10:56:42 -03001947 ret = try_or_set_cluster(fh, master, set);
Hans Verkuil09965172010-08-01 14:32:42 -03001948
1949 /* Copy the new values back to userspace. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001950 if (!ret) {
1951 idx = i;
1952 do {
Hans Verkuiladf41b92011-07-05 06:56:37 -03001953 ret = new_to_user(cs->controls + idx,
Hans Verkuile6402582011-06-14 10:56:42 -03001954 helpers[idx].ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001955 idx = helpers[idx].next;
1956 } while (!ret && idx);
1957 }
1958 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001959 }
Hans Verkuil09965172010-08-01 14:32:42 -03001960
Hans Verkuil09965172010-08-01 14:32:42 -03001961 if (cs->count > ARRAY_SIZE(helper))
1962 kfree(helpers);
1963 return ret;
1964}
1965
1966int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1967{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001968 return try_set_ext_ctrls(NULL, hdl, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03001969}
1970EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1971
Hans Verkuilab892ba2011-06-07 06:47:18 -03001972int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1973 struct v4l2_ext_controls *cs)
Hans Verkuil09965172010-08-01 14:32:42 -03001974{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001975 return try_set_ext_ctrls(fh, hdl, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03001976}
1977EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1978
1979int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1980{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001981 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03001982}
1983EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1984
1985int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1986{
Hans Verkuilab892ba2011-06-07 06:47:18 -03001987 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03001988}
1989EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1990
1991/* Helper function for VIDIOC_S_CTRL compatibility */
Hans Verkuilab892ba2011-06-07 06:47:18 -03001992static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
Hans Verkuil09965172010-08-01 14:32:42 -03001993{
1994 struct v4l2_ctrl *master = ctrl->cluster[0];
1995 int ret;
1996 int i;
1997
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001998 ret = validate_new_int(ctrl, val);
1999 if (ret)
2000 return ret;
2001
Hans Verkuil09965172010-08-01 14:32:42 -03002002 v4l2_ctrl_lock(ctrl);
2003
Hans Verkuil2a863792011-01-11 14:45:03 -03002004 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03002005 for (i = 0; i < master->ncontrols; i++)
2006 if (master->cluster[i])
Hans Verkuil2a863792011-01-11 14:45:03 -03002007 master->cluster[i]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002008
2009 ctrl->val = *val;
Hans Verkuil2a863792011-01-11 14:45:03 -03002010 ctrl->is_new = 1;
Hans Verkuile6402582011-06-14 10:56:42 -03002011 ret = try_or_set_cluster(fh, master, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002012 *val = ctrl->cur.val;
2013 v4l2_ctrl_unlock(ctrl);
2014 return ret;
2015}
2016
Hans Verkuilab892ba2011-06-07 06:47:18 -03002017int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2018 struct v4l2_control *control)
Hans Verkuil09965172010-08-01 14:32:42 -03002019{
2020 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2021
2022 if (ctrl == NULL || !type_is_int(ctrl))
2023 return -EINVAL;
2024
Hans Verkuil7ebbc392011-06-07 04:50:31 -03002025 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2026 return -EACCES;
2027
Hans Verkuilab892ba2011-06-07 06:47:18 -03002028 return set_ctrl(fh, ctrl, &control->value);
Hans Verkuil09965172010-08-01 14:32:42 -03002029}
2030EXPORT_SYMBOL(v4l2_s_ctrl);
2031
2032int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2033{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002034 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
Hans Verkuil09965172010-08-01 14:32:42 -03002035}
2036EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2037
2038int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2039{
2040 /* It's a driver bug if this happens. */
2041 WARN_ON(!type_is_int(ctrl));
Hans Verkuilab892ba2011-06-07 06:47:18 -03002042 return set_ctrl(NULL, ctrl, &val);
Hans Verkuil09965172010-08-01 14:32:42 -03002043}
2044EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
Hans Verkuil6e239392011-06-07 11:13:44 -03002045
Hans Verkuil77068d32011-06-13 18:55:58 -03002046void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
2047 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002048{
Hans Verkuil6e239392011-06-07 11:13:44 -03002049 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002050 list_add_tail(&sev->node, &ctrl->ev_subs);
Hans Verkuil6e239392011-06-07 11:13:44 -03002051 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
Hans Verkuil77068d32011-06-13 18:55:58 -03002052 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
Hans Verkuil6e239392011-06-07 11:13:44 -03002053 struct v4l2_event ev;
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002054 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
Hans Verkuil6e239392011-06-07 11:13:44 -03002055
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002056 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2057 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2058 fill_event(&ev, ctrl, changes);
Hans Verkuil77068d32011-06-13 18:55:58 -03002059 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -03002060 }
2061 v4l2_ctrl_unlock(ctrl);
2062}
Hans Verkuil77068d32011-06-13 18:55:58 -03002063EXPORT_SYMBOL(v4l2_ctrl_add_event);
Hans Verkuil6e239392011-06-07 11:13:44 -03002064
Hans Verkuil77068d32011-06-13 18:55:58 -03002065void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
2066 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002067{
Hans Verkuil6e239392011-06-07 11:13:44 -03002068 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002069 list_del(&sev->node);
Hans Verkuil6e239392011-06-07 11:13:44 -03002070 v4l2_ctrl_unlock(ctrl);
2071}
Hans Verkuil77068d32011-06-13 18:55:58 -03002072EXPORT_SYMBOL(v4l2_ctrl_del_event);