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 */
35 struct v4l2_ctrl_helper {
36 /* Pointer to the control reference of the master control */
37 struct v4l2_ctrl_ref *mref;
38 /* The control corresponding to the v4l2_ext_control ID field. */
39 struct v4l2_ctrl *ctrl;
40 /* v4l2_ext_control index of the next control belonging to the
41 same cluster, or 0 if there isn't any. */
45 /* Small helper function to determine if the autocluster is set to manual
46 mode. In that case the is_volatile flag should be ignored. */
47 static bool is_cur_manual(const struct v4l2_ctrl *master)
49 return master->is_auto && master->cur.val == master->manual_mode_value;
52 /* Same as above, but this checks the against the new value instead of the
54 static bool is_new_manual(const struct v4l2_ctrl *master)
56 return master->is_auto && master->val == master->manual_mode_value;
59 /* Returns NULL or a character pointer array containing the menu for
60 the given control ID. The pointer array ends with a NULL pointer.
61 An empty string signifies a menu entry that is invalid. This allows
62 drivers to disable certain options if it is not supported. */
63 const char * const *v4l2_ctrl_get_menu(u32 id)
65 static const char * const mpeg_audio_sampling_freq[] = {
71 static const char * const mpeg_audio_encoding[] = {
79 static const char * const mpeg_audio_l1_bitrate[] = {
96 static const char * const mpeg_audio_l2_bitrate[] = {
113 static const char * const mpeg_audio_l3_bitrate[] = {
130 static const char * const mpeg_audio_ac3_bitrate[] = {
152 static const char * const mpeg_audio_mode[] = {
159 static const char * const mpeg_audio_mode_extension[] = {
166 static const char * const mpeg_audio_emphasis[] = {
172 static const char * const mpeg_audio_crc[] = {
177 static const char * const mpeg_video_encoding[] = {
183 static const char * const mpeg_video_aspect[] = {
190 static const char * const mpeg_video_bitrate_mode[] = {
195 static const char * const mpeg_stream_type[] = {
196 "MPEG-2 Program Stream",
197 "MPEG-2 Transport Stream",
198 "MPEG-1 System Stream",
199 "MPEG-2 DVD-compatible Stream",
200 "MPEG-1 VCD-compatible Stream",
201 "MPEG-2 SVCD-compatible Stream",
204 static const char * const mpeg_stream_vbi_fmt[] = {
206 "Private packet, IVTV format",
209 static const char * const camera_power_line_frequency[] = {
215 static const char * const camera_exposure_auto[] = {
218 "Shutter Priority Mode",
219 "Aperture Priority Mode",
222 static const char * const colorfx[] = {
235 static const char * const tune_preemphasis[] = {
243 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
244 return mpeg_audio_sampling_freq;
245 case V4L2_CID_MPEG_AUDIO_ENCODING:
246 return mpeg_audio_encoding;
247 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
248 return mpeg_audio_l1_bitrate;
249 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
250 return mpeg_audio_l2_bitrate;
251 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
252 return mpeg_audio_l3_bitrate;
253 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
254 return mpeg_audio_ac3_bitrate;
255 case V4L2_CID_MPEG_AUDIO_MODE:
256 return mpeg_audio_mode;
257 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
258 return mpeg_audio_mode_extension;
259 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
260 return mpeg_audio_emphasis;
261 case V4L2_CID_MPEG_AUDIO_CRC:
262 return mpeg_audio_crc;
263 case V4L2_CID_MPEG_VIDEO_ENCODING:
264 return mpeg_video_encoding;
265 case V4L2_CID_MPEG_VIDEO_ASPECT:
266 return mpeg_video_aspect;
267 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
268 return mpeg_video_bitrate_mode;
269 case V4L2_CID_MPEG_STREAM_TYPE:
270 return mpeg_stream_type;
271 case V4L2_CID_MPEG_STREAM_VBI_FMT:
272 return mpeg_stream_vbi_fmt;
273 case V4L2_CID_POWER_LINE_FREQUENCY:
274 return camera_power_line_frequency;
275 case V4L2_CID_EXPOSURE_AUTO:
276 return camera_exposure_auto;
277 case V4L2_CID_COLORFX:
279 case V4L2_CID_TUNE_PREEMPHASIS:
280 return tune_preemphasis;
285 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
287 /* Return the control name. */
288 const char *v4l2_ctrl_get_name(u32 id)
292 /* Keep the order of the 'case's the same as in videodev2.h! */
293 case V4L2_CID_USER_CLASS: return "User Controls";
294 case V4L2_CID_BRIGHTNESS: return "Brightness";
295 case V4L2_CID_CONTRAST: return "Contrast";
296 case V4L2_CID_SATURATION: return "Saturation";
297 case V4L2_CID_HUE: return "Hue";
298 case V4L2_CID_AUDIO_VOLUME: return "Volume";
299 case V4L2_CID_AUDIO_BALANCE: return "Balance";
300 case V4L2_CID_AUDIO_BASS: return "Bass";
301 case V4L2_CID_AUDIO_TREBLE: return "Treble";
302 case V4L2_CID_AUDIO_MUTE: return "Mute";
303 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
304 case V4L2_CID_BLACK_LEVEL: return "Black Level";
305 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
306 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
307 case V4L2_CID_RED_BALANCE: return "Red Balance";
308 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
309 case V4L2_CID_GAMMA: return "Gamma";
310 case V4L2_CID_EXPOSURE: return "Exposure";
311 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
312 case V4L2_CID_GAIN: return "Gain";
313 case V4L2_CID_HFLIP: return "Horizontal Flip";
314 case V4L2_CID_VFLIP: return "Vertical Flip";
315 case V4L2_CID_HCENTER: return "Horizontal Center";
316 case V4L2_CID_VCENTER: return "Vertical Center";
317 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
318 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
319 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
320 case V4L2_CID_SHARPNESS: return "Sharpness";
321 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
322 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
323 case V4L2_CID_COLOR_KILLER: return "Color Killer";
324 case V4L2_CID_COLORFX: return "Color Effects";
325 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
326 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
327 case V4L2_CID_ROTATE: return "Rotate";
328 case V4L2_CID_BG_COLOR: return "Background Color";
329 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
330 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
331 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
334 /* Keep the order of the 'case's the same as in videodev2.h! */
335 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
336 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
337 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
338 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
339 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
340 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
341 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
342 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
343 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
344 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
345 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
346 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
347 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
348 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
349 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
350 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
351 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
352 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
353 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
354 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
355 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
356 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
357 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
358 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
359 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
360 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
361 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
362 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
363 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
364 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
365 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
366 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
367 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
369 /* CAMERA controls */
370 /* Keep the order of the 'case's the same as in videodev2.h! */
371 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
372 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
373 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
374 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
375 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
376 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
377 case V4L2_CID_PAN_RESET: return "Pan, Reset";
378 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
379 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
380 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
381 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
382 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
383 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
384 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
385 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
386 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
387 case V4L2_CID_PRIVACY: return "Privacy";
388 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
389 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
391 /* FM Radio Modulator control */
392 /* Keep the order of the 'case's the same as in videodev2.h! */
393 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
394 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
395 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
396 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
397 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
398 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
399 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
400 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
401 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
402 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
403 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
404 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
405 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
406 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
407 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
408 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
409 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
410 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
411 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
412 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
418 EXPORT_SYMBOL(v4l2_ctrl_get_name);
420 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
421 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
423 *name = v4l2_ctrl_get_name(id);
427 case V4L2_CID_AUDIO_MUTE:
428 case V4L2_CID_AUDIO_LOUDNESS:
429 case V4L2_CID_AUTO_WHITE_BALANCE:
430 case V4L2_CID_AUTOGAIN:
433 case V4L2_CID_HUE_AUTO:
434 case V4L2_CID_CHROMA_AGC:
435 case V4L2_CID_COLOR_KILLER:
436 case V4L2_CID_MPEG_AUDIO_MUTE:
437 case V4L2_CID_MPEG_VIDEO_MUTE:
438 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
439 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
440 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
441 case V4L2_CID_FOCUS_AUTO:
442 case V4L2_CID_PRIVACY:
443 case V4L2_CID_AUDIO_LIMITER_ENABLED:
444 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
445 case V4L2_CID_PILOT_TONE_ENABLED:
446 case V4L2_CID_ILLUMINATORS_1:
447 case V4L2_CID_ILLUMINATORS_2:
448 *type = V4L2_CTRL_TYPE_BOOLEAN;
452 case V4L2_CID_PAN_RESET:
453 case V4L2_CID_TILT_RESET:
454 *type = V4L2_CTRL_TYPE_BUTTON;
455 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
456 *min = *max = *step = *def = 0;
458 case V4L2_CID_POWER_LINE_FREQUENCY:
459 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
460 case V4L2_CID_MPEG_AUDIO_ENCODING:
461 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
462 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
463 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
464 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
465 case V4L2_CID_MPEG_AUDIO_MODE:
466 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
467 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
468 case V4L2_CID_MPEG_AUDIO_CRC:
469 case V4L2_CID_MPEG_VIDEO_ENCODING:
470 case V4L2_CID_MPEG_VIDEO_ASPECT:
471 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
472 case V4L2_CID_MPEG_STREAM_TYPE:
473 case V4L2_CID_MPEG_STREAM_VBI_FMT:
474 case V4L2_CID_EXPOSURE_AUTO:
475 case V4L2_CID_COLORFX:
476 case V4L2_CID_TUNE_PREEMPHASIS:
477 *type = V4L2_CTRL_TYPE_MENU;
479 case V4L2_CID_RDS_TX_PS_NAME:
480 case V4L2_CID_RDS_TX_RADIO_TEXT:
481 *type = V4L2_CTRL_TYPE_STRING;
483 case V4L2_CID_USER_CLASS:
484 case V4L2_CID_CAMERA_CLASS:
485 case V4L2_CID_MPEG_CLASS:
486 case V4L2_CID_FM_TX_CLASS:
487 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
488 /* You can neither read not write these */
489 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
490 *min = *max = *step = *def = 0;
492 case V4L2_CID_BG_COLOR:
493 *type = V4L2_CTRL_TYPE_INTEGER;
496 /* Max is calculated as RGB888 that is 2^24 */
500 *type = V4L2_CTRL_TYPE_INTEGER;
504 case V4L2_CID_MPEG_AUDIO_ENCODING:
505 case V4L2_CID_MPEG_AUDIO_MODE:
506 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
507 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
508 case V4L2_CID_MPEG_STREAM_TYPE:
509 *flags |= V4L2_CTRL_FLAG_UPDATE;
511 case V4L2_CID_AUDIO_VOLUME:
512 case V4L2_CID_AUDIO_BALANCE:
513 case V4L2_CID_AUDIO_BASS:
514 case V4L2_CID_AUDIO_TREBLE:
515 case V4L2_CID_BRIGHTNESS:
516 case V4L2_CID_CONTRAST:
517 case V4L2_CID_SATURATION:
519 case V4L2_CID_RED_BALANCE:
520 case V4L2_CID_BLUE_BALANCE:
522 case V4L2_CID_SHARPNESS:
523 case V4L2_CID_CHROMA_GAIN:
524 case V4L2_CID_RDS_TX_DEVIATION:
525 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
526 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
527 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
528 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
529 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
530 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
531 case V4L2_CID_PILOT_TONE_DEVIATION:
532 case V4L2_CID_PILOT_TONE_FREQUENCY:
533 case V4L2_CID_TUNE_POWER_LEVEL:
534 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
535 *flags |= V4L2_CTRL_FLAG_SLIDER;
537 case V4L2_CID_PAN_RELATIVE:
538 case V4L2_CID_TILT_RELATIVE:
539 case V4L2_CID_FOCUS_RELATIVE:
540 case V4L2_CID_IRIS_RELATIVE:
541 case V4L2_CID_ZOOM_RELATIVE:
542 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
546 EXPORT_SYMBOL(v4l2_ctrl_fill);
548 /* Helper function to determine whether the control type is compatible with
550 static bool type_is_int(const struct v4l2_ctrl *ctrl)
552 switch (ctrl->type) {
553 case V4L2_CTRL_TYPE_INTEGER64:
554 case V4L2_CTRL_TYPE_STRING:
555 /* Nope, these need v4l2_ext_control */
562 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
564 memset(ev->reserved, 0, sizeof(ev->reserved));
565 ev->type = V4L2_EVENT_CTRL;
567 ev->u.ctrl.changes = changes;
568 ev->u.ctrl.type = ctrl->type;
569 ev->u.ctrl.flags = ctrl->flags;
570 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
571 ev->u.ctrl.value64 = 0;
573 ev->u.ctrl.value64 = ctrl->cur.val64;
574 ev->u.ctrl.minimum = ctrl->minimum;
575 ev->u.ctrl.maximum = ctrl->maximum;
576 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
579 ev->u.ctrl.step = ctrl->step;
580 ev->u.ctrl.default_value = ctrl->default_value;
583 static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
585 struct v4l2_event ev;
586 struct v4l2_subscribed_event *sev;
588 if (list_empty(&ctrl->ev_subs))
590 fill_event(&ev, ctrl, changes);
592 list_for_each_entry(sev, &ctrl->ev_subs, node)
593 if (sev->fh && (sev->fh != fh ||
594 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK)))
595 v4l2_event_queue_fh(sev->fh, &ev);
598 /* Helper function: copy the current control value back to the caller */
599 static int cur_to_user(struct v4l2_ext_control *c,
600 struct v4l2_ctrl *ctrl)
604 switch (ctrl->type) {
605 case V4L2_CTRL_TYPE_STRING:
606 len = strlen(ctrl->cur.string);
607 if (c->size < len + 1) {
611 return copy_to_user(c->string, ctrl->cur.string,
612 len + 1) ? -EFAULT : 0;
613 case V4L2_CTRL_TYPE_INTEGER64:
614 c->value64 = ctrl->cur.val64;
617 c->value = ctrl->cur.val;
623 /* Helper function: copy the caller-provider value as the new control value */
624 static int user_to_new(struct v4l2_ext_control *c,
625 struct v4l2_ctrl *ctrl)
631 switch (ctrl->type) {
632 case V4L2_CTRL_TYPE_INTEGER64:
633 ctrl->val64 = c->value64;
635 case V4L2_CTRL_TYPE_STRING:
639 if (size > ctrl->maximum + 1)
640 size = ctrl->maximum + 1;
641 ret = copy_from_user(ctrl->string, c->string, size);
643 char last = ctrl->string[size - 1];
645 ctrl->string[size - 1] = 0;
646 /* If the string was longer than ctrl->maximum,
647 then return an error. */
648 if (strlen(ctrl->string) == ctrl->maximum && last)
651 return ret ? -EFAULT : 0;
653 ctrl->val = c->value;
659 /* Helper function: copy the new control value back to the caller */
660 static int new_to_user(struct v4l2_ext_control *c,
661 struct v4l2_ctrl *ctrl)
665 switch (ctrl->type) {
666 case V4L2_CTRL_TYPE_STRING:
667 len = strlen(ctrl->string);
668 if (c->size < len + 1) {
669 c->size = ctrl->maximum + 1;
672 return copy_to_user(c->string, ctrl->string,
673 len + 1) ? -EFAULT : 0;
674 case V4L2_CTRL_TYPE_INTEGER64:
675 c->value64 = ctrl->val64;
678 c->value = ctrl->val;
684 /* Copy the new value to the current value. */
685 static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
686 bool update_inactive)
688 bool changed = false;
692 switch (ctrl->type) {
693 case V4L2_CTRL_TYPE_BUTTON:
696 case V4L2_CTRL_TYPE_STRING:
697 /* strings are always 0-terminated */
698 changed = strcmp(ctrl->string, ctrl->cur.string);
699 strcpy(ctrl->cur.string, ctrl->string);
701 case V4L2_CTRL_TYPE_INTEGER64:
702 changed = ctrl->val64 != ctrl->cur.val64;
703 ctrl->cur.val64 = ctrl->val64;
706 changed = ctrl->val != ctrl->cur.val;
707 ctrl->cur.val = ctrl->val;
710 if (update_inactive) {
711 ctrl->flags &= ~V4L2_CTRL_FLAG_INACTIVE;
712 if (!is_cur_manual(ctrl->cluster[0]))
713 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
715 if (changed || update_inactive) {
716 /* If a control was changed that was not one of the controls
717 modified by the application, then send the event to all. */
721 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
722 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
726 /* Copy the current value to the new value */
727 static void cur_to_new(struct v4l2_ctrl *ctrl)
731 switch (ctrl->type) {
732 case V4L2_CTRL_TYPE_STRING:
733 /* strings are always 0-terminated */
734 strcpy(ctrl->string, ctrl->cur.string);
736 case V4L2_CTRL_TYPE_INTEGER64:
737 ctrl->val64 = ctrl->cur.val64;
740 ctrl->val = ctrl->cur.val;
745 /* Return non-zero if one or more of the controls in the cluster has a new
746 value that differs from the current value. */
747 static int cluster_changed(struct v4l2_ctrl *master)
752 for (i = 0; !diff && i < master->ncontrols; i++) {
753 struct v4l2_ctrl *ctrl = master->cluster[i];
757 switch (ctrl->type) {
758 case V4L2_CTRL_TYPE_BUTTON:
759 /* Button controls are always 'different' */
761 case V4L2_CTRL_TYPE_STRING:
762 /* strings are always 0-terminated */
763 diff = strcmp(ctrl->string, ctrl->cur.string);
765 case V4L2_CTRL_TYPE_INTEGER64:
766 diff = ctrl->val64 != ctrl->cur.val64;
769 diff = ctrl->val != ctrl->cur.val;
776 /* Validate integer-type control */
777 static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
782 switch (ctrl->type) {
783 case V4L2_CTRL_TYPE_INTEGER:
784 /* Round towards the closest legal value */
785 val += ctrl->step / 2;
786 if (val < ctrl->minimum)
788 if (val > ctrl->maximum)
790 offset = val - ctrl->minimum;
791 offset = ctrl->step * (offset / ctrl->step);
792 val = ctrl->minimum + offset;
796 case V4L2_CTRL_TYPE_BOOLEAN:
800 case V4L2_CTRL_TYPE_MENU:
801 if (val < ctrl->minimum || val > ctrl->maximum)
803 if (ctrl->qmenu[val][0] == '\0' ||
804 (ctrl->menu_skip_mask & (1 << val)))
808 case V4L2_CTRL_TYPE_BUTTON:
809 case V4L2_CTRL_TYPE_CTRL_CLASS:
818 /* Validate a new control */
819 static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
824 switch (ctrl->type) {
825 case V4L2_CTRL_TYPE_INTEGER:
826 case V4L2_CTRL_TYPE_BOOLEAN:
827 case V4L2_CTRL_TYPE_MENU:
828 case V4L2_CTRL_TYPE_BUTTON:
829 case V4L2_CTRL_TYPE_CTRL_CLASS:
830 return validate_new_int(ctrl, &c->value);
832 case V4L2_CTRL_TYPE_INTEGER64:
835 case V4L2_CTRL_TYPE_STRING:
837 if (len < ctrl->minimum)
839 if ((len - ctrl->minimum) % ctrl->step)
848 static inline u32 node2id(struct list_head *node)
850 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
853 /* Set the handler's error code if it wasn't set earlier already */
854 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
861 /* Initialize the handler */
862 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
863 unsigned nr_of_controls_hint)
865 mutex_init(&hdl->lock);
866 INIT_LIST_HEAD(&hdl->ctrls);
867 INIT_LIST_HEAD(&hdl->ctrl_refs);
868 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
869 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
871 hdl->error = hdl->buckets ? 0 : -ENOMEM;
874 EXPORT_SYMBOL(v4l2_ctrl_handler_init);
876 /* Free all controls and control refs */
877 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
879 struct v4l2_ctrl_ref *ref, *next_ref;
880 struct v4l2_ctrl *ctrl, *next_ctrl;
881 struct v4l2_subscribed_event *sev, *next_sev;
883 if (hdl == NULL || hdl->buckets == NULL)
886 mutex_lock(&hdl->lock);
888 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
889 list_del(&ref->node);
892 /* Free all controls owned by the handler */
893 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
894 list_del(&ctrl->node);
895 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
896 list_del(&sev->node);
903 mutex_unlock(&hdl->lock);
905 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
907 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
908 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
909 with applications that do not use the NEXT_CTRL flag.
911 We just find the n-th private user control. It's O(N), but that should not
912 be an issue in this particular case. */
913 static struct v4l2_ctrl_ref *find_private_ref(
914 struct v4l2_ctrl_handler *hdl, u32 id)
916 struct v4l2_ctrl_ref *ref;
918 id -= V4L2_CID_PRIVATE_BASE;
919 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
920 /* Search for private user controls that are compatible with
922 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
923 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
924 if (!type_is_int(ref->ctrl))
934 /* Find a control with the given ID. */
935 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
937 struct v4l2_ctrl_ref *ref;
940 id &= V4L2_CTRL_ID_MASK;
942 /* Old-style private controls need special handling */
943 if (id >= V4L2_CID_PRIVATE_BASE)
944 return find_private_ref(hdl, id);
945 bucket = id % hdl->nr_of_buckets;
947 /* Simple optimization: cache the last control found */
948 if (hdl->cached && hdl->cached->ctrl->id == id)
951 /* Not in cache, search the hash */
952 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
953 while (ref && ref->ctrl->id != id)
957 hdl->cached = ref; /* cache it! */
961 /* Find a control with the given ID. Take the handler's lock first. */
962 static struct v4l2_ctrl_ref *find_ref_lock(
963 struct v4l2_ctrl_handler *hdl, u32 id)
965 struct v4l2_ctrl_ref *ref = NULL;
968 mutex_lock(&hdl->lock);
969 ref = find_ref(hdl, id);
970 mutex_unlock(&hdl->lock);
975 /* Find a control with the given ID. */
976 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
978 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
980 return ref ? ref->ctrl : NULL;
982 EXPORT_SYMBOL(v4l2_ctrl_find);
984 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
985 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
986 struct v4l2_ctrl *ctrl)
988 struct v4l2_ctrl_ref *ref;
989 struct v4l2_ctrl_ref *new_ref;
991 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
992 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
994 /* Automatically add the control class if it is not yet present. */
995 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
996 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1002 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
1004 return handler_set_err(hdl, -ENOMEM);
1005 new_ref->ctrl = ctrl;
1006 if (ctrl->handler == hdl) {
1007 /* By default each control starts in a cluster of its own.
1008 new_ref->ctrl is basically a cluster array with one
1009 element, so that's perfect to use as the cluster pointer.
1010 But only do this for the handler that owns the control. */
1011 ctrl->cluster = &new_ref->ctrl;
1012 ctrl->ncontrols = 1;
1015 INIT_LIST_HEAD(&new_ref->node);
1017 mutex_lock(&hdl->lock);
1019 /* Add immediately at the end of the list if the list is empty, or if
1020 the last element in the list has a lower ID.
1021 This ensures that when elements are added in ascending order the
1022 insertion is an O(1) operation. */
1023 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1024 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1025 goto insert_in_hash;
1028 /* Find insert position in sorted list */
1029 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1030 if (ref->ctrl->id < id)
1032 /* Don't add duplicates */
1033 if (ref->ctrl->id == id) {
1037 list_add(&new_ref->node, ref->node.prev);
1042 /* Insert the control node in the hash */
1043 new_ref->next = hdl->buckets[bucket];
1044 hdl->buckets[bucket] = new_ref;
1047 mutex_unlock(&hdl->lock);
1051 /* Add a new control */
1052 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1053 const struct v4l2_ctrl_ops *ops,
1054 u32 id, const char *name, enum v4l2_ctrl_type type,
1055 s32 min, s32 max, u32 step, s32 def,
1056 u32 flags, const char * const *qmenu, void *priv)
1058 struct v4l2_ctrl *ctrl;
1059 unsigned sz_extra = 0;
1065 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
1067 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
1068 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1069 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1070 handler_set_err(hdl, -ERANGE);
1073 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1074 type == V4L2_CTRL_TYPE_MENU ||
1075 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1076 (def < min || def > max)) {
1077 handler_set_err(hdl, -ERANGE);
1081 if (type == V4L2_CTRL_TYPE_BUTTON)
1082 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1083 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1084 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1085 else if (type == V4L2_CTRL_TYPE_STRING)
1086 sz_extra += 2 * (max + 1);
1088 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1090 handler_set_err(hdl, -ENOMEM);
1094 INIT_LIST_HEAD(&ctrl->node);
1095 INIT_LIST_HEAD(&ctrl->ev_subs);
1096 ctrl->handler = hdl;
1101 ctrl->flags = flags;
1102 ctrl->minimum = min;
1103 ctrl->maximum = max;
1105 ctrl->qmenu = qmenu;
1107 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1109 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1110 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1111 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1113 memset(ctrl->cur.string, ' ', ctrl->minimum);
1115 if (handler_new_ref(hdl, ctrl)) {
1119 mutex_lock(&hdl->lock);
1120 list_add_tail(&ctrl->node, &hdl->ctrls);
1121 mutex_unlock(&hdl->lock);
1125 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1126 const struct v4l2_ctrl_config *cfg, void *priv)
1129 struct v4l2_ctrl *ctrl;
1130 const char *name = cfg->name;
1131 const char * const *qmenu = cfg->qmenu;
1132 enum v4l2_ctrl_type type = cfg->type;
1133 u32 flags = cfg->flags;
1136 u32 step = cfg->step;
1140 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1143 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1147 WARN_ON(cfg->menu_skip_mask);
1148 if (is_menu && qmenu == NULL)
1149 qmenu = v4l2_ctrl_get_menu(cfg->id);
1151 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1153 is_menu ? cfg->menu_skip_mask : step,
1154 def, flags, qmenu, priv);
1156 ctrl->is_private = cfg->is_private;
1157 ctrl->is_volatile = cfg->is_volatile;
1161 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1163 /* Helper function for standard non-menu controls */
1164 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1165 const struct v4l2_ctrl_ops *ops,
1166 u32 id, s32 min, s32 max, u32 step, s32 def)
1169 enum v4l2_ctrl_type type;
1172 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1173 if (type == V4L2_CTRL_TYPE_MENU) {
1174 handler_set_err(hdl, -EINVAL);
1177 return v4l2_ctrl_new(hdl, ops, id, name, type,
1178 min, max, step, def, flags, NULL, NULL);
1180 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1182 /* Helper function for standard menu controls */
1183 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1184 const struct v4l2_ctrl_ops *ops,
1185 u32 id, s32 max, s32 mask, s32 def)
1187 const char * const *qmenu = v4l2_ctrl_get_menu(id);
1189 enum v4l2_ctrl_type type;
1194 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1195 if (type != V4L2_CTRL_TYPE_MENU) {
1196 handler_set_err(hdl, -EINVAL);
1199 return v4l2_ctrl_new(hdl, ops, id, name, type,
1200 0, max, mask, def, flags, qmenu, NULL);
1202 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1204 /* Add a control from another handler to this handler */
1205 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1206 struct v4l2_ctrl *ctrl)
1208 if (hdl == NULL || hdl->error)
1211 handler_set_err(hdl, -EINVAL);
1214 if (ctrl->handler == hdl)
1216 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1218 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1220 /* Add the controls from another handler to our own. */
1221 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1222 struct v4l2_ctrl_handler *add)
1224 struct v4l2_ctrl *ctrl;
1227 /* Do nothing if either handler is NULL or if they are the same */
1228 if (!hdl || !add || hdl == add)
1232 mutex_lock(&add->lock);
1233 list_for_each_entry(ctrl, &add->ctrls, node) {
1234 /* Skip handler-private controls. */
1235 if (ctrl->is_private)
1237 /* And control classes */
1238 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1240 ret = handler_new_ref(hdl, ctrl);
1244 mutex_unlock(&add->lock);
1247 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1249 /* Cluster controls */
1250 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1254 /* The first control is the master control and it must not be NULL */
1255 BUG_ON(ncontrols == 0 || controls[0] == NULL);
1257 for (i = 0; i < ncontrols; i++) {
1259 controls[i]->cluster = controls;
1260 controls[i]->ncontrols = ncontrols;
1264 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1266 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1267 u8 manual_val, bool set_volatile)
1269 struct v4l2_ctrl *master = controls[0];
1273 v4l2_ctrl_cluster(ncontrols, controls);
1274 WARN_ON(ncontrols <= 1);
1275 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
1276 master->is_auto = true;
1277 master->manual_mode_value = manual_val;
1278 master->flags |= V4L2_CTRL_FLAG_UPDATE;
1279 flag = is_cur_manual(master) ? 0 : V4L2_CTRL_FLAG_INACTIVE;
1281 for (i = 1; i < ncontrols; i++)
1283 controls[i]->is_volatile = set_volatile;
1284 controls[i]->flags |= flag;
1287 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1289 /* Activate/deactivate a control. */
1290 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1292 /* invert since the actual flag is called 'inactive' */
1293 bool inactive = !active;
1300 /* set V4L2_CTRL_FLAG_INACTIVE */
1301 old = test_and_set_bit(4, &ctrl->flags);
1303 /* clear V4L2_CTRL_FLAG_INACTIVE */
1304 old = test_and_clear_bit(4, &ctrl->flags);
1305 if (old != inactive)
1306 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1308 EXPORT_SYMBOL(v4l2_ctrl_activate);
1310 /* Grab/ungrab a control.
1311 Typically used when streaming starts and you want to grab controls,
1312 preventing the user from changing them.
1314 Just call this and the framework will block any attempts to change
1316 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1323 v4l2_ctrl_lock(ctrl);
1325 /* set V4L2_CTRL_FLAG_GRABBED */
1326 old = test_and_set_bit(1, &ctrl->flags);
1328 /* clear V4L2_CTRL_FLAG_GRABBED */
1329 old = test_and_clear_bit(1, &ctrl->flags);
1331 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1332 v4l2_ctrl_unlock(ctrl);
1334 EXPORT_SYMBOL(v4l2_ctrl_grab);
1336 /* Log the control name and value */
1337 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1338 const char *prefix, const char *colon)
1340 int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1341 int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1343 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1345 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1348 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1350 switch (ctrl->type) {
1351 case V4L2_CTRL_TYPE_INTEGER:
1352 printk(KERN_CONT "%d", ctrl->cur.val);
1354 case V4L2_CTRL_TYPE_BOOLEAN:
1355 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1357 case V4L2_CTRL_TYPE_MENU:
1358 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1360 case V4L2_CTRL_TYPE_INTEGER64:
1361 printk(KERN_CONT "%lld", ctrl->cur.val64);
1363 case V4L2_CTRL_TYPE_STRING:
1364 printk(KERN_CONT "%s", ctrl->cur.string);
1367 printk(KERN_CONT "unknown type %d", ctrl->type);
1370 if (fl_inact && fl_grabbed)
1371 printk(KERN_CONT " (inactive, grabbed)\n");
1373 printk(KERN_CONT " (inactive)\n");
1374 else if (fl_grabbed)
1375 printk(KERN_CONT " (grabbed)\n");
1377 printk(KERN_CONT "\n");
1380 /* Log all controls owned by the handler */
1381 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1384 struct v4l2_ctrl *ctrl;
1385 const char *colon = "";
1392 len = strlen(prefix);
1393 if (len && prefix[len - 1] != ' ')
1395 mutex_lock(&hdl->lock);
1396 list_for_each_entry(ctrl, &hdl->ctrls, node)
1397 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1398 log_ctrl(ctrl, prefix, colon);
1399 mutex_unlock(&hdl->lock);
1401 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1403 /* Call s_ctrl for all controls owned by the handler */
1404 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1406 struct v4l2_ctrl *ctrl;
1411 mutex_lock(&hdl->lock);
1412 list_for_each_entry(ctrl, &hdl->ctrls, node)
1415 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1416 struct v4l2_ctrl *master = ctrl->cluster[0];
1419 /* Skip if this control was already handled by a cluster. */
1420 /* Skip button controls and read-only controls. */
1421 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1422 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1425 for (i = 0; i < master->ncontrols; i++) {
1426 if (master->cluster[i]) {
1427 cur_to_new(master->cluster[i]);
1428 master->cluster[i]->is_new = 1;
1429 master->cluster[i]->done = true;
1432 ret = call_op(master, s_ctrl);
1436 mutex_unlock(&hdl->lock);
1439 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1441 /* Implement VIDIOC_QUERYCTRL */
1442 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1444 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1445 struct v4l2_ctrl_ref *ref;
1446 struct v4l2_ctrl *ctrl;
1451 mutex_lock(&hdl->lock);
1453 /* Try to find it */
1454 ref = find_ref(hdl, id);
1456 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1457 /* Find the next control with ID > qc->id */
1459 /* Did we reach the end of the control list? */
1460 if (id >= node2id(hdl->ctrl_refs.prev)) {
1461 ref = NULL; /* Yes, so there is no next control */
1463 /* We found a control with the given ID, so just get
1464 the next one in the list. */
1465 ref = list_entry(ref->node.next, typeof(*ref), node);
1467 /* No control with the given ID exists, so start
1468 searching for the next largest ID. We know there
1469 is one, otherwise the first 'if' above would have
1471 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1472 if (id < ref->ctrl->id)
1476 mutex_unlock(&hdl->lock);
1481 memset(qc, 0, sizeof(*qc));
1482 if (id >= V4L2_CID_PRIVATE_BASE)
1486 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1487 qc->minimum = ctrl->minimum;
1488 qc->maximum = ctrl->maximum;
1489 qc->default_value = ctrl->default_value;
1490 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
1493 qc->step = ctrl->step;
1494 qc->flags = ctrl->flags;
1495 qc->type = ctrl->type;
1498 EXPORT_SYMBOL(v4l2_queryctrl);
1500 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1502 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1504 return v4l2_queryctrl(sd->ctrl_handler, qc);
1506 EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1508 /* Implement VIDIOC_QUERYMENU */
1509 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1511 struct v4l2_ctrl *ctrl;
1514 ctrl = v4l2_ctrl_find(hdl, qm->id);
1520 if (ctrl->qmenu == NULL ||
1521 i < ctrl->minimum || i > ctrl->maximum)
1523 /* Use mask to see if this menu item should be skipped */
1524 if (ctrl->menu_skip_mask & (1 << i))
1526 /* Empty menu items should also be skipped */
1527 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1529 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1532 EXPORT_SYMBOL(v4l2_querymenu);
1534 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1536 return v4l2_querymenu(sd->ctrl_handler, qm);
1538 EXPORT_SYMBOL(v4l2_subdev_querymenu);
1542 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1544 It is not a fully atomic operation, just best-effort only. After all, if
1545 multiple controls have to be set through multiple i2c writes (for example)
1546 then some initial writes may succeed while others fail. Thus leaving the
1547 system in an inconsistent state. The question is how much effort you are
1548 willing to spend on trying to make something atomic that really isn't.
1550 From the point of view of an application the main requirement is that
1551 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1552 error should be returned without actually affecting any controls.
1554 If all the values are correct, then it is acceptable to just give up
1555 in case of low-level errors.
1557 It is important though that the application can tell when only a partial
1558 configuration was done. The way we do that is through the error_idx field
1559 of struct v4l2_ext_controls: if that is equal to the count field then no
1560 controls were affected. Otherwise all controls before that index were
1561 successful in performing their 'get' or 'set' operation, the control at
1562 the given index failed, and you don't know what happened with the controls
1563 after the failed one. Since if they were part of a control cluster they
1564 could have been successfully processed (if a cluster member was encountered
1565 at index < error_idx), they could have failed (if a cluster member was at
1566 error_idx), or they may not have been processed yet (if the first cluster
1567 member appeared after error_idx).
1569 It is all fairly theoretical, though. In practice all you can do is to
1570 bail out. If error_idx == count, then it is an application bug. If
1571 error_idx < count then it is only an application bug if the error code was
1572 EBUSY. That usually means that something started streaming just when you
1573 tried to set the controls. In all other cases it is a driver/hardware
1574 problem and all you can do is to retry or bail out.
1576 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1577 never modifies controls the error_idx is just set to whatever control
1578 has an invalid value.
1581 /* Prepare for the extended g/s/try functions.
1582 Find the controls in the control array and do some basic checks. */
1583 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1584 struct v4l2_ext_controls *cs,
1585 struct v4l2_ctrl_helper *helpers)
1587 struct v4l2_ctrl_helper *h;
1588 bool have_clusters = false;
1591 for (i = 0, h = helpers; i < cs->count; i++, h++) {
1592 struct v4l2_ext_control *c = &cs->controls[i];
1593 struct v4l2_ctrl_ref *ref;
1594 struct v4l2_ctrl *ctrl;
1595 u32 id = c->id & V4L2_CTRL_ID_MASK;
1599 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1602 /* Old-style private controls are not allowed for
1603 extended controls */
1604 if (id >= V4L2_CID_PRIVATE_BASE)
1606 ref = find_ref_lock(hdl, id);
1610 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1613 if (ctrl->cluster[0]->ncontrols > 1)
1614 have_clusters = true;
1615 if (ctrl->cluster[0] != ctrl)
1616 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
1617 /* Store the ref to the master control of the cluster */
1620 /* Initially set next to 0, meaning that there is no other
1621 control in this helper array belonging to the same
1626 /* We are done if there were no controls that belong to a multi-
1631 /* The code below figures out in O(n) time which controls in the list
1632 belong to the same cluster. */
1634 /* This has to be done with the handler lock taken. */
1635 mutex_lock(&hdl->lock);
1637 /* First zero the helper field in the master control references */
1638 for (i = 0; i < cs->count; i++)
1639 helpers[i].mref->helper = 0;
1640 for (i = 0, h = helpers; i < cs->count; i++, h++) {
1641 struct v4l2_ctrl_ref *mref = h->mref;
1643 /* If the mref->helper is set, then it points to an earlier
1644 helper that belongs to the same cluster. */
1646 /* Set the next field of mref->helper to the current
1647 index: this means that that earlier helper now
1648 points to the next helper in the same cluster. */
1649 mref->helper->next = i;
1650 /* mref should be set only for the first helper in the
1651 cluster, clear the others. */
1654 /* Point the mref helper to the current helper struct. */
1657 mutex_unlock(&hdl->lock);
1661 /* Handles the corner case where cs->count == 0. It checks whether the
1662 specified control class exists. If that class ID is 0, then it checks
1663 whether there are any controls at all. */
1664 static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1666 if (ctrl_class == 0)
1667 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1668 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1673 /* Get extended controls. Allocates the helpers array if needed. */
1674 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1676 struct v4l2_ctrl_helper helper[4];
1677 struct v4l2_ctrl_helper *helpers = helper;
1681 cs->error_idx = cs->count;
1682 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1688 return class_check(hdl, cs->ctrl_class);
1690 if (cs->count > ARRAY_SIZE(helper)) {
1691 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1692 if (helpers == NULL)
1696 ret = prepare_ext_ctrls(hdl, cs, helpers);
1697 cs->error_idx = cs->count;
1699 for (i = 0; !ret && i < cs->count; i++)
1700 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1703 for (i = 0; !ret && i < cs->count; i++) {
1704 int (*ctrl_to_user)(struct v4l2_ext_control *c,
1705 struct v4l2_ctrl *ctrl) = cur_to_user;
1706 struct v4l2_ctrl *master;
1708 if (helpers[i].mref == NULL)
1711 master = helpers[i].mref->ctrl;
1714 v4l2_ctrl_lock(master);
1716 /* g_volatile_ctrl will update the new control values */
1717 if (has_op(master, g_volatile_ctrl) && !is_cur_manual(master)) {
1718 for (j = 0; j < master->ncontrols; j++)
1719 cur_to_new(master->cluster[j]);
1720 ret = call_op(master, g_volatile_ctrl);
1721 ctrl_to_user = new_to_user;
1723 /* If OK, then copy the current (for non-volatile controls)
1724 or the new (for volatile controls) control values to the
1730 ret = ctrl_to_user(cs->controls + idx,
1732 idx = helpers[idx].next;
1733 } while (!ret && idx);
1735 v4l2_ctrl_unlock(master);
1738 if (cs->count > ARRAY_SIZE(helper))
1742 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1744 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1746 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1748 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1750 /* Helper function to get a single control */
1751 static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1753 struct v4l2_ctrl *master = ctrl->cluster[0];
1757 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1760 v4l2_ctrl_lock(master);
1761 /* g_volatile_ctrl will update the current control values */
1762 if (ctrl->is_volatile && !is_cur_manual(master)) {
1763 for (i = 0; i < master->ncontrols; i++)
1764 cur_to_new(master->cluster[i]);
1765 ret = call_op(master, g_volatile_ctrl);
1768 *val = ctrl->cur.val;
1770 v4l2_ctrl_unlock(master);
1774 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1776 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1778 if (ctrl == NULL || !type_is_int(ctrl))
1780 return get_ctrl(ctrl, &control->value);
1782 EXPORT_SYMBOL(v4l2_g_ctrl);
1784 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1786 return v4l2_g_ctrl(sd->ctrl_handler, control);
1788 EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1790 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1794 /* It's a driver bug if this happens. */
1795 WARN_ON(!type_is_int(ctrl));
1796 get_ctrl(ctrl, &val);
1799 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1802 /* Core function that calls try/s_ctrl and ensures that the new value is
1803 copied to the current value on a set.
1804 Must be called with ctrl->handler->lock held. */
1805 static int try_or_set_cluster(struct v4l2_fh *fh,
1806 struct v4l2_ctrl *master, bool set)
1812 /* Go through the cluster and either validate the new value or
1813 (if no new value was set), copy the current value to the new
1814 value, ensuring a consistent view for the control ops when
1816 for (i = 0; i < master->ncontrols; i++) {
1817 struct v4l2_ctrl *ctrl = master->cluster[i];
1822 if (!ctrl->is_new) {
1826 /* Check again: it may have changed since the
1827 previous check in try_or_set_ext_ctrls(). */
1828 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
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);
1841 /* 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 /* Validate controls. */
1849 static int validate_ctrls(struct v4l2_ext_controls *cs,
1850 struct v4l2_ctrl_helper *helpers, bool set)
1855 cs->error_idx = cs->count;
1856 for (i = 0; i < cs->count; i++) {
1857 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1861 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1863 /* This test is also done in try_set_control_cluster() which
1864 is called in atomic context, so that has the final say,
1865 but it makes sense to do an up-front check as well. Once
1866 an error occurs in try_set_control_cluster() some other
1867 controls may have been set already and we want to do a
1868 best-effort to avoid that. */
1869 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1871 ret = validate_new(ctrl, &cs->controls[i]);
1878 /* Try or try-and-set controls */
1879 static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1880 struct v4l2_ext_controls *cs,
1883 struct v4l2_ctrl_helper helper[4];
1884 struct v4l2_ctrl_helper *helpers = helper;
1888 cs->error_idx = cs->count;
1889 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1895 return class_check(hdl, cs->ctrl_class);
1897 if (cs->count > ARRAY_SIZE(helper)) {
1898 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1902 ret = prepare_ext_ctrls(hdl, cs, helpers);
1904 ret = validate_ctrls(cs, helpers, set);
1906 cs->error_idx = cs->count;
1907 for (i = 0; !ret && i < cs->count; i++) {
1908 struct v4l2_ctrl *master;
1911 if (helpers[i].mref == NULL)
1915 master = helpers[i].mref->ctrl;
1916 v4l2_ctrl_lock(master);
1918 /* Reset the 'is_new' flags of the cluster */
1919 for (j = 0; j < master->ncontrols; j++)
1920 if (master->cluster[j])
1921 master->cluster[j]->is_new = 0;
1923 /* Copy the new caller-supplied control values.
1924 user_to_new() sets 'is_new' to 1. */
1926 ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
1927 idx = helpers[idx].next;
1928 } while (!ret && idx);
1931 ret = try_or_set_cluster(fh, master, set);
1933 /* Copy the new values back to userspace. */
1937 ret = new_to_user(cs->controls + idx,
1939 idx = helpers[idx].next;
1940 } while (!ret && idx);
1942 v4l2_ctrl_unlock(master);
1945 if (cs->count > ARRAY_SIZE(helper))
1950 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1952 return try_set_ext_ctrls(NULL, hdl, cs, false);
1954 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1956 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1957 struct v4l2_ext_controls *cs)
1959 return try_set_ext_ctrls(fh, hdl, cs, true);
1961 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1963 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1965 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
1967 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1969 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1971 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
1973 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1975 /* Helper function for VIDIOC_S_CTRL compatibility */
1976 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
1978 struct v4l2_ctrl *master = ctrl->cluster[0];
1982 ret = validate_new_int(ctrl, val);
1986 v4l2_ctrl_lock(ctrl);
1988 /* Reset the 'is_new' flags of the cluster */
1989 for (i = 0; i < master->ncontrols; i++)
1990 if (master->cluster[i])
1991 master->cluster[i]->is_new = 0;
1995 ret = try_or_set_cluster(fh, master, true);
1996 *val = ctrl->cur.val;
1997 v4l2_ctrl_unlock(ctrl);
2001 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2002 struct v4l2_control *control)
2004 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2006 if (ctrl == NULL || !type_is_int(ctrl))
2009 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2012 return set_ctrl(fh, ctrl, &control->value);
2014 EXPORT_SYMBOL(v4l2_s_ctrl);
2016 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2018 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
2020 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2022 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2024 /* It's a driver bug if this happens. */
2025 WARN_ON(!type_is_int(ctrl));
2026 return set_ctrl(NULL, ctrl, &val);
2028 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
2030 void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
2031 struct v4l2_subscribed_event *sev)
2033 v4l2_ctrl_lock(ctrl);
2034 list_add_tail(&sev->node, &ctrl->ev_subs);
2035 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
2036 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
2037 struct v4l2_event ev;
2038 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
2040 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2041 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2042 fill_event(&ev, ctrl, changes);
2043 v4l2_event_queue_fh(sev->fh, &ev);
2045 v4l2_ctrl_unlock(ctrl);
2047 EXPORT_SYMBOL(v4l2_ctrl_add_event);
2049 void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
2050 struct v4l2_subscribed_event *sev)
2052 v4l2_ctrl_lock(ctrl);
2053 list_del(&sev->node);
2054 v4l2_ctrl_unlock(ctrl);
2056 EXPORT_SYMBOL(v4l2_ctrl_del_event);