2 V4L2 controls framework implementation.
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
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.
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.
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
21 #include <linux/ctype.h>
22 #include <linux/slab.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-dev.h>
28 #define call_op(master, op) \
29 ((master->ops && master->ops->op) ? master->ops->op(master) : 0)
31 /* Internal temporary helper struct, one for each v4l2_ext_control */
33 /* The control corresponding to the v4l2_ext_control ID field. */
34 struct v4l2_ctrl *ctrl;
35 /* Used internally to mark whether this control was already
40 /* Returns NULL or a character pointer array containing the menu for
41 the given control ID. The pointer array ends with a NULL pointer.
42 An empty string signifies a menu entry that is invalid. This allows
43 drivers to disable certain options if it is not supported. */
44 const char * const *v4l2_ctrl_get_menu(u32 id)
46 static const char * const mpeg_audio_sampling_freq[] = {
52 static const char * const mpeg_audio_encoding[] = {
60 static const char * const mpeg_audio_l1_bitrate[] = {
77 static const char * const mpeg_audio_l2_bitrate[] = {
94 static const char * const mpeg_audio_l3_bitrate[] = {
111 static const char * const mpeg_audio_ac3_bitrate[] = {
133 static const char * const mpeg_audio_mode[] = {
140 static const char * const mpeg_audio_mode_extension[] = {
147 static const char * const mpeg_audio_emphasis[] = {
153 static const char * const mpeg_audio_crc[] = {
158 static const char * const mpeg_video_encoding[] = {
164 static const char * const mpeg_video_aspect[] = {
171 static const char * const mpeg_video_bitrate_mode[] = {
176 static const char * const mpeg_stream_type[] = {
177 "MPEG-2 Program Stream",
178 "MPEG-2 Transport Stream",
179 "MPEG-1 System Stream",
180 "MPEG-2 DVD-compatible Stream",
181 "MPEG-1 VCD-compatible Stream",
182 "MPEG-2 SVCD-compatible Stream",
185 static const char * const mpeg_stream_vbi_fmt[] = {
187 "Private packet, IVTV format",
190 static const char * const camera_power_line_frequency[] = {
196 static const char * const camera_exposure_auto[] = {
199 "Shutter Priority Mode",
200 "Aperture Priority Mode",
203 static const char * const colorfx[] = {
216 static const char * const tune_preemphasis[] = {
224 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
225 return mpeg_audio_sampling_freq;
226 case V4L2_CID_MPEG_AUDIO_ENCODING:
227 return mpeg_audio_encoding;
228 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
229 return mpeg_audio_l1_bitrate;
230 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
231 return mpeg_audio_l2_bitrate;
232 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
233 return mpeg_audio_l3_bitrate;
234 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
235 return mpeg_audio_ac3_bitrate;
236 case V4L2_CID_MPEG_AUDIO_MODE:
237 return mpeg_audio_mode;
238 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
239 return mpeg_audio_mode_extension;
240 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
241 return mpeg_audio_emphasis;
242 case V4L2_CID_MPEG_AUDIO_CRC:
243 return mpeg_audio_crc;
244 case V4L2_CID_MPEG_VIDEO_ENCODING:
245 return mpeg_video_encoding;
246 case V4L2_CID_MPEG_VIDEO_ASPECT:
247 return mpeg_video_aspect;
248 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
249 return mpeg_video_bitrate_mode;
250 case V4L2_CID_MPEG_STREAM_TYPE:
251 return mpeg_stream_type;
252 case V4L2_CID_MPEG_STREAM_VBI_FMT:
253 return mpeg_stream_vbi_fmt;
254 case V4L2_CID_POWER_LINE_FREQUENCY:
255 return camera_power_line_frequency;
256 case V4L2_CID_EXPOSURE_AUTO:
257 return camera_exposure_auto;
258 case V4L2_CID_COLORFX:
260 case V4L2_CID_TUNE_PREEMPHASIS:
261 return tune_preemphasis;
266 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
268 /* Return the control name. */
269 const char *v4l2_ctrl_get_name(u32 id)
273 /* Keep the order of the 'case's the same as in videodev2.h! */
274 case V4L2_CID_USER_CLASS: return "User Controls";
275 case V4L2_CID_BRIGHTNESS: return "Brightness";
276 case V4L2_CID_CONTRAST: return "Contrast";
277 case V4L2_CID_SATURATION: return "Saturation";
278 case V4L2_CID_HUE: return "Hue";
279 case V4L2_CID_AUDIO_VOLUME: return "Volume";
280 case V4L2_CID_AUDIO_BALANCE: return "Balance";
281 case V4L2_CID_AUDIO_BASS: return "Bass";
282 case V4L2_CID_AUDIO_TREBLE: return "Treble";
283 case V4L2_CID_AUDIO_MUTE: return "Mute";
284 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
285 case V4L2_CID_BLACK_LEVEL: return "Black Level";
286 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
287 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
288 case V4L2_CID_RED_BALANCE: return "Red Balance";
289 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
290 case V4L2_CID_GAMMA: return "Gamma";
291 case V4L2_CID_EXPOSURE: return "Exposure";
292 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
293 case V4L2_CID_GAIN: return "Gain";
294 case V4L2_CID_HFLIP: return "Horizontal Flip";
295 case V4L2_CID_VFLIP: return "Vertical Flip";
296 case V4L2_CID_HCENTER: return "Horizontal Center";
297 case V4L2_CID_VCENTER: return "Vertical Center";
298 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
299 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
300 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
301 case V4L2_CID_SHARPNESS: return "Sharpness";
302 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
303 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
304 case V4L2_CID_COLOR_KILLER: return "Color Killer";
305 case V4L2_CID_COLORFX: return "Color Effects";
306 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
307 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
308 case V4L2_CID_ROTATE: return "Rotate";
309 case V4L2_CID_BG_COLOR: return "Background Color";
310 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
311 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
312 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
315 /* Keep the order of the 'case's the same as in videodev2.h! */
316 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
317 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
318 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
319 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
320 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
321 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
322 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
323 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
324 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
325 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
326 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
327 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
328 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
329 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
330 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
331 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
332 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
333 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
334 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
335 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
336 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
337 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
338 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
339 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
340 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
341 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
342 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
343 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
344 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
345 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
346 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
347 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
348 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
350 /* CAMERA controls */
351 /* Keep the order of the 'case's the same as in videodev2.h! */
352 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
353 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
354 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
355 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
356 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
357 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
358 case V4L2_CID_PAN_RESET: return "Pan, Reset";
359 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
360 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
361 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
362 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
363 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
364 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
365 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
366 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
367 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
368 case V4L2_CID_PRIVACY: return "Privacy";
369 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
370 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
372 /* FM Radio Modulator control */
373 /* Keep the order of the 'case's the same as in videodev2.h! */
374 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
375 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
376 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
377 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
378 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
379 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
380 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
381 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
382 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
383 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
384 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
385 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
386 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
387 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
388 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
389 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
390 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
391 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
392 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
393 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
399 EXPORT_SYMBOL(v4l2_ctrl_get_name);
401 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
402 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
404 *name = v4l2_ctrl_get_name(id);
408 case V4L2_CID_AUDIO_MUTE:
409 case V4L2_CID_AUDIO_LOUDNESS:
410 case V4L2_CID_AUTO_WHITE_BALANCE:
411 case V4L2_CID_AUTOGAIN:
414 case V4L2_CID_HUE_AUTO:
415 case V4L2_CID_CHROMA_AGC:
416 case V4L2_CID_COLOR_KILLER:
417 case V4L2_CID_MPEG_AUDIO_MUTE:
418 case V4L2_CID_MPEG_VIDEO_MUTE:
419 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
420 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
421 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
422 case V4L2_CID_FOCUS_AUTO:
423 case V4L2_CID_PRIVACY:
424 case V4L2_CID_AUDIO_LIMITER_ENABLED:
425 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
426 case V4L2_CID_PILOT_TONE_ENABLED:
427 case V4L2_CID_ILLUMINATORS_1:
428 case V4L2_CID_ILLUMINATORS_2:
429 *type = V4L2_CTRL_TYPE_BOOLEAN;
433 case V4L2_CID_PAN_RESET:
434 case V4L2_CID_TILT_RESET:
435 *type = V4L2_CTRL_TYPE_BUTTON;
436 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
437 *min = *max = *step = *def = 0;
439 case V4L2_CID_POWER_LINE_FREQUENCY:
440 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
441 case V4L2_CID_MPEG_AUDIO_ENCODING:
442 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
443 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
444 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
445 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
446 case V4L2_CID_MPEG_AUDIO_MODE:
447 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
448 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
449 case V4L2_CID_MPEG_AUDIO_CRC:
450 case V4L2_CID_MPEG_VIDEO_ENCODING:
451 case V4L2_CID_MPEG_VIDEO_ASPECT:
452 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
453 case V4L2_CID_MPEG_STREAM_TYPE:
454 case V4L2_CID_MPEG_STREAM_VBI_FMT:
455 case V4L2_CID_EXPOSURE_AUTO:
456 case V4L2_CID_COLORFX:
457 case V4L2_CID_TUNE_PREEMPHASIS:
458 *type = V4L2_CTRL_TYPE_MENU;
460 case V4L2_CID_RDS_TX_PS_NAME:
461 case V4L2_CID_RDS_TX_RADIO_TEXT:
462 *type = V4L2_CTRL_TYPE_STRING;
464 case V4L2_CID_USER_CLASS:
465 case V4L2_CID_CAMERA_CLASS:
466 case V4L2_CID_MPEG_CLASS:
467 case V4L2_CID_FM_TX_CLASS:
468 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
469 /* You can neither read not write these */
470 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
471 *min = *max = *step = *def = 0;
473 case V4L2_CID_BG_COLOR:
474 *type = V4L2_CTRL_TYPE_INTEGER;
477 /* Max is calculated as RGB888 that is 2^24 */
481 *type = V4L2_CTRL_TYPE_INTEGER;
485 case V4L2_CID_MPEG_AUDIO_ENCODING:
486 case V4L2_CID_MPEG_AUDIO_MODE:
487 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
488 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
489 case V4L2_CID_MPEG_STREAM_TYPE:
490 *flags |= V4L2_CTRL_FLAG_UPDATE;
492 case V4L2_CID_AUDIO_VOLUME:
493 case V4L2_CID_AUDIO_BALANCE:
494 case V4L2_CID_AUDIO_BASS:
495 case V4L2_CID_AUDIO_TREBLE:
496 case V4L2_CID_BRIGHTNESS:
497 case V4L2_CID_CONTRAST:
498 case V4L2_CID_SATURATION:
500 case V4L2_CID_RED_BALANCE:
501 case V4L2_CID_BLUE_BALANCE:
503 case V4L2_CID_SHARPNESS:
504 case V4L2_CID_CHROMA_GAIN:
505 case V4L2_CID_RDS_TX_DEVIATION:
506 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
507 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
508 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
509 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
510 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
511 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
512 case V4L2_CID_PILOT_TONE_DEVIATION:
513 case V4L2_CID_PILOT_TONE_FREQUENCY:
514 case V4L2_CID_TUNE_POWER_LEVEL:
515 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
516 *flags |= V4L2_CTRL_FLAG_SLIDER;
518 case V4L2_CID_PAN_RELATIVE:
519 case V4L2_CID_TILT_RELATIVE:
520 case V4L2_CID_FOCUS_RELATIVE:
521 case V4L2_CID_IRIS_RELATIVE:
522 case V4L2_CID_ZOOM_RELATIVE:
523 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
527 EXPORT_SYMBOL(v4l2_ctrl_fill);
529 /* Helper function to determine whether the control type is compatible with
531 static bool type_is_int(const struct v4l2_ctrl *ctrl)
533 switch (ctrl->type) {
534 case V4L2_CTRL_TYPE_INTEGER64:
535 case V4L2_CTRL_TYPE_STRING:
536 /* Nope, these need v4l2_ext_control */
543 /* Helper function: copy the current control value back to the caller */
544 static int cur_to_user(struct v4l2_ext_control *c,
545 struct v4l2_ctrl *ctrl)
549 switch (ctrl->type) {
550 case V4L2_CTRL_TYPE_STRING:
551 len = strlen(ctrl->cur.string);
552 if (c->size < len + 1) {
556 return copy_to_user(c->string, ctrl->cur.string,
557 len + 1) ? -EFAULT : 0;
558 case V4L2_CTRL_TYPE_INTEGER64:
559 c->value64 = ctrl->cur.val64;
562 c->value = ctrl->cur.val;
568 /* Helper function: copy the caller-provider value as the new control value */
569 static int user_to_new(struct v4l2_ext_control *c,
570 struct v4l2_ctrl *ctrl)
576 switch (ctrl->type) {
577 case V4L2_CTRL_TYPE_INTEGER64:
578 ctrl->val64 = c->value64;
580 case V4L2_CTRL_TYPE_STRING:
584 if (size > ctrl->maximum + 1)
585 size = ctrl->maximum + 1;
586 ret = copy_from_user(ctrl->string, c->string, size);
588 char last = ctrl->string[size - 1];
590 ctrl->string[size - 1] = 0;
591 /* If the string was longer than ctrl->maximum,
592 then return an error. */
593 if (strlen(ctrl->string) == ctrl->maximum && last)
596 return ret ? -EFAULT : 0;
598 ctrl->val = c->value;
604 /* Helper function: copy the new control value back to the caller */
605 static int new_to_user(struct v4l2_ext_control *c,
606 struct v4l2_ctrl *ctrl)
610 switch (ctrl->type) {
611 case V4L2_CTRL_TYPE_STRING:
612 len = strlen(ctrl->string);
613 if (c->size < len + 1) {
614 c->size = ctrl->maximum + 1;
617 return copy_to_user(c->string, ctrl->string,
618 len + 1) ? -EFAULT : 0;
619 case V4L2_CTRL_TYPE_INTEGER64:
620 c->value64 = ctrl->val64;
623 c->value = ctrl->val;
629 /* Copy the new value to the current value. */
630 static void new_to_cur(struct v4l2_ctrl *ctrl)
634 switch (ctrl->type) {
635 case V4L2_CTRL_TYPE_STRING:
636 /* strings are always 0-terminated */
637 strcpy(ctrl->cur.string, ctrl->string);
639 case V4L2_CTRL_TYPE_INTEGER64:
640 ctrl->cur.val64 = ctrl->val64;
643 ctrl->cur.val = ctrl->val;
648 /* Copy the current value to the new value */
649 static void cur_to_new(struct v4l2_ctrl *ctrl)
653 switch (ctrl->type) {
654 case V4L2_CTRL_TYPE_STRING:
655 /* strings are always 0-terminated */
656 strcpy(ctrl->string, ctrl->cur.string);
658 case V4L2_CTRL_TYPE_INTEGER64:
659 ctrl->val64 = ctrl->cur.val64;
662 ctrl->val = ctrl->cur.val;
667 /* Return non-zero if one or more of the controls in the cluster has a new
668 value that differs from the current value. */
669 static int cluster_changed(struct v4l2_ctrl *master)
674 for (i = 0; !diff && i < master->ncontrols; i++) {
675 struct v4l2_ctrl *ctrl = master->cluster[i];
679 switch (ctrl->type) {
680 case V4L2_CTRL_TYPE_BUTTON:
681 /* Button controls are always 'different' */
683 case V4L2_CTRL_TYPE_STRING:
684 /* strings are always 0-terminated */
685 diff = strcmp(ctrl->string, ctrl->cur.string);
687 case V4L2_CTRL_TYPE_INTEGER64:
688 diff = ctrl->val64 != ctrl->cur.val64;
691 diff = ctrl->val != ctrl->cur.val;
698 /* Validate a new control */
699 static int validate_new(struct v4l2_ctrl *ctrl)
702 char *s = ctrl->string;
706 switch (ctrl->type) {
707 case V4L2_CTRL_TYPE_INTEGER:
708 /* Round towards the closest legal value */
709 val += ctrl->step / 2;
710 if (val < ctrl->minimum)
712 if (val > ctrl->maximum)
714 offset = val - ctrl->minimum;
715 offset = ctrl->step * (offset / ctrl->step);
716 val = ctrl->minimum + offset;
720 case V4L2_CTRL_TYPE_BOOLEAN:
721 ctrl->val = !!ctrl->val;
724 case V4L2_CTRL_TYPE_MENU:
725 if (val < ctrl->minimum || val > ctrl->maximum)
727 if (ctrl->qmenu[val][0] == '\0' ||
728 (ctrl->menu_skip_mask & (1 << val)))
732 case V4L2_CTRL_TYPE_BUTTON:
733 case V4L2_CTRL_TYPE_CTRL_CLASS:
737 case V4L2_CTRL_TYPE_INTEGER64:
740 case V4L2_CTRL_TYPE_STRING:
742 if (len < ctrl->minimum)
744 if ((len - ctrl->minimum) % ctrl->step)
753 static inline u32 node2id(struct list_head *node)
755 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
758 /* Set the handler's error code if it wasn't set earlier already */
759 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
766 /* Initialize the handler */
767 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
768 unsigned nr_of_controls_hint)
770 mutex_init(&hdl->lock);
771 INIT_LIST_HEAD(&hdl->ctrls);
772 INIT_LIST_HEAD(&hdl->ctrl_refs);
773 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
774 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
776 hdl->error = hdl->buckets ? 0 : -ENOMEM;
779 EXPORT_SYMBOL(v4l2_ctrl_handler_init);
781 /* Free all controls and control refs */
782 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
784 struct v4l2_ctrl_ref *ref, *next_ref;
785 struct v4l2_ctrl *ctrl, *next_ctrl;
787 if (hdl == NULL || hdl->buckets == NULL)
790 mutex_lock(&hdl->lock);
792 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
793 list_del(&ref->node);
796 /* Free all controls owned by the handler */
797 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
798 list_del(&ctrl->node);
805 mutex_unlock(&hdl->lock);
807 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
809 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
810 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
811 with applications that do not use the NEXT_CTRL flag.
813 We just find the n-th private user control. It's O(N), but that should not
814 be an issue in this particular case. */
815 static struct v4l2_ctrl_ref *find_private_ref(
816 struct v4l2_ctrl_handler *hdl, u32 id)
818 struct v4l2_ctrl_ref *ref;
820 id -= V4L2_CID_PRIVATE_BASE;
821 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
822 /* Search for private user controls that are compatible with
824 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
825 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
826 if (!type_is_int(ref->ctrl))
836 /* Find a control with the given ID. */
837 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
839 struct v4l2_ctrl_ref *ref;
842 id &= V4L2_CTRL_ID_MASK;
844 /* Old-style private controls need special handling */
845 if (id >= V4L2_CID_PRIVATE_BASE)
846 return find_private_ref(hdl, id);
847 bucket = id % hdl->nr_of_buckets;
849 /* Simple optimization: cache the last control found */
850 if (hdl->cached && hdl->cached->ctrl->id == id)
853 /* Not in cache, search the hash */
854 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
855 while (ref && ref->ctrl->id != id)
859 hdl->cached = ref; /* cache it! */
863 /* Find a control with the given ID. Take the handler's lock first. */
864 static struct v4l2_ctrl_ref *find_ref_lock(
865 struct v4l2_ctrl_handler *hdl, u32 id)
867 struct v4l2_ctrl_ref *ref = NULL;
870 mutex_lock(&hdl->lock);
871 ref = find_ref(hdl, id);
872 mutex_unlock(&hdl->lock);
877 /* Find a control with the given ID. */
878 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
880 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
882 return ref ? ref->ctrl : NULL;
884 EXPORT_SYMBOL(v4l2_ctrl_find);
886 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
887 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
888 struct v4l2_ctrl *ctrl)
890 struct v4l2_ctrl_ref *ref;
891 struct v4l2_ctrl_ref *new_ref;
893 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
894 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
896 /* Automatically add the control class if it is not yet present. */
897 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
898 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
904 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
906 return handler_set_err(hdl, -ENOMEM);
907 new_ref->ctrl = ctrl;
908 if (ctrl->handler == hdl) {
909 /* By default each control starts in a cluster of its own.
910 new_ref->ctrl is basically a cluster array with one
911 element, so that's perfect to use as the cluster pointer.
912 But only do this for the handler that owns the control. */
913 ctrl->cluster = &new_ref->ctrl;
917 INIT_LIST_HEAD(&new_ref->node);
919 mutex_lock(&hdl->lock);
921 /* Add immediately at the end of the list if the list is empty, or if
922 the last element in the list has a lower ID.
923 This ensures that when elements are added in ascending order the
924 insertion is an O(1) operation. */
925 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
926 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
930 /* Find insert position in sorted list */
931 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
932 if (ref->ctrl->id < id)
934 /* Don't add duplicates */
935 if (ref->ctrl->id == id) {
939 list_add(&new_ref->node, ref->node.prev);
944 /* Insert the control node in the hash */
945 new_ref->next = hdl->buckets[bucket];
946 hdl->buckets[bucket] = new_ref;
949 mutex_unlock(&hdl->lock);
953 /* Add a new control */
954 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
955 const struct v4l2_ctrl_ops *ops,
956 u32 id, const char *name, enum v4l2_ctrl_type type,
957 s32 min, s32 max, u32 step, s32 def,
958 u32 flags, const char * const *qmenu, void *priv)
960 struct v4l2_ctrl *ctrl;
961 unsigned sz_extra = 0;
967 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
969 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
970 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
971 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
972 handler_set_err(hdl, -ERANGE);
975 if ((type == V4L2_CTRL_TYPE_INTEGER ||
976 type == V4L2_CTRL_TYPE_MENU ||
977 type == V4L2_CTRL_TYPE_BOOLEAN) &&
978 (def < min || def > max)) {
979 handler_set_err(hdl, -ERANGE);
983 if (type == V4L2_CTRL_TYPE_BUTTON)
984 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
985 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
986 flags |= V4L2_CTRL_FLAG_READ_ONLY;
987 else if (type == V4L2_CTRL_TYPE_STRING)
988 sz_extra += 2 * (max + 1);
990 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
992 handler_set_err(hdl, -ENOMEM);
996 INIT_LIST_HEAD(&ctrl->node);
1002 ctrl->flags = flags;
1003 ctrl->minimum = min;
1004 ctrl->maximum = max;
1006 ctrl->qmenu = qmenu;
1008 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1010 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1011 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1012 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1014 memset(ctrl->cur.string, ' ', ctrl->minimum);
1016 if (handler_new_ref(hdl, ctrl)) {
1020 mutex_lock(&hdl->lock);
1021 list_add_tail(&ctrl->node, &hdl->ctrls);
1022 mutex_unlock(&hdl->lock);
1026 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1027 const struct v4l2_ctrl_config *cfg, void *priv)
1030 struct v4l2_ctrl *ctrl;
1031 const char *name = cfg->name;
1032 const char * const *qmenu = cfg->qmenu;
1033 enum v4l2_ctrl_type type = cfg->type;
1034 u32 flags = cfg->flags;
1037 u32 step = cfg->step;
1041 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1044 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1048 WARN_ON(cfg->menu_skip_mask);
1049 if (is_menu && qmenu == NULL)
1050 qmenu = v4l2_ctrl_get_menu(cfg->id);
1052 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1054 is_menu ? cfg->menu_skip_mask : step,
1055 def, flags, qmenu, priv);
1057 ctrl->is_private = cfg->is_private;
1058 ctrl->is_volatile = cfg->is_volatile;
1062 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1064 /* Helper function for standard non-menu controls */
1065 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1066 const struct v4l2_ctrl_ops *ops,
1067 u32 id, s32 min, s32 max, u32 step, s32 def)
1070 enum v4l2_ctrl_type type;
1073 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1074 if (type == V4L2_CTRL_TYPE_MENU) {
1075 handler_set_err(hdl, -EINVAL);
1078 return v4l2_ctrl_new(hdl, ops, id, name, type,
1079 min, max, step, def, flags, NULL, NULL);
1081 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1083 /* Helper function for standard menu controls */
1084 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1085 const struct v4l2_ctrl_ops *ops,
1086 u32 id, s32 max, s32 mask, s32 def)
1088 const char * const *qmenu = v4l2_ctrl_get_menu(id);
1090 enum v4l2_ctrl_type type;
1095 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1096 if (type != V4L2_CTRL_TYPE_MENU) {
1097 handler_set_err(hdl, -EINVAL);
1100 return v4l2_ctrl_new(hdl, ops, id, name, type,
1101 0, max, mask, def, flags, qmenu, NULL);
1103 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1105 /* Add a control from another handler to this handler */
1106 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1107 struct v4l2_ctrl *ctrl)
1109 if (hdl == NULL || hdl->error)
1112 handler_set_err(hdl, -EINVAL);
1115 if (ctrl->handler == hdl)
1117 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1119 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1121 /* Add the controls from another handler to our own. */
1122 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1123 struct v4l2_ctrl_handler *add)
1125 struct v4l2_ctrl *ctrl;
1128 /* Do nothing if either handler is NULL or if they are the same */
1129 if (!hdl || !add || hdl == add)
1133 mutex_lock(&add->lock);
1134 list_for_each_entry(ctrl, &add->ctrls, node) {
1135 /* Skip handler-private controls. */
1136 if (ctrl->is_private)
1138 ret = handler_new_ref(hdl, ctrl);
1142 mutex_unlock(&add->lock);
1145 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1147 /* Cluster controls */
1148 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1152 /* The first control is the master control and it must not be NULL */
1153 BUG_ON(controls[0] == NULL);
1155 for (i = 0; i < ncontrols; i++) {
1157 controls[i]->cluster = controls;
1158 controls[i]->ncontrols = ncontrols;
1162 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1164 /* Activate/deactivate a control. */
1165 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1171 /* set V4L2_CTRL_FLAG_INACTIVE */
1172 set_bit(4, &ctrl->flags);
1174 /* clear V4L2_CTRL_FLAG_INACTIVE */
1175 clear_bit(4, &ctrl->flags);
1177 EXPORT_SYMBOL(v4l2_ctrl_activate);
1179 /* Grab/ungrab a control.
1180 Typically used when streaming starts and you want to grab controls,
1181 preventing the user from changing them.
1183 Just call this and the framework will block any attempts to change
1185 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1191 /* set V4L2_CTRL_FLAG_GRABBED */
1192 set_bit(1, &ctrl->flags);
1194 /* clear V4L2_CTRL_FLAG_GRABBED */
1195 clear_bit(1, &ctrl->flags);
1197 EXPORT_SYMBOL(v4l2_ctrl_grab);
1199 /* Log the control name and value */
1200 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1201 const char *prefix, const char *colon)
1203 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1204 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1206 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1208 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1211 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1213 switch (ctrl->type) {
1214 case V4L2_CTRL_TYPE_INTEGER:
1215 printk(KERN_CONT "%d", ctrl->cur.val);
1217 case V4L2_CTRL_TYPE_BOOLEAN:
1218 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1220 case V4L2_CTRL_TYPE_MENU:
1221 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1223 case V4L2_CTRL_TYPE_INTEGER64:
1224 printk(KERN_CONT "%lld", ctrl->cur.val64);
1226 case V4L2_CTRL_TYPE_STRING:
1227 printk(KERN_CONT "%s", ctrl->cur.string);
1230 printk(KERN_CONT "unknown type %d", ctrl->type);
1233 if (fl_inact && fl_grabbed)
1234 printk(KERN_CONT " (inactive, grabbed)\n");
1236 printk(KERN_CONT " (inactive)\n");
1237 else if (fl_grabbed)
1238 printk(KERN_CONT " (grabbed)\n");
1240 printk(KERN_CONT "\n");
1243 /* Log all controls owned by the handler */
1244 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1247 struct v4l2_ctrl *ctrl;
1248 const char *colon = "";
1255 len = strlen(prefix);
1256 if (len && prefix[len - 1] != ' ')
1258 mutex_lock(&hdl->lock);
1259 list_for_each_entry(ctrl, &hdl->ctrls, node)
1260 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1261 log_ctrl(ctrl, prefix, colon);
1262 mutex_unlock(&hdl->lock);
1264 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1266 /* Call s_ctrl for all controls owned by the handler */
1267 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1269 struct v4l2_ctrl *ctrl;
1274 mutex_lock(&hdl->lock);
1275 list_for_each_entry(ctrl, &hdl->ctrls, node)
1278 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1279 struct v4l2_ctrl *master = ctrl->cluster[0];
1282 /* Skip if this control was already handled by a cluster. */
1286 for (i = 0; i < master->ncontrols; i++) {
1287 if (master->cluster[i]) {
1288 cur_to_new(master->cluster[i]);
1289 master->cluster[i]->is_new = 1;
1293 /* Skip button controls and read-only controls. */
1294 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1295 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1297 ret = call_op(master, s_ctrl);
1300 for (i = 0; i < master->ncontrols; i++)
1301 if (master->cluster[i])
1302 master->cluster[i]->done = true;
1304 mutex_unlock(&hdl->lock);
1307 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1309 /* Implement VIDIOC_QUERYCTRL */
1310 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1312 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1313 struct v4l2_ctrl_ref *ref;
1314 struct v4l2_ctrl *ctrl;
1319 mutex_lock(&hdl->lock);
1321 /* Try to find it */
1322 ref = find_ref(hdl, id);
1324 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1325 /* Find the next control with ID > qc->id */
1327 /* Did we reach the end of the control list? */
1328 if (id >= node2id(hdl->ctrl_refs.prev)) {
1329 ref = NULL; /* Yes, so there is no next control */
1331 /* We found a control with the given ID, so just get
1332 the next one in the list. */
1333 ref = list_entry(ref->node.next, typeof(*ref), node);
1335 /* No control with the given ID exists, so start
1336 searching for the next largest ID. We know there
1337 is one, otherwise the first 'if' above would have
1339 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1340 if (id < ref->ctrl->id)
1344 mutex_unlock(&hdl->lock);
1349 memset(qc, 0, sizeof(*qc));
1350 if (id >= V4L2_CID_PRIVATE_BASE)
1354 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1355 qc->minimum = ctrl->minimum;
1356 qc->maximum = ctrl->maximum;
1357 qc->default_value = ctrl->default_value;
1358 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
1361 qc->step = ctrl->step;
1362 qc->flags = ctrl->flags;
1363 qc->type = ctrl->type;
1366 EXPORT_SYMBOL(v4l2_queryctrl);
1368 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1370 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1372 return v4l2_queryctrl(sd->ctrl_handler, qc);
1374 EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1376 /* Implement VIDIOC_QUERYMENU */
1377 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1379 struct v4l2_ctrl *ctrl;
1382 ctrl = v4l2_ctrl_find(hdl, qm->id);
1388 if (ctrl->qmenu == NULL ||
1389 i < ctrl->minimum || i > ctrl->maximum)
1391 /* Use mask to see if this menu item should be skipped */
1392 if (ctrl->menu_skip_mask & (1 << i))
1394 /* Empty menu items should also be skipped */
1395 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1397 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1400 EXPORT_SYMBOL(v4l2_querymenu);
1402 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1404 return v4l2_querymenu(sd->ctrl_handler, qm);
1406 EXPORT_SYMBOL(v4l2_subdev_querymenu);
1410 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1412 It is not a fully atomic operation, just best-effort only. After all, if
1413 multiple controls have to be set through multiple i2c writes (for example)
1414 then some initial writes may succeed while others fail. Thus leaving the
1415 system in an inconsistent state. The question is how much effort you are
1416 willing to spend on trying to make something atomic that really isn't.
1418 From the point of view of an application the main requirement is that
1419 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1420 error should be returned without actually affecting any controls.
1422 If all the values are correct, then it is acceptable to just give up
1423 in case of low-level errors.
1425 It is important though that the application can tell when only a partial
1426 configuration was done. The way we do that is through the error_idx field
1427 of struct v4l2_ext_controls: if that is equal to the count field then no
1428 controls were affected. Otherwise all controls before that index were
1429 successful in performing their 'get' or 'set' operation, the control at
1430 the given index failed, and you don't know what happened with the controls
1431 after the failed one. Since if they were part of a control cluster they
1432 could have been successfully processed (if a cluster member was encountered
1433 at index < error_idx), they could have failed (if a cluster member was at
1434 error_idx), or they may not have been processed yet (if the first cluster
1435 member appeared after error_idx).
1437 It is all fairly theoretical, though. In practice all you can do is to
1438 bail out. If error_idx == count, then it is an application bug. If
1439 error_idx < count then it is only an application bug if the error code was
1440 EBUSY. That usually means that something started streaming just when you
1441 tried to set the controls. In all other cases it is a driver/hardware
1442 problem and all you can do is to retry or bail out.
1444 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1445 never modifies controls the error_idx is just set to whatever control
1446 has an invalid value.
1449 /* Prepare for the extended g/s/try functions.
1450 Find the controls in the control array and do some basic checks. */
1451 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1452 struct v4l2_ext_controls *cs,
1453 struct ctrl_helper *helpers)
1457 for (i = 0; i < cs->count; i++) {
1458 struct v4l2_ext_control *c = &cs->controls[i];
1459 struct v4l2_ctrl *ctrl;
1460 u32 id = c->id & V4L2_CTRL_ID_MASK;
1464 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1467 /* Old-style private controls are not allowed for
1468 extended controls */
1469 if (id >= V4L2_CID_PRIVATE_BASE)
1471 ctrl = v4l2_ctrl_find(hdl, id);
1474 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1477 helpers[i].ctrl = ctrl;
1478 helpers[i].handled = false;
1483 typedef int (*cluster_func)(struct v4l2_ext_control *c,
1484 struct v4l2_ctrl *ctrl);
1486 /* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1487 and call the provided function. */
1488 static int cluster_walk(unsigned from,
1489 struct v4l2_ext_controls *cs,
1490 struct ctrl_helper *helpers,
1493 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1497 /* Find any controls from the same cluster and call the function */
1498 for (i = from; !ret && i < cs->count; i++) {
1499 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1501 if (!helpers[i].handled && ctrl->cluster == cluster)
1502 ret = f(&cs->controls[i], ctrl);
1507 static void cluster_done(unsigned from,
1508 struct v4l2_ext_controls *cs,
1509 struct ctrl_helper *helpers)
1511 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1514 /* Find any controls from the same cluster and mark them as handled */
1515 for (i = from; i < cs->count; i++)
1516 if (helpers[i].ctrl->cluster == cluster)
1517 helpers[i].handled = true;
1520 /* Handles the corner case where cs->count == 0. It checks whether the
1521 specified control class exists. If that class ID is 0, then it checks
1522 whether there are any controls at all. */
1523 static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1525 if (ctrl_class == 0)
1526 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1527 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1532 /* Get extended controls. Allocates the helpers array if needed. */
1533 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1535 struct ctrl_helper helper[4];
1536 struct ctrl_helper *helpers = helper;
1540 cs->error_idx = cs->count;
1541 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1547 return class_check(hdl, cs->ctrl_class);
1549 if (cs->count > ARRAY_SIZE(helper)) {
1550 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1551 if (helpers == NULL)
1555 ret = prepare_ext_ctrls(hdl, cs, helpers);
1556 cs->error_idx = cs->count;
1558 for (i = 0; !ret && i < cs->count; i++)
1559 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1562 for (i = 0; !ret && i < cs->count; i++) {
1563 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1564 struct v4l2_ctrl *master = ctrl->cluster[0];
1566 if (helpers[i].handled)
1571 v4l2_ctrl_lock(master);
1572 /* g_volatile_ctrl will update the current control values */
1573 if (ctrl->is_volatile)
1574 ret = call_op(master, g_volatile_ctrl);
1575 /* If OK, then copy the current control values to the caller */
1577 ret = cluster_walk(i, cs, helpers, cur_to_user);
1578 v4l2_ctrl_unlock(master);
1579 cluster_done(i, cs, helpers);
1582 if (cs->count > ARRAY_SIZE(helper))
1586 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1588 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1590 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1592 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1594 /* Helper function to get a single control */
1595 static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1597 struct v4l2_ctrl *master = ctrl->cluster[0];
1600 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1603 v4l2_ctrl_lock(master);
1604 /* g_volatile_ctrl will update the current control values */
1605 if (ctrl->is_volatile)
1606 ret = call_op(master, g_volatile_ctrl);
1607 *val = ctrl->cur.val;
1608 v4l2_ctrl_unlock(master);
1612 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1614 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1616 if (ctrl == NULL || !type_is_int(ctrl))
1618 return get_ctrl(ctrl, &control->value);
1620 EXPORT_SYMBOL(v4l2_g_ctrl);
1622 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1624 return v4l2_g_ctrl(sd->ctrl_handler, control);
1626 EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1628 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1632 /* It's a driver bug if this happens. */
1633 WARN_ON(!type_is_int(ctrl));
1634 get_ctrl(ctrl, &val);
1637 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1640 /* Core function that calls try/s_ctrl and ensures that the new value is
1641 copied to the current value on a set.
1642 Must be called with ctrl->handler->lock held. */
1643 static int try_or_set_control_cluster(struct v4l2_ctrl *master, bool set)
1649 /* Go through the cluster and either validate the new value or
1650 (if no new value was set), copy the current value to the new
1651 value, ensuring a consistent view for the control ops when
1653 for (i = 0; !ret && i < master->ncontrols; i++) {
1654 struct v4l2_ctrl *ctrl = master->cluster[i];
1660 /* Double check this: it may have changed since the
1661 last check in try_or_set_ext_ctrls(). */
1662 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1665 /* Validate if required */
1667 ret = validate_new(ctrl);
1670 /* No new value was set, so copy the current and force
1671 a call to try_ctrl later, since the values for the cluster
1672 may now have changed and the end result might be invalid. */
1677 /* For larger clusters you have to call try_ctrl again to
1678 verify that the controls are still valid after the
1679 'cur_to_new' above. */
1681 ret = call_op(master, try_ctrl);
1683 /* Don't set if there is no change */
1684 if (!ret && set && cluster_changed(master)) {
1685 ret = call_op(master, s_ctrl);
1686 /* If OK, then make the new values permanent. */
1688 for (i = 0; i < master->ncontrols; i++)
1689 new_to_cur(master->cluster[i]);
1694 /* Try or set controls. */
1695 static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1696 struct v4l2_ext_controls *cs,
1697 struct ctrl_helper *helpers,
1703 for (i = 0; i < cs->count; i++) {
1704 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1708 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1710 /* This test is also done in try_set_control_cluster() which
1711 is called in atomic context, so that has the final say,
1712 but it makes sense to do an up-front check as well. Once
1713 an error occurs in try_set_control_cluster() some other
1714 controls may have been set already and we want to do a
1715 best-effort to avoid that. */
1716 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1720 for (i = 0; !ret && i < cs->count; i++) {
1721 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1722 struct v4l2_ctrl *master = ctrl->cluster[0];
1724 if (helpers[i].handled)
1728 v4l2_ctrl_lock(ctrl);
1730 /* Reset the 'is_new' flags of the cluster */
1731 for (j = 0; j < master->ncontrols; j++)
1732 if (master->cluster[j])
1733 master->cluster[j]->is_new = 0;
1735 /* Copy the new caller-supplied control values.
1736 user_to_new() sets 'is_new' to 1. */
1737 ret = cluster_walk(i, cs, helpers, user_to_new);
1740 ret = try_or_set_control_cluster(master, set);
1742 /* Copy the new values back to userspace. */
1744 ret = cluster_walk(i, cs, helpers, new_to_user);
1746 v4l2_ctrl_unlock(ctrl);
1747 cluster_done(i, cs, helpers);
1752 /* Try or try-and-set controls */
1753 static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1754 struct v4l2_ext_controls *cs,
1757 struct ctrl_helper helper[4];
1758 struct ctrl_helper *helpers = helper;
1762 cs->error_idx = cs->count;
1763 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1769 return class_check(hdl, cs->ctrl_class);
1771 if (cs->count > ARRAY_SIZE(helper)) {
1772 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1776 ret = prepare_ext_ctrls(hdl, cs, helpers);
1778 /* First 'try' all controls and abort on error */
1780 ret = try_or_set_ext_ctrls(hdl, cs, helpers, false);
1781 /* If this is a 'set' operation and the initial 'try' failed,
1782 then set error_idx to count to tell the application that no
1783 controls changed value yet. */
1785 cs->error_idx = cs->count;
1787 /* Reset 'handled' state */
1788 for (i = 0; i < cs->count; i++)
1789 helpers[i].handled = false;
1790 ret = try_or_set_ext_ctrls(hdl, cs, helpers, true);
1793 if (cs->count > ARRAY_SIZE(helper))
1798 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1800 return try_set_ext_ctrls(hdl, cs, false);
1802 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1804 int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1806 return try_set_ext_ctrls(hdl, cs, true);
1808 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1810 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1812 return try_set_ext_ctrls(sd->ctrl_handler, cs, false);
1814 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1816 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1818 return try_set_ext_ctrls(sd->ctrl_handler, cs, true);
1820 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1822 /* Helper function for VIDIOC_S_CTRL compatibility */
1823 static int set_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1825 struct v4l2_ctrl *master = ctrl->cluster[0];
1829 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1832 v4l2_ctrl_lock(ctrl);
1834 /* Reset the 'is_new' flags of the cluster */
1835 for (i = 0; i < master->ncontrols; i++)
1836 if (master->cluster[i])
1837 master->cluster[i]->is_new = 0;
1841 ret = try_or_set_control_cluster(master, false);
1843 ret = try_or_set_control_cluster(master, true);
1844 *val = ctrl->cur.val;
1845 v4l2_ctrl_unlock(ctrl);
1849 int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1851 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1853 if (ctrl == NULL || !type_is_int(ctrl))
1856 return set_ctrl(ctrl, &control->value);
1858 EXPORT_SYMBOL(v4l2_s_ctrl);
1860 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1862 return v4l2_s_ctrl(sd->ctrl_handler, control);
1864 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
1866 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
1868 /* It's a driver bug if this happens. */
1869 WARN_ON(!type_is_int(ctrl));
1870 return set_ctrl(ctrl, &val);
1872 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);