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 has_op(master, op) \
29 (master->ops && master->ops->op)
30 #define call_op(master, op) \
31 (has_op(master, op) ? master->ops->op(master) : 0)
33 /* Internal temporary helper struct, one for each v4l2_ext_control */
35 /* The control corresponding to the v4l2_ext_control ID field. */
36 struct v4l2_ctrl *ctrl;
37 /* Used internally to mark whether this control was already
42 /* Returns NULL or a character pointer array containing the menu for
43 the given control ID. The pointer array ends with a NULL pointer.
44 An empty string signifies a menu entry that is invalid. This allows
45 drivers to disable certain options if it is not supported. */
46 const char * const *v4l2_ctrl_get_menu(u32 id)
48 static const char * const mpeg_audio_sampling_freq[] = {
54 static const char * const mpeg_audio_encoding[] = {
62 static const char * const mpeg_audio_l1_bitrate[] = {
79 static const char * const mpeg_audio_l2_bitrate[] = {
96 static const char * const mpeg_audio_l3_bitrate[] = {
113 static const char * const mpeg_audio_ac3_bitrate[] = {
135 static const char * const mpeg_audio_mode[] = {
142 static const char * const mpeg_audio_mode_extension[] = {
149 static const char * const mpeg_audio_emphasis[] = {
155 static const char * const mpeg_audio_crc[] = {
160 static const char * const mpeg_video_encoding[] = {
166 static const char * const mpeg_video_aspect[] = {
173 static const char * const mpeg_video_bitrate_mode[] = {
178 static const char * const mpeg_stream_type[] = {
179 "MPEG-2 Program Stream",
180 "MPEG-2 Transport Stream",
181 "MPEG-1 System Stream",
182 "MPEG-2 DVD-compatible Stream",
183 "MPEG-1 VCD-compatible Stream",
184 "MPEG-2 SVCD-compatible Stream",
187 static const char * const mpeg_stream_vbi_fmt[] = {
189 "Private packet, IVTV format",
192 static const char * const camera_power_line_frequency[] = {
198 static const char * const camera_exposure_auto[] = {
201 "Shutter Priority Mode",
202 "Aperture Priority Mode",
205 static const char * const colorfx[] = {
218 static const char * const tune_preemphasis[] = {
226 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
227 return mpeg_audio_sampling_freq;
228 case V4L2_CID_MPEG_AUDIO_ENCODING:
229 return mpeg_audio_encoding;
230 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
231 return mpeg_audio_l1_bitrate;
232 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
233 return mpeg_audio_l2_bitrate;
234 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
235 return mpeg_audio_l3_bitrate;
236 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
237 return mpeg_audio_ac3_bitrate;
238 case V4L2_CID_MPEG_AUDIO_MODE:
239 return mpeg_audio_mode;
240 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
241 return mpeg_audio_mode_extension;
242 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
243 return mpeg_audio_emphasis;
244 case V4L2_CID_MPEG_AUDIO_CRC:
245 return mpeg_audio_crc;
246 case V4L2_CID_MPEG_VIDEO_ENCODING:
247 return mpeg_video_encoding;
248 case V4L2_CID_MPEG_VIDEO_ASPECT:
249 return mpeg_video_aspect;
250 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
251 return mpeg_video_bitrate_mode;
252 case V4L2_CID_MPEG_STREAM_TYPE:
253 return mpeg_stream_type;
254 case V4L2_CID_MPEG_STREAM_VBI_FMT:
255 return mpeg_stream_vbi_fmt;
256 case V4L2_CID_POWER_LINE_FREQUENCY:
257 return camera_power_line_frequency;
258 case V4L2_CID_EXPOSURE_AUTO:
259 return camera_exposure_auto;
260 case V4L2_CID_COLORFX:
262 case V4L2_CID_TUNE_PREEMPHASIS:
263 return tune_preemphasis;
268 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
270 /* Return the control name. */
271 const char *v4l2_ctrl_get_name(u32 id)
275 /* Keep the order of the 'case's the same as in videodev2.h! */
276 case V4L2_CID_USER_CLASS: return "User Controls";
277 case V4L2_CID_BRIGHTNESS: return "Brightness";
278 case V4L2_CID_CONTRAST: return "Contrast";
279 case V4L2_CID_SATURATION: return "Saturation";
280 case V4L2_CID_HUE: return "Hue";
281 case V4L2_CID_AUDIO_VOLUME: return "Volume";
282 case V4L2_CID_AUDIO_BALANCE: return "Balance";
283 case V4L2_CID_AUDIO_BASS: return "Bass";
284 case V4L2_CID_AUDIO_TREBLE: return "Treble";
285 case V4L2_CID_AUDIO_MUTE: return "Mute";
286 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
287 case V4L2_CID_BLACK_LEVEL: return "Black Level";
288 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
289 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
290 case V4L2_CID_RED_BALANCE: return "Red Balance";
291 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
292 case V4L2_CID_GAMMA: return "Gamma";
293 case V4L2_CID_EXPOSURE: return "Exposure";
294 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
295 case V4L2_CID_GAIN: return "Gain";
296 case V4L2_CID_HFLIP: return "Horizontal Flip";
297 case V4L2_CID_VFLIP: return "Vertical Flip";
298 case V4L2_CID_HCENTER: return "Horizontal Center";
299 case V4L2_CID_VCENTER: return "Vertical Center";
300 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
301 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
302 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
303 case V4L2_CID_SHARPNESS: return "Sharpness";
304 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
305 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
306 case V4L2_CID_COLOR_KILLER: return "Color Killer";
307 case V4L2_CID_COLORFX: return "Color Effects";
308 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
309 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
310 case V4L2_CID_ROTATE: return "Rotate";
311 case V4L2_CID_BG_COLOR: return "Background Color";
312 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
313 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
314 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
317 /* Keep the order of the 'case's the same as in videodev2.h! */
318 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
319 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
320 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
321 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
322 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
323 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
324 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
325 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
326 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
327 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
328 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
329 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
330 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
331 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
332 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
333 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
334 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
335 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
336 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
337 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
338 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
339 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
340 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
341 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
342 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
343 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
344 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
345 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
346 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
347 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
348 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
349 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
350 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
352 /* CAMERA controls */
353 /* Keep the order of the 'case's the same as in videodev2.h! */
354 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
355 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
356 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
357 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
358 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
359 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
360 case V4L2_CID_PAN_RESET: return "Pan, Reset";
361 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
362 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
363 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
364 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
365 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
366 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
367 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
368 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
369 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
370 case V4L2_CID_PRIVACY: return "Privacy";
371 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
372 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
374 /* FM Radio Modulator control */
375 /* Keep the order of the 'case's the same as in videodev2.h! */
376 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
377 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
378 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
379 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
380 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
381 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
382 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
383 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
384 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
385 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
386 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
387 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
388 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
389 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
390 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
391 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
392 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
393 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
394 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
395 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
401 EXPORT_SYMBOL(v4l2_ctrl_get_name);
403 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
404 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
406 *name = v4l2_ctrl_get_name(id);
410 case V4L2_CID_AUDIO_MUTE:
411 case V4L2_CID_AUDIO_LOUDNESS:
412 case V4L2_CID_AUTO_WHITE_BALANCE:
413 case V4L2_CID_AUTOGAIN:
416 case V4L2_CID_HUE_AUTO:
417 case V4L2_CID_CHROMA_AGC:
418 case V4L2_CID_COLOR_KILLER:
419 case V4L2_CID_MPEG_AUDIO_MUTE:
420 case V4L2_CID_MPEG_VIDEO_MUTE:
421 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
422 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
423 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
424 case V4L2_CID_FOCUS_AUTO:
425 case V4L2_CID_PRIVACY:
426 case V4L2_CID_AUDIO_LIMITER_ENABLED:
427 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
428 case V4L2_CID_PILOT_TONE_ENABLED:
429 case V4L2_CID_ILLUMINATORS_1:
430 case V4L2_CID_ILLUMINATORS_2:
431 *type = V4L2_CTRL_TYPE_BOOLEAN;
435 case V4L2_CID_PAN_RESET:
436 case V4L2_CID_TILT_RESET:
437 *type = V4L2_CTRL_TYPE_BUTTON;
438 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
439 *min = *max = *step = *def = 0;
441 case V4L2_CID_POWER_LINE_FREQUENCY:
442 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
443 case V4L2_CID_MPEG_AUDIO_ENCODING:
444 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
445 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
446 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
447 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
448 case V4L2_CID_MPEG_AUDIO_MODE:
449 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
450 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
451 case V4L2_CID_MPEG_AUDIO_CRC:
452 case V4L2_CID_MPEG_VIDEO_ENCODING:
453 case V4L2_CID_MPEG_VIDEO_ASPECT:
454 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
455 case V4L2_CID_MPEG_STREAM_TYPE:
456 case V4L2_CID_MPEG_STREAM_VBI_FMT:
457 case V4L2_CID_EXPOSURE_AUTO:
458 case V4L2_CID_COLORFX:
459 case V4L2_CID_TUNE_PREEMPHASIS:
460 *type = V4L2_CTRL_TYPE_MENU;
462 case V4L2_CID_RDS_TX_PS_NAME:
463 case V4L2_CID_RDS_TX_RADIO_TEXT:
464 *type = V4L2_CTRL_TYPE_STRING;
466 case V4L2_CID_USER_CLASS:
467 case V4L2_CID_CAMERA_CLASS:
468 case V4L2_CID_MPEG_CLASS:
469 case V4L2_CID_FM_TX_CLASS:
470 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
471 /* You can neither read not write these */
472 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
473 *min = *max = *step = *def = 0;
475 case V4L2_CID_BG_COLOR:
476 *type = V4L2_CTRL_TYPE_INTEGER;
479 /* Max is calculated as RGB888 that is 2^24 */
483 *type = V4L2_CTRL_TYPE_INTEGER;
487 case V4L2_CID_MPEG_AUDIO_ENCODING:
488 case V4L2_CID_MPEG_AUDIO_MODE:
489 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
490 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
491 case V4L2_CID_MPEG_STREAM_TYPE:
492 *flags |= V4L2_CTRL_FLAG_UPDATE;
494 case V4L2_CID_AUDIO_VOLUME:
495 case V4L2_CID_AUDIO_BALANCE:
496 case V4L2_CID_AUDIO_BASS:
497 case V4L2_CID_AUDIO_TREBLE:
498 case V4L2_CID_BRIGHTNESS:
499 case V4L2_CID_CONTRAST:
500 case V4L2_CID_SATURATION:
502 case V4L2_CID_RED_BALANCE:
503 case V4L2_CID_BLUE_BALANCE:
505 case V4L2_CID_SHARPNESS:
506 case V4L2_CID_CHROMA_GAIN:
507 case V4L2_CID_RDS_TX_DEVIATION:
508 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
509 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
510 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
511 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
512 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
513 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
514 case V4L2_CID_PILOT_TONE_DEVIATION:
515 case V4L2_CID_PILOT_TONE_FREQUENCY:
516 case V4L2_CID_TUNE_POWER_LEVEL:
517 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
518 *flags |= V4L2_CTRL_FLAG_SLIDER;
520 case V4L2_CID_PAN_RELATIVE:
521 case V4L2_CID_TILT_RELATIVE:
522 case V4L2_CID_FOCUS_RELATIVE:
523 case V4L2_CID_IRIS_RELATIVE:
524 case V4L2_CID_ZOOM_RELATIVE:
525 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
529 EXPORT_SYMBOL(v4l2_ctrl_fill);
531 /* Helper function to determine whether the control type is compatible with
533 static bool type_is_int(const struct v4l2_ctrl *ctrl)
535 switch (ctrl->type) {
536 case V4L2_CTRL_TYPE_INTEGER64:
537 case V4L2_CTRL_TYPE_STRING:
538 /* Nope, these need v4l2_ext_control */
545 /* Helper function: copy the current control value back to the caller */
546 static int cur_to_user(struct v4l2_ext_control *c,
547 struct v4l2_ctrl *ctrl)
551 switch (ctrl->type) {
552 case V4L2_CTRL_TYPE_STRING:
553 len = strlen(ctrl->cur.string);
554 if (c->size < len + 1) {
558 return copy_to_user(c->string, ctrl->cur.string,
559 len + 1) ? -EFAULT : 0;
560 case V4L2_CTRL_TYPE_INTEGER64:
561 c->value64 = ctrl->cur.val64;
564 c->value = ctrl->cur.val;
570 /* Helper function: copy the caller-provider value as the new control value */
571 static int user_to_new(struct v4l2_ext_control *c,
572 struct v4l2_ctrl *ctrl)
578 switch (ctrl->type) {
579 case V4L2_CTRL_TYPE_INTEGER64:
580 ctrl->val64 = c->value64;
582 case V4L2_CTRL_TYPE_STRING:
586 if (size > ctrl->maximum + 1)
587 size = ctrl->maximum + 1;
588 ret = copy_from_user(ctrl->string, c->string, size);
590 char last = ctrl->string[size - 1];
592 ctrl->string[size - 1] = 0;
593 /* If the string was longer than ctrl->maximum,
594 then return an error. */
595 if (strlen(ctrl->string) == ctrl->maximum && last)
598 return ret ? -EFAULT : 0;
600 ctrl->val = c->value;
606 /* Helper function: copy the new control value back to the caller */
607 static int new_to_user(struct v4l2_ext_control *c,
608 struct v4l2_ctrl *ctrl)
612 switch (ctrl->type) {
613 case V4L2_CTRL_TYPE_STRING:
614 len = strlen(ctrl->string);
615 if (c->size < len + 1) {
616 c->size = ctrl->maximum + 1;
619 return copy_to_user(c->string, ctrl->string,
620 len + 1) ? -EFAULT : 0;
621 case V4L2_CTRL_TYPE_INTEGER64:
622 c->value64 = ctrl->val64;
625 c->value = ctrl->val;
631 static int ctrl_to_user(struct v4l2_ext_control *c,
632 struct v4l2_ctrl *ctrl)
634 if (ctrl->is_volatile)
635 return new_to_user(c, ctrl);
636 return cur_to_user(c, ctrl);
639 static int ctrl_is_volatile(struct v4l2_ext_control *c,
640 struct v4l2_ctrl *ctrl)
642 return ctrl->is_volatile;
645 /* Copy the new value to the current value. */
646 static void new_to_cur(struct v4l2_ctrl *ctrl)
650 switch (ctrl->type) {
651 case V4L2_CTRL_TYPE_STRING:
652 /* strings are always 0-terminated */
653 strcpy(ctrl->cur.string, ctrl->string);
655 case V4L2_CTRL_TYPE_INTEGER64:
656 ctrl->cur.val64 = ctrl->val64;
659 ctrl->cur.val = ctrl->val;
664 /* Copy the current value to the new value */
665 static void cur_to_new(struct v4l2_ctrl *ctrl)
669 switch (ctrl->type) {
670 case V4L2_CTRL_TYPE_STRING:
671 /* strings are always 0-terminated */
672 strcpy(ctrl->string, ctrl->cur.string);
674 case V4L2_CTRL_TYPE_INTEGER64:
675 ctrl->val64 = ctrl->cur.val64;
678 ctrl->val = ctrl->cur.val;
683 /* Return non-zero if one or more of the controls in the cluster has a new
684 value that differs from the current value. */
685 static int cluster_changed(struct v4l2_ctrl *master)
690 for (i = 0; !diff && i < master->ncontrols; i++) {
691 struct v4l2_ctrl *ctrl = master->cluster[i];
695 switch (ctrl->type) {
696 case V4L2_CTRL_TYPE_BUTTON:
697 /* Button controls are always 'different' */
699 case V4L2_CTRL_TYPE_STRING:
700 /* strings are always 0-terminated */
701 diff = strcmp(ctrl->string, ctrl->cur.string);
703 case V4L2_CTRL_TYPE_INTEGER64:
704 diff = ctrl->val64 != ctrl->cur.val64;
707 diff = ctrl->val != ctrl->cur.val;
714 /* Validate a new control */
715 static int validate_new(struct v4l2_ctrl *ctrl)
718 char *s = ctrl->string;
722 switch (ctrl->type) {
723 case V4L2_CTRL_TYPE_INTEGER:
724 /* Round towards the closest legal value */
725 val += ctrl->step / 2;
726 if (val < ctrl->minimum)
728 if (val > ctrl->maximum)
730 offset = val - ctrl->minimum;
731 offset = ctrl->step * (offset / ctrl->step);
732 val = ctrl->minimum + offset;
736 case V4L2_CTRL_TYPE_BOOLEAN:
737 ctrl->val = !!ctrl->val;
740 case V4L2_CTRL_TYPE_MENU:
741 if (val < ctrl->minimum || val > ctrl->maximum)
743 if (ctrl->qmenu[val][0] == '\0' ||
744 (ctrl->menu_skip_mask & (1 << val)))
748 case V4L2_CTRL_TYPE_BUTTON:
749 case V4L2_CTRL_TYPE_CTRL_CLASS:
753 case V4L2_CTRL_TYPE_INTEGER64:
756 case V4L2_CTRL_TYPE_STRING:
758 if (len < ctrl->minimum)
760 if ((len - ctrl->minimum) % ctrl->step)
769 static inline u32 node2id(struct list_head *node)
771 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
774 /* Set the handler's error code if it wasn't set earlier already */
775 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
782 /* Initialize the handler */
783 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
784 unsigned nr_of_controls_hint)
786 mutex_init(&hdl->lock);
787 INIT_LIST_HEAD(&hdl->ctrls);
788 INIT_LIST_HEAD(&hdl->ctrl_refs);
789 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
790 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
792 hdl->error = hdl->buckets ? 0 : -ENOMEM;
795 EXPORT_SYMBOL(v4l2_ctrl_handler_init);
797 /* Free all controls and control refs */
798 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
800 struct v4l2_ctrl_ref *ref, *next_ref;
801 struct v4l2_ctrl *ctrl, *next_ctrl;
803 if (hdl == NULL || hdl->buckets == NULL)
806 mutex_lock(&hdl->lock);
808 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
809 list_del(&ref->node);
812 /* Free all controls owned by the handler */
813 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
814 list_del(&ctrl->node);
821 mutex_unlock(&hdl->lock);
823 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
825 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
826 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
827 with applications that do not use the NEXT_CTRL flag.
829 We just find the n-th private user control. It's O(N), but that should not
830 be an issue in this particular case. */
831 static struct v4l2_ctrl_ref *find_private_ref(
832 struct v4l2_ctrl_handler *hdl, u32 id)
834 struct v4l2_ctrl_ref *ref;
836 id -= V4L2_CID_PRIVATE_BASE;
837 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
838 /* Search for private user controls that are compatible with
840 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
841 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
842 if (!type_is_int(ref->ctrl))
852 /* Find a control with the given ID. */
853 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
855 struct v4l2_ctrl_ref *ref;
858 id &= V4L2_CTRL_ID_MASK;
860 /* Old-style private controls need special handling */
861 if (id >= V4L2_CID_PRIVATE_BASE)
862 return find_private_ref(hdl, id);
863 bucket = id % hdl->nr_of_buckets;
865 /* Simple optimization: cache the last control found */
866 if (hdl->cached && hdl->cached->ctrl->id == id)
869 /* Not in cache, search the hash */
870 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
871 while (ref && ref->ctrl->id != id)
875 hdl->cached = ref; /* cache it! */
879 /* Find a control with the given ID. Take the handler's lock first. */
880 static struct v4l2_ctrl_ref *find_ref_lock(
881 struct v4l2_ctrl_handler *hdl, u32 id)
883 struct v4l2_ctrl_ref *ref = NULL;
886 mutex_lock(&hdl->lock);
887 ref = find_ref(hdl, id);
888 mutex_unlock(&hdl->lock);
893 /* Find a control with the given ID. */
894 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
896 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
898 return ref ? ref->ctrl : NULL;
900 EXPORT_SYMBOL(v4l2_ctrl_find);
902 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
903 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
904 struct v4l2_ctrl *ctrl)
906 struct v4l2_ctrl_ref *ref;
907 struct v4l2_ctrl_ref *new_ref;
909 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
910 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
912 /* Automatically add the control class if it is not yet present. */
913 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
914 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
920 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
922 return handler_set_err(hdl, -ENOMEM);
923 new_ref->ctrl = ctrl;
924 if (ctrl->handler == hdl) {
925 /* By default each control starts in a cluster of its own.
926 new_ref->ctrl is basically a cluster array with one
927 element, so that's perfect to use as the cluster pointer.
928 But only do this for the handler that owns the control. */
929 ctrl->cluster = &new_ref->ctrl;
933 INIT_LIST_HEAD(&new_ref->node);
935 mutex_lock(&hdl->lock);
937 /* Add immediately at the end of the list if the list is empty, or if
938 the last element in the list has a lower ID.
939 This ensures that when elements are added in ascending order the
940 insertion is an O(1) operation. */
941 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
942 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
946 /* Find insert position in sorted list */
947 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
948 if (ref->ctrl->id < id)
950 /* Don't add duplicates */
951 if (ref->ctrl->id == id) {
955 list_add(&new_ref->node, ref->node.prev);
960 /* Insert the control node in the hash */
961 new_ref->next = hdl->buckets[bucket];
962 hdl->buckets[bucket] = new_ref;
965 mutex_unlock(&hdl->lock);
969 /* Add a new control */
970 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
971 const struct v4l2_ctrl_ops *ops,
972 u32 id, const char *name, enum v4l2_ctrl_type type,
973 s32 min, s32 max, u32 step, s32 def,
974 u32 flags, const char * const *qmenu, void *priv)
976 struct v4l2_ctrl *ctrl;
977 unsigned sz_extra = 0;
983 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
985 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
986 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
987 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
988 handler_set_err(hdl, -ERANGE);
991 if ((type == V4L2_CTRL_TYPE_INTEGER ||
992 type == V4L2_CTRL_TYPE_MENU ||
993 type == V4L2_CTRL_TYPE_BOOLEAN) &&
994 (def < min || def > max)) {
995 handler_set_err(hdl, -ERANGE);
999 if (type == V4L2_CTRL_TYPE_BUTTON)
1000 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1001 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1002 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1003 else if (type == V4L2_CTRL_TYPE_STRING)
1004 sz_extra += 2 * (max + 1);
1006 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1008 handler_set_err(hdl, -ENOMEM);
1012 INIT_LIST_HEAD(&ctrl->node);
1013 ctrl->handler = hdl;
1018 ctrl->flags = flags;
1019 ctrl->minimum = min;
1020 ctrl->maximum = max;
1022 ctrl->qmenu = qmenu;
1024 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1026 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1027 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1028 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1030 memset(ctrl->cur.string, ' ', ctrl->minimum);
1032 if (handler_new_ref(hdl, ctrl)) {
1036 mutex_lock(&hdl->lock);
1037 list_add_tail(&ctrl->node, &hdl->ctrls);
1038 mutex_unlock(&hdl->lock);
1042 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1043 const struct v4l2_ctrl_config *cfg, void *priv)
1046 struct v4l2_ctrl *ctrl;
1047 const char *name = cfg->name;
1048 const char * const *qmenu = cfg->qmenu;
1049 enum v4l2_ctrl_type type = cfg->type;
1050 u32 flags = cfg->flags;
1053 u32 step = cfg->step;
1057 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1060 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1064 WARN_ON(cfg->menu_skip_mask);
1065 if (is_menu && qmenu == NULL)
1066 qmenu = v4l2_ctrl_get_menu(cfg->id);
1068 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1070 is_menu ? cfg->menu_skip_mask : step,
1071 def, flags, qmenu, priv);
1073 ctrl->is_private = cfg->is_private;
1074 ctrl->is_volatile = cfg->is_volatile;
1078 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1080 /* Helper function for standard non-menu controls */
1081 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1082 const struct v4l2_ctrl_ops *ops,
1083 u32 id, s32 min, s32 max, u32 step, s32 def)
1086 enum v4l2_ctrl_type type;
1089 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1090 if (type == V4L2_CTRL_TYPE_MENU) {
1091 handler_set_err(hdl, -EINVAL);
1094 return v4l2_ctrl_new(hdl, ops, id, name, type,
1095 min, max, step, def, flags, NULL, NULL);
1097 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1099 /* Helper function for standard menu controls */
1100 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1101 const struct v4l2_ctrl_ops *ops,
1102 u32 id, s32 max, s32 mask, s32 def)
1104 const char * const *qmenu = v4l2_ctrl_get_menu(id);
1106 enum v4l2_ctrl_type type;
1111 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1112 if (type != V4L2_CTRL_TYPE_MENU) {
1113 handler_set_err(hdl, -EINVAL);
1116 return v4l2_ctrl_new(hdl, ops, id, name, type,
1117 0, max, mask, def, flags, qmenu, NULL);
1119 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1121 /* Add a control from another handler to this handler */
1122 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1123 struct v4l2_ctrl *ctrl)
1125 if (hdl == NULL || hdl->error)
1128 handler_set_err(hdl, -EINVAL);
1131 if (ctrl->handler == hdl)
1133 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1135 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1137 /* Add the controls from another handler to our own. */
1138 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1139 struct v4l2_ctrl_handler *add)
1141 struct v4l2_ctrl *ctrl;
1144 /* Do nothing if either handler is NULL or if they are the same */
1145 if (!hdl || !add || hdl == add)
1149 mutex_lock(&add->lock);
1150 list_for_each_entry(ctrl, &add->ctrls, node) {
1151 /* Skip handler-private controls. */
1152 if (ctrl->is_private)
1154 ret = handler_new_ref(hdl, ctrl);
1158 mutex_unlock(&add->lock);
1161 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1163 /* Cluster controls */
1164 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1168 /* The first control is the master control and it must not be NULL */
1169 BUG_ON(controls[0] == NULL);
1171 for (i = 0; i < ncontrols; i++) {
1173 controls[i]->cluster = controls;
1174 controls[i]->ncontrols = ncontrols;
1178 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1180 /* Activate/deactivate a control. */
1181 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1187 /* set V4L2_CTRL_FLAG_INACTIVE */
1188 set_bit(4, &ctrl->flags);
1190 /* clear V4L2_CTRL_FLAG_INACTIVE */
1191 clear_bit(4, &ctrl->flags);
1193 EXPORT_SYMBOL(v4l2_ctrl_activate);
1195 /* Grab/ungrab a control.
1196 Typically used when streaming starts and you want to grab controls,
1197 preventing the user from changing them.
1199 Just call this and the framework will block any attempts to change
1201 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1207 /* set V4L2_CTRL_FLAG_GRABBED */
1208 set_bit(1, &ctrl->flags);
1210 /* clear V4L2_CTRL_FLAG_GRABBED */
1211 clear_bit(1, &ctrl->flags);
1213 EXPORT_SYMBOL(v4l2_ctrl_grab);
1215 /* Log the control name and value */
1216 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1217 const char *prefix, const char *colon)
1219 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1220 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1222 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1224 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1227 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1229 switch (ctrl->type) {
1230 case V4L2_CTRL_TYPE_INTEGER:
1231 printk(KERN_CONT "%d", ctrl->cur.val);
1233 case V4L2_CTRL_TYPE_BOOLEAN:
1234 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1236 case V4L2_CTRL_TYPE_MENU:
1237 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1239 case V4L2_CTRL_TYPE_INTEGER64:
1240 printk(KERN_CONT "%lld", ctrl->cur.val64);
1242 case V4L2_CTRL_TYPE_STRING:
1243 printk(KERN_CONT "%s", ctrl->cur.string);
1246 printk(KERN_CONT "unknown type %d", ctrl->type);
1249 if (fl_inact && fl_grabbed)
1250 printk(KERN_CONT " (inactive, grabbed)\n");
1252 printk(KERN_CONT " (inactive)\n");
1253 else if (fl_grabbed)
1254 printk(KERN_CONT " (grabbed)\n");
1256 printk(KERN_CONT "\n");
1259 /* Log all controls owned by the handler */
1260 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1263 struct v4l2_ctrl *ctrl;
1264 const char *colon = "";
1271 len = strlen(prefix);
1272 if (len && prefix[len - 1] != ' ')
1274 mutex_lock(&hdl->lock);
1275 list_for_each_entry(ctrl, &hdl->ctrls, node)
1276 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1277 log_ctrl(ctrl, prefix, colon);
1278 mutex_unlock(&hdl->lock);
1280 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1282 /* Call s_ctrl for all controls owned by the handler */
1283 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1285 struct v4l2_ctrl *ctrl;
1290 mutex_lock(&hdl->lock);
1291 list_for_each_entry(ctrl, &hdl->ctrls, node)
1294 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1295 struct v4l2_ctrl *master = ctrl->cluster[0];
1298 /* Skip if this control was already handled by a cluster. */
1302 for (i = 0; i < master->ncontrols; i++) {
1303 if (master->cluster[i]) {
1304 cur_to_new(master->cluster[i]);
1305 master->cluster[i]->is_new = 1;
1309 /* Skip button controls and read-only controls. */
1310 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1311 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1313 ret = call_op(master, s_ctrl);
1316 for (i = 0; i < master->ncontrols; i++)
1317 if (master->cluster[i])
1318 master->cluster[i]->done = true;
1320 mutex_unlock(&hdl->lock);
1323 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1325 /* Implement VIDIOC_QUERYCTRL */
1326 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1328 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1329 struct v4l2_ctrl_ref *ref;
1330 struct v4l2_ctrl *ctrl;
1335 mutex_lock(&hdl->lock);
1337 /* Try to find it */
1338 ref = find_ref(hdl, id);
1340 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1341 /* Find the next control with ID > qc->id */
1343 /* Did we reach the end of the control list? */
1344 if (id >= node2id(hdl->ctrl_refs.prev)) {
1345 ref = NULL; /* Yes, so there is no next control */
1347 /* We found a control with the given ID, so just get
1348 the next one in the list. */
1349 ref = list_entry(ref->node.next, typeof(*ref), node);
1351 /* No control with the given ID exists, so start
1352 searching for the next largest ID. We know there
1353 is one, otherwise the first 'if' above would have
1355 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1356 if (id < ref->ctrl->id)
1360 mutex_unlock(&hdl->lock);
1365 memset(qc, 0, sizeof(*qc));
1366 if (id >= V4L2_CID_PRIVATE_BASE)
1370 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1371 qc->minimum = ctrl->minimum;
1372 qc->maximum = ctrl->maximum;
1373 qc->default_value = ctrl->default_value;
1374 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
1377 qc->step = ctrl->step;
1378 qc->flags = ctrl->flags;
1379 qc->type = ctrl->type;
1382 EXPORT_SYMBOL(v4l2_queryctrl);
1384 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1386 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1388 return v4l2_queryctrl(sd->ctrl_handler, qc);
1390 EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1392 /* Implement VIDIOC_QUERYMENU */
1393 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1395 struct v4l2_ctrl *ctrl;
1398 ctrl = v4l2_ctrl_find(hdl, qm->id);
1404 if (ctrl->qmenu == NULL ||
1405 i < ctrl->minimum || i > ctrl->maximum)
1407 /* Use mask to see if this menu item should be skipped */
1408 if (ctrl->menu_skip_mask & (1 << i))
1410 /* Empty menu items should also be skipped */
1411 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1413 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1416 EXPORT_SYMBOL(v4l2_querymenu);
1418 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1420 return v4l2_querymenu(sd->ctrl_handler, qm);
1422 EXPORT_SYMBOL(v4l2_subdev_querymenu);
1426 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1428 It is not a fully atomic operation, just best-effort only. After all, if
1429 multiple controls have to be set through multiple i2c writes (for example)
1430 then some initial writes may succeed while others fail. Thus leaving the
1431 system in an inconsistent state. The question is how much effort you are
1432 willing to spend on trying to make something atomic that really isn't.
1434 From the point of view of an application the main requirement is that
1435 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1436 error should be returned without actually affecting any controls.
1438 If all the values are correct, then it is acceptable to just give up
1439 in case of low-level errors.
1441 It is important though that the application can tell when only a partial
1442 configuration was done. The way we do that is through the error_idx field
1443 of struct v4l2_ext_controls: if that is equal to the count field then no
1444 controls were affected. Otherwise all controls before that index were
1445 successful in performing their 'get' or 'set' operation, the control at
1446 the given index failed, and you don't know what happened with the controls
1447 after the failed one. Since if they were part of a control cluster they
1448 could have been successfully processed (if a cluster member was encountered
1449 at index < error_idx), they could have failed (if a cluster member was at
1450 error_idx), or they may not have been processed yet (if the first cluster
1451 member appeared after error_idx).
1453 It is all fairly theoretical, though. In practice all you can do is to
1454 bail out. If error_idx == count, then it is an application bug. If
1455 error_idx < count then it is only an application bug if the error code was
1456 EBUSY. That usually means that something started streaming just when you
1457 tried to set the controls. In all other cases it is a driver/hardware
1458 problem and all you can do is to retry or bail out.
1460 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1461 never modifies controls the error_idx is just set to whatever control
1462 has an invalid value.
1465 /* Prepare for the extended g/s/try functions.
1466 Find the controls in the control array and do some basic checks. */
1467 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1468 struct v4l2_ext_controls *cs,
1469 struct ctrl_helper *helpers)
1473 for (i = 0; i < cs->count; i++) {
1474 struct v4l2_ext_control *c = &cs->controls[i];
1475 struct v4l2_ctrl *ctrl;
1476 u32 id = c->id & V4L2_CTRL_ID_MASK;
1480 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1483 /* Old-style private controls are not allowed for
1484 extended controls */
1485 if (id >= V4L2_CID_PRIVATE_BASE)
1487 ctrl = v4l2_ctrl_find(hdl, id);
1490 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1493 helpers[i].ctrl = ctrl;
1494 helpers[i].handled = false;
1499 typedef int (*cluster_func)(struct v4l2_ext_control *c,
1500 struct v4l2_ctrl *ctrl);
1502 /* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1503 and call the provided function. */
1504 static int cluster_walk(unsigned from,
1505 struct v4l2_ext_controls *cs,
1506 struct ctrl_helper *helpers,
1509 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1513 /* Find any controls from the same cluster and call the function */
1514 for (i = from; !ret && i < cs->count; i++) {
1515 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1517 if (!helpers[i].handled && ctrl->cluster == cluster)
1518 ret = f(&cs->controls[i], ctrl);
1523 static void cluster_done(unsigned from,
1524 struct v4l2_ext_controls *cs,
1525 struct ctrl_helper *helpers)
1527 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1530 /* Find any controls from the same cluster and mark them as handled */
1531 for (i = from; i < cs->count; i++)
1532 if (helpers[i].ctrl->cluster == cluster)
1533 helpers[i].handled = true;
1536 /* Handles the corner case where cs->count == 0. It checks whether the
1537 specified control class exists. If that class ID is 0, then it checks
1538 whether there are any controls at all. */
1539 static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1541 if (ctrl_class == 0)
1542 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1543 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1548 /* Get extended controls. Allocates the helpers array if needed. */
1549 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1551 struct ctrl_helper helper[4];
1552 struct ctrl_helper *helpers = helper;
1556 cs->error_idx = cs->count;
1557 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1563 return class_check(hdl, cs->ctrl_class);
1565 if (cs->count > ARRAY_SIZE(helper)) {
1566 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1567 if (helpers == NULL)
1571 ret = prepare_ext_ctrls(hdl, cs, helpers);
1572 cs->error_idx = cs->count;
1574 for (i = 0; !ret && i < cs->count; i++)
1575 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1578 for (i = 0; !ret && i < cs->count; i++) {
1579 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1580 struct v4l2_ctrl *master = ctrl->cluster[0];
1583 if (helpers[i].handled)
1588 /* Any volatile controls requested from this cluster? */
1589 has_volatiles = ctrl->is_volatile;
1590 if (!has_volatiles && has_op(master, g_volatile_ctrl) &&
1591 master->ncontrols > 1)
1592 has_volatiles = cluster_walk(i, cs, helpers,
1595 v4l2_ctrl_lock(master);
1597 /* g_volatile_ctrl will update the new control values */
1598 if (has_volatiles) {
1599 for (j = 0; j < master->ncontrols; j++)
1600 cur_to_new(master->cluster[j]);
1601 ret = call_op(master, g_volatile_ctrl);
1603 /* If OK, then copy the current (for non-volatile controls)
1604 or the new (for volatile controls) control values to the
1607 ret = cluster_walk(i, cs, helpers, ctrl_to_user);
1608 v4l2_ctrl_unlock(master);
1609 cluster_done(i, cs, helpers);
1612 if (cs->count > ARRAY_SIZE(helper))
1616 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1618 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1620 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1622 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1624 /* Helper function to get a single control */
1625 static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1627 struct v4l2_ctrl *master = ctrl->cluster[0];
1631 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1634 v4l2_ctrl_lock(master);
1635 /* g_volatile_ctrl will update the current control values */
1636 if (ctrl->is_volatile) {
1637 for (i = 0; i < master->ncontrols; i++)
1638 cur_to_new(master->cluster[i]);
1639 ret = call_op(master, g_volatile_ctrl);
1642 *val = ctrl->cur.val;
1644 v4l2_ctrl_unlock(master);
1648 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1650 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1652 if (ctrl == NULL || !type_is_int(ctrl))
1654 return get_ctrl(ctrl, &control->value);
1656 EXPORT_SYMBOL(v4l2_g_ctrl);
1658 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1660 return v4l2_g_ctrl(sd->ctrl_handler, control);
1662 EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1664 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1668 /* It's a driver bug if this happens. */
1669 WARN_ON(!type_is_int(ctrl));
1670 get_ctrl(ctrl, &val);
1673 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1676 /* Core function that calls try/s_ctrl and ensures that the new value is
1677 copied to the current value on a set.
1678 Must be called with ctrl->handler->lock held. */
1679 static int try_or_set_control_cluster(struct v4l2_ctrl *master, bool set)
1685 /* Go through the cluster and either validate the new value or
1686 (if no new value was set), copy the current value to the new
1687 value, ensuring a consistent view for the control ops when
1689 for (i = 0; !ret && i < master->ncontrols; i++) {
1690 struct v4l2_ctrl *ctrl = master->cluster[i];
1696 /* Double check this: it may have changed since the
1697 last check in try_or_set_ext_ctrls(). */
1698 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1701 /* Validate if required */
1703 ret = validate_new(ctrl);
1706 /* No new value was set, so copy the current and force
1707 a call to try_ctrl later, since the values for the cluster
1708 may now have changed and the end result might be invalid. */
1713 /* For larger clusters you have to call try_ctrl again to
1714 verify that the controls are still valid after the
1715 'cur_to_new' above. */
1717 ret = call_op(master, try_ctrl);
1719 /* Don't set if there is no change */
1720 if (!ret && set && cluster_changed(master)) {
1721 ret = call_op(master, s_ctrl);
1722 /* If OK, then make the new values permanent. */
1724 for (i = 0; i < master->ncontrols; i++)
1725 new_to_cur(master->cluster[i]);
1730 /* Try or set controls. */
1731 static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1732 struct v4l2_ext_controls *cs,
1733 struct ctrl_helper *helpers,
1739 for (i = 0; i < cs->count; i++) {
1740 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1744 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1746 /* This test is also done in try_set_control_cluster() which
1747 is called in atomic context, so that has the final say,
1748 but it makes sense to do an up-front check as well. Once
1749 an error occurs in try_set_control_cluster() some other
1750 controls may have been set already and we want to do a
1751 best-effort to avoid that. */
1752 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1756 for (i = 0; !ret && i < cs->count; i++) {
1757 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1758 struct v4l2_ctrl *master = ctrl->cluster[0];
1760 if (helpers[i].handled)
1764 v4l2_ctrl_lock(ctrl);
1766 /* Reset the 'is_new' flags of the cluster */
1767 for (j = 0; j < master->ncontrols; j++)
1768 if (master->cluster[j])
1769 master->cluster[j]->is_new = 0;
1771 /* Copy the new caller-supplied control values.
1772 user_to_new() sets 'is_new' to 1. */
1773 ret = cluster_walk(i, cs, helpers, user_to_new);
1776 ret = try_or_set_control_cluster(master, set);
1778 /* Copy the new values back to userspace. */
1780 ret = cluster_walk(i, cs, helpers, new_to_user);
1782 v4l2_ctrl_unlock(ctrl);
1783 cluster_done(i, cs, helpers);
1788 /* Try or try-and-set controls */
1789 static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1790 struct v4l2_ext_controls *cs,
1793 struct ctrl_helper helper[4];
1794 struct ctrl_helper *helpers = helper;
1798 cs->error_idx = cs->count;
1799 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1805 return class_check(hdl, cs->ctrl_class);
1807 if (cs->count > ARRAY_SIZE(helper)) {
1808 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1812 ret = prepare_ext_ctrls(hdl, cs, helpers);
1814 /* First 'try' all controls and abort on error */
1816 ret = try_or_set_ext_ctrls(hdl, cs, helpers, false);
1817 /* If this is a 'set' operation and the initial 'try' failed,
1818 then set error_idx to count to tell the application that no
1819 controls changed value yet. */
1821 cs->error_idx = cs->count;
1823 /* Reset 'handled' state */
1824 for (i = 0; i < cs->count; i++)
1825 helpers[i].handled = false;
1826 ret = try_or_set_ext_ctrls(hdl, cs, helpers, true);
1829 if (cs->count > ARRAY_SIZE(helper))
1834 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1836 return try_set_ext_ctrls(hdl, cs, false);
1838 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1840 int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1842 return try_set_ext_ctrls(hdl, cs, true);
1844 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1846 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1848 return try_set_ext_ctrls(sd->ctrl_handler, cs, false);
1850 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1852 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1854 return try_set_ext_ctrls(sd->ctrl_handler, cs, true);
1856 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1858 /* Helper function for VIDIOC_S_CTRL compatibility */
1859 static int set_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1861 struct v4l2_ctrl *master = ctrl->cluster[0];
1865 v4l2_ctrl_lock(ctrl);
1867 /* Reset the 'is_new' flags of the cluster */
1868 for (i = 0; i < master->ncontrols; i++)
1869 if (master->cluster[i])
1870 master->cluster[i]->is_new = 0;
1874 ret = try_or_set_control_cluster(master, false);
1876 ret = try_or_set_control_cluster(master, true);
1877 *val = ctrl->cur.val;
1878 v4l2_ctrl_unlock(ctrl);
1882 int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1884 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1886 if (ctrl == NULL || !type_is_int(ctrl))
1889 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1892 return set_ctrl(ctrl, &control->value);
1894 EXPORT_SYMBOL(v4l2_s_ctrl);
1896 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1898 return v4l2_s_ctrl(sd->ctrl_handler, control);
1900 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
1902 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
1904 /* It's a driver bug if this happens. */
1905 WARN_ON(!type_is_int(ctrl));
1906 return set_ctrl(ctrl, &val);
1908 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);