blob: 55dd813b734102a6cad6164fbcfd32dc09bdb421 [file] [log] [blame]
Hans Verkuil09965172010-08-01 14:32:42 -03001/*
2 V4L2 controls framework implementation.
3
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
5
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.
10
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.
15
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
19 */
20
21#include <linux/ctype.h>
Randy Dunlap2b801632010-08-09 14:56:35 -030022#include <linux/slab.h>
Paul Gortmaker35a24632011-08-01 15:26:38 -040023#include <linux/export.h>
Hans Verkuil09965172010-08-01 14:32:42 -030024#include <media/v4l2-ioctl.h>
25#include <media/v4l2-device.h>
26#include <media/v4l2-ctrls.h>
Hans Verkuil6e239392011-06-07 11:13:44 -030027#include <media/v4l2-event.h>
Hans Verkuil09965172010-08-01 14:32:42 -030028#include <media/v4l2-dev.h>
29
Hans Verkuilddac5c12011-06-10 05:43:34 -030030#define has_op(master, op) \
31 (master->ops && master->ops->op)
Hans Verkuil54c911e2011-05-25 06:04:58 -030032#define call_op(master, op) \
Hans Verkuilddac5c12011-06-10 05:43:34 -030033 (has_op(master, op) ? master->ops->op(master) : 0)
Hans Verkuil54c911e2011-05-25 06:04:58 -030034
Hans Verkuil09965172010-08-01 14:32:42 -030035/* Internal temporary helper struct, one for each v4l2_ext_control */
Hans Verkuileb5b16e2011-06-14 10:04:06 -030036struct v4l2_ctrl_helper {
37 /* Pointer to the control reference of the master control */
38 struct v4l2_ctrl_ref *mref;
Hans Verkuil09965172010-08-01 14:32:42 -030039 /* The control corresponding to the v4l2_ext_control ID field. */
40 struct v4l2_ctrl *ctrl;
Hans Verkuileb5b16e2011-06-14 10:04:06 -030041 /* v4l2_ext_control index of the next control belonging to the
42 same cluster, or 0 if there isn't any. */
43 u32 next;
Hans Verkuil09965172010-08-01 14:32:42 -030044};
45
Hans Verkuil72d877c2011-06-10 05:44:36 -030046/* Small helper function to determine if the autocluster is set to manual
Hans Verkuil88365102011-08-26 07:35:14 -030047 mode. */
Hans Verkuil72d877c2011-06-10 05:44:36 -030048static bool is_cur_manual(const struct v4l2_ctrl *master)
49{
50 return master->is_auto && master->cur.val == master->manual_mode_value;
51}
52
53/* Same as above, but this checks the against the new value instead of the
54 current value. */
55static bool is_new_manual(const struct v4l2_ctrl *master)
56{
57 return master->is_auto && master->val == master->manual_mode_value;
58}
59
Hans Verkuil09965172010-08-01 14:32:42 -030060/* Returns NULL or a character pointer array containing the menu for
61 the given control ID. The pointer array ends with a NULL pointer.
62 An empty string signifies a menu entry that is invalid. This allows
63 drivers to disable certain options if it is not supported. */
Hans Verkuil513521e2010-12-29 14:25:52 -030064const char * const *v4l2_ctrl_get_menu(u32 id)
Hans Verkuil09965172010-08-01 14:32:42 -030065{
Hans Verkuil513521e2010-12-29 14:25:52 -030066 static const char * const mpeg_audio_sampling_freq[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030067 "44.1 kHz",
68 "48 kHz",
69 "32 kHz",
70 NULL
71 };
Hans Verkuil513521e2010-12-29 14:25:52 -030072 static const char * const mpeg_audio_encoding[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030073 "MPEG-1/2 Layer I",
74 "MPEG-1/2 Layer II",
75 "MPEG-1/2 Layer III",
76 "MPEG-2/4 AAC",
77 "AC-3",
78 NULL
79 };
Hans Verkuil513521e2010-12-29 14:25:52 -030080 static const char * const mpeg_audio_l1_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030081 "32 kbps",
82 "64 kbps",
83 "96 kbps",
84 "128 kbps",
85 "160 kbps",
86 "192 kbps",
87 "224 kbps",
88 "256 kbps",
89 "288 kbps",
90 "320 kbps",
91 "352 kbps",
92 "384 kbps",
93 "416 kbps",
94 "448 kbps",
95 NULL
96 };
Hans Verkuil513521e2010-12-29 14:25:52 -030097 static const char * const mpeg_audio_l2_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030098 "32 kbps",
99 "48 kbps",
100 "56 kbps",
101 "64 kbps",
102 "80 kbps",
103 "96 kbps",
104 "112 kbps",
105 "128 kbps",
106 "160 kbps",
107 "192 kbps",
108 "224 kbps",
109 "256 kbps",
110 "320 kbps",
111 "384 kbps",
112 NULL
113 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300114 static const char * const mpeg_audio_l3_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300115 "32 kbps",
116 "40 kbps",
117 "48 kbps",
118 "56 kbps",
119 "64 kbps",
120 "80 kbps",
121 "96 kbps",
122 "112 kbps",
123 "128 kbps",
124 "160 kbps",
125 "192 kbps",
126 "224 kbps",
127 "256 kbps",
128 "320 kbps",
129 NULL
130 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300131 static const char * const mpeg_audio_ac3_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300132 "32 kbps",
133 "40 kbps",
134 "48 kbps",
135 "56 kbps",
136 "64 kbps",
137 "80 kbps",
138 "96 kbps",
139 "112 kbps",
140 "128 kbps",
141 "160 kbps",
142 "192 kbps",
143 "224 kbps",
144 "256 kbps",
145 "320 kbps",
146 "384 kbps",
147 "448 kbps",
148 "512 kbps",
149 "576 kbps",
150 "640 kbps",
151 NULL
152 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300153 static const char * const mpeg_audio_mode[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300154 "Stereo",
155 "Joint Stereo",
156 "Dual",
157 "Mono",
158 NULL
159 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300160 static const char * const mpeg_audio_mode_extension[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300161 "Bound 4",
162 "Bound 8",
163 "Bound 12",
164 "Bound 16",
165 NULL
166 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300167 static const char * const mpeg_audio_emphasis[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300168 "No Emphasis",
169 "50/15 us",
170 "CCITT J17",
171 NULL
172 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300173 static const char * const mpeg_audio_crc[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300174 "No CRC",
175 "16-bit CRC",
176 NULL
177 };
Hans Verkuil24c19a22011-12-15 10:46:16 -0300178 static const char * const mpeg_audio_dec_playback[] = {
179 "Auto",
180 "Stereo",
181 "Left",
182 "Right",
183 "Mono",
184 "Swapped Stereo",
185 NULL
186 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300187 static const char * const mpeg_video_encoding[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300188 "MPEG-1",
189 "MPEG-2",
190 "MPEG-4 AVC",
191 NULL
192 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300193 static const char * const mpeg_video_aspect[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300194 "1x1",
195 "4x3",
196 "16x9",
197 "2.21x1",
198 NULL
199 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300200 static const char * const mpeg_video_bitrate_mode[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300201 "Variable Bitrate",
202 "Constant Bitrate",
203 NULL
204 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300205 static const char * const mpeg_stream_type[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300206 "MPEG-2 Program Stream",
207 "MPEG-2 Transport Stream",
208 "MPEG-1 System Stream",
209 "MPEG-2 DVD-compatible Stream",
210 "MPEG-1 VCD-compatible Stream",
211 "MPEG-2 SVCD-compatible Stream",
212 NULL
213 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300214 static const char * const mpeg_stream_vbi_fmt[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300215 "No VBI",
Kamil Debski064f5092011-06-14 10:46:22 -0300216 "Private Packet, IVTV Format",
Hans Verkuil09965172010-08-01 14:32:42 -0300217 NULL
218 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300219 static const char * const camera_power_line_frequency[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300220 "Disabled",
221 "50 Hz",
222 "60 Hz",
Sylwester Nawrockid26a6632011-09-04 19:08:54 -0300223 "Auto",
Hans Verkuil09965172010-08-01 14:32:42 -0300224 NULL
225 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300226 static const char * const camera_exposure_auto[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300227 "Auto Mode",
228 "Manual Mode",
229 "Shutter Priority Mode",
230 "Aperture Priority Mode",
231 NULL
232 };
Sylwester Nawrockicf072132012-04-30 04:34:10 -0300233 static const char * const camera_exposure_metering[] = {
234 "Average",
235 "Center Weighted",
236 "Spot",
237 NULL
238 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300239 static const char * const colorfx[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300240 "None",
241 "Black & White",
242 "Sepia",
243 "Negative",
244 "Emboss",
245 "Sketch",
Kamil Debski064f5092011-06-14 10:46:22 -0300246 "Sky Blue",
247 "Grass Green",
248 "Skin Whiten",
Hans Verkuil09965172010-08-01 14:32:42 -0300249 "Vivid",
Sylwester Nawrocki6491d1a2012-04-02 06:40:19 -0300250 "Aqua",
251 "Art Freeze",
252 "Silhouette",
253 "Solarization",
254 "Antique",
255 "Set Cb/Cr",
Hans Verkuil09965172010-08-01 14:32:42 -0300256 NULL
257 };
Sylwester Nawrockie40a0572012-03-06 07:04:26 -0300258 static const char * const auto_n_preset_white_balance[] = {
259 "Manual",
260 "Auto",
261 "Incandescent",
262 "Fluorescent",
263 "Fluorescent H",
264 "Horizon",
265 "Daylight",
266 "Flash",
267 "Cloudy",
268 "Shade",
269 NULL,
270 };
Sylwester Nawrocki7f84ad82012-05-01 17:39:45 -0300271 static const char * const camera_iso_sensitivity_auto[] = {
272 "Manual",
273 "Auto",
274 NULL
275 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300276 static const char * const tune_preemphasis[] = {
Kamil Debski064f5092011-06-14 10:46:22 -0300277 "No Preemphasis",
Hans Verkuilf769c262012-03-02 04:20:19 -0300278 "50 Microseconds",
279 "75 Microseconds",
Hans Verkuil09965172010-08-01 14:32:42 -0300280 NULL,
281 };
Kamil Debski064f5092011-06-14 10:46:22 -0300282 static const char * const header_mode[] = {
283 "Separate Buffer",
284 "Joined With 1st Frame",
285 NULL,
286 };
287 static const char * const multi_slice[] = {
288 "Single",
289 "Max Macroblocks",
290 "Max Bytes",
291 NULL,
292 };
293 static const char * const entropy_mode[] = {
294 "CAVLC",
295 "CABAC",
296 NULL,
297 };
298 static const char * const mpeg_h264_level[] = {
299 "1",
300 "1b",
301 "1.1",
302 "1.2",
303 "1.3",
304 "2",
305 "2.1",
306 "2.2",
307 "3",
308 "3.1",
309 "3.2",
310 "4",
311 "4.1",
312 "4.2",
313 "5",
314 "5.1",
315 NULL,
316 };
317 static const char * const h264_loop_filter[] = {
318 "Enabled",
319 "Disabled",
320 "Disabled at Slice Boundary",
321 NULL,
322 };
323 static const char * const h264_profile[] = {
324 "Baseline",
325 "Constrained Baseline",
326 "Main",
327 "Extended",
328 "High",
329 "High 10",
330 "High 422",
331 "High 444 Predictive",
332 "High 10 Intra",
333 "High 422 Intra",
334 "High 444 Intra",
335 "CAVLC 444 Intra",
336 "Scalable Baseline",
337 "Scalable High",
338 "Scalable High Intra",
339 "Multiview High",
340 NULL,
341 };
342 static const char * const vui_sar_idc[] = {
343 "Unspecified",
344 "1:1",
345 "12:11",
346 "10:11",
347 "16:11",
348 "40:33",
349 "24:11",
350 "20:11",
351 "32:11",
352 "80:33",
353 "18:11",
354 "15:11",
355 "64:33",
356 "160:99",
357 "4:3",
358 "3:2",
359 "2:1",
360 "Extended SAR",
361 NULL,
362 };
363 static const char * const mpeg_mpeg4_level[] = {
364 "0",
365 "0b",
366 "1",
367 "2",
368 "3",
369 "3b",
370 "4",
371 "5",
372 NULL,
373 };
374 static const char * const mpeg4_profile[] = {
375 "Simple",
Hans Verkuilf769c262012-03-02 04:20:19 -0300376 "Advanced Simple",
Kamil Debski064f5092011-06-14 10:46:22 -0300377 "Core",
378 "Simple Scalable",
379 "Advanced Coding Efficency",
380 NULL,
381 };
382
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300383 static const char * const flash_led_mode[] = {
384 "Off",
385 "Flash",
386 "Torch",
387 NULL,
388 };
389 static const char * const flash_strobe_source[] = {
390 "Software",
391 "External",
392 NULL,
393 };
Hans Verkuil09965172010-08-01 14:32:42 -0300394
Sylwester Nawrockic7361ae2012-01-20 15:37:44 -0300395 static const char * const jpeg_chroma_subsampling[] = {
396 "4:4:4",
397 "4:2:2",
398 "4:2:0",
399 "4:1:1",
400 "4:1:0",
401 "Gray",
402 NULL,
403 };
404
Hans Verkuil09965172010-08-01 14:32:42 -0300405 switch (id) {
406 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
407 return mpeg_audio_sampling_freq;
408 case V4L2_CID_MPEG_AUDIO_ENCODING:
409 return mpeg_audio_encoding;
410 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
411 return mpeg_audio_l1_bitrate;
412 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
413 return mpeg_audio_l2_bitrate;
414 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
415 return mpeg_audio_l3_bitrate;
416 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
417 return mpeg_audio_ac3_bitrate;
418 case V4L2_CID_MPEG_AUDIO_MODE:
419 return mpeg_audio_mode;
420 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
421 return mpeg_audio_mode_extension;
422 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
423 return mpeg_audio_emphasis;
424 case V4L2_CID_MPEG_AUDIO_CRC:
425 return mpeg_audio_crc;
Hans Verkuil24c19a22011-12-15 10:46:16 -0300426 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
427 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
428 return mpeg_audio_dec_playback;
Hans Verkuil09965172010-08-01 14:32:42 -0300429 case V4L2_CID_MPEG_VIDEO_ENCODING:
430 return mpeg_video_encoding;
431 case V4L2_CID_MPEG_VIDEO_ASPECT:
432 return mpeg_video_aspect;
433 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
434 return mpeg_video_bitrate_mode;
435 case V4L2_CID_MPEG_STREAM_TYPE:
436 return mpeg_stream_type;
437 case V4L2_CID_MPEG_STREAM_VBI_FMT:
438 return mpeg_stream_vbi_fmt;
439 case V4L2_CID_POWER_LINE_FREQUENCY:
440 return camera_power_line_frequency;
441 case V4L2_CID_EXPOSURE_AUTO:
442 return camera_exposure_auto;
Sylwester Nawrockicf072132012-04-30 04:34:10 -0300443 case V4L2_CID_EXPOSURE_METERING:
444 return camera_exposure_metering;
Hans Verkuil09965172010-08-01 14:32:42 -0300445 case V4L2_CID_COLORFX:
446 return colorfx;
Sylwester Nawrockie40a0572012-03-06 07:04:26 -0300447 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
448 return auto_n_preset_white_balance;
Sylwester Nawrocki7f84ad82012-05-01 17:39:45 -0300449 case V4L2_CID_ISO_SENSITIVITY_AUTO:
450 return camera_iso_sensitivity_auto;
Hans Verkuil09965172010-08-01 14:32:42 -0300451 case V4L2_CID_TUNE_PREEMPHASIS:
452 return tune_preemphasis;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300453 case V4L2_CID_FLASH_LED_MODE:
454 return flash_led_mode;
455 case V4L2_CID_FLASH_STROBE_SOURCE:
456 return flash_strobe_source;
Kamil Debski064f5092011-06-14 10:46:22 -0300457 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
458 return header_mode;
459 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
460 return multi_slice;
461 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
462 return entropy_mode;
463 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
464 return mpeg_h264_level;
465 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
466 return h264_loop_filter;
467 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
468 return h264_profile;
469 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
470 return vui_sar_idc;
471 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
472 return mpeg_mpeg4_level;
473 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
474 return mpeg4_profile;
Sylwester Nawrockic7361ae2012-01-20 15:37:44 -0300475 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
476 return jpeg_chroma_subsampling;
477
Hans Verkuil09965172010-08-01 14:32:42 -0300478 default:
479 return NULL;
480 }
481}
482EXPORT_SYMBOL(v4l2_ctrl_get_menu);
483
484/* Return the control name. */
485const char *v4l2_ctrl_get_name(u32 id)
486{
487 switch (id) {
488 /* USER controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300489 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300490 case V4L2_CID_USER_CLASS: return "User Controls";
491 case V4L2_CID_BRIGHTNESS: return "Brightness";
492 case V4L2_CID_CONTRAST: return "Contrast";
493 case V4L2_CID_SATURATION: return "Saturation";
494 case V4L2_CID_HUE: return "Hue";
495 case V4L2_CID_AUDIO_VOLUME: return "Volume";
496 case V4L2_CID_AUDIO_BALANCE: return "Balance";
497 case V4L2_CID_AUDIO_BASS: return "Bass";
498 case V4L2_CID_AUDIO_TREBLE: return "Treble";
499 case V4L2_CID_AUDIO_MUTE: return "Mute";
500 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
Hans Verkuil09965172010-08-01 14:32:42 -0300501 case V4L2_CID_BLACK_LEVEL: return "Black Level";
502 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
503 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
504 case V4L2_CID_RED_BALANCE: return "Red Balance";
505 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
506 case V4L2_CID_GAMMA: return "Gamma";
507 case V4L2_CID_EXPOSURE: return "Exposure";
508 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
509 case V4L2_CID_GAIN: return "Gain";
510 case V4L2_CID_HFLIP: return "Horizontal Flip";
511 case V4L2_CID_VFLIP: return "Vertical Flip";
512 case V4L2_CID_HCENTER: return "Horizontal Center";
513 case V4L2_CID_VCENTER: return "Vertical Center";
514 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
515 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
516 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
517 case V4L2_CID_SHARPNESS: return "Sharpness";
518 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
519 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
Hans Verkuil09965172010-08-01 14:32:42 -0300520 case V4L2_CID_COLOR_KILLER: return "Color Killer";
521 case V4L2_CID_COLORFX: return "Color Effects";
522 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
523 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
524 case V4L2_CID_ROTATE: return "Rotate";
525 case V4L2_CID_BG_COLOR: return "Background Color";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300526 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
Jean-François Moine008d35f2010-09-13 07:04:49 -0300527 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
528 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300529 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: return "Min Number of Capture Buffers";
530 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: return "Min Number of Output Buffers";
Sylwester Nawrockicc1d3272011-11-14 08:48:18 -0300531 case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component";
Sylwester Nawrocki6491d1a2012-04-02 06:40:19 -0300532 case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr";
Hans Verkuil09965172010-08-01 14:32:42 -0300533
534 /* MPEG controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300535 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300536 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
537 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
538 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
539 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
540 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
541 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300542 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
543 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
544 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
Hans Verkuil09965172010-08-01 14:32:42 -0300545 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300546 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
547 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
548 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
549 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
550 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
Hans Verkuil09965172010-08-01 14:32:42 -0300551 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300552 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
553 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
554 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
555 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
556 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
Hans Verkuil24c19a22011-12-15 10:46:16 -0300557 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK: return "Audio Playback";
558 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300559 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
560 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
561 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
562 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
563 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
564 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
565 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
566 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
567 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
Hans Verkuil09965172010-08-01 14:32:42 -0300568 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300569 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
Hans Verkuil09965172010-08-01 14:32:42 -0300570 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
Kamil Debski064f5092011-06-14 10:46:22 -0300571 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE: return "Decoder Slice Interface";
572 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER: return "MPEG4 Loop Filter Enable";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300573 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: return "Number of Intra Refresh MBs";
Kamil Debski064f5092011-06-14 10:46:22 -0300574 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: return "Frame Level Rate Control Enable";
575 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE: return "H264 MB Level Rate Control";
576 case V4L2_CID_MPEG_VIDEO_HEADER_MODE: return "Sequence Header Mode";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300577 case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC: return "Max Number of Reference Pics";
Kamil Debski064f5092011-06-14 10:46:22 -0300578 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP: return "H263 I-Frame QP Value";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300579 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP: return "H263 P-Frame QP Value";
580 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP: return "H263 B-Frame QP Value";
Kamil Debski064f5092011-06-14 10:46:22 -0300581 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP: return "H263 Minimum QP Value";
582 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP: return "H263 Maximum QP Value";
583 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: return "H264 I-Frame QP Value";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300584 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: return "H264 P-Frame QP Value";
585 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP: return "H264 B-Frame QP Value";
Kamil Debski064f5092011-06-14 10:46:22 -0300586 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: return "H264 Maximum QP Value";
587 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: return "H264 Minimum QP Value";
588 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM: return "H264 8x8 Transform Enable";
589 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE: return "H264 CPB Buffer Size";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300590 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: return "H264 Entropy Mode";
591 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD: return "H264 I-Frame Period";
Kamil Debski064f5092011-06-14 10:46:22 -0300592 case V4L2_CID_MPEG_VIDEO_H264_LEVEL: return "H264 Level";
593 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA: return "H264 Loop Filter Alpha Offset";
594 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA: return "H264 Loop Filter Beta Offset";
595 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: return "H264 Loop Filter Mode";
596 case V4L2_CID_MPEG_VIDEO_H264_PROFILE: return "H264 Profile";
597 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT: return "Vertical Size of SAR";
598 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH: return "Horizontal Size of SAR";
599 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE: return "Aspect Ratio VUI Enable";
600 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC: return "VUI Aspect Ratio IDC";
601 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: return "MPEG4 I-Frame QP Value";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300602 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: return "MPEG4 P-Frame QP Value";
603 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP: return "MPEG4 B-Frame QP Value";
Kamil Debski064f5092011-06-14 10:46:22 -0300604 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP: return "MPEG4 Minimum QP Value";
605 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP: return "MPEG4 Maximum QP Value";
606 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL: return "MPEG4 Level";
607 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE: return "MPEG4 Profile";
608 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL: return "Quarter Pixel Search Enable";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300609 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: return "Maximum Bytes in a Slice";
610 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: return "Number of MBs in a Slice";
611 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: return "Slice Partitioning Method";
Kamil Debski064f5092011-06-14 10:46:22 -0300612 case V4L2_CID_MPEG_VIDEO_VBV_SIZE: return "VBV Buffer Size";
Hans Verkuil24c19a22011-12-15 10:46:16 -0300613 case V4L2_CID_MPEG_VIDEO_DEC_PTS: return "Video Decoder PTS";
614 case V4L2_CID_MPEG_VIDEO_DEC_FRAME: return "Video Decoder Frame Count";
Hans Verkuil09965172010-08-01 14:32:42 -0300615
616 /* CAMERA controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300617 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300618 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
619 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
620 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
621 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
622 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
623 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
624 case V4L2_CID_PAN_RESET: return "Pan, Reset";
625 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
626 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
627 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
628 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
629 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
630 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
Hans Verkuil09965172010-08-01 14:32:42 -0300631 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
632 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
633 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
634 case V4L2_CID_PRIVACY: return "Privacy";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300635 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
636 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
Sylwester Nawrockid58083c2012-03-06 07:06:55 -0300637 case V4L2_CID_AUTO_EXPOSURE_BIAS: return "Auto Exposure, Bias";
Sylwester Nawrockie40a0572012-03-06 07:04:26 -0300638 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
Sylwester Nawrocki44d44a12012-03-06 07:05:45 -0300639 case V4L2_CID_WIDE_DYNAMIC_RANGE: return "Wide Dynamic Range";
Sylwester Nawrocki82b30562012-05-01 17:38:09 -0300640 case V4L2_CID_IMAGE_STABILIZATION: return "Image Stabilization";
Sylwester Nawrocki7f84ad82012-05-01 17:39:45 -0300641 case V4L2_CID_ISO_SENSITIVITY: return "ISO Sensitivity";
642 case V4L2_CID_ISO_SENSITIVITY_AUTO: return "ISO Sensitivity, Auto";
Sylwester Nawrockicf072132012-04-30 04:34:10 -0300643 case V4L2_CID_EXPOSURE_METERING: return "Exposure, Metering Mode";
Hans Verkuil09965172010-08-01 14:32:42 -0300644
645 /* FM Radio Modulator control */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300646 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300647 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
648 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
649 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
650 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
651 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
652 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
653 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
654 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
655 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300656 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
Hans Verkuil09965172010-08-01 14:32:42 -0300657 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
658 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
659 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
660 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
661 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
662 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
663 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300664 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-Emphasis";
Hans Verkuil09965172010-08-01 14:32:42 -0300665 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
666 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
667
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300668 /* Flash controls */
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300669 case V4L2_CID_FLASH_CLASS: return "Flash Controls";
670 case V4L2_CID_FLASH_LED_MODE: return "LED Mode";
671 case V4L2_CID_FLASH_STROBE_SOURCE: return "Strobe Source";
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300672 case V4L2_CID_FLASH_STROBE: return "Strobe";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300673 case V4L2_CID_FLASH_STROBE_STOP: return "Stop Strobe";
674 case V4L2_CID_FLASH_STROBE_STATUS: return "Strobe Status";
675 case V4L2_CID_FLASH_TIMEOUT: return "Strobe Timeout";
676 case V4L2_CID_FLASH_INTENSITY: return "Intensity, Flash Mode";
677 case V4L2_CID_FLASH_TORCH_INTENSITY: return "Intensity, Torch Mode";
678 case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300679 case V4L2_CID_FLASH_FAULT: return "Faults";
680 case V4L2_CID_FLASH_CHARGE: return "Charge";
Hans Verkuilf08aacf2012-01-16 12:27:15 -0300681 case V4L2_CID_FLASH_READY: return "Ready to Strobe";
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300682
Sylwester Nawrockic7361ae2012-01-20 15:37:44 -0300683 /* JPEG encoder controls */
684 /* Keep the order of the 'case's the same as in videodev2.h! */
685 case V4L2_CID_JPEG_CLASS: return "JPEG Compression Controls";
686 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING: return "Chroma Subsampling";
687 case V4L2_CID_JPEG_RESTART_INTERVAL: return "Restart Interval";
688 case V4L2_CID_JPEG_COMPRESSION_QUALITY: return "Compression Quality";
689 case V4L2_CID_JPEG_ACTIVE_MARKER: return "Active Markers";
690
Sakari Ailus8c9d2362011-10-04 08:20:05 -0300691 /* Image source controls */
692 case V4L2_CID_IMAGE_SOURCE_CLASS: return "Image Source Controls";
693 case V4L2_CID_VBLANK: return "Vertical Blanking";
694 case V4L2_CID_HBLANK: return "Horizontal Blanking";
695 case V4L2_CID_ANALOGUE_GAIN: return "Analogue Gain";
696
Sakari Ailusc643ee12012-02-02 20:17:54 -0300697 /* Image processing controls */
698 case V4L2_CID_IMAGE_PROC_CLASS: return "Image Processing Controls";
699 case V4L2_CID_LINK_FREQ: return "Link Frequency";
700 case V4L2_CID_PIXEL_RATE: return "Pixel Rate";
701
Hans Verkuil09965172010-08-01 14:32:42 -0300702 default:
703 return NULL;
704 }
705}
706EXPORT_SYMBOL(v4l2_ctrl_get_name);
707
708void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
709 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
710{
711 *name = v4l2_ctrl_get_name(id);
712 *flags = 0;
713
714 switch (id) {
715 case V4L2_CID_AUDIO_MUTE:
716 case V4L2_CID_AUDIO_LOUDNESS:
717 case V4L2_CID_AUTO_WHITE_BALANCE:
718 case V4L2_CID_AUTOGAIN:
719 case V4L2_CID_HFLIP:
720 case V4L2_CID_VFLIP:
721 case V4L2_CID_HUE_AUTO:
722 case V4L2_CID_CHROMA_AGC:
723 case V4L2_CID_COLOR_KILLER:
724 case V4L2_CID_MPEG_AUDIO_MUTE:
725 case V4L2_CID_MPEG_VIDEO_MUTE:
726 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
727 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
728 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
729 case V4L2_CID_FOCUS_AUTO:
730 case V4L2_CID_PRIVACY:
731 case V4L2_CID_AUDIO_LIMITER_ENABLED:
732 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
733 case V4L2_CID_PILOT_TONE_ENABLED:
Jean-François Moine008d35f2010-09-13 07:04:49 -0300734 case V4L2_CID_ILLUMINATORS_1:
735 case V4L2_CID_ILLUMINATORS_2:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300736 case V4L2_CID_FLASH_STROBE_STATUS:
737 case V4L2_CID_FLASH_CHARGE:
738 case V4L2_CID_FLASH_READY:
Kamil Debski064f5092011-06-14 10:46:22 -0300739 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
740 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
741 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
742 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
743 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
744 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
745 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
Sylwester Nawrocki44d44a12012-03-06 07:05:45 -0300746 case V4L2_CID_WIDE_DYNAMIC_RANGE:
Sylwester Nawrocki82b30562012-05-01 17:38:09 -0300747 case V4L2_CID_IMAGE_STABILIZATION:
Hans Verkuil09965172010-08-01 14:32:42 -0300748 *type = V4L2_CTRL_TYPE_BOOLEAN;
749 *min = 0;
750 *max = *step = 1;
751 break;
752 case V4L2_CID_PAN_RESET:
753 case V4L2_CID_TILT_RESET:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300754 case V4L2_CID_FLASH_STROBE:
755 case V4L2_CID_FLASH_STROBE_STOP:
Hans Verkuil09965172010-08-01 14:32:42 -0300756 *type = V4L2_CTRL_TYPE_BUTTON;
757 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
758 *min = *max = *step = *def = 0;
759 break;
760 case V4L2_CID_POWER_LINE_FREQUENCY:
761 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
762 case V4L2_CID_MPEG_AUDIO_ENCODING:
763 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
764 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
765 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
766 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
767 case V4L2_CID_MPEG_AUDIO_MODE:
768 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
769 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
770 case V4L2_CID_MPEG_AUDIO_CRC:
Hans Verkuil24c19a22011-12-15 10:46:16 -0300771 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
772 case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
Hans Verkuil09965172010-08-01 14:32:42 -0300773 case V4L2_CID_MPEG_VIDEO_ENCODING:
774 case V4L2_CID_MPEG_VIDEO_ASPECT:
775 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
776 case V4L2_CID_MPEG_STREAM_TYPE:
777 case V4L2_CID_MPEG_STREAM_VBI_FMT:
778 case V4L2_CID_EXPOSURE_AUTO:
779 case V4L2_CID_COLORFX:
Sylwester Nawrockie40a0572012-03-06 07:04:26 -0300780 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
Hans Verkuil09965172010-08-01 14:32:42 -0300781 case V4L2_CID_TUNE_PREEMPHASIS:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300782 case V4L2_CID_FLASH_LED_MODE:
783 case V4L2_CID_FLASH_STROBE_SOURCE:
Kamil Debski064f5092011-06-14 10:46:22 -0300784 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
785 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
786 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
787 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
788 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
789 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
790 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
791 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
792 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
Sylwester Nawrockic7361ae2012-01-20 15:37:44 -0300793 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
Sylwester Nawrocki7f84ad82012-05-01 17:39:45 -0300794 case V4L2_CID_ISO_SENSITIVITY_AUTO:
Sylwester Nawrockicf072132012-04-30 04:34:10 -0300795 case V4L2_CID_EXPOSURE_METERING:
Hans Verkuil09965172010-08-01 14:32:42 -0300796 *type = V4L2_CTRL_TYPE_MENU;
797 break;
Sakari Ailusc643ee12012-02-02 20:17:54 -0300798 case V4L2_CID_LINK_FREQ:
799 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
800 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300801 case V4L2_CID_RDS_TX_PS_NAME:
802 case V4L2_CID_RDS_TX_RADIO_TEXT:
803 *type = V4L2_CTRL_TYPE_STRING;
804 break;
Sylwester Nawrocki7f84ad82012-05-01 17:39:45 -0300805 case V4L2_CID_ISO_SENSITIVITY:
Sylwester Nawrockid58083c2012-03-06 07:06:55 -0300806 case V4L2_CID_AUTO_EXPOSURE_BIAS:
807 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
808 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300809 case V4L2_CID_USER_CLASS:
810 case V4L2_CID_CAMERA_CLASS:
811 case V4L2_CID_MPEG_CLASS:
812 case V4L2_CID_FM_TX_CLASS:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300813 case V4L2_CID_FLASH_CLASS:
Sylwester Nawrockic7361ae2012-01-20 15:37:44 -0300814 case V4L2_CID_JPEG_CLASS:
Sakari Ailus8c9d2362011-10-04 08:20:05 -0300815 case V4L2_CID_IMAGE_SOURCE_CLASS:
Sakari Ailusc643ee12012-02-02 20:17:54 -0300816 case V4L2_CID_IMAGE_PROC_CLASS:
Hans Verkuil09965172010-08-01 14:32:42 -0300817 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
818 /* You can neither read not write these */
819 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
820 *min = *max = *step = *def = 0;
821 break;
822 case V4L2_CID_BG_COLOR:
823 *type = V4L2_CTRL_TYPE_INTEGER;
824 *step = 1;
825 *min = 0;
826 /* Max is calculated as RGB888 that is 2^24 */
827 *max = 0xFFFFFF;
828 break;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300829 case V4L2_CID_FLASH_FAULT:
Sylwester Nawrockic7361ae2012-01-20 15:37:44 -0300830 case V4L2_CID_JPEG_ACTIVE_MARKER:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300831 *type = V4L2_CTRL_TYPE_BITMASK;
832 break;
Kamil Debski064f5092011-06-14 10:46:22 -0300833 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
834 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
835 *type = V4L2_CTRL_TYPE_INTEGER;
836 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
837 break;
Hans Verkuil24c19a22011-12-15 10:46:16 -0300838 case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
839 case V4L2_CID_MPEG_VIDEO_DEC_PTS:
Sakari Ailusc643ee12012-02-02 20:17:54 -0300840 *flags |= V4L2_CTRL_FLAG_VOLATILE;
841 /* Fall through */
842 case V4L2_CID_PIXEL_RATE:
Hans Verkuil24c19a22011-12-15 10:46:16 -0300843 *type = V4L2_CTRL_TYPE_INTEGER64;
Sakari Ailusc643ee12012-02-02 20:17:54 -0300844 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
Hans Verkuilca576812012-04-19 12:37:18 -0300845 *min = *max = *step = *def = 0;
Hans Verkuil24c19a22011-12-15 10:46:16 -0300846 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300847 default:
848 *type = V4L2_CTRL_TYPE_INTEGER;
849 break;
850 }
851 switch (id) {
852 case V4L2_CID_MPEG_AUDIO_ENCODING:
853 case V4L2_CID_MPEG_AUDIO_MODE:
854 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
855 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
856 case V4L2_CID_MPEG_STREAM_TYPE:
857 *flags |= V4L2_CTRL_FLAG_UPDATE;
858 break;
859 case V4L2_CID_AUDIO_VOLUME:
860 case V4L2_CID_AUDIO_BALANCE:
861 case V4L2_CID_AUDIO_BASS:
862 case V4L2_CID_AUDIO_TREBLE:
863 case V4L2_CID_BRIGHTNESS:
864 case V4L2_CID_CONTRAST:
865 case V4L2_CID_SATURATION:
866 case V4L2_CID_HUE:
867 case V4L2_CID_RED_BALANCE:
868 case V4L2_CID_BLUE_BALANCE:
869 case V4L2_CID_GAMMA:
870 case V4L2_CID_SHARPNESS:
871 case V4L2_CID_CHROMA_GAIN:
872 case V4L2_CID_RDS_TX_DEVIATION:
873 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
874 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
875 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
876 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
877 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
878 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
879 case V4L2_CID_PILOT_TONE_DEVIATION:
880 case V4L2_CID_PILOT_TONE_FREQUENCY:
881 case V4L2_CID_TUNE_POWER_LEVEL:
882 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
883 *flags |= V4L2_CTRL_FLAG_SLIDER;
884 break;
885 case V4L2_CID_PAN_RELATIVE:
886 case V4L2_CID_TILT_RELATIVE:
887 case V4L2_CID_FOCUS_RELATIVE:
888 case V4L2_CID_IRIS_RELATIVE:
889 case V4L2_CID_ZOOM_RELATIVE:
890 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
891 break;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300892 case V4L2_CID_FLASH_STROBE_STATUS:
893 case V4L2_CID_FLASH_READY:
894 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
895 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300896 }
897}
898EXPORT_SYMBOL(v4l2_ctrl_fill);
899
900/* Helper function to determine whether the control type is compatible with
901 VIDIOC_G/S_CTRL. */
902static bool type_is_int(const struct v4l2_ctrl *ctrl)
903{
904 switch (ctrl->type) {
905 case V4L2_CTRL_TYPE_INTEGER64:
906 case V4L2_CTRL_TYPE_STRING:
907 /* Nope, these need v4l2_ext_control */
908 return false;
909 default:
910 return true;
911 }
912}
913
Hans Verkuil6e239392011-06-07 11:13:44 -0300914static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
915{
916 memset(ev->reserved, 0, sizeof(ev->reserved));
917 ev->type = V4L2_EVENT_CTRL;
918 ev->id = ctrl->id;
919 ev->u.ctrl.changes = changes;
920 ev->u.ctrl.type = ctrl->type;
921 ev->u.ctrl.flags = ctrl->flags;
922 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
923 ev->u.ctrl.value64 = 0;
924 else
925 ev->u.ctrl.value64 = ctrl->cur.val64;
926 ev->u.ctrl.minimum = ctrl->minimum;
927 ev->u.ctrl.maximum = ctrl->maximum;
Sakari Ailusce580fe2011-08-04 13:51:11 -0300928 if (ctrl->type == V4L2_CTRL_TYPE_MENU
929 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
Hans Verkuil6e239392011-06-07 11:13:44 -0300930 ev->u.ctrl.step = 1;
931 else
932 ev->u.ctrl.step = ctrl->step;
933 ev->u.ctrl.default_value = ctrl->default_value;
934}
935
936static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
937{
938 struct v4l2_event ev;
Hans Verkuil77068d32011-06-13 18:55:58 -0300939 struct v4l2_subscribed_event *sev;
Hans Verkuil6e239392011-06-07 11:13:44 -0300940
Hans Verkuil77068d32011-06-13 18:55:58 -0300941 if (list_empty(&ctrl->ev_subs))
Hans Verkuil3f66f0e2011-06-20 11:56:24 -0300942 return;
Hans Verkuil6e239392011-06-07 11:13:44 -0300943 fill_event(&ev, ctrl, changes);
944
Hans Verkuil77068d32011-06-13 18:55:58 -0300945 list_for_each_entry(sev, &ctrl->ev_subs, node)
Hans de Goedee3e72f392011-10-26 05:52:47 -0300946 if (sev->fh != fh ||
947 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
Hans Verkuil77068d32011-06-13 18:55:58 -0300948 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -0300949}
950
Hans Verkuil09965172010-08-01 14:32:42 -0300951/* Helper function: copy the current control value back to the caller */
952static int cur_to_user(struct v4l2_ext_control *c,
953 struct v4l2_ctrl *ctrl)
954{
955 u32 len;
956
957 switch (ctrl->type) {
958 case V4L2_CTRL_TYPE_STRING:
959 len = strlen(ctrl->cur.string);
960 if (c->size < len + 1) {
961 c->size = len + 1;
962 return -ENOSPC;
963 }
964 return copy_to_user(c->string, ctrl->cur.string,
965 len + 1) ? -EFAULT : 0;
966 case V4L2_CTRL_TYPE_INTEGER64:
967 c->value64 = ctrl->cur.val64;
968 break;
969 default:
970 c->value = ctrl->cur.val;
971 break;
972 }
973 return 0;
974}
975
976/* Helper function: copy the caller-provider value as the new control value */
977static int user_to_new(struct v4l2_ext_control *c,
978 struct v4l2_ctrl *ctrl)
979{
980 int ret;
981 u32 size;
982
Hans Verkuil2a863792011-01-11 14:45:03 -0300983 ctrl->is_new = 1;
Hans Verkuil09965172010-08-01 14:32:42 -0300984 switch (ctrl->type) {
985 case V4L2_CTRL_TYPE_INTEGER64:
986 ctrl->val64 = c->value64;
987 break;
988 case V4L2_CTRL_TYPE_STRING:
989 size = c->size;
990 if (size == 0)
991 return -ERANGE;
992 if (size > ctrl->maximum + 1)
993 size = ctrl->maximum + 1;
994 ret = copy_from_user(ctrl->string, c->string, size);
995 if (!ret) {
996 char last = ctrl->string[size - 1];
997
998 ctrl->string[size - 1] = 0;
999 /* If the string was longer than ctrl->maximum,
1000 then return an error. */
1001 if (strlen(ctrl->string) == ctrl->maximum && last)
1002 return -ERANGE;
1003 }
1004 return ret ? -EFAULT : 0;
1005 default:
1006 ctrl->val = c->value;
1007 break;
1008 }
1009 return 0;
1010}
1011
1012/* Helper function: copy the new control value back to the caller */
1013static int new_to_user(struct v4l2_ext_control *c,
1014 struct v4l2_ctrl *ctrl)
1015{
1016 u32 len;
1017
1018 switch (ctrl->type) {
1019 case V4L2_CTRL_TYPE_STRING:
1020 len = strlen(ctrl->string);
1021 if (c->size < len + 1) {
1022 c->size = ctrl->maximum + 1;
1023 return -ENOSPC;
1024 }
1025 return copy_to_user(c->string, ctrl->string,
1026 len + 1) ? -EFAULT : 0;
1027 case V4L2_CTRL_TYPE_INTEGER64:
1028 c->value64 = ctrl->val64;
1029 break;
1030 default:
1031 c->value = ctrl->val;
1032 break;
1033 }
1034 return 0;
1035}
1036
1037/* Copy the new value to the current value. */
Hans Verkuilab892ba2011-06-07 06:47:18 -03001038static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
1039 bool update_inactive)
Hans Verkuil09965172010-08-01 14:32:42 -03001040{
Hans Verkuil6e239392011-06-07 11:13:44 -03001041 bool changed = false;
1042
Hans Verkuil09965172010-08-01 14:32:42 -03001043 if (ctrl == NULL)
1044 return;
1045 switch (ctrl->type) {
Hans Verkuil6e239392011-06-07 11:13:44 -03001046 case V4L2_CTRL_TYPE_BUTTON:
1047 changed = true;
1048 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001049 case V4L2_CTRL_TYPE_STRING:
1050 /* strings are always 0-terminated */
Hans Verkuil6e239392011-06-07 11:13:44 -03001051 changed = strcmp(ctrl->string, ctrl->cur.string);
Hans Verkuil09965172010-08-01 14:32:42 -03001052 strcpy(ctrl->cur.string, ctrl->string);
1053 break;
1054 case V4L2_CTRL_TYPE_INTEGER64:
Hans Verkuil6e239392011-06-07 11:13:44 -03001055 changed = ctrl->val64 != ctrl->cur.val64;
Hans Verkuil09965172010-08-01 14:32:42 -03001056 ctrl->cur.val64 = ctrl->val64;
1057 break;
1058 default:
Hans Verkuil6e239392011-06-07 11:13:44 -03001059 changed = ctrl->val != ctrl->cur.val;
Hans Verkuil09965172010-08-01 14:32:42 -03001060 ctrl->cur.val = ctrl->val;
1061 break;
1062 }
Hans Verkuil72d877c2011-06-10 05:44:36 -03001063 if (update_inactive) {
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001064 /* Note: update_inactive can only be true for auto clusters. */
1065 ctrl->flags &=
1066 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
1067 if (!is_cur_manual(ctrl->cluster[0])) {
Hans Verkuil72d877c2011-06-10 05:44:36 -03001068 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001069 if (ctrl->cluster[0]->has_volatiles)
1070 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1071 }
Hans de Goede1249a3a2011-10-31 11:16:44 -03001072 fh = NULL;
Hans Verkuil72d877c2011-06-10 05:44:36 -03001073 }
Hans Verkuil639884a2011-07-05 07:09:26 -03001074 if (changed || update_inactive) {
1075 /* If a control was changed that was not one of the controls
1076 modified by the application, then send the event to all. */
1077 if (!ctrl->is_new)
1078 fh = NULL;
Hans Verkuil6e239392011-06-07 11:13:44 -03001079 send_event(fh, ctrl,
1080 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
1081 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
Hans Verkuil639884a2011-07-05 07:09:26 -03001082 }
Hans Verkuil09965172010-08-01 14:32:42 -03001083}
1084
1085/* Copy the current value to the new value */
1086static void cur_to_new(struct v4l2_ctrl *ctrl)
1087{
1088 if (ctrl == NULL)
1089 return;
1090 switch (ctrl->type) {
1091 case V4L2_CTRL_TYPE_STRING:
1092 /* strings are always 0-terminated */
1093 strcpy(ctrl->string, ctrl->cur.string);
1094 break;
1095 case V4L2_CTRL_TYPE_INTEGER64:
1096 ctrl->val64 = ctrl->cur.val64;
1097 break;
1098 default:
1099 ctrl->val = ctrl->cur.val;
1100 break;
1101 }
1102}
1103
1104/* Return non-zero if one or more of the controls in the cluster has a new
1105 value that differs from the current value. */
1106static int cluster_changed(struct v4l2_ctrl *master)
1107{
1108 int diff = 0;
1109 int i;
1110
1111 for (i = 0; !diff && i < master->ncontrols; i++) {
1112 struct v4l2_ctrl *ctrl = master->cluster[i];
1113
1114 if (ctrl == NULL)
1115 continue;
1116 switch (ctrl->type) {
1117 case V4L2_CTRL_TYPE_BUTTON:
1118 /* Button controls are always 'different' */
1119 return 1;
1120 case V4L2_CTRL_TYPE_STRING:
1121 /* strings are always 0-terminated */
1122 diff = strcmp(ctrl->string, ctrl->cur.string);
1123 break;
1124 case V4L2_CTRL_TYPE_INTEGER64:
1125 diff = ctrl->val64 != ctrl->cur.val64;
1126 break;
1127 default:
1128 diff = ctrl->val != ctrl->cur.val;
1129 break;
1130 }
1131 }
1132 return diff;
1133}
1134
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001135/* Validate integer-type control */
1136static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
Hans Verkuil09965172010-08-01 14:32:42 -03001137{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001138 s32 val = *pval;
Hans Verkuil09965172010-08-01 14:32:42 -03001139 u32 offset;
Hans Verkuil09965172010-08-01 14:32:42 -03001140
1141 switch (ctrl->type) {
1142 case V4L2_CTRL_TYPE_INTEGER:
1143 /* Round towards the closest legal value */
1144 val += ctrl->step / 2;
1145 if (val < ctrl->minimum)
1146 val = ctrl->minimum;
1147 if (val > ctrl->maximum)
1148 val = ctrl->maximum;
1149 offset = val - ctrl->minimum;
1150 offset = ctrl->step * (offset / ctrl->step);
1151 val = ctrl->minimum + offset;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001152 *pval = val;
Hans Verkuil09965172010-08-01 14:32:42 -03001153 return 0;
1154
1155 case V4L2_CTRL_TYPE_BOOLEAN:
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001156 *pval = !!val;
Hans Verkuil09965172010-08-01 14:32:42 -03001157 return 0;
1158
1159 case V4L2_CTRL_TYPE_MENU:
Sakari Ailusce580fe2011-08-04 13:51:11 -03001160 case V4L2_CTRL_TYPE_INTEGER_MENU:
Hans Verkuil09965172010-08-01 14:32:42 -03001161 if (val < ctrl->minimum || val > ctrl->maximum)
1162 return -ERANGE;
Sakari Ailusce580fe2011-08-04 13:51:11 -03001163 if (ctrl->menu_skip_mask & (1 << val))
1164 return -EINVAL;
1165 if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1166 ctrl->qmenu[val][0] == '\0')
Hans Verkuil09965172010-08-01 14:32:42 -03001167 return -EINVAL;
1168 return 0;
1169
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001170 case V4L2_CTRL_TYPE_BITMASK:
1171 *pval &= ctrl->maximum;
1172 return 0;
1173
Hans Verkuil09965172010-08-01 14:32:42 -03001174 case V4L2_CTRL_TYPE_BUTTON:
1175 case V4L2_CTRL_TYPE_CTRL_CLASS:
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001176 *pval = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001177 return 0;
1178
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001179 default:
1180 return -EINVAL;
1181 }
1182}
1183
1184/* Validate a new control */
1185static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
1186{
1187 char *s = c->string;
1188 size_t len;
1189
1190 switch (ctrl->type) {
1191 case V4L2_CTRL_TYPE_INTEGER:
1192 case V4L2_CTRL_TYPE_BOOLEAN:
1193 case V4L2_CTRL_TYPE_MENU:
Sakari Ailusce580fe2011-08-04 13:51:11 -03001194 case V4L2_CTRL_TYPE_INTEGER_MENU:
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001195 case V4L2_CTRL_TYPE_BITMASK:
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001196 case V4L2_CTRL_TYPE_BUTTON:
1197 case V4L2_CTRL_TYPE_CTRL_CLASS:
1198 return validate_new_int(ctrl, &c->value);
1199
Hans Verkuil09965172010-08-01 14:32:42 -03001200 case V4L2_CTRL_TYPE_INTEGER64:
1201 return 0;
1202
1203 case V4L2_CTRL_TYPE_STRING:
1204 len = strlen(s);
1205 if (len < ctrl->minimum)
1206 return -ERANGE;
1207 if ((len - ctrl->minimum) % ctrl->step)
1208 return -ERANGE;
1209 return 0;
1210
1211 default:
1212 return -EINVAL;
1213 }
1214}
1215
1216static inline u32 node2id(struct list_head *node)
1217{
1218 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
1219}
1220
1221/* Set the handler's error code if it wasn't set earlier already */
1222static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1223{
1224 if (hdl->error == 0)
1225 hdl->error = err;
1226 return err;
1227}
1228
1229/* Initialize the handler */
1230int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
1231 unsigned nr_of_controls_hint)
1232{
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001233 hdl->lock = &hdl->_lock;
1234 mutex_init(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001235 INIT_LIST_HEAD(&hdl->ctrls);
1236 INIT_LIST_HEAD(&hdl->ctrl_refs);
1237 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
Thomas Meyer9884d7b2011-11-29 17:08:00 -03001238 hdl->buckets = kcalloc(hdl->nr_of_buckets, sizeof(hdl->buckets[0]),
1239 GFP_KERNEL);
Hans Verkuil09965172010-08-01 14:32:42 -03001240 hdl->error = hdl->buckets ? 0 : -ENOMEM;
1241 return hdl->error;
1242}
1243EXPORT_SYMBOL(v4l2_ctrl_handler_init);
1244
1245/* Free all controls and control refs */
1246void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1247{
1248 struct v4l2_ctrl_ref *ref, *next_ref;
1249 struct v4l2_ctrl *ctrl, *next_ctrl;
Hans Verkuil77068d32011-06-13 18:55:58 -03001250 struct v4l2_subscribed_event *sev, *next_sev;
Hans Verkuil09965172010-08-01 14:32:42 -03001251
1252 if (hdl == NULL || hdl->buckets == NULL)
1253 return;
1254
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001255 mutex_lock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001256 /* Free all nodes */
1257 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1258 list_del(&ref->node);
1259 kfree(ref);
1260 }
1261 /* Free all controls owned by the handler */
1262 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1263 list_del(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -03001264 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1265 list_del(&sev->node);
Hans Verkuil09965172010-08-01 14:32:42 -03001266 kfree(ctrl);
1267 }
1268 kfree(hdl->buckets);
1269 hdl->buckets = NULL;
1270 hdl->cached = NULL;
1271 hdl->error = 0;
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001272 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001273}
1274EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1275
1276/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1277 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1278 with applications that do not use the NEXT_CTRL flag.
1279
1280 We just find the n-th private user control. It's O(N), but that should not
1281 be an issue in this particular case. */
1282static struct v4l2_ctrl_ref *find_private_ref(
1283 struct v4l2_ctrl_handler *hdl, u32 id)
1284{
1285 struct v4l2_ctrl_ref *ref;
1286
1287 id -= V4L2_CID_PRIVATE_BASE;
1288 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1289 /* Search for private user controls that are compatible with
1290 VIDIOC_G/S_CTRL. */
1291 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1292 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1293 if (!type_is_int(ref->ctrl))
1294 continue;
1295 if (id == 0)
1296 return ref;
1297 id--;
1298 }
1299 }
1300 return NULL;
1301}
1302
1303/* Find a control with the given ID. */
1304static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1305{
1306 struct v4l2_ctrl_ref *ref;
1307 int bucket;
1308
1309 id &= V4L2_CTRL_ID_MASK;
1310
1311 /* Old-style private controls need special handling */
1312 if (id >= V4L2_CID_PRIVATE_BASE)
1313 return find_private_ref(hdl, id);
1314 bucket = id % hdl->nr_of_buckets;
1315
1316 /* Simple optimization: cache the last control found */
1317 if (hdl->cached && hdl->cached->ctrl->id == id)
1318 return hdl->cached;
1319
1320 /* Not in cache, search the hash */
1321 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1322 while (ref && ref->ctrl->id != id)
1323 ref = ref->next;
1324
1325 if (ref)
1326 hdl->cached = ref; /* cache it! */
1327 return ref;
1328}
1329
1330/* Find a control with the given ID. Take the handler's lock first. */
1331static struct v4l2_ctrl_ref *find_ref_lock(
1332 struct v4l2_ctrl_handler *hdl, u32 id)
1333{
1334 struct v4l2_ctrl_ref *ref = NULL;
1335
1336 if (hdl) {
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001337 mutex_lock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001338 ref = find_ref(hdl, id);
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001339 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001340 }
1341 return ref;
1342}
1343
1344/* Find a control with the given ID. */
1345struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1346{
1347 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1348
1349 return ref ? ref->ctrl : NULL;
1350}
1351EXPORT_SYMBOL(v4l2_ctrl_find);
1352
1353/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1354static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1355 struct v4l2_ctrl *ctrl)
1356{
1357 struct v4l2_ctrl_ref *ref;
1358 struct v4l2_ctrl_ref *new_ref;
1359 u32 id = ctrl->id;
1360 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
1361 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
1362
1363 /* Automatically add the control class if it is not yet present. */
1364 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1365 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1366 return hdl->error;
1367
1368 if (hdl->error)
1369 return hdl->error;
1370
1371 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
1372 if (!new_ref)
1373 return handler_set_err(hdl, -ENOMEM);
1374 new_ref->ctrl = ctrl;
1375 if (ctrl->handler == hdl) {
1376 /* By default each control starts in a cluster of its own.
1377 new_ref->ctrl is basically a cluster array with one
1378 element, so that's perfect to use as the cluster pointer.
1379 But only do this for the handler that owns the control. */
1380 ctrl->cluster = &new_ref->ctrl;
1381 ctrl->ncontrols = 1;
1382 }
1383
1384 INIT_LIST_HEAD(&new_ref->node);
1385
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001386 mutex_lock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001387
1388 /* Add immediately at the end of the list if the list is empty, or if
1389 the last element in the list has a lower ID.
1390 This ensures that when elements are added in ascending order the
1391 insertion is an O(1) operation. */
1392 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1393 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1394 goto insert_in_hash;
1395 }
1396
1397 /* Find insert position in sorted list */
1398 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1399 if (ref->ctrl->id < id)
1400 continue;
1401 /* Don't add duplicates */
1402 if (ref->ctrl->id == id) {
1403 kfree(new_ref);
1404 goto unlock;
1405 }
1406 list_add(&new_ref->node, ref->node.prev);
1407 break;
1408 }
1409
1410insert_in_hash:
1411 /* Insert the control node in the hash */
1412 new_ref->next = hdl->buckets[bucket];
1413 hdl->buckets[bucket] = new_ref;
1414
1415unlock:
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001416 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001417 return 0;
1418}
1419
1420/* Add a new control */
1421static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1422 const struct v4l2_ctrl_ops *ops,
1423 u32 id, const char *name, enum v4l2_ctrl_type type,
1424 s32 min, s32 max, u32 step, s32 def,
Sakari Ailusce580fe2011-08-04 13:51:11 -03001425 u32 flags, const char * const *qmenu,
1426 const s64 *qmenu_int, void *priv)
Hans Verkuil09965172010-08-01 14:32:42 -03001427{
1428 struct v4l2_ctrl *ctrl;
1429 unsigned sz_extra = 0;
1430
1431 if (hdl->error)
1432 return NULL;
1433
1434 /* Sanity checks */
1435 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
Hans Verkuil09965172010-08-01 14:32:42 -03001436 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001437 (type == V4L2_CTRL_TYPE_BITMASK && max == 0) ||
Hans Verkuil09965172010-08-01 14:32:42 -03001438 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
Sakari Ailusce580fe2011-08-04 13:51:11 -03001439 (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL) ||
Hans Verkuil09965172010-08-01 14:32:42 -03001440 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1441 handler_set_err(hdl, -ERANGE);
1442 return NULL;
1443 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001444 if (type != V4L2_CTRL_TYPE_BITMASK && max < min) {
1445 handler_set_err(hdl, -ERANGE);
1446 return NULL;
1447 }
Hans Verkuil02ac0482010-12-29 14:27:05 -03001448 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1449 type == V4L2_CTRL_TYPE_MENU ||
Sakari Ailusce580fe2011-08-04 13:51:11 -03001450 type == V4L2_CTRL_TYPE_INTEGER_MENU ||
Hans Verkuil02ac0482010-12-29 14:27:05 -03001451 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1452 (def < min || def > max)) {
1453 handler_set_err(hdl, -ERANGE);
1454 return NULL;
1455 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001456 if (type == V4L2_CTRL_TYPE_BITMASK && ((def & ~max) || min || step)) {
1457 handler_set_err(hdl, -ERANGE);
1458 return NULL;
1459 }
Hans Verkuil09965172010-08-01 14:32:42 -03001460
1461 if (type == V4L2_CTRL_TYPE_BUTTON)
1462 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1463 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1464 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1465 else if (type == V4L2_CTRL_TYPE_STRING)
1466 sz_extra += 2 * (max + 1);
1467
1468 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1469 if (ctrl == NULL) {
1470 handler_set_err(hdl, -ENOMEM);
1471 return NULL;
1472 }
1473
1474 INIT_LIST_HEAD(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -03001475 INIT_LIST_HEAD(&ctrl->ev_subs);
Hans Verkuil09965172010-08-01 14:32:42 -03001476 ctrl->handler = hdl;
1477 ctrl->ops = ops;
1478 ctrl->id = id;
1479 ctrl->name = name;
1480 ctrl->type = type;
1481 ctrl->flags = flags;
1482 ctrl->minimum = min;
1483 ctrl->maximum = max;
1484 ctrl->step = step;
Sakari Ailusce580fe2011-08-04 13:51:11 -03001485 if (type == V4L2_CTRL_TYPE_MENU)
1486 ctrl->qmenu = qmenu;
1487 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
1488 ctrl->qmenu_int = qmenu_int;
Hans Verkuil09965172010-08-01 14:32:42 -03001489 ctrl->priv = priv;
1490 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1491
1492 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1493 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1494 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1495 if (ctrl->minimum)
1496 memset(ctrl->cur.string, ' ', ctrl->minimum);
1497 }
1498 if (handler_new_ref(hdl, ctrl)) {
1499 kfree(ctrl);
1500 return NULL;
1501 }
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001502 mutex_lock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001503 list_add_tail(&ctrl->node, &hdl->ctrls);
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001504 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001505 return ctrl;
1506}
1507
1508struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1509 const struct v4l2_ctrl_config *cfg, void *priv)
1510{
1511 bool is_menu;
1512 struct v4l2_ctrl *ctrl;
1513 const char *name = cfg->name;
Hans Verkuil513521e2010-12-29 14:25:52 -03001514 const char * const *qmenu = cfg->qmenu;
Sakari Ailusce580fe2011-08-04 13:51:11 -03001515 const s64 *qmenu_int = cfg->qmenu_int;
Hans Verkuil09965172010-08-01 14:32:42 -03001516 enum v4l2_ctrl_type type = cfg->type;
1517 u32 flags = cfg->flags;
1518 s32 min = cfg->min;
1519 s32 max = cfg->max;
1520 u32 step = cfg->step;
1521 s32 def = cfg->def;
1522
1523 if (name == NULL)
1524 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1525 &def, &flags);
1526
Sakari Ailusce580fe2011-08-04 13:51:11 -03001527 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU ||
1528 cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU);
Hans Verkuil09965172010-08-01 14:32:42 -03001529 if (is_menu)
1530 WARN_ON(step);
1531 else
1532 WARN_ON(cfg->menu_skip_mask);
Sakari Ailusce580fe2011-08-04 13:51:11 -03001533 if (cfg->type == V4L2_CTRL_TYPE_MENU && qmenu == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001534 qmenu = v4l2_ctrl_get_menu(cfg->id);
Sakari Ailusce580fe2011-08-04 13:51:11 -03001535 else if (cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU &&
1536 qmenu_int == NULL) {
1537 handler_set_err(hdl, -EINVAL);
1538 return NULL;
1539 }
Hans Verkuil09965172010-08-01 14:32:42 -03001540
1541 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1542 type, min, max,
1543 is_menu ? cfg->menu_skip_mask : step,
Sakari Ailusce580fe2011-08-04 13:51:11 -03001544 def, flags, qmenu, qmenu_int, priv);
Hans Verkuil88365102011-08-26 07:35:14 -03001545 if (ctrl)
Hans Verkuil09965172010-08-01 14:32:42 -03001546 ctrl->is_private = cfg->is_private;
Hans Verkuil09965172010-08-01 14:32:42 -03001547 return ctrl;
1548}
1549EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1550
1551/* Helper function for standard non-menu controls */
1552struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1553 const struct v4l2_ctrl_ops *ops,
1554 u32 id, s32 min, s32 max, u32 step, s32 def)
1555{
1556 const char *name;
1557 enum v4l2_ctrl_type type;
1558 u32 flags;
1559
1560 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
Sakari Ailusce580fe2011-08-04 13:51:11 -03001561 if (type == V4L2_CTRL_TYPE_MENU
1562 || type == V4L2_CTRL_TYPE_INTEGER_MENU) {
Hans Verkuil09965172010-08-01 14:32:42 -03001563 handler_set_err(hdl, -EINVAL);
1564 return NULL;
1565 }
1566 return v4l2_ctrl_new(hdl, ops, id, name, type,
Sakari Ailusce580fe2011-08-04 13:51:11 -03001567 min, max, step, def, flags, NULL, NULL, NULL);
Hans Verkuil09965172010-08-01 14:32:42 -03001568}
1569EXPORT_SYMBOL(v4l2_ctrl_new_std);
1570
1571/* Helper function for standard menu controls */
1572struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1573 const struct v4l2_ctrl_ops *ops,
1574 u32 id, s32 max, s32 mask, s32 def)
1575{
Hans Verkuil513521e2010-12-29 14:25:52 -03001576 const char * const *qmenu = v4l2_ctrl_get_menu(id);
Hans Verkuil09965172010-08-01 14:32:42 -03001577 const char *name;
1578 enum v4l2_ctrl_type type;
1579 s32 min;
1580 s32 step;
1581 u32 flags;
1582
1583 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1584 if (type != V4L2_CTRL_TYPE_MENU) {
1585 handler_set_err(hdl, -EINVAL);
1586 return NULL;
1587 }
1588 return v4l2_ctrl_new(hdl, ops, id, name, type,
Sakari Ailusce580fe2011-08-04 13:51:11 -03001589 0, max, mask, def, flags, qmenu, NULL, NULL);
Hans Verkuil09965172010-08-01 14:32:42 -03001590}
1591EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1592
Sylwester Nawrocki515f3282012-05-06 15:30:44 -03001593/* Helper function for standard integer menu controls */
1594struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
1595 const struct v4l2_ctrl_ops *ops,
1596 u32 id, s32 max, s32 def, const s64 *qmenu_int)
1597{
1598 const char *name;
1599 enum v4l2_ctrl_type type;
1600 s32 min;
1601 s32 step;
1602 u32 flags;
1603
1604 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1605 if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
1606 handler_set_err(hdl, -EINVAL);
1607 return NULL;
1608 }
1609 return v4l2_ctrl_new(hdl, ops, id, name, type,
1610 0, max, 0, def, flags, NULL, qmenu_int, NULL);
1611}
1612EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
1613
Hans Verkuil09965172010-08-01 14:32:42 -03001614/* Add a control from another handler to this handler */
1615struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1616 struct v4l2_ctrl *ctrl)
1617{
1618 if (hdl == NULL || hdl->error)
1619 return NULL;
1620 if (ctrl == NULL) {
1621 handler_set_err(hdl, -EINVAL);
1622 return NULL;
1623 }
1624 if (ctrl->handler == hdl)
1625 return ctrl;
1626 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1627}
1628EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1629
1630/* Add the controls from another handler to our own. */
1631int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1632 struct v4l2_ctrl_handler *add)
1633{
Hans Verkuil072e6602012-03-02 12:41:25 -03001634 struct v4l2_ctrl_ref *ref;
Hans Verkuil09965172010-08-01 14:32:42 -03001635 int ret = 0;
1636
1637 /* Do nothing if either handler is NULL or if they are the same */
1638 if (!hdl || !add || hdl == add)
1639 return 0;
1640 if (hdl->error)
1641 return hdl->error;
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001642 mutex_lock(add->lock);
Hans Verkuil072e6602012-03-02 12:41:25 -03001643 list_for_each_entry(ref, &add->ctrl_refs, node) {
1644 struct v4l2_ctrl *ctrl = ref->ctrl;
1645
Hans Verkuil09965172010-08-01 14:32:42 -03001646 /* Skip handler-private controls. */
1647 if (ctrl->is_private)
1648 continue;
Hans Verkuil6e239392011-06-07 11:13:44 -03001649 /* And control classes */
1650 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1651 continue;
Hans Verkuil09965172010-08-01 14:32:42 -03001652 ret = handler_new_ref(hdl, ctrl);
1653 if (ret)
1654 break;
1655 }
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001656 mutex_unlock(add->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001657 return ret;
1658}
1659EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1660
1661/* Cluster controls */
1662void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1663{
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001664 bool has_volatiles = false;
Hans Verkuil09965172010-08-01 14:32:42 -03001665 int i;
1666
1667 /* The first control is the master control and it must not be NULL */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001668 BUG_ON(ncontrols == 0 || controls[0] == NULL);
Hans Verkuil09965172010-08-01 14:32:42 -03001669
1670 for (i = 0; i < ncontrols; i++) {
1671 if (controls[i]) {
1672 controls[i]->cluster = controls;
1673 controls[i]->ncontrols = ncontrols;
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001674 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
1675 has_volatiles = true;
Hans Verkuil09965172010-08-01 14:32:42 -03001676 }
1677 }
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001678 controls[0]->has_volatiles = has_volatiles;
Hans Verkuil09965172010-08-01 14:32:42 -03001679}
1680EXPORT_SYMBOL(v4l2_ctrl_cluster);
1681
Hans Verkuil72d877c2011-06-10 05:44:36 -03001682void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1683 u8 manual_val, bool set_volatile)
1684{
1685 struct v4l2_ctrl *master = controls[0];
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001686 u32 flag = 0;
Hans Verkuil72d877c2011-06-10 05:44:36 -03001687 int i;
1688
1689 v4l2_ctrl_cluster(ncontrols, controls);
1690 WARN_ON(ncontrols <= 1);
Hans Verkuil82a7c042011-06-28 10:43:13 -03001691 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001692 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
Hans Verkuil72d877c2011-06-10 05:44:36 -03001693 master->is_auto = true;
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001694 master->has_volatiles = set_volatile;
Hans Verkuil72d877c2011-06-10 05:44:36 -03001695 master->manual_mode_value = manual_val;
1696 master->flags |= V4L2_CTRL_FLAG_UPDATE;
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001697
1698 if (!is_cur_manual(master))
1699 flag = V4L2_CTRL_FLAG_INACTIVE |
1700 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001701
1702 for (i = 1; i < ncontrols; i++)
Hans Verkuil88365102011-08-26 07:35:14 -03001703 if (controls[i])
Hans Verkuil72d877c2011-06-10 05:44:36 -03001704 controls[i]->flags |= flag;
Hans Verkuil72d877c2011-06-10 05:44:36 -03001705}
1706EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1707
Hans Verkuil09965172010-08-01 14:32:42 -03001708/* Activate/deactivate a control. */
1709void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1710{
Hans Verkuil6e239392011-06-07 11:13:44 -03001711 /* invert since the actual flag is called 'inactive' */
1712 bool inactive = !active;
1713 bool old;
1714
Hans Verkuil09965172010-08-01 14:32:42 -03001715 if (ctrl == NULL)
1716 return;
1717
Hans Verkuil6e239392011-06-07 11:13:44 -03001718 if (inactive)
Hans Verkuil09965172010-08-01 14:32:42 -03001719 /* set V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001720 old = test_and_set_bit(4, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001721 else
1722 /* clear V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001723 old = test_and_clear_bit(4, &ctrl->flags);
1724 if (old != inactive)
1725 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
Hans Verkuil09965172010-08-01 14:32:42 -03001726}
1727EXPORT_SYMBOL(v4l2_ctrl_activate);
1728
1729/* Grab/ungrab a control.
1730 Typically used when streaming starts and you want to grab controls,
1731 preventing the user from changing them.
1732
1733 Just call this and the framework will block any attempts to change
1734 these controls. */
1735void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1736{
Hans Verkuil6e239392011-06-07 11:13:44 -03001737 bool old;
1738
Hans Verkuil09965172010-08-01 14:32:42 -03001739 if (ctrl == NULL)
1740 return;
1741
Hans Verkuil6e239392011-06-07 11:13:44 -03001742 v4l2_ctrl_lock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001743 if (grabbed)
1744 /* set V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001745 old = test_and_set_bit(1, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001746 else
1747 /* clear V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001748 old = test_and_clear_bit(1, &ctrl->flags);
1749 if (old != grabbed)
1750 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1751 v4l2_ctrl_unlock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001752}
1753EXPORT_SYMBOL(v4l2_ctrl_grab);
1754
1755/* Log the control name and value */
1756static void log_ctrl(const struct v4l2_ctrl *ctrl,
1757 const char *prefix, const char *colon)
1758{
Hans Verkuil09965172010-08-01 14:32:42 -03001759 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1760 return;
1761 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1762 return;
1763
1764 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1765
1766 switch (ctrl->type) {
1767 case V4L2_CTRL_TYPE_INTEGER:
1768 printk(KERN_CONT "%d", ctrl->cur.val);
1769 break;
1770 case V4L2_CTRL_TYPE_BOOLEAN:
1771 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1772 break;
1773 case V4L2_CTRL_TYPE_MENU:
1774 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1775 break;
Sakari Ailusce580fe2011-08-04 13:51:11 -03001776 case V4L2_CTRL_TYPE_INTEGER_MENU:
1777 printk(KERN_CONT "%lld", ctrl->qmenu_int[ctrl->cur.val]);
1778 break;
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001779 case V4L2_CTRL_TYPE_BITMASK:
1780 printk(KERN_CONT "0x%08x", ctrl->cur.val);
1781 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001782 case V4L2_CTRL_TYPE_INTEGER64:
1783 printk(KERN_CONT "%lld", ctrl->cur.val64);
1784 break;
1785 case V4L2_CTRL_TYPE_STRING:
1786 printk(KERN_CONT "%s", ctrl->cur.string);
1787 break;
1788 default:
1789 printk(KERN_CONT "unknown type %d", ctrl->type);
1790 break;
1791 }
Hans Verkuil88365102011-08-26 07:35:14 -03001792 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
1793 V4L2_CTRL_FLAG_GRABBED |
1794 V4L2_CTRL_FLAG_VOLATILE)) {
1795 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1796 printk(KERN_CONT " inactive");
1797 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
1798 printk(KERN_CONT " grabbed");
1799 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
1800 printk(KERN_CONT " volatile");
1801 }
1802 printk(KERN_CONT "\n");
Hans Verkuil09965172010-08-01 14:32:42 -03001803}
1804
1805/* Log all controls owned by the handler */
1806void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1807 const char *prefix)
1808{
1809 struct v4l2_ctrl *ctrl;
1810 const char *colon = "";
1811 int len;
1812
1813 if (hdl == NULL)
1814 return;
1815 if (prefix == NULL)
1816 prefix = "";
1817 len = strlen(prefix);
1818 if (len && prefix[len - 1] != ' ')
1819 colon = ": ";
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001820 mutex_lock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001821 list_for_each_entry(ctrl, &hdl->ctrls, node)
1822 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1823 log_ctrl(ctrl, prefix, colon);
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001824 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001825}
1826EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1827
1828/* Call s_ctrl for all controls owned by the handler */
1829int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1830{
1831 struct v4l2_ctrl *ctrl;
1832 int ret = 0;
1833
1834 if (hdl == NULL)
1835 return 0;
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001836 mutex_lock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001837 list_for_each_entry(ctrl, &hdl->ctrls, node)
1838 ctrl->done = false;
1839
1840 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1841 struct v4l2_ctrl *master = ctrl->cluster[0];
1842 int i;
1843
1844 /* Skip if this control was already handled by a cluster. */
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001845 /* Skip button controls and read-only controls. */
1846 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1847 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
Hans Verkuil09965172010-08-01 14:32:42 -03001848 continue;
1849
Hans Verkuil2a863792011-01-11 14:45:03 -03001850 for (i = 0; i < master->ncontrols; i++) {
1851 if (master->cluster[i]) {
1852 cur_to_new(master->cluster[i]);
1853 master->cluster[i]->is_new = 1;
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001854 master->cluster[i]->done = true;
Hans Verkuil2a863792011-01-11 14:45:03 -03001855 }
1856 }
Hans Verkuil54c911e2011-05-25 06:04:58 -03001857 ret = call_op(master, s_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001858 if (ret)
1859 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001860 }
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001861 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001862 return ret;
1863}
1864EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1865
1866/* Implement VIDIOC_QUERYCTRL */
1867int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1868{
1869 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1870 struct v4l2_ctrl_ref *ref;
1871 struct v4l2_ctrl *ctrl;
1872
1873 if (hdl == NULL)
1874 return -EINVAL;
1875
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001876 mutex_lock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001877
1878 /* Try to find it */
1879 ref = find_ref(hdl, id);
1880
1881 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1882 /* Find the next control with ID > qc->id */
1883
1884 /* Did we reach the end of the control list? */
1885 if (id >= node2id(hdl->ctrl_refs.prev)) {
1886 ref = NULL; /* Yes, so there is no next control */
1887 } else if (ref) {
1888 /* We found a control with the given ID, so just get
1889 the next one in the list. */
1890 ref = list_entry(ref->node.next, typeof(*ref), node);
1891 } else {
1892 /* No control with the given ID exists, so start
1893 searching for the next largest ID. We know there
1894 is one, otherwise the first 'if' above would have
1895 been true. */
1896 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1897 if (id < ref->ctrl->id)
1898 break;
1899 }
1900 }
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03001901 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001902 if (!ref)
1903 return -EINVAL;
1904
1905 ctrl = ref->ctrl;
1906 memset(qc, 0, sizeof(*qc));
Hans Verkuil829fb2d2011-01-16 11:21:40 -03001907 if (id >= V4L2_CID_PRIVATE_BASE)
1908 qc->id = id;
1909 else
1910 qc->id = ctrl->id;
Hans Verkuil09965172010-08-01 14:32:42 -03001911 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1912 qc->minimum = ctrl->minimum;
1913 qc->maximum = ctrl->maximum;
1914 qc->default_value = ctrl->default_value;
Sakari Ailusce580fe2011-08-04 13:51:11 -03001915 if (ctrl->type == V4L2_CTRL_TYPE_MENU
1916 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
Hans Verkuil09965172010-08-01 14:32:42 -03001917 qc->step = 1;
1918 else
1919 qc->step = ctrl->step;
1920 qc->flags = ctrl->flags;
1921 qc->type = ctrl->type;
1922 return 0;
1923}
1924EXPORT_SYMBOL(v4l2_queryctrl);
1925
1926int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1927{
Hans Verkuil87a0c942011-02-22 12:31:07 -03001928 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1929 return -EINVAL;
Hans Verkuil09965172010-08-01 14:32:42 -03001930 return v4l2_queryctrl(sd->ctrl_handler, qc);
1931}
1932EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1933
1934/* Implement VIDIOC_QUERYMENU */
1935int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1936{
1937 struct v4l2_ctrl *ctrl;
1938 u32 i = qm->index;
1939
1940 ctrl = v4l2_ctrl_find(hdl, qm->id);
1941 if (!ctrl)
1942 return -EINVAL;
1943
1944 qm->reserved = 0;
1945 /* Sanity checks */
Sakari Ailusce580fe2011-08-04 13:51:11 -03001946 switch (ctrl->type) {
1947 case V4L2_CTRL_TYPE_MENU:
1948 if (ctrl->qmenu == NULL)
1949 return -EINVAL;
1950 break;
1951 case V4L2_CTRL_TYPE_INTEGER_MENU:
1952 if (ctrl->qmenu_int == NULL)
1953 return -EINVAL;
1954 break;
1955 default:
Hans Verkuil09965172010-08-01 14:32:42 -03001956 return -EINVAL;
Sakari Ailusce580fe2011-08-04 13:51:11 -03001957 }
1958
1959 if (i < ctrl->minimum || i > ctrl->maximum)
1960 return -EINVAL;
1961
Hans Verkuil09965172010-08-01 14:32:42 -03001962 /* Use mask to see if this menu item should be skipped */
1963 if (ctrl->menu_skip_mask & (1 << i))
1964 return -EINVAL;
1965 /* Empty menu items should also be skipped */
Sakari Ailusce580fe2011-08-04 13:51:11 -03001966 if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
1967 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1968 return -EINVAL;
1969 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1970 } else {
1971 qm->value = ctrl->qmenu_int[i];
1972 }
Hans Verkuil09965172010-08-01 14:32:42 -03001973 return 0;
1974}
1975EXPORT_SYMBOL(v4l2_querymenu);
1976
1977int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1978{
1979 return v4l2_querymenu(sd->ctrl_handler, qm);
1980}
1981EXPORT_SYMBOL(v4l2_subdev_querymenu);
1982
1983
1984
1985/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1986
1987 It is not a fully atomic operation, just best-effort only. After all, if
1988 multiple controls have to be set through multiple i2c writes (for example)
1989 then some initial writes may succeed while others fail. Thus leaving the
1990 system in an inconsistent state. The question is how much effort you are
1991 willing to spend on trying to make something atomic that really isn't.
1992
1993 From the point of view of an application the main requirement is that
1994 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1995 error should be returned without actually affecting any controls.
1996
1997 If all the values are correct, then it is acceptable to just give up
1998 in case of low-level errors.
1999
2000 It is important though that the application can tell when only a partial
2001 configuration was done. The way we do that is through the error_idx field
2002 of struct v4l2_ext_controls: if that is equal to the count field then no
2003 controls were affected. Otherwise all controls before that index were
2004 successful in performing their 'get' or 'set' operation, the control at
2005 the given index failed, and you don't know what happened with the controls
2006 after the failed one. Since if they were part of a control cluster they
2007 could have been successfully processed (if a cluster member was encountered
2008 at index < error_idx), they could have failed (if a cluster member was at
2009 error_idx), or they may not have been processed yet (if the first cluster
2010 member appeared after error_idx).
2011
2012 It is all fairly theoretical, though. In practice all you can do is to
2013 bail out. If error_idx == count, then it is an application bug. If
2014 error_idx < count then it is only an application bug if the error code was
2015 EBUSY. That usually means that something started streaming just when you
2016 tried to set the controls. In all other cases it is a driver/hardware
2017 problem and all you can do is to retry or bail out.
2018
2019 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
2020 never modifies controls the error_idx is just set to whatever control
2021 has an invalid value.
2022 */
2023
2024/* Prepare for the extended g/s/try functions.
2025 Find the controls in the control array and do some basic checks. */
2026static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
2027 struct v4l2_ext_controls *cs,
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002028 struct v4l2_ctrl_helper *helpers)
Hans Verkuil09965172010-08-01 14:32:42 -03002029{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002030 struct v4l2_ctrl_helper *h;
2031 bool have_clusters = false;
Hans Verkuil09965172010-08-01 14:32:42 -03002032 u32 i;
2033
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002034 for (i = 0, h = helpers; i < cs->count; i++, h++) {
Hans Verkuil09965172010-08-01 14:32:42 -03002035 struct v4l2_ext_control *c = &cs->controls[i];
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002036 struct v4l2_ctrl_ref *ref;
Hans Verkuil09965172010-08-01 14:32:42 -03002037 struct v4l2_ctrl *ctrl;
2038 u32 id = c->id & V4L2_CTRL_ID_MASK;
2039
Hans Verkuil37cd3b72011-06-07 04:40:04 -03002040 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03002041
2042 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
2043 return -EINVAL;
2044
2045 /* Old-style private controls are not allowed for
2046 extended controls */
2047 if (id >= V4L2_CID_PRIVATE_BASE)
2048 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002049 ref = find_ref_lock(hdl, id);
2050 if (ref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03002051 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002052 ctrl = ref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03002053 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
2054 return -EINVAL;
2055
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002056 if (ctrl->cluster[0]->ncontrols > 1)
2057 have_clusters = true;
2058 if (ctrl->cluster[0] != ctrl)
2059 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
2060 /* Store the ref to the master control of the cluster */
2061 h->mref = ref;
2062 h->ctrl = ctrl;
2063 /* Initially set next to 0, meaning that there is no other
2064 control in this helper array belonging to the same
2065 cluster */
2066 h->next = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002067 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002068
2069 /* We are done if there were no controls that belong to a multi-
2070 control cluster. */
2071 if (!have_clusters)
2072 return 0;
2073
2074 /* The code below figures out in O(n) time which controls in the list
2075 belong to the same cluster. */
2076
2077 /* This has to be done with the handler lock taken. */
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03002078 mutex_lock(hdl->lock);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002079
2080 /* First zero the helper field in the master control references */
2081 for (i = 0; i < cs->count; i++)
2082 helpers[i].mref->helper = 0;
2083 for (i = 0, h = helpers; i < cs->count; i++, h++) {
2084 struct v4l2_ctrl_ref *mref = h->mref;
2085
2086 /* If the mref->helper is set, then it points to an earlier
2087 helper that belongs to the same cluster. */
2088 if (mref->helper) {
2089 /* Set the next field of mref->helper to the current
2090 index: this means that that earlier helper now
2091 points to the next helper in the same cluster. */
2092 mref->helper->next = i;
2093 /* mref should be set only for the first helper in the
2094 cluster, clear the others. */
2095 h->mref = NULL;
2096 }
2097 /* Point the mref helper to the current helper struct. */
2098 mref->helper = h;
2099 }
Sakari Ailus77e7c4e2012-01-24 21:05:34 -03002100 mutex_unlock(hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03002101 return 0;
2102}
2103
Hans Verkuil09965172010-08-01 14:32:42 -03002104/* Handles the corner case where cs->count == 0. It checks whether the
2105 specified control class exists. If that class ID is 0, then it checks
2106 whether there are any controls at all. */
2107static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
2108{
2109 if (ctrl_class == 0)
2110 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
2111 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
2112}
2113
2114
2115
2116/* Get extended controls. Allocates the helpers array if needed. */
2117int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2118{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002119 struct v4l2_ctrl_helper helper[4];
2120 struct v4l2_ctrl_helper *helpers = helper;
Hans Verkuil09965172010-08-01 14:32:42 -03002121 int ret;
Hans Verkuilddac5c12011-06-10 05:43:34 -03002122 int i, j;
Hans Verkuil09965172010-08-01 14:32:42 -03002123
2124 cs->error_idx = cs->count;
2125 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
2126
2127 if (hdl == NULL)
2128 return -EINVAL;
2129
2130 if (cs->count == 0)
2131 return class_check(hdl, cs->ctrl_class);
2132
2133 if (cs->count > ARRAY_SIZE(helper)) {
Xi Wang5f0049b2012-04-06 09:32:36 -03002134 helpers = kmalloc_array(cs->count, sizeof(helper[0]),
2135 GFP_KERNEL);
Hans Verkuil09965172010-08-01 14:32:42 -03002136 if (helpers == NULL)
2137 return -ENOMEM;
2138 }
2139
Hans Verkuil37cd3b72011-06-07 04:40:04 -03002140 ret = prepare_ext_ctrls(hdl, cs, helpers);
2141 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03002142
2143 for (i = 0; !ret && i < cs->count; i++)
2144 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2145 ret = -EACCES;
2146
2147 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002148 int (*ctrl_to_user)(struct v4l2_ext_control *c,
2149 struct v4l2_ctrl *ctrl) = cur_to_user;
2150 struct v4l2_ctrl *master;
Hans Verkuil09965172010-08-01 14:32:42 -03002151
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002152 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03002153 continue;
2154
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002155 master = helpers[i].mref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03002156 cs->error_idx = i;
2157
2158 v4l2_ctrl_lock(master);
Hans Verkuilddac5c12011-06-10 05:43:34 -03002159
2160 /* g_volatile_ctrl will update the new control values */
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002161 if ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
2162 (master->has_volatiles && !is_cur_manual(master))) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03002163 for (j = 0; j < master->ncontrols; j++)
2164 cur_to_new(master->cluster[j]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03002165 ret = call_op(master, g_volatile_ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002166 ctrl_to_user = new_to_user;
Hans Verkuilddac5c12011-06-10 05:43:34 -03002167 }
2168 /* If OK, then copy the current (for non-volatile controls)
2169 or the new (for volatile controls) control values to the
2170 caller */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002171 if (!ret) {
2172 u32 idx = i;
2173
2174 do {
2175 ret = ctrl_to_user(cs->controls + idx,
2176 helpers[idx].ctrl);
2177 idx = helpers[idx].next;
2178 } while (!ret && idx);
2179 }
Hans Verkuil09965172010-08-01 14:32:42 -03002180 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002181 }
2182
2183 if (cs->count > ARRAY_SIZE(helper))
2184 kfree(helpers);
2185 return ret;
2186}
2187EXPORT_SYMBOL(v4l2_g_ext_ctrls);
2188
2189int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2190{
2191 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
2192}
2193EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
2194
2195/* Helper function to get a single control */
2196static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
2197{
2198 struct v4l2_ctrl *master = ctrl->cluster[0];
2199 int ret = 0;
Hans Verkuilddac5c12011-06-10 05:43:34 -03002200 int i;
Hans Verkuil09965172010-08-01 14:32:42 -03002201
2202 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2203 return -EACCES;
2204
2205 v4l2_ctrl_lock(master);
2206 /* g_volatile_ctrl will update the current control values */
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002207 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03002208 for (i = 0; i < master->ncontrols; i++)
2209 cur_to_new(master->cluster[i]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03002210 ret = call_op(master, g_volatile_ctrl);
Hans Verkuilddac5c12011-06-10 05:43:34 -03002211 *val = ctrl->val;
2212 } else {
2213 *val = ctrl->cur.val;
2214 }
Hans Verkuil09965172010-08-01 14:32:42 -03002215 v4l2_ctrl_unlock(master);
2216 return ret;
2217}
2218
2219int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
2220{
2221 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2222
2223 if (ctrl == NULL || !type_is_int(ctrl))
2224 return -EINVAL;
2225 return get_ctrl(ctrl, &control->value);
2226}
2227EXPORT_SYMBOL(v4l2_g_ctrl);
2228
2229int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2230{
2231 return v4l2_g_ctrl(sd->ctrl_handler, control);
2232}
2233EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
2234
2235s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
2236{
2237 s32 val = 0;
2238
2239 /* It's a driver bug if this happens. */
2240 WARN_ON(!type_is_int(ctrl));
2241 get_ctrl(ctrl, &val);
2242 return val;
2243}
2244EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
2245
2246
2247/* Core function that calls try/s_ctrl and ensures that the new value is
2248 copied to the current value on a set.
2249 Must be called with ctrl->handler->lock held. */
Hans Verkuile6402582011-06-14 10:56:42 -03002250static int try_or_set_cluster(struct v4l2_fh *fh,
2251 struct v4l2_ctrl *master, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03002252{
Hans Verkuil72d877c2011-06-10 05:44:36 -03002253 bool update_flag;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002254 int ret;
Hans Verkuil09965172010-08-01 14:32:42 -03002255 int i;
2256
2257 /* Go through the cluster and either validate the new value or
2258 (if no new value was set), copy the current value to the new
2259 value, ensuring a consistent view for the control ops when
2260 called. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002261 for (i = 0; i < master->ncontrols; i++) {
Hans Verkuil09965172010-08-01 14:32:42 -03002262 struct v4l2_ctrl *ctrl = master->cluster[i];
2263
2264 if (ctrl == NULL)
2265 continue;
2266
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002267 if (!ctrl->is_new) {
2268 cur_to_new(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03002269 continue;
2270 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002271 /* Check again: it may have changed since the
2272 previous check in try_or_set_ext_ctrls(). */
2273 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2274 return -EBUSY;
Hans Verkuil09965172010-08-01 14:32:42 -03002275 }
2276
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002277 ret = call_op(master, try_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03002278
2279 /* Don't set if there is no change */
Hans Verkuil72d877c2011-06-10 05:44:36 -03002280 if (ret || !set || !cluster_changed(master))
2281 return ret;
2282 ret = call_op(master, s_ctrl);
Hans Verkuil72d877c2011-06-10 05:44:36 -03002283 if (ret)
2284 return ret;
2285
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002286 /* If OK, then make the new values permanent. */
Hans Verkuil72d877c2011-06-10 05:44:36 -03002287 update_flag = is_cur_manual(master) != is_new_manual(master);
2288 for (i = 0; i < master->ncontrols; i++)
Hans Verkuilab892ba2011-06-07 06:47:18 -03002289 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
Hans Verkuil72d877c2011-06-10 05:44:36 -03002290 return 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002291}
2292
Hans Verkuile6402582011-06-14 10:56:42 -03002293/* Validate controls. */
2294static int validate_ctrls(struct v4l2_ext_controls *cs,
2295 struct v4l2_ctrl_helper *helpers, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03002296{
Hans Verkuile6402582011-06-14 10:56:42 -03002297 unsigned i;
Hans Verkuil09965172010-08-01 14:32:42 -03002298 int ret = 0;
2299
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002300 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03002301 for (i = 0; i < cs->count; i++) {
2302 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
2303
Hans Verkuile6402582011-06-14 10:56:42 -03002304 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03002305
2306 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2307 return -EACCES;
2308 /* This test is also done in try_set_control_cluster() which
2309 is called in atomic context, so that has the final say,
2310 but it makes sense to do an up-front check as well. Once
2311 an error occurs in try_set_control_cluster() some other
2312 controls may have been set already and we want to do a
2313 best-effort to avoid that. */
2314 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2315 return -EBUSY;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002316 ret = validate_new(ctrl, &cs->controls[i]);
2317 if (ret)
2318 return ret;
Hans Verkuil09965172010-08-01 14:32:42 -03002319 }
Hans Verkuile6402582011-06-14 10:56:42 -03002320 return 0;
2321}
Hans Verkuil09965172010-08-01 14:32:42 -03002322
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002323/* Obtain the current volatile values of an autocluster and mark them
2324 as new. */
2325static void update_from_auto_cluster(struct v4l2_ctrl *master)
2326{
2327 int i;
2328
2329 for (i = 0; i < master->ncontrols; i++)
2330 cur_to_new(master->cluster[i]);
2331 if (!call_op(master, g_volatile_ctrl))
2332 for (i = 1; i < master->ncontrols; i++)
2333 if (master->cluster[i])
2334 master->cluster[i]->is_new = 1;
2335}
2336
Hans Verkuile6402582011-06-14 10:56:42 -03002337/* Try or try-and-set controls */
2338static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2339 struct v4l2_ext_controls *cs,
2340 bool set)
2341{
2342 struct v4l2_ctrl_helper helper[4];
2343 struct v4l2_ctrl_helper *helpers = helper;
2344 unsigned i, j;
2345 int ret;
2346
2347 cs->error_idx = cs->count;
2348 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
2349
2350 if (hdl == NULL)
2351 return -EINVAL;
2352
2353 if (cs->count == 0)
2354 return class_check(hdl, cs->ctrl_class);
2355
2356 if (cs->count > ARRAY_SIZE(helper)) {
Xi Wang0a3475e2012-04-06 09:32:37 -03002357 helpers = kmalloc_array(cs->count, sizeof(helper[0]),
2358 GFP_KERNEL);
Hans Verkuile6402582011-06-14 10:56:42 -03002359 if (!helpers)
2360 return -ENOMEM;
2361 }
2362 ret = prepare_ext_ctrls(hdl, cs, helpers);
2363 if (!ret)
2364 ret = validate_ctrls(cs, helpers, set);
2365 if (ret && set)
2366 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03002367 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002368 struct v4l2_ctrl *master;
2369 u32 idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03002370
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002371 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03002372 continue;
2373
Hans Verkuil37cd3b72011-06-07 04:40:04 -03002374 cs->error_idx = i;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002375 master = helpers[i].mref->ctrl;
2376 v4l2_ctrl_lock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002377
Hans Verkuil2a863792011-01-11 14:45:03 -03002378 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03002379 for (j = 0; j < master->ncontrols; j++)
2380 if (master->cluster[j])
Hans Verkuil2a863792011-01-11 14:45:03 -03002381 master->cluster[j]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002382
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002383 /* For volatile autoclusters that are currently in auto mode
2384 we need to discover if it will be set to manual mode.
2385 If so, then we have to copy the current volatile values
2386 first since those will become the new manual values (which
2387 may be overwritten by explicit new values from this set
2388 of controls). */
2389 if (master->is_auto && master->has_volatiles &&
2390 !is_cur_manual(master)) {
2391 /* Pick an initial non-manual value */
2392 s32 new_auto_val = master->manual_mode_value + 1;
2393 u32 tmp_idx = idx;
2394
2395 do {
2396 /* Check if the auto control is part of the
2397 list, and remember the new value. */
2398 if (helpers[tmp_idx].ctrl == master)
2399 new_auto_val = cs->controls[tmp_idx].value;
2400 tmp_idx = helpers[tmp_idx].next;
2401 } while (tmp_idx);
2402 /* If the new value == the manual value, then copy
2403 the current volatile values. */
2404 if (new_auto_val == master->manual_mode_value)
2405 update_from_auto_cluster(master);
2406 }
2407
Hans Verkuil09965172010-08-01 14:32:42 -03002408 /* Copy the new caller-supplied control values.
Hans Verkuil2a863792011-01-11 14:45:03 -03002409 user_to_new() sets 'is_new' to 1. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002410 do {
2411 ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
2412 idx = helpers[idx].next;
2413 } while (!ret && idx);
Hans Verkuil09965172010-08-01 14:32:42 -03002414
2415 if (!ret)
Hans Verkuile6402582011-06-14 10:56:42 -03002416 ret = try_or_set_cluster(fh, master, set);
Hans Verkuil09965172010-08-01 14:32:42 -03002417
2418 /* Copy the new values back to userspace. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002419 if (!ret) {
2420 idx = i;
2421 do {
Hans Verkuiladf41b92011-07-05 06:56:37 -03002422 ret = new_to_user(cs->controls + idx,
Hans Verkuile6402582011-06-14 10:56:42 -03002423 helpers[idx].ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002424 idx = helpers[idx].next;
2425 } while (!ret && idx);
2426 }
2427 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002428 }
Hans Verkuil09965172010-08-01 14:32:42 -03002429
Hans Verkuil09965172010-08-01 14:32:42 -03002430 if (cs->count > ARRAY_SIZE(helper))
2431 kfree(helpers);
2432 return ret;
2433}
2434
2435int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2436{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002437 return try_set_ext_ctrls(NULL, hdl, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03002438}
2439EXPORT_SYMBOL(v4l2_try_ext_ctrls);
2440
Hans Verkuilab892ba2011-06-07 06:47:18 -03002441int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2442 struct v4l2_ext_controls *cs)
Hans Verkuil09965172010-08-01 14:32:42 -03002443{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002444 return try_set_ext_ctrls(fh, hdl, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002445}
2446EXPORT_SYMBOL(v4l2_s_ext_ctrls);
2447
2448int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2449{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002450 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03002451}
2452EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
2453
2454int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2455{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002456 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002457}
2458EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
2459
2460/* Helper function for VIDIOC_S_CTRL compatibility */
Hans Verkuilab892ba2011-06-07 06:47:18 -03002461static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
Hans Verkuil09965172010-08-01 14:32:42 -03002462{
2463 struct v4l2_ctrl *master = ctrl->cluster[0];
2464 int ret;
2465 int i;
2466
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002467 ret = validate_new_int(ctrl, val);
2468 if (ret)
2469 return ret;
2470
Hans Verkuil09965172010-08-01 14:32:42 -03002471 v4l2_ctrl_lock(ctrl);
2472
Hans Verkuil2a863792011-01-11 14:45:03 -03002473 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03002474 for (i = 0; i < master->ncontrols; i++)
2475 if (master->cluster[i])
Hans Verkuil2a863792011-01-11 14:45:03 -03002476 master->cluster[i]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002477
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002478 /* For autoclusters with volatiles that are switched from auto to
2479 manual mode we have to update the current volatile values since
2480 those will become the initial manual values after such a switch. */
2481 if (master->is_auto && master->has_volatiles && ctrl == master &&
2482 !is_cur_manual(master) && *val == master->manual_mode_value)
2483 update_from_auto_cluster(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002484 ctrl->val = *val;
Hans Verkuil2a863792011-01-11 14:45:03 -03002485 ctrl->is_new = 1;
Hans Verkuile6402582011-06-14 10:56:42 -03002486 ret = try_or_set_cluster(fh, master, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002487 *val = ctrl->cur.val;
2488 v4l2_ctrl_unlock(ctrl);
2489 return ret;
2490}
2491
Hans Verkuilab892ba2011-06-07 06:47:18 -03002492int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2493 struct v4l2_control *control)
Hans Verkuil09965172010-08-01 14:32:42 -03002494{
2495 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2496
2497 if (ctrl == NULL || !type_is_int(ctrl))
2498 return -EINVAL;
2499
Hans Verkuil7ebbc392011-06-07 04:50:31 -03002500 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2501 return -EACCES;
2502
Hans Verkuilab892ba2011-06-07 06:47:18 -03002503 return set_ctrl(fh, ctrl, &control->value);
Hans Verkuil09965172010-08-01 14:32:42 -03002504}
2505EXPORT_SYMBOL(v4l2_s_ctrl);
2506
2507int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2508{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002509 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
Hans Verkuil09965172010-08-01 14:32:42 -03002510}
2511EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2512
2513int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2514{
2515 /* It's a driver bug if this happens. */
2516 WARN_ON(!type_is_int(ctrl));
Hans Verkuilab892ba2011-06-07 06:47:18 -03002517 return set_ctrl(NULL, ctrl, &val);
Hans Verkuil09965172010-08-01 14:32:42 -03002518}
2519EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
Hans Verkuil6e239392011-06-07 11:13:44 -03002520
Hans de Goede3e3661492012-04-08 12:59:47 -03002521static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002522{
Hans de Goede3e3661492012-04-08 12:59:47 -03002523 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
2524
2525 if (ctrl == NULL)
2526 return -EINVAL;
2527
Hans Verkuil6e239392011-06-07 11:13:44 -03002528 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002529 list_add_tail(&sev->node, &ctrl->ev_subs);
Hans Verkuil6e239392011-06-07 11:13:44 -03002530 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
Hans Verkuil77068d32011-06-13 18:55:58 -03002531 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
Hans Verkuil6e239392011-06-07 11:13:44 -03002532 struct v4l2_event ev;
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002533 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
Hans Verkuil6e239392011-06-07 11:13:44 -03002534
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002535 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2536 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2537 fill_event(&ev, ctrl, changes);
Hans Verkuil77068d32011-06-13 18:55:58 -03002538 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -03002539 }
2540 v4l2_ctrl_unlock(ctrl);
Hans de Goede3e3661492012-04-08 12:59:47 -03002541 return 0;
Hans Verkuil6e239392011-06-07 11:13:44 -03002542}
Hans Verkuil6e239392011-06-07 11:13:44 -03002543
Hans de Goede3e3661492012-04-08 12:59:47 -03002544static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002545{
Hans de Goede3e3661492012-04-08 12:59:47 -03002546 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
2547
Hans Verkuil6e239392011-06-07 11:13:44 -03002548 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002549 list_del(&sev->node);
Hans Verkuil6e239392011-06-07 11:13:44 -03002550 v4l2_ctrl_unlock(ctrl);
2551}
Hans de Goede3e3661492012-04-08 12:59:47 -03002552
2553void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
2554{
2555 u32 old_changes = old->u.ctrl.changes;
2556
2557 old->u.ctrl = new->u.ctrl;
2558 old->u.ctrl.changes |= old_changes;
2559}
2560EXPORT_SYMBOL(v4l2_ctrl_replace);
2561
2562void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
2563{
2564 new->u.ctrl.changes |= old->u.ctrl.changes;
2565}
2566EXPORT_SYMBOL(v4l2_ctrl_merge);
2567
2568const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
2569 .add = v4l2_ctrl_add_event,
2570 .del = v4l2_ctrl_del_event,
2571 .replace = v4l2_ctrl_replace,
2572 .merge = v4l2_ctrl_merge,
2573};
2574EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
Hans Verkuile2ecb252012-02-02 08:20:53 -03002575
2576int v4l2_ctrl_log_status(struct file *file, void *fh)
2577{
2578 struct video_device *vfd = video_devdata(file);
2579 struct v4l2_fh *vfh = file->private_data;
2580
2581 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
2582 v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
2583 vfd->v4l2_dev->name);
2584 return 0;
2585}
2586EXPORT_SYMBOL(v4l2_ctrl_log_status);
Hans Verkuila26243b2012-01-27 16:18:42 -03002587
2588int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
2589 struct v4l2_event_subscription *sub)
2590{
2591 if (sub->type == V4L2_EVENT_CTRL)
Hans de Goede3e3661492012-04-08 12:59:47 -03002592 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
Hans Verkuila26243b2012-01-27 16:18:42 -03002593 return -EINVAL;
2594}
2595EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
2596
2597unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
2598{
2599 struct v4l2_fh *fh = file->private_data;
2600
2601 if (v4l2_event_pending(fh))
2602 return POLLPRI;
2603 poll_wait(file, &fh->wait, wait);
2604 return 0;
2605}
2606EXPORT_SYMBOL(v4l2_ctrl_poll);