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 /* Small helper function to determine if the autocluster is set to manual
43 mode. In that case the is_volatile flag should be ignored. */
44 static bool is_cur_manual(const struct v4l2_ctrl *master)
46 return master->is_auto && master->cur.val == master->manual_mode_value;
49 /* Same as above, but this checks the against the new value instead of the
51 static bool is_new_manual(const struct v4l2_ctrl *master)
53 return master->is_auto && master->val == master->manual_mode_value;
56 /* Returns NULL or a character pointer array containing the menu for
57 the given control ID. The pointer array ends with a NULL pointer.
58 An empty string signifies a menu entry that is invalid. This allows
59 drivers to disable certain options if it is not supported. */
60 const char * const *v4l2_ctrl_get_menu(u32 id)
62 static const char * const mpeg_audio_sampling_freq[] = {
68 static const char * const mpeg_audio_encoding[] = {
76 static const char * const mpeg_audio_l1_bitrate[] = {
93 static const char * const mpeg_audio_l2_bitrate[] = {
110 static const char * const mpeg_audio_l3_bitrate[] = {
127 static const char * const mpeg_audio_ac3_bitrate[] = {
149 static const char * const mpeg_audio_mode[] = {
156 static const char * const mpeg_audio_mode_extension[] = {
163 static const char * const mpeg_audio_emphasis[] = {
169 static const char * const mpeg_audio_crc[] = {
174 static const char * const mpeg_video_encoding[] = {
180 static const char * const mpeg_video_aspect[] = {
187 static const char * const mpeg_video_bitrate_mode[] = {
192 static const char * const mpeg_stream_type[] = {
193 "MPEG-2 Program Stream",
194 "MPEG-2 Transport Stream",
195 "MPEG-1 System Stream",
196 "MPEG-2 DVD-compatible Stream",
197 "MPEG-1 VCD-compatible Stream",
198 "MPEG-2 SVCD-compatible Stream",
201 static const char * const mpeg_stream_vbi_fmt[] = {
203 "Private packet, IVTV format",
206 static const char * const camera_power_line_frequency[] = {
212 static const char * const camera_exposure_auto[] = {
215 "Shutter Priority Mode",
216 "Aperture Priority Mode",
219 static const char * const colorfx[] = {
232 static const char * const tune_preemphasis[] = {
240 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
241 return mpeg_audio_sampling_freq;
242 case V4L2_CID_MPEG_AUDIO_ENCODING:
243 return mpeg_audio_encoding;
244 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
245 return mpeg_audio_l1_bitrate;
246 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
247 return mpeg_audio_l2_bitrate;
248 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
249 return mpeg_audio_l3_bitrate;
250 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
251 return mpeg_audio_ac3_bitrate;
252 case V4L2_CID_MPEG_AUDIO_MODE:
253 return mpeg_audio_mode;
254 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
255 return mpeg_audio_mode_extension;
256 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
257 return mpeg_audio_emphasis;
258 case V4L2_CID_MPEG_AUDIO_CRC:
259 return mpeg_audio_crc;
260 case V4L2_CID_MPEG_VIDEO_ENCODING:
261 return mpeg_video_encoding;
262 case V4L2_CID_MPEG_VIDEO_ASPECT:
263 return mpeg_video_aspect;
264 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
265 return mpeg_video_bitrate_mode;
266 case V4L2_CID_MPEG_STREAM_TYPE:
267 return mpeg_stream_type;
268 case V4L2_CID_MPEG_STREAM_VBI_FMT:
269 return mpeg_stream_vbi_fmt;
270 case V4L2_CID_POWER_LINE_FREQUENCY:
271 return camera_power_line_frequency;
272 case V4L2_CID_EXPOSURE_AUTO:
273 return camera_exposure_auto;
274 case V4L2_CID_COLORFX:
276 case V4L2_CID_TUNE_PREEMPHASIS:
277 return tune_preemphasis;
282 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
284 /* Return the control name. */
285 const char *v4l2_ctrl_get_name(u32 id)
289 /* Keep the order of the 'case's the same as in videodev2.h! */
290 case V4L2_CID_USER_CLASS: return "User Controls";
291 case V4L2_CID_BRIGHTNESS: return "Brightness";
292 case V4L2_CID_CONTRAST: return "Contrast";
293 case V4L2_CID_SATURATION: return "Saturation";
294 case V4L2_CID_HUE: return "Hue";
295 case V4L2_CID_AUDIO_VOLUME: return "Volume";
296 case V4L2_CID_AUDIO_BALANCE: return "Balance";
297 case V4L2_CID_AUDIO_BASS: return "Bass";
298 case V4L2_CID_AUDIO_TREBLE: return "Treble";
299 case V4L2_CID_AUDIO_MUTE: return "Mute";
300 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
301 case V4L2_CID_BLACK_LEVEL: return "Black Level";
302 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
303 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
304 case V4L2_CID_RED_BALANCE: return "Red Balance";
305 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
306 case V4L2_CID_GAMMA: return "Gamma";
307 case V4L2_CID_EXPOSURE: return "Exposure";
308 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
309 case V4L2_CID_GAIN: return "Gain";
310 case V4L2_CID_HFLIP: return "Horizontal Flip";
311 case V4L2_CID_VFLIP: return "Vertical Flip";
312 case V4L2_CID_HCENTER: return "Horizontal Center";
313 case V4L2_CID_VCENTER: return "Vertical Center";
314 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
315 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
316 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
317 case V4L2_CID_SHARPNESS: return "Sharpness";
318 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
319 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
320 case V4L2_CID_COLOR_KILLER: return "Color Killer";
321 case V4L2_CID_COLORFX: return "Color Effects";
322 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
323 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
324 case V4L2_CID_ROTATE: return "Rotate";
325 case V4L2_CID_BG_COLOR: return "Background Color";
326 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
327 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
328 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
331 /* Keep the order of the 'case's the same as in videodev2.h! */
332 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
333 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
334 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
335 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
336 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
337 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
338 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
339 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
340 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
341 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
342 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
343 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
344 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
345 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
346 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
347 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
348 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
349 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
350 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
351 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
352 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
353 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
354 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
355 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
356 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
357 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
358 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
359 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
360 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
361 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
362 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
363 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
364 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
366 /* CAMERA controls */
367 /* Keep the order of the 'case's the same as in videodev2.h! */
368 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
369 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
370 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
371 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
372 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
373 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
374 case V4L2_CID_PAN_RESET: return "Pan, Reset";
375 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
376 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
377 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
378 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
379 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
380 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
381 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
382 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
383 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
384 case V4L2_CID_PRIVACY: return "Privacy";
385 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
386 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
388 /* FM Radio Modulator control */
389 /* Keep the order of the 'case's the same as in videodev2.h! */
390 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
391 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
392 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
393 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
394 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
395 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
396 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
397 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
398 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
399 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
400 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
401 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
402 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
403 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
404 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
405 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
406 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
407 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
408 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
409 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
415 EXPORT_SYMBOL(v4l2_ctrl_get_name);
417 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
418 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
420 *name = v4l2_ctrl_get_name(id);
424 case V4L2_CID_AUDIO_MUTE:
425 case V4L2_CID_AUDIO_LOUDNESS:
426 case V4L2_CID_AUTO_WHITE_BALANCE:
427 case V4L2_CID_AUTOGAIN:
430 case V4L2_CID_HUE_AUTO:
431 case V4L2_CID_CHROMA_AGC:
432 case V4L2_CID_COLOR_KILLER:
433 case V4L2_CID_MPEG_AUDIO_MUTE:
434 case V4L2_CID_MPEG_VIDEO_MUTE:
435 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
436 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
437 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
438 case V4L2_CID_FOCUS_AUTO:
439 case V4L2_CID_PRIVACY:
440 case V4L2_CID_AUDIO_LIMITER_ENABLED:
441 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
442 case V4L2_CID_PILOT_TONE_ENABLED:
443 case V4L2_CID_ILLUMINATORS_1:
444 case V4L2_CID_ILLUMINATORS_2:
445 *type = V4L2_CTRL_TYPE_BOOLEAN;
449 case V4L2_CID_PAN_RESET:
450 case V4L2_CID_TILT_RESET:
451 *type = V4L2_CTRL_TYPE_BUTTON;
452 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
453 *min = *max = *step = *def = 0;
455 case V4L2_CID_POWER_LINE_FREQUENCY:
456 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
457 case V4L2_CID_MPEG_AUDIO_ENCODING:
458 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
459 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
460 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
461 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
462 case V4L2_CID_MPEG_AUDIO_MODE:
463 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
464 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
465 case V4L2_CID_MPEG_AUDIO_CRC:
466 case V4L2_CID_MPEG_VIDEO_ENCODING:
467 case V4L2_CID_MPEG_VIDEO_ASPECT:
468 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
469 case V4L2_CID_MPEG_STREAM_TYPE:
470 case V4L2_CID_MPEG_STREAM_VBI_FMT:
471 case V4L2_CID_EXPOSURE_AUTO:
472 case V4L2_CID_COLORFX:
473 case V4L2_CID_TUNE_PREEMPHASIS:
474 *type = V4L2_CTRL_TYPE_MENU;
476 case V4L2_CID_RDS_TX_PS_NAME:
477 case V4L2_CID_RDS_TX_RADIO_TEXT:
478 *type = V4L2_CTRL_TYPE_STRING;
480 case V4L2_CID_USER_CLASS:
481 case V4L2_CID_CAMERA_CLASS:
482 case V4L2_CID_MPEG_CLASS:
483 case V4L2_CID_FM_TX_CLASS:
484 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
485 /* You can neither read not write these */
486 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
487 *min = *max = *step = *def = 0;
489 case V4L2_CID_BG_COLOR:
490 *type = V4L2_CTRL_TYPE_INTEGER;
493 /* Max is calculated as RGB888 that is 2^24 */
497 *type = V4L2_CTRL_TYPE_INTEGER;
501 case V4L2_CID_MPEG_AUDIO_ENCODING:
502 case V4L2_CID_MPEG_AUDIO_MODE:
503 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
504 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
505 case V4L2_CID_MPEG_STREAM_TYPE:
506 *flags |= V4L2_CTRL_FLAG_UPDATE;
508 case V4L2_CID_AUDIO_VOLUME:
509 case V4L2_CID_AUDIO_BALANCE:
510 case V4L2_CID_AUDIO_BASS:
511 case V4L2_CID_AUDIO_TREBLE:
512 case V4L2_CID_BRIGHTNESS:
513 case V4L2_CID_CONTRAST:
514 case V4L2_CID_SATURATION:
516 case V4L2_CID_RED_BALANCE:
517 case V4L2_CID_BLUE_BALANCE:
519 case V4L2_CID_SHARPNESS:
520 case V4L2_CID_CHROMA_GAIN:
521 case V4L2_CID_RDS_TX_DEVIATION:
522 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
523 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
524 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
525 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
526 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
527 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
528 case V4L2_CID_PILOT_TONE_DEVIATION:
529 case V4L2_CID_PILOT_TONE_FREQUENCY:
530 case V4L2_CID_TUNE_POWER_LEVEL:
531 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
532 *flags |= V4L2_CTRL_FLAG_SLIDER;
534 case V4L2_CID_PAN_RELATIVE:
535 case V4L2_CID_TILT_RELATIVE:
536 case V4L2_CID_FOCUS_RELATIVE:
537 case V4L2_CID_IRIS_RELATIVE:
538 case V4L2_CID_ZOOM_RELATIVE:
539 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
543 EXPORT_SYMBOL(v4l2_ctrl_fill);
545 /* Helper function to determine whether the control type is compatible with
547 static bool type_is_int(const struct v4l2_ctrl *ctrl)
549 switch (ctrl->type) {
550 case V4L2_CTRL_TYPE_INTEGER64:
551 case V4L2_CTRL_TYPE_STRING:
552 /* Nope, these need v4l2_ext_control */
559 /* Helper function: copy the current control value back to the caller */
560 static int cur_to_user(struct v4l2_ext_control *c,
561 struct v4l2_ctrl *ctrl)
565 switch (ctrl->type) {
566 case V4L2_CTRL_TYPE_STRING:
567 len = strlen(ctrl->cur.string);
568 if (c->size < len + 1) {
572 return copy_to_user(c->string, ctrl->cur.string,
573 len + 1) ? -EFAULT : 0;
574 case V4L2_CTRL_TYPE_INTEGER64:
575 c->value64 = ctrl->cur.val64;
578 c->value = ctrl->cur.val;
584 /* Helper function: copy the caller-provider value as the new control value */
585 static int user_to_new(struct v4l2_ext_control *c,
586 struct v4l2_ctrl *ctrl)
592 switch (ctrl->type) {
593 case V4L2_CTRL_TYPE_INTEGER64:
594 ctrl->val64 = c->value64;
596 case V4L2_CTRL_TYPE_STRING:
600 if (size > ctrl->maximum + 1)
601 size = ctrl->maximum + 1;
602 ret = copy_from_user(ctrl->string, c->string, size);
604 char last = ctrl->string[size - 1];
606 ctrl->string[size - 1] = 0;
607 /* If the string was longer than ctrl->maximum,
608 then return an error. */
609 if (strlen(ctrl->string) == ctrl->maximum && last)
612 return ret ? -EFAULT : 0;
614 ctrl->val = c->value;
620 /* Helper function: copy the new control value back to the caller */
621 static int new_to_user(struct v4l2_ext_control *c,
622 struct v4l2_ctrl *ctrl)
626 switch (ctrl->type) {
627 case V4L2_CTRL_TYPE_STRING:
628 len = strlen(ctrl->string);
629 if (c->size < len + 1) {
630 c->size = ctrl->maximum + 1;
633 return copy_to_user(c->string, ctrl->string,
634 len + 1) ? -EFAULT : 0;
635 case V4L2_CTRL_TYPE_INTEGER64:
636 c->value64 = ctrl->val64;
639 c->value = ctrl->val;
645 static int ctrl_to_user(struct v4l2_ext_control *c,
646 struct v4l2_ctrl *ctrl)
648 if (ctrl->is_volatile)
649 return new_to_user(c, ctrl);
650 return cur_to_user(c, ctrl);
653 static int ctrl_is_volatile(struct v4l2_ext_control *c,
654 struct v4l2_ctrl *ctrl)
656 return ctrl->is_volatile;
659 /* Copy the new value to the current value. */
660 static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
661 bool update_inactive)
665 switch (ctrl->type) {
666 case V4L2_CTRL_TYPE_STRING:
667 /* strings are always 0-terminated */
668 strcpy(ctrl->cur.string, ctrl->string);
670 case V4L2_CTRL_TYPE_INTEGER64:
671 ctrl->cur.val64 = ctrl->val64;
674 ctrl->cur.val = ctrl->val;
677 if (update_inactive) {
678 ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
679 if (!is_cur_manual(ctrl->cluster[0]))
680 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
684 /* Copy the current value to the new value */
685 static void cur_to_new(struct v4l2_ctrl *ctrl)
689 switch (ctrl->type) {
690 case V4L2_CTRL_TYPE_STRING:
691 /* strings are always 0-terminated */
692 strcpy(ctrl->string, ctrl->cur.string);
694 case V4L2_CTRL_TYPE_INTEGER64:
695 ctrl->val64 = ctrl->cur.val64;
698 ctrl->val = ctrl->cur.val;
703 /* Return non-zero if one or more of the controls in the cluster has a new
704 value that differs from the current value. */
705 static int cluster_changed(struct v4l2_ctrl *master)
710 for (i = 0; !diff && i < master->ncontrols; i++) {
711 struct v4l2_ctrl *ctrl = master->cluster[i];
715 switch (ctrl->type) {
716 case V4L2_CTRL_TYPE_BUTTON:
717 /* Button controls are always 'different' */
719 case V4L2_CTRL_TYPE_STRING:
720 /* strings are always 0-terminated */
721 diff = strcmp(ctrl->string, ctrl->cur.string);
723 case V4L2_CTRL_TYPE_INTEGER64:
724 diff = ctrl->val64 != ctrl->cur.val64;
727 diff = ctrl->val != ctrl->cur.val;
734 /* Validate a new control */
735 static int validate_new(struct v4l2_ctrl *ctrl)
738 char *s = ctrl->string;
742 switch (ctrl->type) {
743 case V4L2_CTRL_TYPE_INTEGER:
744 /* Round towards the closest legal value */
745 val += ctrl->step / 2;
746 if (val < ctrl->minimum)
748 if (val > ctrl->maximum)
750 offset = val - ctrl->minimum;
751 offset = ctrl->step * (offset / ctrl->step);
752 val = ctrl->minimum + offset;
756 case V4L2_CTRL_TYPE_BOOLEAN:
757 ctrl->val = !!ctrl->val;
760 case V4L2_CTRL_TYPE_MENU:
761 if (val < ctrl->minimum || val > ctrl->maximum)
763 if (ctrl->qmenu[val][0] == '\0' ||
764 (ctrl->menu_skip_mask & (1 << val)))
768 case V4L2_CTRL_TYPE_BUTTON:
769 case V4L2_CTRL_TYPE_CTRL_CLASS:
773 case V4L2_CTRL_TYPE_INTEGER64:
776 case V4L2_CTRL_TYPE_STRING:
778 if (len < ctrl->minimum)
780 if ((len - ctrl->minimum) % ctrl->step)
789 static inline u32 node2id(struct list_head *node)
791 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
794 /* Set the handler's error code if it wasn't set earlier already */
795 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
802 /* Initialize the handler */
803 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
804 unsigned nr_of_controls_hint)
806 mutex_init(&hdl->lock);
807 INIT_LIST_HEAD(&hdl->ctrls);
808 INIT_LIST_HEAD(&hdl->ctrl_refs);
809 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
810 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
812 hdl->error = hdl->buckets ? 0 : -ENOMEM;
815 EXPORT_SYMBOL(v4l2_ctrl_handler_init);
817 /* Free all controls and control refs */
818 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
820 struct v4l2_ctrl_ref *ref, *next_ref;
821 struct v4l2_ctrl *ctrl, *next_ctrl;
823 if (hdl == NULL || hdl->buckets == NULL)
826 mutex_lock(&hdl->lock);
828 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
829 list_del(&ref->node);
832 /* Free all controls owned by the handler */
833 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
834 list_del(&ctrl->node);
841 mutex_unlock(&hdl->lock);
843 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
845 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
846 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
847 with applications that do not use the NEXT_CTRL flag.
849 We just find the n-th private user control. It's O(N), but that should not
850 be an issue in this particular case. */
851 static struct v4l2_ctrl_ref *find_private_ref(
852 struct v4l2_ctrl_handler *hdl, u32 id)
854 struct v4l2_ctrl_ref *ref;
856 id -= V4L2_CID_PRIVATE_BASE;
857 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
858 /* Search for private user controls that are compatible with
860 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
861 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
862 if (!type_is_int(ref->ctrl))
872 /* Find a control with the given ID. */
873 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
875 struct v4l2_ctrl_ref *ref;
878 id &= V4L2_CTRL_ID_MASK;
880 /* Old-style private controls need special handling */
881 if (id >= V4L2_CID_PRIVATE_BASE)
882 return find_private_ref(hdl, id);
883 bucket = id % hdl->nr_of_buckets;
885 /* Simple optimization: cache the last control found */
886 if (hdl->cached && hdl->cached->ctrl->id == id)
889 /* Not in cache, search the hash */
890 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
891 while (ref && ref->ctrl->id != id)
895 hdl->cached = ref; /* cache it! */
899 /* Find a control with the given ID. Take the handler's lock first. */
900 static struct v4l2_ctrl_ref *find_ref_lock(
901 struct v4l2_ctrl_handler *hdl, u32 id)
903 struct v4l2_ctrl_ref *ref = NULL;
906 mutex_lock(&hdl->lock);
907 ref = find_ref(hdl, id);
908 mutex_unlock(&hdl->lock);
913 /* Find a control with the given ID. */
914 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
916 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
918 return ref ? ref->ctrl : NULL;
920 EXPORT_SYMBOL(v4l2_ctrl_find);
922 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
923 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
924 struct v4l2_ctrl *ctrl)
926 struct v4l2_ctrl_ref *ref;
927 struct v4l2_ctrl_ref *new_ref;
929 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
930 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
932 /* Automatically add the control class if it is not yet present. */
933 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
934 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
940 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
942 return handler_set_err(hdl, -ENOMEM);
943 new_ref->ctrl = ctrl;
944 if (ctrl->handler == hdl) {
945 /* By default each control starts in a cluster of its own.
946 new_ref->ctrl is basically a cluster array with one
947 element, so that's perfect to use as the cluster pointer.
948 But only do this for the handler that owns the control. */
949 ctrl->cluster = &new_ref->ctrl;
953 INIT_LIST_HEAD(&new_ref->node);
955 mutex_lock(&hdl->lock);
957 /* Add immediately at the end of the list if the list is empty, or if
958 the last element in the list has a lower ID.
959 This ensures that when elements are added in ascending order the
960 insertion is an O(1) operation. */
961 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
962 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
966 /* Find insert position in sorted list */
967 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
968 if (ref->ctrl->id < id)
970 /* Don't add duplicates */
971 if (ref->ctrl->id == id) {
975 list_add(&new_ref->node, ref->node.prev);
980 /* Insert the control node in the hash */
981 new_ref->next = hdl->buckets[bucket];
982 hdl->buckets[bucket] = new_ref;
985 mutex_unlock(&hdl->lock);
989 /* Add a new control */
990 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
991 const struct v4l2_ctrl_ops *ops,
992 u32 id, const char *name, enum v4l2_ctrl_type type,
993 s32 min, s32 max, u32 step, s32 def,
994 u32 flags, const char * const *qmenu, void *priv)
996 struct v4l2_ctrl *ctrl;
997 unsigned sz_extra = 0;
1003 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
1005 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
1006 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1007 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1008 handler_set_err(hdl, -ERANGE);
1011 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1012 type == V4L2_CTRL_TYPE_MENU ||
1013 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1014 (def < min || def > max)) {
1015 handler_set_err(hdl, -ERANGE);
1019 if (type == V4L2_CTRL_TYPE_BUTTON)
1020 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1021 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1022 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1023 else if (type == V4L2_CTRL_TYPE_STRING)
1024 sz_extra += 2 * (max + 1);
1026 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1028 handler_set_err(hdl, -ENOMEM);
1032 INIT_LIST_HEAD(&ctrl->node);
1033 ctrl->handler = hdl;
1038 ctrl->flags = flags;
1039 ctrl->minimum = min;
1040 ctrl->maximum = max;
1042 ctrl->qmenu = qmenu;
1044 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1046 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1047 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1048 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1050 memset(ctrl->cur.string, ' ', ctrl->minimum);
1052 if (handler_new_ref(hdl, ctrl)) {
1056 mutex_lock(&hdl->lock);
1057 list_add_tail(&ctrl->node, &hdl->ctrls);
1058 mutex_unlock(&hdl->lock);
1062 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1063 const struct v4l2_ctrl_config *cfg, void *priv)
1066 struct v4l2_ctrl *ctrl;
1067 const char *name = cfg->name;
1068 const char * const *qmenu = cfg->qmenu;
1069 enum v4l2_ctrl_type type = cfg->type;
1070 u32 flags = cfg->flags;
1073 u32 step = cfg->step;
1077 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1080 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1084 WARN_ON(cfg->menu_skip_mask);
1085 if (is_menu && qmenu == NULL)
1086 qmenu = v4l2_ctrl_get_menu(cfg->id);
1088 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1090 is_menu ? cfg->menu_skip_mask : step,
1091 def, flags, qmenu, priv);
1093 ctrl->is_private = cfg->is_private;
1094 ctrl->is_volatile = cfg->is_volatile;
1098 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1100 /* Helper function for standard non-menu controls */
1101 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1102 const struct v4l2_ctrl_ops *ops,
1103 u32 id, s32 min, s32 max, u32 step, s32 def)
1106 enum v4l2_ctrl_type type;
1109 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1110 if (type == V4L2_CTRL_TYPE_MENU) {
1111 handler_set_err(hdl, -EINVAL);
1114 return v4l2_ctrl_new(hdl, ops, id, name, type,
1115 min, max, step, def, flags, NULL, NULL);
1117 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1119 /* Helper function for standard menu controls */
1120 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1121 const struct v4l2_ctrl_ops *ops,
1122 u32 id, s32 max, s32 mask, s32 def)
1124 const char * const *qmenu = v4l2_ctrl_get_menu(id);
1126 enum v4l2_ctrl_type type;
1131 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1132 if (type != V4L2_CTRL_TYPE_MENU) {
1133 handler_set_err(hdl, -EINVAL);
1136 return v4l2_ctrl_new(hdl, ops, id, name, type,
1137 0, max, mask, def, flags, qmenu, NULL);
1139 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1141 /* Add a control from another handler to this handler */
1142 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1143 struct v4l2_ctrl *ctrl)
1145 if (hdl == NULL || hdl->error)
1148 handler_set_err(hdl, -EINVAL);
1151 if (ctrl->handler == hdl)
1153 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1155 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1157 /* Add the controls from another handler to our own. */
1158 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1159 struct v4l2_ctrl_handler *add)
1161 struct v4l2_ctrl *ctrl;
1164 /* Do nothing if either handler is NULL or if they are the same */
1165 if (!hdl || !add || hdl == add)
1169 mutex_lock(&add->lock);
1170 list_for_each_entry(ctrl, &add->ctrls, node) {
1171 /* Skip handler-private controls. */
1172 if (ctrl->is_private)
1174 ret = handler_new_ref(hdl, ctrl);
1178 mutex_unlock(&add->lock);
1181 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1183 /* Cluster controls */
1184 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1188 /* The first control is the master control and it must not be NULL */
1189 BUG_ON(ncontrols == 0 || controls[0] == NULL);
1191 for (i = 0; i < ncontrols; i++) {
1193 controls[i]->cluster = controls;
1194 controls[i]->ncontrols = ncontrols;
1198 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1200 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1201 u8 manual_val, bool set_volatile)
1203 struct v4l2_ctrl *master = controls[0];
1207 v4l2_ctrl_cluster(ncontrols, controls);
1208 WARN_ON(ncontrols <= 1);
1209 master->is_auto = true;
1210 master->manual_mode_value = manual_val;
1211 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1212 flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
1214 for (i = 1; i < ncontrols; i++)
1216 controls[i]->is_volatile = set_volatile;
1217 controls[i]->flags |= flag;
1220 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1222 /* Activate/deactivate a control. */
1223 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1229 /* set V4L2_CTRL_FLAG_INACTIVE */
1230 set_bit(4, &ctrl->flags);
1232 /* clear V4L2_CTRL_FLAG_INACTIVE */
1233 clear_bit(4, &ctrl->flags);
1235 EXPORT_SYMBOL(v4l2_ctrl_activate);
1237 /* Grab/ungrab a control.
1238 Typically used when streaming starts and you want to grab controls,
1239 preventing the user from changing them.
1241 Just call this and the framework will block any attempts to change
1243 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1249 /* set V4L2_CTRL_FLAG_GRABBED */
1250 set_bit(1, &ctrl->flags);
1252 /* clear V4L2_CTRL_FLAG_GRABBED */
1253 clear_bit(1, &ctrl->flags);
1255 EXPORT_SYMBOL(v4l2_ctrl_grab);
1257 /* Log the control name and value */
1258 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1259 const char *prefix, const char *colon)
1261 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1262 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1264 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1266 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1269 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1271 switch (ctrl->type) {
1272 case V4L2_CTRL_TYPE_INTEGER:
1273 printk(KERN_CONT "%d", ctrl->cur.val);
1275 case V4L2_CTRL_TYPE_BOOLEAN:
1276 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1278 case V4L2_CTRL_TYPE_MENU:
1279 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1281 case V4L2_CTRL_TYPE_INTEGER64:
1282 printk(KERN_CONT "%lld", ctrl->cur.val64);
1284 case V4L2_CTRL_TYPE_STRING:
1285 printk(KERN_CONT "%s", ctrl->cur.string);
1288 printk(KERN_CONT "unknown type %d", ctrl->type);
1291 if (fl_inact && fl_grabbed)
1292 printk(KERN_CONT " (inactive, grabbed)\n");
1294 printk(KERN_CONT " (inactive)\n");
1295 else if (fl_grabbed)
1296 printk(KERN_CONT " (grabbed)\n");
1298 printk(KERN_CONT "\n");
1301 /* Log all controls owned by the handler */
1302 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1305 struct v4l2_ctrl *ctrl;
1306 const char *colon = "";
1313 len = strlen(prefix);
1314 if (len && prefix[len - 1] != ' ')
1316 mutex_lock(&hdl->lock);
1317 list_for_each_entry(ctrl, &hdl->ctrls, node)
1318 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1319 log_ctrl(ctrl, prefix, colon);
1320 mutex_unlock(&hdl->lock);
1322 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1324 /* Call s_ctrl for all controls owned by the handler */
1325 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1327 struct v4l2_ctrl *ctrl;
1332 mutex_lock(&hdl->lock);
1333 list_for_each_entry(ctrl, &hdl->ctrls, node)
1336 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1337 struct v4l2_ctrl *master = ctrl->cluster[0];
1340 /* Skip if this control was already handled by a cluster. */
1344 for (i = 0; i < master->ncontrols; i++) {
1345 if (master->cluster[i]) {
1346 cur_to_new(master->cluster[i]);
1347 master->cluster[i]->is_new = 1;
1351 /* Skip button controls and read-only controls. */
1352 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1353 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1355 ret = call_op(master, s_ctrl);
1358 for (i = 0; i < master->ncontrols; i++)
1359 if (master->cluster[i])
1360 master->cluster[i]->done = true;
1362 mutex_unlock(&hdl->lock);
1365 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1367 /* Implement VIDIOC_QUERYCTRL */
1368 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1370 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1371 struct v4l2_ctrl_ref *ref;
1372 struct v4l2_ctrl *ctrl;
1377 mutex_lock(&hdl->lock);
1379 /* Try to find it */
1380 ref = find_ref(hdl, id);
1382 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1383 /* Find the next control with ID > qc->id */
1385 /* Did we reach the end of the control list? */
1386 if (id >= node2id(hdl->ctrl_refs.prev)) {
1387 ref = NULL; /* Yes, so there is no next control */
1389 /* We found a control with the given ID, so just get
1390 the next one in the list. */
1391 ref = list_entry(ref->node.next, typeof(*ref), node);
1393 /* No control with the given ID exists, so start
1394 searching for the next largest ID. We know there
1395 is one, otherwise the first 'if' above would have
1397 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1398 if (id < ref->ctrl->id)
1402 mutex_unlock(&hdl->lock);
1407 memset(qc, 0, sizeof(*qc));
1408 if (id >= V4L2_CID_PRIVATE_BASE)
1412 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1413 qc->minimum = ctrl->minimum;
1414 qc->maximum = ctrl->maximum;
1415 qc->default_value = ctrl->default_value;
1416 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
1419 qc->step = ctrl->step;
1420 qc->flags = ctrl->flags;
1421 qc->type = ctrl->type;
1424 EXPORT_SYMBOL(v4l2_queryctrl);
1426 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1428 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1430 return v4l2_queryctrl(sd->ctrl_handler, qc);
1432 EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1434 /* Implement VIDIOC_QUERYMENU */
1435 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1437 struct v4l2_ctrl *ctrl;
1440 ctrl = v4l2_ctrl_find(hdl, qm->id);
1446 if (ctrl->qmenu == NULL ||
1447 i < ctrl->minimum || i > ctrl->maximum)
1449 /* Use mask to see if this menu item should be skipped */
1450 if (ctrl->menu_skip_mask & (1 << i))
1452 /* Empty menu items should also be skipped */
1453 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1455 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1458 EXPORT_SYMBOL(v4l2_querymenu);
1460 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1462 return v4l2_querymenu(sd->ctrl_handler, qm);
1464 EXPORT_SYMBOL(v4l2_subdev_querymenu);
1468 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1470 It is not a fully atomic operation, just best-effort only. After all, if
1471 multiple controls have to be set through multiple i2c writes (for example)
1472 then some initial writes may succeed while others fail. Thus leaving the
1473 system in an inconsistent state. The question is how much effort you are
1474 willing to spend on trying to make something atomic that really isn't.
1476 From the point of view of an application the main requirement is that
1477 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1478 error should be returned without actually affecting any controls.
1480 If all the values are correct, then it is acceptable to just give up
1481 in case of low-level errors.
1483 It is important though that the application can tell when only a partial
1484 configuration was done. The way we do that is through the error_idx field
1485 of struct v4l2_ext_controls: if that is equal to the count field then no
1486 controls were affected. Otherwise all controls before that index were
1487 successful in performing their 'get' or 'set' operation, the control at
1488 the given index failed, and you don't know what happened with the controls
1489 after the failed one. Since if they were part of a control cluster they
1490 could have been successfully processed (if a cluster member was encountered
1491 at index < error_idx), they could have failed (if a cluster member was at
1492 error_idx), or they may not have been processed yet (if the first cluster
1493 member appeared after error_idx).
1495 It is all fairly theoretical, though. In practice all you can do is to
1496 bail out. If error_idx == count, then it is an application bug. If
1497 error_idx < count then it is only an application bug if the error code was
1498 EBUSY. That usually means that something started streaming just when you
1499 tried to set the controls. In all other cases it is a driver/hardware
1500 problem and all you can do is to retry or bail out.
1502 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1503 never modifies controls the error_idx is just set to whatever control
1504 has an invalid value.
1507 /* Prepare for the extended g/s/try functions.
1508 Find the controls in the control array and do some basic checks. */
1509 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1510 struct v4l2_ext_controls *cs,
1511 struct ctrl_helper *helpers)
1515 for (i = 0; i < cs->count; i++) {
1516 struct v4l2_ext_control *c = &cs->controls[i];
1517 struct v4l2_ctrl *ctrl;
1518 u32 id = c->id & V4L2_CTRL_ID_MASK;
1522 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1525 /* Old-style private controls are not allowed for
1526 extended controls */
1527 if (id >= V4L2_CID_PRIVATE_BASE)
1529 ctrl = v4l2_ctrl_find(hdl, id);
1532 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1535 helpers[i].ctrl = ctrl;
1536 helpers[i].handled = false;
1541 typedef int (*cluster_func)(struct v4l2_ext_control *c,
1542 struct v4l2_ctrl *ctrl);
1544 /* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1545 and call the provided function. */
1546 static int cluster_walk(unsigned from,
1547 struct v4l2_ext_controls *cs,
1548 struct ctrl_helper *helpers,
1551 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1555 /* Find any controls from the same cluster and call the function */
1556 for (i = from; !ret && i < cs->count; i++) {
1557 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1559 if (!helpers[i].handled && ctrl->cluster == cluster)
1560 ret = f(&cs->controls[i], ctrl);
1565 static void cluster_done(unsigned from,
1566 struct v4l2_ext_controls *cs,
1567 struct ctrl_helper *helpers)
1569 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1572 /* Find any controls from the same cluster and mark them as handled */
1573 for (i = from; i < cs->count; i++)
1574 if (helpers[i].ctrl->cluster == cluster)
1575 helpers[i].handled = true;
1578 /* Handles the corner case where cs->count == 0. It checks whether the
1579 specified control class exists. If that class ID is 0, then it checks
1580 whether there are any controls at all. */
1581 static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1583 if (ctrl_class == 0)
1584 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1585 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1590 /* Get extended controls. Allocates the helpers array if needed. */
1591 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1593 struct ctrl_helper helper[4];
1594 struct ctrl_helper *helpers = helper;
1598 cs->error_idx = cs->count;
1599 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1605 return class_check(hdl, cs->ctrl_class);
1607 if (cs->count > ARRAY_SIZE(helper)) {
1608 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1609 if (helpers == NULL)
1613 ret = prepare_ext_ctrls(hdl, cs, helpers);
1614 cs->error_idx = cs->count;
1616 for (i = 0; !ret && i < cs->count; i++)
1617 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1620 for (i = 0; !ret && i < cs->count; i++) {
1621 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1622 struct v4l2_ctrl *master = ctrl->cluster[0];
1625 if (helpers[i].handled)
1630 /* Any volatile controls requested from this cluster? */
1631 has_volatiles = ctrl->is_volatile;
1632 if (!has_volatiles && has_op(master, g_volatile_ctrl) &&
1633 master->ncontrols > 1)
1634 has_volatiles = cluster_walk(i, cs, helpers,
1637 v4l2_ctrl_lock(master);
1639 /* g_volatile_ctrl will update the new control values */
1640 if (has_volatiles && !is_cur_manual(master)) {
1641 for (j = 0; j < master->ncontrols; j++)
1642 cur_to_new(master->cluster[j]);
1643 ret = call_op(master, g_volatile_ctrl);
1645 /* If OK, then copy the current (for non-volatile controls)
1646 or the new (for volatile controls) control values to the
1649 ret = cluster_walk(i, cs, helpers, ctrl_to_user);
1650 v4l2_ctrl_unlock(master);
1651 cluster_done(i, cs, helpers);
1654 if (cs->count > ARRAY_SIZE(helper))
1658 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1660 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1662 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1664 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1666 /* Helper function to get a single control */
1667 static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1669 struct v4l2_ctrl *master = ctrl->cluster[0];
1673 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1676 v4l2_ctrl_lock(master);
1677 /* g_volatile_ctrl will update the current control values */
1678 if (ctrl->is_volatile && !is_cur_manual(master)) {
1679 for (i = 0; i < master->ncontrols; i++)
1680 cur_to_new(master->cluster[i]);
1681 ret = call_op(master, g_volatile_ctrl);
1684 *val = ctrl->cur.val;
1686 v4l2_ctrl_unlock(master);
1690 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1692 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1694 if (ctrl == NULL || !type_is_int(ctrl))
1696 return get_ctrl(ctrl, &control->value);
1698 EXPORT_SYMBOL(v4l2_g_ctrl);
1700 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1702 return v4l2_g_ctrl(sd->ctrl_handler, control);
1704 EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1706 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1710 /* It's a driver bug if this happens. */
1711 WARN_ON(!type_is_int(ctrl));
1712 get_ctrl(ctrl, &val);
1715 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1718 /* Core function that calls try/s_ctrl and ensures that the new value is
1719 copied to the current value on a set.
1720 Must be called with ctrl->handler->lock held. */
1721 static int try_or_set_control_cluster(struct v4l2_fh *fh,
1722 struct v4l2_ctrl *master, bool set)
1729 /* Go through the cluster and either validate the new value or
1730 (if no new value was set), copy the current value to the new
1731 value, ensuring a consistent view for the control ops when
1733 for (i = 0; !ret && i < master->ncontrols; i++) {
1734 struct v4l2_ctrl *ctrl = master->cluster[i];
1740 /* Double check this: it may have changed since the
1741 last check in try_or_set_ext_ctrls(). */
1742 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1745 /* Validate if required */
1747 ret = validate_new(ctrl);
1750 /* No new value was set, so copy the current and force
1751 a call to try_ctrl later, since the values for the cluster
1752 may now have changed and the end result might be invalid. */
1757 /* For larger clusters you have to call try_ctrl again to
1758 verify that the controls are still valid after the
1759 'cur_to_new' above. */
1761 ret = call_op(master, try_ctrl);
1763 /* Don't set if there is no change */
1764 if (ret || !set || !cluster_changed(master))
1766 ret = call_op(master, s_ctrl);
1767 /* If OK, then make the new values permanent. */
1771 update_flag = is_cur_manual(master) != is_new_manual(master);
1772 for (i = 0; i < master->ncontrols; i++)
1773 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
1777 /* Try or set controls. */
1778 static int try_or_set_ext_ctrls(struct v4l2_fh *fh,
1779 struct v4l2_ctrl_handler *hdl,
1780 struct v4l2_ext_controls *cs,
1781 struct ctrl_helper *helpers,
1787 for (i = 0; i < cs->count; i++) {
1788 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1792 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1794 /* This test is also done in try_set_control_cluster() which
1795 is called in atomic context, so that has the final say,
1796 but it makes sense to do an up-front check as well. Once
1797 an error occurs in try_set_control_cluster() some other
1798 controls may have been set already and we want to do a
1799 best-effort to avoid that. */
1800 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1804 for (i = 0; !ret && i < cs->count; i++) {
1805 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1806 struct v4l2_ctrl *master = ctrl->cluster[0];
1808 if (helpers[i].handled)
1812 v4l2_ctrl_lock(ctrl);
1814 /* Reset the 'is_new' flags of the cluster */
1815 for (j = 0; j < master->ncontrols; j++)
1816 if (master->cluster[j])
1817 master->cluster[j]->is_new = 0;
1819 /* Copy the new caller-supplied control values.
1820 user_to_new() sets 'is_new' to 1. */
1821 ret = cluster_walk(i, cs, helpers, user_to_new);
1824 ret = try_or_set_control_cluster(fh, master, set);
1826 /* Copy the new values back to userspace. */
1828 ret = cluster_walk(i, cs, helpers, new_to_user);
1830 v4l2_ctrl_unlock(ctrl);
1831 cluster_done(i, cs, helpers);
1836 /* Try or try-and-set controls */
1837 static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1838 struct v4l2_ext_controls *cs,
1841 struct ctrl_helper helper[4];
1842 struct ctrl_helper *helpers = helper;
1846 cs->error_idx = cs->count;
1847 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1853 return class_check(hdl, cs->ctrl_class);
1855 if (cs->count > ARRAY_SIZE(helper)) {
1856 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1860 ret = prepare_ext_ctrls(hdl, cs, helpers);
1862 /* First 'try' all controls and abort on error */
1864 ret = try_or_set_ext_ctrls(NULL, hdl, cs, helpers, false);
1865 /* If this is a 'set' operation and the initial 'try' failed,
1866 then set error_idx to count to tell the application that no
1867 controls changed value yet. */
1869 cs->error_idx = cs->count;
1871 /* Reset 'handled' state */
1872 for (i = 0; i < cs->count; i++)
1873 helpers[i].handled = false;
1874 ret = try_or_set_ext_ctrls(fh, hdl, cs, helpers, true);
1877 if (cs->count > ARRAY_SIZE(helper))
1882 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1884 return try_set_ext_ctrls(NULL, hdl, cs, false);
1886 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1888 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1889 struct v4l2_ext_controls *cs)
1891 return try_set_ext_ctrls(fh, hdl, cs, true);
1893 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1895 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1897 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
1899 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1901 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1903 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
1905 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1907 /* Helper function for VIDIOC_S_CTRL compatibility */
1908 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
1910 struct v4l2_ctrl *master = ctrl->cluster[0];
1914 v4l2_ctrl_lock(ctrl);
1916 /* Reset the 'is_new' flags of the cluster */
1917 for (i = 0; i < master->ncontrols; i++)
1918 if (master->cluster[i])
1919 master->cluster[i]->is_new = 0;
1923 ret = try_or_set_control_cluster(NULL, master, false);
1925 ret = try_or_set_control_cluster(fh, master, true);
1926 *val = ctrl->cur.val;
1927 v4l2_ctrl_unlock(ctrl);
1931 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1932 struct v4l2_control *control)
1934 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1936 if (ctrl == NULL || !type_is_int(ctrl))
1939 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1942 return set_ctrl(fh, ctrl, &control->value);
1944 EXPORT_SYMBOL(v4l2_s_ctrl);
1946 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1948 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
1950 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
1952 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
1954 /* It's a driver bug if this happens. */
1955 WARN_ON(!type_is_int(ctrl));
1956 return set_ctrl(NULL, ctrl, &val);
1958 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);