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-event.h>
27 #include <media/v4l2-dev.h>
29 #define has_op(master, op) \
30 (master->ops && master->ops->op)
31 #define call_op(master, op) \
32 (has_op(master, op) ? master->ops->op(master) : 0)
34 /* Internal temporary helper struct, one for each v4l2_ext_control */
36 /* The control corresponding to the v4l2_ext_control ID field. */
37 struct v4l2_ctrl *ctrl;
38 /* Used internally to mark whether this control was already
43 /* Small helper function to determine if the autocluster is set to manual
44 mode. In that case the is_volatile flag should be ignored. */
45 static bool is_cur_manual(const struct v4l2_ctrl *master)
47 return master->is_auto && master->cur.val == master->manual_mode_value;
50 /* Same as above, but this checks the against the new value instead of the
52 static bool is_new_manual(const struct v4l2_ctrl *master)
54 return master->is_auto && master->val == master->manual_mode_value;
57 /* Returns NULL or a character pointer array containing the menu for
58 the given control ID. The pointer array ends with a NULL pointer.
59 An empty string signifies a menu entry that is invalid. This allows
60 drivers to disable certain options if it is not supported. */
61 const char * const *v4l2_ctrl_get_menu(u32 id)
63 static const char * const mpeg_audio_sampling_freq[] = {
69 static const char * const mpeg_audio_encoding[] = {
77 static const char * const mpeg_audio_l1_bitrate[] = {
94 static const char * const mpeg_audio_l2_bitrate[] = {
111 static const char * const mpeg_audio_l3_bitrate[] = {
128 static const char * const mpeg_audio_ac3_bitrate[] = {
150 static const char * const mpeg_audio_mode[] = {
157 static const char * const mpeg_audio_mode_extension[] = {
164 static const char * const mpeg_audio_emphasis[] = {
170 static const char * const mpeg_audio_crc[] = {
175 static const char * const mpeg_video_encoding[] = {
181 static const char * const mpeg_video_aspect[] = {
188 static const char * const mpeg_video_bitrate_mode[] = {
193 static const char * const mpeg_stream_type[] = {
194 "MPEG-2 Program Stream",
195 "MPEG-2 Transport Stream",
196 "MPEG-1 System Stream",
197 "MPEG-2 DVD-compatible Stream",
198 "MPEG-1 VCD-compatible Stream",
199 "MPEG-2 SVCD-compatible Stream",
202 static const char * const mpeg_stream_vbi_fmt[] = {
204 "Private packet, IVTV format",
207 static const char * const camera_power_line_frequency[] = {
213 static const char * const camera_exposure_auto[] = {
216 "Shutter Priority Mode",
217 "Aperture Priority Mode",
220 static const char * const colorfx[] = {
233 static const char * const tune_preemphasis[] = {
241 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
242 return mpeg_audio_sampling_freq;
243 case V4L2_CID_MPEG_AUDIO_ENCODING:
244 return mpeg_audio_encoding;
245 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
246 return mpeg_audio_l1_bitrate;
247 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
248 return mpeg_audio_l2_bitrate;
249 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
250 return mpeg_audio_l3_bitrate;
251 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
252 return mpeg_audio_ac3_bitrate;
253 case V4L2_CID_MPEG_AUDIO_MODE:
254 return mpeg_audio_mode;
255 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
256 return mpeg_audio_mode_extension;
257 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
258 return mpeg_audio_emphasis;
259 case V4L2_CID_MPEG_AUDIO_CRC:
260 return mpeg_audio_crc;
261 case V4L2_CID_MPEG_VIDEO_ENCODING:
262 return mpeg_video_encoding;
263 case V4L2_CID_MPEG_VIDEO_ASPECT:
264 return mpeg_video_aspect;
265 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
266 return mpeg_video_bitrate_mode;
267 case V4L2_CID_MPEG_STREAM_TYPE:
268 return mpeg_stream_type;
269 case V4L2_CID_MPEG_STREAM_VBI_FMT:
270 return mpeg_stream_vbi_fmt;
271 case V4L2_CID_POWER_LINE_FREQUENCY:
272 return camera_power_line_frequency;
273 case V4L2_CID_EXPOSURE_AUTO:
274 return camera_exposure_auto;
275 case V4L2_CID_COLORFX:
277 case V4L2_CID_TUNE_PREEMPHASIS:
278 return tune_preemphasis;
283 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
285 /* Return the control name. */
286 const char *v4l2_ctrl_get_name(u32 id)
290 /* Keep the order of the 'case's the same as in videodev2.h! */
291 case V4L2_CID_USER_CLASS: return "User Controls";
292 case V4L2_CID_BRIGHTNESS: return "Brightness";
293 case V4L2_CID_CONTRAST: return "Contrast";
294 case V4L2_CID_SATURATION: return "Saturation";
295 case V4L2_CID_HUE: return "Hue";
296 case V4L2_CID_AUDIO_VOLUME: return "Volume";
297 case V4L2_CID_AUDIO_BALANCE: return "Balance";
298 case V4L2_CID_AUDIO_BASS: return "Bass";
299 case V4L2_CID_AUDIO_TREBLE: return "Treble";
300 case V4L2_CID_AUDIO_MUTE: return "Mute";
301 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
302 case V4L2_CID_BLACK_LEVEL: return "Black Level";
303 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
304 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
305 case V4L2_CID_RED_BALANCE: return "Red Balance";
306 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
307 case V4L2_CID_GAMMA: return "Gamma";
308 case V4L2_CID_EXPOSURE: return "Exposure";
309 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
310 case V4L2_CID_GAIN: return "Gain";
311 case V4L2_CID_HFLIP: return "Horizontal Flip";
312 case V4L2_CID_VFLIP: return "Vertical Flip";
313 case V4L2_CID_HCENTER: return "Horizontal Center";
314 case V4L2_CID_VCENTER: return "Vertical Center";
315 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
316 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
317 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
318 case V4L2_CID_SHARPNESS: return "Sharpness";
319 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
320 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
321 case V4L2_CID_COLOR_KILLER: return "Color Killer";
322 case V4L2_CID_COLORFX: return "Color Effects";
323 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
324 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
325 case V4L2_CID_ROTATE: return "Rotate";
326 case V4L2_CID_BG_COLOR: return "Background Color";
327 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
328 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
329 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
332 /* Keep the order of the 'case's the same as in videodev2.h! */
333 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
334 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
335 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
336 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
337 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
338 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
339 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
340 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
341 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
342 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
343 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
344 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
345 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
346 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
347 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
348 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
349 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
350 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
351 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
352 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
353 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
354 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
355 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
356 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
357 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
358 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
359 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
360 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
361 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
362 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
363 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
364 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
365 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
367 /* CAMERA controls */
368 /* Keep the order of the 'case's the same as in videodev2.h! */
369 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
370 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
371 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
372 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
373 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
374 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
375 case V4L2_CID_PAN_RESET: return "Pan, Reset";
376 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
377 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
378 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
379 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
380 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
381 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
382 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
383 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
384 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
385 case V4L2_CID_PRIVACY: return "Privacy";
386 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
387 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
389 /* FM Radio Modulator control */
390 /* Keep the order of the 'case's the same as in videodev2.h! */
391 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
392 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
393 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
394 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
395 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
396 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
397 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
398 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
399 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
400 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
401 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
402 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
403 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
404 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
405 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
406 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
407 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
408 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
409 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
410 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
416 EXPORT_SYMBOL(v4l2_ctrl_get_name);
418 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
419 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
421 *name = v4l2_ctrl_get_name(id);
425 case V4L2_CID_AUDIO_MUTE:
426 case V4L2_CID_AUDIO_LOUDNESS:
427 case V4L2_CID_AUTO_WHITE_BALANCE:
428 case V4L2_CID_AUTOGAIN:
431 case V4L2_CID_HUE_AUTO:
432 case V4L2_CID_CHROMA_AGC:
433 case V4L2_CID_COLOR_KILLER:
434 case V4L2_CID_MPEG_AUDIO_MUTE:
435 case V4L2_CID_MPEG_VIDEO_MUTE:
436 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
437 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
438 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
439 case V4L2_CID_FOCUS_AUTO:
440 case V4L2_CID_PRIVACY:
441 case V4L2_CID_AUDIO_LIMITER_ENABLED:
442 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
443 case V4L2_CID_PILOT_TONE_ENABLED:
444 case V4L2_CID_ILLUMINATORS_1:
445 case V4L2_CID_ILLUMINATORS_2:
446 *type = V4L2_CTRL_TYPE_BOOLEAN;
450 case V4L2_CID_PAN_RESET:
451 case V4L2_CID_TILT_RESET:
452 *type = V4L2_CTRL_TYPE_BUTTON;
453 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
454 *min = *max = *step = *def = 0;
456 case V4L2_CID_POWER_LINE_FREQUENCY:
457 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
458 case V4L2_CID_MPEG_AUDIO_ENCODING:
459 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
460 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
461 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
462 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
463 case V4L2_CID_MPEG_AUDIO_MODE:
464 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
465 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
466 case V4L2_CID_MPEG_AUDIO_CRC:
467 case V4L2_CID_MPEG_VIDEO_ENCODING:
468 case V4L2_CID_MPEG_VIDEO_ASPECT:
469 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
470 case V4L2_CID_MPEG_STREAM_TYPE:
471 case V4L2_CID_MPEG_STREAM_VBI_FMT:
472 case V4L2_CID_EXPOSURE_AUTO:
473 case V4L2_CID_COLORFX:
474 case V4L2_CID_TUNE_PREEMPHASIS:
475 *type = V4L2_CTRL_TYPE_MENU;
477 case V4L2_CID_RDS_TX_PS_NAME:
478 case V4L2_CID_RDS_TX_RADIO_TEXT:
479 *type = V4L2_CTRL_TYPE_STRING;
481 case V4L2_CID_USER_CLASS:
482 case V4L2_CID_CAMERA_CLASS:
483 case V4L2_CID_MPEG_CLASS:
484 case V4L2_CID_FM_TX_CLASS:
485 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
486 /* You can neither read not write these */
487 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
488 *min = *max = *step = *def = 0;
490 case V4L2_CID_BG_COLOR:
491 *type = V4L2_CTRL_TYPE_INTEGER;
494 /* Max is calculated as RGB888 that is 2^24 */
498 *type = V4L2_CTRL_TYPE_INTEGER;
502 case V4L2_CID_MPEG_AUDIO_ENCODING:
503 case V4L2_CID_MPEG_AUDIO_MODE:
504 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
505 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
506 case V4L2_CID_MPEG_STREAM_TYPE:
507 *flags |= V4L2_CTRL_FLAG_UPDATE;
509 case V4L2_CID_AUDIO_VOLUME:
510 case V4L2_CID_AUDIO_BALANCE:
511 case V4L2_CID_AUDIO_BASS:
512 case V4L2_CID_AUDIO_TREBLE:
513 case V4L2_CID_BRIGHTNESS:
514 case V4L2_CID_CONTRAST:
515 case V4L2_CID_SATURATION:
517 case V4L2_CID_RED_BALANCE:
518 case V4L2_CID_BLUE_BALANCE:
520 case V4L2_CID_SHARPNESS:
521 case V4L2_CID_CHROMA_GAIN:
522 case V4L2_CID_RDS_TX_DEVIATION:
523 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
524 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
525 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
526 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
527 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
528 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
529 case V4L2_CID_PILOT_TONE_DEVIATION:
530 case V4L2_CID_PILOT_TONE_FREQUENCY:
531 case V4L2_CID_TUNE_POWER_LEVEL:
532 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
533 *flags |= V4L2_CTRL_FLAG_SLIDER;
535 case V4L2_CID_PAN_RELATIVE:
536 case V4L2_CID_TILT_RELATIVE:
537 case V4L2_CID_FOCUS_RELATIVE:
538 case V4L2_CID_IRIS_RELATIVE:
539 case V4L2_CID_ZOOM_RELATIVE:
540 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
544 EXPORT_SYMBOL(v4l2_ctrl_fill);
546 /* Helper function to determine whether the control type is compatible with
548 static bool type_is_int(const struct v4l2_ctrl *ctrl)
550 switch (ctrl->type) {
551 case V4L2_CTRL_TYPE_INTEGER64:
552 case V4L2_CTRL_TYPE_STRING:
553 /* Nope, these need v4l2_ext_control */
560 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
562 memset(ev->reserved, 0, sizeof(ev->reserved));
563 ev->type = V4L2_EVENT_CTRL;
565 ev->u.ctrl.changes = changes;
566 ev->u.ctrl.type = ctrl->type;
567 ev->u.ctrl.flags = ctrl->flags;
568 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
569 ev->u.ctrl.value64 = 0;
571 ev->u.ctrl.value64 = ctrl->cur.val64;
572 ev->u.ctrl.minimum = ctrl->minimum;
573 ev->u.ctrl.maximum = ctrl->maximum;
574 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
577 ev->u.ctrl.step = ctrl->step;
578 ev->u.ctrl.default_value = ctrl->default_value;
581 static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
583 struct v4l2_event ev;
584 struct v4l2_ctrl_fh *pos;
586 if (list_empty(&ctrl->fhs))
588 fill_event(&ev, ctrl, changes);
590 list_for_each_entry(pos, &ctrl->fhs, node)
592 v4l2_event_queue_fh(pos->fh, &ev);
595 /* Helper function: copy the current control value back to the caller */
596 static int cur_to_user(struct v4l2_ext_control *c,
597 struct v4l2_ctrl *ctrl)
601 switch (ctrl->type) {
602 case V4L2_CTRL_TYPE_STRING:
603 len = strlen(ctrl->cur.string);
604 if (c->size < len + 1) {
608 return copy_to_user(c->string, ctrl->cur.string,
609 len + 1) ? -EFAULT : 0;
610 case V4L2_CTRL_TYPE_INTEGER64:
611 c->value64 = ctrl->cur.val64;
614 c->value = ctrl->cur.val;
620 /* Helper function: copy the caller-provider value as the new control value */
621 static int user_to_new(struct v4l2_ext_control *c,
622 struct v4l2_ctrl *ctrl)
628 switch (ctrl->type) {
629 case V4L2_CTRL_TYPE_INTEGER64:
630 ctrl->val64 = c->value64;
632 case V4L2_CTRL_TYPE_STRING:
636 if (size > ctrl->maximum + 1)
637 size = ctrl->maximum + 1;
638 ret = copy_from_user(ctrl->string, c->string, size);
640 char last = ctrl->string[size - 1];
642 ctrl->string[size - 1] = 0;
643 /* If the string was longer than ctrl->maximum,
644 then return an error. */
645 if (strlen(ctrl->string) == ctrl->maximum && last)
648 return ret ? -EFAULT : 0;
650 ctrl->val = c->value;
656 /* Helper function: copy the new control value back to the caller */
657 static int new_to_user(struct v4l2_ext_control *c,
658 struct v4l2_ctrl *ctrl)
662 switch (ctrl->type) {
663 case V4L2_CTRL_TYPE_STRING:
664 len = strlen(ctrl->string);
665 if (c->size < len + 1) {
666 c->size = ctrl->maximum + 1;
669 return copy_to_user(c->string, ctrl->string,
670 len + 1) ? -EFAULT : 0;
671 case V4L2_CTRL_TYPE_INTEGER64:
672 c->value64 = ctrl->val64;
675 c->value = ctrl->val;
681 static int ctrl_to_user(struct v4l2_ext_control *c,
682 struct v4l2_ctrl *ctrl)
684 if (ctrl->is_volatile)
685 return new_to_user(c, ctrl);
686 return cur_to_user(c, ctrl);
689 static int ctrl_is_volatile(struct v4l2_ext_control *c,
690 struct v4l2_ctrl *ctrl)
692 return ctrl->is_volatile;
695 /* Copy the new value to the current value. */
696 static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
697 bool update_inactive)
699 bool changed = false;
703 switch (ctrl->type) {
704 case V4L2_CTRL_TYPE_BUTTON:
707 case V4L2_CTRL_TYPE_STRING:
708 /* strings are always 0-terminated */
709 changed = strcmp(ctrl->string, ctrl->cur.string);
710 strcpy(ctrl->cur.string, ctrl->string);
712 case V4L2_CTRL_TYPE_INTEGER64:
713 changed = ctrl->val64 != ctrl->cur.val64;
714 ctrl->cur.val64 = ctrl->val64;
717 changed = ctrl->val != ctrl->cur.val;
718 ctrl->cur.val = ctrl->val;
721 if (update_inactive) {
722 ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
723 if (!is_cur_manual(ctrl->cluster[0]))
724 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
726 if (changed || update_inactive)
728 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
729 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
732 /* Copy the current value to the new value */
733 static void cur_to_new(struct v4l2_ctrl *ctrl)
737 switch (ctrl->type) {
738 case V4L2_CTRL_TYPE_STRING:
739 /* strings are always 0-terminated */
740 strcpy(ctrl->string, ctrl->cur.string);
742 case V4L2_CTRL_TYPE_INTEGER64:
743 ctrl->val64 = ctrl->cur.val64;
746 ctrl->val = ctrl->cur.val;
751 /* Return non-zero if one or more of the controls in the cluster has a new
752 value that differs from the current value. */
753 static int cluster_changed(struct v4l2_ctrl *master)
758 for (i = 0; !diff && i < master->ncontrols; i++) {
759 struct v4l2_ctrl *ctrl = master->cluster[i];
763 switch (ctrl->type) {
764 case V4L2_CTRL_TYPE_BUTTON:
765 /* Button controls are always 'different' */
767 case V4L2_CTRL_TYPE_STRING:
768 /* strings are always 0-terminated */
769 diff = strcmp(ctrl->string, ctrl->cur.string);
771 case V4L2_CTRL_TYPE_INTEGER64:
772 diff = ctrl->val64 != ctrl->cur.val64;
775 diff = ctrl->val != ctrl->cur.val;
782 /* Validate a new control */
783 static int validate_new(struct v4l2_ctrl *ctrl)
786 char *s = ctrl->string;
790 switch (ctrl->type) {
791 case V4L2_CTRL_TYPE_INTEGER:
792 /* Round towards the closest legal value */
793 val += ctrl->step / 2;
794 if (val < ctrl->minimum)
796 if (val > ctrl->maximum)
798 offset = val - ctrl->minimum;
799 offset = ctrl->step * (offset / ctrl->step);
800 val = ctrl->minimum + offset;
804 case V4L2_CTRL_TYPE_BOOLEAN:
805 ctrl->val = !!ctrl->val;
808 case V4L2_CTRL_TYPE_MENU:
809 if (val < ctrl->minimum || val > ctrl->maximum)
811 if (ctrl->qmenu[val][0] == '\0' ||
812 (ctrl->menu_skip_mask & (1 << val)))
816 case V4L2_CTRL_TYPE_BUTTON:
817 case V4L2_CTRL_TYPE_CTRL_CLASS:
821 case V4L2_CTRL_TYPE_INTEGER64:
824 case V4L2_CTRL_TYPE_STRING:
826 if (len < ctrl->minimum)
828 if ((len - ctrl->minimum) % ctrl->step)
837 static inline u32 node2id(struct list_head *node)
839 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
842 /* Set the handler's error code if it wasn't set earlier already */
843 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
850 /* Initialize the handler */
851 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
852 unsigned nr_of_controls_hint)
854 mutex_init(&hdl->lock);
855 INIT_LIST_HEAD(&hdl->ctrls);
856 INIT_LIST_HEAD(&hdl->ctrl_refs);
857 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
858 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
860 hdl->error = hdl->buckets ? 0 : -ENOMEM;
863 EXPORT_SYMBOL(v4l2_ctrl_handler_init);
865 /* Free all controls and control refs */
866 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
868 struct v4l2_ctrl_ref *ref, *next_ref;
869 struct v4l2_ctrl *ctrl, *next_ctrl;
870 struct v4l2_ctrl_fh *ctrl_fh, *next_ctrl_fh;
872 if (hdl == NULL || hdl->buckets == NULL)
875 mutex_lock(&hdl->lock);
877 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
878 list_del(&ref->node);
881 /* Free all controls owned by the handler */
882 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
883 list_del(&ctrl->node);
884 list_for_each_entry_safe(ctrl_fh, next_ctrl_fh, &ctrl->fhs, node) {
885 list_del(&ctrl_fh->node);
894 mutex_unlock(&hdl->lock);
896 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
898 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
899 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
900 with applications that do not use the NEXT_CTRL flag.
902 We just find the n-th private user control. It's O(N), but that should not
903 be an issue in this particular case. */
904 static struct v4l2_ctrl_ref *find_private_ref(
905 struct v4l2_ctrl_handler *hdl, u32 id)
907 struct v4l2_ctrl_ref *ref;
909 id -= V4L2_CID_PRIVATE_BASE;
910 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
911 /* Search for private user controls that are compatible with
913 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
914 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
915 if (!type_is_int(ref->ctrl))
925 /* Find a control with the given ID. */
926 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
928 struct v4l2_ctrl_ref *ref;
931 id &= V4L2_CTRL_ID_MASK;
933 /* Old-style private controls need special handling */
934 if (id >= V4L2_CID_PRIVATE_BASE)
935 return find_private_ref(hdl, id);
936 bucket = id % hdl->nr_of_buckets;
938 /* Simple optimization: cache the last control found */
939 if (hdl->cached && hdl->cached->ctrl->id == id)
942 /* Not in cache, search the hash */
943 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
944 while (ref && ref->ctrl->id != id)
948 hdl->cached = ref; /* cache it! */
952 /* Find a control with the given ID. Take the handler's lock first. */
953 static struct v4l2_ctrl_ref *find_ref_lock(
954 struct v4l2_ctrl_handler *hdl, u32 id)
956 struct v4l2_ctrl_ref *ref = NULL;
959 mutex_lock(&hdl->lock);
960 ref = find_ref(hdl, id);
961 mutex_unlock(&hdl->lock);
966 /* Find a control with the given ID. */
967 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
969 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
971 return ref ? ref->ctrl : NULL;
973 EXPORT_SYMBOL(v4l2_ctrl_find);
975 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
976 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
977 struct v4l2_ctrl *ctrl)
979 struct v4l2_ctrl_ref *ref;
980 struct v4l2_ctrl_ref *new_ref;
982 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
983 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
985 /* Automatically add the control class if it is not yet present. */
986 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
987 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
993 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
995 return handler_set_err(hdl, -ENOMEM);
996 new_ref->ctrl = ctrl;
997 if (ctrl->handler == hdl) {
998 /* By default each control starts in a cluster of its own.
999 new_ref->ctrl is basically a cluster array with one
1000 element, so that's perfect to use as the cluster pointer.
1001 But only do this for the handler that owns the control. */
1002 ctrl->cluster = &new_ref->ctrl;
1003 ctrl->ncontrols = 1;
1006 INIT_LIST_HEAD(&new_ref->node);
1008 mutex_lock(&hdl->lock);
1010 /* Add immediately at the end of the list if the list is empty, or if
1011 the last element in the list has a lower ID.
1012 This ensures that when elements are added in ascending order the
1013 insertion is an O(1) operation. */
1014 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1015 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1017 goto insert_in_hash;
1020 /* Find insert position in sorted list */
1021 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1022 if (ref->ctrl->id < id)
1024 /* Don't add duplicates */
1025 if (ref->ctrl->id == id) {
1029 list_add(&new_ref->node, ref->node.prev);
1034 /* Insert the control node in the hash */
1035 new_ref->next = hdl->buckets[bucket];
1036 hdl->buckets[bucket] = new_ref;
1039 mutex_unlock(&hdl->lock);
1043 /* Add a new control */
1044 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1045 const struct v4l2_ctrl_ops *ops,
1046 u32 id, const char *name, enum v4l2_ctrl_type type,
1047 s32 min, s32 max, u32 step, s32 def,
1048 u32 flags, const char * const *qmenu, void *priv)
1050 struct v4l2_ctrl *ctrl;
1051 unsigned sz_extra = 0;
1057 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
1059 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
1060 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1061 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1062 handler_set_err(hdl, -ERANGE);
1065 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1066 type == V4L2_CTRL_TYPE_MENU ||
1067 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1068 (def < min || def > max)) {
1069 handler_set_err(hdl, -ERANGE);
1073 if (type == V4L2_CTRL_TYPE_BUTTON)
1074 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1075 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1076 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1077 else if (type == V4L2_CTRL_TYPE_STRING)
1078 sz_extra += 2 * (max + 1);
1080 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1082 handler_set_err(hdl, -ENOMEM);
1086 INIT_LIST_HEAD(&ctrl->node);
1087 INIT_LIST_HEAD(&ctrl->fhs);
1088 ctrl->handler = hdl;
1093 ctrl->flags = flags;
1094 ctrl->minimum = min;
1095 ctrl->maximum = max;
1097 ctrl->qmenu = qmenu;
1099 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1101 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1102 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1103 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1105 memset(ctrl->cur.string, ' ', ctrl->minimum);
1107 if (handler_new_ref(hdl, ctrl)) {
1111 mutex_lock(&hdl->lock);
1112 list_add_tail(&ctrl->node, &hdl->ctrls);
1113 mutex_unlock(&hdl->lock);
1117 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1118 const struct v4l2_ctrl_config *cfg, void *priv)
1121 struct v4l2_ctrl *ctrl;
1122 const char *name = cfg->name;
1123 const char * const *qmenu = cfg->qmenu;
1124 enum v4l2_ctrl_type type = cfg->type;
1125 u32 flags = cfg->flags;
1128 u32 step = cfg->step;
1132 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1135 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1139 WARN_ON(cfg->menu_skip_mask);
1140 if (is_menu && qmenu == NULL)
1141 qmenu = v4l2_ctrl_get_menu(cfg->id);
1143 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1145 is_menu ? cfg->menu_skip_mask : step,
1146 def, flags, qmenu, priv);
1148 ctrl->is_private = cfg->is_private;
1149 ctrl->is_volatile = cfg->is_volatile;
1153 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1155 /* Helper function for standard non-menu controls */
1156 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1157 const struct v4l2_ctrl_ops *ops,
1158 u32 id, s32 min, s32 max, u32 step, s32 def)
1161 enum v4l2_ctrl_type type;
1164 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1165 if (type == V4L2_CTRL_TYPE_MENU) {
1166 handler_set_err(hdl, -EINVAL);
1169 return v4l2_ctrl_new(hdl, ops, id, name, type,
1170 min, max, step, def, flags, NULL, NULL);
1172 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1174 /* Helper function for standard menu controls */
1175 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1176 const struct v4l2_ctrl_ops *ops,
1177 u32 id, s32 max, s32 mask, s32 def)
1179 const char * const *qmenu = v4l2_ctrl_get_menu(id);
1181 enum v4l2_ctrl_type type;
1186 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1187 if (type != V4L2_CTRL_TYPE_MENU) {
1188 handler_set_err(hdl, -EINVAL);
1191 return v4l2_ctrl_new(hdl, ops, id, name, type,
1192 0, max, mask, def, flags, qmenu, NULL);
1194 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1196 /* Add a control from another handler to this handler */
1197 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1198 struct v4l2_ctrl *ctrl)
1200 if (hdl == NULL || hdl->error)
1203 handler_set_err(hdl, -EINVAL);
1206 if (ctrl->handler == hdl)
1208 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1210 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1212 /* Add the controls from another handler to our own. */
1213 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1214 struct v4l2_ctrl_handler *add)
1216 struct v4l2_ctrl *ctrl;
1219 /* Do nothing if either handler is NULL or if they are the same */
1220 if (!hdl || !add || hdl == add)
1224 mutex_lock(&add->lock);
1225 list_for_each_entry(ctrl, &add->ctrls, node) {
1226 /* Skip handler-private controls. */
1227 if (ctrl->is_private)
1229 /* And control classes */
1230 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1232 ret = handler_new_ref(hdl, ctrl);
1236 mutex_unlock(&add->lock);
1239 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1241 /* Cluster controls */
1242 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1246 /* The first control is the master control and it must not be NULL */
1247 BUG_ON(ncontrols == 0 || controls[0] == NULL);
1249 for (i = 0; i < ncontrols; i++) {
1251 controls[i]->cluster = controls;
1252 controls[i]->ncontrols = ncontrols;
1256 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1258 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1259 u8 manual_val, bool set_volatile)
1261 struct v4l2_ctrl *master = controls[0];
1265 v4l2_ctrl_cluster(ncontrols, controls);
1266 WARN_ON(ncontrols <= 1);
1267 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
1268 master->is_auto = true;
1269 master->manual_mode_value = manual_val;
1270 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1271 flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
1273 for (i = 1; i < ncontrols; i++)
1275 controls[i]->is_volatile = set_volatile;
1276 controls[i]->flags |= flag;
1279 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1281 /* Activate/deactivate a control. */
1282 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1284 /* invert since the actual flag is called 'inactive' */
1285 bool inactive = !active;
1292 /* set V4L2_CTRL_FLAG_INACTIVE */
1293 old = test_and_set_bit(4, &ctrl->flags);
1295 /* clear V4L2_CTRL_FLAG_INACTIVE */
1296 old = test_and_clear_bit(4, &ctrl->flags);
1297 if (old != inactive)
1298 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1300 EXPORT_SYMBOL(v4l2_ctrl_activate);
1302 /* Grab/ungrab a control.
1303 Typically used when streaming starts and you want to grab controls,
1304 preventing the user from changing them.
1306 Just call this and the framework will block any attempts to change
1308 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1315 v4l2_ctrl_lock(ctrl);
1317 /* set V4L2_CTRL_FLAG_GRABBED */
1318 old = test_and_set_bit(1, &ctrl->flags);
1320 /* clear V4L2_CTRL_FLAG_GRABBED */
1321 old = test_and_clear_bit(1, &ctrl->flags);
1323 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1324 v4l2_ctrl_unlock(ctrl);
1326 EXPORT_SYMBOL(v4l2_ctrl_grab);
1328 /* Log the control name and value */
1329 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1330 const char *prefix, const char *colon)
1332 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1333 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1335 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1337 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1340 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1342 switch (ctrl->type) {
1343 case V4L2_CTRL_TYPE_INTEGER:
1344 printk(KERN_CONT "%d", ctrl->cur.val);
1346 case V4L2_CTRL_TYPE_BOOLEAN:
1347 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1349 case V4L2_CTRL_TYPE_MENU:
1350 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1352 case V4L2_CTRL_TYPE_INTEGER64:
1353 printk(KERN_CONT "%lld", ctrl->cur.val64);
1355 case V4L2_CTRL_TYPE_STRING:
1356 printk(KERN_CONT "%s", ctrl->cur.string);
1359 printk(KERN_CONT "unknown type %d", ctrl->type);
1362 if (fl_inact && fl_grabbed)
1363 printk(KERN_CONT " (inactive, grabbed)\n");
1365 printk(KERN_CONT " (inactive)\n");
1366 else if (fl_grabbed)
1367 printk(KERN_CONT " (grabbed)\n");
1369 printk(KERN_CONT "\n");
1372 /* Log all controls owned by the handler */
1373 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1376 struct v4l2_ctrl *ctrl;
1377 const char *colon = "";
1384 len = strlen(prefix);
1385 if (len && prefix[len - 1] != ' ')
1387 mutex_lock(&hdl->lock);
1388 list_for_each_entry(ctrl, &hdl->ctrls, node)
1389 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1390 log_ctrl(ctrl, prefix, colon);
1391 mutex_unlock(&hdl->lock);
1393 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1395 /* Call s_ctrl for all controls owned by the handler */
1396 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1398 struct v4l2_ctrl *ctrl;
1403 mutex_lock(&hdl->lock);
1404 list_for_each_entry(ctrl, &hdl->ctrls, node)
1407 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1408 struct v4l2_ctrl *master = ctrl->cluster[0];
1411 /* Skip if this control was already handled by a cluster. */
1415 for (i = 0; i < master->ncontrols; i++) {
1416 if (master->cluster[i]) {
1417 cur_to_new(master->cluster[i]);
1418 master->cluster[i]->is_new = 1;
1422 /* Skip button controls and read-only controls. */
1423 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1424 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1426 ret = call_op(master, s_ctrl);
1429 for (i = 0; i < master->ncontrols; i++)
1430 if (master->cluster[i])
1431 master->cluster[i]->done = true;
1433 mutex_unlock(&hdl->lock);
1436 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1438 /* Implement VIDIOC_QUERYCTRL */
1439 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1441 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1442 struct v4l2_ctrl_ref *ref;
1443 struct v4l2_ctrl *ctrl;
1448 mutex_lock(&hdl->lock);
1450 /* Try to find it */
1451 ref = find_ref(hdl, id);
1453 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1454 /* Find the next control with ID > qc->id */
1456 /* Did we reach the end of the control list? */
1457 if (id >= node2id(hdl->ctrl_refs.prev)) {
1458 ref = NULL; /* Yes, so there is no next control */
1460 /* We found a control with the given ID, so just get
1461 the next one in the list. */
1462 ref = list_entry(ref->node.next, typeof(*ref), node);
1464 /* No control with the given ID exists, so start
1465 searching for the next largest ID. We know there
1466 is one, otherwise the first 'if' above would have
1468 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1469 if (id < ref->ctrl->id)
1473 mutex_unlock(&hdl->lock);
1478 memset(qc, 0, sizeof(*qc));
1479 if (id >= V4L2_CID_PRIVATE_BASE)
1483 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1484 qc->minimum = ctrl->minimum;
1485 qc->maximum = ctrl->maximum;
1486 qc->default_value = ctrl->default_value;
1487 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
1490 qc->step = ctrl->step;
1491 qc->flags = ctrl->flags;
1492 qc->type = ctrl->type;
1495 EXPORT_SYMBOL(v4l2_queryctrl);
1497 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1499 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1501 return v4l2_queryctrl(sd->ctrl_handler, qc);
1503 EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1505 /* Implement VIDIOC_QUERYMENU */
1506 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1508 struct v4l2_ctrl *ctrl;
1511 ctrl = v4l2_ctrl_find(hdl, qm->id);
1517 if (ctrl->qmenu == NULL ||
1518 i < ctrl->minimum || i > ctrl->maximum)
1520 /* Use mask to see if this menu item should be skipped */
1521 if (ctrl->menu_skip_mask & (1 << i))
1523 /* Empty menu items should also be skipped */
1524 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1526 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1529 EXPORT_SYMBOL(v4l2_querymenu);
1531 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1533 return v4l2_querymenu(sd->ctrl_handler, qm);
1535 EXPORT_SYMBOL(v4l2_subdev_querymenu);
1539 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1541 It is not a fully atomic operation, just best-effort only. After all, if
1542 multiple controls have to be set through multiple i2c writes (for example)
1543 then some initial writes may succeed while others fail. Thus leaving the
1544 system in an inconsistent state. The question is how much effort you are
1545 willing to spend on trying to make something atomic that really isn't.
1547 From the point of view of an application the main requirement is that
1548 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1549 error should be returned without actually affecting any controls.
1551 If all the values are correct, then it is acceptable to just give up
1552 in case of low-level errors.
1554 It is important though that the application can tell when only a partial
1555 configuration was done. The way we do that is through the error_idx field
1556 of struct v4l2_ext_controls: if that is equal to the count field then no
1557 controls were affected. Otherwise all controls before that index were
1558 successful in performing their 'get' or 'set' operation, the control at
1559 the given index failed, and you don't know what happened with the controls
1560 after the failed one. Since if they were part of a control cluster they
1561 could have been successfully processed (if a cluster member was encountered
1562 at index < error_idx), they could have failed (if a cluster member was at
1563 error_idx), or they may not have been processed yet (if the first cluster
1564 member appeared after error_idx).
1566 It is all fairly theoretical, though. In practice all you can do is to
1567 bail out. If error_idx == count, then it is an application bug. If
1568 error_idx < count then it is only an application bug if the error code was
1569 EBUSY. That usually means that something started streaming just when you
1570 tried to set the controls. In all other cases it is a driver/hardware
1571 problem and all you can do is to retry or bail out.
1573 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1574 never modifies controls the error_idx is just set to whatever control
1575 has an invalid value.
1578 /* Prepare for the extended g/s/try functions.
1579 Find the controls in the control array and do some basic checks. */
1580 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1581 struct v4l2_ext_controls *cs,
1582 struct ctrl_helper *helpers)
1586 for (i = 0; i < cs->count; i++) {
1587 struct v4l2_ext_control *c = &cs->controls[i];
1588 struct v4l2_ctrl *ctrl;
1589 u32 id = c->id & V4L2_CTRL_ID_MASK;
1593 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1596 /* Old-style private controls are not allowed for
1597 extended controls */
1598 if (id >= V4L2_CID_PRIVATE_BASE)
1600 ctrl = v4l2_ctrl_find(hdl, id);
1603 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1606 helpers[i].ctrl = ctrl;
1607 helpers[i].handled = false;
1612 typedef int (*cluster_func)(struct v4l2_ext_control *c,
1613 struct v4l2_ctrl *ctrl);
1615 /* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1616 and call the provided function. */
1617 static int cluster_walk(unsigned from,
1618 struct v4l2_ext_controls *cs,
1619 struct ctrl_helper *helpers,
1622 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1626 /* Find any controls from the same cluster and call the function */
1627 for (i = from; !ret && i < cs->count; i++) {
1628 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1630 if (!helpers[i].handled && ctrl->cluster == cluster)
1631 ret = f(&cs->controls[i], ctrl);
1636 static void cluster_done(unsigned from,
1637 struct v4l2_ext_controls *cs,
1638 struct ctrl_helper *helpers)
1640 struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1643 /* Find any controls from the same cluster and mark them as handled */
1644 for (i = from; i < cs->count; i++)
1645 if (helpers[i].ctrl->cluster == cluster)
1646 helpers[i].handled = true;
1649 /* Handles the corner case where cs->count == 0. It checks whether the
1650 specified control class exists. If that class ID is 0, then it checks
1651 whether there are any controls at all. */
1652 static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1654 if (ctrl_class == 0)
1655 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1656 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1661 /* Get extended controls. Allocates the helpers array if needed. */
1662 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1664 struct ctrl_helper helper[4];
1665 struct ctrl_helper *helpers = helper;
1669 cs->error_idx = cs->count;
1670 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1676 return class_check(hdl, cs->ctrl_class);
1678 if (cs->count > ARRAY_SIZE(helper)) {
1679 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1680 if (helpers == NULL)
1684 ret = prepare_ext_ctrls(hdl, cs, helpers);
1685 cs->error_idx = cs->count;
1687 for (i = 0; !ret && i < cs->count; i++)
1688 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1691 for (i = 0; !ret && i < cs->count; i++) {
1692 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1693 struct v4l2_ctrl *master = ctrl->cluster[0];
1696 if (helpers[i].handled)
1701 /* Any volatile controls requested from this cluster? */
1702 has_volatiles = ctrl->is_volatile;
1703 if (!has_volatiles && has_op(master, g_volatile_ctrl) &&
1704 master->ncontrols > 1)
1705 has_volatiles = cluster_walk(i, cs, helpers,
1708 v4l2_ctrl_lock(master);
1710 /* g_volatile_ctrl will update the new control values */
1711 if (has_volatiles && !is_cur_manual(master)) {
1712 for (j = 0; j < master->ncontrols; j++)
1713 cur_to_new(master->cluster[j]);
1714 ret = call_op(master, g_volatile_ctrl);
1716 /* If OK, then copy the current (for non-volatile controls)
1717 or the new (for volatile controls) control values to the
1720 ret = cluster_walk(i, cs, helpers, ctrl_to_user);
1721 v4l2_ctrl_unlock(master);
1722 cluster_done(i, cs, helpers);
1725 if (cs->count > ARRAY_SIZE(helper))
1729 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1731 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1733 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1735 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1737 /* Helper function to get a single control */
1738 static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1740 struct v4l2_ctrl *master = ctrl->cluster[0];
1744 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1747 v4l2_ctrl_lock(master);
1748 /* g_volatile_ctrl will update the current control values */
1749 if (ctrl->is_volatile && !is_cur_manual(master)) {
1750 for (i = 0; i < master->ncontrols; i++)
1751 cur_to_new(master->cluster[i]);
1752 ret = call_op(master, g_volatile_ctrl);
1755 *val = ctrl->cur.val;
1757 v4l2_ctrl_unlock(master);
1761 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1763 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1765 if (ctrl == NULL || !type_is_int(ctrl))
1767 return get_ctrl(ctrl, &control->value);
1769 EXPORT_SYMBOL(v4l2_g_ctrl);
1771 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1773 return v4l2_g_ctrl(sd->ctrl_handler, control);
1775 EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1777 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1781 /* It's a driver bug if this happens. */
1782 WARN_ON(!type_is_int(ctrl));
1783 get_ctrl(ctrl, &val);
1786 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1789 /* Core function that calls try/s_ctrl and ensures that the new value is
1790 copied to the current value on a set.
1791 Must be called with ctrl->handler->lock held. */
1792 static int try_or_set_control_cluster(struct v4l2_fh *fh,
1793 struct v4l2_ctrl *master, bool set)
1800 /* Go through the cluster and either validate the new value or
1801 (if no new value was set), copy the current value to the new
1802 value, ensuring a consistent view for the control ops when
1804 for (i = 0; !ret && i < master->ncontrols; i++) {
1805 struct v4l2_ctrl *ctrl = master->cluster[i];
1811 /* Double check this: it may have changed since the
1812 last check in try_or_set_ext_ctrls(). */
1813 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1816 /* Validate if required */
1818 ret = validate_new(ctrl);
1821 /* No new value was set, so copy the current and force
1822 a call to try_ctrl later, since the values for the cluster
1823 may now have changed and the end result might be invalid. */
1828 /* For larger clusters you have to call try_ctrl again to
1829 verify that the controls are still valid after the
1830 'cur_to_new' above. */
1832 ret = call_op(master, try_ctrl);
1834 /* Don't set if there is no change */
1835 if (ret || !set || !cluster_changed(master))
1837 ret = call_op(master, s_ctrl);
1838 /* If OK, then make the new values permanent. */
1842 update_flag = is_cur_manual(master) != is_new_manual(master);
1843 for (i = 0; i < master->ncontrols; i++)
1844 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
1848 /* Try or set controls. */
1849 static int try_or_set_ext_ctrls(struct v4l2_fh *fh,
1850 struct v4l2_ctrl_handler *hdl,
1851 struct v4l2_ext_controls *cs,
1852 struct ctrl_helper *helpers,
1858 for (i = 0; i < cs->count; i++) {
1859 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1863 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1865 /* This test is also done in try_set_control_cluster() which
1866 is called in atomic context, so that has the final say,
1867 but it makes sense to do an up-front check as well. Once
1868 an error occurs in try_set_control_cluster() some other
1869 controls may have been set already and we want to do a
1870 best-effort to avoid that. */
1871 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1875 for (i = 0; !ret && i < cs->count; i++) {
1876 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1877 struct v4l2_ctrl *master = ctrl->cluster[0];
1879 if (helpers[i].handled)
1883 v4l2_ctrl_lock(ctrl);
1885 /* Reset the 'is_new' flags of the cluster */
1886 for (j = 0; j < master->ncontrols; j++)
1887 if (master->cluster[j])
1888 master->cluster[j]->is_new = 0;
1890 /* Copy the new caller-supplied control values.
1891 user_to_new() sets 'is_new' to 1. */
1892 ret = cluster_walk(i, cs, helpers, user_to_new);
1895 ret = try_or_set_control_cluster(fh, master, set);
1897 /* Copy the new values back to userspace. */
1899 ret = cluster_walk(i, cs, helpers, new_to_user);
1901 v4l2_ctrl_unlock(ctrl);
1902 cluster_done(i, cs, helpers);
1907 /* Try or try-and-set controls */
1908 static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1909 struct v4l2_ext_controls *cs,
1912 struct ctrl_helper helper[4];
1913 struct ctrl_helper *helpers = helper;
1917 cs->error_idx = cs->count;
1918 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1924 return class_check(hdl, cs->ctrl_class);
1926 if (cs->count > ARRAY_SIZE(helper)) {
1927 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1931 ret = prepare_ext_ctrls(hdl, cs, helpers);
1933 /* First 'try' all controls and abort on error */
1935 ret = try_or_set_ext_ctrls(NULL, hdl, cs, helpers, false);
1936 /* If this is a 'set' operation and the initial 'try' failed,
1937 then set error_idx to count to tell the application that no
1938 controls changed value yet. */
1940 cs->error_idx = cs->count;
1942 /* Reset 'handled' state */
1943 for (i = 0; i < cs->count; i++)
1944 helpers[i].handled = false;
1945 ret = try_or_set_ext_ctrls(fh, hdl, cs, helpers, true);
1948 if (cs->count > ARRAY_SIZE(helper))
1953 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1955 return try_set_ext_ctrls(NULL, hdl, cs, false);
1957 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1959 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1960 struct v4l2_ext_controls *cs)
1962 return try_set_ext_ctrls(fh, hdl, cs, true);
1964 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1966 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1968 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
1970 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1972 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1974 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
1976 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1978 /* Helper function for VIDIOC_S_CTRL compatibility */
1979 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
1981 struct v4l2_ctrl *master = ctrl->cluster[0];
1985 v4l2_ctrl_lock(ctrl);
1987 /* Reset the 'is_new' flags of the cluster */
1988 for (i = 0; i < master->ncontrols; i++)
1989 if (master->cluster[i])
1990 master->cluster[i]->is_new = 0;
1994 ret = try_or_set_control_cluster(NULL, master, false);
1996 ret = try_or_set_control_cluster(fh, master, true);
1997 *val = ctrl->cur.val;
1998 v4l2_ctrl_unlock(ctrl);
2002 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2003 struct v4l2_control *control)
2005 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2007 if (ctrl == NULL || !type_is_int(ctrl))
2010 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2013 return set_ctrl(fh, ctrl, &control->value);
2015 EXPORT_SYMBOL(v4l2_s_ctrl);
2017 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2019 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
2021 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2023 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2025 /* It's a driver bug if this happens. */
2026 WARN_ON(!type_is_int(ctrl));
2027 return set_ctrl(NULL, ctrl, &val);
2029 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
2031 void v4l2_ctrl_add_fh(struct v4l2_ctrl_handler *hdl,
2032 struct v4l2_ctrl_fh *ctrl_fh,
2033 struct v4l2_event_subscription *sub)
2035 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, sub->id);
2037 v4l2_ctrl_lock(ctrl);
2038 list_add_tail(&ctrl_fh->node, &ctrl->fhs);
2039 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
2040 (sub->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
2041 struct v4l2_event ev;
2043 fill_event(&ev, ctrl, V4L2_EVENT_CTRL_CH_VALUE |
2044 V4L2_EVENT_CTRL_CH_FLAGS);
2045 v4l2_event_queue_fh(ctrl_fh->fh, &ev);
2047 v4l2_ctrl_unlock(ctrl);
2049 EXPORT_SYMBOL(v4l2_ctrl_add_fh);
2051 void v4l2_ctrl_del_fh(struct v4l2_ctrl *ctrl, struct v4l2_fh *fh)
2053 struct v4l2_ctrl_fh *pos;
2055 v4l2_ctrl_lock(ctrl);
2056 list_for_each_entry(pos, &ctrl->fhs, node) {
2057 if (pos->fh == fh) {
2058 list_del(&pos->node);
2063 v4l2_ctrl_unlock(ctrl);
2065 EXPORT_SYMBOL(v4l2_ctrl_del_fh);
2067 int v4l2_ctrl_subscribe_fh(struct v4l2_fh *fh,
2068 struct v4l2_event_subscription *sub, unsigned n)
2070 struct v4l2_ctrl_handler *hdl = fh->ctrl_handler;
2074 ret = v4l2_event_init(fh);
2076 if (hdl->nr_of_refs * 2 > n)
2077 n = hdl->nr_of_refs * 2;
2078 ret = v4l2_event_alloc(fh, n);
2081 ret = v4l2_event_subscribe(fh, sub);
2084 EXPORT_SYMBOL(v4l2_ctrl_subscribe_fh);