2 V4L2 controls framework implementation.
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/ctype.h>
22 #include <linux/slab.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-dev.h>
28 /* Internal temporary helper struct, one for each v4l2_ext_control */
30 /* The control corresponding to the v4l2_ext_control ID field. */
31 struct v4l2_ctrl
*ctrl
;
32 /* Used internally to mark whether this control was already
37 /* Returns NULL or a character pointer array containing the menu for
38 the given control ID. The pointer array ends with a NULL pointer.
39 An empty string signifies a menu entry that is invalid. This allows
40 drivers to disable certain options if it is not supported. */
41 const char * const *v4l2_ctrl_get_menu(u32 id
)
43 static const char * const mpeg_audio_sampling_freq
[] = {
49 static const char * const mpeg_audio_encoding
[] = {
57 static const char * const mpeg_audio_l1_bitrate
[] = {
74 static const char * const mpeg_audio_l2_bitrate
[] = {
91 static const char * const mpeg_audio_l3_bitrate
[] = {
108 static const char * const mpeg_audio_ac3_bitrate
[] = {
130 static const char * const mpeg_audio_mode
[] = {
137 static const char * const mpeg_audio_mode_extension
[] = {
144 static const char * const mpeg_audio_emphasis
[] = {
150 static const char * const mpeg_audio_crc
[] = {
155 static const char * const mpeg_video_encoding
[] = {
161 static const char * const mpeg_video_aspect
[] = {
168 static const char * const mpeg_video_bitrate_mode
[] = {
173 static const char * const mpeg_stream_type
[] = {
174 "MPEG-2 Program Stream",
175 "MPEG-2 Transport Stream",
176 "MPEG-1 System Stream",
177 "MPEG-2 DVD-compatible Stream",
178 "MPEG-1 VCD-compatible Stream",
179 "MPEG-2 SVCD-compatible Stream",
182 static const char * const mpeg_stream_vbi_fmt
[] = {
184 "Private packet, IVTV format",
187 static const char * const camera_power_line_frequency
[] = {
193 static const char * const camera_exposure_auto
[] = {
196 "Shutter Priority Mode",
197 "Aperture Priority Mode",
200 static const char * const colorfx
[] = {
213 static const char * const tune_preemphasis
[] = {
221 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ
:
222 return mpeg_audio_sampling_freq
;
223 case V4L2_CID_MPEG_AUDIO_ENCODING
:
224 return mpeg_audio_encoding
;
225 case V4L2_CID_MPEG_AUDIO_L1_BITRATE
:
226 return mpeg_audio_l1_bitrate
;
227 case V4L2_CID_MPEG_AUDIO_L2_BITRATE
:
228 return mpeg_audio_l2_bitrate
;
229 case V4L2_CID_MPEG_AUDIO_L3_BITRATE
:
230 return mpeg_audio_l3_bitrate
;
231 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE
:
232 return mpeg_audio_ac3_bitrate
;
233 case V4L2_CID_MPEG_AUDIO_MODE
:
234 return mpeg_audio_mode
;
235 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
:
236 return mpeg_audio_mode_extension
;
237 case V4L2_CID_MPEG_AUDIO_EMPHASIS
:
238 return mpeg_audio_emphasis
;
239 case V4L2_CID_MPEG_AUDIO_CRC
:
240 return mpeg_audio_crc
;
241 case V4L2_CID_MPEG_VIDEO_ENCODING
:
242 return mpeg_video_encoding
;
243 case V4L2_CID_MPEG_VIDEO_ASPECT
:
244 return mpeg_video_aspect
;
245 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
246 return mpeg_video_bitrate_mode
;
247 case V4L2_CID_MPEG_STREAM_TYPE
:
248 return mpeg_stream_type
;
249 case V4L2_CID_MPEG_STREAM_VBI_FMT
:
250 return mpeg_stream_vbi_fmt
;
251 case V4L2_CID_POWER_LINE_FREQUENCY
:
252 return camera_power_line_frequency
;
253 case V4L2_CID_EXPOSURE_AUTO
:
254 return camera_exposure_auto
;
255 case V4L2_CID_COLORFX
:
257 case V4L2_CID_TUNE_PREEMPHASIS
:
258 return tune_preemphasis
;
263 EXPORT_SYMBOL(v4l2_ctrl_get_menu
);
265 /* Return the control name. */
266 const char *v4l2_ctrl_get_name(u32 id
)
270 /* Keep the order of the 'case's the same as in videodev2.h! */
271 case V4L2_CID_USER_CLASS
: return "User Controls";
272 case V4L2_CID_BRIGHTNESS
: return "Brightness";
273 case V4L2_CID_CONTRAST
: return "Contrast";
274 case V4L2_CID_SATURATION
: return "Saturation";
275 case V4L2_CID_HUE
: return "Hue";
276 case V4L2_CID_AUDIO_VOLUME
: return "Volume";
277 case V4L2_CID_AUDIO_BALANCE
: return "Balance";
278 case V4L2_CID_AUDIO_BASS
: return "Bass";
279 case V4L2_CID_AUDIO_TREBLE
: return "Treble";
280 case V4L2_CID_AUDIO_MUTE
: return "Mute";
281 case V4L2_CID_AUDIO_LOUDNESS
: return "Loudness";
282 case V4L2_CID_BLACK_LEVEL
: return "Black Level";
283 case V4L2_CID_AUTO_WHITE_BALANCE
: return "White Balance, Automatic";
284 case V4L2_CID_DO_WHITE_BALANCE
: return "Do White Balance";
285 case V4L2_CID_RED_BALANCE
: return "Red Balance";
286 case V4L2_CID_BLUE_BALANCE
: return "Blue Balance";
287 case V4L2_CID_GAMMA
: return "Gamma";
288 case V4L2_CID_EXPOSURE
: return "Exposure";
289 case V4L2_CID_AUTOGAIN
: return "Gain, Automatic";
290 case V4L2_CID_GAIN
: return "Gain";
291 case V4L2_CID_HFLIP
: return "Horizontal Flip";
292 case V4L2_CID_VFLIP
: return "Vertical Flip";
293 case V4L2_CID_HCENTER
: return "Horizontal Center";
294 case V4L2_CID_VCENTER
: return "Vertical Center";
295 case V4L2_CID_POWER_LINE_FREQUENCY
: return "Power Line Frequency";
296 case V4L2_CID_HUE_AUTO
: return "Hue, Automatic";
297 case V4L2_CID_WHITE_BALANCE_TEMPERATURE
: return "White Balance Temperature";
298 case V4L2_CID_SHARPNESS
: return "Sharpness";
299 case V4L2_CID_BACKLIGHT_COMPENSATION
: return "Backlight Compensation";
300 case V4L2_CID_CHROMA_AGC
: return "Chroma AGC";
301 case V4L2_CID_COLOR_KILLER
: return "Color Killer";
302 case V4L2_CID_COLORFX
: return "Color Effects";
303 case V4L2_CID_AUTOBRIGHTNESS
: return "Brightness, Automatic";
304 case V4L2_CID_BAND_STOP_FILTER
: return "Band-Stop Filter";
305 case V4L2_CID_ROTATE
: return "Rotate";
306 case V4L2_CID_BG_COLOR
: return "Background Color";
307 case V4L2_CID_CHROMA_GAIN
: return "Chroma Gain";
308 case V4L2_CID_ILLUMINATORS_1
: return "Illuminator 1";
309 case V4L2_CID_ILLUMINATORS_2
: return "Illuminator 2";
312 /* Keep the order of the 'case's the same as in videodev2.h! */
313 case V4L2_CID_MPEG_CLASS
: return "MPEG Encoder Controls";
314 case V4L2_CID_MPEG_STREAM_TYPE
: return "Stream Type";
315 case V4L2_CID_MPEG_STREAM_PID_PMT
: return "Stream PMT Program ID";
316 case V4L2_CID_MPEG_STREAM_PID_AUDIO
: return "Stream Audio Program ID";
317 case V4L2_CID_MPEG_STREAM_PID_VIDEO
: return "Stream Video Program ID";
318 case V4L2_CID_MPEG_STREAM_PID_PCR
: return "Stream PCR Program ID";
319 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO
: return "Stream PES Audio ID";
320 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO
: return "Stream PES Video ID";
321 case V4L2_CID_MPEG_STREAM_VBI_FMT
: return "Stream VBI Format";
322 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ
: return "Audio Sampling Frequency";
323 case V4L2_CID_MPEG_AUDIO_ENCODING
: return "Audio Encoding";
324 case V4L2_CID_MPEG_AUDIO_L1_BITRATE
: return "Audio Layer I Bitrate";
325 case V4L2_CID_MPEG_AUDIO_L2_BITRATE
: return "Audio Layer II Bitrate";
326 case V4L2_CID_MPEG_AUDIO_L3_BITRATE
: return "Audio Layer III Bitrate";
327 case V4L2_CID_MPEG_AUDIO_MODE
: return "Audio Stereo Mode";
328 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
: return "Audio Stereo Mode Extension";
329 case V4L2_CID_MPEG_AUDIO_EMPHASIS
: return "Audio Emphasis";
330 case V4L2_CID_MPEG_AUDIO_CRC
: return "Audio CRC";
331 case V4L2_CID_MPEG_AUDIO_MUTE
: return "Audio Mute";
332 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE
: return "Audio AAC Bitrate";
333 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE
: return "Audio AC-3 Bitrate";
334 case V4L2_CID_MPEG_VIDEO_ENCODING
: return "Video Encoding";
335 case V4L2_CID_MPEG_VIDEO_ASPECT
: return "Video Aspect";
336 case V4L2_CID_MPEG_VIDEO_B_FRAMES
: return "Video B Frames";
337 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
: return "Video GOP Size";
338 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE
: return "Video GOP Closure";
339 case V4L2_CID_MPEG_VIDEO_PULLDOWN
: return "Video Pulldown";
340 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
: return "Video Bitrate Mode";
341 case V4L2_CID_MPEG_VIDEO_BITRATE
: return "Video Bitrate";
342 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
: return "Video Peak Bitrate";
343 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION
: return "Video Temporal Decimation";
344 case V4L2_CID_MPEG_VIDEO_MUTE
: return "Video Mute";
345 case V4L2_CID_MPEG_VIDEO_MUTE_YUV
: return "Video Mute YUV";
347 /* CAMERA controls */
348 /* Keep the order of the 'case's the same as in videodev2.h! */
349 case V4L2_CID_CAMERA_CLASS
: return "Camera Controls";
350 case V4L2_CID_EXPOSURE_AUTO
: return "Auto Exposure";
351 case V4L2_CID_EXPOSURE_ABSOLUTE
: return "Exposure Time, Absolute";
352 case V4L2_CID_EXPOSURE_AUTO_PRIORITY
: return "Exposure, Dynamic Framerate";
353 case V4L2_CID_PAN_RELATIVE
: return "Pan, Relative";
354 case V4L2_CID_TILT_RELATIVE
: return "Tilt, Relative";
355 case V4L2_CID_PAN_RESET
: return "Pan, Reset";
356 case V4L2_CID_TILT_RESET
: return "Tilt, Reset";
357 case V4L2_CID_PAN_ABSOLUTE
: return "Pan, Absolute";
358 case V4L2_CID_TILT_ABSOLUTE
: return "Tilt, Absolute";
359 case V4L2_CID_FOCUS_ABSOLUTE
: return "Focus, Absolute";
360 case V4L2_CID_FOCUS_RELATIVE
: return "Focus, Relative";
361 case V4L2_CID_FOCUS_AUTO
: return "Focus, Automatic";
362 case V4L2_CID_ZOOM_ABSOLUTE
: return "Zoom, Absolute";
363 case V4L2_CID_ZOOM_RELATIVE
: return "Zoom, Relative";
364 case V4L2_CID_ZOOM_CONTINUOUS
: return "Zoom, Continuous";
365 case V4L2_CID_PRIVACY
: return "Privacy";
366 case V4L2_CID_IRIS_ABSOLUTE
: return "Iris, Absolute";
367 case V4L2_CID_IRIS_RELATIVE
: return "Iris, Relative";
369 /* FM Radio Modulator control */
370 /* Keep the order of the 'case's the same as in videodev2.h! */
371 case V4L2_CID_FM_TX_CLASS
: return "FM Radio Modulator Controls";
372 case V4L2_CID_RDS_TX_DEVIATION
: return "RDS Signal Deviation";
373 case V4L2_CID_RDS_TX_PI
: return "RDS Program ID";
374 case V4L2_CID_RDS_TX_PTY
: return "RDS Program Type";
375 case V4L2_CID_RDS_TX_PS_NAME
: return "RDS PS Name";
376 case V4L2_CID_RDS_TX_RADIO_TEXT
: return "RDS Radio Text";
377 case V4L2_CID_AUDIO_LIMITER_ENABLED
: return "Audio Limiter Feature Enabled";
378 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME
: return "Audio Limiter Release Time";
379 case V4L2_CID_AUDIO_LIMITER_DEVIATION
: return "Audio Limiter Deviation";
380 case V4L2_CID_AUDIO_COMPRESSION_ENABLED
: return "Audio Compression Feature Enabled";
381 case V4L2_CID_AUDIO_COMPRESSION_GAIN
: return "Audio Compression Gain";
382 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD
: return "Audio Compression Threshold";
383 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME
: return "Audio Compression Attack Time";
384 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME
: return "Audio Compression Release Time";
385 case V4L2_CID_PILOT_TONE_ENABLED
: return "Pilot Tone Feature Enabled";
386 case V4L2_CID_PILOT_TONE_DEVIATION
: return "Pilot Tone Deviation";
387 case V4L2_CID_PILOT_TONE_FREQUENCY
: return "Pilot Tone Frequency";
388 case V4L2_CID_TUNE_PREEMPHASIS
: return "Pre-emphasis settings";
389 case V4L2_CID_TUNE_POWER_LEVEL
: return "Tune Power Level";
390 case V4L2_CID_TUNE_ANTENNA_CAPACITOR
: return "Tune Antenna Capacitor";
396 EXPORT_SYMBOL(v4l2_ctrl_get_name
);
398 void v4l2_ctrl_fill(u32 id
, const char **name
, enum v4l2_ctrl_type
*type
,
399 s32
*min
, s32
*max
, s32
*step
, s32
*def
, u32
*flags
)
401 *name
= v4l2_ctrl_get_name(id
);
405 case V4L2_CID_AUDIO_MUTE
:
406 case V4L2_CID_AUDIO_LOUDNESS
:
407 case V4L2_CID_AUTO_WHITE_BALANCE
:
408 case V4L2_CID_AUTOGAIN
:
411 case V4L2_CID_HUE_AUTO
:
412 case V4L2_CID_CHROMA_AGC
:
413 case V4L2_CID_COLOR_KILLER
:
414 case V4L2_CID_MPEG_AUDIO_MUTE
:
415 case V4L2_CID_MPEG_VIDEO_MUTE
:
416 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE
:
417 case V4L2_CID_MPEG_VIDEO_PULLDOWN
:
418 case V4L2_CID_EXPOSURE_AUTO_PRIORITY
:
419 case V4L2_CID_FOCUS_AUTO
:
420 case V4L2_CID_PRIVACY
:
421 case V4L2_CID_AUDIO_LIMITER_ENABLED
:
422 case V4L2_CID_AUDIO_COMPRESSION_ENABLED
:
423 case V4L2_CID_PILOT_TONE_ENABLED
:
424 case V4L2_CID_ILLUMINATORS_1
:
425 case V4L2_CID_ILLUMINATORS_2
:
426 *type
= V4L2_CTRL_TYPE_BOOLEAN
;
430 case V4L2_CID_PAN_RESET
:
431 case V4L2_CID_TILT_RESET
:
432 *type
= V4L2_CTRL_TYPE_BUTTON
;
433 *flags
|= V4L2_CTRL_FLAG_WRITE_ONLY
;
434 *min
= *max
= *step
= *def
= 0;
436 case V4L2_CID_POWER_LINE_FREQUENCY
:
437 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ
:
438 case V4L2_CID_MPEG_AUDIO_ENCODING
:
439 case V4L2_CID_MPEG_AUDIO_L1_BITRATE
:
440 case V4L2_CID_MPEG_AUDIO_L2_BITRATE
:
441 case V4L2_CID_MPEG_AUDIO_L3_BITRATE
:
442 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE
:
443 case V4L2_CID_MPEG_AUDIO_MODE
:
444 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
:
445 case V4L2_CID_MPEG_AUDIO_EMPHASIS
:
446 case V4L2_CID_MPEG_AUDIO_CRC
:
447 case V4L2_CID_MPEG_VIDEO_ENCODING
:
448 case V4L2_CID_MPEG_VIDEO_ASPECT
:
449 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
450 case V4L2_CID_MPEG_STREAM_TYPE
:
451 case V4L2_CID_MPEG_STREAM_VBI_FMT
:
452 case V4L2_CID_EXPOSURE_AUTO
:
453 case V4L2_CID_COLORFX
:
454 case V4L2_CID_TUNE_PREEMPHASIS
:
455 *type
= V4L2_CTRL_TYPE_MENU
;
457 case V4L2_CID_RDS_TX_PS_NAME
:
458 case V4L2_CID_RDS_TX_RADIO_TEXT
:
459 *type
= V4L2_CTRL_TYPE_STRING
;
461 case V4L2_CID_USER_CLASS
:
462 case V4L2_CID_CAMERA_CLASS
:
463 case V4L2_CID_MPEG_CLASS
:
464 case V4L2_CID_FM_TX_CLASS
:
465 *type
= V4L2_CTRL_TYPE_CTRL_CLASS
;
466 /* You can neither read not write these */
467 *flags
|= V4L2_CTRL_FLAG_READ_ONLY
| V4L2_CTRL_FLAG_WRITE_ONLY
;
468 *min
= *max
= *step
= *def
= 0;
470 case V4L2_CID_BG_COLOR
:
471 *type
= V4L2_CTRL_TYPE_INTEGER
;
474 /* Max is calculated as RGB888 that is 2^24 */
478 *type
= V4L2_CTRL_TYPE_INTEGER
;
482 case V4L2_CID_MPEG_AUDIO_ENCODING
:
483 case V4L2_CID_MPEG_AUDIO_MODE
:
484 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
485 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
486 case V4L2_CID_MPEG_STREAM_TYPE
:
487 *flags
|= V4L2_CTRL_FLAG_UPDATE
;
489 case V4L2_CID_AUDIO_VOLUME
:
490 case V4L2_CID_AUDIO_BALANCE
:
491 case V4L2_CID_AUDIO_BASS
:
492 case V4L2_CID_AUDIO_TREBLE
:
493 case V4L2_CID_BRIGHTNESS
:
494 case V4L2_CID_CONTRAST
:
495 case V4L2_CID_SATURATION
:
497 case V4L2_CID_RED_BALANCE
:
498 case V4L2_CID_BLUE_BALANCE
:
500 case V4L2_CID_SHARPNESS
:
501 case V4L2_CID_CHROMA_GAIN
:
502 case V4L2_CID_RDS_TX_DEVIATION
:
503 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME
:
504 case V4L2_CID_AUDIO_LIMITER_DEVIATION
:
505 case V4L2_CID_AUDIO_COMPRESSION_GAIN
:
506 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD
:
507 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME
:
508 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME
:
509 case V4L2_CID_PILOT_TONE_DEVIATION
:
510 case V4L2_CID_PILOT_TONE_FREQUENCY
:
511 case V4L2_CID_TUNE_POWER_LEVEL
:
512 case V4L2_CID_TUNE_ANTENNA_CAPACITOR
:
513 *flags
|= V4L2_CTRL_FLAG_SLIDER
;
515 case V4L2_CID_PAN_RELATIVE
:
516 case V4L2_CID_TILT_RELATIVE
:
517 case V4L2_CID_FOCUS_RELATIVE
:
518 case V4L2_CID_IRIS_RELATIVE
:
519 case V4L2_CID_ZOOM_RELATIVE
:
520 *flags
|= V4L2_CTRL_FLAG_WRITE_ONLY
;
524 EXPORT_SYMBOL(v4l2_ctrl_fill
);
526 /* Helper function to determine whether the control type is compatible with
528 static bool type_is_int(const struct v4l2_ctrl
*ctrl
)
530 switch (ctrl
->type
) {
531 case V4L2_CTRL_TYPE_INTEGER64
:
532 case V4L2_CTRL_TYPE_STRING
:
533 /* Nope, these need v4l2_ext_control */
540 /* Helper function: copy the current control value back to the caller */
541 static int cur_to_user(struct v4l2_ext_control
*c
,
542 struct v4l2_ctrl
*ctrl
)
546 switch (ctrl
->type
) {
547 case V4L2_CTRL_TYPE_STRING
:
548 len
= strlen(ctrl
->cur
.string
);
549 if (c
->size
< len
+ 1) {
553 return copy_to_user(c
->string
, ctrl
->cur
.string
,
554 len
+ 1) ? -EFAULT
: 0;
555 case V4L2_CTRL_TYPE_INTEGER64
:
556 c
->value64
= ctrl
->cur
.val64
;
559 c
->value
= ctrl
->cur
.val
;
565 /* Helper function: copy the caller-provider value as the new control value */
566 static int user_to_new(struct v4l2_ext_control
*c
,
567 struct v4l2_ctrl
*ctrl
)
573 switch (ctrl
->type
) {
574 case V4L2_CTRL_TYPE_INTEGER64
:
575 ctrl
->val64
= c
->value64
;
577 case V4L2_CTRL_TYPE_STRING
:
581 if (size
> ctrl
->maximum
+ 1)
582 size
= ctrl
->maximum
+ 1;
583 ret
= copy_from_user(ctrl
->string
, c
->string
, size
);
585 char last
= ctrl
->string
[size
- 1];
587 ctrl
->string
[size
- 1] = 0;
588 /* If the string was longer than ctrl->maximum,
589 then return an error. */
590 if (strlen(ctrl
->string
) == ctrl
->maximum
&& last
)
593 return ret
? -EFAULT
: 0;
595 ctrl
->val
= c
->value
;
601 /* Helper function: copy the new control value back to the caller */
602 static int new_to_user(struct v4l2_ext_control
*c
,
603 struct v4l2_ctrl
*ctrl
)
607 switch (ctrl
->type
) {
608 case V4L2_CTRL_TYPE_STRING
:
609 len
= strlen(ctrl
->string
);
610 if (c
->size
< len
+ 1) {
611 c
->size
= ctrl
->maximum
+ 1;
614 return copy_to_user(c
->string
, ctrl
->string
,
615 len
+ 1) ? -EFAULT
: 0;
616 case V4L2_CTRL_TYPE_INTEGER64
:
617 c
->value64
= ctrl
->val64
;
620 c
->value
= ctrl
->val
;
626 /* Copy the new value to the current value. */
627 static void new_to_cur(struct v4l2_ctrl
*ctrl
)
631 switch (ctrl
->type
) {
632 case V4L2_CTRL_TYPE_STRING
:
633 /* strings are always 0-terminated */
634 strcpy(ctrl
->cur
.string
, ctrl
->string
);
636 case V4L2_CTRL_TYPE_INTEGER64
:
637 ctrl
->cur
.val64
= ctrl
->val64
;
640 ctrl
->cur
.val
= ctrl
->val
;
645 /* Copy the current value to the new value */
646 static void cur_to_new(struct v4l2_ctrl
*ctrl
)
650 switch (ctrl
->type
) {
651 case V4L2_CTRL_TYPE_STRING
:
652 /* strings are always 0-terminated */
653 strcpy(ctrl
->string
, ctrl
->cur
.string
);
655 case V4L2_CTRL_TYPE_INTEGER64
:
656 ctrl
->val64
= ctrl
->cur
.val64
;
659 ctrl
->val
= ctrl
->cur
.val
;
664 /* Return non-zero if one or more of the controls in the cluster has a new
665 value that differs from the current value. */
666 static int cluster_changed(struct v4l2_ctrl
*master
)
671 for (i
= 0; !diff
&& i
< master
->ncontrols
; i
++) {
672 struct v4l2_ctrl
*ctrl
= master
->cluster
[i
];
676 switch (ctrl
->type
) {
677 case V4L2_CTRL_TYPE_BUTTON
:
678 /* Button controls are always 'different' */
680 case V4L2_CTRL_TYPE_STRING
:
681 /* strings are always 0-terminated */
682 diff
= strcmp(ctrl
->string
, ctrl
->cur
.string
);
684 case V4L2_CTRL_TYPE_INTEGER64
:
685 diff
= ctrl
->val64
!= ctrl
->cur
.val64
;
688 diff
= ctrl
->val
!= ctrl
->cur
.val
;
695 /* Validate a new control */
696 static int validate_new(struct v4l2_ctrl
*ctrl
)
699 char *s
= ctrl
->string
;
703 switch (ctrl
->type
) {
704 case V4L2_CTRL_TYPE_INTEGER
:
705 /* Round towards the closest legal value */
706 val
+= ctrl
->step
/ 2;
707 if (val
< ctrl
->minimum
)
709 if (val
> ctrl
->maximum
)
711 offset
= val
- ctrl
->minimum
;
712 offset
= ctrl
->step
* (offset
/ ctrl
->step
);
713 val
= ctrl
->minimum
+ offset
;
717 case V4L2_CTRL_TYPE_BOOLEAN
:
718 ctrl
->val
= !!ctrl
->val
;
721 case V4L2_CTRL_TYPE_MENU
:
722 if (val
< ctrl
->minimum
|| val
> ctrl
->maximum
)
724 if (ctrl
->qmenu
[val
][0] == '\0' ||
725 (ctrl
->menu_skip_mask
& (1 << val
)))
729 case V4L2_CTRL_TYPE_BUTTON
:
730 case V4L2_CTRL_TYPE_CTRL_CLASS
:
734 case V4L2_CTRL_TYPE_INTEGER64
:
737 case V4L2_CTRL_TYPE_STRING
:
739 if (len
< ctrl
->minimum
)
741 if ((len
- ctrl
->minimum
) % ctrl
->step
)
750 static inline u32
node2id(struct list_head
*node
)
752 return list_entry(node
, struct v4l2_ctrl_ref
, node
)->ctrl
->id
;
755 /* Set the handler's error code if it wasn't set earlier already */
756 static inline int handler_set_err(struct v4l2_ctrl_handler
*hdl
, int err
)
763 /* Initialize the handler */
764 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler
*hdl
,
765 unsigned nr_of_controls_hint
)
767 mutex_init(&hdl
->lock
);
768 INIT_LIST_HEAD(&hdl
->ctrls
);
769 INIT_LIST_HEAD(&hdl
->ctrl_refs
);
770 hdl
->nr_of_buckets
= 1 + nr_of_controls_hint
/ 8;
771 hdl
->buckets
= kzalloc(sizeof(hdl
->buckets
[0]) * hdl
->nr_of_buckets
,
773 hdl
->error
= hdl
->buckets
? 0 : -ENOMEM
;
776 EXPORT_SYMBOL(v4l2_ctrl_handler_init
);
778 /* Free all controls and control refs */
779 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler
*hdl
)
781 struct v4l2_ctrl_ref
*ref
, *next_ref
;
782 struct v4l2_ctrl
*ctrl
, *next_ctrl
;
784 if (hdl
== NULL
|| hdl
->buckets
== NULL
)
787 mutex_lock(&hdl
->lock
);
789 list_for_each_entry_safe(ref
, next_ref
, &hdl
->ctrl_refs
, node
) {
790 list_del(&ref
->node
);
793 /* Free all controls owned by the handler */
794 list_for_each_entry_safe(ctrl
, next_ctrl
, &hdl
->ctrls
, node
) {
795 list_del(&ctrl
->node
);
802 mutex_unlock(&hdl
->lock
);
804 EXPORT_SYMBOL(v4l2_ctrl_handler_free
);
806 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
807 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
808 with applications that do not use the NEXT_CTRL flag.
810 We just find the n-th private user control. It's O(N), but that should not
811 be an issue in this particular case. */
812 static struct v4l2_ctrl_ref
*find_private_ref(
813 struct v4l2_ctrl_handler
*hdl
, u32 id
)
815 struct v4l2_ctrl_ref
*ref
;
817 id
-= V4L2_CID_PRIVATE_BASE
;
818 list_for_each_entry(ref
, &hdl
->ctrl_refs
, node
) {
819 /* Search for private user controls that are compatible with
821 if (V4L2_CTRL_ID2CLASS(ref
->ctrl
->id
) == V4L2_CTRL_CLASS_USER
&&
822 V4L2_CTRL_DRIVER_PRIV(ref
->ctrl
->id
)) {
823 if (!type_is_int(ref
->ctrl
))
833 /* Find a control with the given ID. */
834 static struct v4l2_ctrl_ref
*find_ref(struct v4l2_ctrl_handler
*hdl
, u32 id
)
836 struct v4l2_ctrl_ref
*ref
;
839 id
&= V4L2_CTRL_ID_MASK
;
841 /* Old-style private controls need special handling */
842 if (id
>= V4L2_CID_PRIVATE_BASE
)
843 return find_private_ref(hdl
, id
);
844 bucket
= id
% hdl
->nr_of_buckets
;
846 /* Simple optimization: cache the last control found */
847 if (hdl
->cached
&& hdl
->cached
->ctrl
->id
== id
)
850 /* Not in cache, search the hash */
851 ref
= hdl
->buckets
? hdl
->buckets
[bucket
] : NULL
;
852 while (ref
&& ref
->ctrl
->id
!= id
)
856 hdl
->cached
= ref
; /* cache it! */
860 /* Find a control with the given ID. Take the handler's lock first. */
861 static struct v4l2_ctrl_ref
*find_ref_lock(
862 struct v4l2_ctrl_handler
*hdl
, u32 id
)
864 struct v4l2_ctrl_ref
*ref
= NULL
;
867 mutex_lock(&hdl
->lock
);
868 ref
= find_ref(hdl
, id
);
869 mutex_unlock(&hdl
->lock
);
874 /* Find a control with the given ID. */
875 struct v4l2_ctrl
*v4l2_ctrl_find(struct v4l2_ctrl_handler
*hdl
, u32 id
)
877 struct v4l2_ctrl_ref
*ref
= find_ref_lock(hdl
, id
);
879 return ref
? ref
->ctrl
: NULL
;
881 EXPORT_SYMBOL(v4l2_ctrl_find
);
883 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
884 static int handler_new_ref(struct v4l2_ctrl_handler
*hdl
,
885 struct v4l2_ctrl
*ctrl
)
887 struct v4l2_ctrl_ref
*ref
;
888 struct v4l2_ctrl_ref
*new_ref
;
890 u32 class_ctrl
= V4L2_CTRL_ID2CLASS(id
) | 1;
891 int bucket
= id
% hdl
->nr_of_buckets
; /* which bucket to use */
893 /* Automatically add the control class if it is not yet present. */
894 if (id
!= class_ctrl
&& find_ref_lock(hdl
, class_ctrl
) == NULL
)
895 if (!v4l2_ctrl_new_std(hdl
, NULL
, class_ctrl
, 0, 0, 0, 0))
901 new_ref
= kzalloc(sizeof(*new_ref
), GFP_KERNEL
);
903 return handler_set_err(hdl
, -ENOMEM
);
904 new_ref
->ctrl
= ctrl
;
905 if (ctrl
->handler
== hdl
) {
906 /* By default each control starts in a cluster of its own.
907 new_ref->ctrl is basically a cluster array with one
908 element, so that's perfect to use as the cluster pointer.
909 But only do this for the handler that owns the control. */
910 ctrl
->cluster
= &new_ref
->ctrl
;
914 INIT_LIST_HEAD(&new_ref
->node
);
916 mutex_lock(&hdl
->lock
);
918 /* Add immediately at the end of the list if the list is empty, or if
919 the last element in the list has a lower ID.
920 This ensures that when elements are added in ascending order the
921 insertion is an O(1) operation. */
922 if (list_empty(&hdl
->ctrl_refs
) || id
> node2id(hdl
->ctrl_refs
.prev
)) {
923 list_add_tail(&new_ref
->node
, &hdl
->ctrl_refs
);
927 /* Find insert position in sorted list */
928 list_for_each_entry(ref
, &hdl
->ctrl_refs
, node
) {
929 if (ref
->ctrl
->id
< id
)
931 /* Don't add duplicates */
932 if (ref
->ctrl
->id
== id
) {
936 list_add(&new_ref
->node
, ref
->node
.prev
);
941 /* Insert the control node in the hash */
942 new_ref
->next
= hdl
->buckets
[bucket
];
943 hdl
->buckets
[bucket
] = new_ref
;
946 mutex_unlock(&hdl
->lock
);
950 /* Add a new control */
951 static struct v4l2_ctrl
*v4l2_ctrl_new(struct v4l2_ctrl_handler
*hdl
,
952 const struct v4l2_ctrl_ops
*ops
,
953 u32 id
, const char *name
, enum v4l2_ctrl_type type
,
954 s32 min
, s32 max
, u32 step
, s32 def
,
955 u32 flags
, const char * const *qmenu
, void *priv
)
957 struct v4l2_ctrl
*ctrl
;
958 unsigned sz_extra
= 0;
964 if (id
== 0 || name
== NULL
|| id
>= V4L2_CID_PRIVATE_BASE
||
965 def
< min
|| def
> max
|| max
< min
||
966 (type
== V4L2_CTRL_TYPE_INTEGER
&& step
== 0) ||
967 (type
== V4L2_CTRL_TYPE_MENU
&& qmenu
== NULL
) ||
968 (type
== V4L2_CTRL_TYPE_STRING
&& max
== 0)) {
969 handler_set_err(hdl
, -ERANGE
);
973 if (type
== V4L2_CTRL_TYPE_BUTTON
)
974 flags
|= V4L2_CTRL_FLAG_WRITE_ONLY
;
975 else if (type
== V4L2_CTRL_TYPE_CTRL_CLASS
)
976 flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
977 else if (type
== V4L2_CTRL_TYPE_STRING
)
978 sz_extra
+= 2 * (max
+ 1);
980 ctrl
= kzalloc(sizeof(*ctrl
) + sz_extra
, GFP_KERNEL
);
982 handler_set_err(hdl
, -ENOMEM
);
986 INIT_LIST_HEAD(&ctrl
->node
);
998 ctrl
->cur
.val
= ctrl
->val
= ctrl
->default_value
= def
;
1000 if (ctrl
->type
== V4L2_CTRL_TYPE_STRING
) {
1001 ctrl
->cur
.string
= (char *)&ctrl
[1] + sz_extra
- (max
+ 1);
1002 ctrl
->string
= (char *)&ctrl
[1] + sz_extra
- 2 * (max
+ 1);
1004 memset(ctrl
->cur
.string
, ' ', ctrl
->minimum
);
1006 if (handler_new_ref(hdl
, ctrl
)) {
1010 mutex_lock(&hdl
->lock
);
1011 list_add_tail(&ctrl
->node
, &hdl
->ctrls
);
1012 mutex_unlock(&hdl
->lock
);
1016 struct v4l2_ctrl
*v4l2_ctrl_new_custom(struct v4l2_ctrl_handler
*hdl
,
1017 const struct v4l2_ctrl_config
*cfg
, void *priv
)
1020 struct v4l2_ctrl
*ctrl
;
1021 const char *name
= cfg
->name
;
1022 const char * const *qmenu
= cfg
->qmenu
;
1023 enum v4l2_ctrl_type type
= cfg
->type
;
1024 u32 flags
= cfg
->flags
;
1027 u32 step
= cfg
->step
;
1031 v4l2_ctrl_fill(cfg
->id
, &name
, &type
, &min
, &max
, &step
,
1034 is_menu
= (cfg
->type
== V4L2_CTRL_TYPE_MENU
);
1038 WARN_ON(cfg
->menu_skip_mask
);
1039 if (is_menu
&& qmenu
== NULL
)
1040 qmenu
= v4l2_ctrl_get_menu(cfg
->id
);
1042 ctrl
= v4l2_ctrl_new(hdl
, cfg
->ops
, cfg
->id
, name
,
1044 is_menu
? cfg
->menu_skip_mask
: step
,
1045 def
, flags
, qmenu
, priv
);
1047 ctrl
->is_private
= cfg
->is_private
;
1048 ctrl
->is_volatile
= cfg
->is_volatile
;
1052 EXPORT_SYMBOL(v4l2_ctrl_new_custom
);
1054 /* Helper function for standard non-menu controls */
1055 struct v4l2_ctrl
*v4l2_ctrl_new_std(struct v4l2_ctrl_handler
*hdl
,
1056 const struct v4l2_ctrl_ops
*ops
,
1057 u32 id
, s32 min
, s32 max
, u32 step
, s32 def
)
1060 enum v4l2_ctrl_type type
;
1063 v4l2_ctrl_fill(id
, &name
, &type
, &min
, &max
, &step
, &def
, &flags
);
1064 if (type
== V4L2_CTRL_TYPE_MENU
) {
1065 handler_set_err(hdl
, -EINVAL
);
1068 return v4l2_ctrl_new(hdl
, ops
, id
, name
, type
,
1069 min
, max
, step
, def
, flags
, NULL
, NULL
);
1071 EXPORT_SYMBOL(v4l2_ctrl_new_std
);
1073 /* Helper function for standard menu controls */
1074 struct v4l2_ctrl
*v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler
*hdl
,
1075 const struct v4l2_ctrl_ops
*ops
,
1076 u32 id
, s32 max
, s32 mask
, s32 def
)
1078 const char * const *qmenu
= v4l2_ctrl_get_menu(id
);
1080 enum v4l2_ctrl_type type
;
1085 v4l2_ctrl_fill(id
, &name
, &type
, &min
, &max
, &step
, &def
, &flags
);
1086 if (type
!= V4L2_CTRL_TYPE_MENU
) {
1087 handler_set_err(hdl
, -EINVAL
);
1090 return v4l2_ctrl_new(hdl
, ops
, id
, name
, type
,
1091 0, max
, mask
, def
, flags
, qmenu
, NULL
);
1093 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu
);
1095 /* Add a control from another handler to this handler */
1096 struct v4l2_ctrl
*v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler
*hdl
,
1097 struct v4l2_ctrl
*ctrl
)
1099 if (hdl
== NULL
|| hdl
->error
)
1102 handler_set_err(hdl
, -EINVAL
);
1105 if (ctrl
->handler
== hdl
)
1107 return handler_new_ref(hdl
, ctrl
) ? NULL
: ctrl
;
1109 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl
);
1111 /* Add the controls from another handler to our own. */
1112 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler
*hdl
,
1113 struct v4l2_ctrl_handler
*add
)
1115 struct v4l2_ctrl
*ctrl
;
1118 /* Do nothing if either handler is NULL or if they are the same */
1119 if (!hdl
|| !add
|| hdl
== add
)
1123 mutex_lock(&add
->lock
);
1124 list_for_each_entry(ctrl
, &add
->ctrls
, node
) {
1125 /* Skip handler-private controls. */
1126 if (ctrl
->is_private
)
1128 ret
= handler_new_ref(hdl
, ctrl
);
1132 mutex_unlock(&add
->lock
);
1135 EXPORT_SYMBOL(v4l2_ctrl_add_handler
);
1137 /* Cluster controls */
1138 void v4l2_ctrl_cluster(unsigned ncontrols
, struct v4l2_ctrl
**controls
)
1142 /* The first control is the master control and it must not be NULL */
1143 BUG_ON(controls
[0] == NULL
);
1145 for (i
= 0; i
< ncontrols
; i
++) {
1147 controls
[i
]->cluster
= controls
;
1148 controls
[i
]->ncontrols
= ncontrols
;
1152 EXPORT_SYMBOL(v4l2_ctrl_cluster
);
1154 /* Activate/deactivate a control. */
1155 void v4l2_ctrl_activate(struct v4l2_ctrl
*ctrl
, bool active
)
1161 /* set V4L2_CTRL_FLAG_INACTIVE */
1162 set_bit(4, &ctrl
->flags
);
1164 /* clear V4L2_CTRL_FLAG_INACTIVE */
1165 clear_bit(4, &ctrl
->flags
);
1167 EXPORT_SYMBOL(v4l2_ctrl_activate
);
1169 /* Grab/ungrab a control.
1170 Typically used when streaming starts and you want to grab controls,
1171 preventing the user from changing them.
1173 Just call this and the framework will block any attempts to change
1175 void v4l2_ctrl_grab(struct v4l2_ctrl
*ctrl
, bool grabbed
)
1181 /* set V4L2_CTRL_FLAG_GRABBED */
1182 set_bit(1, &ctrl
->flags
);
1184 /* clear V4L2_CTRL_FLAG_GRABBED */
1185 clear_bit(1, &ctrl
->flags
);
1187 EXPORT_SYMBOL(v4l2_ctrl_grab
);
1189 /* Log the control name and value */
1190 static void log_ctrl(const struct v4l2_ctrl
*ctrl
,
1191 const char *prefix
, const char *colon
)
1193 int fl_inact
= ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
;
1194 int fl_grabbed
= ctrl
->flags
& V4L2_CTRL_FLAG_GRABBED
;
1196 if (ctrl
->flags
& (V4L2_CTRL_FLAG_DISABLED
| V4L2_CTRL_FLAG_WRITE_ONLY
))
1198 if (ctrl
->type
== V4L2_CTRL_TYPE_CTRL_CLASS
)
1201 printk(KERN_INFO
"%s%s%s: ", prefix
, colon
, ctrl
->name
);
1203 switch (ctrl
->type
) {
1204 case V4L2_CTRL_TYPE_INTEGER
:
1205 printk(KERN_CONT
"%d", ctrl
->cur
.val
);
1207 case V4L2_CTRL_TYPE_BOOLEAN
:
1208 printk(KERN_CONT
"%s", ctrl
->cur
.val
? "true" : "false");
1210 case V4L2_CTRL_TYPE_MENU
:
1211 printk(KERN_CONT
"%s", ctrl
->qmenu
[ctrl
->cur
.val
]);
1213 case V4L2_CTRL_TYPE_INTEGER64
:
1214 printk(KERN_CONT
"%lld", ctrl
->cur
.val64
);
1216 case V4L2_CTRL_TYPE_STRING
:
1217 printk(KERN_CONT
"%s", ctrl
->cur
.string
);
1220 printk(KERN_CONT
"unknown type %d", ctrl
->type
);
1223 if (fl_inact
&& fl_grabbed
)
1224 printk(KERN_CONT
" (inactive, grabbed)\n");
1226 printk(KERN_CONT
" (inactive)\n");
1227 else if (fl_grabbed
)
1228 printk(KERN_CONT
" (grabbed)\n");
1230 printk(KERN_CONT
"\n");
1233 /* Log all controls owned by the handler */
1234 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler
*hdl
,
1237 struct v4l2_ctrl
*ctrl
;
1238 const char *colon
= "";
1245 len
= strlen(prefix
);
1246 if (len
&& prefix
[len
- 1] != ' ')
1248 mutex_lock(&hdl
->lock
);
1249 list_for_each_entry(ctrl
, &hdl
->ctrls
, node
)
1250 if (!(ctrl
->flags
& V4L2_CTRL_FLAG_DISABLED
))
1251 log_ctrl(ctrl
, prefix
, colon
);
1252 mutex_unlock(&hdl
->lock
);
1254 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status
);
1256 /* Call s_ctrl for all controls owned by the handler */
1257 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler
*hdl
)
1259 struct v4l2_ctrl
*ctrl
;
1264 mutex_lock(&hdl
->lock
);
1265 list_for_each_entry(ctrl
, &hdl
->ctrls
, node
)
1268 list_for_each_entry(ctrl
, &hdl
->ctrls
, node
) {
1269 struct v4l2_ctrl
*master
= ctrl
->cluster
[0];
1272 /* Skip if this control was already handled by a cluster. */
1276 for (i
= 0; i
< master
->ncontrols
; i
++)
1277 cur_to_new(master
->cluster
[i
]);
1279 /* Skip button controls and read-only controls. */
1280 if (ctrl
->type
== V4L2_CTRL_TYPE_BUTTON
||
1281 (ctrl
->flags
& V4L2_CTRL_FLAG_READ_ONLY
))
1283 ret
= master
->ops
->s_ctrl(master
);
1286 for (i
= 0; i
< master
->ncontrols
; i
++)
1287 if (master
->cluster
[i
])
1288 master
->cluster
[i
]->done
= true;
1290 mutex_unlock(&hdl
->lock
);
1293 EXPORT_SYMBOL(v4l2_ctrl_handler_setup
);
1295 /* Implement VIDIOC_QUERYCTRL */
1296 int v4l2_queryctrl(struct v4l2_ctrl_handler
*hdl
, struct v4l2_queryctrl
*qc
)
1298 u32 id
= qc
->id
& V4L2_CTRL_ID_MASK
;
1299 struct v4l2_ctrl_ref
*ref
;
1300 struct v4l2_ctrl
*ctrl
;
1305 mutex_lock(&hdl
->lock
);
1307 /* Try to find it */
1308 ref
= find_ref(hdl
, id
);
1310 if ((qc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) && !list_empty(&hdl
->ctrl_refs
)) {
1311 /* Find the next control with ID > qc->id */
1313 /* Did we reach the end of the control list? */
1314 if (id
>= node2id(hdl
->ctrl_refs
.prev
)) {
1315 ref
= NULL
; /* Yes, so there is no next control */
1317 /* We found a control with the given ID, so just get
1318 the next one in the list. */
1319 ref
= list_entry(ref
->node
.next
, typeof(*ref
), node
);
1321 /* No control with the given ID exists, so start
1322 searching for the next largest ID. We know there
1323 is one, otherwise the first 'if' above would have
1325 list_for_each_entry(ref
, &hdl
->ctrl_refs
, node
)
1326 if (id
< ref
->ctrl
->id
)
1330 mutex_unlock(&hdl
->lock
);
1335 memset(qc
, 0, sizeof(*qc
));
1337 strlcpy(qc
->name
, ctrl
->name
, sizeof(qc
->name
));
1338 qc
->minimum
= ctrl
->minimum
;
1339 qc
->maximum
= ctrl
->maximum
;
1340 qc
->default_value
= ctrl
->default_value
;
1341 if (qc
->type
== V4L2_CTRL_TYPE_MENU
)
1344 qc
->step
= ctrl
->step
;
1345 qc
->flags
= ctrl
->flags
;
1346 qc
->type
= ctrl
->type
;
1349 EXPORT_SYMBOL(v4l2_queryctrl
);
1351 int v4l2_subdev_queryctrl(struct v4l2_subdev
*sd
, struct v4l2_queryctrl
*qc
)
1353 return v4l2_queryctrl(sd
->ctrl_handler
, qc
);
1355 EXPORT_SYMBOL(v4l2_subdev_queryctrl
);
1357 /* Implement VIDIOC_QUERYMENU */
1358 int v4l2_querymenu(struct v4l2_ctrl_handler
*hdl
, struct v4l2_querymenu
*qm
)
1360 struct v4l2_ctrl
*ctrl
;
1363 ctrl
= v4l2_ctrl_find(hdl
, qm
->id
);
1369 if (ctrl
->qmenu
== NULL
||
1370 i
< ctrl
->minimum
|| i
> ctrl
->maximum
)
1372 /* Use mask to see if this menu item should be skipped */
1373 if (ctrl
->menu_skip_mask
& (1 << i
))
1375 /* Empty menu items should also be skipped */
1376 if (ctrl
->qmenu
[i
] == NULL
|| ctrl
->qmenu
[i
][0] == '\0')
1378 strlcpy(qm
->name
, ctrl
->qmenu
[i
], sizeof(qm
->name
));
1381 EXPORT_SYMBOL(v4l2_querymenu
);
1383 int v4l2_subdev_querymenu(struct v4l2_subdev
*sd
, struct v4l2_querymenu
*qm
)
1385 return v4l2_querymenu(sd
->ctrl_handler
, qm
);
1387 EXPORT_SYMBOL(v4l2_subdev_querymenu
);
1391 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1393 It is not a fully atomic operation, just best-effort only. After all, if
1394 multiple controls have to be set through multiple i2c writes (for example)
1395 then some initial writes may succeed while others fail. Thus leaving the
1396 system in an inconsistent state. The question is how much effort you are
1397 willing to spend on trying to make something atomic that really isn't.
1399 From the point of view of an application the main requirement is that
1400 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1401 error should be returned without actually affecting any controls.
1403 If all the values are correct, then it is acceptable to just give up
1404 in case of low-level errors.
1406 It is important though that the application can tell when only a partial
1407 configuration was done. The way we do that is through the error_idx field
1408 of struct v4l2_ext_controls: if that is equal to the count field then no
1409 controls were affected. Otherwise all controls before that index were
1410 successful in performing their 'get' or 'set' operation, the control at
1411 the given index failed, and you don't know what happened with the controls
1412 after the failed one. Since if they were part of a control cluster they
1413 could have been successfully processed (if a cluster member was encountered
1414 at index < error_idx), they could have failed (if a cluster member was at
1415 error_idx), or they may not have been processed yet (if the first cluster
1416 member appeared after error_idx).
1418 It is all fairly theoretical, though. In practice all you can do is to
1419 bail out. If error_idx == count, then it is an application bug. If
1420 error_idx < count then it is only an application bug if the error code was
1421 EBUSY. That usually means that something started streaming just when you
1422 tried to set the controls. In all other cases it is a driver/hardware
1423 problem and all you can do is to retry or bail out.
1425 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1426 never modifies controls the error_idx is just set to whatever control
1427 has an invalid value.
1430 /* Prepare for the extended g/s/try functions.
1431 Find the controls in the control array and do some basic checks. */
1432 static int prepare_ext_ctrls(struct v4l2_ctrl_handler
*hdl
,
1433 struct v4l2_ext_controls
*cs
,
1434 struct ctrl_helper
*helpers
,
1439 for (i
= 0; i
< cs
->count
; i
++) {
1440 struct v4l2_ext_control
*c
= &cs
->controls
[i
];
1441 struct v4l2_ctrl
*ctrl
;
1442 u32 id
= c
->id
& V4L2_CTRL_ID_MASK
;
1447 if (cs
->ctrl_class
&& V4L2_CTRL_ID2CLASS(id
) != cs
->ctrl_class
)
1450 /* Old-style private controls are not allowed for
1451 extended controls */
1452 if (id
>= V4L2_CID_PRIVATE_BASE
)
1454 ctrl
= v4l2_ctrl_find(hdl
, id
);
1457 if (ctrl
->flags
& V4L2_CTRL_FLAG_DISABLED
)
1460 helpers
[i
].ctrl
= ctrl
;
1461 helpers
[i
].handled
= false;
1466 typedef int (*cluster_func
)(struct v4l2_ext_control
*c
,
1467 struct v4l2_ctrl
*ctrl
);
1469 /* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1470 and call the provided function. */
1471 static int cluster_walk(unsigned from
,
1472 struct v4l2_ext_controls
*cs
,
1473 struct ctrl_helper
*helpers
,
1476 struct v4l2_ctrl
**cluster
= helpers
[from
].ctrl
->cluster
;
1480 /* Find any controls from the same cluster and call the function */
1481 for (i
= from
; !ret
&& i
< cs
->count
; i
++) {
1482 struct v4l2_ctrl
*ctrl
= helpers
[i
].ctrl
;
1484 if (!helpers
[i
].handled
&& ctrl
->cluster
== cluster
)
1485 ret
= f(&cs
->controls
[i
], ctrl
);
1490 static void cluster_done(unsigned from
,
1491 struct v4l2_ext_controls
*cs
,
1492 struct ctrl_helper
*helpers
)
1494 struct v4l2_ctrl
**cluster
= helpers
[from
].ctrl
->cluster
;
1497 /* Find any controls from the same cluster and mark them as handled */
1498 for (i
= from
; i
< cs
->count
; i
++)
1499 if (helpers
[i
].ctrl
->cluster
== cluster
)
1500 helpers
[i
].handled
= true;
1503 /* Handles the corner case where cs->count == 0. It checks whether the
1504 specified control class exists. If that class ID is 0, then it checks
1505 whether there are any controls at all. */
1506 static int class_check(struct v4l2_ctrl_handler
*hdl
, u32 ctrl_class
)
1508 if (ctrl_class
== 0)
1509 return list_empty(&hdl
->ctrl_refs
) ? -EINVAL
: 0;
1510 return find_ref_lock(hdl
, ctrl_class
| 1) ? 0 : -EINVAL
;
1515 /* Get extended controls. Allocates the helpers array if needed. */
1516 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler
*hdl
, struct v4l2_ext_controls
*cs
)
1518 struct ctrl_helper helper
[4];
1519 struct ctrl_helper
*helpers
= helper
;
1523 cs
->error_idx
= cs
->count
;
1524 cs
->ctrl_class
= V4L2_CTRL_ID2CLASS(cs
->ctrl_class
);
1530 return class_check(hdl
, cs
->ctrl_class
);
1532 if (cs
->count
> ARRAY_SIZE(helper
)) {
1533 helpers
= kmalloc(sizeof(helper
[0]) * cs
->count
, GFP_KERNEL
);
1534 if (helpers
== NULL
)
1538 ret
= prepare_ext_ctrls(hdl
, cs
, helpers
, false);
1540 for (i
= 0; !ret
&& i
< cs
->count
; i
++)
1541 if (helpers
[i
].ctrl
->flags
& V4L2_CTRL_FLAG_WRITE_ONLY
)
1544 for (i
= 0; !ret
&& i
< cs
->count
; i
++) {
1545 struct v4l2_ctrl
*ctrl
= helpers
[i
].ctrl
;
1546 struct v4l2_ctrl
*master
= ctrl
->cluster
[0];
1548 if (helpers
[i
].handled
)
1553 v4l2_ctrl_lock(master
);
1554 /* g_volatile_ctrl will update the current control values */
1555 if (ctrl
->is_volatile
&& master
->ops
->g_volatile_ctrl
)
1556 ret
= master
->ops
->g_volatile_ctrl(master
);
1557 /* If OK, then copy the current control values to the caller */
1559 ret
= cluster_walk(i
, cs
, helpers
, cur_to_user
);
1560 v4l2_ctrl_unlock(master
);
1561 cluster_done(i
, cs
, helpers
);
1564 if (cs
->count
> ARRAY_SIZE(helper
))
1568 EXPORT_SYMBOL(v4l2_g_ext_ctrls
);
1570 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev
*sd
, struct v4l2_ext_controls
*cs
)
1572 return v4l2_g_ext_ctrls(sd
->ctrl_handler
, cs
);
1574 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls
);
1576 /* Helper function to get a single control */
1577 static int get_ctrl(struct v4l2_ctrl
*ctrl
, s32
*val
)
1579 struct v4l2_ctrl
*master
= ctrl
->cluster
[0];
1582 if (ctrl
->flags
& V4L2_CTRL_FLAG_WRITE_ONLY
)
1585 v4l2_ctrl_lock(master
);
1586 /* g_volatile_ctrl will update the current control values */
1587 if (ctrl
->is_volatile
&& master
->ops
->g_volatile_ctrl
)
1588 ret
= master
->ops
->g_volatile_ctrl(master
);
1589 *val
= ctrl
->cur
.val
;
1590 v4l2_ctrl_unlock(master
);
1594 int v4l2_g_ctrl(struct v4l2_ctrl_handler
*hdl
, struct v4l2_control
*control
)
1596 struct v4l2_ctrl
*ctrl
= v4l2_ctrl_find(hdl
, control
->id
);
1598 if (ctrl
== NULL
|| !type_is_int(ctrl
))
1600 return get_ctrl(ctrl
, &control
->value
);
1602 EXPORT_SYMBOL(v4l2_g_ctrl
);
1604 int v4l2_subdev_g_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*control
)
1606 return v4l2_g_ctrl(sd
->ctrl_handler
, control
);
1608 EXPORT_SYMBOL(v4l2_subdev_g_ctrl
);
1610 s32
v4l2_ctrl_g_ctrl(struct v4l2_ctrl
*ctrl
)
1614 /* It's a driver bug if this happens. */
1615 WARN_ON(!type_is_int(ctrl
));
1616 get_ctrl(ctrl
, &val
);
1619 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl
);
1622 /* Core function that calls try/s_ctrl and ensures that the new value is
1623 copied to the current value on a set.
1624 Must be called with ctrl->handler->lock held. */
1625 static int try_or_set_control_cluster(struct v4l2_ctrl
*master
, bool set
)
1631 /* Go through the cluster and either validate the new value or
1632 (if no new value was set), copy the current value to the new
1633 value, ensuring a consistent view for the control ops when
1635 for (i
= 0; !ret
&& i
< master
->ncontrols
; i
++) {
1636 struct v4l2_ctrl
*ctrl
= master
->cluster
[i
];
1641 if (ctrl
->has_new
) {
1642 /* Double check this: it may have changed since the
1643 last check in try_or_set_ext_ctrls(). */
1644 if (set
&& (ctrl
->flags
& V4L2_CTRL_FLAG_GRABBED
))
1647 /* Validate if required */
1649 ret
= validate_new(ctrl
);
1652 /* No new value was set, so copy the current and force
1653 a call to try_ctrl later, since the values for the cluster
1654 may now have changed and the end result might be invalid. */
1659 /* For larger clusters you have to call try_ctrl again to
1660 verify that the controls are still valid after the
1661 'cur_to_new' above. */
1662 if (!ret
&& master
->ops
->try_ctrl
&& try)
1663 ret
= master
->ops
->try_ctrl(master
);
1665 /* Don't set if there is no change */
1666 if (!ret
&& set
&& cluster_changed(master
)) {
1667 ret
= master
->ops
->s_ctrl(master
);
1668 /* If OK, then make the new values permanent. */
1670 for (i
= 0; i
< master
->ncontrols
; i
++)
1671 new_to_cur(master
->cluster
[i
]);
1676 /* Try or set controls. */
1677 static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler
*hdl
,
1678 struct v4l2_ext_controls
*cs
,
1679 struct ctrl_helper
*helpers
,
1685 cs
->error_idx
= cs
->count
;
1686 for (i
= 0; i
< cs
->count
; i
++) {
1687 struct v4l2_ctrl
*ctrl
= helpers
[i
].ctrl
;
1692 if (ctrl
->flags
& V4L2_CTRL_FLAG_READ_ONLY
)
1694 /* This test is also done in try_set_control_cluster() which
1695 is called in atomic context, so that has the final say,
1696 but it makes sense to do an up-front check as well. Once
1697 an error occurs in try_set_control_cluster() some other
1698 controls may have been set already and we want to do a
1699 best-effort to avoid that. */
1700 if (set
&& (ctrl
->flags
& V4L2_CTRL_FLAG_GRABBED
))
1704 for (i
= 0; !ret
&& i
< cs
->count
; i
++) {
1705 struct v4l2_ctrl
*ctrl
= helpers
[i
].ctrl
;
1706 struct v4l2_ctrl
*master
= ctrl
->cluster
[0];
1710 if (helpers
[i
].handled
)
1713 v4l2_ctrl_lock(ctrl
);
1715 /* Reset the 'has_new' flags of the cluster */
1716 for (j
= 0; j
< master
->ncontrols
; j
++)
1717 if (master
->cluster
[j
])
1718 master
->cluster
[j
]->has_new
= 0;
1720 /* Copy the new caller-supplied control values.
1721 user_to_new() sets 'has_new' to 1. */
1722 ret
= cluster_walk(i
, cs
, helpers
, user_to_new
);
1725 ret
= try_or_set_control_cluster(master
, set
);
1727 /* Copy the new values back to userspace. */
1729 ret
= cluster_walk(i
, cs
, helpers
, new_to_user
);
1731 v4l2_ctrl_unlock(ctrl
);
1732 cluster_done(i
, cs
, helpers
);
1737 /* Try or try-and-set controls */
1738 static int try_set_ext_ctrls(struct v4l2_ctrl_handler
*hdl
,
1739 struct v4l2_ext_controls
*cs
,
1742 struct ctrl_helper helper
[4];
1743 struct ctrl_helper
*helpers
= helper
;
1747 cs
->error_idx
= cs
->count
;
1748 cs
->ctrl_class
= V4L2_CTRL_ID2CLASS(cs
->ctrl_class
);
1754 return class_check(hdl
, cs
->ctrl_class
);
1756 if (cs
->count
> ARRAY_SIZE(helper
)) {
1757 helpers
= kmalloc(sizeof(helper
[0]) * cs
->count
, GFP_KERNEL
);
1761 ret
= prepare_ext_ctrls(hdl
, cs
, helpers
, !set
);
1765 /* First 'try' all controls and abort on error */
1766 ret
= try_or_set_ext_ctrls(hdl
, cs
, helpers
, false);
1767 /* If this is a 'set' operation and the initial 'try' failed,
1768 then set error_idx to count to tell the application that no
1769 controls changed value yet. */
1771 cs
->error_idx
= cs
->count
;
1773 /* Reset 'handled' state */
1774 for (i
= 0; i
< cs
->count
; i
++)
1775 helpers
[i
].handled
= false;
1776 ret
= try_or_set_ext_ctrls(hdl
, cs
, helpers
, true);
1780 if (cs
->count
> ARRAY_SIZE(helper
))
1785 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler
*hdl
, struct v4l2_ext_controls
*cs
)
1787 return try_set_ext_ctrls(hdl
, cs
, false);
1789 EXPORT_SYMBOL(v4l2_try_ext_ctrls
);
1791 int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler
*hdl
, struct v4l2_ext_controls
*cs
)
1793 return try_set_ext_ctrls(hdl
, cs
, true);
1795 EXPORT_SYMBOL(v4l2_s_ext_ctrls
);
1797 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev
*sd
, struct v4l2_ext_controls
*cs
)
1799 return try_set_ext_ctrls(sd
->ctrl_handler
, cs
, false);
1801 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls
);
1803 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev
*sd
, struct v4l2_ext_controls
*cs
)
1805 return try_set_ext_ctrls(sd
->ctrl_handler
, cs
, true);
1807 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls
);
1809 /* Helper function for VIDIOC_S_CTRL compatibility */
1810 static int set_ctrl(struct v4l2_ctrl
*ctrl
, s32
*val
)
1812 struct v4l2_ctrl
*master
= ctrl
->cluster
[0];
1816 v4l2_ctrl_lock(ctrl
);
1818 /* Reset the 'has_new' flags of the cluster */
1819 for (i
= 0; i
< master
->ncontrols
; i
++)
1820 if (master
->cluster
[i
])
1821 master
->cluster
[i
]->has_new
= 0;
1825 ret
= try_or_set_control_cluster(master
, false);
1827 ret
= try_or_set_control_cluster(master
, true);
1828 *val
= ctrl
->cur
.val
;
1829 v4l2_ctrl_unlock(ctrl
);
1833 int v4l2_s_ctrl(struct v4l2_ctrl_handler
*hdl
, struct v4l2_control
*control
)
1835 struct v4l2_ctrl
*ctrl
= v4l2_ctrl_find(hdl
, control
->id
);
1837 if (ctrl
== NULL
|| !type_is_int(ctrl
))
1840 return set_ctrl(ctrl
, &control
->value
);
1842 EXPORT_SYMBOL(v4l2_s_ctrl
);
1844 int v4l2_subdev_s_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*control
)
1846 return v4l2_s_ctrl(sd
->ctrl_handler
, control
);
1848 EXPORT_SYMBOL(v4l2_subdev_s_ctrl
);
1850 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl
*ctrl
, s32 val
)
1852 /* It's a driver bug if this happens. */
1853 WARN_ON(!type_is_int(ctrl
));
1854 return set_ctrl(ctrl
, &val
);
1856 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl
);