blob: 37c064829ea55f7622c9a76caf61dcdb85bb4446 [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 };
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300241 static const char * const flash_led_mode[] = {
242 "Off",
243 "Flash",
244 "Torch",
245 NULL,
246 };
247 static const char * const flash_strobe_source[] = {
248 "Software",
249 "External",
250 NULL,
251 };
Hans Verkuil09965172010-08-01 14:32:42 -0300252
253 switch (id) {
254 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
255 return mpeg_audio_sampling_freq;
256 case V4L2_CID_MPEG_AUDIO_ENCODING:
257 return mpeg_audio_encoding;
258 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
259 return mpeg_audio_l1_bitrate;
260 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
261 return mpeg_audio_l2_bitrate;
262 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
263 return mpeg_audio_l3_bitrate;
264 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
265 return mpeg_audio_ac3_bitrate;
266 case V4L2_CID_MPEG_AUDIO_MODE:
267 return mpeg_audio_mode;
268 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
269 return mpeg_audio_mode_extension;
270 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
271 return mpeg_audio_emphasis;
272 case V4L2_CID_MPEG_AUDIO_CRC:
273 return mpeg_audio_crc;
274 case V4L2_CID_MPEG_VIDEO_ENCODING:
275 return mpeg_video_encoding;
276 case V4L2_CID_MPEG_VIDEO_ASPECT:
277 return mpeg_video_aspect;
278 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
279 return mpeg_video_bitrate_mode;
280 case V4L2_CID_MPEG_STREAM_TYPE:
281 return mpeg_stream_type;
282 case V4L2_CID_MPEG_STREAM_VBI_FMT:
283 return mpeg_stream_vbi_fmt;
284 case V4L2_CID_POWER_LINE_FREQUENCY:
285 return camera_power_line_frequency;
286 case V4L2_CID_EXPOSURE_AUTO:
287 return camera_exposure_auto;
288 case V4L2_CID_COLORFX:
289 return colorfx;
290 case V4L2_CID_TUNE_PREEMPHASIS:
291 return tune_preemphasis;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300292 case V4L2_CID_FLASH_LED_MODE:
293 return flash_led_mode;
294 case V4L2_CID_FLASH_STROBE_SOURCE:
295 return flash_strobe_source;
Hans Verkuil09965172010-08-01 14:32:42 -0300296 default:
297 return NULL;
298 }
299}
300EXPORT_SYMBOL(v4l2_ctrl_get_menu);
301
302/* Return the control name. */
303const char *v4l2_ctrl_get_name(u32 id)
304{
305 switch (id) {
306 /* USER controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300307 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300308 case V4L2_CID_USER_CLASS: return "User Controls";
309 case V4L2_CID_BRIGHTNESS: return "Brightness";
310 case V4L2_CID_CONTRAST: return "Contrast";
311 case V4L2_CID_SATURATION: return "Saturation";
312 case V4L2_CID_HUE: return "Hue";
313 case V4L2_CID_AUDIO_VOLUME: return "Volume";
314 case V4L2_CID_AUDIO_BALANCE: return "Balance";
315 case V4L2_CID_AUDIO_BASS: return "Bass";
316 case V4L2_CID_AUDIO_TREBLE: return "Treble";
317 case V4L2_CID_AUDIO_MUTE: return "Mute";
318 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
Hans Verkuil09965172010-08-01 14:32:42 -0300319 case V4L2_CID_BLACK_LEVEL: return "Black Level";
320 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
321 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
322 case V4L2_CID_RED_BALANCE: return "Red Balance";
323 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
324 case V4L2_CID_GAMMA: return "Gamma";
325 case V4L2_CID_EXPOSURE: return "Exposure";
326 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
327 case V4L2_CID_GAIN: return "Gain";
328 case V4L2_CID_HFLIP: return "Horizontal Flip";
329 case V4L2_CID_VFLIP: return "Vertical Flip";
330 case V4L2_CID_HCENTER: return "Horizontal Center";
331 case V4L2_CID_VCENTER: return "Vertical Center";
332 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
333 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
334 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
335 case V4L2_CID_SHARPNESS: return "Sharpness";
336 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
337 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
Hans Verkuil09965172010-08-01 14:32:42 -0300338 case V4L2_CID_COLOR_KILLER: return "Color Killer";
339 case V4L2_CID_COLORFX: return "Color Effects";
340 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
341 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
342 case V4L2_CID_ROTATE: return "Rotate";
343 case V4L2_CID_BG_COLOR: return "Background Color";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300344 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
Jean-François Moine008d35f2010-09-13 07:04:49 -0300345 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
346 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
Hans Verkuil09965172010-08-01 14:32:42 -0300347
348 /* MPEG controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300349 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300350 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
351 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
352 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
353 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
354 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
355 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300356 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
357 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
358 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
Hans Verkuil09965172010-08-01 14:32:42 -0300359 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300360 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
361 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
362 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
363 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
364 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
Hans Verkuil09965172010-08-01 14:32:42 -0300365 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300366 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
367 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
368 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
369 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
370 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
371 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
372 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
373 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
374 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
375 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
376 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
377 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
378 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
379 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
Hans Verkuil09965172010-08-01 14:32:42 -0300380 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300381 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
Hans Verkuil09965172010-08-01 14:32:42 -0300382 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
Hans Verkuil09965172010-08-01 14:32:42 -0300383
384 /* CAMERA controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300385 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300386 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
387 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
388 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
389 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
390 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
391 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
392 case V4L2_CID_PAN_RESET: return "Pan, Reset";
393 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
394 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
395 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
396 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
397 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
398 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
Hans Verkuil09965172010-08-01 14:32:42 -0300399 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
400 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
401 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
402 case V4L2_CID_PRIVACY: return "Privacy";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300403 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
404 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
Hans Verkuil09965172010-08-01 14:32:42 -0300405
406 /* FM Radio Modulator control */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300407 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300408 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
409 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
410 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
411 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
412 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
413 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
414 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
415 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
416 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
417 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
418 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
419 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
420 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
421 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
422 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
423 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
424 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
425 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
426 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
427 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
428
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300429 /* Flash controls */
430 case V4L2_CID_FLASH_CLASS: return "Flash controls";
431 case V4L2_CID_FLASH_LED_MODE: return "LED mode";
432 case V4L2_CID_FLASH_STROBE_SOURCE: return "Strobe source";
433 case V4L2_CID_FLASH_STROBE: return "Strobe";
434 case V4L2_CID_FLASH_STROBE_STOP: return "Stop strobe";
435 case V4L2_CID_FLASH_STROBE_STATUS: return "Strobe status";
436 case V4L2_CID_FLASH_TIMEOUT: return "Strobe timeout";
437 case V4L2_CID_FLASH_INTENSITY: return "Intensity, flash mode";
438 case V4L2_CID_FLASH_TORCH_INTENSITY: return "Intensity, torch mode";
439 case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, indicator";
440 case V4L2_CID_FLASH_FAULT: return "Faults";
441 case V4L2_CID_FLASH_CHARGE: return "Charge";
442 case V4L2_CID_FLASH_READY: return "Ready to strobe";
443
Hans Verkuil09965172010-08-01 14:32:42 -0300444 default:
445 return NULL;
446 }
447}
448EXPORT_SYMBOL(v4l2_ctrl_get_name);
449
450void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
451 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
452{
453 *name = v4l2_ctrl_get_name(id);
454 *flags = 0;
455
456 switch (id) {
457 case V4L2_CID_AUDIO_MUTE:
458 case V4L2_CID_AUDIO_LOUDNESS:
459 case V4L2_CID_AUTO_WHITE_BALANCE:
460 case V4L2_CID_AUTOGAIN:
461 case V4L2_CID_HFLIP:
462 case V4L2_CID_VFLIP:
463 case V4L2_CID_HUE_AUTO:
464 case V4L2_CID_CHROMA_AGC:
465 case V4L2_CID_COLOR_KILLER:
466 case V4L2_CID_MPEG_AUDIO_MUTE:
467 case V4L2_CID_MPEG_VIDEO_MUTE:
468 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
469 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
470 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
471 case V4L2_CID_FOCUS_AUTO:
472 case V4L2_CID_PRIVACY:
473 case V4L2_CID_AUDIO_LIMITER_ENABLED:
474 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
475 case V4L2_CID_PILOT_TONE_ENABLED:
Jean-François Moine008d35f2010-09-13 07:04:49 -0300476 case V4L2_CID_ILLUMINATORS_1:
477 case V4L2_CID_ILLUMINATORS_2:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300478 case V4L2_CID_FLASH_STROBE_STATUS:
479 case V4L2_CID_FLASH_CHARGE:
480 case V4L2_CID_FLASH_READY:
Hans Verkuil09965172010-08-01 14:32:42 -0300481 *type = V4L2_CTRL_TYPE_BOOLEAN;
482 *min = 0;
483 *max = *step = 1;
484 break;
485 case V4L2_CID_PAN_RESET:
486 case V4L2_CID_TILT_RESET:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300487 case V4L2_CID_FLASH_STROBE:
488 case V4L2_CID_FLASH_STROBE_STOP:
Hans Verkuil09965172010-08-01 14:32:42 -0300489 *type = V4L2_CTRL_TYPE_BUTTON;
490 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
491 *min = *max = *step = *def = 0;
492 break;
493 case V4L2_CID_POWER_LINE_FREQUENCY:
494 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
495 case V4L2_CID_MPEG_AUDIO_ENCODING:
496 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
497 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
498 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
499 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
500 case V4L2_CID_MPEG_AUDIO_MODE:
501 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
502 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
503 case V4L2_CID_MPEG_AUDIO_CRC:
504 case V4L2_CID_MPEG_VIDEO_ENCODING:
505 case V4L2_CID_MPEG_VIDEO_ASPECT:
506 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
507 case V4L2_CID_MPEG_STREAM_TYPE:
508 case V4L2_CID_MPEG_STREAM_VBI_FMT:
509 case V4L2_CID_EXPOSURE_AUTO:
510 case V4L2_CID_COLORFX:
511 case V4L2_CID_TUNE_PREEMPHASIS:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300512 case V4L2_CID_FLASH_LED_MODE:
513 case V4L2_CID_FLASH_STROBE_SOURCE:
Hans Verkuil09965172010-08-01 14:32:42 -0300514 *type = V4L2_CTRL_TYPE_MENU;
515 break;
516 case V4L2_CID_RDS_TX_PS_NAME:
517 case V4L2_CID_RDS_TX_RADIO_TEXT:
518 *type = V4L2_CTRL_TYPE_STRING;
519 break;
520 case V4L2_CID_USER_CLASS:
521 case V4L2_CID_CAMERA_CLASS:
522 case V4L2_CID_MPEG_CLASS:
523 case V4L2_CID_FM_TX_CLASS:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300524 case V4L2_CID_FLASH_CLASS:
Hans Verkuil09965172010-08-01 14:32:42 -0300525 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
526 /* You can neither read not write these */
527 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
528 *min = *max = *step = *def = 0;
529 break;
530 case V4L2_CID_BG_COLOR:
531 *type = V4L2_CTRL_TYPE_INTEGER;
532 *step = 1;
533 *min = 0;
534 /* Max is calculated as RGB888 that is 2^24 */
535 *max = 0xFFFFFF;
536 break;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300537 case V4L2_CID_FLASH_FAULT:
538 *type = V4L2_CTRL_TYPE_BITMASK;
539 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300540 default:
541 *type = V4L2_CTRL_TYPE_INTEGER;
542 break;
543 }
544 switch (id) {
545 case V4L2_CID_MPEG_AUDIO_ENCODING:
546 case V4L2_CID_MPEG_AUDIO_MODE:
547 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
548 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
549 case V4L2_CID_MPEG_STREAM_TYPE:
550 *flags |= V4L2_CTRL_FLAG_UPDATE;
551 break;
552 case V4L2_CID_AUDIO_VOLUME:
553 case V4L2_CID_AUDIO_BALANCE:
554 case V4L2_CID_AUDIO_BASS:
555 case V4L2_CID_AUDIO_TREBLE:
556 case V4L2_CID_BRIGHTNESS:
557 case V4L2_CID_CONTRAST:
558 case V4L2_CID_SATURATION:
559 case V4L2_CID_HUE:
560 case V4L2_CID_RED_BALANCE:
561 case V4L2_CID_BLUE_BALANCE:
562 case V4L2_CID_GAMMA:
563 case V4L2_CID_SHARPNESS:
564 case V4L2_CID_CHROMA_GAIN:
565 case V4L2_CID_RDS_TX_DEVIATION:
566 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
567 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
568 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
569 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
570 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
571 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
572 case V4L2_CID_PILOT_TONE_DEVIATION:
573 case V4L2_CID_PILOT_TONE_FREQUENCY:
574 case V4L2_CID_TUNE_POWER_LEVEL:
575 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
576 *flags |= V4L2_CTRL_FLAG_SLIDER;
577 break;
578 case V4L2_CID_PAN_RELATIVE:
579 case V4L2_CID_TILT_RELATIVE:
580 case V4L2_CID_FOCUS_RELATIVE:
581 case V4L2_CID_IRIS_RELATIVE:
582 case V4L2_CID_ZOOM_RELATIVE:
583 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
584 break;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300585 case V4L2_CID_FLASH_STROBE_STATUS:
586 case V4L2_CID_FLASH_READY:
587 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
588 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300589 }
590}
591EXPORT_SYMBOL(v4l2_ctrl_fill);
592
593/* Helper function to determine whether the control type is compatible with
594 VIDIOC_G/S_CTRL. */
595static bool type_is_int(const struct v4l2_ctrl *ctrl)
596{
597 switch (ctrl->type) {
598 case V4L2_CTRL_TYPE_INTEGER64:
599 case V4L2_CTRL_TYPE_STRING:
600 /* Nope, these need v4l2_ext_control */
601 return false;
602 default:
603 return true;
604 }
605}
606
Hans Verkuil6e239392011-06-07 11:13:44 -0300607static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
608{
609 memset(ev->reserved, 0, sizeof(ev->reserved));
610 ev->type = V4L2_EVENT_CTRL;
611 ev->id = ctrl->id;
612 ev->u.ctrl.changes = changes;
613 ev->u.ctrl.type = ctrl->type;
614 ev->u.ctrl.flags = ctrl->flags;
615 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
616 ev->u.ctrl.value64 = 0;
617 else
618 ev->u.ctrl.value64 = ctrl->cur.val64;
619 ev->u.ctrl.minimum = ctrl->minimum;
620 ev->u.ctrl.maximum = ctrl->maximum;
621 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
622 ev->u.ctrl.step = 1;
623 else
624 ev->u.ctrl.step = ctrl->step;
625 ev->u.ctrl.default_value = ctrl->default_value;
626}
627
628static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
629{
630 struct v4l2_event ev;
Hans Verkuil77068d32011-06-13 18:55:58 -0300631 struct v4l2_subscribed_event *sev;
Hans Verkuil6e239392011-06-07 11:13:44 -0300632
Hans Verkuil77068d32011-06-13 18:55:58 -0300633 if (list_empty(&ctrl->ev_subs))
Hans Verkuil3f66f0e2011-06-20 11:56:24 -0300634 return;
Hans Verkuil6e239392011-06-07 11:13:44 -0300635 fill_event(&ev, ctrl, changes);
636
Hans Verkuil77068d32011-06-13 18:55:58 -0300637 list_for_each_entry(sev, &ctrl->ev_subs, node)
Hans Verkuil60c07322011-06-29 08:56:22 -0300638 if (sev->fh && (sev->fh != fh ||
639 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK)))
Hans Verkuil77068d32011-06-13 18:55:58 -0300640 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -0300641}
642
Hans Verkuil09965172010-08-01 14:32:42 -0300643/* Helper function: copy the current control value back to the caller */
644static int cur_to_user(struct v4l2_ext_control *c,
645 struct v4l2_ctrl *ctrl)
646{
647 u32 len;
648
649 switch (ctrl->type) {
650 case V4L2_CTRL_TYPE_STRING:
651 len = strlen(ctrl->cur.string);
652 if (c->size < len + 1) {
653 c->size = len + 1;
654 return -ENOSPC;
655 }
656 return copy_to_user(c->string, ctrl->cur.string,
657 len + 1) ? -EFAULT : 0;
658 case V4L2_CTRL_TYPE_INTEGER64:
659 c->value64 = ctrl->cur.val64;
660 break;
661 default:
662 c->value = ctrl->cur.val;
663 break;
664 }
665 return 0;
666}
667
668/* Helper function: copy the caller-provider value as the new control value */
669static int user_to_new(struct v4l2_ext_control *c,
670 struct v4l2_ctrl *ctrl)
671{
672 int ret;
673 u32 size;
674
Hans Verkuil2a863792011-01-11 14:45:03 -0300675 ctrl->is_new = 1;
Hans Verkuil09965172010-08-01 14:32:42 -0300676 switch (ctrl->type) {
677 case V4L2_CTRL_TYPE_INTEGER64:
678 ctrl->val64 = c->value64;
679 break;
680 case V4L2_CTRL_TYPE_STRING:
681 size = c->size;
682 if (size == 0)
683 return -ERANGE;
684 if (size > ctrl->maximum + 1)
685 size = ctrl->maximum + 1;
686 ret = copy_from_user(ctrl->string, c->string, size);
687 if (!ret) {
688 char last = ctrl->string[size - 1];
689
690 ctrl->string[size - 1] = 0;
691 /* If the string was longer than ctrl->maximum,
692 then return an error. */
693 if (strlen(ctrl->string) == ctrl->maximum && last)
694 return -ERANGE;
695 }
696 return ret ? -EFAULT : 0;
697 default:
698 ctrl->val = c->value;
699 break;
700 }
701 return 0;
702}
703
704/* Helper function: copy the new control value back to the caller */
705static int new_to_user(struct v4l2_ext_control *c,
706 struct v4l2_ctrl *ctrl)
707{
708 u32 len;
709
710 switch (ctrl->type) {
711 case V4L2_CTRL_TYPE_STRING:
712 len = strlen(ctrl->string);
713 if (c->size < len + 1) {
714 c->size = ctrl->maximum + 1;
715 return -ENOSPC;
716 }
717 return copy_to_user(c->string, ctrl->string,
718 len + 1) ? -EFAULT : 0;
719 case V4L2_CTRL_TYPE_INTEGER64:
720 c->value64 = ctrl->val64;
721 break;
722 default:
723 c->value = ctrl->val;
724 break;
725 }
726 return 0;
727}
728
729/* Copy the new value to the current value. */
Hans Verkuilab892ba2011-06-07 06:47:18 -0300730static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
731 bool update_inactive)
Hans Verkuil09965172010-08-01 14:32:42 -0300732{
Hans Verkuil6e239392011-06-07 11:13:44 -0300733 bool changed = false;
734
Hans Verkuil09965172010-08-01 14:32:42 -0300735 if (ctrl == NULL)
736 return;
737 switch (ctrl->type) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300738 case V4L2_CTRL_TYPE_BUTTON:
739 changed = true;
740 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300741 case V4L2_CTRL_TYPE_STRING:
742 /* strings are always 0-terminated */
Hans Verkuil6e239392011-06-07 11:13:44 -0300743 changed = strcmp(ctrl->string, ctrl->cur.string);
Hans Verkuil09965172010-08-01 14:32:42 -0300744 strcpy(ctrl->cur.string, ctrl->string);
745 break;
746 case V4L2_CTRL_TYPE_INTEGER64:
Hans Verkuil6e239392011-06-07 11:13:44 -0300747 changed = ctrl->val64 != ctrl->cur.val64;
Hans Verkuil09965172010-08-01 14:32:42 -0300748 ctrl->cur.val64 = ctrl->val64;
749 break;
750 default:
Hans Verkuil6e239392011-06-07 11:13:44 -0300751 changed = ctrl->val != ctrl->cur.val;
Hans Verkuil09965172010-08-01 14:32:42 -0300752 ctrl->cur.val = ctrl->val;
753 break;
754 }
Hans Verkuil72d877c2011-06-10 05:44:36 -0300755 if (update_inactive) {
756 ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
757 if (!is_cur_manual(ctrl->cluster[0]))
758 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
759 }
Hans Verkuil639884a2011-07-05 07:09:26 -0300760 if (changed || update_inactive) {
761 /* If a control was changed that was not one of the controls
762 modified by the application, then send the event to all. */
763 if (!ctrl->is_new)
764 fh = NULL;
Hans Verkuil6e239392011-06-07 11:13:44 -0300765 send_event(fh, ctrl,
766 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
767 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
Hans Verkuil639884a2011-07-05 07:09:26 -0300768 }
Hans Verkuil09965172010-08-01 14:32:42 -0300769}
770
771/* Copy the current value to the new value */
772static void cur_to_new(struct v4l2_ctrl *ctrl)
773{
774 if (ctrl == NULL)
775 return;
776 switch (ctrl->type) {
777 case V4L2_CTRL_TYPE_STRING:
778 /* strings are always 0-terminated */
779 strcpy(ctrl->string, ctrl->cur.string);
780 break;
781 case V4L2_CTRL_TYPE_INTEGER64:
782 ctrl->val64 = ctrl->cur.val64;
783 break;
784 default:
785 ctrl->val = ctrl->cur.val;
786 break;
787 }
788}
789
790/* Return non-zero if one or more of the controls in the cluster has a new
791 value that differs from the current value. */
792static int cluster_changed(struct v4l2_ctrl *master)
793{
794 int diff = 0;
795 int i;
796
797 for (i = 0; !diff && i < master->ncontrols; i++) {
798 struct v4l2_ctrl *ctrl = master->cluster[i];
799
800 if (ctrl == NULL)
801 continue;
802 switch (ctrl->type) {
803 case V4L2_CTRL_TYPE_BUTTON:
804 /* Button controls are always 'different' */
805 return 1;
806 case V4L2_CTRL_TYPE_STRING:
807 /* strings are always 0-terminated */
808 diff = strcmp(ctrl->string, ctrl->cur.string);
809 break;
810 case V4L2_CTRL_TYPE_INTEGER64:
811 diff = ctrl->val64 != ctrl->cur.val64;
812 break;
813 default:
814 diff = ctrl->val != ctrl->cur.val;
815 break;
816 }
817 }
818 return diff;
819}
820
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300821/* Validate integer-type control */
822static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
Hans Verkuil09965172010-08-01 14:32:42 -0300823{
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300824 s32 val = *pval;
Hans Verkuil09965172010-08-01 14:32:42 -0300825 u32 offset;
Hans Verkuil09965172010-08-01 14:32:42 -0300826
827 switch (ctrl->type) {
828 case V4L2_CTRL_TYPE_INTEGER:
829 /* Round towards the closest legal value */
830 val += ctrl->step / 2;
831 if (val < ctrl->minimum)
832 val = ctrl->minimum;
833 if (val > ctrl->maximum)
834 val = ctrl->maximum;
835 offset = val - ctrl->minimum;
836 offset = ctrl->step * (offset / ctrl->step);
837 val = ctrl->minimum + offset;
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300838 *pval = val;
Hans Verkuil09965172010-08-01 14:32:42 -0300839 return 0;
840
841 case V4L2_CTRL_TYPE_BOOLEAN:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300842 *pval = !!val;
Hans Verkuil09965172010-08-01 14:32:42 -0300843 return 0;
844
845 case V4L2_CTRL_TYPE_MENU:
846 if (val < ctrl->minimum || val > ctrl->maximum)
847 return -ERANGE;
848 if (ctrl->qmenu[val][0] == '\0' ||
849 (ctrl->menu_skip_mask & (1 << val)))
850 return -EINVAL;
851 return 0;
852
Hans Verkuilfa4d7092011-05-23 04:07:05 -0300853 case V4L2_CTRL_TYPE_BITMASK:
854 *pval &= ctrl->maximum;
855 return 0;
856
Hans Verkuil09965172010-08-01 14:32:42 -0300857 case V4L2_CTRL_TYPE_BUTTON:
858 case V4L2_CTRL_TYPE_CTRL_CLASS:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300859 *pval = 0;
Hans Verkuil09965172010-08-01 14:32:42 -0300860 return 0;
861
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300862 default:
863 return -EINVAL;
864 }
865}
866
867/* Validate a new control */
868static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
869{
870 char *s = c->string;
871 size_t len;
872
873 switch (ctrl->type) {
874 case V4L2_CTRL_TYPE_INTEGER:
875 case V4L2_CTRL_TYPE_BOOLEAN:
876 case V4L2_CTRL_TYPE_MENU:
Hans Verkuilfa4d7092011-05-23 04:07:05 -0300877 case V4L2_CTRL_TYPE_BITMASK:
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300878 case V4L2_CTRL_TYPE_BUTTON:
879 case V4L2_CTRL_TYPE_CTRL_CLASS:
880 return validate_new_int(ctrl, &c->value);
881
Hans Verkuil09965172010-08-01 14:32:42 -0300882 case V4L2_CTRL_TYPE_INTEGER64:
883 return 0;
884
885 case V4L2_CTRL_TYPE_STRING:
886 len = strlen(s);
887 if (len < ctrl->minimum)
888 return -ERANGE;
889 if ((len - ctrl->minimum) % ctrl->step)
890 return -ERANGE;
891 return 0;
892
893 default:
894 return -EINVAL;
895 }
896}
897
898static inline u32 node2id(struct list_head *node)
899{
900 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
901}
902
903/* Set the handler's error code if it wasn't set earlier already */
904static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
905{
906 if (hdl->error == 0)
907 hdl->error = err;
908 return err;
909}
910
911/* Initialize the handler */
912int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
913 unsigned nr_of_controls_hint)
914{
915 mutex_init(&hdl->lock);
916 INIT_LIST_HEAD(&hdl->ctrls);
917 INIT_LIST_HEAD(&hdl->ctrl_refs);
918 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
919 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
920 GFP_KERNEL);
921 hdl->error = hdl->buckets ? 0 : -ENOMEM;
922 return hdl->error;
923}
924EXPORT_SYMBOL(v4l2_ctrl_handler_init);
925
926/* Free all controls and control refs */
927void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
928{
929 struct v4l2_ctrl_ref *ref, *next_ref;
930 struct v4l2_ctrl *ctrl, *next_ctrl;
Hans Verkuil77068d32011-06-13 18:55:58 -0300931 struct v4l2_subscribed_event *sev, *next_sev;
Hans Verkuil09965172010-08-01 14:32:42 -0300932
933 if (hdl == NULL || hdl->buckets == NULL)
934 return;
935
936 mutex_lock(&hdl->lock);
937 /* Free all nodes */
938 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
939 list_del(&ref->node);
940 kfree(ref);
941 }
942 /* Free all controls owned by the handler */
943 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
944 list_del(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -0300945 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
946 list_del(&sev->node);
Hans Verkuil09965172010-08-01 14:32:42 -0300947 kfree(ctrl);
948 }
949 kfree(hdl->buckets);
950 hdl->buckets = NULL;
951 hdl->cached = NULL;
952 hdl->error = 0;
953 mutex_unlock(&hdl->lock);
954}
955EXPORT_SYMBOL(v4l2_ctrl_handler_free);
956
957/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
958 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
959 with applications that do not use the NEXT_CTRL flag.
960
961 We just find the n-th private user control. It's O(N), but that should not
962 be an issue in this particular case. */
963static struct v4l2_ctrl_ref *find_private_ref(
964 struct v4l2_ctrl_handler *hdl, u32 id)
965{
966 struct v4l2_ctrl_ref *ref;
967
968 id -= V4L2_CID_PRIVATE_BASE;
969 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
970 /* Search for private user controls that are compatible with
971 VIDIOC_G/S_CTRL. */
972 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
973 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
974 if (!type_is_int(ref->ctrl))
975 continue;
976 if (id == 0)
977 return ref;
978 id--;
979 }
980 }
981 return NULL;
982}
983
984/* Find a control with the given ID. */
985static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
986{
987 struct v4l2_ctrl_ref *ref;
988 int bucket;
989
990 id &= V4L2_CTRL_ID_MASK;
991
992 /* Old-style private controls need special handling */
993 if (id >= V4L2_CID_PRIVATE_BASE)
994 return find_private_ref(hdl, id);
995 bucket = id % hdl->nr_of_buckets;
996
997 /* Simple optimization: cache the last control found */
998 if (hdl->cached && hdl->cached->ctrl->id == id)
999 return hdl->cached;
1000
1001 /* Not in cache, search the hash */
1002 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1003 while (ref && ref->ctrl->id != id)
1004 ref = ref->next;
1005
1006 if (ref)
1007 hdl->cached = ref; /* cache it! */
1008 return ref;
1009}
1010
1011/* Find a control with the given ID. Take the handler's lock first. */
1012static struct v4l2_ctrl_ref *find_ref_lock(
1013 struct v4l2_ctrl_handler *hdl, u32 id)
1014{
1015 struct v4l2_ctrl_ref *ref = NULL;
1016
1017 if (hdl) {
1018 mutex_lock(&hdl->lock);
1019 ref = find_ref(hdl, id);
1020 mutex_unlock(&hdl->lock);
1021 }
1022 return ref;
1023}
1024
1025/* Find a control with the given ID. */
1026struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1027{
1028 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1029
1030 return ref ? ref->ctrl : NULL;
1031}
1032EXPORT_SYMBOL(v4l2_ctrl_find);
1033
1034/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1035static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1036 struct v4l2_ctrl *ctrl)
1037{
1038 struct v4l2_ctrl_ref *ref;
1039 struct v4l2_ctrl_ref *new_ref;
1040 u32 id = ctrl->id;
1041 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
1042 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
1043
1044 /* Automatically add the control class if it is not yet present. */
1045 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1046 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1047 return hdl->error;
1048
1049 if (hdl->error)
1050 return hdl->error;
1051
1052 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
1053 if (!new_ref)
1054 return handler_set_err(hdl, -ENOMEM);
1055 new_ref->ctrl = ctrl;
1056 if (ctrl->handler == hdl) {
1057 /* By default each control starts in a cluster of its own.
1058 new_ref->ctrl is basically a cluster array with one
1059 element, so that's perfect to use as the cluster pointer.
1060 But only do this for the handler that owns the control. */
1061 ctrl->cluster = &new_ref->ctrl;
1062 ctrl->ncontrols = 1;
1063 }
1064
1065 INIT_LIST_HEAD(&new_ref->node);
1066
1067 mutex_lock(&hdl->lock);
1068
1069 /* Add immediately at the end of the list if the list is empty, or if
1070 the last element in the list has a lower ID.
1071 This ensures that when elements are added in ascending order the
1072 insertion is an O(1) operation. */
1073 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1074 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1075 goto insert_in_hash;
1076 }
1077
1078 /* Find insert position in sorted list */
1079 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1080 if (ref->ctrl->id < id)
1081 continue;
1082 /* Don't add duplicates */
1083 if (ref->ctrl->id == id) {
1084 kfree(new_ref);
1085 goto unlock;
1086 }
1087 list_add(&new_ref->node, ref->node.prev);
1088 break;
1089 }
1090
1091insert_in_hash:
1092 /* Insert the control node in the hash */
1093 new_ref->next = hdl->buckets[bucket];
1094 hdl->buckets[bucket] = new_ref;
1095
1096unlock:
1097 mutex_unlock(&hdl->lock);
1098 return 0;
1099}
1100
1101/* Add a new control */
1102static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1103 const struct v4l2_ctrl_ops *ops,
1104 u32 id, const char *name, enum v4l2_ctrl_type type,
1105 s32 min, s32 max, u32 step, s32 def,
Hans Verkuil513521e2010-12-29 14:25:52 -03001106 u32 flags, const char * const *qmenu, void *priv)
Hans Verkuil09965172010-08-01 14:32:42 -03001107{
1108 struct v4l2_ctrl *ctrl;
1109 unsigned sz_extra = 0;
1110
1111 if (hdl->error)
1112 return NULL;
1113
1114 /* Sanity checks */
1115 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
Hans Verkuil09965172010-08-01 14:32:42 -03001116 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001117 (type == V4L2_CTRL_TYPE_BITMASK && max == 0) ||
Hans Verkuil09965172010-08-01 14:32:42 -03001118 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1119 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1120 handler_set_err(hdl, -ERANGE);
1121 return NULL;
1122 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001123 if (type != V4L2_CTRL_TYPE_BITMASK && max < min) {
1124 handler_set_err(hdl, -ERANGE);
1125 return NULL;
1126 }
Hans Verkuil02ac0482010-12-29 14:27:05 -03001127 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1128 type == V4L2_CTRL_TYPE_MENU ||
1129 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1130 (def < min || def > max)) {
1131 handler_set_err(hdl, -ERANGE);
1132 return NULL;
1133 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001134 if (type == V4L2_CTRL_TYPE_BITMASK && ((def & ~max) || min || step)) {
1135 handler_set_err(hdl, -ERANGE);
1136 return NULL;
1137 }
Hans Verkuil09965172010-08-01 14:32:42 -03001138
1139 if (type == V4L2_CTRL_TYPE_BUTTON)
1140 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1141 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1142 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1143 else if (type == V4L2_CTRL_TYPE_STRING)
1144 sz_extra += 2 * (max + 1);
1145
1146 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1147 if (ctrl == NULL) {
1148 handler_set_err(hdl, -ENOMEM);
1149 return NULL;
1150 }
1151
1152 INIT_LIST_HEAD(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -03001153 INIT_LIST_HEAD(&ctrl->ev_subs);
Hans Verkuil09965172010-08-01 14:32:42 -03001154 ctrl->handler = hdl;
1155 ctrl->ops = ops;
1156 ctrl->id = id;
1157 ctrl->name = name;
1158 ctrl->type = type;
1159 ctrl->flags = flags;
1160 ctrl->minimum = min;
1161 ctrl->maximum = max;
1162 ctrl->step = step;
1163 ctrl->qmenu = qmenu;
1164 ctrl->priv = priv;
1165 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1166
1167 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1168 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1169 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1170 if (ctrl->minimum)
1171 memset(ctrl->cur.string, ' ', ctrl->minimum);
1172 }
1173 if (handler_new_ref(hdl, ctrl)) {
1174 kfree(ctrl);
1175 return NULL;
1176 }
1177 mutex_lock(&hdl->lock);
1178 list_add_tail(&ctrl->node, &hdl->ctrls);
1179 mutex_unlock(&hdl->lock);
1180 return ctrl;
1181}
1182
1183struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1184 const struct v4l2_ctrl_config *cfg, void *priv)
1185{
1186 bool is_menu;
1187 struct v4l2_ctrl *ctrl;
1188 const char *name = cfg->name;
Hans Verkuil513521e2010-12-29 14:25:52 -03001189 const char * const *qmenu = cfg->qmenu;
Hans Verkuil09965172010-08-01 14:32:42 -03001190 enum v4l2_ctrl_type type = cfg->type;
1191 u32 flags = cfg->flags;
1192 s32 min = cfg->min;
1193 s32 max = cfg->max;
1194 u32 step = cfg->step;
1195 s32 def = cfg->def;
1196
1197 if (name == NULL)
1198 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1199 &def, &flags);
1200
1201 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1202 if (is_menu)
1203 WARN_ON(step);
1204 else
1205 WARN_ON(cfg->menu_skip_mask);
1206 if (is_menu && qmenu == NULL)
1207 qmenu = v4l2_ctrl_get_menu(cfg->id);
1208
1209 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1210 type, min, max,
1211 is_menu ? cfg->menu_skip_mask : step,
1212 def, flags, qmenu, priv);
1213 if (ctrl) {
1214 ctrl->is_private = cfg->is_private;
1215 ctrl->is_volatile = cfg->is_volatile;
1216 }
1217 return ctrl;
1218}
1219EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1220
1221/* Helper function for standard non-menu controls */
1222struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1223 const struct v4l2_ctrl_ops *ops,
1224 u32 id, s32 min, s32 max, u32 step, s32 def)
1225{
1226 const char *name;
1227 enum v4l2_ctrl_type type;
1228 u32 flags;
1229
1230 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1231 if (type == V4L2_CTRL_TYPE_MENU) {
1232 handler_set_err(hdl, -EINVAL);
1233 return NULL;
1234 }
1235 return v4l2_ctrl_new(hdl, ops, id, name, type,
1236 min, max, step, def, flags, NULL, NULL);
1237}
1238EXPORT_SYMBOL(v4l2_ctrl_new_std);
1239
1240/* Helper function for standard menu controls */
1241struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1242 const struct v4l2_ctrl_ops *ops,
1243 u32 id, s32 max, s32 mask, s32 def)
1244{
Hans Verkuil513521e2010-12-29 14:25:52 -03001245 const char * const *qmenu = v4l2_ctrl_get_menu(id);
Hans Verkuil09965172010-08-01 14:32:42 -03001246 const char *name;
1247 enum v4l2_ctrl_type type;
1248 s32 min;
1249 s32 step;
1250 u32 flags;
1251
1252 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1253 if (type != V4L2_CTRL_TYPE_MENU) {
1254 handler_set_err(hdl, -EINVAL);
1255 return NULL;
1256 }
1257 return v4l2_ctrl_new(hdl, ops, id, name, type,
1258 0, max, mask, def, flags, qmenu, NULL);
1259}
1260EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1261
1262/* Add a control from another handler to this handler */
1263struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1264 struct v4l2_ctrl *ctrl)
1265{
1266 if (hdl == NULL || hdl->error)
1267 return NULL;
1268 if (ctrl == NULL) {
1269 handler_set_err(hdl, -EINVAL);
1270 return NULL;
1271 }
1272 if (ctrl->handler == hdl)
1273 return ctrl;
1274 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1275}
1276EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1277
1278/* Add the controls from another handler to our own. */
1279int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1280 struct v4l2_ctrl_handler *add)
1281{
1282 struct v4l2_ctrl *ctrl;
1283 int ret = 0;
1284
1285 /* Do nothing if either handler is NULL or if they are the same */
1286 if (!hdl || !add || hdl == add)
1287 return 0;
1288 if (hdl->error)
1289 return hdl->error;
1290 mutex_lock(&add->lock);
1291 list_for_each_entry(ctrl, &add->ctrls, node) {
1292 /* Skip handler-private controls. */
1293 if (ctrl->is_private)
1294 continue;
Hans Verkuil6e239392011-06-07 11:13:44 -03001295 /* And control classes */
1296 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1297 continue;
Hans Verkuil09965172010-08-01 14:32:42 -03001298 ret = handler_new_ref(hdl, ctrl);
1299 if (ret)
1300 break;
1301 }
1302 mutex_unlock(&add->lock);
1303 return ret;
1304}
1305EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1306
1307/* Cluster controls */
1308void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1309{
1310 int i;
1311
1312 /* The first control is the master control and it must not be NULL */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001313 BUG_ON(ncontrols == 0 || controls[0] == NULL);
Hans Verkuil09965172010-08-01 14:32:42 -03001314
1315 for (i = 0; i < ncontrols; i++) {
1316 if (controls[i]) {
1317 controls[i]->cluster = controls;
1318 controls[i]->ncontrols = ncontrols;
1319 }
1320 }
1321}
1322EXPORT_SYMBOL(v4l2_ctrl_cluster);
1323
Hans Verkuil72d877c2011-06-10 05:44:36 -03001324void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1325 u8 manual_val, bool set_volatile)
1326{
1327 struct v4l2_ctrl *master = controls[0];
1328 u32 flag;
1329 int i;
1330
1331 v4l2_ctrl_cluster(ncontrols, controls);
1332 WARN_ON(ncontrols <= 1);
Hans Verkuil82a7c042011-06-28 10:43:13 -03001333 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001334 master->is_auto = true;
1335 master->manual_mode_value = manual_val;
1336 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1337 flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
1338
1339 for (i = 1; i < ncontrols; i++)
1340 if (controls[i]) {
1341 controls[i]->is_volatile = set_volatile;
1342 controls[i]->flags |= flag;
1343 }
1344}
1345EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1346
Hans Verkuil09965172010-08-01 14:32:42 -03001347/* Activate/deactivate a control. */
1348void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1349{
Hans Verkuil6e239392011-06-07 11:13:44 -03001350 /* invert since the actual flag is called 'inactive' */
1351 bool inactive = !active;
1352 bool old;
1353
Hans Verkuil09965172010-08-01 14:32:42 -03001354 if (ctrl == NULL)
1355 return;
1356
Hans Verkuil6e239392011-06-07 11:13:44 -03001357 if (inactive)
Hans Verkuil09965172010-08-01 14:32:42 -03001358 /* set V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001359 old = test_and_set_bit(4, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001360 else
1361 /* clear V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001362 old = test_and_clear_bit(4, &ctrl->flags);
1363 if (old != inactive)
1364 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
Hans Verkuil09965172010-08-01 14:32:42 -03001365}
1366EXPORT_SYMBOL(v4l2_ctrl_activate);
1367
1368/* Grab/ungrab a control.
1369 Typically used when streaming starts and you want to grab controls,
1370 preventing the user from changing them.
1371
1372 Just call this and the framework will block any attempts to change
1373 these controls. */
1374void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1375{
Hans Verkuil6e239392011-06-07 11:13:44 -03001376 bool old;
1377
Hans Verkuil09965172010-08-01 14:32:42 -03001378 if (ctrl == NULL)
1379 return;
1380
Hans Verkuil6e239392011-06-07 11:13:44 -03001381 v4l2_ctrl_lock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001382 if (grabbed)
1383 /* set V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001384 old = test_and_set_bit(1, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001385 else
1386 /* clear V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001387 old = test_and_clear_bit(1, &ctrl->flags);
1388 if (old != grabbed)
1389 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1390 v4l2_ctrl_unlock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001391}
1392EXPORT_SYMBOL(v4l2_ctrl_grab);
1393
1394/* Log the control name and value */
1395static void log_ctrl(const struct v4l2_ctrl *ctrl,
1396 const char *prefix, const char *colon)
1397{
1398 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1399 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1400
1401 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1402 return;
1403 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1404 return;
1405
1406 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1407
1408 switch (ctrl->type) {
1409 case V4L2_CTRL_TYPE_INTEGER:
1410 printk(KERN_CONT "%d", ctrl->cur.val);
1411 break;
1412 case V4L2_CTRL_TYPE_BOOLEAN:
1413 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1414 break;
1415 case V4L2_CTRL_TYPE_MENU:
1416 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1417 break;
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001418 case V4L2_CTRL_TYPE_BITMASK:
1419 printk(KERN_CONT "0x%08x", ctrl->cur.val);
1420 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001421 case V4L2_CTRL_TYPE_INTEGER64:
1422 printk(KERN_CONT "%lld", ctrl->cur.val64);
1423 break;
1424 case V4L2_CTRL_TYPE_STRING:
1425 printk(KERN_CONT "%s", ctrl->cur.string);
1426 break;
1427 default:
1428 printk(KERN_CONT "unknown type %d", ctrl->type);
1429 break;
1430 }
1431 if (fl_inact && fl_grabbed)
1432 printk(KERN_CONT " (inactive, grabbed)\n");
1433 else if (fl_inact)
1434 printk(KERN_CONT " (inactive)\n");
1435 else if (fl_grabbed)
1436 printk(KERN_CONT " (grabbed)\n");
1437 else
1438 printk(KERN_CONT "\n");
1439}
1440
1441/* Log all controls owned by the handler */
1442void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1443 const char *prefix)
1444{
1445 struct v4l2_ctrl *ctrl;
1446 const char *colon = "";
1447 int len;
1448
1449 if (hdl == NULL)
1450 return;
1451 if (prefix == NULL)
1452 prefix = "";
1453 len = strlen(prefix);
1454 if (len && prefix[len - 1] != ' ')
1455 colon = ": ";
1456 mutex_lock(&hdl->lock);
1457 list_for_each_entry(ctrl, &hdl->ctrls, node)
1458 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1459 log_ctrl(ctrl, prefix, colon);
1460 mutex_unlock(&hdl->lock);
1461}
1462EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1463
1464/* Call s_ctrl for all controls owned by the handler */
1465int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1466{
1467 struct v4l2_ctrl *ctrl;
1468 int ret = 0;
1469
1470 if (hdl == NULL)
1471 return 0;
1472 mutex_lock(&hdl->lock);
1473 list_for_each_entry(ctrl, &hdl->ctrls, node)
1474 ctrl->done = false;
1475
1476 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1477 struct v4l2_ctrl *master = ctrl->cluster[0];
1478 int i;
1479
1480 /* Skip if this control was already handled by a cluster. */
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001481 /* Skip button controls and read-only controls. */
1482 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1483 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
Hans Verkuil09965172010-08-01 14:32:42 -03001484 continue;
1485
Hans Verkuil2a863792011-01-11 14:45:03 -03001486 for (i = 0; i < master->ncontrols; i++) {
1487 if (master->cluster[i]) {
1488 cur_to_new(master->cluster[i]);
1489 master->cluster[i]->is_new = 1;
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001490 master->cluster[i]->done = true;
Hans Verkuil2a863792011-01-11 14:45:03 -03001491 }
1492 }
Hans Verkuil54c911e2011-05-25 06:04:58 -03001493 ret = call_op(master, s_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001494 if (ret)
1495 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001496 }
1497 mutex_unlock(&hdl->lock);
1498 return ret;
1499}
1500EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1501
1502/* Implement VIDIOC_QUERYCTRL */
1503int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1504{
1505 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1506 struct v4l2_ctrl_ref *ref;
1507 struct v4l2_ctrl *ctrl;
1508
1509 if (hdl == NULL)
1510 return -EINVAL;
1511
1512 mutex_lock(&hdl->lock);
1513
1514 /* Try to find it */
1515 ref = find_ref(hdl, id);
1516
1517 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1518 /* Find the next control with ID > qc->id */
1519
1520 /* Did we reach the end of the control list? */
1521 if (id >= node2id(hdl->ctrl_refs.prev)) {
1522 ref = NULL; /* Yes, so there is no next control */
1523 } else if (ref) {
1524 /* We found a control with the given ID, so just get
1525 the next one in the list. */
1526 ref = list_entry(ref->node.next, typeof(*ref), node);
1527 } else {
1528 /* No control with the given ID exists, so start
1529 searching for the next largest ID. We know there
1530 is one, otherwise the first 'if' above would have
1531 been true. */
1532 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1533 if (id < ref->ctrl->id)
1534 break;
1535 }
1536 }
1537 mutex_unlock(&hdl->lock);
1538 if (!ref)
1539 return -EINVAL;
1540
1541 ctrl = ref->ctrl;
1542 memset(qc, 0, sizeof(*qc));
Hans Verkuil829fb2d2011-01-16 11:21:40 -03001543 if (id >= V4L2_CID_PRIVATE_BASE)
1544 qc->id = id;
1545 else
1546 qc->id = ctrl->id;
Hans Verkuil09965172010-08-01 14:32:42 -03001547 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1548 qc->minimum = ctrl->minimum;
1549 qc->maximum = ctrl->maximum;
1550 qc->default_value = ctrl->default_value;
Laurent Pincharteac9aa02010-12-07 08:57:25 -03001551 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
Hans Verkuil09965172010-08-01 14:32:42 -03001552 qc->step = 1;
1553 else
1554 qc->step = ctrl->step;
1555 qc->flags = ctrl->flags;
1556 qc->type = ctrl->type;
1557 return 0;
1558}
1559EXPORT_SYMBOL(v4l2_queryctrl);
1560
1561int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1562{
Hans Verkuil87a0c942011-02-22 12:31:07 -03001563 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1564 return -EINVAL;
Hans Verkuil09965172010-08-01 14:32:42 -03001565 return v4l2_queryctrl(sd->ctrl_handler, qc);
1566}
1567EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1568
1569/* Implement VIDIOC_QUERYMENU */
1570int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1571{
1572 struct v4l2_ctrl *ctrl;
1573 u32 i = qm->index;
1574
1575 ctrl = v4l2_ctrl_find(hdl, qm->id);
1576 if (!ctrl)
1577 return -EINVAL;
1578
1579 qm->reserved = 0;
1580 /* Sanity checks */
1581 if (ctrl->qmenu == NULL ||
1582 i < ctrl->minimum || i > ctrl->maximum)
1583 return -EINVAL;
1584 /* Use mask to see if this menu item should be skipped */
1585 if (ctrl->menu_skip_mask & (1 << i))
1586 return -EINVAL;
1587 /* Empty menu items should also be skipped */
1588 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1589 return -EINVAL;
1590 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1591 return 0;
1592}
1593EXPORT_SYMBOL(v4l2_querymenu);
1594
1595int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1596{
1597 return v4l2_querymenu(sd->ctrl_handler, qm);
1598}
1599EXPORT_SYMBOL(v4l2_subdev_querymenu);
1600
1601
1602
1603/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1604
1605 It is not a fully atomic operation, just best-effort only. After all, if
1606 multiple controls have to be set through multiple i2c writes (for example)
1607 then some initial writes may succeed while others fail. Thus leaving the
1608 system in an inconsistent state. The question is how much effort you are
1609 willing to spend on trying to make something atomic that really isn't.
1610
1611 From the point of view of an application the main requirement is that
1612 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1613 error should be returned without actually affecting any controls.
1614
1615 If all the values are correct, then it is acceptable to just give up
1616 in case of low-level errors.
1617
1618 It is important though that the application can tell when only a partial
1619 configuration was done. The way we do that is through the error_idx field
1620 of struct v4l2_ext_controls: if that is equal to the count field then no
1621 controls were affected. Otherwise all controls before that index were
1622 successful in performing their 'get' or 'set' operation, the control at
1623 the given index failed, and you don't know what happened with the controls
1624 after the failed one. Since if they were part of a control cluster they
1625 could have been successfully processed (if a cluster member was encountered
1626 at index < error_idx), they could have failed (if a cluster member was at
1627 error_idx), or they may not have been processed yet (if the first cluster
1628 member appeared after error_idx).
1629
1630 It is all fairly theoretical, though. In practice all you can do is to
1631 bail out. If error_idx == count, then it is an application bug. If
1632 error_idx < count then it is only an application bug if the error code was
1633 EBUSY. That usually means that something started streaming just when you
1634 tried to set the controls. In all other cases it is a driver/hardware
1635 problem and all you can do is to retry or bail out.
1636
1637 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1638 never modifies controls the error_idx is just set to whatever control
1639 has an invalid value.
1640 */
1641
1642/* Prepare for the extended g/s/try functions.
1643 Find the controls in the control array and do some basic checks. */
1644static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1645 struct v4l2_ext_controls *cs,
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001646 struct v4l2_ctrl_helper *helpers)
Hans Verkuil09965172010-08-01 14:32:42 -03001647{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001648 struct v4l2_ctrl_helper *h;
1649 bool have_clusters = false;
Hans Verkuil09965172010-08-01 14:32:42 -03001650 u32 i;
1651
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001652 for (i = 0, h = helpers; i < cs->count; i++, h++) {
Hans Verkuil09965172010-08-01 14:32:42 -03001653 struct v4l2_ext_control *c = &cs->controls[i];
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001654 struct v4l2_ctrl_ref *ref;
Hans Verkuil09965172010-08-01 14:32:42 -03001655 struct v4l2_ctrl *ctrl;
1656 u32 id = c->id & V4L2_CTRL_ID_MASK;
1657
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001658 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001659
1660 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1661 return -EINVAL;
1662
1663 /* Old-style private controls are not allowed for
1664 extended controls */
1665 if (id >= V4L2_CID_PRIVATE_BASE)
1666 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001667 ref = find_ref_lock(hdl, id);
1668 if (ref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001669 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001670 ctrl = ref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001671 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1672 return -EINVAL;
1673
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001674 if (ctrl->cluster[0]->ncontrols > 1)
1675 have_clusters = true;
1676 if (ctrl->cluster[0] != ctrl)
1677 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
1678 /* Store the ref to the master control of the cluster */
1679 h->mref = ref;
1680 h->ctrl = ctrl;
1681 /* Initially set next to 0, meaning that there is no other
1682 control in this helper array belonging to the same
1683 cluster */
1684 h->next = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001685 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001686
1687 /* We are done if there were no controls that belong to a multi-
1688 control cluster. */
1689 if (!have_clusters)
1690 return 0;
1691
1692 /* The code below figures out in O(n) time which controls in the list
1693 belong to the same cluster. */
1694
1695 /* This has to be done with the handler lock taken. */
1696 mutex_lock(&hdl->lock);
1697
1698 /* First zero the helper field in the master control references */
1699 for (i = 0; i < cs->count; i++)
1700 helpers[i].mref->helper = 0;
1701 for (i = 0, h = helpers; i < cs->count; i++, h++) {
1702 struct v4l2_ctrl_ref *mref = h->mref;
1703
1704 /* If the mref->helper is set, then it points to an earlier
1705 helper that belongs to the same cluster. */
1706 if (mref->helper) {
1707 /* Set the next field of mref->helper to the current
1708 index: this means that that earlier helper now
1709 points to the next helper in the same cluster. */
1710 mref->helper->next = i;
1711 /* mref should be set only for the first helper in the
1712 cluster, clear the others. */
1713 h->mref = NULL;
1714 }
1715 /* Point the mref helper to the current helper struct. */
1716 mref->helper = h;
1717 }
1718 mutex_unlock(&hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001719 return 0;
1720}
1721
Hans Verkuil09965172010-08-01 14:32:42 -03001722/* Handles the corner case where cs->count == 0. It checks whether the
1723 specified control class exists. If that class ID is 0, then it checks
1724 whether there are any controls at all. */
1725static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1726{
1727 if (ctrl_class == 0)
1728 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1729 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1730}
1731
1732
1733
1734/* Get extended controls. Allocates the helpers array if needed. */
1735int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1736{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001737 struct v4l2_ctrl_helper helper[4];
1738 struct v4l2_ctrl_helper *helpers = helper;
Hans Verkuil09965172010-08-01 14:32:42 -03001739 int ret;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001740 int i, j;
Hans Verkuil09965172010-08-01 14:32:42 -03001741
1742 cs->error_idx = cs->count;
1743 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1744
1745 if (hdl == NULL)
1746 return -EINVAL;
1747
1748 if (cs->count == 0)
1749 return class_check(hdl, cs->ctrl_class);
1750
1751 if (cs->count > ARRAY_SIZE(helper)) {
1752 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1753 if (helpers == NULL)
1754 return -ENOMEM;
1755 }
1756
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001757 ret = prepare_ext_ctrls(hdl, cs, helpers);
1758 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001759
1760 for (i = 0; !ret && i < cs->count; i++)
1761 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1762 ret = -EACCES;
1763
1764 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001765 int (*ctrl_to_user)(struct v4l2_ext_control *c,
1766 struct v4l2_ctrl *ctrl) = cur_to_user;
1767 struct v4l2_ctrl *master;
Hans Verkuil09965172010-08-01 14:32:42 -03001768
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001769 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001770 continue;
1771
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001772 master = helpers[i].mref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001773 cs->error_idx = i;
1774
1775 v4l2_ctrl_lock(master);
Hans Verkuilddac5c12011-06-10 05:43:34 -03001776
1777 /* g_volatile_ctrl will update the new control values */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001778 if (has_op(master, g_volatile_ctrl) && !is_cur_manual(master)) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03001779 for (j = 0; j < master->ncontrols; j++)
1780 cur_to_new(master->cluster[j]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03001781 ret = call_op(master, g_volatile_ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001782 ctrl_to_user = new_to_user;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001783 }
1784 /* If OK, then copy the current (for non-volatile controls)
1785 or the new (for volatile controls) control values to the
1786 caller */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001787 if (!ret) {
1788 u32 idx = i;
1789
1790 do {
1791 ret = ctrl_to_user(cs->controls + idx,
1792 helpers[idx].ctrl);
1793 idx = helpers[idx].next;
1794 } while (!ret && idx);
1795 }
Hans Verkuil09965172010-08-01 14:32:42 -03001796 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001797 }
1798
1799 if (cs->count > ARRAY_SIZE(helper))
1800 kfree(helpers);
1801 return ret;
1802}
1803EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1804
1805int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1806{
1807 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1808}
1809EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1810
1811/* Helper function to get a single control */
1812static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1813{
1814 struct v4l2_ctrl *master = ctrl->cluster[0];
1815 int ret = 0;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001816 int i;
Hans Verkuil09965172010-08-01 14:32:42 -03001817
1818 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1819 return -EACCES;
1820
1821 v4l2_ctrl_lock(master);
1822 /* g_volatile_ctrl will update the current control values */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001823 if (ctrl->is_volatile && !is_cur_manual(master)) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03001824 for (i = 0; i < master->ncontrols; i++)
1825 cur_to_new(master->cluster[i]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03001826 ret = call_op(master, g_volatile_ctrl);
Hans Verkuilddac5c12011-06-10 05:43:34 -03001827 *val = ctrl->val;
1828 } else {
1829 *val = ctrl->cur.val;
1830 }
Hans Verkuil09965172010-08-01 14:32:42 -03001831 v4l2_ctrl_unlock(master);
1832 return ret;
1833}
1834
1835int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1836{
1837 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1838
1839 if (ctrl == NULL || !type_is_int(ctrl))
1840 return -EINVAL;
1841 return get_ctrl(ctrl, &control->value);
1842}
1843EXPORT_SYMBOL(v4l2_g_ctrl);
1844
1845int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1846{
1847 return v4l2_g_ctrl(sd->ctrl_handler, control);
1848}
1849EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1850
1851s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1852{
1853 s32 val = 0;
1854
1855 /* It's a driver bug if this happens. */
1856 WARN_ON(!type_is_int(ctrl));
1857 get_ctrl(ctrl, &val);
1858 return val;
1859}
1860EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1861
1862
1863/* Core function that calls try/s_ctrl and ensures that the new value is
1864 copied to the current value on a set.
1865 Must be called with ctrl->handler->lock held. */
Hans Verkuile6402582011-06-14 10:56:42 -03001866static int try_or_set_cluster(struct v4l2_fh *fh,
1867 struct v4l2_ctrl *master, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03001868{
Hans Verkuil72d877c2011-06-10 05:44:36 -03001869 bool update_flag;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001870 int ret;
Hans Verkuil09965172010-08-01 14:32:42 -03001871 int i;
1872
1873 /* Go through the cluster and either validate the new value or
1874 (if no new value was set), copy the current value to the new
1875 value, ensuring a consistent view for the control ops when
1876 called. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001877 for (i = 0; i < master->ncontrols; i++) {
Hans Verkuil09965172010-08-01 14:32:42 -03001878 struct v4l2_ctrl *ctrl = master->cluster[i];
1879
1880 if (ctrl == NULL)
1881 continue;
1882
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001883 if (!ctrl->is_new) {
1884 cur_to_new(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001885 continue;
1886 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001887 /* Check again: it may have changed since the
1888 previous check in try_or_set_ext_ctrls(). */
1889 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1890 return -EBUSY;
Hans Verkuil09965172010-08-01 14:32:42 -03001891 }
1892
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001893 ret = call_op(master, try_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001894
1895 /* Don't set if there is no change */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001896 if (ret || !set || !cluster_changed(master))
1897 return ret;
1898 ret = call_op(master, s_ctrl);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001899 if (ret)
1900 return ret;
1901
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001902 /* If OK, then make the new values permanent. */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001903 update_flag = is_cur_manual(master) != is_new_manual(master);
1904 for (i = 0; i < master->ncontrols; i++)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001905 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001906 return 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001907}
1908
Hans Verkuile6402582011-06-14 10:56:42 -03001909/* Validate controls. */
1910static int validate_ctrls(struct v4l2_ext_controls *cs,
1911 struct v4l2_ctrl_helper *helpers, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03001912{
Hans Verkuile6402582011-06-14 10:56:42 -03001913 unsigned i;
Hans Verkuil09965172010-08-01 14:32:42 -03001914 int ret = 0;
1915
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001916 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001917 for (i = 0; i < cs->count; i++) {
1918 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1919
Hans Verkuile6402582011-06-14 10:56:42 -03001920 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001921
1922 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1923 return -EACCES;
1924 /* This test is also done in try_set_control_cluster() which
1925 is called in atomic context, so that has the final say,
1926 but it makes sense to do an up-front check as well. Once
1927 an error occurs in try_set_control_cluster() some other
1928 controls may have been set already and we want to do a
1929 best-effort to avoid that. */
1930 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1931 return -EBUSY;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001932 ret = validate_new(ctrl, &cs->controls[i]);
1933 if (ret)
1934 return ret;
Hans Verkuil09965172010-08-01 14:32:42 -03001935 }
Hans Verkuile6402582011-06-14 10:56:42 -03001936 return 0;
1937}
Hans Verkuil09965172010-08-01 14:32:42 -03001938
Hans Verkuile6402582011-06-14 10:56:42 -03001939/* Try or try-and-set controls */
1940static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1941 struct v4l2_ext_controls *cs,
1942 bool set)
1943{
1944 struct v4l2_ctrl_helper helper[4];
1945 struct v4l2_ctrl_helper *helpers = helper;
1946 unsigned i, j;
1947 int ret;
1948
1949 cs->error_idx = cs->count;
1950 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1951
1952 if (hdl == NULL)
1953 return -EINVAL;
1954
1955 if (cs->count == 0)
1956 return class_check(hdl, cs->ctrl_class);
1957
1958 if (cs->count > ARRAY_SIZE(helper)) {
1959 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1960 if (!helpers)
1961 return -ENOMEM;
1962 }
1963 ret = prepare_ext_ctrls(hdl, cs, helpers);
1964 if (!ret)
1965 ret = validate_ctrls(cs, helpers, set);
1966 if (ret && set)
1967 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001968 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001969 struct v4l2_ctrl *master;
1970 u32 idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001971
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001972 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001973 continue;
1974
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001975 cs->error_idx = i;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001976 master = helpers[i].mref->ctrl;
1977 v4l2_ctrl_lock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001978
Hans Verkuil2a863792011-01-11 14:45:03 -03001979 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03001980 for (j = 0; j < master->ncontrols; j++)
1981 if (master->cluster[j])
Hans Verkuil2a863792011-01-11 14:45:03 -03001982 master->cluster[j]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001983
1984 /* Copy the new caller-supplied control values.
Hans Verkuil2a863792011-01-11 14:45:03 -03001985 user_to_new() sets 'is_new' to 1. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001986 do {
1987 ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
1988 idx = helpers[idx].next;
1989 } while (!ret && idx);
Hans Verkuil09965172010-08-01 14:32:42 -03001990
1991 if (!ret)
Hans Verkuile6402582011-06-14 10:56:42 -03001992 ret = try_or_set_cluster(fh, master, set);
Hans Verkuil09965172010-08-01 14:32:42 -03001993
1994 /* Copy the new values back to userspace. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001995 if (!ret) {
1996 idx = i;
1997 do {
Hans Verkuiladf41b92011-07-05 06:56:37 -03001998 ret = new_to_user(cs->controls + idx,
Hans Verkuile6402582011-06-14 10:56:42 -03001999 helpers[idx].ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002000 idx = helpers[idx].next;
2001 } while (!ret && idx);
2002 }
2003 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002004 }
Hans Verkuil09965172010-08-01 14:32:42 -03002005
Hans Verkuil09965172010-08-01 14:32:42 -03002006 if (cs->count > ARRAY_SIZE(helper))
2007 kfree(helpers);
2008 return ret;
2009}
2010
2011int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2012{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002013 return try_set_ext_ctrls(NULL, hdl, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03002014}
2015EXPORT_SYMBOL(v4l2_try_ext_ctrls);
2016
Hans Verkuilab892ba2011-06-07 06:47:18 -03002017int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2018 struct v4l2_ext_controls *cs)
Hans Verkuil09965172010-08-01 14:32:42 -03002019{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002020 return try_set_ext_ctrls(fh, hdl, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002021}
2022EXPORT_SYMBOL(v4l2_s_ext_ctrls);
2023
2024int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2025{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002026 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03002027}
2028EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
2029
2030int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2031{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002032 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002033}
2034EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
2035
2036/* Helper function for VIDIOC_S_CTRL compatibility */
Hans Verkuilab892ba2011-06-07 06:47:18 -03002037static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
Hans Verkuil09965172010-08-01 14:32:42 -03002038{
2039 struct v4l2_ctrl *master = ctrl->cluster[0];
2040 int ret;
2041 int i;
2042
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002043 ret = validate_new_int(ctrl, val);
2044 if (ret)
2045 return ret;
2046
Hans Verkuil09965172010-08-01 14:32:42 -03002047 v4l2_ctrl_lock(ctrl);
2048
Hans Verkuil2a863792011-01-11 14:45:03 -03002049 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03002050 for (i = 0; i < master->ncontrols; i++)
2051 if (master->cluster[i])
Hans Verkuil2a863792011-01-11 14:45:03 -03002052 master->cluster[i]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002053
2054 ctrl->val = *val;
Hans Verkuil2a863792011-01-11 14:45:03 -03002055 ctrl->is_new = 1;
Hans Verkuile6402582011-06-14 10:56:42 -03002056 ret = try_or_set_cluster(fh, master, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002057 *val = ctrl->cur.val;
2058 v4l2_ctrl_unlock(ctrl);
2059 return ret;
2060}
2061
Hans Verkuilab892ba2011-06-07 06:47:18 -03002062int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2063 struct v4l2_control *control)
Hans Verkuil09965172010-08-01 14:32:42 -03002064{
2065 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2066
2067 if (ctrl == NULL || !type_is_int(ctrl))
2068 return -EINVAL;
2069
Hans Verkuil7ebbc392011-06-07 04:50:31 -03002070 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2071 return -EACCES;
2072
Hans Verkuilab892ba2011-06-07 06:47:18 -03002073 return set_ctrl(fh, ctrl, &control->value);
Hans Verkuil09965172010-08-01 14:32:42 -03002074}
2075EXPORT_SYMBOL(v4l2_s_ctrl);
2076
2077int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2078{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002079 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
Hans Verkuil09965172010-08-01 14:32:42 -03002080}
2081EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2082
2083int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2084{
2085 /* It's a driver bug if this happens. */
2086 WARN_ON(!type_is_int(ctrl));
Hans Verkuilab892ba2011-06-07 06:47:18 -03002087 return set_ctrl(NULL, ctrl, &val);
Hans Verkuil09965172010-08-01 14:32:42 -03002088}
2089EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
Hans Verkuil6e239392011-06-07 11:13:44 -03002090
Hans Verkuil77068d32011-06-13 18:55:58 -03002091void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
2092 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002093{
Hans Verkuil6e239392011-06-07 11:13:44 -03002094 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002095 list_add_tail(&sev->node, &ctrl->ev_subs);
Hans Verkuil6e239392011-06-07 11:13:44 -03002096 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
Hans Verkuil77068d32011-06-13 18:55:58 -03002097 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
Hans Verkuil6e239392011-06-07 11:13:44 -03002098 struct v4l2_event ev;
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002099 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
Hans Verkuil6e239392011-06-07 11:13:44 -03002100
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002101 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2102 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2103 fill_event(&ev, ctrl, changes);
Hans Verkuil77068d32011-06-13 18:55:58 -03002104 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -03002105 }
2106 v4l2_ctrl_unlock(ctrl);
2107}
Hans Verkuil77068d32011-06-13 18:55:58 -03002108EXPORT_SYMBOL(v4l2_ctrl_add_event);
Hans Verkuil6e239392011-06-07 11:13:44 -03002109
Hans Verkuil77068d32011-06-13 18:55:58 -03002110void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
2111 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002112{
Hans Verkuil6e239392011-06-07 11:13:44 -03002113 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002114 list_del(&sev->node);
Hans Verkuil6e239392011-06-07 11:13:44 -03002115 v4l2_ctrl_unlock(ctrl);
2116}
Hans Verkuil77068d32011-06-13 18:55:58 -03002117EXPORT_SYMBOL(v4l2_ctrl_del_event);